Skip to content

Commit 460d70d

Browse files
authored
Merge pull request #21 from rsps/add-gcc-info
Add gcc_version()
2 parents 0453d0c + 02b00e8 commit 460d70d

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
* `var_dump_all()` macro in `debug.cmake`.
13+
* `gcc_version()` function in `compilers/gcc.cmake`.
1314

1415
### Changed
1516

cmake/rsp/compilers/gcc.cmake

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,45 @@ if (NOT DEFINED RSP_GCC_STRICT_COMPILE_OPTIONS)
191191
#
192192
#-dD
193193
)
194+
endif ()
195+
196+
if (NOT COMMAND "gcc_version")
197+
198+
#! gcc_version : Returns the GCC tool version
199+
#
200+
# @param OUTPUT <variable> The variable to assign result to.
201+
#
202+
# @return
203+
# [OUTPUT] GCC version.
204+
#
205+
function(gcc_version)
206+
set(options "")
207+
set(oneValueArgs OUTPUT)
208+
set(multiValueArgs "")
209+
210+
cmake_parse_arguments(INPUT "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
211+
requires_arguments("OUTPUT" INPUT)
212+
213+
# ---------------------------------------------------------------------------------------------- #
214+
215+
find_program(GCC_TOOL NAMES g++-latest g++-HEAD g++-14 g++-13 g++-12 g++-11)
216+
217+
execute_process(
218+
COMMAND ${GCC_TOOL} --version
219+
OUTPUT_VARIABLE GCC_TOOL_VERSION
220+
ERROR_VARIABLE GCC_TOOL_VERSION
221+
)
222+
223+
string(REGEX MATCH "[0-9]+(\\.[0-9]+)+" GCC_TOOL_VERSION "${GCC_TOOL_VERSION}")
224+
225+
# ---------------------------------------------------------------------------------------------- #
226+
227+
# Set the resulting version
228+
set("${INPUT_OUTPUT}" "${GCC_TOOL_VERSION}")
229+
230+
return(
231+
PROPAGATE
232+
"${INPUT_OUTPUT}"
233+
)
234+
endfunction()
194235
endif ()

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,18 @@ set(my_compile_options "${RSP_GCC_STRICT_COMPILE_OPTIONS}")
3636
3737
# Modify your preset...
3838
list(REMOVE_ITEM my_compile_options "-Wswitch-default")
39-
```
39+
```
40+
41+
## `gcc_version()`
42+
43+
**Available Since: `v0.2.0`**
44+
45+
Obtains the installed GCC version. The function accepts the following parameters:
46+
47+
* `OUTPUT`: _Output variable to assign the result to._
48+
49+
```cmake
50+
gcc_version(OUTPUT version)
51+
52+
message("GCC: ${version}") # 14.2.0
53+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
include("rsp/testing")
2+
include("rsp/compilers/gcc")
3+
4+
define_test_case(
5+
"GCC Version Test"
6+
LABELS "gcc;version;compilers"
7+
)
8+
9+
# -------------------------------------------------------------------------------------------------------------- #
10+
# Actual tests
11+
# -------------------------------------------------------------------------------------------------------------- #
12+
13+
define_test("can obtain GCC version" "can_obtain_gcc_version")
14+
function(can_obtain_gcc_version)
15+
16+
gcc_version(OUTPUT version)
17+
18+
assert_string_not_empty("${version}" MESSAGE "GCC version not obtained")
19+
endfunction()

0 commit comments

Comments
 (0)