source: trunk/LMDZ.COMMON/makelmdz_fcm

Last change on this file was 3872, checked in by afalco, 3 weeks ago

Added a compilation option: -clean
Cleans current compilation directory (without deleting the rest).
Useful if you have plenty of binaries you want to keep but want to start a compilation from "scratch".
AF

File size: 30.4 KB
Line 
1#!/bin/bash
2# $Id: makelmdz_fcm 1615 2012-02-10 15:42:26Z emillour $
3# This is a script in Bash to compile the GCM and related tools
4#
5##set -x
6set -e
7########################################################################
8# options par defaut pour la commande make
9########################################################################
10
11
12dim="96x72x19"
13physique=lmd
14filtre=filtrez
15grille=reg
16couple=false
17veget=false
18sisvat=false
19rrtm=false
20dust=false
21strataer=false
22chimie=false
23chemistry=false
24parallel=none
25paramem="par"
26compil_mod=prod
27io=ioipsl
28LIBPREFIX=""
29cosp=false
30bands=""
31scatterers=""
32job=1
33full=''
34libphy=false
35clean=false
36
37arch_defined="FALSE"
38arch_path="arch"
39arch_default_path="arch"
40
41LMDGCM=`/bin/pwd`
42LIBOGCM=$LMDGCM/libo
43LIBFGCM=$LMDGCM/libf
44DYN_COMMON_PATH=$LIBFGCM/dyn3d_common
45# path for optional packages, but default set to ".void_dir"
46FILTRE_PATH=$LMDGCM/.void_dir
47DYN_PHYS_PATH=$LMDGCM/.void_dir
48DYN_PHYS_SUB_PATH=$LMDGCM/.void_dir
49PHY_COMMON_PATH=$LMDGCM/.void_dir
50RRTM_PATH=$LMDGCM/.void_dir
51DUST_PATH=$LMDGCM/.void_dir
52STRATAER_PATH=$LMDGCM/.void_dir
53SISVAT_PATH=$LMDGCM/.void_dir
54COSP_PATH=$LMDGCM/.void_dir
55CHEM_PATH=$LMDGCM/.void_dir
56CLOUD_PATH=$LMDGCM/.void_dir
57MUPHY_PATH=$LMDGCM/.void_dir
58AERONO_PATH=$LMDGCM/.void_dir
59EVOLUTION_PATH=$LMDGCM/.void_dir
60# Path to fcm utility:
61##fcm_path=$LMDGCM/tools/fcm/bin
62##fcm_path=/planeto/mturbet/planeto2-mturbet/FCM_V1.2/bin
63fcm_path=$(dirname $(which fcm))
64
65########################################################################
66#  Quelques initialisations de variables du shell.
67########################################################################
68
69CPP_KEY=""
70INCLUDE_DIR=""
71LIB=""
72adjnt=""
73COMPIL_FFLAGS="%PROD_FFLAGS"
74PARA_FFLAGS=""
75PARA_LD=""
76EXT_SRC=""
77
78########################################################################
79# lecture des options de mymake
80########################################################################
81
82# Get the compilation command
83compilation_command="$(basename $0) $@"
84
85# Parse arguments and capture options
86while (($# > 0))
87  do
88  case $1 in
89      "-h") cat <<fin
90Usage :
91makelmdz_fcm [options] -arch arch_name exec
92[-h]                       : brief help
93[-d [[IMx]JMx]LM]          : IM, JM, LM are the dimensions in x, y, z (default: $dim)
94[-s nscat]                 : (Generic) Number of radiatively active scatterers
95[-b IRxVIS]                : (Generic) Number of infrared (IR) and visible (VIS) bands for radiative transfer
96[-p PHYS]                  : set of physical parametrizations (in libf/phyPHYS), (default: lmd)
97[-prod / -dev / -debug]    : compilation mode production (default) / developement / debug .
98[-c false/MPI1/OMCT]       : (Earth) coupling with ocean model : MPI1/OMCT/false (default: false)
99[-v false/orchidee2.0/orchidee1.9/true] : (Earth) version of the vegetation model to include (default: false)
100          false       : no vegetation model
101          orchidee2.0 : compile using ORCHIDEE 2.0 (or more recent version)
102          orchidee1.9 : compile using ORCHIDEE up to the version including OpenMP in ORCHIDEE : tag 1.9-1.9.5(version AR5)-1.9.6
103          true        : (obsolete; for backward compatibility) use ORCHIDEE tag 1.9-1.9.6
104[-chimie INCA/false]       : (Earth) with INCA chemistry model or without (default: false)
105[-cosp true/false]         : (Earth) add the cosp model (default: false)
106[-sisvat true/false]  : (Earth) compile with/without sisvat package (default: false)
107[-rrtm true/false]    : (Earth) compile with/without rrtm package (default: false)
108[-dust true/false]    : (Earth) compile with/without the dust package by Boucher and co (default: false)
109[-strataer true/false]    : (Earth) compile with/without the strat aer package by Boucher and co (default: false)
110[-parallel none/mpi/omp/mpi_omp] : parallelism (default: none) : mpi, openmp or mixted mpi_openmp
111[-g GRI]                   : grid configuration in dyn3d/GRI_xy.h  (default: reg, inclues a zoom)
112[-io ioipsl/mix/xios]                   : Input/Output library (default: ioipsl)
113[-include INCLUDES]        : extra include path to add
114[-cpp CPP_KEY]             : additional preprocessing definitions
115[-adjnt]                   : adjoint model, not operational ...
116[-mem]                     : reduced memory dynamics (if in parallel mode)
117[-filtre NOMFILTRE]        : use filtre from libf/NOMFILTRE (default: filtrez)
118[-link LINKS]              : additional links with other libraries
119[-j n]                     : active parallel compiling on ntask
120[-full]                    : full (re-)compilation (from scratch)
121[-libphy]                  : only compile physics package (no dynamics or main program)
122[-fcm_path path]           : path to the fcm tool (default: tools/fcm/bin)
123[-ext_src path]            : path to an additional set of routines to compile with the model
124[-arch_path path]          : path to architecture files (default: $arch_default_path)
125 -arch arch                : target architecture
126 exec                      : executable to build
127fin
128      exit;;
129
130      "-d")
131      dim=$2 ; shift ; shift ;;
132
133      "-p")
134      physique="$2" ;  shift ; shift ;;
135
136      "-s")
137      scatterers=$2 ; shift ; shift ;;
138
139      "-b")
140      bands=$2 ; shift ; shift ;;
141
142      "-g")
143      grille="$2" ; shift ; shift ;;
144
145      "-c")
146      couple="$2" ; shift ; shift ;;
147
148      "-prod")
149      compil_mod="prod" ; shift ;;
150
151      "-dev")
152      compil_mod="dev" ; shift ;;
153
154      "-debug")
155      compil_mod="debug" ; shift ;;
156
157      "-io")
158      io="$2" ; shift ; shift ;;
159
160      "-v")
161      veget="$2" ; shift ; shift ;;
162
163      "-sisvat")
164      sisvat="$2" ; shift ; shift ;;
165
166      "-rrtm")
167      rrtm="$2" ; shift ; shift ;;
168
169      "-dust")
170      dust="$2" ; shift ; shift ;;
171
172      "-strataer")
173      strataer="$2" ; shift ; shift ;;
174
175      "-chimie")
176      chimie="$2" ; shift ; shift ;;
177
178      "-parallel")
179      parallel="$2" ; shift ; shift ;;
180
181      "-include")
182      INCLUDE_DIR="$INCLUDE_DIR -I$2" ; shift ; shift ;;
183
184      "-cpp")
185      CPP_KEY="$CPP_KEY $2" ; shift ; shift ;;
186
187      "-adjnt")
188      echo "not operational ... work to be done here ";exit 1
189      opt_dep="$opt_dep adjnt" ; adjnt="-ladjnt -ldyn3d "
190      optim="$optim -Dadj" ; shift ;;
191
192      "-cosp")
193          cosp="$2" ; shift ; shift ;;
194
195      "-mem")
196          paramem="mem" ; shift ;;
197
198      "-filtre")
199      filtre=$2 ; shift ; shift ;;
200
201      "-link")
202      LIB="$LIB $2" ; shift ; shift ;;
203
204      "-fcm_path")
205      fcm_path=$2 ; shift ; shift ;;
206
207      "-ext_src")
208      EXT_SRC=$2 ; shift ; shift ;;
209
210      "-j")
211      job=$2 ; shift ; shift ;;
212
213      "-full")
214      full="-full" ; shift ;;
215
216      "-libphy")
217      libphy="true" ; shift ;;
218
219      "-clean")
220      clean="true" ; shift ;;
221
222      "-arch")
223      arch=$2 ; arch_defined="TRUE" ; shift ; shift ;;
224
225      "-arch_path")
226      arch_path=$2 ; arch_path_defined="TRUE"; shift ; shift ;;
227
228      *)
229      code="$1" ; shift ;;
230  esac
231done
232
233###############################################################
234# path to fcm
235###############################################################
236# handle case when provided path to fcm was given as a relative
237# path (from makelmdz_fcm script directory) and not an absolute path
238if [[ ${fcm_path:0:1} != "/" ]] ; then
239  # prepend with makelmdz_fcm location
240  fcm_path=$(cd $(dirname $0) ; pwd)"/"${fcm_path}
241fi
242
243# add fcm_path to PATH
244export PATH=${fcm_path}:${PATH}
245
246echo "Path to fcm:"
247echo ${fcm_path}
248
249#################################
250# setup arch.env and arch.path  #
251#################################
252rm -f .void_file
253echo > .void_file
254rm -rf .void_dir
255mkdir .void_dir
256
257if [[ "$arch_defined" == "TRUE" ]]
258then
259  rm -f arch.path
260  rm -f arch.env
261
262  if test -f $arch_path/arch-${arch}.path
263  then
264    ln -s $arch_path/arch-${arch}.path arch.path
265  elif test -f $arch_default_path/arch-${arch}.path
266  then
267    ln -s $arch_default_path/arch-${arch}.path arch.path
268  fi
269
270  if test -f $arch_path/arch-${arch}.env
271  then
272    ln -s $arch_path/arch-${arch}.env arch.env
273  elif test -f $arch_default_path/arch-${arch}.env
274  then
275    ln -s $arch_default_path/arch-${arch}.env arch.env
276  else
277    ln -s .void_file arch.env
278  fi
279  # source architecture PATH and ENV files
280  source arch.env
281  source arch.path
282else
283  echo "You must define a target architecture"
284  exit 1
285fi
286
287########################################################################
288# Definition des clefs CPP, des chemins des includes et modules
289#  et des libraries
290########################################################################
291
292if [[ "$compil_mod" == "prod" ]]
293then
294  COMPIL_FFLAGS="%PROD_FFLAGS"
295elif [[ "$compil_mod" == "dev" ]]
296then
297  COMPIL_FFLAGS="%DEV_FFLAGS"
298elif [[ "$compil_mod" == "debug" ]]
299then
300  COMPIL_FFLAGS="%DEBUG_FFLAGS"
301fi
302
303phys_root=$physique
304if [[ "${physique:0:3}" == "lmd" ]] ; then phys_root=lmd ; fi
305if [[ "${physique:0:3}" == "mar" ]] ; then phys_root=mar ; fi
306if [[ "${physique:0:4}" == "mars" ]] ; then phys_root=mars ; fi
307if [[ "${physique:0:3}" == "std" ]] ; then phys_root=std ; fi
308if [[ "${physique:0:5}" == "venus" ]] ; then phys_root=venus ; fi
309if [[ "${physique:0:5}" == "titan" ]] ; then phys_root=titan ; fi
310if [[ "${physique:0:3}" == "dev" ]] ; then phys_root=dev ; fi
311
312if [[ "$physique" != "nophys" ]]
313then
314   #We'll use some physics
315   CPP_KEY="$CPP_KEY CPP_PHYS"
316   if [[ "${phys_root}" == "lmd" ]]
317   then
318   #For lmd physics, default planet type is Earth
319   CPP_KEY="$CPP_KEY CPP_EARTH"
320   fi
321   # set physics common utilities path
322   PHY_COMMON_PATH="${LIBFGCM}/phy_common"
323   # set the dyn/phys interface path
324   DYN_PHYS_PATH="${LIBFGCM}/dynphy_lonlat"
325   DYN_PHYS_SUB_PATH="${LIBFGCM}/dynphy_lonlat/phy${phys_root}"
326fi
327
328if [[ "$filtre" == "filtrez" ]]
329then
330   FILTRE_PATH=${LIBFGCM}/$filtre
331fi
332
333if [[ "$chimie" == "INCA" ]]
334then
335   CPP_KEY="$CPP_KEY INCA"
336   INCLUDE_DIR="$INCLUDE_DIR -I${INCA_INCDIR}"
337   LIB="$LIB -L${INCA_LIBDIR} ${INCA_LIB}"
338fi
339
340if [[ "$couple" != "false" ]]
341then
342    if [[ "$couple" == "MPI1" ]]
343    then
344    CPP_KEY="$CPP_KEY CPP_COUPLE"
345    export OASIS_INCDIR=$LMDGCM/../../prism/X64/build/lib/psmile.MPI1
346    export OASIS_LIBDIR=$LMDGCM/../../prism/X64/lib
347    INCLUDE_DIR="$INCLUDE_DIR -I${OASIS_INCDIR}"
348    LIB="$LIB -L${OASIS_LIBDIR} ${OASIS_LIB}"
349    else
350    CPP_KEY="$CPP_KEY CPP_COUPLE CPP_OMCT"
351    INCLUDE_DIR="$INCLUDE_DIR -I${OASIS_INCDIR}"
352    LIB="$LIB -L${OASIS_LIBDIR} ${OASIS_LIB}"
353    fi
354fi
355
356if [[ "$parallel" != "none" && \
357     ( "$code" == "newstart" || "$code" == "start2archive" ) ]]
358then
359    echo "newstart or start2archive utilities should be run in serial!"
360    echo " Compile without -parallel [mpi|omp|mpi_omp] option!"
361    exit 1
362fi
363
364if [[ "$parallel" == "mpi" ]]
365then
366   CPP_KEY="$CPP_KEY CPP_PARA CPP_MPI"
367   PARA_FFLAGS="%MPI_FFLAGS"
368   PARA_LD="%MPI_LD"
369elif [[ "$parallel" == "omp" ]]
370then
371   CPP_KEY="$CPP_KEY CPP_PARA CPP_OMP"
372   PARA_FFLAGS="%OMP_FFLAGS"
373   PARA_LD="%OMP_LD"
374elif [[ "$parallel" == "mpi_omp" ]]
375then
376   CPP_KEY="$CPP_KEY CPP_PARA CPP_MPI CPP_OMP"
377   PARA_FFLAGS="%MPI_FFLAGS %OMP_FFLAGS"
378   PARA_LD="%MPI_LD %OMP_LD"
379fi
380
381if [[ ( "$parallel" == "omp" || "$parallel" == "mpi_omp" ) \
382   && "$compil_mod" == "debug" ]]
383then
384    echo "Usually, parallelization with OpenMP requires some optimization."
385    echo "We suggest switching to \"-dev\"."
386fi
387
388if [ "$veget" = "true" -o "$veget" = "orchidee1.9" -o "$veget" = "orchidee2.0" ]
389then
390#NB: option 'true': for backward compatibility. To be used with ORCHIDEE tag 1.9-1.9.6
391#    For this case, cpp flag ORCHIDEE_NOOPENMP must be added to the makelmdz_fcm arguments
392#    option orchidee1.9 : Compile with ORCHIDEE version up to the inclusion of OpenMP in ORCHIDEE : tag 1.9-1.9.5(version AR5)-1.9.6
393   INCLUDE_DIR="${INCLUDE_DIR} ${ORCHIDEE_INCDIR}"
394   CPP_KEY="$CPP_KEY CPP_VEGET"
395# temporary, for Orchidee versions 1.9.* (before openmp activation)
396   if [[ "$veget" == "orchidee1.9" ]] ; then
397      CPP_KEY="$CPP_KEY ORCHIDEE_NOOPENMP"
398   fi
399   if [[ "$veget" == "orchidee2.0" ]] ; then
400      orch_libs="sechiba parameters stomate parallel orglob orchidee"
401   else
402      orch_libs="sechiba parameters stomate parallel orglob"
403   fi
404   LIB="${LIB} -L${ORCHIDEE_LIBDIR} ${ORCHIDEE_LIB}"
405elif [[ "$veget" != "false" ]] ; then
406   echo "Option -v $veget does not exist"
407   echo "Use ./makelmdz_fcm -h for more information"
408   exit 1
409fi
410
411if [[ "$sisvat" == "true" ]]
412then
413   CPP_KEY="$CPP_KEY CPP_SISVAT"
414   SISVAT_PATH="$LIBFGCM/%PHYS/sisvat"
415fi
416
417if [[ "$rrtm" == "true" ]]
418then
419   CPP_KEY="$CPP_KEY CPP_RRTM"
420   RRTM_PATH="$LIBFGCM/%PHYS/rrtm"
421fi
422
423if [[ "$dust" == "true" ]]
424then
425   CPP_KEY="$CPP_KEY CPP_Dust"
426   DUST_PATH="$LIBFGCM/%PHYS/Dust"
427fi
428
429if [[ "$strataer" == "true" ]]
430then
431   CPP_KEY="$CPP_KEY CPP_StratAer"
432   STRATAER_PATH="$LIBFGCM/%PHYS/StratAer"
433fi
434
435if [[ $io == ioipsl ]]
436then
437   CPP_KEY="$CPP_KEY CPP_IOIPSL"
438   INCLUDE_DIR="$INCLUDE_DIR ${IOIPSL_INCDIR}"
439   LIB="$LIB ${IOIPSL_LIBDIR} ${IOIPSL_LIB}"
440elif [[ $io == mix ]]
441then
442   # For now, xios implies also using ioipsl
443   CPP_KEY="$CPP_KEY CPP_IOIPSL CPP_XIOS"
444   INCLUDE_DIR="$INCLUDE_DIR ${IOIPSL_INCDIR} ${XIOS_INCDIR}"
445   LIB="$LIB ${IOIPSL_LIBDIR} ${IOIPSL_LIB} ${XIOS_LIBDIR} ${XIOS_LIB}"
446elif [[ $io == xios ]]
447then
448   # For now, xios implies also using ioipsl
449   CPP_KEY="$CPP_KEY CPP_IOIPSL CPP_XIOS CPP_IOIPSL_NO_OUTPUT"
450   INCLUDE_DIR="$INCLUDE_DIR ${IOIPSL_INCDIR} ${XIOS_INCDIR}"
451   LIB="$LIB ${IOIPSL_LIBDIR} ${IOIPSL_LIB} ${XIOS_LIBDIR} ${XIOS_LIB}"
452fi
453
454if [[ "$cosp" == "true" ]]
455then
456   CPP_KEY="$CPP_KEY CPP_COSP"
457   COSP_PATH="$LIBFGCM/cosp"
458#   LIB="${LIB} -l${LIBPREFIX}cosp"
459fi
460
461# for Titan (but could be used by others as well), there is also "chimtitan"
462if [[ -d ${LIBFGCM}/chim${physique} ]]
463then
464   CHEM_PATH="${LIBFGCM}/chim${physique}"
465   INCLUDE_DIR="$INCLUDE_DIR -I${LIBFGCM}/chim${physique}"
466fi
467
468# for Titan add microphysics dependencies
469if [[ -d ${LIBFGCM}/muphy${physique} ]] ; then
470  if ! [ `echo $CPP_KEY | grep -c "OLD_COMPILO"` -gt 0 ] ; then
471    # add muphy{physique} to the list of files to compile.
472    MUPHY_PATH="${LIBFGCM}/muphy${physique}"
473    # For Titan as we embbed some libraries with CPP keys, we define them:
474    #   -DPREC={32|64|80} real kind precision (32: simple, 64: double, 80: extended double)
475    #   -DHAVE_NC_FTN=1 (mandatory: for support of NetCDF in lintnd).
476    CPP_KEY="$CPP_KEY PREC=64 HAVE_NC_FTN=1"
477    if [[ "${physique}" == "titan" ]] ; then
478      echo "Yaahh you're about to use YAMMS in a GCM..."
479    fi
480  fi
481fi
482
483# for Venus (but could be used by others as well), there is also "phyvenus/cloudvenus"
484if [[ -d ${LIBFGCM}/phy${physique}/cloud${physique} ]]
485then
486   CLOUD_PATH="${LIBFGCM}/phy${physique}/cloud${physique}"
487   INCLUDE_DIR="$INCLUDE_DIR -I${LIBFGCM}/phy${physique}/cloud${physique}"
488fi
489
490# for Mars (but could be used by others as well), there is also "aeronomars"
491if [[ -d ${LIBFGCM}/aerono${physique} ]]
492then
493   AERONO_PATH="${LIBFGCM}/aerono${physique}"
494   INCLUDE_DIR="$INCLUDE_DIR -I${LIBFGCM}/aerono${physique}"
495fi
496
497if [[ -d ${LIBFGCM}/evolution &&  "$code" == "pem" ]]
498then
499if [[ "$physique" == "std" ]]
500then
501   CPP_KEY="$CPP_KEY CPP_STD"
502fi
503   EVOLUTION_PATH="${LIBFGCM}/evolution"
504   INCLUDE="$INCLUDE -I${LIBFGCM}/evolution"
505fi
506
507if [[ -d ${LIBFGCM}/evolution &&  "$code" == "reshape_XIOS_output" ]]
508then
509if [[ "$physique" == "std" ]]
510then
511   CPP_KEY="$CPP_KEY CPP_STD"
512fi
513   EVOLUTION_PATH="${LIBFGCM}/evolution"
514   INCLUDE="$INCLUDE -I${LIBFGCM}/evolution"
515fi
516
517
518# NETCDF library include/library paths
519INCLUDE_DIR="$INCLUDE_DIR ${NETCDF_INCDIR}"
520LIB="$LIB ${NETCDF_LIBDIR} ${NETCDF_LIB}"
521
522########################################################################
523# calcul du nombre de dimensions
524########################################################################
525
526
527dim_full=$dim
528dim=`echo $dim | sed -e 's/[^0-9]/ /g'`
529set $dim
530dimc=$#
531echo calcul de la dimension
532echo dim $dim
533echo dimc $dimc
534
535
536########################################################################
537# Gestion des dimensions du modele.
538# on cree ou remplace le fichier des dimensions
539########################################################################
540
541cd $LIBFGCM/grid
542if [[ -f dimensions.h ]]
543then
544  echo 'ATTENTION: vous etes sans doute en train de compiler le modele par ailleurs'
545  echo "Attendez que la premiere compilation soit terminee pour relancer la suivante."
546  echo "Si vous etes sur que vous ne compilez pas le modele par ailleurs,"
547  echo  vous pouvez continuer en repondant oui.
548  echo "Voulez-vous vraiment continuer?"
549  echo ""
550  echo "WARNING: you are probably already compiling the model somewhere else."
551  echo "Wait until the first compilation is finished before launching this one."
552  echo "If you are sure that you are not compiling elsewhere, just answer "
553  echo "yes (or 'oui') to the question below to proceed."
554  echo "Do you wish to continue?"
555  read reponse
556  if [[ $reponse == "oui" || $reponse == "yes" ]]
557  then
558    \rm -f $LIBFGCM/grid/dimensions.h
559  else
560    exit 1
561  fi
562fi
563
564
565cd $LIBFGCM/grid/dimension
566./makdim $dim
567if (($? != 0))
568then
569    exit 1
570fi
571
572cat $LIBFGCM/grid/dimensions.h
573cd $LMDGCM
574
575if [[ "$bands" != "" ]]
576then
577  # Generic model, recreate bands.h (IR & VIS bands for radiative transfer)
578  makbandsarg=$(echo $bands | sed -e 's/[^0-9]/ /g')
579  cd $LIBFGCM/phy$physique/bands
580  ./makbands $makbandsarg
581  cat $LIBFGCM/phy$physique/bands.h
582  cd $LMDGCM
583fi
584
585if [[ "$scatterers" != "" ]]
586then
587  if [[ -f $LIBFGCM/phy$physique/scatterers/make_scatterers ]]
588  then
589    # Generic model, recreate scatterers.h
590    cd $LIBFGCM/phy$physique/scatterers
591    ./make_scatterers $scatterers
592    cat $LIBFGCM/phy$physique/scatterers.h
593    cd $LMDGCM
594  else
595    echo "Error: you should not use the -s # option with -p $physique"
596    exit 1
597  fi
598fi
599
600
601########################################################################
602# Generation of a Fortran subroutine to track compilation and version
603# details through the executable file
604########################################################################
605
606# Path to the root directory where we want to check version control system
607root_dir="$LMDGCM/.."
608
609# Path and name of the generated file
610version_F90file="$LIBFGCM/misc/pgrm_version.F90"
611
612# Path and name of the file containing the compilation and version details
613default_out_file="pgrm_version_details.txt"
614
615# Get the current date
616current_date=$(date)
617
618# Function to process problematic characters
619#   - Convert input to UTF-8 and ignore invalid characters
620#   - Remove carriage return characters to prevent formatting issues
621#   - Escape backslashes and single quotes
622clean_output() {
623    iconv -f "$(locale charmap)" -t UTF-8//IGNORE \
624        | tr -d '\r' \
625        | sed 's/\\/\\\\/g; s/'\''/'\'\''/g'
626}
627
628vcs_info=""
629vcs_diff=""
630vcs_stat=""
631# Check if there is a Git repository at the root
632if command -v git > /dev/null 2>&1 && git -C "$root_dir" rev-parse --is-inside-work-tree > /dev/null 2>&1; then
633    dir_name="$(basename "$root_dir")"
634    vcs_info+="-> Git Information for \"$dir_name\"\n$(git -C "$root_dir" log -1 --pretty=format:"%H%n%an%n%ad%n%s" | clean_output)\n"
635    vcs_stat+="-> Git Status for \"$dir_name\"\n$(git -C "$root_dir" status --short | clean_output)\n"
636    vcs_diff+="-> Git Diff for \"$dir_name\"\n$(git -C "$root_dir" diff | clean_output)\n"
637else # No so we iterate through each subdirectory to determine version control system
638    for dir in "$root_dir"/*; do
639        # Skip if it is not a directory or the name contains "git" or "svn"
640        [ -d "$dir" ] || continue
641        [[ "$dir" == *git* || "$dir" == *svn* ]] && continue
642
643        # Determine the version control system for each subdirectory
644        dir_name="$(basename "$dir")"
645        if command -v svn > /dev/null 2>&1 && svn info "$dir" > /dev/null 2>&1; then # SVN
646            vcs_info+="\n-> SVN Information for \"$dir_name\"\n$(svn info "$dir" | clean_output)\n"
647            vcs_diff+="\n-> SVN Diff for \"$dir_name\"\n$(svn diff "$dir" | clean_output)\n"
648            vcs_stat+="\n-> SVN Status for \"$dir_name\"\n$(svn status "$dir" | clean_output)\n"
649        elif command -v git > /dev/null 2>&1 && git -C "$dir" rev-parse --is-inside-work-tree > /dev/null 2>&1; then # Git
650            vcs_info+="-> Git Information for \"$dir_name\"\n$(git -C "$dir" log -1 --pretty=format:"%H%n%an%n%ad%n%s" | clean_output)\n"
651            vcs_diff+="\n-> Git Diff for \"$dir_name\"\n$(git -C "$dir" diff | clean_output)\n"
652            vcs_stat+="\n-> Git Status for \"$dir_name\"\n$(git -C "$dir" status --short | clean_output)\n"
653        else # None
654            vcs_info+="\n-> No version control system for \"$dir_name\"\n"
655        fi
656    done
657fi
658
659# Generate the Fortran subroutine
660cat << EOF > "$version_F90file"
661MODULE pgrm_version_mod
662
663!***********************************************************************
664! File generated automatically at compilation
665!
666! DESCRIPTION:
667!    The subroutine 'print_pgrm_version' prints compilation details, the version
668!    control information (SVN or Git), the status and the diff result if applicable.
669!
670! PARAMETERS:
671!    None.
672!
673! USAGE:
674!    Use the command-line option "--version [file]" when running your program:
675!       ./myprogram --version [file]
676!    This will write compilation and version details into the specified [file].
677!    If [file] is omitted, the default name "pgrm_version_details.txt" will be used.
678!    This feature helps track code builds and their exact compilation context
679!    directly from the executable.
680!***********************************************************************
681
682implicit none
683
684character(*), parameter :: default_out_file = "${default_out_file}"
685
686!=======================================================================
687contains
688!=======================================================================
689
690SUBROUTINE print_pgrm_version(user_out_file)
691
692!---- Arguments
693character(*), optional, intent(in) :: user_out_file
694
695!---- Variables
696integer, parameter        :: io_unit = 10
697character(:), allocatable :: out_file
698
699!---- Code
700if (present(user_out_file)) then
701    out_file = trim(adjustl(user_out_file))
702else
703    out_file = trim(adjustl(default_out_file))
704endif
705
706open(io_unit,file = out_file,status = 'replace',action = 'write')
707
708write(*,*)
709write(*,'(a)') '-> Writing compilation details to the file "'//out_file//'".'
710write(io_unit,'(a)') '========================= COMPILATION DETAILS =========================='
711write(io_unit,'(a)') '-> Date   : ${current_date}'
712write(io_unit,'(a)') '-> Command: ${compilation_command}'
713write(io_unit,*)
714EOF
715
716if [ -n "$vcs_info" ]; then
717    echo "write(*,'(a)') '-> Writing information result to the file \"'//out_file//'\".'" >> "$version_F90file"
718    echo "write(io_unit,'(a)') '===================== VERSION CONTROL INFORMATION ======================'" >> "$version_F90file"
719    while IFS= read -r line; do
720        echo "write(io_unit,'(a)') '${line}'" >> "$version_F90file"
721    done <<< "$(echo -e "$vcs_info")"
722else
723    echo "write(io_unit,'(a)') '====================== NO VERSION CONTROL SYSTEM ======================='" >> "$version_F90file"
724fi
725
726if [ -n "$vcs_stat" ]; then
727    echo "write(*,'(a)') '-> Writing status result to the file \"'//out_file//'\".'" >> "$version_F90file"
728    echo "write(io_unit,*)" >> "$version_F90file"
729    echo "write(io_unit,'(a)') '======================== VERSION CONTROL STATUS ========================'" >> "$version_F90file"
730    while IFS= read -r line; do
731        echo "write(io_unit,'(a)') '${line}'" >> "$version_F90file"
732    done <<< "$(echo -e "$vcs_stat")"
733fi
734
735if [ -n "$vcs_diff" ]; then
736    echo "write(*,'(a)') '-> Writing diff result to the file \"'//out_file//'\".'" >> "$version_F90file"
737    echo "write(io_unit,*)" >> "$version_F90file"
738    echo "write(io_unit,'(a)') '========================= VERSION CONTROL DIFF ========================='" >> "$version_F90file"
739    while IFS= read -r line; do
740        echo "write(io_unit,'(a)') '${line}'" >> "$version_F90file"
741    done <<< "$(echo -e "$vcs_diff")"
742fi
743
744cat << EOF >> "$version_F90file"
745write(io_unit,'(a)') '========================================================================'
746write(*,*)
747
748close(io_unit)
749
750END SUBROUTINE print_pgrm_version
751
752END MODULE pgrm_version_mod
753EOF
754
755# Termination message
756echo "'$version_F90file' has been generated successfully."
757
758
759########################################################################
760# Differentes dynamiques (3d, 2d, 1d)
761########################################################################
762
763dimension=`echo $dim | wc -w`
764echo dimension $dimension
765
766if (( $dimension == 3 ))
767then
768  cd $LIBFGCM/grid
769  \rm fxyprim.h
770  cp -p fxy_${grille}.h fxyprim.h
771#else
772#  echo "Probleme dans les dimensions de la dynamique !!"
773#  echo "Non reactive pour l'instant !!!"
774fi
775
776if (( $dimension == 1 ))
777then
778## Sanity check: 1D models should be used in serial
779## unless compiled with xios and then it should be in mpi only
780  if [[ $io == xios ]]
781  then
782   echo "OK io xios"
783   if [[ $parallel != "mpi" ]]
784   then
785    echo "Error: a 1D model compiled with xios should also be compiled with "
786    echo "       -parallel mpi option!"
787    exit 1
788   fi
789  else
790  # no xios, check that it is in serial only
791   if [[ $parallel != "none" ]]
792   then
793    echo "Error: a 1D model should not be compiled with "
794    echo "       -parallel [mpi|omp|mpi_omp] option!"
795    exit 1
796   fi
797  fi # of if [[ $io == "xios" ]]
798  CPP_KEY="$CPP_KEY CPP_1D"
799##in 1D, add dyn3d to include path (because main prog is in physics)
800  INCLUDE_DIR="$INCLUDE_DIR -Ilibf/dyn3d -Ilibf/dyn3d_common"
801## no filtre in 1d:
802  FILTRE_PATH=$LMDGCM/.void_dir
803## no need to compile all routines in dyn3d_common either:
804  DYN_COMMON_PATH=$LMDGCM/.void_dir
805## no need to compile all routines in dynlmdz_phy... ;
806## (because key ones are included in 1D main program)
807  DYN_PHYS_PATH=$LMDGCM/.void_dir
808  DYN_PHYS_SUB_PATH=$LMDGCM/.void_dir
809fi
810
811######################################################################
812#   Traitement special pour le nouveau rayonnement de Laurent Li.
813#   ---> YM desactive pour le traitemement en parallele
814######################################################################
815
816#if [[ -f $libf/phy$physique/raddim.h ]]
817#then
818# if [[ -f $libf/phy$physique/raddim.$dimh.h ]]
819#then
820#  \rm -f $libf/phy$physique/raddim.h
821#  cp -p $libf/phy$physique/raddim.$dimh.h $libf/phy$physique/raddim.h
822#  echo $libf/phy$physique/raddim.$dimh.h
823#  cat $libf/phy$physique/raddim.h
824# else
825#  echo On peut diminuer la taille de l executable en creant
826#  echo le fichier $libf/phy$physique/raddim.$dimh.h
827#  \cp -p $libf/phy$physique/raddim.defaut.h $libf/phy$physique/raddim.h
828# fi
829#fi
830
831######################################################################
832# Gestion du filtre qui n'existe qu'en 3d.
833######################################################################
834
835if (( `expr $dimc \> 2` == 1 ))
836then
837   filtre="FILTRE=$filtre"
838else
839   filtre="FILTRE= L_FILTRE= "
840fi
841echo MACRO FILTRE $filtre
842
843echo $dimc
844
845
846
847######################################################################
848# Creation du suffixe de la configuration
849######################################################################
850
851if [[ $libphy != "true" ]];then
852  SUFF_NAME=_${dim_full}
853fi
854SUFF_NAME=${SUFF_NAME}_phy${physique}
855
856if [[ "$bands" != "" ]]
857then
858  #if bands option -b VISxIR is used, add "_bVISxIR" to executable name
859  SUFF_NAME=${SUFF_NAME}_b$bands
860fi
861
862if [[ "$parallel" != "none" ]]
863then
864  SUFF_NAME=${SUFF_NAME}_para
865  if (( $dimension == 1 ))
866  then
867    # dynamics-related routines in"dyn1d" subdirectory of phy${physique}
868    DYN=phy${physique}/dyn1d
869  else
870    DYN=dyn${dimc}d${paramem}
871  fi
872  if [[ "$paramem" == "mem" ]]
873  then
874   SUFF_NAME=${SUFF_NAME}_${paramem}
875  fi
876else
877  SUFF_NAME=${SUFF_NAME}_seq
878  if (( $dimension == 1 ))
879  then
880    # dynamics-related routines in"dyn1d" subdirectory of phy${physique}
881    DYN=phy${physique}/dyn1d
882  else
883    DYN=dyn${dimc}d
884  fi
885fi
886
887if [[ $veget != "false" ]]
888then
889  SUFF_NAME=${SUFF_NAME}_orch
890fi
891
892if [[ $couple != "false" ]]
893then
894  SUFF_NAME=${SUFF_NAME}_couple
895fi
896
897if [[ $chimie == "INCA" ]]
898then
899  SUFF_NAME=${SUFF_NAME}_inca
900fi
901SUFF_DEBUG=
902if [[ "$compil_mod" == "debug" ]]
903then
904  SUFF_DEBUG=_debug
905fi
906SUFF_DIR=${SUFF_NAME}${SUFF_DEBUG}
907
908if [[ $libphy == "true" ]]
909then
910  # special case where we compile only the physics
911  DYN=$LMDGCM/.void_dir
912  DYN_COMMON_PATH=$LMDGCM/.void_dir
913  FILTRE_PATH=$LMDGCM/.void_dir
914  DYN_PHYS_PATH=$LMDGCM/.void_dir
915  DYN_PHYS_SUB_PATH=$LMDGCM/.void_dir
916  #and there is no main program to generate
917  code=""
918  SUFF_NAME=""
919  # SUFF_DIR=
920else
921  SUFF_NAME=${SUFF_NAME}.e
922fi
923
924cd $LMDGCM
925config_fcm="config.fcm"
926rm -f $config_fcm
927touch $config_fcm
928
929echo "%ARCH          $arch"          >> $config_fcm
930echo "%INCDIR        $INCLUDE_DIR"       >> $config_fcm
931echo "%LIB           $LIB"           >> $config_fcm
932echo "%ROOT_PATH     $PWD"           >> $config_fcm
933echo "%LIBF          $LIBFGCM"       >> $config_fcm
934echo "%LIBO          $LIBOGCM"       >> $config_fcm
935echo "%DYN           $DYN"           >> $config_fcm
936echo "%DYN_COMMON    $DYN_COMMON_PATH" >> $config_fcm
937echo "%PHY_COMMON    $PHY_COMMON_PATH" >> $config_fcm
938echo "%FILTRE        $FILTRE_PATH"   >> $config_fcm
939echo "%PHYS          phy${physique}" >> $config_fcm
940echo "%DYN_PHYS      $DYN_PHYS_PATH" >> $config_fcm
941echo "%DYN_PHYS_SUB  $DYN_PHYS_SUB_PATH" >> $config_fcm
942echo "%RRTM          $RRTM_PATH"     >> $config_fcm
943echo "%DUST          $DUST_PATH"     >> $config_fcm
944echo "%STRATAER      $STRATAER_PATH" >> $config_fcm
945echo "%SISVAT        $SISVAT_PATH"   >> $config_fcm
946echo "%COSP          $COSP_PATH"     >> $config_fcm
947echo "%CHEM          $CHEM_PATH"     >> $config_fcm
948echo "%CLOUD         $CLOUD_PATH"    >> $config_fcm
949echo "%MUPHY         $MUPHY_PATH"    >> $config_fcm
950echo "%AERONO        $AERONO_PATH"   >> $config_fcm
951echo "%EVOLUTION     $EVOLUTION_PATH"  >> $config_fcm
952echo "%CPP_KEY       $CPP_KEY"       >> $config_fcm
953echo "%EXEC          $code"          >> $config_fcm
954echo "%SUFF_NAME     $SUFF_NAME"     >> $config_fcm
955echo "%SUFF_DIR      $SUFF_DIR"      >> $config_fcm
956echo "%COMPIL_FFLAGS $COMPIL_FFLAGS" >> $config_fcm
957echo "%PARA_FFLAGS   $PARA_FFLAGS"   >> $config_fcm
958echo "%PARA_LD       $PARA_LD"       >> $config_fcm
959echo "%EXT_SRC       $EXT_SRC"       >> $config_fcm
960
961# setup arch.fcm and arch.opt
962rm -f arch.fcm
963rm -f arch.opt
964ln -s arch/arch-${arch}.fcm arch.fcm
965if test -f arch/arch-${arch}.opt &&  [ $compil_mod = "prod" ]
966  then
967  ln -s arch/arch-${arch}.opt arch.opt
968else
969  ln -s .void_file arch.opt
970fi
971
972# cleanup before compiling
973if [[ $libphy != "true" ]]; then
974  rm -f bin/${code}${SUFF_NAME}
975fi
976rm -f $LIBOGCM/${arch}${SUFF_DIR}/.config/fcm.bld.lock
977
978function delete_dim_h {
979  if [[ -r $LIBFGCM/grid/dimensions.h ]];then
980    echo "Removing dimensions.h"
981    \rm -f $LIBFGCM/grid/dimensions.h
982  fi
983}
984
985if [[ $clean == "true" ]];then
986    echo "-- Cleaning up folder $LIBOGCM/${arch}${SUFF_DIR} --"
987    delete_dim_h
988    rm -rf tmp_src
989    rm -rf config
990    rm -rf $LIBOGCM/${arch}${SUFF_DIR}
991    exit 0
992fi
993
994./build_gcm ${fcm_path} -j $job $full || (echo "Build gcm failed."; delete_dim_h; exit 1)
995
996rm -rf tmp_src
997rm -rf config
998ln -s $LIBOGCM/${arch}${SUFF_DIR}/.config config
999ln -s $LIBOGCM/${arch}${SUFF_DIR}/.config/ppsrc tmp_src
1000
1001delete_dim_h
Note: See TracBrowser for help on using the repository browser.