Skip to content

Commit cb522d4

Browse files
committed
Auto-install environment setup
1 parent 9d190ec commit cb522d4

File tree

4 files changed

+129
-51
lines changed

4 files changed

+129
-51
lines changed

scripts/build.sh

Lines changed: 126 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -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,12 +138,18 @@ 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
125147
setup_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

@@ -146,6 +169,51 @@ install_precommit_if_git() {
146169
return 0
147170
}
148171

172+
# Determine the RC file for the current shell
173+
get_shell_rc_file() {
174+
shell_name=$(basename "${SHELL}")
175+
case "${shell_name}" in
176+
bash) printf '%s\n' "${HOME}/.bashrc" ;;
177+
zsh) printf '%s\n' "${HOME}/.zshrc" ;;
178+
ksh) printf '%s\n' "${HOME}/.kshrc" ;;
179+
*) log warning "Unknown shell: ${shell_name}"; printf '%s\n' "${HOME}/.bashrc" ;;
180+
esac
181+
}
182+
183+
sentinel_marker_='celeritas-build-env'
184+
185+
# Check if sentinel marker exists in rc file
186+
has_sentinel() {
187+
rc_file=$1
188+
if [ ! -f "${rc_file}" ]; then
189+
return 1
190+
fi
191+
grep -q "${sentinel_marker_}" "${rc_file}" 2>/dev/null
192+
}
193+
194+
# Install environment sourcing to shell rc file
195+
install_shell_env() {
196+
rc_file=$1
197+
198+
if has_sentinel "${rc_file}"; then
199+
log debug "Skipping modification of ${rc_file}: sentinel exists"
200+
return 1
201+
fi
202+
203+
log info "Installing environment sourcing to ${rc_file}"
204+
cat >> "${rc_file}" <<EOF
205+
# >>> ${sentinel_marker_} >>>
206+
load_system_env() {
207+
CELER_SOURCE_DIR="${CELER_SOURCE_DIR}"
208+
if [ -d "\${CELER_SOURCE_DIR}" ]; then
209+
. "\${CELER_SOURCE_DIR}/scripts/env/\$1.sh"
210+
fi
211+
}
212+
load_system_env "${SYSTEM_NAME}"
213+
# <<< ${sentinel_marker_} <<<
214+
EOF
215+
}
216+
149217
#-----------------------------------------------------------------------------#
150218

151219
# Determine the source directory from the build script
@@ -155,22 +223,48 @@ export CELER_SOURCE_DIR="$(cd "$(dirname "$0")"/.. && pwd)"
155223
cd ${CELER_SOURCE_DIR}
156224

157225
# Determine system name, failing on an empty string
158-
SYSTEM_NAME=$(fancy_hostname)
226+
SYSTEM_NAME="$(fancy_hostname)"
159227
if [ -z "${SYSTEM_NAME}" ]; then
160228
log warning "Could not determine SYSTEM_NAME from LMOD_SYSTEM_NAME or HOSTNAME"
161229
log error "Empty SYSTEM_NAME, needed to load environment and presets"
162230
exit 1
163231
fi
164232

165233
# 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 '')"
234+
OLD_CMAKE="$(find_command cmake)"
235+
OLD_PRE_COMMIT="$(find_command pre-commit)"
236+
OLD_XDG_CACHE_HOME="${XDG_CACHE_HOME}"
168237

169238
# Load environment paths using the system name
170-
load_system_env "${SYSTEM_NAME}"
239+
ENV_SCRIPT="$(get_system_env "${SYSTEM_NAME}")"
240+
if [ -n "${ENV_SCRIPT}" ]; then
241+
source_script "${ENV_SCRIPT}"
242+
fi
243+
CMAKE="$(find_command cmake)"
171244

