Skip to content

Commit 2248b8b

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

File tree

2 files changed

+74
-26
lines changed

2 files changed

+74
-26
lines changed

CMakeLists.txt

+3-26
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

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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)
21+
string(APPEND c_compiler_flags " ${CMAKE_C_COMPILE_OPTIONS_PIC}")
22+
endif()
23+
if(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 "${c_language_flags} ${CMAKE_SHARED_LINKER_FLAGS}" linker_flags)
34+
string(STRIP "${linker_flags} ${CMAKE_SHARED_LINKER_FLAGS_${config_uppercase}}" linker_flags)
35+
if(pic)
36+
string(PREPEND linker_flags "${CMAKE_C_COMPILE_OPTIONS_PIC} ")
37+
endif()
38+
indent_message("Linker flags .........................." "${linker_flags}" ${indent_num})
39+
else()
40+
string(STRIP "${CMAKE_STATIC_LINKER_FLAGS} ${CMAKE_STATIC_LINKER_FLAGS_${config_uppercase}}" archiver_options)
41+
indent_message("Archiver options ......................" "${archiver_options}" ${indent_num})
42+
endif()
43+
endfunction()
44+
45+
function(flags_summary)
46+
get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
47+
if(is_multi_config)
48+
list(JOIN CMAKE_CONFIGURATION_TYPES ", " configs)
49+
message("Available build configurations ........ ${configs}")
50+
if(CMAKE_GENERATOR MATCHES "Visual Studio")
51+
set(default_config "Debug")
52+
else()
53+
list(GET CMAKE_CONFIGURATION_TYPES 0 default_config)
54+
endif()
55+
message("Default build configuration ........... ${default_config}")
56+
foreach(config IN LISTS CMAKE_CONFIGURATION_TYPES)
57+
message("")
58+
message("'${config}' build configuration:")
59+
print_flags_per_config(${config} 2)
60+
endforeach()
61+
else()
62+
message("CMAKE_BUILD_TYPE ...................... ${CMAKE_BUILD_TYPE}")
63+
print_flags_per_config(${CMAKE_BUILD_TYPE} 0)
64+
endif()
65+
message("")
66+
message([=[
67+
NOTE: The summary above may not exactly match the final applied build flags
68+
if any additional CMAKE_* or environment variables have been modified.
69+
To see the exact flags applied, build with the --verbose option.
70+
]=])
71+
endfunction()

0 commit comments

Comments
 (0)