From 9870ee1dcbc9ca2c13d58c6f045b1a74d121fc93 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Riette?= <sebastien.riette@meteo.fr>
Date: Fri, 14 Apr 2023 11:40:11 +0200
Subject: [PATCH] S. Riette 14 Apr 2023: bf for compilation from within the
 repository

---
 build/with_fcm/make_fcm.sh      | 55 ++++++++++++++++++++++++++-------
 tools/check_commit_testprogs.sh | 34 +++-----------------
 2 files changed, 49 insertions(+), 40 deletions(-)

diff --git a/build/with_fcm/make_fcm.sh b/build/with_fcm/make_fcm.sh
index 8bace3ada..20d1f0c29 100755
--- a/build/with_fcm/make_fcm.sh
+++ b/build/with_fcm/make_fcm.sh
@@ -1,6 +1,7 @@
 #!/bin/bash
 
 set -e
+#set -x
 
 fcm_version=tags/2021.05.0
 fiat_version=1295120464c3905e5edcbb887e4921686653eab8
@@ -11,6 +12,8 @@ function parse_args() {
   ARCH=
   GMKFILE=
   MESONHPROFILE=
+  useexpand=1
+  commit=""
   # pass unrecognized arguments to fcm
   FCM_ARGS=""
   
@@ -25,6 +28,8 @@ $0 [options]
 --arch ARCH  	        build using arch files $ARCH_PATH/arch-ARCH.* [gnu]
 --gmkfile FILE        build using a gmkpack configuration file (--arch must be used to give a name to the build dir)
 --mesonhprofile FILE  build using Méso-NH profile and rules (--arch must be used to give a name to the build dir)
+--noexpand            do not use mnh_expand (code will be in array-syntax)"
+--commit              commit hash (or a directory) to test; do not use this option from within a repository
 
 Unrecognized options are passed to the fcm build command. Useful options include :
 --new                   clean build tree before building
@@ -44,6 +49,8 @@ EOF
         GMKFILE=$1 ; shift ;;
       "--mesonhprofile")
         MESONHPROFILE=$1 ; shift ;;
+      '--noexpand') useexpand=0;;
+      '--commit') commit=$1; shift;;
       *)
         FCM_ARGS="$FCM_ARGS $OPTION" ;;
     esac
@@ -250,7 +257,7 @@ check_install_fcm
 # Check the fiat installation
 check_install_fiat
 
-# Create the build directory and populate it
+# Create the build directory and set up the build system
 builddir=arch_$ARCH
 if [ -d $builddir ]; then
   echo "$builddir already exists. To rerun compilation, please enter this directory and use the compilation.sh script."
@@ -270,26 +277,52 @@ else
 fi
 cp fcm-make.cfg $builddir
 cd $builddir
