Skip to content

Commit da3ed8b

Browse files
committed
Merge bitcoin/bitcoin#31662: cmake: Do not modify CMAKE_TRY_COMPILE_TARGET_TYPE globally
2c4b229 cmake: Introduce `FUZZ_LIBS` (Hennadii Stepanov) ea929c0 scripted-diff: Rename CMake helper module (Hennadii Stepanov) 8d238c1 cmake: Delete `check_cxx_source_links*` macros (Hennadii Stepanov) 71bf829 cmake: Convert `check_cxx_source_compiles_with_flags` to a function (Hennadii Stepanov) 88ee680 cmake: Delete `check_cxx_source_links_with_flags` macro (Hennadii Stepanov) 09e8fd2 build: Don't override CMake's default try_compile target (Hennadii Stepanov) Pull request description: This was requested in bitcoin/bitcoin#31359 (comment). From bitcoin/bitcoin#31359 (comment): > (Almost?) every CMake check internally uses the [`try_compile()`](https://cmake.org/cmake/help/latest/command/try_compile.html) command, whose behaviour, in turn, depends on the [`CMAKE_TRY_COMPILE_TARGET_TYPE`](https://cmake.org/cmake/help/latest/variable/CMAKE_TRY_COMPILE_TARGET_TYPE.html) variable: > > 1. The default value, `EXECUTABLE`, enables both compiler and linker checks. > > 2. The `STATIC_LIBRARY` value enables only compiler checks. > > > To mimic Autotools' behaviour, we [disabled](https://github.com/bitcoin/bitcoin/blob/d3f42fa08fc385752881afa5740f40287cfb4d8b/cmake/module/CheckSourceCompilesAndLinks.cmake#L9-L10) linker checks by setting `CMAKE_TRY_COMPILE_TARGET_TYPE` to `STATIC_LIBRARY` globally (perhaps not the best design). This effectively separates the entire CMake script into regions where `CMAKE_TRY_COMPILE_TARGET_TYPE` is: > > * unset > > * set to `STATIC_LIBRARY` > > * set to `EXECUTABLE` From bitcoin/bitcoin#31359 (comment): > > This seems very fragile and unintuitive, and the fact that this could silently break at any point is not documented in any way. I don't think other bad design decisions should lead to us having to write even more boilerplate code to fix things that should "just work" (minus the upstream bugs). > > Agreed. I forgot that we set `CMAKE_TRY_COMPILE_TARGET_TYPE` globally. And even worse, it's buried in a module. If that upsets CMake internal tests, I think we should undo that. This PR ensures that `CMAKE_TRY_COMPILE_TARGET_TYPE` is modified only within local scopes. Additionally, the `FUZZ_LIBS` variable has been introduced to handle additional libraries required for linking, rather than link options, in certain build environment, such as OSS-Fuzz. ACKs for top commit: TheCharlatan: Re-ACK 2c4b229 theuni: utACK 2c4b229 Tree-SHA512: f72ffa8f50f216fc1a2f8027ba8ddfd4acd42b94ff6c1cb2138f2da51eb8f945660e97d3c247d7f3f7ec8dfebbccec3ab84347d6ae2e3f8a40f3d7aa8b14cde9
2 parents 9d7672b + 2c4b229 commit da3ed8b

7 files changed

+76
-64
lines changed

CMakeLists.txt

+5-2
Original file line numberDiff line numberDiff line change
@@ -378,13 +378,16 @@ endif()
378378
target_link_options(sanitize_interface INTERFACE ${SANITIZER_LDFLAGS})
379379

