Skip to content

Commit aa4c5f3

Browse files
committed
cmake: Rework flags summary
1 parent 4af241b commit aa4c5f3

File tree

2 files changed

+73
-26
lines changed

2 files changed

+73
-26
lines changed

CMakeLists.txt

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -334,34 +334,11 @@ endif()
334334
message("Cross compiling ....................... ${cross_status}")
335335
message("Valgrind .............................. ${SECP256K1_VALGRIND}")
336336
get_directory_property(definitions COMPILE_DEFINITIONS)
337-
string(REPLACE ";" " " definitions "${definitions}")
337+
list(JOIN definitions " " definitions)
338338
message("Preprocessor defined macros ........... ${definitions}")
339339
message("C compiler ............................ ${CMAKE_C_COMPILER_ID} ${CMAKE_C_COMPILER_VERSION}, ${CMAKE_C_COMPILER}")
340-
message("CFLAGS ................................ ${CMAKE_C_FLAGS}")
341-
get_directory_property(compile_options COMPILE_OPTIONS)
342-
string(REPLACE ";" " " compile_options "${compile_options}")
343-
message("Compile options ....................... " ${compile_options})
344-
if(NOT is_multi_config)
345-
message("Build type:")
346-
message(" - CMAKE_BUILD_TYPE ................... ${CMAKE_BUILD_TYPE}")
347-
string(TOUPPER "${CMAKE_BUILD_TYPE}" build_type)
348-
message(" - CFLAGS ............................. ${CMAKE_C_FLAGS_${build_type}}")
349-
message(" - LDFLAGS for executables ............ ${CMAKE_EXE_LINKER_FLAGS_${build_type}}")
350-
message(" - LDFLAGS for shared libraries ....... ${CMAKE_SHARED_LINKER_FLAGS_${build_type}}")
351-
else()
352-
message("Supported configurations .............. ${CMAKE_CONFIGURATION_TYPES}")
353-
message("RelWithDebInfo configuration:")
354-
message(" - CFLAGS ............................. ${CMAKE_C_FLAGS_RELWITHDEBINFO}")
355-
message(" - LDFLAGS for executables ............ ${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}")
356-
message(" - LDFLAGS for shared libraries ....... ${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO}")
357-
message("Debug configuration:")
358-
message(" - CFLAGS ............................. ${CMAKE_C_FLAGS_DEBUG}")
359-
message(" - LDFLAGS for executables ............ ${CMAKE_EXE_LINKER_FLAGS_DEBUG}")
360-
message(" - LDFLAGS for shared libraries ....... ${CMAKE_SHARED_LINKER_FLAGS_DEBUG}")
361-
endif()
362-
if(SECP256K1_APPEND_CFLAGS)
363-
message("SECP256K1_APPEND_CFLAGS ............... ${SECP256K1_APPEND_CFLAGS}")
364-
endif()
340+
include(FlagsSummary)
341+
flags_summary()
365342
message("")
366343
if(print_msan_notice)
367344
message(

cmake/FlagsSummary.cmake

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
include_guard(GLOBAL)
2+
3+
function(indent_message header content indent_num)
4+
if(indent_num GREATER 0)
5+
string(REPEAT " " ${indent_num} indentation)
6+
string(REPEAT "." ${indent_num} tail)
7+
string(REGEX REPLACE "${tail}$" "" header "${header}")
8+
endif()
9+
message("${indentation}${header} ${content}")
10+
endfunction()
11+
12+
# Print tools' flags on best-effort. Include the abstracted
13+
# CMake flags that we touch ourselves.
14+
function(print_flags_per_config config indent_num)
15+
string(TOUPPER "${config}" config_uppercase)
16+
17+
string(STRIP "${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${config_uppercase}}" c_language_flags)
18+
string(STRIP "${c_language_flags} ${CMAKE_C${CMAKE_C_STANDARD}_STANDARD_COMPILE_OPTION}" c_compiler_flags)
19+
get_target_property(pic secp256k1 POSITION_INDEPENDENT_CODE)
20+
if(pic AND CMAKE_C_COMPILE_OPTIONS_PIC)
21+
string(APPEND c_compiler_flags " ${CMAKE_C_COMPILE_OPTIONS_PIC}")
22+
endif()
23+
if(CMAKE_C_COMPILE_OPTIONS_VISIBILITY AND CMAKE_C_VISIBILITY_PRESET)
24+
string(APPEND c_compiler_flags " ${CMAKE_C_COMPILE_OPTIONS_VISIBILITY}${CMAKE_C_VISIBILITY_PRESET}")
25+
endif()
26+
get_directory_property(compile_options COMPILE_OPTIONS)
27+
list(JOIN compile_options " " compile_options)
28+
string(STRIP "${c_compiler_flags} ${compile_options}" c_compiler_flags)
29+
string(STRIP "${c_compiler_flags} ${SECP256K1_APPEND_CFLAGS}" c_compiler_flags)
30+
indent_message("C compiler flags ......................" "${c_compiler_flags}" ${indent_num})
31+
32+
if(BUILD_SHARED_LIBS)
33+
string(STRIP "${CMAKE_SHARED_LINKER_FLAGS} ${CMAKE_SHARED_LINKER_FLAGS_${config_uppercase}}" linker_flags)
34+
if(NOT MSVC)
35+
string(STRIP "${c_language_flags} ${linker_flags}" linker_flags)
36+
endif()
37+
indent_message("Linker flags .........................." "${linker_flags}" ${indent_num})
38+
else()
39+
string(STRIP "${CMAKE_STATIC_LINKER_FLAGS} ${CMAKE_STATIC_LINKER_FLAGS_${config_uppercase}}" archiver_options)
40+
indent_message("Archiver options ......................" "${archiver_options}" ${indent_num})
41+
endif()
42+
endfunction()
43+
44+
function(flags_summary)
45+
get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
46+
if(is_multi_config)
47+
list(JOIN CMAKE_CONFIGURATION_TYPES ", " configs)
48+
message("Available build configurations ........ ${configs}")
49+
if(CMAKE_GENERATOR MATCHES "Visual Studio")
50+
set(default_config "Debug")
51+
else()
52+
list(GET CMAKE_CONFIGURATION_TYPES 0 default_config)
53+
endif()
54+
message("Default build configuration ........... ${default_config}")
55+
foreach(config IN LISTS CMAKE_CONFIGURATION_TYPES)
56+
message("")
57+
message("'${config}' build configuration:")
58+
print_flags_per_config(${config} 2)
59+
endforeach()
60+
else()
61+
message("CMAKE_BUILD_TYPE ...................... ${CMAKE_BUILD_TYPE}")
62+
print_flags_per_config(${CMAKE_BUILD_TYPE} 0)
63+
endif()
64+
message("")
65+
message([=[
66+
NOTE: The summary above may not exactly match the final applied build flags
67+
if any additional CMAKE_* or environment variables have been modified.
68+
To see the exact flags applied, build with the --verbose option.
69+
]=])
70+
endfunction()

0 commit comments

Comments
 (0)