Skip to content

Commit 4ac1e24

Browse files
committed
Merge #74: cmake: Fix and improve hardening for Windows
d889870 fixup! build: Generate `share/toolchain.cmake` in depends (Hennadii Stepanov) 8abb5fd fixup! cmake: Build `bitcoind` executable (Hennadii Stepanov) 861361c fixup! cmake: Add `SANITIZERS` option (Hennadii Stepanov) 402d2bf fixup! cmake: Add `WERROR` option (Hennadii Stepanov) 90dad92 fixup! cmake: Add `HARDENING` option (Hennadii Stepanov) 9913a24 fixup! cmake: Add platform-specific compiler flags (Hennadii Stepanov) a18f905 fixup! cmake: Add `TryAppendCXXFlags` module (Hennadii Stepanov) Pull request description: Some compile flags, for example `-fstack-protector-all`, might impact the linking stage as well. Instead of guessing, this PR makes the `try_append_cxx_flags` function add new flags in both compile and link contexts. Top commit has no ACKs. Tree-SHA512: 0e73e1f81bbb9d0afb3b08783cbd51128919c2f0b11ad9afbe963afef882ffff2332f9f6f47d2bda7ded9a15d4d6dd8e3a40460736c95eb7f2bed2325d42bdba
2 parents bbd3d4d + d889870 commit 4ac1e24

File tree

5 files changed

+49
-13
lines changed

5 files changed

+49
-13
lines changed

CMakeLists.txt

+10-9
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,12 @@ if(WIN32)
156156
# See: https://devblogs.microsoft.com/cppblog/improved-parallelism-in-msbuild/.
157157
list(APPEND CMAKE_VS_GLOBALS "UseMultiToolTask=true")
158158

159-
try_append_cxx_flags("/W3" TARGET core_interface)
160-
try_append_cxx_flags("/wd4018" TARGET core_interface)
161-
try_append_cxx_flags("/wd4244" TARGET core_interface)
162-
try_append_cxx_flags("/wd4267" TARGET core_interface)
163-
try_append_cxx_flags("/wd4715" TARGET core_interface)
164-
try_append_cxx_flags("/wd4805" TARGET core_interface)
159+
try_append_cxx_flags("/W3" TARGET core_interface SKIP_LINK)
160+
try_append_cxx_flags("/wd4018" TARGET core_interface SKIP_LINK)
161+
try_append_cxx_flags("/wd4244" TARGET core_interface SKIP_LINK)
162+
try_append_cxx_flags("/wd4267" TARGET core_interface SKIP_LINK)
163+
try_append_cxx_flags("/wd4715" TARGET core_interface SKIP_LINK)
164+
try_append_cxx_flags("/wd4805" TARGET core_interface SKIP_LINK)
165165
target_compile_definitions(core_interface INTERFACE
166166
_CRT_SECURE_NO_WARNINGS
167167
_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING
@@ -176,7 +176,7 @@ if(WIN32)
176176
)
177177
# Avoid the use of aligned vector instructions when building for Windows.
178178
# See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54412.
179-
try_append_cxx_flags("-Wa,-muse-unaligned-vector-move" TARGET core_interface)
179+
try_append_cxx_flags("-Wa,-muse-unaligned-vector-move" TARGET core_interface SKIP_LINK)
180180
try_append_linker_flag("-static" TARGET core_interface)
181181
# We require Windows 7 (NT 6.1) or later.
182182
try_append_linker_flag("-Wl,--major-subsystem-version,6" TARGET core_interface)
@@ -221,6 +221,7 @@ if(SANITIZERS)
221221
# fail if a bad argument is passed, e.g. -fsanitize=undfeined
222222
try_append_cxx_flags("-fsanitize=${SANITIZERS}" TARGET sanitizing_interface
223223
RESULT_VAR cxx_supports_sanitizers
224+
SKIP_LINK
224225
)
225226
if(NOT cxx_supports_sanitizers)
226227
message(FATAL_ERROR "Compiler did not accept requested flags.")
@@ -320,7 +321,7 @@ if(HARDENING)
320321
$<$<NOT:$<CONFIG:Debug>>:-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3>
321322
)
322323

323-
try_append_cxx_flags("-Wstack-protector" TARGET hardening_interface)
324+
try_append_cxx_flags("-Wstack-protector" TARGET hardening_interface SKIP_LINK)
324325
try_append_cxx_flags("-fstack-protector-all" TARGET hardening_interface)
325326
try_append_cxx_flags("-fcf-protection=full" TARGET hardening_interface)
326327

