cmake: Convert check_cxx_source_compiles_with_flags to a function

This commit is contained in:
Hennadii Stepanov 2025-02-20 12:39:03 +00:00
parent 88ee6800c9
commit 71bf8294a9
No known key found for this signature in database
GPG key ID: 410108112E7EA81F
5 changed files with 48 additions and 16 deletions

View file

@ -387,12 +387,13 @@ target_link_options(sanitize_interface INTERFACE ${SANITIZER_LDFLAGS})
if(BUILD_FUZZ_BINARY) if(BUILD_FUZZ_BINARY)
include(CheckSourceCompilesAndLinks) include(CheckSourceCompilesAndLinks)
check_cxx_source_compiles_with_flags("${SANITIZER_LDFLAGS}" " check_cxx_source_compiles_with_flags("
#include <cstdint> #include <cstdint>
#include <cstddef> #include <cstddef>
extern \"C\" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { return 0; } extern \"C\" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { return 0; }
// No main() function. // No main() function.
" FUZZ_BINARY_LINKS_WITHOUT_MAIN_FUNCTION " FUZZ_BINARY_LINKS_WITHOUT_MAIN_FUNCTION
LDFLAGS ${SANITIZER_LDFLAGS}
) )
endif() endif()

View file

@ -7,6 +7,7 @@
# buildsystem. # buildsystem.
include(CheckCXXSourceCompiles) include(CheckCXXSourceCompiles)
include(CheckSourceCompilesAndLinks)
# Check for __builtin_prefetch support in the compiler. # Check for __builtin_prefetch support in the compiler.
check_cxx_source_compiles(" check_cxx_source_compiles("
@ -42,7 +43,7 @@ if(MSVC)
else() else()
set(SSE42_CXXFLAGS -msse4.2) set(SSE42_CXXFLAGS -msse4.2)
endif() endif()
check_cxx_source_compiles_with_flags("${SSE42_CXXFLAGS}" " check_cxx_source_compiles_with_flags("
#include <cstdint> #include <cstdint>
#if defined(_MSC_VER) #if defined(_MSC_VER)
#include <intrin.h> #include <intrin.h>
@ -58,11 +59,12 @@ check_cxx_source_compiles_with_flags("${SSE42_CXXFLAGS}" "
return l; return l;
} }
" HAVE_SSE42 " HAVE_SSE42
CXXFLAGS ${SSE42_CXXFLAGS}
) )
# Check for ARMv8 w/ CRC and CRYPTO extensions support in the compiler. # Check for ARMv8 w/ CRC and CRYPTO extensions support in the compiler.
set(ARM64_CRC_CXXFLAGS -march=armv8-a+crc+crypto) set(ARM64_CRC_CXXFLAGS -march=armv8-a+crc+crypto)
check_cxx_source_compiles_with_flags("${ARM64_CRC_CXXFLAGS}" " check_cxx_source_compiles_with_flags("
#include <arm_acle.h> #include <arm_acle.h>
#include <arm_neon.h> #include <arm_neon.h>
@ -76,6 +78,7 @@ check_cxx_source_compiles_with_flags("${ARM64_CRC_CXXFLAGS}" "
return 0; return 0;
} }
" HAVE_ARM64_CRC32C " HAVE_ARM64_CRC32C
CXXFLAGS ${ARM64_CRC_CXXFLAGS}
) )
add_library(crc32c_common INTERFACE) add_library(crc32c_common INTERFACE)

View file