380380
if(BUILD_FUZZ_BINARY)
381-
include(CheckSourceCompilesAndLinks)
382-
check_cxx_source_links_with_flags("${SANITIZER_LDFLAGS}" "
381+
target_link_libraries(core_interface INTERFACE ${FUZZ_LIBS})
382+
include(CheckSourceCompilesWithFlags)
383+
check_cxx_source_compiles_with_flags("
383384
#include <cstdint>
384385
#include <cstddef>
385386
extern \"C\" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { return 0; }
386387
// No main() function.
387388
" FUZZ_BINARY_LINKS_WITHOUT_MAIN_FUNCTION
389+
LDFLAGS ${SANITIZER_LDFLAGS}
390+
LINK_LIBRARIES ${FUZZ_LIBS}
388391
)
389392
endif()
390393

cmake/crc32c.cmake

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# buildsystem.
88

99
include(CheckCXXSourceCompiles)
10+
include(CheckSourceCompilesWithFlags)
1011

1112
# Check for __builtin_prefetch support in the compiler.
1213
check_cxx_source_compiles("
@@ -42,7 +43,7 @@ if(MSVC)
4243
else()
4344
set(SSE42_CXXFLAGS -msse4.2)
4445
endif()
45-
check_cxx_source_compiles_with_flags("${SSE42_CXXFLAGS}" "
46+
check_cxx_source_compiles_with_flags("
4647
#include <cstdint>
4748
#if defined(_MSC_VER)
4849
#include <intrin.h>
@@ -58,11 +59,12 @@ check_cxx_source_compiles_with_flags("${SSE42_CXXFLAGS}" "
5859
return l;
5960
}
6061
" HAVE_SSE42
62+
CXXFLAGS ${SSE42_CXXFLAGS}
6163
)
6264

6365
# Check for ARMv8 w/ CRC and CRYPTO extensions support in the compiler.
6466
set(ARM64_CRC_CXXFLAGS -march=armv8-a+crc+crypto)
65-
check_cxx_source_compiles_with_flags("${ARM64_CRC_CXXFLAGS}" "
67+
check_cxx_source_compiles_with_flags("
6668
#include <arm_acle.h>
6769
#include <arm_neon.h>
6870
@@ -76,6 +78,7 @@ check_cxx_source_compiles_with_flags("${ARM64_CRC_CXXFLAGS}" "
7678
return 0;
7779
}
7880
" HAVE_ARM64_CRC32C
81+
CXXFLAGS ${ARM64_CRC_CXXFLAGS}
7982
)
8083

8184
add_library(crc32c STATIC EXCLUDE_FROM_ALL

cmake/introspection.cmake

+9-5
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,11 @@ check_cxx_source_compiles("
160160
)
161161

162162
if(NOT MSVC)
163-
include(CheckSourceCompilesAndLinks)
163+
include(CheckSourceCompilesWithFlags)
164164

165165
# Check for SSE4.1 intrinsics.
166166
set(SSE41_CXXFLAGS -msse4.1)
167-
check_cxx_source_compiles_with_flags("${SSE41_CXXFLAGS}" "
167+
check_cxx_source_compiles_with_flags("
168168
#include <immintrin.h>
169169
170170
int main()
@@ -175,12 +175,13 @@ if(NOT MSVC)
175175
return _mm_extract_epi32(r, 3);
176176
}
177177
" HAVE_SSE41
178+
CXXFLAGS ${SSE41_CXXFLAGS}
178179
)
179180
set(ENABLE_SSE41 ${HAVE_SSE41})
180181

181182
# Check for AVX2 intrinsics.
182183
set(AVX2_CXXFLAGS -mavx -mavx2)
183-
check_cxx_source_compiles_with_flags("${AVX2_CXXFLAGS}" "
184+
check_cxx_source_compiles_with_flags("
184185
#include <immintrin.h>
185186
186187
int main()
@@ -189,12 +190,13 @@ if(NOT MSVC)
189190
return _mm256_extract_epi32(l, 7);
190191
}
191192
" HAVE_AVX2
193+
CXXFLAGS ${AVX2_CXXFLAGS}
192194
)
193195
set(ENABLE_AVX2 ${HAVE_AVX2})
194196

195197
# Check for x86 SHA-NI intrinsics.
196198
set(X86_SHANI_CXXFLAGS -msse4 -msha)
197-
check_cxx_source_compiles_with_flags("${X86_SHANI_CXXFLAGS}" "
199+
check_cxx_source_compiles_with_flags("
198200
#include <immintrin.h>
199201
200202
int main()
@@ -205,12 +207,13 @@ if(NOT MSVC)
205207
return _mm_extract_epi32(_mm_sha256rnds2_epu32(i, j, k), 0);
206208
}
207209
" HAVE_X86_SHANI
210+
CXXFLAGS ${X86_SHANI_CXXFLAGS}
208211
)
209212
set(ENABLE_X86_SHANI ${HAVE_X86_SHANI})
210213

211214
# Check for ARMv8 SHA-NI intrinsics.
212215
set(ARM_SHANI_CXXFLAGS -march=armv8-a+crypto)
213-
check_cxx_source_compiles_with_flags("${ARM_SHANI_CXXFLAGS}" "
216+
check_cxx_source_compiles_with_flags("
214217
#include <arm_neon.h>
215218
216219
int main()
@@ -222,6 +225,7 @@ if(NOT MSVC)
222225
vsha256su1q_u32(a, b, c);
223226
}
224227
" HAVE_ARM_SHANI
228+
CXXFLAGS ${ARM_SHANI_CXXFLAGS}
225229
)
226230
set(ENABLE_ARM_SHANI ${HAVE_ARM_SHANI})
227231
endif()

cmake/minisketch.cmake

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
# Distributed under the MIT software license, see the accompanying
33
# file COPYING or https://opensource.org/license/mit/.
44

5+
include(CheckSourceCompilesWithFlags)
6+
57
# Check for clmul instructions support.
68
if(MSVC)
7-
set(CLMUL_CXXFLAGS)
9+
set(CLMUL_CXXFLAGS "")
810
else()
911
set(CLMUL_CXXFLAGS -mpclmul)
1012
endif()
11-
check_cxx_source_compiles_with_flags("${CLMUL_CXXFLAGS}" "
13+
check_cxx_source_compiles_with_flags("
1214
#include <immintrin.h>
1315
#include <cstdint>
1416
@@ -22,6 +24,7 @@ check_cxx_source_compiles_with_flags("${CLMUL_CXXFLAGS}" "
2224
return e == 0;
2325
}
2426
" HAVE_CLMUL
27+
CXXFLAGS ${CLMUL_CXXFLAGS}
2528
)
2629

2730
add_library(minisketch_common INTERFACE)

cmake/module/CheckSourceCompilesAndLinks.cmake

-39
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
5+
include_guard(GLOBAL)
6+
7+
#[=[
8+
Check once if C++ source code can be compiled.
9+
10+
Options:
11+
12+
CXXFLAGS - A list of additional flags to pass to the compiler.
13+
14+
LDFLAGS - A list of additional flags to pass to the linker.
15+
16+
LINK_LIBRARIES - A list of libraries to add to the link command.
17+
18+
For historical reasons, among the CMake `CMAKE_REQUIRED_*` variables that influence
19+
`check_cxx_source_compiles()`, only `CMAKE_REQUIRED_FLAGS` is a string rather than
20+
a list. Additionally, `target_compile_options()` also expects a list of options.
21+
22+
The `check_cxx_source_compiles_with_flags()` function handles this case and accepts
23+
`CXXFLAGS` as a list, simplifying the code at the caller site.
24+
25+
#]=]
26+
function(check_cxx_source_compiles_with_flags source result_var)
27+
cmake_parse_arguments(PARSE_ARGV 2 _ "" "" "CXXFLAGS;LDFLAGS;LINK_LIBRARIES")
28+
list(JOIN __CXXFLAGS " " CMAKE_REQUIRED_FLAGS)
29+
set(CMAKE_REQUIRED_LINK_OPTIONS ${__LDFLAGS})
30+
set(CMAKE_REQUIRED_LIBRARIES ${__LINK_LIBRARIES})
31+
include(CheckCXXSourceCompiles)
32+
check_cxx_source_compiles("${source}" ${result_var})
33+
set(${result_var} ${${result_var}} PARENT_SCOPE)
34+
endfunction()

cmake/module/TestAppendRequiredLibraries.cmake

+18-14
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ function(test_append_socket_library target)
2525
}
2626
")
2727

28-
include(CheckSourceCompilesAndLinks)
29-
check_cxx_source_links("${check_socket_source}" IFADDR_LINKS_WITHOUT_LIBSOCKET)
28+
include(CheckCXXSourceCompiles)
29+
check_cxx_source_compiles("${check_socket_source}" IFADDR_LINKS_WITHOUT_LIBSOCKET)
3030
if(NOT IFADDR_LINKS_WITHOUT_LIBSOCKET)
31-
check_cxx_source_links_with_libs(socket "${check_socket_source}" IFADDR_NEEDS_LINK_TO_LIBSOCKET)
31+
include(CheckSourceCompilesWithFlags)
32+
check_cxx_source_compiles_with_flags("${check_socket_source}" IFADDR_NEEDS_LINK_TO_LIBSOCKET
33+
LINK_LIBRARIES socket
34+
)
3235
if(IFADDR_NEEDS_LINK_TO_LIBSOCKET)
3336
target_link_libraries(${target} INTERFACE socket)
3437
else()
@@ -77,16 +80,17 @@ function(test_append_atomic_library target)
7780
}
7881
")
7982

80-
include(CheckSourceCompilesAndLinks)
81-
check_cxx_source_links("${check_atomic_source}" STD_ATOMIC_LINKS_WITHOUT_LIBATOMIC)
82-
if(STD_ATOMIC_LINKS_WITHOUT_LIBATOMIC)
83-
return()
84-
endif()
85-
86-
check_cxx_source_links_with_libs(atomic "${check_atomic_source}" STD_ATOMIC_NEEDS_LINK_TO_LIBATOMIC)
87-
if(STD_ATOMIC_NEEDS_LINK_TO_LIBATOMIC)
88-
target_link_libraries(${target} INTERFACE atomic)
89-
else()
90-
message(FATAL_ERROR "Cannot figure out how to use std::atomic.")
83+
include(CheckCXXSourceCompiles)
84+
check_cxx_source_compiles("${check_atomic_source}" STD_ATOMIC_LINKS_WITHOUT_LIBATOMIC)
85+
if(NOT STD_ATOMIC_LINKS_WITHOUT_LIBATOMIC)
86+
include(CheckSourceCompilesWithFlags)
87+
check_cxx_source_compiles_with_flags("${check_atomic_source}" STD_ATOMIC_NEEDS_LINK_TO_LIBATOMIC
88+
LINK_LIBRARIES atomic
89+
)
90+
if(STD_ATOMIC_NEEDS_LINK_TO_LIBATOMIC)
91+
target_link_libraries(${target} INTERFACE atomic)
92+
else()
93+
message(FATAL_ERROR "Cannot figure out how to use std::atomic.")
94+
endif()
9195
endif()
9296
endfunction()

0 commit comments

Comments
 (0)