Skip to content

Commit 71bf829

Browse files
committed
cmake: Convert check_cxx_source_compiles_with_flags to a function
1 parent 88ee680 commit 71bf829

File tree

5 files changed

+48
-16
lines changed

5 files changed

+48
-16
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,12 +387,13 @@ target_link_options(sanitize_interface INTERFACE ${SANITIZER_LDFLAGS})
387387

388388
if(BUILD_FUZZ_BINARY)
389389
include(CheckSourceCompilesAndLinks)
390-
check_cxx_source_compiles_with_flags("${SANITIZER_LDFLAGS}" "
390+
check_cxx_source_compiles_with_flags("
391391
#include <cstdint>
392392
#include <cstddef>
393393
extern \"C\" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { return 0; }
394394
// No main() function.
395395
" FUZZ_BINARY_LINKS_WITHOUT_MAIN_FUNCTION
396+
LDFLAGS ${SANITIZER_LDFLAGS}
396397
)
397398
endif()
398399

cmake/crc32c.cmake

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# buildsystem.
88

99
include(CheckCXXSourceCompiles)
10+
include(CheckSourceCompilesAndLinks)
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_common INTERFACE)

cmake/introspection.cmake

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ if(NOT MSVC)
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

Lines changed: 5 additions & 2 deletions
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(CheckSourceCompilesAndLinks)
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

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,34 @@ include_guard(GLOBAL)
66
include(CheckCXXSourceCompiles)
77
include(CMakePushCheckState)
88

9-
macro(check_cxx_source_compiles_with_flags flags source)
10-
cmake_push_check_state(RESET)
11-
set(CMAKE_REQUIRED_FLAGS ${flags})
12-
list(JOIN CMAKE_REQUIRED_FLAGS " " CMAKE_REQUIRED_FLAGS)
13-
check_cxx_source_compiles("${source}" ${ARGN})
14-
cmake_pop_check_state()
15-
endmacro()
9+
#[=[
10+
Check once if C++ source code can be compiled.
11+
12+
Options:
13+
14+
CXXFLAGS - A list of additional flags to pass to the compiler.
15+
16+
LDFLAGS - A list of additional flags to pass to the linker.
17+
18+
LINK_LIBRARIES - A list of libraries to add to the link command.
19+
20+
For historical reasons, among the CMake `CMAKE_REQUIRED_*` variables that influence
21+
`check_cxx_source_compiles()`, only `CMAKE_REQUIRED_FLAGS` is a string rather than
22+
a list. Additionally, `target_compile_options()` also expects a list of options.
23+
24+
The `check_cxx_source_compiles_with_flags()` function handles this case and accepts
25+
`CXXFLAGS` as a list, simplifying the code at the caller site.
26+
27+
#]=]
28+
function(check_cxx_source_compiles_with_flags source result_var)
29+
cmake_parse_arguments(PARSE_ARGV 2 _ "" "" "CXXFLAGS;LDFLAGS;LINK_LIBRARIES")
30+
list(JOIN __CXXFLAGS " " CMAKE_REQUIRED_FLAGS)
31+
set(CMAKE_REQUIRED_LINK_OPTIONS ${__LDFLAGS})
32+
set(CMAKE_REQUIRED_LIBRARIES ${__LINK_LIBRARIES})
33+
include(CheckCXXSourceCompiles)
34+
check_cxx_source_compiles("${source}" ${result_var})
35+
set(${result_var} ${${result_var}} PARENT_SCOPE)
36+
endfunction()
1637

1738
macro(check_cxx_source_links_with_libs libs source)
1839
cmake_push_check_state(RESET)

0 commit comments

Comments
 (0)