Skip to content

GH-46487: [C++] Refactor lz4 from ExternalProject to FetchContent - #46390

Merged
jonkeane merged 8 commits into
apache:mainfrom
jonkeane:ExternalProject
May 18, 2025
Merged

jonkeane merged 8 commits into
apache:mainfrom
jonkeane:ExternalProject

Conversation

@jonkeane

@jonkeane jonkeane commented May 10, 2025 •

Copy link
Copy Markdown
Member

Rationale for this change

FetchContent is easier to maintain than ExternalProject because we don't need to create import CMake targets manually with FetchContent.

What changes are included in this PR?

Use FetchContent instead of ExternalProject for bundled LZ4.

Are these changes tested?

Yes

Are there any user-facing changes?

No

@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #45303 has been automatically assigned in GitHub to PR creator.

@kou

kou commented May 11, 2025

Copy link
Copy Markdown
Member

Can we use one PR per dependency (instead of one commit per dependency in one PR) for easy to review/merge?

FYI: We already have "Bump bundled XXX" issues:

Some of them have WIP PR such as #45306 .

@jonkeane

jonkeane commented May 11, 2025 •

Copy link
Copy Markdown
Member Author

Can we use one PR per dependency (instead of one commit per dependency in one PR) for easy to review/merge?

Yes, of course. Probably should have done that to start with. I'll repurpose this PR for zlib lz4 and then make a second one for ZSTD

@jonkeane jonkeane changed the title GH-45303: [C++] Refactor bundled dependencies from ExternalProject to FetchContent GH-45303: [C++] Refactor lz4 from ExternalProject to FetchContent May 11, 2025
@jonkeane
jonkeane marked this pull request as ready for review May 11, 2025 13:11
if(NOT DEFINED CMAKE_EXPORT_NO_PACKAGE_REGISTRY)
set(CMAKE_EXPORT_NO_PACKAGE_REGISTRY ON)
endif()
install(TARGETS lz4_static EXPORT orc_targets)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why exporting to orc_targets?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We don't need to install built LZ4 because we create libarrow_bundled_dependencies.a for bundled dependencies.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hmm, yeah let me try. When I tried before I had complaints on windows because of this, but I've removed it and we can see what CI does.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah, did you see this error?

https://github.com/apache/arrow/actions/runs/14963508416/job/42029340434?pr=46390#step:9:833

CMake Error: install(EXPORT "orc_targets" ...) includes target "orc" which requires target "lz4_static" that is not in any export set.

I'll take a look at this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How about this?

diff --git a/cpp/cmake_modules/ThirdpartyToolchain.cmake b/cpp/cmake_modules/ThirdpartyToolchain.cmake
index 6cfcdc84c0..5541721ec5 100644
--- a/cpp/cmake_modules/ThirdpartyToolchain.cmake
+++ b/cpp/cmake_modules/ThirdpartyToolchain.cmake
@@ -2678,10 +2678,11 @@ macro(build_lz4)
   fetchcontent_makeavailable(lz4_ep)
 
   # Since lz4 is now part of our build system, we can directly use the target
-  add_library(LZ4::lz4 ALIAS lz4_static)
+  add_library(LZ4::lz4 INTERFACE IMPORTED)
+  target_link_libraries(LZ4::lz4 INTERFACE lz4_static)
 
   # Add to bundled static libs
-  list(APPEND ARROW_BUNDLED_STATIC_LIBS LZ4::lz4)
+  list(APPEND ARROW_BUNDLED_STATIC_LIBS lz4_static)
 endmacro()
 
 if(ARROW_WITH_LZ4)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah, we need to update the "Since lz4 is now part of ..." comment too...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yup! And ah ok, thanks, lemme try that. I also updated the comment, but I might not be describing that in the best way, so very open to suggestions.

Comment on lines +2680 to +2688
# Get the source and binary directories after fetching content
fetchcontent_getproperties(lz4_ep
SOURCE_DIR LZ4_SOURCE_DIR
BINARY_DIR LZ4_BINARY_DIR)

