@@ -59,19 +59,36 @@ fancy_hostname() {
5959 printf ' %s\n' " ${sys} "
6060}
6161
62- # Load environment (make sure this is not executed as a subshell)
63- load_system_env () {
62+ # Determine the environment script for the given hostname
63+ get_system_env () {
6464 ENV_SCRIPT=" ${CELER_SOURCE_DIR} /scripts/env/$1 .sh"
65- if [ -f " ${ENV_SCRIPT} " ]; then
66- log info " Sourcing environment script at ${ENV_SCRIPT} "
67- set +e
68- if ! . " $ENV_SCRIPT " ; then
69- log error " Environment setup for $1 failed"
70- exit 1
71- fi
72- set -e
73- else
65+ if [ ! -f " ${ENV_SCRIPT} " ]; then
7466 log debug " No environment script exists at ${ENV_SCRIPT} "
67+ else
68+ log info " Found environment script at ${ENV_SCRIPT} "
69+ printf ' %s\n' " ${ENV_SCRIPT} "
70+ fi
71+ }
72+
73+ # Source a script safely (make sure this is not executed as a subshell)
74+ source_script () {
75+ set +e
76+ if ! . " $1 " ; then
77+ log error " Failed to source '$1 '"
78+ exit 1
79+ fi
80+ _celer_build=0
81+ set -e
82+ }
83+
84+ # Allow downstream scripts (e.g., excl) to load additional source files
85+ load_system_env () {
86+ env_script_=" $( get_system_env " ${1} " ) "
87+ if [ -z " ${env_script_} " ]; then
88+ log error " Failed to find environment script for '$1 '"
89+ return 1
90+ else
91+ source_script " ${env_script_} "
7592 fi
7693}
7794
@@ -121,31 +138,81 @@ check_ccache_usage() {
121138 fi
122139}
123140
141+ # Find a command in PATH, suppressing error messages
142+ find_command () {
143+ command -v " $1 " 2> /dev/null || printf " "
144+ }
145+
124146# Auto-detect and configure ccache if available
125147setup_ccache () {
126- if CCACHE_PROGRAM=" $( command -v ccache 2> /dev/null) " ; then
148+ CCACHE_PROGRAM=" $( find_command ccache) "
149+ if [ -n " ${CCACHE_PROGRAM} " ]; then
127150 log info " Using ccache: ${CCACHE_PROGRAM} "
128151 check_ccache_usage
129- [ -n " ${CCACHE_PROGRAM} " ] && export CCACHE_PROGRAM
152+ export CCACHE_PROGRAM
130153 fi
131154}
132155
133156# Check if pre-commit hook is installed and install if missing
134157install_precommit_if_git () {
135- if git_dir=$( git rev-parse --git-dir 2> /dev/null) ; then
136- :
137- else
158+ if ! git_hook_path=" $( git rev-parse --git-path hooks/pre-commit 2> /dev/null) " ; then
138159 log debug " Not in a git repository, skipping pre-commit check"
139160 return 1
140161 fi
162+ log debug " Checking for pre-commit hooks at '${git_hook_path} '"
141163
142- if [ ! -f " ${git_dir} /hooks/pre-commit " ]; then
164+ if [ ! -f " ${git_hook_path} " ]; then
143165 log info " Pre-commit hook not found, installing commit hooks"
144166 ./scripts/dev/install-commit-hooks.sh
145167 fi
146168 return 0
147169}
148170
171+ # Determine the RC file for the current shell
172+ get_shell_rc_file () {
173+ shell_name=$( basename " ${SHELL} " )
174+ case " ${shell_name} " in
175+ bash) printf ' %s\n' " ${HOME} /.bashrc" ;;
176+ zsh) printf ' %s\n' " ${HOME} /.zshrc" ;;
177+ ksh) printf ' %s\n' " ${HOME} /.kshrc" ;;
178+ * ) log warning " Unknown shell: ${shell_name} " ; printf ' %s\n' " ${HOME} /.bashrc" ;;
179+ esac
180+ }
181+
182+ sentinel_marker_=' celeritas-build-env'
183+
184+ # Check if sentinel marker exists in rc file
185+ has_sentinel () {
186+ rc_file=$1
187+ if [ ! -f " ${rc_file} " ]; then
188+ return 1
189+ fi
190+ grep -q " ${sentinel_marker_} " " ${rc_file} " 2> /dev/null
191+ }
192+
193+ # Install environment sourcing to shell rc file
194+ install_shell_env () {
195+ rc_file=$1
196+
197+ if has_sentinel " ${rc_file} " ; then
198+ log debug " Skipping modification of ${rc_file} : sentinel exists"
199+ return 1
200+ fi
201+
202+ log info " Installing environment sourcing to ${rc_file} "
203+ cat >> " ${rc_file} " << EOF
204+ # >>> ${sentinel_marker_} >>>
205+ load_system_env() {
206+ CELER_SOURCE_DIR="${CELER_SOURCE_DIR} "
207+ if [ -d "\$ {CELER_SOURCE_DIR}" ]; then
208+ . "\$ {CELER_SOURCE_DIR}/scripts/env/\$ 1.sh"
209+ fi
210+ }
211+ load_system_env "${SYSTEM_NAME} "
212+ # <<< ${sentinel_marker_} <<<
213+ EOF
214+ }
215+
149216# -----------------------------------------------------------------------------#
150217
151218# Determine the source directory from the build script
@@ -155,22 +222,48 @@ export CELER_SOURCE_DIR="$(cd "$(dirname "$0")"/.. && pwd)"
155222cd ${CELER_SOURCE_DIR}
156223
157224# Determine system name, failing on an empty string
158- SYSTEM_NAME=$( fancy_hostname)
225+ SYSTEM_NAME=" $( fancy_hostname) "
159226if [ -z " ${SYSTEM_NAME} " ]; then
160227 log warning " Could not determine SYSTEM_NAME from LMOD_SYSTEM_NAME or HOSTNAME"
161228 log error " Empty SYSTEM_NAME, needed to load environment and presets"
162229 exit 1
163230fi
164231
165232# Check whether cmake/pre-commit change from environment
166- OLD_CMAKE=" $( command -v cmake 2> /dev/null || printf ' ' ) "
167- OLD_PRE_COMMIT=" $( command -v pre-commit 2> /dev/null || printf ' ' ) "
233+ OLD_CMAKE=" $( find_command cmake) "
234+ OLD_PRE_COMMIT=" $( find_command pre-commit) "
235+ OLD_XDG_CACHE_HOME=" ${XDG_CACHE_HOME} "
168236
169237# Load environment paths using the system name
170- load_system_env " ${SYSTEM_NAME} "
238+ ENV_SCRIPT=" $( get_system_env " ${SYSTEM_NAME} " ) "
239+ if [ -n " ${ENV_SCRIPT} " ]; then
240+ source_script " ${ENV_SCRIPT} "
241+ fi
242+ CMAKE=" $( find_command cmake) "
171243
172- NEW_CMAKE=" $( command -v cmake 2> /dev/null || printf ' cmake unavailable' ) "
173- NEW_PRE_COMMIT=" $( command -v pre-commit 2> /dev/null || printf ' ' ) "
244+ # Check whether the environment changed
245+ needs_env=false
246+ if [ " $( find_command cmake) " != " ${OLD_PRE_COMMIT} " ]; then
247+ log warning " Local environment script uses a different pre-commit than your \$ PATH"
248+ needs_env=true
249+ fi
250+ if [ " ${CMAKE} " != " ${OLD_CMAKE} " ]; then
251+ log warning " Local environment script uses a different CMake than your \$ PATH"
252+ needs_env=true
253+ fi
254+ if [ " ${OLD_XDG_CACHE_HOME} " != " ${XDG_CACHE_HOME} " ]; then
255+ log warning " Cache directory changed from XDG_CACHE_HOME=${OLD_XDG_CACHE_HOME} to ${XDG_CACHE_HOME} "
256+ needs_env=true
257+ fi
258+ if ${needs_env} ; then
259+ rc_file=" $( get_shell_rc_file) "
260+ if install_shell_env " ${rc_file} " " ${ENV_SCRIPT} " ; then
261+ needs_env=false
262+ else
263+ log warning " Please manually add the following to ${rc_file} :"
264+ printf ' . %s\n' " ${ENV_SCRIPT} " >&2
265+ fi
266+ fi
174267
175268# Link preset file
176269ln_presets " ${SYSTEM_NAME} "
@@ -179,16 +272,16 @@ ln_presets "${SYSTEM_NAME}"
179272setup_ccache
180273
181274# Check arguments and give presets if missing
182- if [ $# -eq 0 ]; then
275+ if [ $# -eq 0 ] ; then
183276 printf ' %s\n' " Usage: $0 PRESET [config_args...]" >&2
184- if command -v cmake > /dev/null 2>&1 ; then
185- if cmake --list-presets >&2 ; then
186- log info " Try using the '${SYSTEM_NAME} ' or 'dev' presets ('base'), or 'default' if not"
187- else
188- log error " CMake may be too old or JSON file may be broken"
189- fi
190- else
277+ if [ -n " ${CMAKE} " ]; then
191278 log error " cmake unavailable: cannot call --list-presets"
279+ exit 1
280+ fi
281+ if " ${CMAKE} " --list-presets >&2 ; then
282+ log info " Try using the '${SYSTEM_NAME} ' or 'dev' presets ('base'), or 'default' if not"
283+ else
284+ log error " CMake may be too old or JSON file may be broken"
192285 fi
193286 exit 2
194287fi
@@ -209,15 +302,11 @@ if cmake --build --preset="${CMAKE_PRESET}"; then
209302 fi
210303
211304 install_precommit_if_git
212- if [ " ${NEW_PRE_COMMIT} " != " ${OLD_PRE_COMMIT} " ]; then
213- log warning " Local environment script uses a different pre-commit than your \$ PATH:"
214- log info " Recommend adding '. ${CELER_SOURCE_DIR} /${ENV_SCRIPT} ' to your shell rc"
215- fi
216-
217- if [ " ${NEW_CMAKE} " != " ${OLD_CMAKE} " ]; then
218- log warning " Local environment script uses a different CMake than your \$ PATH:"
219- log info " Recommend adding '. ${CELER_SOURCE_DIR} /${ENV_SCRIPT} ' to your shell rc"
220- fi
221305else
222306 log error " build failed: check configuration and build errors above"
223307fi
308+
309+ if ${needs_env} ; then
310+ log warning " Environment changed: please manually add the following to ${rc_file} :"
311+ printf ' . %s\n' " ${ENV_SCRIPT} " >&2
312+ fi
0 commit comments