@@ -80,6 +80,13 @@ set(CMAKE_CXX_EXTENSIONS OFF)
80
80
81
81
set (configure_warnings)
82
82
83
+ # The core interface library aims to encapsulate all common build flags.
84
+ # It is intended to be a usage requirement for all other targets.
85
+ add_library (core INTERFACE )
86
+
87
+ include (TryAppendCXXFlags)
88
+ include (TryAppendLinkerFlag)
89
+
83
90
if (WIN32 )
84
91
#[=[
85
92
This build system supports two ways to build binaries for Windows.
@@ -97,29 +104,63 @@ if(WIN32)
97
104
- A run-time library must be specified explicitly using _MT definition.
98
105
]=]
99
106
100
- add_compile_definitions (_WIN32_WINNT=0x0601 _WIN32_IE=0x0501 WIN32_LEAN_AND_MEAN NOMINMAX)
107
+ target_compile_definitions (core INTERFACE
108
+ _WIN32_WINNT=0x0601
109
+ _WIN32_IE=0x0501
110
+ WIN32_LEAN_AND_MEAN
111
+ NOMINMAX
112
+ )
101
113
102
114
if (MSVC )
103
115
set (CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>" )
104
- add_compile_options (/utf-8 /Zc:__cplusplus)
116
+ target_compile_options (core INTERFACE
117
+ /utf-8
118
+ /Zc:__cplusplus
119
+ )
105
120
# Improve parallelism in MSBuild.
106
121
# See: https://devblogs.microsoft.com/cppblog/improved-parallelism-in-msbuild/.
107
122
list (APPEND CMAKE_VS_GLOBALS "UseMultiToolTask=true" )
108
123
endif ()
109
124
110
125
if (MINGW)
111
- add_compile_definitions (WIN32 _WINDOWS _MT)
126
+ target_compile_definitions (core INTERFACE
127
+ WIN32
128
+ _WINDOWS
129
+ _MT
130
+ )
112
131
# We require Windows 7 (NT 6.1) or later.
113
- add_link_options (-Wl,--major-subsystem-version ,6 -Wl,--minor-subsystem-version ,1)
132
+ try_append_linker_flag(core "-Wl,--major-subsystem-version,6" )
133
+ try_append_linker_flag(core "-Wl,--minor-subsystem-version,1" )
114
134
endif ()
115
135
endif ()
116
136
137
+ # Use 64-bit off_t on 32-bit Linux.
138
+ if (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_SIZEOF_VOID_P EQUAL 4)
139
+ # Ensure 64-bit offsets are used for filesystem accesses for 32-bit compilation.
140
+ target_compile_definitions (core INTERFACE
141
+ _FILE_OFFSET_BITS=64
142
+ )
143
+ endif ()
144
+
117
145
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin" )
118
- add_compile_definitions (MAC_OSX)
146
+ target_compile_definitions (core INTERFACE
147
+ MAC_OSX
148
+ )
149
+ # These flags are specific to ld64, and may cause issues with other linkers.
150
+ # For example: GNU ld will interpret -dead_strip as -de and then try and use
151
+ # "ad_strip" as the symbol for the entry point.
152
+ try_append_linker_flag(core "-Wl,-dead_strip" )
153
+ try_append_linker_flag(core "-Wl,-dead_strip_dylibs" )
154
+ try_append_linker_flag(core "-Wl,-headerpad_max_install_names" )
119
155
endif ()
120
156
121
- if (CMAKE_CROSSCOMPILING AND DEPENDS_ALLOW_HOST_PACKAGES)
122
- list (APPEND CMAKE_FIND_ROOT_PATH "${CMAKE_SYSTEM_PREFIX_PATH} " )
157
+ if (CMAKE_CROSSCOMPILING )
158
+ target_compile_definitions (core INTERFACE ${DEPENDS_COMPILE_DEFINITIONS} )
159
+ target_compile_options (core INTERFACE "$<$<COMPILE_LANGUAGE:C>:${DEPENDS_C_COMPILER_FLAGS} >" )
160
+ target_compile_options (core INTERFACE "$<$<COMPILE_LANGUAGE:CXX>:${DEPENDS_CXX_COMPILER_FLAGS} >" )
161
+ if (DEPENDS_ALLOW_HOST_PACKAGES)
162
+ list (APPEND CMAKE_FIND_ROOT_PATH "${CMAKE_SYSTEM_PREFIX_PATH} " )
163
+ endif ()
123
164
endif ()
124
165
125
166
include (AddThreadsIfNeeded)
@@ -140,6 +181,47 @@ include(cmake/leveldb.cmake)
140
181
include (cmake/minisketch.cmake)
141
182
include (cmake/secp256k1.cmake)
142
183
184
+ include (ProcessConfigurations)
185
+ set_default_config(RelWithDebInfo)
186
+
187
+ # Redefine configuration flags.
188
+ target_compile_definitions (core INTERFACE
189
+ $<$<CONFIG:Debug>:DEBUG>
190
+ $<$<CONFIG:Debug>:DEBUG_LOCKORDER>
191
+ $<$<CONFIG:Debug>:DEBUG_LOCKCONTENTION>
192
+ $<$<CONFIG:Debug>:RPC_DOC_CHECK>
193
+ $<$<CONFIG:Debug>:ABORT_ON_FAILED_ASSUME>
194
+ )
195
+ # We leave assertions on.
196
+ if (MSVC )
197
+ remove_c_flag_from_all_configs(/DNDEBUG)
198
+ remove_cxx_flag_from_all_configs(/DNDEBUG)
199
+ else ()
200
+ remove_c_flag_from_all_configs(-DNDEBUG)
201
+ remove_cxx_flag_from_all_configs(-DNDEBUG)
202
+
203
+ # Prefer -O2 optimization level. (-O3 is CMake's default for Release for many compilers.)
204
+ replace_c_flag_in_config(Release -O3 -O2)
205
+ replace_cxx_flag_in_config(Release -O3 -O2)
206
+
207
+ set (debug_c_flags "" )
208
+ set (debug_cxx_flags "" )
209
+ try_append_cxx_flags(debug_cxx_flags "-O0" RESULT_VAR compiler_supports_O0)
210
+ if (compiler_supports_O0)
211
+ string (STRIP "${debug_c_flags} -O0" debug_c_flags)
212
+ endif ()
213
+ try_append_cxx_flags(debug_cxx_flags "-g3" RESULT_VAR compiler_supports_g3)
214
+ if (compiler_supports_g3)
215
+ string (STRIP "${debug_c_flags} -g3" debug_c_flags)
216
+ else ()
217
+ try_append_cxx_flags(debug_cxx_flags "-g" )
218
+ string (STRIP "${debug_c_flags} -g" debug_c_flags)
219
+ endif ()
220
+ try_append_cxx_flags(debug_cxx_flags "-ftrapv" )
221
+ set (CMAKE_C_FLAGS_DEBUG "${debug_c_flags} " )
222
+ set (CMAKE_CXX_FLAGS_DEBUG "${debug_cxx_flags} " )
223
+ endif ()
224
+
143
225
include (cmake/optional .cmake)
144
226
145
227
if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.14)
@@ -163,6 +245,9 @@ add_subdirectory(test)
163
245
164
246
include (cmake/tests.cmake)
165
247
248
+ get_target_property (definitions core INTERFACE_COMPILE_DEFINITIONS )
249
+ separate_by_configs(definitions )
250
+
166
251
message ("\n " )
167
252
message ("Configure summary" )
168
253
message ("=================" )
@@ -190,40 +275,33 @@ else()
190
275
set (cross_status "FALSE" )
191
276
endif ()
192
277
message ("Cross compiling ....................... ${cross_status} " )
193
- get_directory_property (definitions COMPILE_DEFINITIONS )
194
- string (REPLACE ";" " " definitions "${definitions} " )
195
- message ("Preprocessor defined macros ........... ${definitions} " )
278
+ message ("Preprocessor defined macros ........... ${definitions_ALL} " )
196
279
message ("C compiler ............................ ${CMAKE_C_COMPILER} " )
197
- message ("CFLAGS ................................ ${CMAKE_C_FLAGS} " )
280
+ list (JOIN DEPENDS_C_COMPILER_FLAGS " " depends_c_flags)
281
+ string (STRIP "${CMAKE_C_FLAGS} ${depends_c_flags} " combined_c_flags)
282
+ message ("CFLAGS ................................ ${combined_c_flags} " )
198
283
message ("C++ compiler .......................... ${CMAKE_CXX_COMPILER} " )
199
- message ("CXXFLAGS .............................. ${CMAKE_CXX_FLAGS} " )
200
- get_directory_property (common_compile_options COMPILE_OPTIONS)
201
- string (REPLACE ";" " " common_compile_options "${common_compile_options} " )
284
+ list (JOIN DEPENDS_CXX_COMPILER_FLAGS " " depends_cxx_flags)
285
+ string (STRIP "${CMAKE_CXX_FLAGS} ${depends_cxx_flags} " combined_cxx_flags)
286
+ message ("CXXFLAGS .............................. ${combined_cxx_flags} " )
287
+ get_target_property (common_compile_options core INTERFACE_COMPILE_OPTIONS)
288
+ if (common_compile_options)
289
+ list (JOIN common_compile_options " " common_compile_options)
290
+ else ()
291
+ set (common_compile_options)
292
+ endif ()
293
+ string (GENEX_STRIP "${common_compile_options} " common_compile_options)
202
294
message ("Common compile options ................ ${common_compile_options} " )
203
- get_directory_property (common_link_options LINK_OPTIONS)
204
- string (REPLACE ";" " " common_link_options "${common_link_options} " )
205
- message ("Common link options ................... ${common_link_options} " )
206
- if (DEFINED CMAKE_BUILD_TYPE )
207
- message ("Build type:" )
208
- message (" - CMAKE_BUILD_TYPE ................... ${CMAKE_BUILD_TYPE} " )
209
- string (TOUPPER "${CMAKE_BUILD_TYPE} " build_type )
210
- message (" - CFLAGS ............................. ${CMAKE_C_FLAGS_${build_type} }" )
211
- message (" - CXXFLAGS ........................... ${CMAKE_CXX_FLAGS_${build_type} }" )
212
- message (" - LDFLAGS for executables ............ ${CMAKE_EXE_LINKER_FLAGS_${build_type} }" )
213
- message (" - LDFLAGS for shared libraries ....... ${CMAKE_SHARED_LINKER_FLAGS_${build_type} }" )
295
+ get_target_property (common_link_options core INTERFACE_LINK_OPTIONS)
296
+ if (common_link_options)
297
+ list (JOIN common_link_options " " common_link_options)
214
298
else ()
215
- message ("Available configurations .............. ${CMAKE_CONFIGURATION_TYPES} " )
216
- message ("Debug configuration:" )
217
- message (" - CFLAGS ............................. ${CMAKE_C_FLAGS_DEBUG} " )
218
- message (" - CXXFLAGS ........................... ${CMAKE_CXX_FLAGS_DEBUG} " )
219
- message (" - LDFLAGS for executables ............ ${CMAKE_EXE_LINKER_FLAGS_DEBUG} " )
220
- message (" - LDFLAGS for shared libraries ....... ${CMAKE_SHARED_LINKER_FLAGS_DEBUG} " )
221
- message ("Release configuration:" )
222
- message (" - CFLAGS ............................. ${CMAKE_C_FLAGS_RELEASE} " )
223
- message (" - CXXFLAGS ........................... ${CMAKE_CXX_FLAGS_RELEASE} " )
224
- message (" - LDFLAGS for executables ............ ${CMAKE_EXE_LINKER_FLAGS_RELEASE} " )
225
- message (" - LDFLAGS for shared libraries ....... ${CMAKE_SHARED_LINKER_FLAGS_RELEASE} " )
299
+ set (common_link_options)
226
300
endif ()
301
+ message ("Common link options ................... ${common_link_options} " )
302
+ message ("Linker flags for executables .......... ${CMAKE_EXE_LINKER_FLAGS} " )
303
+ message ("Linker flags for shared libraries ..... ${CMAKE_SHARED_LINKER_FLAGS} " )
304
+ print_config_flags()
227
305
message ("Use assembly routines ................. ${ASM} " )
228
306
message ("Use ccache for compiling .............. ${CCACHE} " )
229
307
message ("\n " )
@@ -234,3 +312,17 @@ if(configure_warnings)
234
312
endforeach ()
235
313
message (" ******\n " )
236
314
endif ()
315
+
316
+ # We want all build properties to be encapsulated properly.
317
+ get_directory_property (global_compile_definitions COMPILE_DEFINITIONS )
318
+ if (global_compile_definitions)
319
+ message (AUTHOR_WARNING "The directory's COMPILE_DEFINITIONS property is not empty: ${global_compile_definitions} " )
320
+ endif ()
321
+ get_directory_property (global_compile_options COMPILE_OPTIONS)
322
+ if (global_compile_options)
323
+ message (AUTHOR_WARNING "The directory's COMPILE_OPTIONS property is not empty: ${global_compile_options} " )
324
+ endif ()
325
+ get_directory_property (global_link_options LINK_OPTIONS)
326
+ if (global_link_options)
327
+ message (AUTHOR_WARNING "The directory's LINK_OPTIONS property is not empty: ${global_link_options} " )
328
+ endif ()
0 commit comments