-mkdir src
-cd src
-if [ -d ../../../../src/common ]; then
-  #We compile directly from a PHYEX repository
-  ln -s ../../../../src/common/* .
-  ln -s ../../../../src/testprogs/* .
+
+# Populate the source directory with (modified) PHYEX source code
+[ "$commit" == "" ] && commit=$PWD/../../.. #Current script run from within a PHYEX repository
+if echo $commit | grep '/' | grep -v '^tags/' > /dev/null; then
+  # We get the source code directly from a directory
+  fromdir=$commit
+else
+  # We use a commit to checkout
+  fromdir=''
+fi
+#Expand options
+if [ $useexpand == 1 ]; then
+  expand_options="-D MNH_EXPAND -D MNH_EXPAND_LOOP"
 else
-  #We compile after an execution of prep_code
-  ln -s ../../../../src/* .
+  expand_options=""
 fi
+PHYEXTOOLSDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"/../../../tools #if run from within a PHYEX repository
+UPDATEDPATH=$PATH
+which prep_code.sh > /dev/null || export UPDATEDPATH=$PHYEXTOOLSDIR:$PATH
+subs="$subs -s turb -s shallow -s turb_mnh -s micro -s aux -s ice_adjust -s rain_ice -s rain_ice_old -s support"
+if [ "$fromdir" == '' ]; then
+  echo "Clone repository, and checkout commit $commit (using prep_code.sh)"
+  if [[ $commit == testprogs${separator}* ]]; then
+    PATH=$UPDATEDPATH prep_code.sh -c $commit src #This commit is ready for inclusion
+  else
+    PATH=$UPDATEDPATH prep_code.sh -c $commit $expand_options $subs -m testprogs src
+  fi
+else
+  echo "Copy $fromdir"
+  mkdir src
+  scp -q -r $fromdir/src src/
+  PATH=$UPDATEDPATH prep_code.sh $expand_options $subs -m testprogs src
+fi
+
+# Add some code
+cd src
 ln -s ../../fiat/src fiat
 cat <<EOF > dummyprog.F90
 PROGRAM DUMMYPROG
   PRINT*, "CREATED TO FORCE FCM TO LINK SOMETHING"
 END PROGRAM DUMMYPROG
 EOF
+
+# Build the compilation script and run it
 cd ..
 build_compilation_script src
-
-# Run the compilation
 ./compilation.sh
 ln -s build/bin/libphyex.so .
 
diff --git a/tools/check_commit_testprogs.sh b/tools/check_commit_testprogs.sh
index 0af5c3fc1..77e43312f 100755
--- a/tools/check_commit_testprogs.sh
+++ b/tools/check_commit_testprogs.sh
@@ -78,7 +78,7 @@ commit=""
 reference=""
 tests=""
 suppress=0
-useexpand=1
+useexpand=""
 archfile=$defaultarchfile
 refarchfile=$defaultarchfile
 
@@ -90,7 +90,7 @@ while [ -n "$1" ]; do
     '-r') run=$(($run+1));;
     '-C') check=1;;
     '-t') tests="$2"; shift;;
-    '--noexpand') useexpand=0;;
+    '--noexpand') useexpand=$1;;
     '--repo-user') export PHYEXREPOuser=$2; shift;;
     '--repo-protocol') export PHYEXREPOprotocol=$2; shift;;
     '-a') archfile="$2"; shift;;
@@ -149,9 +149,7 @@ if [ $check -eq 1 -a -z "${reference-}" ]; then
   exit 3
 fi
 
-fromdir=''
 if echo $commit | grep '/' | grep -v '^tags/' > /dev/null; then
-  fromdir=$commit
   name=$(echo $commit | sed 's/\//'${separator}'/g' | sed 's/:/'${separator}'/g' | sed 's/\./'${separator}'/g')
   [ $suppress -eq 1 -a -d $TESTDIR/$name ] && rm -rf $TESTDIR/$name
 elif echo $specialName | grep -w $commit > /dev/null; then
@@ -187,36 +185,14 @@ if [ $compilation -eq 1 ]; then
   fi
   mkdir $TESTDIR/$name
   cd $TESTDIR/$name/
+  cp -r $PHYEXTOOLSDIR/../build . #We use the compilation system from the same commit as the current script
 
   MNH_EXPAND_DIR=$PHYEXTOOLSDIR/mnh_expand
-  export PATH=$MNH_EXPAND_DIR/filepp:$MNH_EXPAND_DIR/MNH_Expand_Array:$PATH
-
-  if [ $useexpand == 1 ]; then
-    expand_options="-D MNH_EXPAND -D MNH_EXPAND_LOOP"
-  else
-    expand_options=""
-  fi
-  subs="$subs -s turb -s shallow -s turb_mnh -s micro -s aux -s ice_adjust -s rain_ice -s rain_ice_old -s support"
-  prep_code=$PHYEXTOOLSDIR/prep_code.sh
-
-  if [ "$fromdir" == '' ]; then
-    echo "Clone repository, and checkout commit $commit (using prep_code.sh)"
-    if [[ $commit == testprogs${separator}* ]]; then
-      $prep_code -c $commit src #This commit is ready for inclusion
-    else
-      $prep_code -c $commit $expand_options $subs -m testprogs src
-    fi
-  else
-    echo "Copy $fromdir"
-    mkdir src
-    scp -q -r $fromdir/src src/
-    $prep_code $expand_options $subs -m testprogs src
-  fi
-  cp -r $PHYEXTOOLSDIR/../build . #We use the compilation system from the same commit as the current script
+  export PATH=$PHYEXTOOLSDIR:$MNH_EXPAND_DIR/filepp:$MNH_EXPAND_DIR/MNH_Expand_Array:$PATH
 
   cd $TESTDIR/$name/build/with_fcm/
   rm -rf arch_*
-  ./make_fcm.sh --arch $archfile 2>&1 | tee Output_compilation
+  ./make_fcm.sh $useexpand --commit $commit --arch $archfile 2>&1 | tee Output_compilation
 fi
 
 if [ $run -ge 1 ]; then
-- 
GitLab