172-
NEW_CMAKE="$(command -v cmake 2>/dev/null || printf 'cmake unavailable')"
173-
NEW_PRE_COMMIT="$(command -v pre-commit 2>/dev/null || printf '')"
245+
# Check whether the environment changed
246+
needs_env=false
247+
if [ "$(find_command cmake)" != "${OLD_PRE_COMMIT}" ]; then
248+
log warning "Local environment script uses a different pre-commit than your \$PATH"
249+
needs_env=true
250+
fi
251+
if [ "${CMAKE}" != "${OLD_CMAKE}" ]; then
252+
log warning "Local environment script uses a different CMake than your \$PATH"
253+
needs_env=true
254+
fi
255+
if [ "${OLD_XDG_CACHE_HOME}" != "${XDG_CACHE_HOME}" ]; then
256+
log warning "Cache directory changed from XDG_CACHE_HOME=${OLD_XDG_CACHE_HOME} to ${XDG_CACHE_HOME}"
257+
needs_env=true
258+
fi
259+
if ${needs_env}; then
260+
rc_file="$(get_shell_rc_file)"
261+
if install_shell_env "${rc_file}" "${ENV_SCRIPT}"; then
262+
needs_env=false
263+
else
264+
log warning "Please manually add the following to ${rc_file}:"
265+
printf ' . %s\n' "${ENV_SCRIPT}" >&2
266+
fi
267+
fi
174268

175269
# Link preset file
176270
ln_presets "${SYSTEM_NAME}"
@@ -179,16 +273,16 @@ ln_presets "${SYSTEM_NAME}"
179273
setup_ccache
180274

181275
# Check arguments and give presets if missing
182-
if [ $# -eq 0 ]; then
276+
if [ $# -eq 0 ] ; then
183277
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
278+
if [ -n "${CMAKE}" ]; then
191279
log error "cmake unavailable: cannot call --list-presets"
280+
exit 1
281+
fi
282+
if "${CMAKE}" --list-presets >&2 ; then
283+
log info "Try using the '${SYSTEM_NAME}' or 'dev' presets ('base'), or 'default' if not"
284+
else
285+
log error "CMake may be too old or JSON file may be broken"
192286
fi
193287
exit 2
194288
fi
@@ -209,15 +303,11 @@ if cmake --build --preset="${CMAKE_PRESET}"; then
209303
fi
210304

211305
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
221306
else
222307
log error "build failed: check configuration and build errors above"
223308
fi
309+
310+
if ${needs_env}; then
311+
log warning "Environment changed: please manually add the following to ${rc_file}:"
312+
printf ' . %s\n' "${ENV_SCRIPT}" >&2
313+
fi

scripts/env/faraday.sh

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55
#-----------------------------------------------------------------------------#
66

77
if ! command -v load_system_env >/dev/null 2>&1; then
8-
printf 'error: define a function "load_system_env" in your shell rc:
9-
load_system_env() {
10-
. ${CELER_SOURCE_DIR}/scripts/env/$1.sh
11-
}
12-
' >&2
8+
printf "error: expected load_system_env helper function via build.sh or shell\n" >&2
139
return 1
1410
fi
1511

scripts/env/hudson.sh

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55
#-----------------------------------------------------------------------------#
66

77
if ! command -v load_system_env >/dev/null 2>&1; then
8-
printf 'error: define a function "load_system_env" in your .bashrc:
9-
load_system_env() {
10-
. "${CELER_SOURCE_DIR}/scripts/env/$1.sh"
11-
}
12-
' >&2
8+
printf "error: expected load_system_env helper function via build.sh or shell\n" >&2
139
return 1
1410
fi
1511

scripts/env/milan0.sh

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55
#-----------------------------------------------------------------------------#
66

77
if ! command -v load_system_env >/dev/null 2>&1; then
8-
printf 'error: define a function "load_system_env" in your .bashrc:
9-
load_system_env() {
10-
. "${CELER_SOURCE_DIR}/scripts/env/$1.sh"
11-
}
12-
' >&2
8+
printf "error: expected load_system_env helper function via build.sh or shell\n" >&2
139
return 1
1410
fi
1511

0 commit comments

Comments
 (0)