From e5f3bf5474aff61ea6fb79f9e0c6dcf782cfae64 Mon Sep 17 00:00:00 2001
From: Dmitry Kalinkin <dmitry.kalinkin@gmail.com>
Date: Sat, 23 Sep 2023 20:51:12 -0400
Subject: [PATCH] add bin/build_eicrecon.sh (like the build_detector.sh)

---
 .gitlab-ci.yml        | 11 ++++++++++
 bin/build_eicrecon.sh | 48 +++++++++++++++++++++++++++++++++++++++++++
 bin/env.sh            |  6 ++++++
 3 files changed, 65 insertions(+)
 create mode 100755 bin/build_eicrecon.sh

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 6a3e731..02b605e 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -69,6 +69,15 @@ common:detector:
     - print_env.sh
     - ls -lrth
 
+common:eicrecon:
+  stage: initialize
+  rules:
+    - if: '$CI_SERVER_HOST == "eicweb.phy.anl.gov"'
+  needs: ["common:setup"]
+  script:
+    - source .local/bin/env.sh && build_eicrecon.sh
+
+
 detector_benchmarks:
   stage: benchmarks
   rules:
@@ -85,7 +94,9 @@ detector_benchmarks:
         DETECTOR_CONFIG: 'epic_arches'
         DETECTOR_VERSION: 'main'
         DETECTOR_REPOSITORYURL: 'https://github.com/eic/epic.git'
+        EICRECON_REPOSITORYURL: 'https://github.com/eic/EICrecon.git'
       - DETECTOR: 'epic'
         DETECTOR_CONFIG: 'epic_brycecanyon'
         DETECTOR_VERSION: 'main'
         DETECTOR_REPOSITORYURL: 'https://github.com/eic/epic.git'
+        EICRECON_REPOSITORYURL: 'https://github.com/eic/EICrecon.git'
diff --git a/bin/build_eicrecon.sh b/bin/build_eicrecon.sh
new file mode 100755
index 0000000..14851c5
--- /dev/null
+++ b/bin/build_eicrecon.sh
@@ -0,0 +1,48 @@
+#!/bin/bash
+
+## =============================================================================
+## Build and install the EICrecon package into our local prefix
+## =============================================================================
+
+## =============================================================================
+## Load the environment variables. To build the detector we need the following
+## variables:
+##
+## - LOCAL_PREFIX:     location where local packages should be installed
+
+if [ -n "${LOCAL_PREFIX}" ] ; then 
+  source .local/bin/env.sh
+else
+  source ${LOCAL_PREFIX}/bin/env.sh
+fi
+
+
+## =============================================================================
+## Step 1: download/update the detector definitions (if needed)
+pushd "$(mktemp -d)"
+
+echo "Fetching EICrecon"
+if [ -n "${DETECTOR_DEPLOY_TOKEN_USERNAME:-}" -a -n "${DETECTOR_DEPLOY_TOKEN_PASSWORD:-}" ]; then
+  DEPLOY_TOKEN="${DETECTOR_DEPLOY_TOKEN_USERNAME}:${DETECTOR_DEPLOY_TOKEN_PASSWORD}@"
+  echo "Deploy token for ${DETECTOR_DEPLOY_TOKEN_USERNAME} is masked in the next line."
+else
+  DEPLOY_TOKEN=""
+fi
+echo "git clone -b ${EICRECON_VERSION} --depth 1 ${EICRECON_REPOSITORYURL:-https://eicweb.phy.anl.gov/EIC/eicrecons/EICrecon.git} EICrecon"
+git clone -b ${EICRECON_VERSION} --depth 1 ${EICRECON_REPOSITORYURL:-https://${DEPLOY_TOKEN}eicweb.phy.anl.gov/EIC/eicrecons/EICrecon.git} EICrecon
+rm -rf "EICrecon/.git"
+
+## =============================================================================
+## Step 2: Compile and install the detector definition
+echo "Building and installing the ${DETECTOR} package"
+
+cd EICrecon
+cmake -DCMAKE_INSTALL_PREFIX=${LOCAL_PREFIX} && make -j$(($(nproc)/4+1)) install || exit 1
+TO_REMOVE="$PWD"
+popd
+rm -rf "$TO_REMOVE"
+unset TO_REMOVE
+
+## =============================================================================
+## Step 3: That's all!
+echo "Detector build/install complete!"
diff --git a/bin/env.sh b/bin/env.sh
index e1c9e1f..190931b 100755
--- a/bin/env.sh
+++ b/bin/env.sh
@@ -127,6 +127,12 @@ echo "LOCAL_DATA_PATH=${LOCAL_DATA_PATH}" >> .env
 echo "Adding LOCAL_PREFIX to PATH and LD_LIBRARY_PATH"
 export PATH=${LOCAL_PREFIX}/bin:${PATH}
 export LD_LIBRARY_PATH=${LOCAL_PREFIX}/lib:${LD_LIBRARY_PATH}
+if [ -d "${LOCAL_PREFIX}/lib/EICrecon/plugins" ]; then
+    echo "Using EICrecon plugins from ${LOCAL_PREFIX}/lib/EICrecon/plugins"
+    export JANA_PLUGIN_PATH=${LOCAL_PREFIX}/lib/EICrecon/plugins
+else
+    echo "The ${LOCAL_PREFIX}/lib/EICrecon/plugins doesn't exist"
+fi
 
 # Local field maps
 mkdir -p ${LOCAL_DATA_PATH}/fieldmaps
-- 
GitLab