Index: LMDZ5/branches/testing/makelmdz
===================================================================
--- LMDZ5/branches/testing/makelmdz	(revision 2220)
+++ LMDZ5/branches/testing/makelmdz	(revision 2298)
@@ -77,5 +77,5 @@
 
 CPP_KEY="" 
-INCLUDE='-I$(LIBF)/grid -I$(LIBF)/bibio -I$(LIBF)/filtrez -I. '
+INCLUDE='-I$(LIBF)/grid -I$(LIBF)/misc -I$(LIBF)/filtrez -I. '
 LIB=""
 adjnt=""
@@ -84,5 +84,6 @@
 PARA_LD=""
 EXT_SRC=""
-
+#src_dirs: directories containing source files 
+src_dirs="grid misc" 
 ########################################################################
 # lecture des options
@@ -106,4 +107,7 @@
           true        : (obsolete; for backward compatibility) use ORCHIDEE tag 1.9-1.9.6
 [-chimie INCA/false]       : with INCA chemistry model or without (default: false)
+[-cosp true/false]    : compile with/without cosp package (default: false)
+[-sisvat true/false]  : compile with/without sisvat package (default: false)
+[-rrtm true/false]    : compile with/without rrtm package (default: false)
 [-parallel none/mpi/omp/mpi_omp] : parallelism (default: none) : mpi, openmp or mixted mpi_openmp
 [-g GRI]                   : grid configuration in dyn3d/GRI_xy.h  (default: reg, inclues a zoom)
@@ -186,5 +190,5 @@
 
       "-ext_src")
-	  EXT_SRC=$2 ; shift ; shift ;;
+	  EXT_SRC=$2 ; src_dirs="$src_dirs $EXT_SRC" ; shift ; shift ;;
 
       "-arch")
@@ -280,12 +284,24 @@
 for inc in $archfileline ; do INCLUDE="$INCLUDE `echo $inc | grep '\-I'`" ; done
 
+phys_root=$physique
+if [[ "${physique:0:3}" == "lmd" ]] ; then phys_root=lmd ; fi
+if [[ "${physique:0:4}" == "mars" ]] ; then phys_root=mars ; fi
+if [[ "${physique:0:3}" == "std" ]] ; then phys_root=std ; fi
+if [[ "${physique:0:5}" == "venus" ]] ; then phys_root=venus ; fi
+if [[ "${physique:0:5}" == "titan" ]] ; then phys_root=titan ; fi
+if [[ "${physique:0:3}" == "mar" ]] ; then phys_root=mar ; fi
+if [[ "${physique:0:3}" == "dev" ]] ; then phys_root=dev ; fi
+
 if [[ "$physique" != "nophys" ]]
 then
    #We'll use some physics
+   src_dirs="$src_dirs phy$physique"
+   LIBPHY='$(LIBO)/libphy'"$physique"'.a'
+   lib_phy='-lphy'"$physique"
    CPP_KEY="$CPP_KEY CPP_PHYS"
-   if [[ "${physique:0:3}" == "lmd" ]]
-   then
-   #For lmd physics, default planet type is Earth
-   CPP_KEY="$CPP_KEY CPP_EARTH"
+   if [[ $phys_root == "lmd" ]]
+      then
+      #For lmd physics, default planet type is Earth
+      CPP_KEY="$CPP_KEY CPP_EARTH"
    fi
 fi
@@ -316,5 +332,5 @@
       echo "You should use option \"-mem\"."
       exit 1
-  fi     
+  fi
 fi
 
@@ -412,5 +428,6 @@
 then
    CPP_KEY="$CPP_KEY CPP_COSP"
-   COSP_PATH="$LIBFGCM/cosp"
+   COSP_PATH="$LIBFGCM/phylmd/cosp"
+   src_dirs="$src_dirs cosp"
 #   LIB="${LIB} -l${LIBPREFIX}cosp"
    opt_dep="$opt_dep cosp"
