File: autogen.sh

package info (click to toggle)
mpich 5.0.0~b1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 251,904 kB
  • sloc: ansic: 1,323,408; cpp: 82,869; f90: 72,420; javascript: 40,763; perl: 28,243; sh: 19,447; python: 16,174; xml: 14,418; makefile: 9,494; fortran: 8,046; java: 4,635; pascal: 352; asm: 324; ruby: 176; awk: 27; lisp: 19; php: 8; sed: 4
file content (1104 lines) | stat: -rwxr-xr-x 33,239 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
#! /bin/sh
##
## Copyright (C) by Argonne National Laboratory
##     See COPYRIGHT in top-level directory
##

# Update all of the derived files
# For best performance, execute this in the top-level directory.
# There are some experimental features to allow it to be executed in
# subdirectories
#
# Eventually, we want to allow this script to be executed anywhere in the
# mpich tree.  This is not yet implemented.


warn() {
    echo "===> WARNING: $@"
}

error() {
    echo "===> ERROR:   $@"
}

echo_n() {
    # "echo -n" isn't portable, must portably implement with printf
    printf "%s" "$*"
}

########################################################################
## Checks to make sure we are running from the correct location
########################################################################

echo_n "Verifying the location of autogen.sh... "
if [ ! -d maint -o ! -s maint/version.m4 ] ; then
    echo "must execute at top level directory for now"
    exit 1
fi
# Set the SRCROOTDIR to be used later and avoid "cd ../../"-like usage.
SRCROOTDIR=$PWD
echo "done"

########################################################################
## Initialize variables to default values (possibly from the environment)
########################################################################

# Default choices
do_fortran=yes  # if no, skips patching libtool for Fortran
do_errmsgs=yes
do_getcvars=yes
do_f77=yes
do_f90=no  # we generate f90 binding in configure
do_f08=yes
do_cxx=yes
do_build_configure=yes
do_atdir_check=no
do_atver_check=yes
do_subcfg_m4=yes
do_hwloc=no
do_ofi=no
do_ucx=no
do_json=yes
do_yaksa=yes
do_test=yes
do_hydra=yes
do_romio=yes
do_pmi=yes
do_doc=no

yaksa_depth=2

do_quick=no
# Check -quick option. When enabled, skip as much as we can.
for arg in "$@" ; do
    if test $arg = "-quick"; then
        do_quick=yes
        do_hwloc=no
        do_json=no
        do_ofi=no
        do_ucx=no
        do_yaksa=no
        do_test=yes
        do_hydra=yes
        do_romio=no

        if test -e 'modules.tar.gz' -a ! -e 'modules/PREBUILT' ; then
            echo_n "Untaring modules.tar.gz... "
            tar xf modules.tar.gz
            echo "done"
        fi
    fi
done

# Allow MAKE to be set from the environment
MAKE=${MAKE-make}

# amdirs are the directories that make use of autoreconf
amdirs=". src/mpl src/pmi"

autoreconf_args="-if"
export autoreconf_args

########################################################################
## Utility functions
########################################################################

recreate_tmp() {
    rm -rf .tmp
    mkdir .tmp 2>&1 >/dev/null
}

# Assume Program's install-dir is <install-dir>/bin/<prog>.
# Given program name as the 1st argument,
# the install-dir is returned is returned in 2nd argument.
# e.g., ProgHomeDir libtoolize libtooldir.
ProgHomeDir() {
    prog=$1
    progpath="`which $prog`"
    progbindir="`dirname $progpath`"
    proghome=`(cd $progbindir/.. && pwd)`
    eval $2=$proghome
}

# checking and patching submodules
check_submodule_presence() {
    if test ! -f "$SRCROOTDIR/$1/configure.ac"; then
        error "Submodule $1 is not checked out."
        error "if you just git cloned this repository, run"
        error "    git submodule update --init"
        error "to checkout the submodules."
        exit 1
    fi
}

########################################################################
## prerequisite functions
########################################################################

