Skip to content

Commit 605750a

Browse files
committed
Ensure enforce.sh doesn't use unset variables
Test this by using `set -u` in build scripts
1 parent 0cbf1c0 commit 605750a

File tree

5 files changed

+45
-42
lines changed

5 files changed

+45
-42
lines changed

ci/build.sh

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
#!/bin/bash
22
#
33
# Copyright 2017 - 2022 James E. King III
4+
# Copyright 2020 - 2025 Alexander Grund
45
# Distributed under the Boost Software License, Version 1.0.
56
# (See accompanying file LICENSE_1_0.txt or copy at
67
# http://www.boost.org/LICENSE_1_0.txt)
78
#
89
# Bash script to perform a bjam build
910
#
1011

11-
set -ex
12+
set -eux
1213

1314
: "${B2_TARGETS:="libs/$SELF/test"}"
1415

1516
. "$(dirname "${BASH_SOURCE[0]}")"/enforce.sh
1617

17-
export UBSAN_OPTIONS=print_stacktrace=1,report_error_type=1,${UBSAN_OPTIONS}
18+
export UBSAN_OPTIONS=print_stacktrace=1,report_error_type=1,${UBSAN_OPTIONS:-}
1819

1920
cd "$BOOST_ROOT"
2021

@@ -33,9 +34,9 @@ if [[ -f "$b2_config" ]]; then
3334
fi
3435

3536
# shellcheck disable=SC2086
36-
${B2_WRAPPER} ./b2 ${B2_TARGETS} "${B2_ARGS[@]}" "$@"
37+
${B2_WRAPPER:-} ./b2 ${B2_TARGETS} "${B2_ARGS[@]}" "$@"
3738

38-
if [ "$B2_USE_CCACHE" == "1" ] && command -v ccache &> /dev/null; then
39+
if [ "${B2_USE_CCACHE:-0}" == "1" ] && command -v ccache &> /dev/null; then
3940
echo "CCache summary"
4041
ccache -s
4142
fi

ci/codecov.sh

+6-5
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
# a pull request.
2424
#
2525

26-
set -ex
26+
set -eux
2727

2828
. "$(dirname "${BASH_SOURCE[0]}")"/enforce.sh
2929

30-
coverage_action=$1
30+
coverage_action=${1:-'<unset>'}
3131

3232
if [[ "$coverage_action" == "setup" ]]; then
33-
if [ -z "$B2_CI_VERSION" ]; then
33+
if [ -z "${B2_CI_VERSION:-}" ]; then
3434
# Old CI version needs to use the prefixes
3535
export B2_VARIANT="variant=debug"
3636
export B2_CXXFLAGS="${B2_CXXFLAGS:+$B2_CXXFLAGS }cxxflags=-fkeep-static-functions cxxflags=--coverage"
@@ -42,7 +42,7 @@ if [[ "$coverage_action" == "setup" ]]; then
4242
fi
4343

