@@ -80,6 +80,13 @@ set(CMAKE_CXX_EXTENSIONS OFF)
8080
8181set (configure_warnings)
8282
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+ 
8390if (WIN32 )
8491  #[=[ 
8592  This build system supports two ways to build binaries for Windows. 
@@ -97,29 +104,63 @@ if(WIN32)
97104  - A run-time library must be specified explicitly using _MT definition. 
98105  ]=] 
99106
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+   )
101113
102114  if (MSVC )
103115    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+     )
105120    # Improve parallelism in MSBuild. 
106121    # See: https://devblogs.microsoft.com/cppblog/improved-parallelism-in-msbuild/. 
107122    list (APPEND  CMAKE_VS_GLOBALS "UseMultiToolTask=true" )
108123  endif ()
109124
110125  if (MINGW)
111-     add_compile_definitions (WIN32  _WINDOWS _MT)
126+     target_compile_definitions (core INTERFACE 
127+       WIN32 
128+       _WINDOWS
129+       _MT
130+     )
112131    # 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" )
114134  endif ()
115135endif ()
116136
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+ 
117145if (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" )
119155endif ()
120156
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 ()
123164endif ()
124165
125166include (AddThreadsIfNeeded)
@@ -140,6 +181,47 @@ include(cmake/leveldb.cmake)
140181include (cmake/minisketch.cmake)
141182include (cmake/secp256k1.cmake)
142183
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+ 
143225include (cmake/optional .cmake)
144226
145227if (CMAKE_VERSION  VERSION_GREATER_EQUAL  3.14)
@@ -163,6 +245,9 @@ add_subdirectory(test)
163245
164246include (cmake/tests.cmake)
165247
248+ get_target_property (definitions  core INTERFACE_COMPILE_DEFINITIONS )
249+ separate_by_configs(definitions )
250+ 
166251message ("\n " )
167252message ("Configure summary" )
168253message ("=================" )
@@ -190,40 +275,33 @@ else()
190275  set (cross_status "FALSE" )
191276endif ()
192277message ("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} " )
196279message ("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} " )
198283message ("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)
202294message ("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)
214298else ()
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)
226300endif ()
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()
227305message ("Use assembly routines ................. ${ASM} " )
228306message ("Use ccache for compiling .............. ${CCACHE} " )
229307message ("\n " )
@@ -234,3 +312,17 @@ if(configure_warnings)
234312    endforeach ()
235313    message ("  ******\n " )
236314endif ()
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