autoconf=
set_autotools() {
    if test -z "$autoconf" ; then
        if [ -n "$autotoolsdir" ] ; then
            if [ -x $autotoolsdir/autoconf -a -x $autotoolsdir/autoheader ] ; then
                autoconf=$autotoolsdir/autoconf
                autoheader=$autotoolsdir/autoheader
                autoreconf=$autotoolsdir/autoreconf
                automake=$autotoolsdir/automake
                autom4te=$autotoolsdir/autom4te
                aclocal=$autotoolsdir/aclocal
                if [ -x "$autotoolsdir/glibtoolize" ] ; then
                    libtoolize=$autotoolsdir/glibtoolize
                else
                    libtoolize=$autotoolsdir/libtoolize
                fi

                AUTOCONF=$autoconf
                AUTOHEADER=$autoheader
                AUTORECONF=$autoreconf
                AUTOMAKE=$automake
                AUTOM4TE=$autom4te
                ACLOCAL=$aclocal
                LIBTOOLIZE=$libtoolize

                export AUTOCONF
                export AUTOHEADER
                export AUTORECONF
                export AUTOM4TE
                export AUTOMAKE
                export ACLOCAL
                export LIBTOOLIZE
            else
                echo "could not find executable autoconf and autoheader in $autotoolsdir"
                exit 1
            fi
        else
            autoconf=${AUTOCONF:-autoconf}
            autoheader=${AUTOHEADER:-autoheader}
            autoreconf=${AUTORECONF:-autoreconf}
            autom4te=${AUTOM4TE:-autom4te}
            automake=${AUTOMAKE:-automake}
            aclocal=${ACLOCAL:-aclocal}
            if test -z "${LIBTOOLIZE+set}" && ( glibtoolize --version ) >/dev/null 2>&1 ; then
                libtoolize=glibtoolize
            else
                libtoolize=${LIBTOOLIZE:-libtoolize}
            fi
        fi
    fi
}

externals=
set_externals() {
    if test -z "$externals" ; then
        #TODO: if necessary, run: git submodule update --init

        # external packages that require autogen.sh to be run for each of them
        externals="test/mpi"

        if [ "yes" = "$do_hydra" ] ; then
            externals="${externals} src/pm/hydra"
        fi

        if [ "yes" = "$do_romio" ] ; then
            externals="${externals} src/mpi/romio"
        fi

        if [ "yes" = "$do_pmi" ] ; then
            externals="${externals} src/pmi"
        fi

        if [ "yes" = "$do_hwloc" ] ; then
            check_submodule_presence modules/hwloc
            externals="${externals} modules/hwloc"
        fi

        if [ "yes" = "$do_ucx" ] ; then
            check_submodule_presence modules/ucx
            externals="${externals} modules/ucx"
        fi

        if [ "yes" = "$do_ofi" ] ; then
            check_submodule_presence modules/libfabric
            externals="${externals} modules/libfabric"
        fi

        if [ "yes" = "$do_json" ] ; then
            check_submodule_presence "modules/json-c"
            externals="${externals} modules/json-c"
        fi

        if [ "yes" = "$do_yaksa" ] ; then
            externals="${externals} src/mpi/datatype/typerep/yaksa"
        fi
    fi
}

check_python3() {
    echo_n "Checking for Python 3... "
    PYTHON=

    python_one_liner="import sys; print(sys.version_info[0])"
    PYTHON_PATH=`command -v python`
    if test "x$PYTHON_PATH" != x ; then
        version=`$PYTHON_PATH -c "$python_one_liner"`
        if test "$version" = 3 ; then
            PYTHON=$PYTHON_PATH
        fi
    fi
    if test "x$PYTHON" = x ; then
        PYTHON_PATH=`command -v python3`
        if test "x$PYTHON_PATH" != x ; then
            version=`$PYTHON_PATH -c "$python_one_liner"`
            if test "$version" = 3 ; then
                PYTHON=$PYTHON_PATH
            fi
        fi
    fi

    if test -z "$PYTHON" ; then
        echo "not found"
        exit 1
    else
        echo "$PYTHON"
    fi
}

set_PYTHON() {
    if test -z "$PYTHON" ; then
        check_python3
    fi
}

########################################################################
## Step functions
########################################################################

sync_external () {
    srcdir=$1
    destdir=$2

    echo "syncing '$srcdir' --> '$destdir'"

    # deletion prevents creating 'confdb/confdb' situation, also cleans
    # stray files that may have crept in somehow
    rm -rf "$destdir"
    cp -pPR "$srcdir" "$destdir"
}