@@ -362,7 +363,7 @@ if(WERROR)
362363
else()
363364
set(werror_flag "-Werror")
364365
endif()
365-
try_append_cxx_flags(${werror_flag} TARGET core_interface RESULT_VAR compiler_supports_werror)
366+
try_append_cxx_flags(${werror_flag} TARGET core_interface RESULT_VAR compiler_supports_werror SKIP_LINK)
366367
if(NOT compiler_supports_werror)
367368
message(FATAL_ERROR "WERROR set but ${werror_flag} is not usable.")
368369
endif()

cmake/module/AddLibeventIfNeeded.cmake

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function(add_libevent_if_needed)
4848
)
4949
check_evhttp_connection_get_peer(PkgConfig::libevent)
5050
target_link_libraries(PkgConfig::libevent INTERFACE
51-
$<$<BOOL:${MINGW}>:iphlpapi;ssp;ws2_32>
51+
$<$<BOOL:${MINGW}>:iphlpapi;ws2_32>
5252
)
5353
add_library(libevent::libevent ALIAS PkgConfig::libevent)
5454

cmake/module/TryAppendCXXFlags.cmake

+29-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ include_guard(GLOBAL)
66
include(CheckCXXSourceCompiles)
77

88
#[=[
9+
Add language-wide flags, which will be passed to all invocations of the compiler.
10+
This includes invocations that drive compiling and those that drive linking.
11+
912
Usage examples:
1013
1114
try_append_cxx_flags("-Wformat -Wformat-security" VAR warn_cxx_flags)
@@ -44,7 +47,7 @@ In configuration output, this function prints a string by the following pattern:
4447
function(try_append_cxx_flags flags)
4548
cmake_parse_arguments(PARSE_ARGV 1
4649
TACXXF # prefix
47-
"" # options
50+
"SKIP_LINK" # options
4851
"TARGET;VAR;SOURCE;RESULT_VAR" # one_value_keywords
4952
"IF_CHECK_PASSED;IF_CHECK_FAILED" # multi_value_keywords
5053
)
@@ -98,10 +101,33 @@ function(try_append_cxx_flags flags)
98101
if(DEFINED TACXXF_RESULT_VAR)
99102
set(${TACXXF_RESULT_VAR} "${${result}}" PARENT_SCOPE)
100103
endif()
104+
105+
if(TACXXF_SKIP_LINK)
106+
return()
107+
endif()
108+
109+
# This forces running a linker.
110+
set(CMAKE_TRY_COMPILE_TARGET_TYPE EXECUTABLE)
111+
set(CMAKE_REQUIRED_FLAGS "${flags} ${working_linker_werror_flag}")
112+
check_cxx_source_compiles("${source}" ${result})
113+
114+
if(${result})
115+
if(DEFINED TACXXF_IF_CHECK_PASSED)
116+
if(DEFINED TACXXF_TARGET)
117+
target_link_options(${TACXXF_TARGET} INTERFACE ${TACXXF_IF_CHECK_PASSED})
118+
endif()
119+
else()
120+
if(DEFINED TACXXF_TARGET)
121+
target_link_options(${TACXXF_TARGET} INTERFACE ${flags})
122+
endif()
123+
endif()
124+
else()
125+
message(WARNING "The ${flags} fail(s) to link.")
126+
endif()
101127
endfunction()
102128

103129
if(MSVC)
104-
try_append_cxx_flags("/WX /options:strict" VAR working_compiler_werror_flag)
130+
try_append_cxx_flags("/WX /options:strict" VAR working_compiler_werror_flag SKIP_LINK)
105131
else()
106-
try_append_cxx_flags("-Werror" VAR working_compiler_werror_flag)
132+
try_append_cxx_flags("-Werror" VAR working_compiler_werror_flag SKIP_LINK)
107133
endif()

depends/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ $(host_prefix)/share/toolchain.cmake : toolchain.cmake.in $(host_prefix)/.stamp_
285285
-e 's|@no_upnp@|$(NO_UPNP)|' \
286286
-e 's|@no_natpmp@|$(NO_NATPMP)|' \
287287
-e 's|@no_usdt@|$(NO_USDT)|' \
288+
-e 's|@no_harden@|$(NO_HARDEN)|' \
288289
$< > $@
289290
touch $@
290291

depends/toolchain.cmake.in

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Copyright (c) 2023-present The Bitcoin Core developers
2+
# Distributed under the MIT software license, see the accompanying
3+
# file COPYING or https://opensource.org/license/mit/.
4+
15
# This file is expected to be highly volatile and may still change substantially.
26

37
set(CMAKE_SYSTEM_NAME @host_system@)
@@ -138,3 +142,7 @@ endif()
138142
if(NOT WITH_USDT AND "@no_usdt@" STREQUAL "1")
139143
set(WITH_USDT OFF CACHE STRING "")
140144
endif()
145+
146+
if(NOT HARDENING AND "@no_harden@" STREQUAL "1")
147+
set(HARDENING OFF CACHE STRING "")
148+
endif()

0 commit comments

Comments
 (0)