Skip to content

Commit 773848a

Browse files
raulcdkou
andauthored
GH-48315: [C++] Use FetchContent for bundled CRC32C (#48318)
### Rationale for this change As a follow up of requiring a minimum CMake version >= 3.25 we discussed moving our dependencies from ExternalProject to FetchContent. This can simplify our third party dependency management. ### What changes are included in this PR? The general change is moving CRC32C from `ExternalProject` to `FetchContent`. We will be able to clean up installation once google-cloud-cpp is also migated. ### Are these changes tested? Yes, the changes are tested locally and on CI. ### Are there any user-facing changes? No * GitHub Issue: #48315 Lead-authored-by: Raúl Cumplido <[email protected]> Co-authored-by: Sutou Kouhei <[email protected]> Signed-off-by: Sutou Kouhei <[email protected]>
1 parent 156d0be commit 773848a

File tree

1 file changed

+71
-34
lines changed

1 file changed

+71
-34
lines changed

cpp/cmake_modules/ThirdpartyToolchain.cmake

Lines changed: 71 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3391,42 +3391,79 @@ endif()
33913391
# ----------------------------------------------------------------------
33923392
# GCS and dependencies
33933393

3394-
macro(build_crc32c_once)
3395-
if(NOT TARGET crc32c_ep)
3396-
message(STATUS "Building crc32c from source")
3397-
# Build crc32c
3398-
set(CRC32C_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/crc32c_ep-install")
3399-
set(CRC32C_INCLUDE_DIR "${CRC32C_PREFIX}/include")
3400-
set(CRC32C_CMAKE_ARGS
3401-
${EP_COMMON_CMAKE_ARGS}
3402-
"-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>"
3403-
-DCRC32C_BUILD_TESTS=OFF
3404-
-DCRC32C_BUILD_BENCHMARKS=OFF
3405-
-DCRC32C_USE_GLOG=OFF)
3394+
function(build_crc32c_once)
3395+
list(APPEND CMAKE_MESSAGE_INDENT "CRC32C: ")
3396+
message(STATUS "Building CRC32C from source using FetchContent")
3397+
set(CRC32C_VENDORED
3398+
TRUE
3399+
PARENT_SCOPE)
3400+
set(CRC32C_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/crc32c_fc-install")
3401+
set(CRC32C_PREFIX
3402+
"${CRC32C_PREFIX}"
3403+
PARENT_SCOPE)
34063404

3407-
set(_CRC32C_STATIC_LIBRARY
3408-
"${CRC32C_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}crc32c${CMAKE_STATIC_LIBRARY_SUFFIX}"
3409-
)
3410-
set(CRC32C_BUILD_BYPRODUCTS ${_CRC32C_STATIC_LIBRARY})
3411-
set(CRC32C_LIBRARIES crc32c)
3405+
fetchcontent_declare(crc32c
3406+
${FC_DECLARE_COMMON_OPTIONS}
3407+
URL ${CRC32C_SOURCE_URL}
3408+
URL_HASH "SHA256=${ARROW_CRC32C_BUILD_SHA256_CHECKSUM}")
34123409

3413-
externalproject_add(crc32c_ep
3414-
${EP_COMMON_OPTIONS}
3415-
INSTALL_DIR ${CRC32C_PREFIX}
3416-
URL ${CRC32C_SOURCE_URL}
3417-
URL_HASH "SHA256=${ARROW_CRC32C_BUILD_SHA256_CHECKSUM}"
3418-
CMAKE_ARGS ${CRC32C_CMAKE_ARGS}
3419-
BUILD_BYPRODUCTS ${CRC32C_BUILD_BYPRODUCTS})
3420-
# Work around https://gitlab.kitware.com/cmake/cmake/issues/15052
3421-
file(MAKE_DIRECTORY "${CRC32C_INCLUDE_DIR}")
3422-
add_library(Crc32c::crc32c STATIC IMPORTED)
3423-
set_target_properties(Crc32c::crc32c PROPERTIES IMPORTED_LOCATION
3424-
${_CRC32C_STATIC_LIBRARY})
3425-
target_include_directories(Crc32c::crc32c BEFORE INTERFACE "${CRC32C_INCLUDE_DIR}")
3426-
add_dependencies(Crc32c::crc32c crc32c_ep)
3427-
list(APPEND ARROW_BUNDLED_STATIC_LIBS Crc32c::crc32c)
3410+
prepare_fetchcontent()
3411+
3412+
set(CRC32C_BUILD_TESTS OFF)
3413+
set(CRC32C_BUILD_BENCHMARKS OFF)
3414+
set(CRC32C_USE_GLOG OFF)
3415+
set(CRC32C_INSTALL ON)
3416+
fetchcontent_makeavailable(crc32c)
3417+
3418+
# Create alias target for consistency (crc32c exports as Crc32c::crc32c when installed)
3419+
if(NOT TARGET Crc32c::crc32c)
3420+
add_library(Crc32c::crc32c ALIAS crc32c)
34283421
endif()
3429-
endmacro()
3422+
3423+
# google-cloud-cpp requires crc32c to be installed to a known location.
3424+
# We have to do this in two steps to avoid double installation of crc32c
3425+
# when Arrow is installed.
3426+
# This custom target ensures crc32c is built before we install.
3427+
add_custom_target(crc32c_built DEPENDS Crc32c::crc32c)
3428+
3429+
# Disable crc32c's install script after it's built to prevent double installation.
3430+
add_custom_command(OUTPUT "${crc32c_BINARY_DIR}/cmake_install.cmake.saved"
3431+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
3432+
"${crc32c_BINARY_DIR}/cmake_install.cmake"
3433+
"${crc32c_BINARY_DIR}/cmake_install.cmake.saved"
3434+
COMMAND ${CMAKE_COMMAND} -E echo
3435+
"# crc32c install disabled to prevent double installation with Arrow"
3436+
> "${crc32c_BINARY_DIR}/cmake_install.cmake"
3437+
DEPENDS crc32c_built
3438+
COMMENT "Disabling crc32c install to prevent double installation"
3439+
VERBATIM)
3440+
3441+
add_custom_target(crc32c_install_disabled ALL
3442+
DEPENDS "${crc32c_BINARY_DIR}/cmake_install.cmake.saved")
3443+
3444+
# Install crc32c to CRC32C_PREFIX for google-cloud-cpp to find.
3445+
add_custom_command(OUTPUT "${CRC32C_PREFIX}/.crc32c_installed"
3446+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
3447+
"${crc32c_BINARY_DIR}/cmake_install.cmake.saved"
3448+
"${crc32c_BINARY_DIR}/cmake_install.cmake.tmp"
3449+
COMMAND ${CMAKE_COMMAND} -DCMAKE_INSTALL_PREFIX=${CRC32C_PREFIX}
3450+
-DCMAKE_INSTALL_CONFIG_NAME=$<CONFIG> -P
3451+
"${crc32c_BINARY_DIR}/cmake_install.cmake.tmp" ||
3452+
${CMAKE_COMMAND} -E true
3453+
COMMAND ${CMAKE_COMMAND} -E touch
3454+
"${CRC32C_PREFIX}/.crc32c_installed"
3455+
DEPENDS crc32c_install_disabled
3456+
COMMENT "Installing crc32c to ${CRC32C_PREFIX} for google-cloud-cpp"
3457+
VERBATIM)
3458+
3459+
# Make crc32c_fc depend on the install completion marker.
3460+
add_custom_target(crc32c_fc DEPENDS "${CRC32C_PREFIX}/.crc32c_installed")
3461+
3462+
set(ARROW_BUNDLED_STATIC_LIBS
3463+
${ARROW_BUNDLED_STATIC_LIBS} Crc32c::crc32c
3464+
PARENT_SCOPE)
3465+
list(POP_BACK CMAKE_MESSAGE_INDENT)
3466+
endfunction()
34303467

34313468
macro(build_nlohmann_json)
34323469
message(STATUS "Building nlohmann-json from source")
@@ -3521,7 +3558,7 @@ macro(build_google_cloud_cpp_storage)
35213558
if(ZLIB_VENDORED)
35223559
add_dependencies(google_cloud_cpp_dependencies zlib_ep)
35233560
endif()
3524-
add_dependencies(google_cloud_cpp_dependencies crc32c_ep)
3561+
add_dependencies(google_cloud_cpp_dependencies crc32c_fc)
35253562
add_dependencies(google_cloud_cpp_dependencies nlohmann_json::nlohmann_json)
35263563

35273564
set(GOOGLE_CLOUD_CPP_STATIC_LIBRARY_STORAGE

0 commit comments

Comments
 (0)