fn_copy_confdb_etc() {
    # This used to be an optionally installed hook to help with git-svn
    # versions of the old SVN repo.  Now that we are using git, this is our
    # mechanism that replaces relative svn:externals paths, such as for
    # "confdb" and "mpl". The basic plan is to delete the destdir and then
    # copy all of the files, warts and all, from the source directory to the
    # destination directory.
    echo
    echo "####################################"
    echo "## Replicating confdb (and similar)"
    echo "####################################"
    echo

    confdb_dirs=
    confdb_dirs="${confdb_dirs} src/mpl/confdb"
    if test "$do_pmi" = "yes" ; then
        confdb_dirs="${confdb_dirs} src/pmi/confdb"
        if test "$do_quick" = "no" ; then
            sync_external src/mpl src/pmi/mpl
            confdb_dirs="${confdb_dirs} src/pmi/mpl/confdb"
        fi
    fi
    if test "$do_romio" = "yes" ; then
        confdb_dirs="${confdb_dirs} src/mpi/romio/confdb"
        if test "$do_quick" = "no" ; then
            sync_external src/mpl src/mpi/romio/mpl
            confdb_dirs="${confdb_dirs} src/mpi/romio/mpl/confdb"
        fi
    fi
    if test "$do_hydra" = "yes" ; then
        confdb_dirs="${confdb_dirs} src/pm/hydra/confdb"
        if test "$do_quick" = "no" ; then
            mkdir -p src/pm/hydra/modules
            sync_external src/mpl src/pm/hydra/modules/mpl
            sync_external src/pmi src/pm/hydra/modules/pmi
            sync_external modules/hwloc src/pm/hydra/modules/hwloc
            # remove .git directories to avoid confusing git clean
            rm -rf src/pm/hydra/modules/hwloc/.git
            confdb_dirs="${confdb_dirs} src/pm/hydra/modules/mpl/confdb"
            confdb_dirs="${confdb_dirs} src/pm/hydra/modules/pmi/confdb"
            confdb_dirs="${confdb_dirs} src/pm/hydra/modules/pmi/mpl/confdb"
        fi
    fi
    if test "$do_test" = "yes" ; then
        confdb_dirs="${confdb_dirs} test/mpi/confdb"
        confdb_dirs="${confdb_dirs} test/mpi/dtpools/confdb"
    fi

    # all the confdb directories, by various names
    for destdir in $confdb_dirs ; do
        sync_external confdb "$destdir"
    done

    # a couple of other random files
    if [ -f maint/version.m4 ] ; then
        cp -pPR maint/version.m4 src/pm/hydra/version.m4
        cp -pPR maint/version.m4 src/mpi/romio/version.m4
        cp -pPR maint/version.m4 src/pmi/version.m4
        cp -pPR maint/version.m4 test/mpi/version.m4
    fi

    # Now sanity check that some of the above sync was successful
    f="aclocal_cc.m4"
    for d in $confdb_dirs ; do
        if [ -f "$d/$f" ] ; then :
        else
            error "expected to find '$f' in '$d'"
            exit 1
        fi
    done
}

fn_maint_configure() {
    set_autotools
    echo
    echo "------------------------------------"
    echo "Initiating building required scripts"
    (cd maint && $autoconf && rm -rf autom4te*.cache && ./configure)
    echo "Done building required scripts"
    echo "------------------------------------"
    echo
}

fn_errmsgs() {
    if test ! -x maint/extracterrmsgs ; then
        fn_maint_configure
    fi

    echo_n "Extracting error messages... "
    rm -rf .tmp
    rm -f .err
    rm -f unusederr.txt
    maint/extracterrmsgs -careful=unusederr.txt \
        -skip=src/util/multichannel/mpi.c `cat maint/errmsgdirs` > \
        .tmp 2>.err
    # (error here is ok)
    echo "done"

    update_errdefs=yes
    if [ -s .err ] ; then
        cat .err
        rm -f .err2
        grep -v "Warning:" .err > .err2
        if [ -s .err2 ] ; then
            warn "Because of errors in extracting error messages, the file"
            warn "src/mpi/errhan/defmsg.h was not updated."
            error "Error message files in src/mpi/errhan were not updated."
            rm -f .tmp .err .err2
            exit 1
        fi
        rm -f .err .err2
    else
        # In case it exists but has zero size
        rm -f .err
    fi
    if [ -s unusederr.txt ] ; then
        warn "There are unused error message texts in src/mpi/errhan/errnames.txt"
        warn "See the file unusederr.txt for the complete list"
    fi
    if [ -s .tmp -a "$update_errdefs" = "yes" ] ; then
        mv .tmp src/mpi/errhan/defmsg.h
    fi
    if [ ! -s src/mpi/errhan/defmsg.h ] ; then
        error "Unable to extract error messages"
        exit 1
    fi
}

