Skip to content

Commit 43febe9

Browse files
authored
Merge pull request #14 from rsps/add-build-info
Add build info
2 parents 1b75a30 + b25da0a commit 43febe9

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed

cmake/rsp/debug.cmake

+88
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ include_guard(GLOBAL)
77
# Debug
88
message(VERBOSE "rsp/debug module included")
99

10+
include("rsp/output/utils")
1011
include("rsp/helpers")
1112

1213
if (NOT COMMAND "dump")
@@ -194,3 +195,90 @@ if (NOT COMMAND "var_dump")
194195

195196
endfunction()
196197
endif ()
198+
199+
if(NOT COMMAND "build_info")
200+
201+
#! build_info : Output build information to stdout or stderr (Cmake's message type specific)
202+
#
203+
# @see https://cmake.org/cmake/help/latest/command/message.html
204+
#
205+
# @param [<mode>] Option - Cmake's message type. Defaults to NOTICE, when not specified.
206+
# @param [OUTPUT <variable>] Optional - If specified, message is assigned to output variable instead of
207+
# being printed to stdout or stderr.
208+
#
209+
# @return
210+
# [OUTPUT] The resulting output variable, if OUTPUT was specified.
211+
#
212+
function(build_info)
213+
set(options "${RSP_CMAKE_MESSAGE_MODES}")
214+
set(oneValueArgs OUTPUT)
215+
set(multiValueArgs "")
216+
217+
cmake_parse_arguments(INPUT "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
218+
219+
# ---------------------------------------------------------------------------------------------- #
220+
221+
# Message mode
222+
resolve_msg_mode("NOTICE")
223+
224+
# ---------------------------------------------------------------------------------------------- #
225+
226+
set(info_list
227+
# Build Type
228+
# @see https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html
229+
"Type|${CMAKE_BUILD_TYPE}"
230+
231+
# Library Type (NOTE: LIB_TYPE is NOT a predefined cmake variable)
232+
"Library Type|${LIB_TYPE}"
233+
234+
# Compiler flags
235+
"Compiler flags|${CMAKE_CXX_COMPILE_FLAGS}"
236+
237+
# Compiler cxx debug flags
238+
"Compiler cxx debug flags|${CMAKE_CXX_FLAGS_DEBUG}"
239+
240+
# Compiler cxx release flags
241+
"Compiler cxx release flags|${CMAKE_CXX_FLAGS_RELEASE}"
242+
243+
# Compiler cxx min size flags
244+
"Compiler cxx min size flags|${CMAKE_CXX_FLAGS_MINSIZEREL}"
245+
246+
# Compiler cxx flags
247+
"Compiler cxx flags|${CMAKE_CXX_FLAGS}"
248+
)
249+
250+
# ---------------------------------------------------------------------------------------------- #
251+
252+
set(buffer "")
253+
254+
foreach (item IN LISTS info_list)
255+
string(REPLACE "|" ";" parts "${item}")
256+
list(GET parts 0 label)
257+
list(GET parts 1 value)
258+
259+
list(APPEND buffer "${COLOR_MAGENTA}${label}:${RESTORE} ${value}")
260+
endforeach ()
261+
262+
# ---------------------------------------------------------------------------------------------- #
263+
264+
# Convert list to a string with newlines...
265+
string(REPLACE ";" "\n" buffer "${buffer}")
266+
267+
# Attempt to keep the formatting - see details in rsp/output::output()
268+
string(ASCII 13 CR)
269+
set(formatted_output "${CR}${COLOR_BRIGHT_MAGENTA}build info:${RESTORE}\n${buffer}")
270+
string(REPLACE "\n" "\n " formatted_output "${formatted_output}")
271+
272+
# ---------------------------------------------------------------------------------------------- #
273+
274+
# Assign to output variable, if requested and stop any further processing.
275+
if (DEFINED INPUT_OUTPUT)
276+
set("${INPUT_OUTPUT}" "${formatted_output}")
277+
return(PROPAGATE "${INPUT_OUTPUT}")
278+
endif ()
279+
280+
# ---------------------------------------------------------------------------------------------- #
281+
282+
message(${msg_mode} "${formatted_output}")
283+
endfunction()
284+
endif ()
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
title: Build Info
3+
description: Using git_find_version_tag
4+
keywords: debug, debugging, build info, cmake
5+
author: RSP Systems A/S
6+
---
7+
8+
# Build Info
9+
10+
The `build_info()` function dumps build information about the current build.
11+
It accepts the following parameter:
12+
13+
* < mode >: (_option_), _cmake [message mode](https://cmake.org/cmake/help/latest/command/message.html#general-messages), e.g. `WARNING`, `NOTICE`, `STATUS`, ...etc._
14+
_Defaults to `NOTICE` is mode is not specified._
15+
* `OUTPUT`: (_optional_), _output variable. If specified, message is assigned to variable, instead of being printed to `stdout` or `stderr`._
16+
17+
## Example
18+
19+
```cmake
20+
set(LIB_TYPE "SHARED")
21+
22+
build_info()
23+
```
24+
25+
Outputs a message similar to this (_not all properties are shown in example_):
26+
27+
```txt
28+
build info:
29+
Type: Debug
30+
Library Type: SHARED
31+
Compiler flags: ...
32+
Compiler cxx debug flags: ...
33+
Compiler cxx release flags: ...
34+
Compiler cxx min size flags: ...
35+
Compiler cxx flags: ...
36+
```
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
include("rsp/testing")
2+
include("rsp/debug")
3+
4+
define_test_case(
5+
"Build Info Test"
6+
LABELS "debug;build_info"
7+
)
8+
9+
# -------------------------------------------------------------------------------------------------------------- #
10+
# Actual Tests
11+
# -------------------------------------------------------------------------------------------------------------- #
12+
13+
define_test("can output build info" "outputs_build_info")
14+
function(outputs_build_info)
15+
16+
# Set a few of the build info variables
17+
set(CMAKE_BUILD_TYPE "Debug")
18+
set(LIB_TYPE "STATIC")
19+
20+
# ---------------------------------------------------------------------- #
21+
22+
build_info(OUTPUT result)
23+
24+
# Debug
25+
# message("${result}")
26+
27+
# ---------------------------------------------------------------------- #
28+
29+
# Ensure that a few of the properties are in the output
30+
assert_string_contains("${result}" "Type: ${CMAKE_BUILD_TYPE}" MESSAGE "Incorrect build info output (build type)")
31+
assert_string_contains("${result}" "Library Type: ${LIB_TYPE}" MESSAGE "Incorrect build info output (library type)")
32+
endfunction()

0 commit comments

Comments
 (0)