From 3415afef57ad3f33e660ccc96332285364bbd419 Mon Sep 17 00:00:00 2001 From: alin Date: Tue, 18 Mar 2025 15:26:44 +0100 Subject: [PATCH 1/5] Fix GCC_TOOL bleeds into global scope While our "Core Lib" actually expects a GCC_TOOL (path) to be available, invoking the gcc_version() automatically cached the property into CMake's global scope. This fixes the issue. --- cmake/rsp/compilers/gcc.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/rsp/compilers/gcc.cmake b/cmake/rsp/compilers/gcc.cmake index 7fe1f61..f235253 100644 --- a/cmake/rsp/compilers/gcc.cmake +++ b/cmake/rsp/compilers/gcc.cmake @@ -212,7 +212,7 @@ if (NOT COMMAND "gcc_version") # ---------------------------------------------------------------------------------------------- # - find_program(GCC_TOOL NAMES g++-latest g++-HEAD g++-14 g++-13 g++-12 g++-11) + find_program(GCC_TOOL NAMES g++-latest g++-HEAD g++-14 g++-13 g++-12 g++-11 NO_CACHE) execute_process( COMMAND ${GCC_TOOL} --version From f9e5a71d2823f87fa1b9679c7fa10e75ebcd2ea5 Mon Sep 17 00:00:00 2001 From: alin Date: Tue, 18 Mar 2025 15:36:20 +0100 Subject: [PATCH 2/5] Add gcc_info() Function obtains path to available GCC cli, and also assigns the obtained version. The gcc_version() util has been changed to use this new util to obtain GCC's version, which no longer bleeds variables into CMake's global scope. --- cmake/rsp/compilers/gcc.cmake | 52 +++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/cmake/rsp/compilers/gcc.cmake b/cmake/rsp/compilers/gcc.cmake index f235253..30a3e7d 100644 --- a/cmake/rsp/compilers/gcc.cmake +++ b/cmake/rsp/compilers/gcc.cmake @@ -193,16 +193,17 @@ if (NOT DEFINED RSP_GCC_STRICT_COMPILE_OPTIONS) ) endif () -if (NOT COMMAND "gcc_version") +if (NOT COMMAND "gcc_info") - #! gcc_version : Returns the GCC tool version + #! gcc_version : Assigns path to the available / installed GCC and its version # # @param OUTPUT The variable to assign result to. # # @return - # [OUTPUT] GCC version. + # [OUTPUT] Path to GCC. + # [OUTPUT]_VERSION GCC version. # - function(gcc_version) + function(gcc_info) set(options "") set(oneValueArgs OUTPUT) set(multiValueArgs "") @@ -215,17 +216,52 @@ if (NOT COMMAND "gcc_version") find_program(GCC_TOOL NAMES g++-latest g++-HEAD g++-14 g++-13 g++-12 g++-11 NO_CACHE) execute_process( - COMMAND ${GCC_TOOL} --version - OUTPUT_VARIABLE GCC_TOOL_VERSION - ERROR_VARIABLE GCC_TOOL_VERSION + COMMAND ${GCC_TOOL} --version + OUTPUT_VARIABLE GCC_TOOL_VERSION + ERROR_VARIABLE GCC_TOOL_VERSION ) string(REGEX MATCH "[0-9]+(\\.[0-9]+)+" GCC_TOOL_VERSION "${GCC_TOOL_VERSION}") # ---------------------------------------------------------------------------------------------- # + # Set the resulting path and version + set("${INPUT_OUTPUT}" "${GCC_TOOL}") + set("${INPUT_OUTPUT}_VERSION" "${GCC_TOOL_VERSION}") + + return( + PROPAGATE + "${INPUT_OUTPUT}" + "${INPUT_OUTPUT}_VERSION" + ) + endfunction() +endif () + +if (NOT COMMAND "gcc_version") + + #! gcc_version : Returns the GCC tool version + # + # @param OUTPUT The variable to assign result to. + # + # @return + # [OUTPUT] GCC version. + # + function(gcc_version) + set(options "") + set(oneValueArgs OUTPUT) + set(multiValueArgs "") + + cmake_parse_arguments(INPUT "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + requires_arguments("OUTPUT" INPUT) + + # ---------------------------------------------------------------------------------------------- # + + gcc_info(OUTPUT result) + + # ---------------------------------------------------------------------------------------------- # + # Set the resulting version - set("${INPUT_OUTPUT}" "${GCC_TOOL_VERSION}") + set("${INPUT_OUTPUT}" "${result_VERSION}") return( PROPAGATE From de3ff015a5a8765a0ad2f395af2de0f4b4e12254 Mon Sep 17 00:00:00 2001 From: alin Date: Tue, 18 Mar 2025 15:36:29 +0100 Subject: [PATCH 3/5] Add test for gcc_info() --- tests/unit/compilerse/gcc_info_test.cmake | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 tests/unit/compilerse/gcc_info_test.cmake diff --git a/tests/unit/compilerse/gcc_info_test.cmake b/tests/unit/compilerse/gcc_info_test.cmake new file mode 100644 index 0000000..8525687 --- /dev/null +++ b/tests/unit/compilerse/gcc_info_test.cmake @@ -0,0 +1,23 @@ +include("rsp/testing") +include("rsp/compilers/gcc") +include("rsp/debug") + +define_test_case( + "GCC Info Test" + LABELS "gcc;version;compilers" +) + +# -------------------------------------------------------------------------------------------------------------- # +# Actual tests +# -------------------------------------------------------------------------------------------------------------- # + +define_test("can obtain GCC path and version" "can_obtain_gcc_info") +function(can_obtain_gcc_info) + + gcc_info(OUTPUT result) + + assert_string_not_empty("${result}" MESSAGE "GCC path not obtained") + + assert_defined("result_VERSION" MESSAGE "GCC Version not defined") + assert_string_not_empty("${result_VERSION}" MESSAGE "GCC path not obtained") +endfunction() From 573505fc93965cea85f47cf0f0870f709cc3675e Mon Sep 17 00:00:00 2001 From: alin Date: Tue, 18 Mar 2025 15:36:48 +0100 Subject: [PATCH 4/5] Change release notes --- CHANGELOG.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 802f949..25fb4b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +* `gcc_info()` function in `compilers/gcc.cmake`. + +### Changed + +* `gcc_version()` now uses `gcc_info()` to obtain GCC version. + +### Fixed + +* Internal property bleeds into global scope, when calling `gcc_version()`, due auto caching by `find_program()`. + ## [v0.2.0] - 2025-03-18 ### Added From 4887096a077ab447fb746a04edafea9b4e4323c5 Mon Sep 17 00:00:00 2001 From: alin Date: Tue, 18 Mar 2025 15:41:09 +0100 Subject: [PATCH 5/5] Add doc. for gcc_info() --- docs/+current/modules/compiler/gcc.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/docs/+current/modules/compiler/gcc.md b/docs/+current/modules/compiler/gcc.md index 36badc9..4a8dfc0 100644 --- a/docs/+current/modules/compiler/gcc.md +++ b/docs/+current/modules/compiler/gcc.md @@ -38,6 +38,28 @@ set(my_compile_options "${RSP_GCC_STRICT_COMPILE_OPTIONS}") list(REMOVE_ITEM my_compile_options "-Wswitch-default") ``` +## `gcc_info()` + +**Available Since: `v0.3.0`** + +Obtains the path and version of the installed GCC version. The function accepts the following parameters: + +* `OUTPUT`: _Output variable to assign the result to._ + +**Output** + +* `[OUTPUT]`: _Path to installed GCC tool._ +* `[OUTPUT]_VERSION`: _GCC version._ + +**Example** + +```cmake +gcc_info(OUTPUT gcc) + +message("GCC (path): ${gcc}") # /usr/bin/g++-14 +message("GCC (version): ${gcc_VERSION}") # 14.2.0 +``` + ## `gcc_version()` **Available Since: `v0.2.0`**