fn_getcvars() {
    echo_n "Extracting control variables (cvar) ... "
    if test ! -x maint/extractcvars ; then
        fn_maint_configure
    fi

    if ./maint/extractcvars ; then
        echo "done"
    else
        echo "failed"
        error "unable to extract control variables"
        exit 1
    fi
}

fn_maint_version() {
    set_autotools
    # build a substitute maint/Version script now that we store the single copy of
    # this information in an m4 file for autoconf's benefit
    echo_n "Generating a helper maint/Version... "
    if $autom4te -l M4sugar maint/Version.base.m4 > maint/Version ; then
        echo "done"
    else
        echo "error"
        error "unable to correctly generate maint/Version shell helper"
    fi
}

fn_update_README() {
    if test ! -f ./maint/Version ; then
        fn_maint_version
    fi

    echo_n "Updating the README... "

    # import MPICH_VERSION and LIBFABRIC_VERSION
    . ./maint/Version

    if [ -f README.vin ] ; then
        sed -e "s/%VERSION%/${MPICH_VERSION}/g" -e "s/%LIBFABRIC_VERSION%/${LIBFABRIC_VERSION}/g" README.vin > README
        echo "done"
    else
        echo "error"
        error "README.vin file not present, unable to update README version number (perhaps we are running in a release tarball source tree?)"
    fi
}

fn_gen_subcfg_m4() {
    echo_n "Creating subsys_include.m4... "
    ./maint/gen_subcfg_m4
    echo "done"
}

fn_romio_glue() {
    echo_n "Building ROMIO glue code... "
    ( cd src/glue/romio && chmod a+x ./all_romio_symbols && ./all_romio_symbols ../../mpi/romio/include/mpio.h.in )
    echo "done"
}

fn_abi() {
    ($PYTHON maint/gen_abi.py)
}

fn_f77() {
    set_PYTHON
    echo_n "Building Fortran 77 interface... "
    $PYTHON maint/gen_binding_f77.py
    echo "done"
}

fn_f90() {
    echo_n "Building Fortran 90 interface... "
    $PYTHON maint/gen_binding_f90.py
    echo "done"
}

fn_f08() {
    echo_n "Building Fortran 08 interface... "
    $PYTHON maint/gen_binding_f08.py
    echo "done"
}

fn_cxx() {
    echo_n "Building C++ interface... "
    ( cd src/binding/cxx && chmod a+x ./buildiface &&
        ./buildiface -nosep -initfile=./cxx.vlist )
    echo "done"
}

fn_ch4_api() {
    set_PYTHON
    echo_n "generating ch4 API boilerplates... "
    $PYTHON ./maint/gen_ch4_api.py
}

fn_gen_coll() {
    set_PYTHON
    echo_n "generating Collective functions..."
    $PYTHON maint/gen_coll.py
    echo "done"
}

fn_gen_binding_c() {
    set_PYTHON
    echo_n "generating MPI C functions..."
    _opt=
    if test "$do_quick" = "yes"; then
        _opt="$_opt -single-source"
    fi
    if test "$do_doc" = "yes"; then
        _opt="$_opt -output-mansrc"
    fi
    $PYTHON maint/gen_binding_c.py $_opt
    echo "done"
}

fn_json_gen() {
    echo_n "generating json char arrays... "
    ./maint/tuning/coll/json_gen.sh
    echo "done"
}

# internal
_patch_libtool() {
    _file=$1
    _patch=$2
    if test -f "$_file" ; then
        echo_n "Patching $_file with $_patch..."
        patch -N -s -l $_file maint/patches/optional/confdb/$_patch && :
        if [ $? -eq 0 ] ; then
            # Remove possible leftovers, which don't imply a failure
            rm -f $_file.orig
            # updates _patch_successful for return
            _patch_successful=yes
            echo "done"
        else
            echo "failed"
        fi
    fi
}