@ -164,7 +164,7 @@ if(NOT MSVC)
# Check for SSE4.1 intrinsics. # Check for SSE4.1 intrinsics.
set(SSE41_CXXFLAGS -msse4.1) set(SSE41_CXXFLAGS -msse4.1)
check_cxx_source_compiles_with_flags("${SSE41_CXXFLAGS}" " check_cxx_source_compiles_with_flags("
#include <immintrin.h> #include <immintrin.h>
int main() int main()
@ -175,12 +175,13 @@ if(NOT MSVC)
return _mm_extract_epi32(r, 3); return _mm_extract_epi32(r, 3);
} }
" HAVE_SSE41 " HAVE_SSE41
CXXFLAGS ${SSE41_CXXFLAGS}
) )
set(ENABLE_SSE41 ${HAVE_SSE41}) set(ENABLE_SSE41 ${HAVE_SSE41})
# Check for AVX2 intrinsics. # Check for AVX2 intrinsics.
set(AVX2_CXXFLAGS -mavx -mavx2) set(AVX2_CXXFLAGS -mavx -mavx2)
check_cxx_source_compiles_with_flags("${AVX2_CXXFLAGS}" " check_cxx_source_compiles_with_flags("
#include <immintrin.h> #include <immintrin.h>
int main() int main()
@ -189,12 +190,13 @@ if(NOT MSVC)
return _mm256_extract_epi32(l, 7); return _mm256_extract_epi32(l, 7);
} }
" HAVE_AVX2 " HAVE_AVX2
CXXFLAGS ${AVX2_CXXFLAGS}
) )
set(ENABLE_AVX2 ${HAVE_AVX2}) set(ENABLE_AVX2 ${HAVE_AVX2})
# Check for x86 SHA-NI intrinsics. # Check for x86 SHA-NI intrinsics.
set(X86_SHANI_CXXFLAGS -msse4 -msha) set(X86_SHANI_CXXFLAGS -msse4 -msha)
check_cxx_source_compiles_with_flags("${X86_SHANI_CXXFLAGS}" " check_cxx_source_compiles_with_flags("
#include <immintrin.h> #include <immintrin.h>
int main() int main()
@ -205,12 +207,13 @@ if(NOT MSVC)
return _mm_extract_epi32(_mm_sha256rnds2_epu32(i, j, k), 0); return _mm_extract_epi32(_mm_sha256rnds2_epu32(i, j, k), 0);
} }
" HAVE_X86_SHANI " HAVE_X86_SHANI
CXXFLAGS ${X86_SHANI_CXXFLAGS}
) )
set(ENABLE_X86_SHANI ${HAVE_X86_SHANI}) set(ENABLE_X86_SHANI ${HAVE_X86_SHANI})
# Check for ARMv8 SHA-NI intrinsics. # Check for ARMv8 SHA-NI intrinsics.
set(ARM_SHANI_CXXFLAGS -march=armv8-a+crypto) set(ARM_SHANI_CXXFLAGS -march=armv8-a+crypto)
check_cxx_source_compiles_with_flags("${ARM_SHANI_CXXFLAGS}" " check_cxx_source_compiles_with_flags("
#include <arm_neon.h> #include <arm_neon.h>
int main() int main()
@ -222,6 +225,7 @@ if(NOT MSVC)
vsha256su1q_u32(a, b, c); vsha256su1q_u32(a, b, c);
} }
" HAVE_ARM_SHANI " HAVE_ARM_SHANI
CXXFLAGS ${ARM_SHANI_CXXFLAGS}
) )
set(ENABLE_ARM_SHANI ${HAVE_ARM_SHANI}) set(ENABLE_ARM_SHANI ${HAVE_ARM_SHANI})
endif() endif()

View file

@ -2,13 +2,15 @@
# Distributed under the MIT software license, see the accompanying # Distributed under the MIT software license, see the accompanying
# file COPYING or https://opensource.org/license/mit/. # file COPYING or https://opensource.org/license/mit/.
include(CheckSourceCompilesAndLinks)
# Check for clmul instructions support. # Check for clmul instructions support.
if(MSVC) if(MSVC)
set(CLMUL_CXXFLAGS) set(CLMUL_CXXFLAGS "")
else() else()
set(CLMUL_CXXFLAGS -mpclmul) set(CLMUL_CXXFLAGS -mpclmul)
endif() endif()
check_cxx_source_compiles_with_flags("${CLMUL_CXXFLAGS}" " check_cxx_source_compiles_with_flags("
#include <immintrin.h> #include <immintrin.h>
#include <cstdint> #include <cstdint>
@ -22,6 +24,7 @@ check_cxx_source_compiles_with_flags("${CLMUL_CXXFLAGS}" "
return e == 0; return e == 0;
} }
" HAVE_CLMUL " HAVE_CLMUL
CXXFLAGS ${CLMUL_CXXFLAGS}
) )
add_library(minisketch_common INTERFACE) add_library(minisketch_common INTERFACE)

View file

@ -6,13 +6,34 @@ include_guard(GLOBAL)
include(CheckCXXSourceCompiles) include(CheckCXXSourceCompiles)
include(CMakePushCheckState) include(CMakePushCheckState)
macro(check_cxx_source_compiles_with_flags flags source) #[=[
cmake_push_check_state(RESET) Check once if C++ source code can be compiled.
set(CMAKE_REQUIRED_FLAGS ${flags})
list(JOIN CMAKE_REQUIRED_FLAGS " " CMAKE_REQUIRED_FLAGS) Options:
check_cxx_source_compiles("${source}" ${ARGN})
cmake_pop_check_state() CXXFLAGS - A list of additional flags to pass to the compiler.
endmacro()
LDFLAGS - A list of additional flags to pass to the linker.
LINK_LIBRARIES - A list of libraries to add to the link command.
For historical reasons, among the CMake `CMAKE_REQUIRED_*` variables that influence
`check_cxx_source_compiles()`, only `CMAKE_REQUIRED_FLAGS` is a string rather than
a list. Additionally, `target_compile_options()` also expects a list of options.
The `check_cxx_source_compiles_with_flags()` function handles this case and accepts
`CXXFLAGS` as a list, simplifying the code at the caller site.
#]=]
function(check_cxx_source_compiles_with_flags source result_var)
cmake_parse_arguments(PARSE_ARGV 2 _ "" "" "CXXFLAGS;LDFLAGS;LINK_LIBRARIES")
list(JOIN __CXXFLAGS " " CMAKE_REQUIRED_FLAGS)
set(CMAKE_REQUIRED_LINK_OPTIONS ${__LDFLAGS})
set(CMAKE_REQUIRED_LIBRARIES ${__LINK_LIBRARIES})
include(CheckCXXSourceCompiles)
check_cxx_source_compiles("${source}" ${result_var})
set(${result_var} ${${result_var}} PARENT_SCOPE)
endfunction()
macro(check_cxx_source_links_with_libs libs source) macro(check_cxx_source_links_with_libs libs source)
cmake_push_check_state(RESET) cmake_push_check_state(RESET)