@@ -422,4 +439,5 @@
 then
    CPP_KEY="$CPP_KEY CPP_SISVAT"
+   src_dirs="$src_dirs phy${physique}/sisvat"
 fi
 
@@ -427,4 +445,5 @@
 then
    CPP_KEY="$CPP_KEY CPP_RRTM"
+   src_dirs="$src_dirs phy${physique}/rrtm"
 fi
 
@@ -489,4 +508,12 @@
 
 if (( $dimc == 3 )) ; then
+   src_dirs="$src_dirs $filtre dyn3d_common dyn3d${FLAG_PARA}"
+   if [[ $physique != "nophys" ]] ; then
+     src_dirs="$src_dirs dynlonlat_phylonlat dynlonlat_phylonlat/phy${phys_root}"
+     libdyn_phy="-ldynlonlat_phylonlat"
+     LIBDYN_PHYS='$(LIBO)/libdynlonlat_phylonlat.a'
+     INCLUDE="$INCLUDE "'-I$(LIBF)/dynlonlat_phylonlat'
+     INCLUDE="$INCLUDE "'-I$(LIBF)/dynlonlat_phylonlat/'"phy${phys_root}"
+   fi
    cd $LIBFGCM/grid
    \rm fxyprim.h
@@ -495,10 +522,14 @@
    INCLUDE="$INCLUDE "'-I$(LIBF)/dyn3d${FLAG_PARA} -I$(LIBF)/dyn3d_common '
 elif (( $dimc == 2 )) ; then
+   src_dirs="$src_dirs dyn2d"
    filtre="FILTRE= L_FILTRE= "
    INCLUDE="$INCLUDE "'-I$(LIBF)/dyn2d'
 elif (( $dimc == 1 )) ; then
+   #src_dirs="$src_dirs dyn3d dyn3d_common filtrez"
+   src_dirs="$src_dirs phy${physique}/dyn1d"
    CPP_KEY="$CPP_KEY CPP_1D"
-   filtre="L_DYN= DYN= FILTRE= L_FILTRE= DIRMAIN=phy$physique "
-   INCLUDE="$INCLUDE "'-I$(LIBF)/dyn3d -I$(LIBF)/dyn3d_common ' # Pas tres propre
+   filtre="L_DYN= DYN= FILTRE= L_FILTRE= "
+   #INCLUDE="$INCLUDE "'-I$(LIBF)/dyn3d -I$(LIBF)/dyn3d_common ' # Pas tres propre
+   INCLUDE="$INCLUDE "' -I$(LIBF)/phy'"$physique"'/dyn1d'
 else
    echo Dimension dimc=$dimc pas prevu ; exit
@@ -539,21 +570,52 @@
 cd $LMDGCM
 
-find libf -name '*.[Fh]' -print > tmp77
-find libf -name '*.[Fh]' -exec egrep -i " *use *ioipsl" {} \; -print > tmp90
-find libf -name '*.F90' -print >> tmp90
-
-if [[ ! ( -r makefile ) || ! ( -r liste_des_sources_f90 ) || ! ( -r liste_des_sources_f77 ) || ` diff tmp77 liste_des_sources_f77 | wc -w ` -ne 0 || ` diff tmp90 liste_des_sources_f90 | wc -w ` -ne 0 ]]
+
+if [[ -r .makelmdz ]]
+then
+old_lmdz_configuration=$(cat .makelmdz )
+else
+old_lmdz_configuration=""
+fi
+lmdz_configuration="$src_dirs"
+if [[ "$lmdz_configuration" != "$old_lmdz_configuration" ]]
+then
+  configuration_change="true"
+else
+  configuration_change="false"
+fi
+
+mkdir -p make_dir
+suf_make=`echo $src_dirs | sed -e 's/\//_/g' -e 's/ /_/g'`
+echo suf_make $suf_make
+
+\rm tmp77 tmp90
+for dir in $src_dirs ; do
+find libf/$dir -name '*.[Fh]' -print | sort >> tmp77
+find libf/$dir -name '*.F90' -print | sort >> tmp90
+done
+
+liste77=make_dir/liste_des_sources_f77_$suf_make
+liste90=make_dir/liste_des_sources_f90_$suf_make
+makefile=make_dir/makefile_$suf_make
+
+if [[ $configuration_change == "true" || ! ( -r $makefile ) || ! ( -r $liste90 ) || ! ( -r $liste77 ) || ` diff tmp77 $liste77 | wc -w ` -ne 0 || ` diff tmp90 $liste90 | wc -w ` -ne 0 ]]
 then
   echo "les fichiers suivants ont ete crees ou detruits"
   echo "ou les fichiers suivants sont passes ou ne sont plus en Fortran 90"