autoreconf_amdir() {
    _dir=$1
    if [ -d "$_dir" -o -L "$_dir" ] ; then
        echo "------------------------------------------------------------------------"
        echo "running $autoreconf in $_dir"
        (cd $_dir && $autoreconf $autoreconf_args) || exit 1

        # Patching ltmain.sh and libtool.m4
        # This works with libtool versions 2.4 - 2.4.2.
        # Older versions are not supported to build mpich.
        # Newer versions should have this patch already included.

        _patch_successful=no

        _patch_libtool $_dir/confdb/ltmain.sh  intel-compiler.patch
        _patch_libtool $_dir/confdb/ltmain.sh  nagfor.patch
        _patch_libtool $_dir/confdb/libtool.m4 sys_lib_dlsearch_path_spec.patch
        _patch_libtool $_dir/confdb/libtool.m4 big-sur.patch
        _patch_libtool $_dir/confdb/libtool.m4 hpc-sdk.patch
        if test "$do_fortran" = "yes" ; then
            _patch_libtool $_dir/confdb/libtool.m4 darwin-ifort.patch
            _patch_libtool $_dir/confdb/libtool.m4 oracle-fort.patch
            _patch_libtool $_dir/confdb/libtool.m4 flang.patch
            _patch_libtool $_dir/confdb/libtool.m4 arm-compiler.patch
            _patch_libtool $_dir/confdb/libtool.m4 ibm-xlf.patch
        fi

        if test "$_patch_successful" = "yes" ; then
            (cd $_dir && $autoconf -f) || exit 1
            # Reset timestamps to avoid confusing make
            touch -r $_dir/confdb/ltversion.m4 $_dir/confdb/ltmain.sh $_dir/confdb/libtool.m4
        fi
    fi
}

autogen_external() {
    _dir=$1
    if [ -d "$_dir" -o -L "$_dir" ] ; then
        echo "------------------------------------------------------------------------"
        echo "running third-party initialization in $_dir"
        if test "$_dir" = "src/mpi/datatype/typerep/yaksa" -a -n "$yaksa_depth" ; then
            (cd $_dir && ./autogen.sh --pup-max-nesting=$yaksa_depth) || exit 1
        else
            (cd $_dir && ./autogen.sh) || exit 1
        fi
    else
        error "external directory $_dir missing"
        exit 1
    fi
}

fn_build_configure() {
    set_autotools
    if [ "$do_build_configure" = "yes" ] ; then
        set_externals
        for external in $externals ; do
            autogen_external $external
        done

        for amdir in $amdirs ; do
            autoreconf_amdir $amdir
        done
    fi
}

########################################################################
## functions for checking prerequisites
########################################################################

