Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmake: Amend crc32c static library #171

Merged
merged 1 commit into from
May 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 24 additions & 21 deletions cmake/crc32c.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ check_cxx_source_compiles_with_flags("${SSE42_CXXFLAGS}" "
)

# Check for ARMv8 w/ CRC and CRYPTO extensions support in the compiler.
set(ARM_CRC_CXXFLAGS -march=armv8-a+crc+crypto)
check_cxx_source_compiles_with_flags("${ARM_CRC_CXXFLAGS}" "
set(ARM64_CRC_CXXFLAGS -march=armv8-a+crc+crypto)
check_cxx_source_compiles_with_flags("${ARM64_CRC_CXXFLAGS}" "
#include <arm_acle.h>
#include <arm_neon.h>

Expand All @@ -78,38 +78,41 @@ check_cxx_source_compiles_with_flags("${ARM_CRC_CXXFLAGS}" "
" HAVE_ARM64_CRC32C
)

add_library(crc32c_common INTERFACE)
target_compile_definitions(crc32c_common INTERFACE
HAVE_BUILTIN_PREFETCH=$<BOOL:${HAVE_BUILTIN_PREFETCH}>
HAVE_MM_PREFETCH=$<BOOL:${HAVE_MM_PREFETCH}>
HAVE_STRONG_GETAUXVAL=$<BOOL:${HAVE_STRONG_GETAUXVAL}>
BYTE_ORDER_BIG_ENDIAN=$<STREQUAL:${CMAKE_CXX_BYTE_ORDER},BIG_ENDIAN>
)
target_link_libraries(crc32c_common INTERFACE core_base_interface)

add_library(crc32c STATIC EXCLUDE_FROM_ALL
${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c.cc
${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c_portable.cc
)

target_compile_definitions(crc32c
PRIVATE
HAVE_BUILTIN_PREFETCH=$<BOOL:${HAVE_BUILTIN_PREFETCH}>
HAVE_MM_PREFETCH=$<BOOL:${HAVE_MM_PREFETCH}>
HAVE_STRONG_GETAUXVAL=$<BOOL:${HAVE_STRONG_GETAUXVAL}>
HAVE_SSE42=$<BOOL:${HAVE_SSE42}>
HAVE_ARM64_CRC32C=$<BOOL:${HAVE_ARM64_CRC32C}>
BYTE_ORDER_BIG_ENDIAN=$<STREQUAL:${CMAKE_CXX_BYTE_ORDER},BIG_ENDIAN>
)

target_include_directories(crc32c
PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src/crc32c/include>
)
target_link_libraries(crc32c PRIVATE crc32c_common)

if(HAVE_SSE42)
target_sources(crc32c PRIVATE ${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c_sse42.cc)
set_property(SOURCE ${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c_sse42.cc
APPEND PROPERTY COMPILE_OPTIONS ${SSE42_CXXFLAGS}
add_library(crc32c_sse42 STATIC EXCLUDE_FROM_ALL
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't these be OBJECT libraries since the static libs themselves are never useful?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it could. But CMake's developers recommend to use STATIC libraries if there is no compelling reasons for OBJECT ones. For example, from Professional CMake: A Practical Guide 18th Edition

Prefer defining static libraries over object libraries. Static libraries are simpler, have more
complete and robust support from earlier CMake versions and they are well understood by most
developers. Object libraries have their uses, but they are also less flexible than static libraries.

${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c_sse42.cc
)
target_compile_definitions(crc32c_sse42 PUBLIC HAVE_SSE42=1)
target_compile_options(crc32c_sse42 PRIVATE ${SSE42_CXXFLAGS})
target_link_libraries(crc32c_sse42 PRIVATE crc32c_common)
target_link_libraries(crc32c PRIVATE crc32c_sse42)
endif()

if(HAVE_ARM64_CRC32C)
target_sources(crc32c PRIVATE ${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c_arm64.cc)
set_property(SOURCE ${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c_arm64.cc
APPEND PROPERTY COMPILE_OPTIONS ${ARM_CRC_CXXFLAGS}
add_library(crc32c_arm64 STATIC EXCLUDE_FROM_ALL
${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c_arm64.cc
)
target_compile_definitions(crc32c_arm64 PUBLIC HAVE_ARM64_CRC32C=1)
target_compile_options(crc32c_arm64 PRIVATE ${ARM64_CRC_CXXFLAGS})
target_link_libraries(crc32c_arm64 PRIVATE crc32c_common)
target_link_libraries(crc32c PRIVATE crc32c_arm64)
endif()

target_link_libraries(crc32c PRIVATE core_base_interface)
Loading