From 42c162c87e3c474dc78ecf73ef2245c196e99bec Mon Sep 17 00:00:00 2001 From: Alexander Grund Date: Thu, 11 Aug 2022 18:04:36 +0200 Subject: [PATCH] Handle multiple entries in `B2_DEFINES` As pointed out by @sdarwin multiple values in `B2_DEFINES` led to a failing build as the scripts only supported a single value for the "new" var usages. This is a regression from the old usage where `B2_DEFINES: define=FOO=1 define=BAR=2` could be used. This is now fixed by a small parser function which generates multiple `define=*` entries and additionally now also supports spaces in the define values. Same is done for `B2_INCLUDE` which has the same issue. Also includes the change to the CI script from #177 and an additional test in test.cpp to verify that this works. --- .github/workflows/old_ci.yml | 15 +++++++++++++++ ci/enforce.sh | 25 +++++++++++++++++++++++-- test/test.cpp | 19 +++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/.github/workflows/old_ci.yml b/.github/workflows/old_ci.yml index 4b65565..e16beec 100644 --- a/.github/workflows/old_ci.yml +++ b/.github/workflows/old_ci.yml @@ -55,6 +55,21 @@ jobs: B2_LINKFLAGS: linkflags=-fsanitize=undefined linkflags=-fno-sanitize-recover=all linkflags=-fuse-ld=gold os: ubuntu-20.04 install: g++-8 + + - name: New var usage, multiple values per key + env: + B2_CI_VERSION: 1 + B2_TOOLSET: gcc-8 + B2_ADDRESS_MODEL: 64 + B2_LINK: shared,static + B2_THREADING: threading=multi,single + B2_VARIANT: release + B2_DEFINES: BOOST_NO_STRESS_TEST=1 BOOST_IMPORTANT=1 BOOST_ALSO_IMPORTANT="with space" + B2_LINKFLAGS: -fsanitize=undefined -fno-sanitize-recover=all -fuse-ld=gold + B2_FLAGS: define=BOOST_CI_TEST_DEFINES=1 + os: ubuntu-20.04 + install: g++-8 + - name: Travis-like coverage collection coverage: yes env: diff --git a/ci/enforce.sh b/ci/enforce.sh index 40ff788..e768a8c 100755 --- a/ci/enforce.sh +++ b/ci/enforce.sh @@ -85,12 +85,33 @@ if [ -z "$B2_CI_VERSION" ]; then -j${B2_JOBS} ) else + # Generate multiple "option=value" entries from the value of an environment variable + # Handles correct splitting and quoting + function append_b2_args + { + local var_name="$1" + local option_name="$2" + if [ -n "${!var_name}" ]; then + while IFS= read -r -d '' value; do + # If the value has an assignment and a space we need to quote it + if [[ $value == *"="*" "* ]]; then + B2_ARGS+=("${option_name}=${value%%=*}=\"${value#*=}\"") + else + B2_ARGS+=("${option_name}=${value}") + fi + done < <(echo "${!var_name}" | xargs -n 1 printf '%s\0') + fi + } + B2_ARGS=( ${B2_TOOLSET:+"toolset=$B2_TOOLSET"} "cxxstd=$B2_CXXSTD" ${B2_CXXFLAGS:+"cxxflags=$B2_CXXFLAGS"} - ${B2_DEFINES:+"define=$B2_DEFINES"} - ${B2_INCLUDE:+"include=$B2_INCLUDE"} + ) + append_b2_args B2_DEFINES define + append_b2_args B2_INCLUDE include + B2_ARGS=( + "${B2_ARGS[@]}" ${B2_LINKFLAGS:+"linkflags=$B2_LINKFLAGS"} ${B2_TESTFLAGS} ${B2_ADDRESS_MODEL:+address-model=$B2_ADDRESS_MODEL} diff --git a/test/test.cpp b/test/test.cpp index 889a313..9320adb 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -17,8 +17,27 @@ // Check that including a file from the same directory works #include "test2.hpp" +// Test of the CI scripts passing the correct defines +#ifdef BOOST_CI_TEST_DEFINES + #ifndef BOOST_NO_STRESS_TEST + #error "Missing define BOOST_NO_STRESS_TEST" + #endif + #ifndef BOOST_IMPORTANT + #error "Missing define BOOST_IMPORTANT" + #endif + #ifndef BOOST_ALSO_IMPORTANT + #error "Missing define BOOST_ALSO_IMPORTANT" + #endif + #define BOOST_CI_STRINGIZE_2(x) #x + #define BOOST_CI_STRINGIZE(x) BOOST_CI_STRINGIZE_2(x) +#endif + int main() { +#ifdef BOOST_CI_TEST_DEFINES + const std::string macro_value = BOOST_CI_STRINGIZE(BOOST_ALSO_IMPORTANT); + BOOST_TEST_EQ(macro_value, "with space"); +#endif const bool isMSVC = MSVC_VALUE; std::map > map; map["result"].push_back(boost::boost_ci::get_answer());