fn_check_autotools() {
    set_autotools
    ProgHomeDir $autoconf   autoconfdir
    ProgHomeDir $automake   automakedir
    ProgHomeDir $libtoolize libtooldir

    echo_n "Checking if autotools are in the same location... "
    if [ "$autoconfdir" = "$automakedir" -a "$autoconfdir" = "$libtooldir" ] ; then
        same_atdir=yes
        echo "yes, all in $autoconfdir"
    else
        same_atdir=no
        echo "no"
        echo "	autoconf is in $autoconfdir"
        echo "	automake is in $automakedir"
        echo "	libtool  is in $libtooldir"
        # Emit a big warning message if $same_atdir = no.
        warn "Autotools are in different locations. In rare occasion,"
        warn "resulting configure or makefile may fail in some unexpected ways."
    fi

    ########################################################################
    ## Check if autoreconf can be patched to work
    ## when autotools are not in the same location.
    ## This test needs to be done before individual tests of autotools
    ########################################################################

    # If autotools are not in the same location, override autoreconf appropriately.
    if [ "$same_atdir" != "yes" ] ; then
        if [ -z "$libtooldir" ] ; then
            ProgHomeDir $libtoolize libtooldir
        fi
        libtoolm4dir="$libtooldir/share/aclocal"
        echo_n "Checking if $autoreconf accepts -I $libtoolm4dir... "
        new_autoreconf_works=no
        if [ -d "$libtoolm4dir" -a -f "$libtoolm4dir/libtool.m4" ] ; then
            recreate_tmp
            cat >.tmp/configure.ac <<_EOF
AC_INIT(foo,1.0)
AC_PROG_LIBTOOL
AC_OUTPUT
_EOF
            AUTORECONF="$autoreconf -I $libtoolm4dir"
            if (cd .tmp && $AUTORECONF -ivf >/dev/null 2>&1) ; then
                new_autoreconf_works=yes
            fi
            rm -rf .tmp
        fi
        echo "$new_autoreconf_works"
        # If autoreconf accepts -I <libtool's m4 dir> correctly, use -I.
        # If not, run libtoolize before autoreconf (i.e. for autoconf <= 2.63)
        # This test is more general than checking the autoconf version.
        if [ "$new_autoreconf_works" != "yes" ] ; then
            echo_n "Checking if $autoreconf works after an additional $libtoolize step... "
            new_autoreconf_works=no
            recreate_tmp
            # Need AC_CONFIG_
            cat >.tmp/configure.ac <<_EOF
AC_INIT(foo,1.0)
AC_CONFIG_AUX_DIR([m4])
AC_CONFIG_MACRO_DIR([m4])
AC_PROG_LIBTOOL
AC_OUTPUT
_EOF
            cat >.tmp/Makefile.am <<_EOF
ACLOCAL_AMFLAGS = -I m4
_EOF
            AUTORECONF="eval $libtoolize && $autoreconf"
            if (cd .tmp && $AUTORECONF -ivf >u.txt 2>&1) ; then
                new_autoreconf_works=yes
            fi
            rm -rf .tmp
            echo "$new_autoreconf_works"
        fi
        if [ "$new_autoreconf_works" = "yes" ] ; then
            export AUTORECONF
            autoreconf="$AUTORECONF"
        else
            # Since all autoreconf workarounds do not work, we need
            # to require all autotools to be in the same directory.
            do_atdir_check=yes
            error "Since none of the autoreconf workaround works"
            error "and autotools are not in the same directory, aborting..."
            error "Updating autotools or putting all autotools in the same location"
            error "may resolve the issue."
            exit 1
        fi
    fi

    if test $do_quick = "yes" ; then
        : # skip autotool versions check in quick mode (since it is too slow)
    else
        ########################################################################
        ## Verify autoconf version
        ########################################################################

        echo_n "Checking for autoconf version... "
        recreate_tmp
        ver=2.69
        cat > .tmp/configure.ac<<EOF
AC_INIT
AC_PREREQ($ver)
AC_OUTPUT
EOF
        if (cd .tmp && $autoreconf $autoreconf_args >autoreconf.out 2>autoreconf.err) ; then
            echo ">= $ver"
        else
            echo "bad autoconf installation"
            echo "--- autoreconf diagnostics ---"
            $(cat autoreconf.err)
            echo "--- autoreconf diagnostics ---"
            cat <<EOF
You either do not have autoconf in your path or it is too old (version
$ver or higher required). You may be able to use

    autoconf --version

Unfortunately, there is no standard format for the version output and
it changes between autotools versions.  In addition, some versions of
autoconf choose among many versions and provide incorrect output).
EOF
            exit 1
        fi


        ########################################################################
        ## Verify automake version
        ########################################################################

        echo_n "Checking for automake version... "
        recreate_tmp
        ver=1.15
        cat > .tmp/configure.ac<<EOF
AC_INIT(testver,1.0)
AC_CONFIG_AUX_DIR([m4])
AC_CONFIG_MACRO_DIR([m4])
m4_ifdef([AM_INIT_AUTOMAKE],,[m4_fatal([AM_INIT_AUTOMAKE not defined])])
AM_INIT_AUTOMAKE([$ver foreign])
AC_MSG_RESULT([A message])
AC_OUTPUT([Makefile])
EOF
    cat <<EOF >.tmp/Makefile.am
ACLOCAL_AMFLAGS = -I m4
EOF
        if [ ! -d .tmp/m4 ] ; then mkdir .tmp/m4 >/dev/null 2>&1 ; fi
        if (cd .tmp && $autoreconf $autoreconf_args >/dev/null 2>&1 ) ; then
            echo ">= $ver"
        else
            echo "bad automake installation"
            cat <<EOF