# Set up the imported library for compatibility with the rest of the build system
set(LZ4_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/_deps/lz4_ep-build")
set(LZ4_INCLUDE_DIR "${LZ4_SOURCE_DIR}/lib")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we remove them?

In general, we want to use CMake targets instead of separated XXX_INCLUDE_DIR and so on variables.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yup, they are cruft I should have removed. Done now

if(NOT DEFINED CMAKE_EXPORT_NO_PACKAGE_REGISTRY)
set(CMAKE_EXPORT_NO_PACKAGE_REGISTRY ON)
endif()
install(TARGETS lz4_static EXPORT orc_targets)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We don't need to install built LZ4 because we create libarrow_bundled_dependencies.a for bundled dependencies.

Comment on lines +4745 to +4755
"-DLZ4_LIBRARY=$<TARGET_FILE:LZ4::lz4>"
"-DLZ4_STATIC_LIB=$<TARGET_FILE:LZ4::lz4>"
"-DLZ4_LIBRARY=$<LZ4_TARGET>"
"-DLZ4_STATIC_LIB=$<LZ4_TARGET>"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In general, we always use XXX_TARGET instead of XXX::xxx when we define XXX_TARGET.

In this case, we can replace all existing LZ4::lz4 with ${LZ4_TARGET} with this approach. (It's OK.)
If we don't want to replace all existing LZ4::lz4, how about the following without defining LZ4_TARGET?

diff --git a/cpp/cmake_modules/ThirdpartyToolchain.cmake b/cpp/cmake_modules/ThirdpartyToolchain.cmake
index 92ea7e346f..36aba76cfc 100644
--- a/cpp/cmake_modules/ThirdpartyToolchain.cmake
+++ b/cpp/cmake_modules/ThirdpartyToolchain.cmake
@@ -4709,7 +4709,12 @@ function(build_orc)
                         INTERFACE_INCLUDE_DIRECTORIES)
     get_filename_component(ORC_SNAPPY_ROOT "${ORC_SNAPPY_INCLUDE_DIR}" DIRECTORY)
 
-    get_target_property(ORC_LZ4_ROOT LZ4::lz4 INTERFACE_INCLUDE_DIRECTORIES)
+    if(LZ4_VENDORED)
+      set(ORC_LZ4_TARGET lz4_static)
+    else()
+      set(ORC_LZ4_TARGET LZ4::lz4)
+    endif()
+    get_target_property(ORC_LZ4_ROOT ${ORC_LZ4_TARGET} INTERFACE_INCLUDE_DIRECTORIES)
     get_filename_component(ORC_LZ4_ROOT "${ORC_LZ4_ROOT}" DIRECTORY)
 
     get_target_property(ORC_ZSTD_ROOT ${ARROW_ZSTD_LIBZSTD} INTERFACE_INCLUDE_DIRECTORIES)
@@ -4735,8 +4740,8 @@ function(build_orc)
         "-DSNAPPY_HOME=${ORC_SNAPPY_ROOT}"
         "-DSNAPPY_LIBRARY=$<TARGET_FILE:${Snappy_TARGET}>"
         "-DLZ4_HOME=${ORC_LZ4_ROOT}"
-        "-DLZ4_LIBRARY=$<TARGET_FILE:LZ4::lz4>"
-        "-DLZ4_STATIC_LIB=$<TARGET_FILE:LZ4::lz4>"
+        "-DLZ4_LIBRARY=$<TARGET_FILE:${ORC_LZ4_TARGET}>"
+        "-DLZ4_STATIC_LIB=$<TARGET_FILE:${ORC_LZ4_TARGET}>"
         "-DLZ4_INCLUDE_DIR=${ORC_LZ4_ROOT}/include"
         "-DSNAPPY_INCLUDE_DIR=${ORC_SNAPPY_INCLUDE_DIR}"
         "-DZSTD_HOME=${ORC_ZSTD_ROOT}"

build_orc is only the place that requires a real (not alias) LZ4 CMake target. We can do this special approach only here.

Comment on lines +4754 to +4755
"-DLZ4_LIBRARY=$<LZ4_TARGET>"
"-DLZ4_STATIC_LIB=$<LZ4_TARGET>"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

BTW, do we need TARGET_FILE:${...}?

Suggested change
"-DLZ4_LIBRARY=$<LZ4_TARGET>"
"-DLZ4_STATIC_LIB=$<LZ4_TARGET>"
"-DLZ4_LIBRARY=$<TARGET_FILE:${LZ4_TARGET}>"
"-DLZ4_STATIC_LIB=$<TARGET_FILE:${LZ4_TARGET}>"

@jonkeane

Copy link
Copy Markdown
Member Author

@github-actions crossbow submit test-ubuntu-24.04-cpp-gcc-13-bundled

@github-actions

Copy link
Copy Markdown

Revision: ae2e181

Submitted crossbow builds: ursacomputing/crossbow @ actions-848cd401e8

Task Status
test-ubuntu-24.04-cpp-gcc-13-bundled GitHub Actions

@kou

kou commented May 15, 2025

Copy link
Copy Markdown
Member

@github-actions crossbow submit test-ubuntu-24.04-cpp-gcc-13-bundled

@github-actions

Copy link
Copy Markdown

Revision: a9c8fbd

Submitted crossbow builds: ursacomputing/crossbow @ actions-7cb3fee6e0

Task Status
test-ubuntu-24.04-cpp-gcc-13-bundled GitHub Actions

@jonkeane

Copy link
Copy Markdown
Member Author

@github-actions crossbow submit -g cpp -g r

@github-actions

Copy link
Copy Markdown

Revision: a9c8fbd

Submitted crossbow builds: ursacomputing/crossbow @ actions-8fcd638307

Task Status
example-cpp-minimal-build-static GitHub Actions
example-cpp-minimal-build-static-system-dependency GitHub Actions
example-cpp-tutorial GitHub Actions
r-binary-packages GitHub Actions
r-recheck-most GitHub Actions
test-alpine-linux-cpp GitHub Actions
test-build-cpp-fuzz GitHub Actions
test-conda-cpp GitHub Actions
test-conda-cpp-meson GitHub Actions
test-conda-cpp-valgrind GitHub Actions
test-cuda-cpp-ubuntu-22.04-cuda-11.7.1 GitHub Actions
test-debian-12-cpp-amd64 GitHub Actions
test-debian-12-cpp-i386 GitHub Actions
test-fedora-39-cpp GitHub Actions
test-r-arrow-backwards-compatibility GitHub Actions
test-r-clang-asan GitHub Actions
test-r-clang-ubsan GitHub Actions
test-r-depsource-bundled Azure
test-r-depsource-system GitHub Actions
test-r-dev-duckdb GitHub Actions
test-r-devdocs GitHub Actions
test-r-extra-packages GitHub Actions
test-r-gcc-11 GitHub Actions
test-r-gcc-12 GitHub Actions
test-r-install-local GitHub Actions
test-r-install-local-minsizerel GitHub Actions
test-r-linux-as-cran GitHub Actions
test-r-linux-rchk GitHub Actions
test-r-linux-sanitizer GitHub Actions
test-r-linux-valgrind GitHub Actions
test-r-m1-san GitHub Actions
test-r-macos-as-cran GitHub Actions
test-r-minimal-build Azure
test-r-offline-maximal GitHub Actions
test-r-offline-minimal Azure
test-r-rhub-debian-gcc-devel-lto-latest Azure
test-r-rhub-debian-gcc-release-custom-ccache Azure
test-r-rhub-ubuntu-release-latest Azure
test-r-rocker-r-ver-latest Azure
test-r-rstudio-r-base-4.1-opensuse155 Azure
test-r-rstudio-r-base-4.2-focal Azure
test-r-ubuntu-22.04 GitHub Actions
test-r-versions GitHub Actions
test-ubuntu-22.04-cpp GitHub Actions
test-ubuntu-22.04-cpp-20 GitHub Actions
test-ubuntu-22.04-cpp-bundled GitHub Actions
test-ubuntu-22.04-cpp-emscripten GitHub Actions
test-ubuntu-22.04-cpp-no-threading GitHub Actions
test-ubuntu-24.04-cpp GitHub Actions
test-ubuntu-24.04-cpp-bundled-offline GitHub Actions
test-ubuntu-24.04-cpp-gcc-13-bundled GitHub Actions
test-ubuntu-24.04-cpp-gcc-14 GitHub Actions
test-ubuntu-24.04-cpp-minimal-with-formats GitHub Actions
test-ubuntu-24.04-cpp-thread-sanitizer GitHub Actions

@jonkeane

Copy link
Copy Markdown
Member Author

The two CI failures are not related to this PR and are tracked at #46343 and #46435

@kou kou changed the title GH-45303: [C++] Refactor lz4 from ExternalProject to FetchContent GH-46487: [C++] Refactor lz4 from ExternalProject to FetchContent May 18, 2025
@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #46487 has been automatically assigned in GitHub to PR creator.

@kou kou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

+1

@conbench-apache-arrow

Copy link
Copy Markdown

After merging your PR, Conbench analyzed the 4 benchmarking runs that have been run so far on merge-commit f4d52af.

There were no benchmark performance regressions. 🎉

The full Conbench report has more details. It also includes information about 2 possible false positives for unstable benchmarks that are known to sometimes produce them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants