From 284720e6545427ff3321828c100a83b56b83cebb Mon Sep 17 00:00:00 2001 From: alin Date: Tue, 18 Mar 2025 14:18:27 +0100 Subject: [PATCH 1/4] Add gcc_version() util --- cmake/rsp/compilers/gcc.cmake | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/cmake/rsp/compilers/gcc.cmake b/cmake/rsp/compilers/gcc.cmake index 322dca1..7fe1f61 100644 --- a/cmake/rsp/compilers/gcc.cmake +++ b/cmake/rsp/compilers/gcc.cmake @@ -191,4 +191,45 @@ if (NOT DEFINED RSP_GCC_STRICT_COMPILE_OPTIONS) # #-dD ) +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) + + # ---------------------------------------------------------------------------------------------- # + + find_program(GCC_TOOL NAMES g++-latest g++-HEAD g++-14 g++-13 g++-12 g++-11) + + execute_process( + 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 version + set("${INPUT_OUTPUT}" "${GCC_TOOL_VERSION}") + + return( + PROPAGATE + "${INPUT_OUTPUT}" + ) + endfunction() endif () \ No newline at end of file From f052823145b7f55bb7f03c1bf32dee0a22cff446 Mon Sep 17 00:00:00 2001 From: alin Date: Tue, 18 Mar 2025 14:23:08 +0100 Subject: [PATCH 2/4] Add test for gcc_version() --- tests/unit/compilerse/gcc_version_test.cmake | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 tests/unit/compilerse/gcc_version_test.cmake diff --git a/tests/unit/compilerse/gcc_version_test.cmake b/tests/unit/compilerse/gcc_version_test.cmake new file mode 100644 index 0000000..e097952 --- /dev/null +++ b/tests/unit/compilerse/gcc_version_test.cmake @@ -0,0 +1,19 @@ +include("rsp/testing") +include("rsp/compilers/gcc") + +define_test_case( + "GCC Version Test" + LABELS "gcc;version;compilers" +) + +# -------------------------------------------------------------------------------------------------------------- # +# Actual tests +# -------------------------------------------------------------------------------------------------------------- # + +define_test("can obtain GCC version" "can_obtain_gcc_version") +function(can_obtain_gcc_version) + + gcc_version(OUTPUT version) + + assert_string_not_empty("${version}" MESSAGE "GCC version not obtained") +endfunction() From 8b06262e21887b19348565e8278b26e7ad56e4a3 Mon Sep 17 00:00:00 2001 From: alin Date: Tue, 18 Mar 2025 14:23:18 +0100 Subject: [PATCH 3/4] Change release notes --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e7c5bf5..2e2fd67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added * `var_dump_all()` macro in `debug.cmake`. +* `gcc_version()` function in `compilers/gcc.cmake`. ### Changed From 02b00e80c20ecb79299704ec6c1cbfe5fb65e116 Mon Sep 17 00:00:00 2001 From: alin Date: Tue, 18 Mar 2025 14:27:19 +0100 Subject: [PATCH 4/4] Add doc. for gcc_version() --- docs/+current/modules/compiler/gcc.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/docs/+current/modules/compiler/gcc.md b/docs/+current/modules/compiler/gcc.md index 6dabb8f..36badc9 100644 --- a/docs/+current/modules/compiler/gcc.md +++ b/docs/+current/modules/compiler/gcc.md @@ -36,4 +36,18 @@ set(my_compile_options "${RSP_GCC_STRICT_COMPILE_OPTIONS}") # Modify your preset... list(REMOVE_ITEM my_compile_options "-Wswitch-default") -``` \ No newline at end of file +``` + +## `gcc_version()` + +**Available Since: `v0.2.0`** + +Obtains the installed GCC version. The function accepts the following parameters: + +* `OUTPUT`: _Output variable to assign the result to._ + +```cmake +gcc_version(OUTPUT version) + +message("GCC: ${version}") # 14.2.0 +```