Skip to content

Commit 965917d

Browse files
authored
Merge pull request #22 from rsps/add-gcc-info
Add gcc info
2 parents 5fe2a9f + 4887096 commit 965917d

File tree

4 files changed

+102
-9
lines changed

4 files changed

+102
-9
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
* `gcc_info()` function in `compilers/gcc.cmake`.
13+
14+
### Changed
15+
16+
* `gcc_version()` now uses `gcc_info()` to obtain GCC version.
17+
18+
### Fixed
19+
20+
* Internal property bleeds into global scope, when calling `gcc_version()`, due auto caching by `find_program()`.
21+
1022
## [v0.2.0] - 2025-03-18
1123

1224
### Added

cmake/rsp/compilers/gcc.cmake

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,17 @@ if (NOT DEFINED RSP_GCC_STRICT_COMPILE_OPTIONS)
193193
)
194194
endif ()
195195

196-
if (NOT COMMAND "gcc_version")
196+
if (NOT COMMAND "gcc_info")
197197

198-
#! gcc_version : Returns the GCC tool version
198+
#! gcc_version : Assigns path to the available / installed GCC and its version
199199
#
200200
# @param OUTPUT <variable> The variable to assign result to.
201201
#
202202
# @return
203-
# [OUTPUT] GCC version.
203+
# [OUTPUT] Path to GCC.
204+
# [OUTPUT]_VERSION GCC version.
204205
#
205-
function(gcc_version)
206+
function(gcc_info)
206207
set(options "")
207208
set(oneValueArgs OUTPUT)
208209
set(multiValueArgs "")
@@ -212,20 +213,55 @@ if (NOT COMMAND "gcc_version")
212213

213214
# ---------------------------------------------------------------------------------------------- #
214215

215-
find_program(GCC_TOOL NAMES g++-latest g++-HEAD g++-14 g++-13 g++-12 g++-11)
216+
find_program(GCC_TOOL NAMES g++-latest g++-HEAD g++-14 g++-13 g++-12 g++-11 NO_CACHE)
216217

217218
execute_process(
218-
COMMAND ${GCC_TOOL} --version
219-
OUTPUT_VARIABLE GCC_TOOL_VERSION
220-
ERROR_VARIABLE GCC_TOOL_VERSION
219+
COMMAND ${GCC_TOOL} --version
220+
OUTPUT_VARIABLE GCC_TOOL_VERSION
221+
ERROR_VARIABLE GCC_TOOL_VERSION
221222
)
222223

223224
string(REGEX MATCH "[0-9]+(\\.[0-9]+)+" GCC_TOOL_VERSION "${GCC_TOOL_VERSION}")
224225

225226
# ---------------------------------------------------------------------------------------------- #
226227

228+
# Set the resulting path and version
229+
set("${INPUT_OUTPUT}" "${GCC_TOOL}")
230+
set("${INPUT_OUTPUT}_VERSION" "${GCC_TOOL_VERSION}")
231+
232+
return(
233+
PROPAGATE
234+
"${INPUT_OUTPUT}"
235+
"${INPUT_OUTPUT}_VERSION"
236+
)
237+
endfunction()
238+
endif ()
239+
240+
if (NOT COMMAND "gcc_version")
241+
242+
#! gcc_version : Returns the GCC tool version
243+
#
244+
# @param OUTPUT <variable> The variable to assign result to.
245+
#
246+
# @return
247+
# [OUTPUT] GCC version.
248+
#
249+
function(gcc_version)
250+
set(options "")
251+
set(oneValueArgs OUTPUT)
252+
set(multiValueArgs "")
253+
254+
cmake_parse_arguments(INPUT "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
255+
requires_arguments("OUTPUT" INPUT)
256+
257+
# ---------------------------------------------------------------------------------------------- #
258+
259+
gcc_info(OUTPUT result)
260+
261+
# ---------------------------------------------------------------------------------------------- #
262+
227263
# Set the resulting version
228-
set("${INPUT_OUTPUT}" "${GCC_TOOL_VERSION}")
264+
set("${INPUT_OUTPUT}" "${result_VERSION}")
229265

230266
return(
231267
PROPAGATE

docs/+current/modules/compiler/gcc.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,28 @@ set(my_compile_options "${RSP_GCC_STRICT_COMPILE_OPTIONS}")
3838
list(REMOVE_ITEM my_compile_options "-Wswitch-default")
3939
```
4040

41+
## `gcc_info()`
42+
43+
**Available Since: `v0.3.0`**
44+
45+
Obtains the path and version of the installed GCC version. The function accepts the following parameters:
46+
47+
* `OUTPUT`: _Output variable to assign the result to._
48+
49+
**Output**
50+
51+
* `[OUTPUT]`: _Path to installed GCC tool._
52+
* `[OUTPUT]_VERSION`: _GCC version._
53+
54+
**Example**
55+
56+
```cmake
57+
gcc_info(OUTPUT gcc)
58+
59+
message("GCC (path): ${gcc}") # /usr/bin/g++-14
60+
message("GCC (version): ${gcc_VERSION}") # 14.2.0
61+
```
62+
4163
## `gcc_version()`
4264

4365
**Available Since: `v0.2.0`**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
include("rsp/testing")
2+
include("rsp/compilers/gcc")
3+
include("rsp/debug")
4+
5+
define_test_case(
6+
"GCC Info Test"
7+
LABELS "gcc;version;compilers"
8+
)
9+
10+
# -------------------------------------------------------------------------------------------------------------- #
11+
# Actual tests
12+
# -------------------------------------------------------------------------------------------------------------- #
13+
14+
define_test("can obtain GCC path and version" "can_obtain_gcc_info")
15+
function(can_obtain_gcc_info)
16+
17+
gcc_info(OUTPUT result)
18+
19+
assert_string_not_empty("${result}" MESSAGE "GCC path not obtained")
20+
21+
assert_defined("result_VERSION" MESSAGE "GCC Version not defined")
22+
assert_string_not_empty("${result_VERSION}" MESSAGE "GCC path not obtained")
23+
endfunction()

0 commit comments

Comments
 (0)