You either do not have automake in your path or it is too old (version
$ver or higher required). You may be able to use

    automake --version

Unfortunately, there is no standard format for the version output and
it changes between autotools versions.  In addition, some versions of
autoconf choose among many versions and provide incorrect output).
EOF
            exit 1
        fi


        ########################################################################
        ## Verify libtool version
        ########################################################################

        echo_n "Checking for libtool version... "
        recreate_tmp
        ver=2.4.4
        cat <<EOF >.tmp/configure.ac
AC_INIT(testver,1.0)
AC_CONFIG_AUX_DIR([m4])
AC_CONFIG_MACRO_DIR([m4])
m4_ifdef([LT_PREREQ],,[m4_fatal([LT_PREREQ not defined])])
LT_PREREQ($ver)
LT_INIT()
AC_MSG_RESULT([A message])
EOF
        cat <<EOF >.tmp/Makefile.am
ACLOCAL_AMFLAGS = -I m4
EOF
        if [ ! -d .tmp/m4 ] ; then mkdir .tmp/m4 >/dev/null 2>&1 ; fi
        if (cd .tmp && $autoreconf $autoreconf_args >/dev/null 2>&1 ) ; then
            echo ">= $ver"
        else
            echo "bad libtool installation"
            cat <<EOF
You either do not have libtool in your path or it is too old
(version $ver or higher required). You may be able to use

    libtool --version

Unfortunately, there is no standard format for the version output and
it changes between autotools versions.  In addition, some versions of
autoconf choose among many versions and provide incorrect output).
EOF
            exit 1
        fi
    fi
}

fn_check_bash_find_patch_xargs() {
    echo_n "Checking for bash... "
    if test "`command -v bash 2>&1 > /dev/null ; echo $?`" = "0" ;then
        echo "done"
    else
        echo "bash not found" ;
        exit 1;
    fi

    echo_n "Checking for UNIX find... "
    find ./maint -name 'configure.ac' > /dev/null 2>&1
    if [ $? = 0 ] ; then
        echo "done"
    else
        echo "not found (error)"
        exit 1
    fi

    echo_n "Checking for UNIX patch... "
    patch -v > /dev/null 2>&1
    if [ $? = 0 ] ; then
        echo "done"
    else
        echo "not found (error)"
        exit 1
    fi

    echo_n "Checking if xargs rm -rf works... "
    if [ -d "`find ./maint -name __random_dir__`" ] ; then
        error "found a directory named __random_dir__"
        exit 1
    else
        mkdir ./maint/__random_dir__
        find ./maint -name __random_dir__ | xargs rm -rf > /dev/null 2>&1
        if [ $? = 0 ] ; then
            echo "yes"
        else
            echo "no (error)"
            rm -rf ./maint/__random_dir__
            exit 1
        fi
    fi
}

# end of utility functions
#-----------------------------------------------------------------------

########################################################################
## Read the command-line arguments
########################################################################