4444
elif [[ "$coverage_action" == "collect" ]] || [[ "$coverage_action" == "upload" ]]; then
45-
if [ -z "$GCOV" ]; then
45+
if [ -z "${GCOV:-}" ]; then
4646
ver=7 # default
4747
if [ "${B2_TOOLSET%%-*}" == "gcc" ]; then
4848
if [[ "$B2_TOOLSET" =~ gcc- ]]; then
@@ -55,6 +55,7 @@ elif [[ "$coverage_action" == "collect" ]] || [[ "$coverage_action" == "upload"
5555
fi
5656

5757
: "${LCOV_VERSION:=v2.3}" # Set default lcov version to install
58+
: "${LCOV_OPTIONS:=}"
5859

5960
: "${LCOV_BRANCH_COVERAGE:=1}" # Set default for branch coverage
6061

@@ -136,7 +137,7 @@ elif [[ "$coverage_action" == "collect" ]] || [[ "$coverage_action" == "upload"
136137
# pull request)
137138
lcov ${LCOV_OPTIONS} --list coverage.info
138139

139-
if [[ "$coverage_action" == "upload" ]] && [[ "$BOOST_CI_CODECOV_IO_UPLOAD" != "skip" ]]; then
140+
if [[ "$coverage_action" == "upload" ]] && [[ "${BOOST_CI_CODECOV_IO_UPLOAD:-}" != "skip" ]]; then
140141
#
141142
# upload to codecov.io
142143
#

ci/common_install.sh

+11-10
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function print_on_gha {
3535
}
3636

3737
# Setup ccache
38-
if [ "$B2_USE_CCACHE" == "1" ]; then
38+
if [ "${B2_USE_CCACHE:-}" == "1" ]; then
3939
if ! "$CI_DIR"/setup_ccache.sh 2>&1; then
4040
{ set +x; } &> /dev/null
4141
echo
@@ -59,7 +59,7 @@ print_on_gha "::endgroup::"
5959
print_on_gha "::group::Checkout and setup Boost build tree"
6060
pythonexecutable=$(get_python_executable)
6161

62-
if [ -z "$SELF" ]; then
62+
if [ -z "${SELF:-}" ]; then
6363
export SELF=$($pythonexecutable "$CI_DIR/get_libname.py")
6464
fi
6565

@@ -92,6 +92,7 @@ cp -r "$BOOST_CI_SRC_FOLDER"/* "libs/$SELF"
9292
export BOOST_ROOT="$(pwd)"
9393
export PATH="$(pwd):$PATH"
9494

95+
git --version
9596
DEPINST_ARGS=()
9697
if [[ -n "$GIT_FETCH_JOBS" ]]; then
9798
DEPINST_ARGS+=("--git_args" "--jobs $GIT_FETCH_JOBS")
@@ -103,7 +104,7 @@ print_on_gha "::endgroup::"
103104

104105
print_on_gha "::group::Setup B2"
105106
# Deduce B2_TOOLSET if unset from B2_COMPILER
106-
if [ -z "$B2_TOOLSET" ] && [ -n "$B2_COMPILER" ]; then
107+
if [ -z "${B2_TOOLSET:-}" ] && [ -n "${B2_COMPILER:-}" ]; then
107108
if [[ "$B2_COMPILER" =~ clang ]]; then
108109
export B2_TOOLSET=clang
109110
elif [[ "$B2_COMPILER" =~ gcc|g\+\+ ]]; then
@@ -114,7 +115,7 @@ if [ -z "$B2_TOOLSET" ] && [ -n "$B2_COMPILER" ]; then
114115
fi
115116
fi
116117

117-
if [[ "$B2_TOOLSET" == clang* ]]; then
118+
if [[ "${B2_TOOLSET:-}" == clang* ]]; then
118119
# If clang was installed from LLVM APT it will not have a /usr/bin/clang++
119120
# so we need to add the correctly versioned llvm bin path to the PATH
120121
if [ -f "/etc/debian_version" ]; then
@@ -134,7 +135,7 @@ if [[ "$B2_TOOLSET" == clang* ]]; then
134135
ls -ls "/usr/lib/llvm-${ver}/bin" || true
135136
hash -r || true
136137
fi
137-
elif [ -n "${XCODE_APP}" ]; then
138+
elif [ -n "${XCODE_APP:-}" ]; then
138139
sudo xcode-select -switch "${XCODE_APP}"
139140
fi
140141
command -v clang || true
@@ -152,7 +153,7 @@ fi
152153

153154
# Set up user-config to actually use B2_COMPILER if set
154155
userConfigPath=$HOME/user-config.jam
155-
if [ -n "$B2_COMPILER" ]; then
156+
if [ -n "${B2_COMPILER:-}" ]; then
156157
echo '$B2_COMPILER set. Configuring user-config'
157158

158159
# Get C++ compiler
@@ -212,13 +213,13 @@ function show_bootstrap_log
212213
}
213214
print_on_gha "::endgroup::"
214215

215-
if [[ "$B2_DONT_BOOTSTRAP" != "1" ]]; then
216+
if [[ "${B2_DONT_BOOTSTRAP:0}" != "1" ]]; then
216217
print_on_gha "::group::Bootstrap B2"
217218
trap show_bootstrap_log ERR
218219
# Check if b2 already exists. This would (only) happen in a caching scenario. The purpose of caching is to save time by not recompiling everything.
219220
# The user may clear cache or delete b2 beforehand if they wish to rebuild.
220221
if [ ! -f b2 ] || ! b2_version_output=$(./b2 --version); then
221-
${B2_WRAPPER} ./bootstrap.sh
222+
${B2_WRAPPER:-} ./bootstrap.sh
222223
else
223224
# b2 expects versions to match
224225
engineversion=$(echo "$b2_version_output" | tr -s ' ' | cut -d' ' -f2 | cut -d'-' -f1)
@@ -229,10 +230,10 @@ if [[ "$B2_DONT_BOOTSTRAP" != "1" ]]; then
229230
if [[ "${enginemajorversion}" == "${coremajorversion}" ]] && [[ "${engineminorversion}" == "${coreminorversion}" ]]; then
230231
echo "b2 already exists and has the same version number"
231232
else
232-
${B2_WRAPPER} ./bootstrap.sh
233+
${B2_WRAPPER:-} ./bootstrap.sh
233234
fi
234235
fi
235236
trap - ERR
236-
${B2_WRAPPER} ./b2 -d0 headers
237+
${B2_WRAPPER:-} ./b2 -d0 headers
237238
print_on_gha "::endgroup::"
238239
fi

ci/enforce.sh

+22-22
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22
#
33
# Copyright 2017 - 2019 James E. King III
4-
# Copyright 2020 - 2024 Alexander Grund
4+
# Copyright 2020 - 2025 Alexander Grund
55
# Distributed under the Boost Software License, Version 1.0.
66
# (See accompanying file LICENSE_1_0.txt or copy at
77
# http://www.boost.org/LICENSE_1_0.txt)
@@ -27,17 +27,17 @@ function enforce_b2 {
2727
local old_varname=$1
2828
local new_varname=B2_${old_varname}
2929

30-
if [ -z "${!new_varname}" ]; then
31-
if [ -n "${!old_varname}" ]; then
30+
if [ -z "${!new_varname:-}" ]; then
31+
if [ -n "${!old_varname:-}" ]; then
3232
if [ "$TRAVIS" = "true" ]; then
3333
local ci_script=".travis.yml"
34-
elif [ -n "${GITHUB_WORKFLOW}" ]; then
34+
elif [ -n "${GITHUB_WORKFLOW:-}" ]; then
3535
local ci_script="${GITHUB_WORKFLOW} workflow"
36-
elif [ -n "$AGENT_OS" ]; then
36+
elif [ -n "${AGENT_OS:-}" ]; then
3737
local ci_script=".azure-pipelines.yml or azure-pipelines.yml"
3838
fi
3939
echo
40-
echo "WARNING: Your ${ci_script} file needs to be updated:"
40+
echo "WARNING: Your ${ci_script:-CI} file needs to be updated:"
4141
echo " use ${new_varname} instead of ${old_varname}"
4242
echo
4343
export "${new_varname}"="${!old_varname}"
@@ -54,26 +54,26 @@ enforce_b2 "TESTFLAGS"
5454
enforce_b2 "TOOLSET"
5555

5656
# default language level: C++11
57-
if [ -z "$B2_CXXSTD" ]; then
57+
if [ -z "${B2_CXXSTD:-}" ]; then
5858
export B2_CXXSTD=11
5959
fi
6060

6161
# default parallel build jobs: number of CPUs available + 1
62-
if [ -z "${B2_JOBS}" ]; then
62+
if [ -z "${B2_JOBS:-}" ]; then
6363
pythonexecutable=$(get_python_executable)
6464
cpus=$(grep -c 'processor' /proc/cpuinfo || $pythonexecutable -c 'import multiprocessing as mp; print(mp.cpu_count())' || echo "2")
6565
export B2_JOBS=$((cpus + 1))
6666
fi
6767

6868
# Build cmdline arguments for B2 in the array B2_ARGS to preserve quotes
6969

70-
if [ -n "$B2_CI_VERSION" ]; then # Set B2_CI_VERSION to opt in to new features
70+
if [ -n "${B2_CI_VERSION:-}" ]; then # Set B2_CI_VERSION to opt in to new features
7171
function append_b2_args {
7272
# Generate multiple "option=value" entries from the value of an environment variable
7373
# Handles correct splitting and quoting
7474
local var_name="$1"
7575
local option_name="$2"
76-
if [ -n "${!var_name}" ]; then
76+
if [ -n "${!var_name:-}" ]; then
7777
while IFS= read -r -d '' value; do
7878
# If the value has an assignment and a space we need to quote it
7979
if [[ $value == *"="*" "* ]]; then
@@ -96,18 +96,18 @@ if [ -n "$B2_CI_VERSION" ]; then # Set B2_CI_VERSION to opt in to new features
9696
B2_ARGS=(
9797
"${B2_ARGS[@]}"
9898
${B2_LINKFLAGS:+"linkflags=$B2_LINKFLAGS"}
99-
${B2_TESTFLAGS}
99+
${B2_TESTFLAGS:-}
100100
${B2_ADDRESS_MODEL:+address-model=$B2_ADDRESS_MODEL}
101101
${B2_LINK:+link=$B2_LINK}
102102
${B2_VISIBILITY:+visibility=$B2_VISIBILITY}
103103
${B2_STDLIB:+"stdlib=$B2_STDLIB"}
104-
${B2_THREADING}
104+
${B2_THREADING:-}
105105
${B2_VARIANT:+variant=$B2_VARIANT}
106106
${B2_ASAN:+address-sanitizer=norecover}
107107
${B2_TSAN:+thread-sanitizer=norecover}
108108
${B2_UBSAN:+undefined-sanitizer=norecover}
109109
-j"${B2_JOBS}"
110-
${B2_FLAGS}
110+
${B2_FLAGS:-}
111111
)
112112
else
113113
# Legacy codepath for compatibility for for old versions of the .github/*.yml files:
@@ -116,15 +116,15 @@ else
116116
B2_ARGS=(
117117
toolset="$B2_TOOLSET"
118118
cxxstd="$B2_CXXSTD"
119-
$B2_CXXFLAGS
120-
$B2_DEFINES
121-
$B2_INCLUDE
122-
$B2_LINKFLAGS
123-
$B2_TESTFLAGS
124-
$B2_ADDRESS_MODEL
125-
$B2_LINK
126-
$B2_THREADING
127-
$B2_VARIANT
119+
${B2_CXXFLAGS:-}
120+
${B2_DEFINES:-}
121+
${B2_INCLUDE:-}
122+
${B2_LINKFLAGS:-}
123+
${B2_TESTFLAGS:-}
124+
${B2_ADDRESS_MODEL:-}
125+
${B2_LINK:-}
126+
${B2_THREADING:-}
127+
${B2_VARIANT:-}
128128
-j"${B2_JOBS}"
129129
)
130130
fi

ci/travis/codecov.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# Bash script to run in travis to perform codecov.io integration
1010
#
1111

12-
set -ex
12+
set -eux
1313

1414
CI_DIR="$(dirname "${BASH_SOURCE[0]}")/.."
1515

0 commit comments

Comments
 (0)