--- MITgcm/tools/genmake2 2003/10/21 15:29:20 1.14 +++ MITgcm/tools/genmake2 2003/11/13 15:10:40 1.33 @@ -1,6 +1,6 @@ #!/bin/bash # -# $Header: /home/ubuntu/mnt/e9_copy/MITgcm/tools/genmake2,v 1.14 2003/10/21 15:29:20 edhill Exp $ +# $Header: /home/ubuntu/mnt/e9_copy/MITgcm/tools/genmake2,v 1.33 2003/11/13 15:10:40 adcroft Exp $ # # Makefile generator for MITgcm UV codes # created by cnh 03/98 @@ -46,14 +46,14 @@ PKG_GROUPS=$ROOTDIR"/pkg/pkg_groups" if test -r $PKG_GROUPS ; then cat $PKG_GROUPS | sed -e 's/#.*$//g' | sed -e 's/:/ : /g' > ./p1.tmp - cat ./p1.tmp | awk '(NF>2 && $2==":"){ print $0 }' > ./p2.tmp + cat ./p1.tmp | $AWK '(NF>2 && $2==":"){ print $0 }' > ./p2.tmp matched=0 for i in $PACKAGES ; do line=`grep "^ *$i" ./p2.tmp` RETVAL=$? if test "x$RETVAL" = x0 ; then matched=1 - replace=`echo $line | awk '{ $1=""; $2=""; print $0 }'` + replace=`echo $line | $AWK '{ $1=""; $2=""; print $0 }'` echo " replacing \"$i\" with: $replace" new_packages="$new_packages $replace" else @@ -72,7 +72,8 @@ tmp1=`uname`"_"`uname -m` tmp2=`echo $tmp1 | sed -e 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'` - PLATFORM=`echo $tmp2 | sed -e 's/i[3-6]86/ia32/' | sed -e 's/athlon/ia32/'` + tmp3=`echo $tmp2 | sed -e 's/power macintosh/ppc/'` + PLATFORM=`echo $tmp3 | sed -e 's/i[3-6]86/ia32/' | sed -e 's/athlon/ia32/'` OFLIST=`(cd $ROOTDIR/tools/build_options; ls | grep "^$PLATFORM")` echo " The platform appears to be: $PLATFORM" # if test "x$OFLIST" = x ; then @@ -103,8 +104,18 @@ CPP="cpp -traditional -P" fi + # makedepend is not always available + if test "x${MAKEDEPEND}" = x ; then + which makedepend >& /dev/null + RETVAL=$? + if test "x${RETVAL}" = x1 ; then + echo " makedepend was not found. Using xmakedpend instead." + MAKEDEPEND='$(TOOLSDIR)/xmakedepend' + fi + fi + # look for possible fortran compilers - tmp="$MITGCM_FC $FC g77 f77 pgf77 pgf95 ifc f90 f95 mpif77 mpf77 mpxlf95" + tmp="$MITGCM_FC $FC efc g77 f77 pgf77 pgf95 ifc f90 f95 mpif77 mpf77 mpxlf95" p_FC= for c in $tmp ; do rm -f ./hello.f ./hello @@ -121,6 +132,7 @@ p_FC="$p_FC $c" fi done + rm -f ./hello.f ./hello if test "x${p_FC}" = x ; then cat 1>&2 <0){ for(i=2;i<=NF;i++){nn++; print "PNAME["nn"]="$1"\nDNAME["nn"]="$i} }' \ + | $AWK 'BEGIN{nn=0;} (NF>0){ for(i=2;i<=NF;i++){nn++; print "PNAME["nn"]="$1"\nDNAME["nn"]="$i} }' \ > ./.pd_tmp source ./.pd_tmp rm -f ./.pd_tmp @@ -262,24 +274,84 @@ exit 1 } -#eh3 # This is the generic configuration. -#eh3 set LN = ( 'ln -s' ) -#eh3 set CPP = ( '/lib/cpp -P' ) -#eh3 set S64 = ( '$(TOOLSDIR)/set64bitConst.sh' ) -#eh3 set KPP = ( ) -#eh3 set FC = ( 'f77' ) -#eh3 set LINK = $FC -#eh3 set MAKEDEPEND = ( 'makedepend' ) -#eh3 set INCLUDES = ( -I. ) -#eh3 set FFLAGS = ( ) -#eh3 set FOPTIM = ( ) -#eh3 set CFLAGS = ( ) -#eh3 set KFLAGS1 = ( ) -#eh3 set KFLAGS2 = ( ) -#eh3 set LIBS = ( ) -#eh3 set KPPFILES = ( ) -#eh3 if (! $?NOOPTFILES ) set NOOPTFILES = ( ) -#eh3 if (! $?NOOPTFLAGS ) set NOOPTFLAGS = ( ) +# Build a CPP macro to automate calling C routines from FORTRAN +get_fortran_c_namemangling() { + default_nm="#define FC_NAMEMANGLE(X) X ## _" + + cat > genmake_test.c < genmake_test.log 2>&1 + RETVAL=$? + if test "x$RETVAL" != x0 ; then + cat genmake_test.log >> genmake_errors + FC_NAMEMANGLE=$default_nm + echo + echo "WARNING: C test compile fails -- please see \"genmake_errors\"" + echo "WARNING: We'll try to use: FC_NAMEMANGLE='$FC_NAMEMANGLE'" + echo "WARNING: Please contact if you need help here." + return 1 + fi + c_tcall=`nm genmake_test.o | grep tcall | cut -d ' ' -f 3` + RETVAL=$? + if test "x$RETVAL" != x0 ; then + FC_NAMEMANGLE=$default_nm + echo + echo "WARNING: The \"nm\" command failed." + echo "WARNING: We'll try to use: FC_NAMEMANGLE='$FC_NAMEMANGLE'" + echo "WARNING: Please contact if you need help here." + return 1 + fi + cat > genmake_tcomp.f < genmake_tcomp.log 2>&1 + RETVAL=$? + if test "x$RETVAL" != x0 ; then + cat genmake_tcomp.log >> genmake_errors + FC_NAMEMANGLE=$default_nm + echo + echo "WARNING: FORTRAN test compile fails -- please see \"genmake_errors\"" + echo "WARNING: We'll try to use: FC_NAMEMANGLE='$FC_NAMEMANGLE'" + echo "WARNING: Please contact if you need help here." + return 1 + fi + f_tcall=`nm genmake_tcomp.o | grep tcall | cut -d ' ' -f 3` + RETVAL=$? + if test "x$RETVAL" != x0 ; then + FC_NAMEMANGLE=$default_nm + echo + echo "WARNING: The \"nm\" command failed." + echo "WARNING: We'll try to use: FC_NAMEMANGLE='$FC_NAMEMANGLE'" + echo "WARNING: Please contact if you need help here." + return 1 + fi + + c_a=`echo $c_tcall | sed -e 's|tcall|Y Y|' | cut -d ' ' -f 1 | sed -e 's|Y||'` + f_a=`echo $f_tcall | sed -e 's|tcall|Y Y|' | cut -d ' ' -f 1 | sed -e 's|Y||'` + c_b=`echo $c_tcall | sed -e 's|tcall|Y Y|' | cut -d ' ' -f 2 | sed -e 's|Y||'` + f_b=`echo $f_tcall | sed -e 's|tcall|Y Y|' | cut -d ' ' -f 2 | sed -e 's|Y||'` + + nmangle="X" + if test "x$c_a" != "x$f_a" ; then + comm="echo x$f_a | sed -e 's|x$c_a||'" + a=`eval $comm` + nmangle="$a ## $nmangle" + fi + if test "x$c_b" != "x$f_b" ; then + comm="echo x$f_b | sed -e 's|x$c_b||'" + b=`eval $comm` + nmangle="$nmangle ## $b" + fi + + FC_NAMEMANGLE="#define FC_NAMEMANGLE(X) $nmangle" + + # cleanup the testing files + rm -f genmake_tcomp.* genmake_test.* +} # Set defaults here COMMANDL="$0 $@" @@ -290,7 +362,8 @@ KPP= FC= LINK= -DEFINES="-DWORDLENGTH=4" +# DEFINES="-DWORDLENGTH=4" +DEFINES= PACKAGES= ENABLE= DISABLE= @@ -311,6 +384,11 @@ NOOPTFILES= NOOPTFLAGS= +# DEFINES checked by test compilation +HAVE_SYSTEM= +HAVE_FDATE= +FC_NAMEMANGLE= + MODS= TOOLSDIR= SOURCEDIRS= @@ -319,6 +397,7 @@ PWD=`pwd` MAKE=make +AWK=awk THISHOSTNAME=`hostname` THISCWD=`pwd` THISDATE=`date` @@ -334,6 +413,17 @@ AUTODIFF_PKG_USED=f AD_OPTFILE= +TAF= +AD_TAF_FLAGS= +FTL_TAF_FLAGS= +SVD_TAF_FLAGS= +TAF_EXTRA= +TAMC= +AD_TAMC_FLAGS= +FTL_TAF_FLAGS= +SVD_TAMC_FLAGS= +TAMC_EXTRA= + # The following state can be set directly by command-line switches gm_s1="OPTFILE PDEPEND PDEFAULT MAKEFILE PLATFORM ROOTDIR MODS DISABLE ENABLE NOOPT" @@ -344,13 +434,15 @@ gm_s4="CFLAGS KFLAGS1 KFLAGS2 LIBS KPPFILES NOOPTFILES NOOPTFLAGS" gm_s5="TOOLSDIR SOURCEDIRS INCLUDEDIRS PWD MAKE THISHOSTNAME THISDATE MACHINE" gm_s6="EXECUTABLE EXEHOOK EXEDIR PACKAGES_CONF" +gm_s7="HAVE_SYSTEM HAVE_FDATE FC_NAMEMANGLE" # The following are all related to adjoint/tangent-linear stuff -gm_s7="AUTODIFF_PKG_USED AD_OPTFILE TAMC TAF DIFF_FLAGS AD_TAMC_FLAGS AD_TAF_FLAGS" -gm_s8="FTL_TAMC_FLAGS FTL_TAF_FLAGS SVD_TAMC_FLAGS SVD_TAF_FLAGS" -gm_s9="" +gm_s10="AUTODIFF_PKG_USED AD_OPTFILE TAMC TAF AD_TAMC_FLAGS AD_TAF_FLAGS" +gm_s11="FTL_TAMC_FLAGS FTL_TAF_FLAGS SVD_TAMC_FLAGS SVD_TAF_FLAGS" +gm_s12="TAF_EXTRA TAMC_EXTRA" -gm_state="COMMANDL $gm_s1 $gm_s2 $gm_s3 $gm_s4 $gm_s5 $gm_s6 $gm_s7 $gm_s8" +gm_state="COMMANDL $gm_s1 $gm_s2 $gm_s3 $gm_s4 $gm_s5 $gm_s6 $gm_s7" +gm_state="$gm_state $gm_s10 $gm_s11 $gm_s12" echo @@ -495,6 +587,16 @@ -ds | --ds) DUMPSTATE=t ;; + -taf_extra | --taf_extra) + ac_prev=TAF_EXTRA ;; + -taf_extra=* | --taf_extra=*) + TAF_EXTRA=$ac_optarg ;; + + -tamc_extra | --tamc_extra) + ac_prev=TAMC_EXTRA ;; + -tamc_extra=* | --tamc_extra=*) + TAMC_EXTRA=$ac_optarg ;; + -*) echo "Error: unrecognized option: "$ac_option usage @@ -629,6 +731,7 @@ 1) within the options file ("CPP=...") as specified by "-of=OPTFILE" 2) the "./genmake_local" file + 3) with the CPP environment variable EOF exit 1 @@ -639,6 +742,61 @@ MAKEDEPEND=makedepend fi + +printf "\n=== Checking system libraries ===\n" +echo -n " Do we have the system() command using $FC... " +cat > genmake_tcomp.f < genmake_tcomp.log 2>&1 +RETVAL=$? +if test "x$RETVAL" = x0 ; then + HAVE_SYSTEM=t + DEFINES="$DEFINES -DHAVE_SYSTEM" + echo "yes" +else + HAVE_SYSTEM= + echo "no" +fi +rm -f genmake_tcomp* + +echo -n " Do we have the fdate() command using $FC... " +cat > genmake_tcomp.f < genmake_tcomp.log 2>&1 +RETVAL=$? +if test "x$RETVAL" = x0 ; then + HAVE_FDATE=t + DEFINES="$DEFINES -DHAVE_FDATE" + echo "yes" +else + HAVE_FDATE= + echo "no" +fi +rm -f genmake_tcomp* + +echo " The name mangling convention for $FC is... " +#FC_NAMEMANGLE="#define FC_NAMEMANGLE(X) X ## _" +if test "x$FC_NAMEMANGLE" = x ; then + get_fortran_c_namemangling +fi +echo " '$FC_NAMEMANGLE'" +echo "$FC_NAMEMANGLE" > FC_NAMEMANGLE.h.template +cmp FC_NAMEMANGLE.h FC_NAMEMANGLE.h.template > /dev/null 2>&1 +RETVAL=$? +if test "x$RETVAL" != x0 ; then + mv -f FC_NAMEMANGLE.h.template FC_NAMEMANGLE.h +fi + + printf "\n=== Setting defaults ===\n" echo -n " Adding MODS directories: " for d in $MODS ; do @@ -721,7 +879,7 @@ # Strip the comments and then convert the dependency file into # two arrays: PNAME, DNAME cat $PDEPEND | sed -e 's/#.*$//g' \ - | awk 'BEGIN{nn=-1;} (NF>0){ for(i=2;i<=NF;i++){nn++; print "PNAME_"nn"="$1"\nDNAME_"nn"="$i}} END{print "nname="nn}' \ + | $AWK 'BEGIN{nn=-1;} (NF>0){ for(i=2;i<=NF;i++){nn++; print "PNAME_"nn"="$1"\nDNAME_"nn"="$i}} END{print "nname="nn}' \ > ./.pd_tmp RETVAL=$? if test ! "x${RETVAL}" = x0 ; then @@ -755,7 +913,7 @@ else echo " using PDEFAULT=\"$PDEFAULT\"" # Strip the comments and add all the names - def=`cat $PDEFAULT | sed -e 's/#.*$//g' | awk '(NF>0){print $0}'` + def=`cat $PDEFAULT | sed -e 's/#.*$//g' | $AWK '(NF>0){print $0}'` RETVAL=$? if test "x${RETVAL}" != x0 ; then echo -n "Error: can't parse default package list " @@ -886,24 +1044,6 @@ fi done -# This is compatible with the old genmake. The "DISABLE_*" flags -# need to be replaced by the "ALLOW_*" flags set below. -# -# echo -n " Setting package-specific CPP flags: " -# pkrm=( mom_fluxform mom_vecinv generic_advdiff ) -# pkrmdef=( -DDISABLE_MOM_FLUXFORM -DDISABLE_MOM_VECINV -DDISABLE_GENERIC_ADVDIFF -DDISABLE_DEBUGMODE ) -# echo -n " " -# i=0 -# while test $i -lt "${#pkrm[@]}" ; do -# echo "$PACKAGES" | grep "${pk[$i]}" > /dev/null 2>&1 -# RETVAL=$? -# if test "x$RETVAL" = x1 ; then -# DEFINES="$DEFINES ${pkdef[$i]}" -# echo -n " ${pkdef[$i]}" -# fi -# i=$(( $i + 1 )) -# done -# echo # Create a list of #define and #undef to enable/disable packages PACKAGES_DOT_H=PACKAGES_CONFIG.h @@ -935,24 +1075,8 @@ fi done if test "x$has_pack" = xf ; then - undef=`echo "ALLOW_$n" | awk '{print toupper($0)}'` + undef=`echo "ALLOW_$n" | $AWK '{print toupper($0)}'` echo "#undef $undef" >> $PACKAGES_DOT_H".tmp" -# DEFINES="$DEFINES -U$undef" - -#EH3 WARNING : This is an UGLY HACK that needs to be removed!!! - case $n in - mom_fluxform) - DEFINES="$DEFINES -DDISABLE_MOM_FLUXFORM" - ;; - mom_vecinv) - DEFINES="$DEFINES -DDISABLE_MOM_VECINV" - ;; - debug) - DEFINES="$DEFINES -DDISABLE_DEBUGMODE" - ;; - esac -#EH3 WARNING : This is an UGLY HACK that needs to be removed!!! - fi fi done @@ -961,7 +1085,7 @@ C Packages enabled by genmake: EOF for i in $PACKAGES ; do - def=`echo "ALLOW_$i" | awk '{print toupper($0)}'` + def=`echo "ALLOW_$i" | $AWK '{print toupper($0)}'` echo "#define $def" >> $PACKAGES_DOT_H".tmp" #eh3 DEFINES="$DEFINES -D$def" @@ -1018,8 +1142,8 @@ fi done -echo " Searching for CPP_OPTIONS.h in order to warn about the presence" -echo " of \"#define ALLOW_PKGNAME\"-type statements:" +echo " Searching for CPP_OPTIONS.h in order to warn about the presence" +echo " of \"#define ALLOW_PKGNAME\"-type statements:" CPP_OPTIONS= spaths=". $INCLUDEDIRS" for i in $spaths ; do @@ -1096,7 +1220,7 @@ for sf in $sfiles ; do if test ! -r ".links.tmp/$sf" ; then if test -f "$d/$sf" ; then - extn=`echo $sf | awk -F '.' '{print $NF}'` + extn=`echo $sf | $AWK -F '.' '{print $NF}'` touch .links.tmp/$sf deplist="$deplist $sf" case $extn in @@ -1150,6 +1274,11 @@ echo "# $0 $@" >> $MAKEFILE echo "# executed by:" >> $MAKEFILE echo "# $USER@${THISHOSTNAME}:${THISCWD}" >> $MAKEFILE + +EXE_AD=$EXECUTABLE"_ad" +EXE_FTL=$EXECUTABLE"_ftl" +EXE_SVD=$EXECUTABLE"_svd" + cat >>$MAKEFILE < \$@ clean: + -cat AD_CONFIG.template > AD_CONFIG.h -rm -rf *.o *.f *.p *.f90 *.mod ${RMFILES} work.{pc,pcl} Clean: @make clean @make cleanlinks - -rm -f Makefile.bak genmake_state genmake_optfile make.log + -rm -f Makefile.bak genmake_state genmake_*optfile make.log CLEAN: @make Clean -find \$(EXEDIR) -name "*.meta" -exec rm {} \; @@ -1273,7 +1406,7 @@ -find \$(EXEDIR) -name "fort.*" -exec rm {} \; -rm -f \$(EXECUTABLE) output.txt -Makefile: makefile +#eh3 Makefile: makefile makefile: ${0} $@ cleanlinks: @@ -1303,6 +1436,9 @@ TAMC = ${TAMC} TAF = ${TAF} +TAF_EXTRA = ${TAF_EXTRA} +TAMC_EXTRA = ${TAMC_EXTRA} + EOF ad_vars="AD_TAMC_FLAGS AD_TAF_FLAGS" @@ -1325,39 +1461,79 @@ echo -n " $i" >> $MAKEFILE done echo >> $MAKEFILE +rm -f ad_files cat >>$MAKEFILE < AD_CONFIG.h + @make \$(F77FILES) @make \$(AD_FLOW_FILES) cat \$(AD_FLOW_FILES) \$(AD_FILES) > ad_input_code.f - ad_taf_output.f: ad_input_code.f - \$(TAF) \$(AD_TAF_FLAGS) ad_input_code.f + \$(TAF) \$(AD_TAF_FLAGS) \$(TAF_EXTRA) ad_input_code.f cat ad_input_code_ad.f | sed -f adjoint_sed > ad_taf_output.f ad_taf: ad_taf_output.o \$(OBJFILES) - \$(LINK) -o ${EXECUTABLE} \$(FFLAGS) \$(FOPTIM) \$(OBJFILES) ad_taf_output.o \$(LIBS) - + \$(LINK) -o ${EXE_AD} \$(FFLAGS) \$(FOPTIM) \$(OBJFILES) ad_taf_output.o \$(LIBS) ad_tamc_output.f: ad_input_code.f - \$(TAMC) \$(AD_TAMC_FLAGS) ad_input_code.f - cat ad_input_code_ad.f | sed -f adjoint_sed > ad_taf_output.f + \$(TAMC) \$(AD_TAMC_FLAGS) \$(TAMC_EXTRA) ad_input_code.f + cat ad_input_code_ad.f | sed -f adjoint_sed > ad_tamc_output.f ad_tamc: ad_tamc_output.o \$(OBJFILES) - \$(LINK) -o ${EXECUTABLE} \$(FFLAGS) \$(FOPTIM) \$(OBJFILES) ad_tamc_output.o \$(LIBS) + \$(LINK) -o ${EXE_AD} \$(FFLAGS) \$(FOPTIM) \$(OBJFILES) ad_tamc_output.o \$(LIBS) + + +# ... FTL ... +ftlall: ftl_taf +ftltaf: ftl_taf_output.f +ftltamc: ftl_tamc_output.f + +ftl_input_code.f: \$(AD_FILES) \$(HEADERFILES) + cmp ftl_config.template AD_CONFIG.h || cat ftl_config.template > AD_CONFIG.h + @make \$(F77FILES) + @make \$(AD_FLOW_FILES) + cat \$(AD_FLOW_FILES) \$(AD_FILES) > ftl_input_code.f + +ftl_taf_output.f: ftl_input_code.f + \$(TAF) \$(FTL_TAF_FLAGS) \$(TAF_EXTRA) ftl_input_code.f + cat ftl_input_code_ftl.f | sed -f adjoint_sed > ftl_taf_output.f +ftl_taf: ftl_taf_output.o \$(OBJFILES) + \$(LINK) -o ${EXE_FTL} \$(FFLAGS) \$(FOPTIM) \$(OBJFILES) ftl_taf_output.o \$(LIBS) -flt_taf_output.f: ad_input_code.f - \$(TAF) \$(FTL_TAF_FLAGS) ad_input_code.f - cat ad_input_code_ad.f | sed -f adjoint_sed > flt_taf_output.f +ftl_tamc_output.f: ftl_input_code.f + \$(TAMC) \$(FTL_TAMC_FLAGS) \$(TAMC_EXTRA) ftl_input_code.f + cat ftl_input_code_ftl.f | sed -f adjoint_sed > ftl_tamc_output.f -flt_taf: flt_taf_output.o \$(OBJFILES) - \$(LINK) -o ${EXECUTABLE} \$(FFLAGS) \$(FOPTIM) \$(OBJFILES) flt_taf_output.o \$(LIBS) +ftl_tamc: ftl_tamc_output.o \$(OBJFILES) + \$(LINK) -o ${EXE_FTL} \$(FFLAGS) \$(FOPTIM) \$(OBJFILES) ftl_tamc_output.o \$(LIBS) +# ... SVD ... +svd: svd_taf +svd_taf_f: svd_taf_output.f + +svd_input_code.f: \$(SRCFILES) + cmp svd_config.template AD_CONFIG.h || cat svd_config.template > AD_CONFIG.h + @make \$(F77FILES) + @make \$(AD_FLOW_FILES) + cat \$(AD_FLOW_FILES) \$(AD_FILES) > svd_input_code.f + +svd_taf_output.f: svd_input_code.f + \$(TAF) \$(SVD_TAF_FLAGS) \$(TAF_EXTRA) svd_input_code.f + cat svd_input_code_ad.f | sed -f adjoint_sed > svd_taf_output.f + +svd_taf: svd_taf_output.o \$(OBJFILES) + \$(LINK) -o ${EXE_SVD} \$(FFLAGS) \$(FOPTIM) \$(OBJFILES) svd_taf_output.o \$(LIBS) + #========================================= @@ -1397,6 +1573,45 @@ printf "\n=== Done ===\n" +# Write the "template" files for the adjoint builds +cat >AD_CONFIG.template <ad_config.template <ftl_config.template <svd_config.template <