for arg in "$@" ; do
    case $arg in 
        -quick)
            ;;
	-echo)
	    set -x
	    ;;
	
	-atdircheck=*)
	    val=`echo X$arg | sed -e 's/^X-atdircheck=//'`
            case $val in
		yes|YES|true|TRUE|1) do_atdir_check=yes ;;
		no|NO|false|FALSE|0) do_atdir_check=no ;;
		*) warn "unknown option: $arg."
            esac
            ;;

	-atvercheck=*)
            val=`echo X$arg | sed -e 's/^X-atvercheck=//'`
            case $val in
		yes|YES|true|TRUE|1) do_atver_check=yes ;;
		no|NO|false|FALSE|0) do_atver_check=no ;;
		*) warn "unknown option: $arg."
            esac
            ;;

	-yaksa-depth=*)
            val=`echo X$arg | sed -e 's/^X-yaksa-depth=//'`
            yaksa_depth=$val
            ;;

	-do=*|--do=*)
	    opt=`echo A$arg | sed -e 's/^A--*do=//'`
            if type fn_$opt | grep -q 'function' ; then
                echo Running step $opt...
                fn_$opt
                exit 0
            else
		echo "-do=$opt is unrecognized"
		exit 1
            fi
	    ;;

        -verbose-autoreconf|--verbose-autoreconf)
            autoreconf_args="-vif"
            export autoreconf_args
            ;;

	-with-autotools=*|--with-autotools=*)
	    autotoolsdir=`echo "A$arg" | sed -e 's/.*=//'`
	    ;;

        -without-*|--without-*)
            opt=`echo A$arg | sed -e 's/^A--*without-//'`
            var=do_$opt
            eval $var=no
            case "$opt" in
                fortran)
                    do_f77=no
                    do_f90=no
                    do_f08=no
                    ;;
            esac
            ;;

        -with-*|--with-*)
            opt=`echo A$arg | sed -e 's/^A--*with-//'`
            var=do_$opt
            eval $var=yes
            case "$opt" in
                f77 | f90 | f08)
                    do_fortran=yes
                    ;;
            esac
            ;;

	-help|--help|-usage|--usage)
	    cat <<EOF
   ./autogen.sh [ --with-autotools=dir ] \\
                [ -atdircheck=[yes|no] ] \\
                [ -atvercheck=[yes|no] ] \\
                [ --verbose-autoreconf ] \\
                [ --do=stepname ] [ -distrib ]
    Update the files in the MPICH build tree.  This file builds the 
    configure files, creates the Makefile.in files, extracts the error
    messages.

    You can use --with-autotools=dir to specify a directory that
    contains alternate autotools.

    -atdircheck=[yes|no] specifies the enforcement of all autotools
    should be installed in the same directory.

    -atvercheck=[yes|no] specifies if the check for the version of 
    autotools should be carried out.

    -distrib creates a distribution version of the Makefile.in files (no
    targets for updating the Makefile.in from Makefile.sm or rebuilding the
    autotools targets).  This does not create the configure files because
    some of those depend on rules in the Makefile.in in that directory.  
    Thus, to build all of the files for a distribution, run autogen.sh
    twice, as in 
         autogen.sh && autogen.sh -distrib

    -quick skips most of the modules autoconf. This is used when modules
    are prebuilt and used specifically in CI testing to accelerate the build.
EOF
	    exit 1
	    ;;

	*)
	    echo "unknown argument $arg"
	    exit 1
	    ;;

    esac
done

########################################################################
## Check for the location of autotools
########################################################################

if test "$do_quick" = "no" ; then
    fn_check_autotools
else
    set_autotools
fi
fn_check_bash_find_patch_xargs
check_python3

########################################################################
## Setup external packages
########################################################################
set_externals

########################################################################
## duplicating confdb, mpl, version.m4 etc.
########################################################################
fn_copy_confdb_etc

########################################################################
## Building maint/Version
########################################################################
fn_maint_version

########################################################################
## Building the README
########################################################################
fn_update_README

set -e

########################################################################
## Building subsys_include.m4
########################################################################
if [ "X$do_subcfg_m4" = Xyes ] ; then
    fn_gen_subcfg_m4
fi


########################################################################
## Building ROMIO glue code
########################################################################
fn_romio_glue

########################################################################
## Building Collective top-level code
########################################################################
fn_gen_coll

########################################################################
## Building C interfaces
########################################################################
fn_gen_binding_c

########################################################################
## Building non-C interfaces
########################################################################

# Create the bindings if necessary 
fn_abi

if [ $do_f77 = "yes" ] ; then
    fn_f77
else
    touch src/binding/fortran/mpif_h/mpif.h.in
fi
if [ $do_f90 = "yes" ] ; then
    fn_f90
fi
if [ $do_f08 = "yes" ] ; then
    fn_f08
else
    touch src/binding/fortran/use_mpi_f08/mpi_f08_compile_constants.f90.in
fi

if [ $do_cxx = "yes" ] ; then
    fn_cxx
fi

########################################################################
## Extract error messages
########################################################################
if [ $do_errmsgs = "yes" ] ; then
    fn_errmsgs
fi


########################################################################
## Build required scripts
########################################################################
fn_maint_configure

# new parameter code
if test "$do_getcvars" = "yes" ; then
    fn_getcvars
fi

########################################################################
## Running autotools on non-simplemake directories
########################################################################
fn_build_configure

fn_ch4_api

fn_json_gen