Skip to content

Commit 8ff8b25

Browse files
committed
cmake: Rework flags summary
1 parent 5d31c32 commit 8ff8b25

File tree

2 files changed

+90
-26
lines changed

2 files changed

+90
-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

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
38+
string(STRIP "${CMAKE_SHARED_LIBRARY_C_FLAGS} ${linker_flags}" linker_flags)
39+
get_target_property(soversion secp256k1 SOVERSION)
40+
string(STRIP "${linker_flags} ${CMAKE_SHARED_LIBRARY_SONAME_C_FLAG}${PROJECT_NAME}.so.${soversion}" linker_flags)
41+
endif()
42+
if(APPLE)
43+
get_target_property(compatibility_version secp256k1 MACHO_COMPATIBILITY_VERSION)
44+
if(compatibility_version)
45+
string(STRIP "${linker_flags} ${CMAKE_C_OSX_COMPATIBILITY_VERSION_FLAG}${compatibility_version}" linker_flags)
46+
endif()
47+
get_target_property(current_version secp256k1 MACHO_CURRENT_VERSION)
48+
if(compatibility_version)
49+
string(STRIP "${linker_flags} ${CMAKE_C_OSX_CURRENT_VERSION_FLAG}${current_version}" linker_flags)
50+
endif()
51+
endif()
52+
indent_message("Linker flags .........................." "${linker_flags}" ${indent_num})
53+
else()
54+
string(REGEX REPLACE "(^| )<[^ ]*>( |$)" "" archiver_options "${CMAKE_C_ARCHIVE_CREATE}")
55+
string(STRIP "${archiver_options} ${CMAKE_STATIC_LINKER_FLAGS}" archiver_options)
56+
string(STRIP "${archiver_options} ${CMAKE_STATIC_LINKER_FLAGS_${config_uppercase}}" archiver_options)
57+
indent_message("Archiver options ......................" "${archiver_options}" ${indent_num})
58+
endif()
59+
endfunction()
60+
61+
function(flags_summary)
62+
get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
63+
if(is_multi_config)
64+
list(JOIN CMAKE_CONFIGURATION_TYPES ", " configs)
65+
message("Available build configurations ........ ${configs}")
66+
if(CMAKE_GENERATOR MATCHES "Visual Studio")
67+
set(default_config "Debug")
68+
else()
69+
list(GET CMAKE_CONFIGURATION_TYPES 0 default_config)
70+
endif()
71+
message("Default build configuration ........... ${default_config}")
72+
foreach(config IN LISTS CMAKE_CONFIGURATION_TYPES)
73+
message("")
74+
message("'${config}' build configuration:")
75+
print_flags_per_config("${config}" 2)
76+
endforeach()
77+
else()
78+
message("CMAKE_BUILD_TYPE ...................... ${CMAKE_BUILD_TYPE}")
79+
print_flags_per_config("${CMAKE_BUILD_TYPE}" 0)
80+
endif()
81+
message("")
82+
message([=[
83+
NOTE: The summary above may not exactly match the final applied build flags
84+
if any additional CMAKE_* or environment variables have been modified.
85+
To see the exact flags applied, build with the --verbose option.
86+
]=])
87+
endfunction()

0 commit comments

Comments
 (0)