Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 45 additions & 9 deletions cmake/rsp/compilers/gcc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 <variable> 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 "")
Expand All @@ -212,20 +213,55 @@ 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
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 <variable> 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
Expand Down
22 changes: 22 additions & 0 deletions docs/+current/modules/compiler/gcc.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`**
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/compilerse/gcc_info_test.cmake
Original file line number Diff line number Diff line change
@@ -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()