From 8dae1c161c4d3bc3a1ef3cd48de0da9a89e44e65 Mon Sep 17 00:00:00 2001 From: John Mulligan Date: Tue, 1 Nov 2022 14:51:57 -0400 Subject: [PATCH] script: add discover_compiler function to lib-build.sh The discover_compiler function is an abstraction over the current compiler detection code in run-make.sh. It is intended to be flexible enough to work on {centos,rhel} systems, but currently is just an updated version of the logic from run-make.sh. The intent is that this function will grow and become useful for other scripts used for building (possibly do_cmake.sh for example). Signed-off-by: John Mulligan (cherry picked from commit 561bf4ea9b4dd8f6710a959c5bd38eb4085a9d52) --- src/script/lib-build.sh | 29 +++++++++++++++++++++++++++++ src/script/run-make.sh | 14 +++++--------- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/script/lib-build.sh b/src/script/lib-build.sh index 17c2fe72148c7..c6ce94b633ad7 100644 --- a/src/script/lib-build.sh +++ b/src/script/lib-build.sh @@ -48,3 +48,32 @@ function get_processors() { fi fi } + +# discover_compiler takes one argument, purpose, which may be used +# to adjust the results for a specific need. It sets three environment +# variables `discovered_c_compiler`, `discovered_cxx_compiler` and +# `discovered_compiler_env`. The `discovered_compiler_env` variable +# may be blank. If not, it will contain a file that needs to be sourced +# prior to using the compiler. +function discover_compiler() { + # nb: currently purpose is not used for detection + local purpose="$1" + ci_debug "Finding compiler for ${purpose}" + + local compiler_env="" + local cxx_compiler=g++ + local c_compiler=gcc + # ubuntu/debian ci builds prefer clang + for i in {14..10}; do + if type -t "clang-$i" > /dev/null; then + cxx_compiler="clang++-$i" + c_compiler="clang-$i" + break + fi + done + + export discovered_c_compiler="${c_compiler}" + export discovered_cxx_compiler="${cxx_compiler}" + export discovered_compiler_env="${compiler_env}" + return 0 +} diff --git a/src/script/run-make.sh b/src/script/run-make.sh index 4331e581aa584..8e04e6fc8a46f 100755 --- a/src/script/run-make.sh +++ b/src/script/run-make.sh @@ -89,15 +89,11 @@ EOM fi $DRY_RUN ccache -sz # Reset the ccache statistics and show the current configuration - local cxx_compiler=g++ - local c_compiler=gcc - for i in $(seq 14 -1 10); do - if type -t clang-$i > /dev/null; then - cxx_compiler="clang++-$i" - c_compiler="clang-$i" - break - fi - done + if ! discover_compiler ci-build ; then + ci_debug "Failed to discover a compiler" + fi + local cxx_compiler="${discovered_cxx_compiler}" + local c_compiler="${discovered_c_compiler}" local cmake_opts cmake_opts+=" -DCMAKE_CXX_COMPILER=$cxx_compiler -DCMAKE_C_COMPILER=$c_compiler" cmake_opts+=" -DCMAKE_CXX_FLAGS_DEBUG=-Werror" -- 2.39.5