-  diff liste_des_sources_f77 tmp77
-  diff liste_des_sources_f90 tmp90
-  \cp -f tmp77 liste_des_sources_f77
-  \cp -f tmp90 liste_des_sources_f90
-  echo "on recree le makefile"
-  ./create_make_gcm > tmp 
-  \mv -f tmp makefile
-  echo "Nouveau makefile cree"
-fi
+  diff $liste77 tmp77
+  diff $liste90 tmp90
+  \cp -f tmp77 $liste77
+  \cp -f tmp90 $liste90
+  echo "Recreating the makefile"
+  echo "src_dirs: $src_dirs"
+  ./create_make_gcm $src_dirs > tmp 
+  \mv -f tmp $makefile
+  echo "New makefile created"
+fi
+
+ln -sf $makefile makefile
+
+#Create a .makelmdz file containing main compilation option for later comparisons
+echo "$lmdz_configuration" > .makelmdz
 
 #################################################################
@@ -562,8 +624,16 @@
 
 source_code=${code}.F
+dirmain=dyn${dimc}d${FLAG_PARA}
 if [[ -r $LMDGCM/libf/dyn${dimc}d${FLAG_PARA}/${code}.F90 ]]
 then
   source_code=${code}.F90
 elif [[ -r $LMDGCM/libf/phy$physique/${code}.F90 ]] ; then
+  dirmain=phy$physique
+  source_code=${code}.F90
+elif [[ -r $LMDGCM/libf/dynlonlat_phylonlat/phy$phys_root/${code}.F90 ]] ; then
+  dirmain="dynlonlat_phylonlat/phy${phys_root}"
+  source_code=${code}.F90
+elif [[ -r $LMDGCM/libf/phy$physique/dyn1d/${code}.F90 ]] ; then
+  dirmain=phy$physique/dyn1d
   source_code=${code}.F90
 fi
@@ -619,6 +689,10 @@
 LIBO=${LIBOGCM}/${nomlib} \
 "PHYS=$physique" \
+LIBPHY=${LIBPHY} \
+LIBDYN_PHYS=${LIBDYN_PHYS} \
 DIM=$dimc \
 FLAG_PARA=$FLAG_PARA \
+L_PHY="$lib_phy" \
+L_DYN_PHY="$libdyn_phy" \
 L_ADJNT=$adjnt \
 L_COSP="$lcosp" \
@@ -633,4 +707,5 @@
 MOD_SUFFIX="mod" \
 AR=$arcommand \
+DIRMAIN=$dirmain \
 SOURCE=$source_code \
 PROG=$code
@@ -645,6 +720,10 @@
 LIBO=${LIBOGCM}/${nomlib} \
 "PHYS=$physique" \
+LIBPHY=${LIBPHY} \
+LIBDYN_PHYS=${LIBDYN_PHYS} \
 DIM=$dimc \
 FLAG_PARA=$FLAG_PARA \
+L_PHY="$lib_phy" \
+L_DYN_PHY="$libdyn_phy" \
 L_ADJNT=$adjnt \
 L_COSP="$lcosp" \
@@ -659,4 +738,5 @@
 MOD_SUFFIX="mod" \
 AR=$arcommand \
+DIRMAIN=$dirmain \
 SOURCE=$source_code \
 PROG=$code
