From 224f55745b0e17a57853d5404b54b669a5f03926 Mon Sep 17 00:00:00 2001 From: matthew brian white Date: Thu, 19 Nov 2026 12:55:35 +0000 Subject: [PATCH 01/11] build: support Fedora and add CMake presets and installable package - Add scripts/setup-fedora.sh and a Fedora container CI job. - Add CMakePresets.json; Makefile and CI now use the presets. - Use GNUInstallDirs so the CMake package installs to lib64 where needed. - Require Protobuf >= 5.28 from the system, otherwise fetch it. - Fetch Abseil 20240722.1 separately; the Abseil bundled with Protobuf v29.3 does not compile with GCC 14 and later. - Install tzdata and tzdata-legacy in the Ubuntu setup script, and tzdata in the Fedora one; timezone tests need the legacy zone names. - Verify the ANTLR jar download against a pinned SHA-256. - Install SubstraitConfig.cmake and a version file, export targets as Substrait::, and install the ANTLR4 runtime so consumers can use find_package(Substrait CONFIG). - Document setup scripts, presets and package usage in the README. Signed-off-by: matthew brian white --- .github/workflows/build_test.yml | 40 +++++++++++++++---- CMakeLists.txt | 45 +++++++++++++++++++-- CMakePresets.json | 62 +++++++++++++++++++++++++++++ Makefile | 27 +++---------- README.md | 68 ++++++++++++++++++++++++++++++-- cmake/SubstraitConfig.cmake.in | 27 +++++++++++++ docker/README.md | 4 +- scripts/setup-fedora.sh | 35 ++++++++++++++++ scripts/setup-ubuntu.sh | 7 +++- third_party/CMakeLists.txt | 2 +- third_party/protobuf.cmake | 29 +++++++++++++- 11 files changed, 304 insertions(+), 42 deletions(-) create mode 100644 CMakePresets.json create mode 100644 cmake/SubstraitConfig.cmake.in create mode 100755 scripts/setup-fedora.sh diff --git a/.github/workflows/build_test.yml b/.github/workflows/build_test.yml index 0c780d3f..28fabee3 100644 --- a/.github/workflows/build_test.yml +++ b/.github/workflows/build_test.yml @@ -55,18 +55,44 @@ jobs: - name: Configure CMake run: | cmake --version - cmake -B build -G Ninja \ - -DCMAKE_BUILD_TYPE=Debug \ - -DBUILD_TZ_LIB=ON \ - -DCMAKE_C_COMPILER_LAUNCHER=ccache \ - -DCMAKE_CXX_COMPILER_LAUNCHER=ccache + cmake --preset ci - name: Build + run: cmake --build --preset ci + + - name: Test + run: ctest --preset ci + + fedora-build-and-test: + runs-on: ubuntu-latest + container: fedora:latest + + steps: + - name: Install git (needed for submodule checkout) + run: dnf install -y git + + - uses: actions/checkout@v4 + with: + submodules: recursive + + - name: Setup Fedora + run: ./scripts/setup-fedora.sh + + - name: Set up ccache + uses: hendrikmuhs/ccache-action@1bbbcda0748b3e340dee71a314fa68ffcbd6df79 # v1.2.21 + with: + key: fedora + + - name: Configure CMake run: | - cmake --build build + cmake --version + cmake --preset ci + + - name: Build + run: cmake --build --preset ci - name: Test - run: ctest --test-dir build --output-on-failure --timeout 30 + run: ctest --preset ci windows-build-and-test: runs-on: windows-latest diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a6820d6..c468d7d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,12 @@ cmake_minimum_required(VERSION 3.24) # set the project name -project(substrait-cpp) +project( + substrait-cpp + VERSION 0.1.0 + LANGUAGES C CXX) + +include(GNUInstallDirs) message(STATUS "Build type: ${CMAKE_BUILD_TYPE}") @@ -48,8 +53,13 @@ set(ANTLR4_TAG df4d68c09cdef73e023b8838a8bc7ca4dff1d1de) include(ExternalAntlr4Cpp) include_directories(${ANTLR4_INCLUDE_DIRS}) set(ANTLR_EXECUTABLE_DIR ${CMAKE_CURRENT_BINARY_DIR}) -file(DOWNLOAD https://www.antlr.org/download/antlr-4.13.2-complete.jar - "${ANTLR_EXECUTABLE_DIR}/antlr.jar") +file( + DOWNLOAD + https://www.antlr.org/download/antlr-4.13.2-complete.jar + "${ANTLR_EXECUTABLE_DIR}/antlr.jar" + EXPECTED_HASH + SHA256=eae2dfa119a64327444672aff63e9ec35a20180dc5b8090b7a6ab85125df4d76 + TLS_VERIFY ON) set(ANTLR_EXECUTABLE "${ANTLR_EXECUTABLE_DIR}/antlr.jar") # Local files come first. @@ -68,7 +78,34 @@ if(${SUBSTRAIT_CPP_BUILD_TESTING}) enable_testing() endif() -install(EXPORT SubstraitTargets DESTINATION lib/cmake/Substrait) +# Install the exported targets together with a package configuration file, so +# that consumers can use `find_package(Substrait CONFIG REQUIRED)` and link +# against `Substrait::`. +include(CMakePackageConfigHelpers) +set(SUBSTRAIT_CPP_CMAKE_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/Substrait) + +install( + EXPORT SubstraitTargets + NAMESPACE Substrait:: + DESTINATION ${SUBSTRAIT_CPP_CMAKE_INSTALL_DIR}) + +# The ANTLR4 runtime is built by ExternalProject and is not part of any export +# set; install it so that the exported static libraries can be linked. +get_filename_component(SUBSTRAIT_CPP_ANTLR4_LIBRARY_FILE + "${ANTLR4_STATIC_LIBRARIES}" NAME) +install(FILES ${ANTLR4_STATIC_LIBRARIES} DESTINATION ${CMAKE_INSTALL_LIBDIR}) + +configure_package_config_file( + cmake/SubstraitConfig.cmake.in + "${CMAKE_CURRENT_BINARY_DIR}/SubstraitConfig.cmake" + INSTALL_DESTINATION ${SUBSTRAIT_CPP_CMAKE_INSTALL_DIR}) +write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/SubstraitConfigVersion.cmake" + VERSION ${PROJECT_VERSION} + COMPATIBILITY SameMajorVersion) +install(FILES "${CMAKE_CURRENT_BINARY_DIR}/SubstraitConfig.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/SubstraitConfigVersion.cmake" + DESTINATION ${SUBSTRAIT_CPP_CMAKE_INSTALL_DIR}) add_subdirectory(src/substrait) add_subdirectory(export) diff --git a/CMakePresets.json b/CMakePresets.json new file mode 100644 index 00000000..651eccb8 --- /dev/null +++ b/CMakePresets.json @@ -0,0 +1,62 @@ +{ + "version": 5, + "cmakeMinimumRequired": { "major": 3, "minor": 24, "patch": 0 }, + "configurePresets": [ + { + "name": "base", + "hidden": true, + "generator": "Ninja", + "binaryDir": "${sourceDir}/build-${presetName}", + "cacheVariables": { + "CMAKE_EXPORT_COMPILE_COMMANDS": "ON" + } + }, + { + "name": "debug", + "displayName": "Debug", + "inherits": "base", + "cacheVariables": { "CMAKE_BUILD_TYPE": "Debug" } + }, + { + "name": "release", + "displayName": "Release", + "inherits": "base", + "cacheVariables": { "CMAKE_BUILD_TYPE": "Release" } + }, + { + "name": "asan", + "displayName": "Debug with ASan/UBSan", + "inherits": "debug", + "cacheVariables": { "SUBSTRAIT_CPP_SANITIZE_DEBUG_BUILD": "ON" } + }, + { + "name": "ci", + "displayName": "CI (Debug, ccache)", + "inherits": "debug", + "binaryDir": "${sourceDir}/build", + "cacheVariables": { + "BUILD_TZ_LIB": "ON", + "CMAKE_C_COMPILER_LAUNCHER": "ccache", + "CMAKE_CXX_COMPILER_LAUNCHER": "ccache" + } + } + ], + "buildPresets": [ + { "name": "debug", "configurePreset": "debug" }, + { "name": "release", "configurePreset": "release" }, + { "name": "asan", "configurePreset": "asan" }, + { "name": "ci", "configurePreset": "ci" } + ], + "testPresets": [ + { + "name": "default", + "hidden": true, + "output": { "outputOnFailure": true }, + "execution": { "timeout": 30 } + }, + { "name": "debug", "inherits": "default", "configurePreset": "debug" }, + { "name": "release", "inherits": "default", "configurePreset": "release" }, + { "name": "asan", "inherits": "default", "configurePreset": "asan" }, + { "name": "ci", "inherits": "default", "configurePreset": "ci" } + ] +} diff --git a/Makefile b/Makefile index 1742629b..44a300d6 100644 --- a/Makefile +++ b/Makefile @@ -1,36 +1,19 @@ # SPDX-License-Identifier: Apache-2.0 -.PHONY: all clean build debug release +# Build configurations live in CMakePresets.json. -BUILD_TYPE := Release +.PHONY: all clean debug release asan format all: debug clean: @rm -rf build-* -build-common: - @mkdir -p build-${BUILD_TYPE} - @cd build-${BUILD_TYPE} && \ - cmake -Wno-dev \ - -DCMAKE_BUILD_TYPE=${BUILD_TYPE} \ - -DPREFER_STATIC_LIBS=OFF \ - $(FORCE_COLOR) \ - .. - -build: - VERBOSE=1 cmake --build build-${BUILD_TYPE} -j $${CPU_COUNT:-`nproc`} || \ - cmake --build build-${BUILD_TYPE} +debug release asan: + cmake --preset $@ -Wno-dev $(FORCE_COLOR) + cmake --build --preset $@ -j $${CPU_COUNT:-`nproc`} format: set -f bash scripts/run-cmake-format.sh && bash scripts/run-clang-format.sh set +f - -debug: - @$(MAKE) build-common BUILD_TYPE=Debug - @$(MAKE) build BUILD_TYPE=Debug - -release: - @$(MAKE) build-common BUILD_TYPE=Release - @$(MAKE) build BUILD_TYPE=Release diff --git a/README.md b/README.md index 61433ba6..25982bc0 100644 --- a/README.md +++ b/README.md @@ -16,15 +16,77 @@ git submodule sync --recursive git submodule update --init --recursive ``` -### Setting up on Linux (Ubuntu 20.04 or later) +### Setting up on Linux -Once you have checked out substrait-cpp, you can set up and build like so: +Once you have checked out substrait-cpp, install the build tools and build. +Run the setup script from the repository root. It installs system packages, so +it needs `sudo` (you will be prompted for your password unless you are root). + +| Distribution | Setup script | Package manager | +|---|---|---| +| Ubuntu (20.04 or later) | `./scripts/setup-ubuntu.sh` | `apt` | +| Fedora | `./scripts/setup-fedora.sh` | `dnf` | + +What the scripts install: a C++ compiler, CMake, Ninja, ccache, git, `tzdata`, +libuuid and libcurl development headers, a Java runtime (needed at build time by +ANTLR), `clang-format`/`clang-tidy` and `cmake-format`. The Fedora script also +installs GoogleTest from the distribution. + +Ubuntu: + +```shell +$ ./scripts/setup-ubuntu.sh +$ make +``` + +Fedora: ```shell -$ ./scripts/setup-ubuntu.sh +$ ./scripts/setup-fedora.sh $ make ``` +`make` builds the `debug` configuration. `make release` and `make asan` build +the other configurations. Builds use [CMake presets](CMakePresets.json) +(`debug`, `release`, `asan`, and `ci`, which also uses ccache): + +```shell +$ cmake --preset debug +$ cmake --build --preset debug +$ ctest --preset debug +``` + +The first build takes several minutes because Protobuf and Abseil are +downloaded and compiled. If the system Protobuf is older than +`SUBSTRAIT_CPP_PROTOBUF_MIN_VERSION` (5.28), a suitable version is fetched +automatically. The Fedora script does not install Protobuf; the Ubuntu script +only installs a `protoc` binary into `~/.local`. + +Other dnf-based distributions can usually use the Fedora script. Without root +access, run the script inside a container (for example `fedora:latest` or +`ubuntu:24.04`) with the repository copied in. + +### Using substrait-cpp from another CMake project + +Install it, then find it with `find_package`: + +```shell +$ cmake --install build-debug --prefix /path/to/prefix +``` + +```cmake +find_package(Substrait 0.1 CONFIG REQUIRED) +target_link_libraries(my_app PRIVATE Substrait::substrait_io) +``` + +Configure your project with `-DCMAKE_PREFIX_PATH=/path/to/prefix`. The install +includes Protobuf, Abseil, fmt, yaml-cpp, date and the ANTLR4 runtime, which the +package config locates for you. The library directory is `lib64` on Fedora and +`lib` on Ubuntu. + +The ANTLR parser jar is downloaded at configure time and verified against a +pinned SHA-256 hash, so a changed or corrupted download fails the configure step. + ### Coding style Basically the coding style is based on Google C++ Style, but there are some naming style changed: - Function case style change to 'camelBack' diff --git a/cmake/SubstraitConfig.cmake.in b/cmake/SubstraitConfig.cmake.in new file mode 100644 index 00000000..3e9f61dc --- /dev/null +++ b/cmake/SubstraitConfig.cmake.in @@ -0,0 +1,27 @@ +@PACKAGE_INIT@ + +include(CMakeFindDependencyMacro) + +# Dependencies that appear in the interface of the exported targets. They are +# either installed alongside this package or provided by the system. +find_dependency(Protobuf CONFIG) +find_dependency(absl CONFIG) +find_dependency(fmt CONFIG) +find_dependency(yaml-cpp CONFIG) +find_dependency(date CONFIG) + +# The ANTLR4 runtime is built with ExternalProject and installed by this +# package as a plain static library, so it has to be re-declared here. +if(NOT TARGET antlr4_static) + add_library(antlr4_static STATIC IMPORTED) + set_target_properties( + antlr4_static + PROPERTIES + IMPORTED_LOCATION + "${PACKAGE_PREFIX_DIR}/@CMAKE_INSTALL_LIBDIR@/@SUBSTRAIT_CPP_ANTLR4_LIBRARY_FILE@" + ) +endif() + +include("${CMAKE_CURRENT_LIST_DIR}/SubstraitTargets.cmake") + +check_required_components(Substrait) diff --git a/docker/README.md b/docker/README.md index 0f30717f..d250585e 100644 --- a/docker/README.md +++ b/docker/README.md @@ -14,8 +14,8 @@ docker run -it substrait-cpp ## Evaluate -Run function tests +Run tests ```bash -./build-Debug/substrait/function/tests/substrait_function_test +cd substrait-cpp && ctest --preset debug ``` diff --git a/scripts/setup-fedora.sh b/scripts/setup-fedora.sh new file mode 100755 index 00000000..45873610 --- /dev/null +++ b/scripts/setup-fedora.sh @@ -0,0 +1,35 @@ +#!/bin/bash +# SPDX-License-Identifier: Apache-2.0 + +# Minimal setup for Fedora (and other dnf-based distributions). +# +# Protobuf is intentionally not installed from the distribution: if the system +# version is older than SUBSTRAIT_CPP_PROTOBUF_MIN_VERSION the build fetches and +# builds a suitable one with FetchContent. +set -eufx -o pipefail + +# Use sudo only when not already root (e.g. inside a container). +SUDO="" +if [ "$(id -u)" -ne 0 ]; then + SUDO="sudo" +fi + +$SUDO dnf install -y \ + gcc-c++ \ + cmake \ + ninja-build \ + ccache \ + git \ + make \ + unzip \ + tzdata \ + libuuid-devel \ + libcurl-devel \ + gtest-devel \ + gmock-devel \ + java-latest-openjdk-headless \ + clang-tools-extra \ + pipx + +# The `cmake-format` PyPI package is a shim; the tool ships in `cmakelang`. +pipx install cmakelang diff --git a/scripts/setup-ubuntu.sh b/scripts/setup-ubuntu.sh index a44281b8..ccdec8ed 100755 --- a/scripts/setup-ubuntu.sh +++ b/scripts/setup-ubuntu.sh @@ -21,7 +21,12 @@ sudo --preserve-env apt install -y \ clang-format-15 \ uuid-dev \ default-jre \ - libcurl4-openssl-dev + libcurl4-openssl-dev \ + tzdata + +# Legacy timezone names such as US/Central moved to a separate package in +# Ubuntu 24.04 and later; the package does not exist on older releases. +sudo apt install -y tzdata-legacy || true # Install the currently supported version of protobuf: PB_REL="https://github.com/protocolbuffers/protobuf/releases" diff --git a/third_party/CMakeLists.txt b/third_party/CMakeLists.txt index 8489a8a0..d678880b 100644 --- a/third_party/CMakeLists.txt +++ b/third_party/CMakeLists.txt @@ -3,7 +3,7 @@ # Ensure `option()` in subdirectories honors normal variables set here. set(CMAKE_POLICY_DEFAULT_CMP0077 NEW) -set(SUBSTRAIT_CPP_ABSL_DEFAULT_FETCH_TAG "20240116.2") +set(SUBSTRAIT_CPP_ABSL_DEFAULT_FETCH_TAG "20240722.1") set(SUBSTRAIT_CPP_PROTOBUF_DEFAULT_FETCH_TAG "v29.3") set(SUBSTRAIT_CPP_GTEST_DEFAULT_FETCH_TAG "v1.14.0") diff --git a/third_party/protobuf.cmake b/third_party/protobuf.cmake index 6eaf60ae..ad3b9bfd 100644 --- a/third_party/protobuf.cmake +++ b/third_party/protobuf.cmake @@ -8,17 +8,26 @@ option(SUBSTRAIT_CPP_FIND_PROTOBUF_CONFIG "Use CONFIG mode to find system Protobuf via find_package" ON) option(SUBSTRAIT_CPP_FETCH_PROTOBUF "Download Protobuf via FetchContent if not found" ON) +set(SUBSTRAIT_CPP_ABSL_FETCH_TAG + ${SUBSTRAIT_CPP_ABSL_DEFAULT_FETCH_TAG} + CACHE STRING "Git tag or commit to use for Abseil FetchContent") set(SUBSTRAIT_CPP_PROTOBUF_FETCH_TAG ${SUBSTRAIT_CPP_PROTOBUF_DEFAULT_FETCH_TAG} CACHE STRING "Git tag or commit to use for Protobuf FetchContent") +# Protobuf's CMake package version is 5.. for the v. +# releases (e.g. v28.2 is 5.28.2). Older system packages (e.g. 3.19 shipped by +# some distributions) are rejected so that the FetchContent fallback is used. +set(SUBSTRAIT_CPP_PROTOBUF_MIN_VERSION + 5.28 + CACHE STRING "Minimum acceptable version of a system Protobuf") # First use `find_package`. This allows downstream projects to inject their # version with `FetchContent_Declare(... OVERRIDE_FIND_PACKAGE`. if(SUBSTRAIT_CPP_USE_SYSTEM_PROTOBUF) if(SUBSTRAIT_CPP_FIND_PROTOBUF_CONFIG) - find_package(Protobuf CONFIG) + find_package(Protobuf ${SUBSTRAIT_CPP_PROTOBUF_MIN_VERSION} CONFIG) else() - find_package(Protobuf) + find_package(Protobuf ${SUBSTRAIT_CPP_PROTOBUF_MIN_VERSION}) endif() endif() @@ -41,6 +50,22 @@ if(NOT Protobuf_FOUND AND SUBSTRAIT_CPP_FETCH_PROTOBUF) OFF CACHE INTERNAL "") set(ABSL_PROPAGATE_CXX_STD ON) + # The Abseil bundled with older Protobuf releases does not compile with recent + # compilers (e.g. GCC 14+ on Fedora), so fetch a newer Abseil separately and + # tell Protobuf to use it via find_package. + message(STATUS "Fetching Abseil version ${SUBSTRAIT_CPP_ABSL_FETCH_TAG}") + fetchcontent_declare( + absl + GIT_REPOSITORY https://github.com/abseil/abseil-cpp.git + GIT_TAG ${SUBSTRAIT_CPP_ABSL_FETCH_TAG} + SYSTEM OVERRIDE_FIND_PACKAGE) + set(protobuf_ABSL_PROVIDER + "package" + CACHE STRING "") + # Needed so the Abseil targets are in an export set for install(EXPORT). + set(ABSL_ENABLE_INSTALL + ON + CACHE BOOL "") if(MSVC) set(protobuf_MSVC_STATIC_RUNTIME OFF) add_compile_options("/W0") From 092ce5a3051fb199e14a21119e19df9b7b6ed43c Mon Sep 17 00:00:00 2001 From: matthew brian white Date: Thu, 19 Nov 2026 14:22:47 +0000 Subject: [PATCH 02/11] ci: pin Ubuntu jobs to 24.04 for clang-format-15 availability --- .github/workflows/build_test.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build_test.yml b/.github/workflows/build_test.yml index 28fabee3..292cf3b6 100644 --- a/.github/workflows/build_test.yml +++ b/.github/workflows/build_test.yml @@ -9,7 +9,9 @@ on: jobs: check: - runs-on: ubuntu-latest + # Pinned: clang-format-15 (used by setup-ubuntu.sh) is not available on + # ubuntu-latest (26.04). + runs-on: ubuntu-24.04 steps: - uses: actions/checkout@v3 @@ -27,7 +29,7 @@ jobs: run: git diff --exit-code ubuntu-build-and-test: - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 steps: - uses: actions/checkout@v3 From 739ffdc6e53fc032cde287bd913e00a3e99ee264 Mon Sep 17 00:00:00 2001 From: matthew brian white Date: Thu, 19 Nov 2026 14:25:20 +0000 Subject: [PATCH 03/11] build: add license header to SubstraitConfig.cmake.in --- cmake/SubstraitConfig.cmake.in | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmake/SubstraitConfig.cmake.in b/cmake/SubstraitConfig.cmake.in index 3e9f61dc..d7a4e7b3 100644 --- a/cmake/SubstraitConfig.cmake.in +++ b/cmake/SubstraitConfig.cmake.in @@ -1,3 +1,5 @@ +# SPDX-License-Identifier: Apache-2.0 + @PACKAGE_INIT@ include(CMakeFindDependencyMacro) From 052cc0eb3543bd6de3a317e726484accea085015 Mon Sep 17 00:00:00 2001 From: matthew brian white Date: Thu, 19 Nov 2026 14:39:08 +0000 Subject: [PATCH 04/11] build: bump fmt to 10.2.1 for MSVC 14.51 compatibility --- third_party/fmt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/third_party/fmt b/third_party/fmt index 05e3a923..e69e5f97 160000 --- a/third_party/fmt +++ b/third_party/fmt @@ -1 +1 @@ -Subproject commit 05e3a9233ac1dd0c2d9643e046dbb5f788c39f61 +Subproject commit e69e5f977d458f2650bb346dadf2ad30c5320281 From 5ff41d4e9bb9c045b11050a9d04e0850a2be7bc7 Mon Sep 17 00:00:00 2001 From: matthew brian white Date: Thu, 19 Nov 2026 14:49:35 +0000 Subject: [PATCH 05/11] style: cmake-format ANTLR download --- CMakeLists.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c468d7d5..af88c27f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,8 +54,7 @@ include(ExternalAntlr4Cpp) include_directories(${ANTLR4_INCLUDE_DIRS}) set(ANTLR_EXECUTABLE_DIR ${CMAKE_CURRENT_BINARY_DIR}) file( - DOWNLOAD - https://www.antlr.org/download/antlr-4.13.2-complete.jar + DOWNLOAD https://www.antlr.org/download/antlr-4.13.2-complete.jar "${ANTLR_EXECUTABLE_DIR}/antlr.jar" EXPECTED_HASH SHA256=eae2dfa119a64327444672aff63e9ec35a20180dc5b8090b7a6ab85125df4d76 From c7574080e37aeac72787ca79c5fadc1750bc4c08 Mon Sep 17 00:00:00 2001 From: matthew brian white Date: Thu, 19 Nov 2026 15:21:49 +0000 Subject: [PATCH 06/11] build: address CodeRabbit review feedback for dependencies and installation --- CMakeLists.txt | 15 +++++++++-- README.md | 4 ++- cmake/SubstraitConfig.cmake.in | 14 ++++++++-- scripts/run-clang-format.sh | 12 ++++++++- scripts/setup-ubuntu.sh | 14 +++++++--- third_party/protobuf.cmake | 49 ++++++++++++++++++++++++++++++---- 6 files changed, 93 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index af88c27f..d2b3ea39 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -92,12 +92,23 @@ install( # set; install it so that the exported static libraries can be linked. get_filename_component(SUBSTRAIT_CPP_ANTLR4_LIBRARY_FILE "${ANTLR4_STATIC_LIBRARIES}" NAME) -install(FILES ${ANTLR4_STATIC_LIBRARIES} DESTINATION ${CMAKE_INSTALL_LIBDIR}) +string(REPLACE "$(Configuration)" "$" _SUBSTRAIT_ANTLR4_INSTALL_FILE "${ANTLR4_STATIC_LIBRARIES}") +string(REPLACE "$(CONFIGURATION)" "$" _SUBSTRAIT_ANTLR4_INSTALL_FILE "${_SUBSTRAIT_ANTLR4_INSTALL_FILE}") +install(FILES ${_SUBSTRAIT_ANTLR4_INSTALL_FILE} DESTINATION ${CMAKE_INSTALL_LIBDIR}) + +if(DEFINED Protobuf_VERSION AND Protobuf_VERSION) + set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION "${Protobuf_VERSION}") +elseif(DEFINED protobuf_VERSION AND protobuf_VERSION) + set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION "${protobuf_VERSION}") +else() + set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION "5.28") +endif() configure_package_config_file( cmake/SubstraitConfig.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/SubstraitConfig.cmake" - INSTALL_DESTINATION ${SUBSTRAIT_CPP_CMAKE_INSTALL_DIR}) + INSTALL_DESTINATION ${SUBSTRAIT_CPP_CMAKE_INSTALL_DIR} + PATH_VARS CMAKE_INSTALL_LIBDIR) write_basic_package_version_file( "${CMAKE_CURRENT_BINARY_DIR}/SubstraitConfigVersion.cmake" VERSION ${PROJECT_VERSION} diff --git a/README.md b/README.md index 25982bc0..412a5a7f 100644 --- a/README.md +++ b/README.md @@ -24,9 +24,11 @@ it needs `sudo` (you will be prompted for your password unless you are root). | Distribution | Setup script | Package manager | |---|---|---| -| Ubuntu (20.04 or later) | `./scripts/setup-ubuntu.sh` | `apt` | +| Ubuntu (24.04 or later) | `./scripts/setup-ubuntu.sh` | `apt` | | Fedora | `./scripts/setup-fedora.sh` | `dnf` | +> **Note**: Building substrait-cpp requires CMake 3.24 or newer. Ubuntu 24.04 supplies CMake 3.28; if using older Ubuntu releases (e.g. 22.04), install CMake >= 3.24 (e.g. via the Kitware APT repository or `pip`). + What the scripts install: a C++ compiler, CMake, Ninja, ccache, git, `tzdata`, libuuid and libcurl development headers, a Java runtime (needed at build time by ANTLR), `clang-format`/`clang-tidy` and `cmake-format`. The Fedora script also diff --git a/cmake/SubstraitConfig.cmake.in b/cmake/SubstraitConfig.cmake.in index d7a4e7b3..b79a243a 100644 --- a/cmake/SubstraitConfig.cmake.in +++ b/cmake/SubstraitConfig.cmake.in @@ -2,11 +2,19 @@ @PACKAGE_INIT@ +# Preserve the package prefix and libdir before dependency discovery, +# since find_dependency() calls in nested packages can overwrite PACKAGE_PREFIX_DIR. +set(_SUBSTRAIT_INSTALL_LIBDIR "@PACKAGE_CMAKE_INSTALL_LIBDIR@") + include(CMakeFindDependencyMacro) # Dependencies that appear in the interface of the exported targets. They are # either installed alongside this package or provided by the system. -find_dependency(Protobuf CONFIG) +if(@SUBSTRAIT_CPP_FIND_PROTOBUF_CONFIG@) + find_dependency(Protobuf @SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION@ CONFIG) +else() + find_dependency(Protobuf @SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION@) +endif() find_dependency(absl CONFIG) find_dependency(fmt CONFIG) find_dependency(yaml-cpp CONFIG) @@ -20,10 +28,12 @@ if(NOT TARGET antlr4_static) antlr4_static PROPERTIES IMPORTED_LOCATION - "${PACKAGE_PREFIX_DIR}/@CMAKE_INSTALL_LIBDIR@/@SUBSTRAIT_CPP_ANTLR4_LIBRARY_FILE@" + "${_SUBSTRAIT_INSTALL_LIBDIR}/@SUBSTRAIT_CPP_ANTLR4_LIBRARY_FILE@" ) endif() +unset(_SUBSTRAIT_INSTALL_LIBDIR) + include("${CMAKE_CURRENT_LIST_DIR}/SubstraitTargets.cmake") check_required_components(Substrait) diff --git a/scripts/run-clang-format.sh b/scripts/run-clang-format.sh index dc457083..0ec31efb 100755 --- a/scripts/run-clang-format.sh +++ b/scripts/run-clang-format.sh @@ -3,4 +3,14 @@ SCRIPTDIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) WORKDIR="$( cd $SCRIPTDIR/.. && pwd )" -find $WORKDIR/src $WORKDIR/include \( -name '*.h' -o -name '*.cpp' \) -exec clang-format-15 -style=file -i {} \; +CLANG_FORMAT="clang-format-15" +if ! command -v "$CLANG_FORMAT" &> /dev/null; then + CLANG_FORMAT="clang-format" +fi + +if ! command -v "$CLANG_FORMAT" &> /dev/null; then + echo "Error: clang-format not found. Please install clang-format (e.g. clang-format-15 or clang-tools-extra)." >&2 + exit 1 +fi + +find $WORKDIR/src $WORKDIR/include \( -name '*.h' -o -name '*.cpp' \) -exec "$CLANG_FORMAT" -style=file -i {} \; diff --git a/scripts/setup-ubuntu.sh b/scripts/setup-ubuntu.sh index ccdec8ed..7b7960da 100755 --- a/scripts/setup-ubuntu.sh +++ b/scripts/setup-ubuntu.sh @@ -1,14 +1,20 @@ #!/bin/bash # SPDX-License-Identifier: Apache-2.0 -# Minimal setup for Ubuntu 20.04. +# Minimal setup for Ubuntu 24.04 (requires CMake 3.24+). set -eufx -o pipefail +# Use sudo only when not already root (e.g. inside a container). +SUDO="" +if [ "$(id -u)" -ne 0 ]; then + SUDO="sudo" +fi + # Update the list of packages so we're running the latest. -sudo apt update +$SUDO apt update # Install all dependencies. -sudo --preserve-env apt install -y \ +$SUDO --preserve-env apt install -y \ wget \ g++ \ cmake \ @@ -26,7 +32,7 @@ sudo --preserve-env apt install -y \ # Legacy timezone names such as US/Central moved to a separate package in # Ubuntu 24.04 and later; the package does not exist on older releases. -sudo apt install -y tzdata-legacy || true +$SUDO apt install -y tzdata-legacy || true # Install the currently supported version of protobuf: PB_REL="https://github.com/protocolbuffers/protobuf/releases" diff --git a/third_party/protobuf.cmake b/third_party/protobuf.cmake index ad3b9bfd..58ec2ca9 100644 --- a/third_party/protobuf.cmake +++ b/third_party/protobuf.cmake @@ -25,9 +25,48 @@ set(SUBSTRAIT_CPP_PROTOBUF_MIN_VERSION # version with `FetchContent_Declare(... OVERRIDE_FIND_PACKAGE`. if(SUBSTRAIT_CPP_USE_SYSTEM_PROTOBUF) if(SUBSTRAIT_CPP_FIND_PROTOBUF_CONFIG) - find_package(Protobuf ${SUBSTRAIT_CPP_PROTOBUF_MIN_VERSION} CONFIG) + find_package(Protobuf CONFIG QUIET) + if(Protobuf_FOUND) + # Check version suitability: + # Protobuf upstream CONFIG reports either v2X.Y.Z or 5.X.Y.Z. + # We require at least 5.28 (i.e. 28.0 in new CONFIG numbering). + set(_protobuf_ver "${Protobuf_VERSION}") + if(NOT _protobuf_ver AND protobuf_VERSION) + set(_protobuf_ver "${protobuf_VERSION}") + endif() + if(_protobuf_ver) + if((_protobuf_ver VERSION_GREATER_EQUAL "21.0.0" AND _protobuf_ver VERSION_LESS "28.0.0") + OR (_protobuf_ver VERSION_LESS "5.28.0" AND _protobuf_ver VERSION_LESS "21.0.0")) + message(STATUS "Found system Protobuf CONFIG version ${_protobuf_ver}, but at least 5.28 (28.0) is required.") + set(Protobuf_FOUND FALSE) + endif() + endif() + endif() else() - find_package(Protobuf ${SUBSTRAIT_CPP_PROTOBUF_MIN_VERSION}) + # In module mode, CMake's FindProtobuf creates imported targets before checking version. + # To prevent leaked targets when system Protobuf is too old, probe the header first. + find_path(_SUBSTRAIT_PROTOBUF_PROBE_DIR + NAMES google/protobuf/port_def.inc google/protobuf/stubs/common.h) + set(_protobuf_module_acceptable FALSE) + if(_SUBSTRAIT_PROTOBUF_PROBE_DIR) + if(EXISTS "${_SUBSTRAIT_PROTOBUF_PROBE_DIR}/google/protobuf/port_def.inc") + file(STRINGS "${_SUBSTRAIT_PROTOBUF_PROBE_DIR}/google/protobuf/port_def.inc" + _PROTO_DEF_LINE REGEX "#define[ \t]+GOOGLE_PROTOBUF_VERSION[ \t]+[0-9]+") + elseif(EXISTS "${_SUBSTRAIT_PROTOBUF_PROBE_DIR}/google/protobuf/stubs/common.h") + file(STRINGS "${_SUBSTRAIT_PROTOBUF_PROBE_DIR}/google/protobuf/stubs/common.h" + _PROTO_DEF_LINE REGEX "#define[ \t]+GOOGLE_PROTOBUF_VERSION[ \t]+[0-9]+") + endif() + if(_PROTO_DEF_LINE) + string(REGEX MATCH "[0-9]+" _PROTO_NUM "${_PROTO_DEF_LINE}") + # 5.28.0 corresponds to GOOGLE_PROTOBUF_VERSION 5028000 + if(_PROTO_NUM GREATER_EQUAL 5028000) + set(_protobuf_module_acceptable TRUE) + endif() + endif() + endif() + if(_protobuf_module_acceptable) + find_package(Protobuf ${SUBSTRAIT_CPP_PROTOBUF_MIN_VERSION}) + endif() endif() endif() @@ -61,18 +100,18 @@ if(NOT Protobuf_FOUND AND SUBSTRAIT_CPP_FETCH_PROTOBUF) SYSTEM OVERRIDE_FIND_PACKAGE) set(protobuf_ABSL_PROVIDER "package" - CACHE STRING "") + CACHE STRING "" FORCE) # Needed so the Abseil targets are in an export set for install(EXPORT). set(ABSL_ENABLE_INSTALL ON - CACHE BOOL "") + CACHE BOOL "" FORCE) if(MSVC) set(protobuf_MSVC_STATIC_RUNTIME OFF) add_compile_options("/W0") else() add_compile_options("-w") endif() - fetchcontent_makeavailable(protobuf) + fetchcontent_makeavailable(absl protobuf) fetchcontent_getproperties(Protobuf SOURCE_DIR Protobuf_SOURCE_DIR) # This exists from Protobuf version 22.0 and up. include(${Protobuf_SOURCE_DIR}/cmake/protobuf-generate.cmake) From 536ee361a60b3b702fc53b489565f6bd190fd51b Mon Sep 17 00:00:00 2001 From: matthew brian white Date: Mon, 23 Nov 2026 09:16:33 +0000 Subject: [PATCH 07/11] build: refine Protobuf version normalization and enforce clang-format-15 --- CMakeLists.txt | 14 ++++--- scripts/run-clang-format.sh | 6 +-- third_party/protobuf.cmake | 80 +++++++++++++++++++++++++++++-------- 3 files changed, 72 insertions(+), 28 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d2b3ea39..e974ae3d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -96,12 +96,14 @@ string(REPLACE "$(Configuration)" "$" _SUBSTRAIT_ANTLR4_INSTALL_FILE "${ string(REPLACE "$(CONFIGURATION)" "$" _SUBSTRAIT_ANTLR4_INSTALL_FILE "${_SUBSTRAIT_ANTLR4_INSTALL_FILE}") install(FILES ${_SUBSTRAIT_ANTLR4_INSTALL_FILE} DESTINATION ${CMAKE_INSTALL_LIBDIR}) -if(DEFINED Protobuf_VERSION AND Protobuf_VERSION) - set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION "${Protobuf_VERSION}") -elseif(DEFINED protobuf_VERSION AND protobuf_VERSION) - set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION "${protobuf_VERSION}") -else() - set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION "5.28") +if(NOT DEFINED SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION OR NOT SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION) + if(DEFINED Protobuf_VERSION AND Protobuf_VERSION) + set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION "${Protobuf_VERSION}") + elseif(DEFINED protobuf_VERSION AND protobuf_VERSION) + set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION "${protobuf_VERSION}") + else() + message(FATAL_ERROR "SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION is not defined after third_party setup.") + endif() endif() configure_package_config_file( diff --git a/scripts/run-clang-format.sh b/scripts/run-clang-format.sh index 0ec31efb..2161d560 100755 --- a/scripts/run-clang-format.sh +++ b/scripts/run-clang-format.sh @@ -5,11 +5,7 @@ WORKDIR="$( cd $SCRIPTDIR/.. && pwd )" CLANG_FORMAT="clang-format-15" if ! command -v "$CLANG_FORMAT" &> /dev/null; then - CLANG_FORMAT="clang-format" -fi - -if ! command -v "$CLANG_FORMAT" &> /dev/null; then - echo "Error: clang-format not found. Please install clang-format (e.g. clang-format-15 or clang-tools-extra)." >&2 + echo "Error: clang-format-15 not found. Please install clang-format-15 to match CI formatting." >&2 exit 1 fi diff --git a/third_party/protobuf.cmake b/third_party/protobuf.cmake index 58ec2ca9..34c07baa 100644 --- a/third_party/protobuf.cmake +++ b/third_party/protobuf.cmake @@ -21,51 +21,89 @@ set(SUBSTRAIT_CPP_PROTOBUF_MIN_VERSION 5.28 CACHE STRING "Minimum acceptable version of a system Protobuf") +# Helper function to normalize Protobuf versions: +# Upstream Protobuf CONFIG packages report versions 26.x, 27.x, 28.x, 29.x, etc. +# These correspond to language versions 5.26.x, 5.27.x, 5.28.x, 5.29.x. +function(substrait_normalize_protobuf_version IN_VER OUT_VER) + set(VER "${IN_VER}") + if(VER MATCHES "^([2-9][0-9])(\\..*)?$") + set(VER "5.${VER}") + endif() + set(${OUT_VER} "${VER}" PARENT_SCOPE) +endfunction() + # First use `find_package`. This allows downstream projects to inject their # version with `FetchContent_Declare(... OVERRIDE_FIND_PACKAGE`. if(SUBSTRAIT_CPP_USE_SYSTEM_PROTOBUF) if(SUBSTRAIT_CPP_FIND_PROTOBUF_CONFIG) find_package(Protobuf CONFIG QUIET) if(Protobuf_FOUND) - # Check version suitability: - # Protobuf upstream CONFIG reports either v2X.Y.Z or 5.X.Y.Z. - # We require at least 5.28 (i.e. 28.0 in new CONFIG numbering). + # Check version suitability against configured SUBSTRAIT_CPP_PROTOBUF_MIN_VERSION set(_protobuf_ver "${Protobuf_VERSION}") if(NOT _protobuf_ver AND protobuf_VERSION) set(_protobuf_ver "${protobuf_VERSION}") endif() if(_protobuf_ver) - if((_protobuf_ver VERSION_GREATER_EQUAL "21.0.0" AND _protobuf_ver VERSION_LESS "28.0.0") - OR (_protobuf_ver VERSION_LESS "5.28.0" AND _protobuf_ver VERSION_LESS "21.0.0")) - message(STATUS "Found system Protobuf CONFIG version ${_protobuf_ver}, but at least 5.28 (28.0) is required.") + substrait_normalize_protobuf_version("${SUBSTRAIT_CPP_PROTOBUF_MIN_VERSION}" _min_ver_normalized) + substrait_normalize_protobuf_version("${_protobuf_ver}" _protobuf_ver_normalized) + if(_protobuf_ver_normalized VERSION_LESS _min_ver_normalized) + message(STATUS "Found system Protobuf CONFIG version ${_protobuf_ver} (${_protobuf_ver_normalized}), but at least ${SUBSTRAIT_CPP_PROTOBUF_MIN_VERSION} (${_min_ver_normalized}) is required.") set(Protobuf_FOUND FALSE) endif() endif() + if(Protobuf_FOUND) + set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION "${_protobuf_ver}" CACHE INTERNAL "Protobuf version used for Substrait build") + endif() endif() else() # In module mode, CMake's FindProtobuf creates imported targets before checking version. # To prevent leaked targets when system Protobuf is too old, probe the header first. - find_path(_SUBSTRAIT_PROTOBUF_PROBE_DIR - NAMES google/protobuf/port_def.inc google/protobuf/stubs/common.h) + if(DEFINED Protobuf_INCLUDE_DIR AND Protobuf_INCLUDE_DIR) + set(_SUBSTRAIT_PROTOBUF_PROBE_DIR "${Protobuf_INCLUDE_DIR}") + elseif(DEFINED PROTOBUF_INCLUDE_DIR AND PROTOBUF_INCLUDE_DIR) + set(_SUBSTRAIT_PROTOBUF_PROBE_DIR "${PROTOBUF_INCLUDE_DIR}") + else() + find_path(_SUBSTRAIT_PROTOBUF_PROBE_DIR + NAMES google/protobuf/stubs/common.h google/protobuf/port_def.inc) + endif() set(_protobuf_module_acceptable FALSE) if(_SUBSTRAIT_PROTOBUF_PROBE_DIR) - if(EXISTS "${_SUBSTRAIT_PROTOBUF_PROBE_DIR}/google/protobuf/port_def.inc") - file(STRINGS "${_SUBSTRAIT_PROTOBUF_PROBE_DIR}/google/protobuf/port_def.inc" - _PROTO_DEF_LINE REGEX "#define[ \t]+GOOGLE_PROTOBUF_VERSION[ \t]+[0-9]+") - elseif(EXISTS "${_SUBSTRAIT_PROTOBUF_PROBE_DIR}/google/protobuf/stubs/common.h") + set(_proto_num "") + if(EXISTS "${_SUBSTRAIT_PROTOBUF_PROBE_DIR}/google/protobuf/stubs/common.h") file(STRINGS "${_SUBSTRAIT_PROTOBUF_PROBE_DIR}/google/protobuf/stubs/common.h" - _PROTO_DEF_LINE REGEX "#define[ \t]+GOOGLE_PROTOBUF_VERSION[ \t]+[0-9]+") + _PROTO_DEF_LINE REGEX "#define[ \t]+(GOOGLE_)?PROTOBUF_VERSION[ \t]+[0-9]+") + if(_PROTO_DEF_LINE) + string(REGEX MATCH "[0-9]+" _proto_num "${_PROTO_DEF_LINE}") + endif() + endif() + if(NOT _proto_num AND EXISTS "${_SUBSTRAIT_PROTOBUF_PROBE_DIR}/google/protobuf/port_def.inc") + file(STRINGS "${_SUBSTRAIT_PROTOBUF_PROBE_DIR}/google/protobuf/port_def.inc" + _PROTO_DEF_LINE REGEX "#define[ \t]+(GOOGLE_)?PROTOBUF_VERSION[ \t]+[0-9]+") + if(_PROTO_DEF_LINE) + string(REGEX MATCH "[0-9]+" _proto_num "${_PROTO_DEF_LINE}") + endif() endif() - if(_PROTO_DEF_LINE) - string(REGEX MATCH "[0-9]+" _PROTO_NUM "${_PROTO_DEF_LINE}") - # 5.28.0 corresponds to GOOGLE_PROTOBUF_VERSION 5028000 - if(_PROTO_NUM GREATER_EQUAL 5028000) + if(_proto_num) + substrait_normalize_protobuf_version("${SUBSTRAIT_CPP_PROTOBUF_MIN_VERSION}" _min_norm) + if(_min_norm MATCHES "^([0-9]+)\\.([0-9]+)") + math(EXPR _min_proto_num "${CMAKE_MATCH_1} * 1000000 + ${CMAKE_MATCH_2} * 1000") + else() + set(_min_proto_num 5028000) + endif() + if(_proto_num GREATER_EQUAL _min_proto_num) set(_protobuf_module_acceptable TRUE) endif() endif() endif() if(_protobuf_module_acceptable) find_package(Protobuf ${SUBSTRAIT_CPP_PROTOBUF_MIN_VERSION}) + if(Protobuf_FOUND) + set(_protobuf_ver "${Protobuf_VERSION}") + if(NOT _protobuf_ver AND protobuf_VERSION) + set(_protobuf_ver "${protobuf_VERSION}") + endif() + set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION "${_protobuf_ver}" CACHE INTERNAL "Protobuf version used for Substrait build") + endif() endif() endif() endif() @@ -121,6 +159,14 @@ if(NOT Protobuf_FOUND AND SUBSTRAIT_CPP_FETCH_PROTOBUF) if(TARGET protoc AND NOT TARGET protobuf::protoc) add_executable(protobuf::protoc ALIAS protoc) endif() + if(DEFINED Protobuf_VERSION AND Protobuf_VERSION) + set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION "${Protobuf_VERSION}" CACHE INTERNAL "Protobuf version used for Substrait build") + elseif(DEFINED protobuf_VERSION AND protobuf_VERSION) + set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION "${protobuf_VERSION}" CACHE INTERNAL "Protobuf version used for Substrait build") + else() + string(REGEX REPLACE "^v" "" _tag_ver "${SUBSTRAIT_CPP_PROTOBUF_FETCH_TAG}") + set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION "${_tag_ver}" CACHE INTERNAL "Protobuf version used for Substrait build") + endif() endif() if(NOT TARGET protobuf::libprotobuf) From 64213a84433c29bc21d683e5360be38648c34bc4 Mon Sep 17 00:00:00 2001 From: matthew brian white Date: Mon, 23 Nov 2026 12:04:59 +0000 Subject: [PATCH 08/11] style: apply cmake-format --- CMakeLists.txt | 19 ++++++---- third_party/protobuf.cmake | 73 ++++++++++++++++++++++++++------------ 2 files changed, 64 insertions(+), 28 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e974ae3d..ece1ecb5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -92,17 +92,24 @@ install( # set; install it so that the exported static libraries can be linked. get_filename_component(SUBSTRAIT_CPP_ANTLR4_LIBRARY_FILE "${ANTLR4_STATIC_LIBRARIES}" NAME) -string(REPLACE "$(Configuration)" "$" _SUBSTRAIT_ANTLR4_INSTALL_FILE "${ANTLR4_STATIC_LIBRARIES}") -string(REPLACE "$(CONFIGURATION)" "$" _SUBSTRAIT_ANTLR4_INSTALL_FILE "${_SUBSTRAIT_ANTLR4_INSTALL_FILE}") -install(FILES ${_SUBSTRAIT_ANTLR4_INSTALL_FILE} DESTINATION ${CMAKE_INSTALL_LIBDIR}) - -if(NOT DEFINED SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION OR NOT SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION) +string(REPLACE "$(Configuration)" "$" _SUBSTRAIT_ANTLR4_INSTALL_FILE + "${ANTLR4_STATIC_LIBRARIES}") +string(REPLACE "$(CONFIGURATION)" "$" _SUBSTRAIT_ANTLR4_INSTALL_FILE + "${_SUBSTRAIT_ANTLR4_INSTALL_FILE}") +install(FILES ${_SUBSTRAIT_ANTLR4_INSTALL_FILE} + DESTINATION ${CMAKE_INSTALL_LIBDIR}) + +if(NOT DEFINED SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION + OR NOT SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION) if(DEFINED Protobuf_VERSION AND Protobuf_VERSION) set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION "${Protobuf_VERSION}") elseif(DEFINED protobuf_VERSION AND protobuf_VERSION) set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION "${protobuf_VERSION}") else() - message(FATAL_ERROR "SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION is not defined after third_party setup.") + message( + FATAL_ERROR + "SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION is not defined after third_party setup." + ) endif() endif() diff --git a/third_party/protobuf.cmake b/third_party/protobuf.cmake index 34c07baa..e21f1edb 100644 --- a/third_party/protobuf.cmake +++ b/third_party/protobuf.cmake @@ -29,7 +29,9 @@ function(substrait_normalize_protobuf_version IN_VER OUT_VER) if(VER MATCHES "^([2-9][0-9])(\\..*)?$") set(VER "5.${VER}") endif() - set(${OUT_VER} "${VER}" PARENT_SCOPE) + set(${OUT_VER} + "${VER}" + PARENT_SCOPE) endfunction() # First use `find_package`. This allows downstream projects to inject their @@ -44,15 +46,22 @@ if(SUBSTRAIT_CPP_USE_SYSTEM_PROTOBUF) set(_protobuf_ver "${protobuf_VERSION}") endif() if(_protobuf_ver) - substrait_normalize_protobuf_version("${SUBSTRAIT_CPP_PROTOBUF_MIN_VERSION}" _min_ver_normalized) - substrait_normalize_protobuf_version("${_protobuf_ver}" _protobuf_ver_normalized) + substrait_normalize_protobuf_version( + "${SUBSTRAIT_CPP_PROTOBUF_MIN_VERSION}" _min_ver_normalized) + substrait_normalize_protobuf_version("${_protobuf_ver}" + _protobuf_ver_normalized) if(_protobuf_ver_normalized VERSION_LESS _min_ver_normalized) - message(STATUS "Found system Protobuf CONFIG version ${_protobuf_ver} (${_protobuf_ver_normalized}), but at least ${SUBSTRAIT_CPP_PROTOBUF_MIN_VERSION} (${_min_ver_normalized}) is required.") + message( + STATUS + "Found system Protobuf CONFIG version ${_protobuf_ver} (${_protobuf_ver_normalized}), but at least ${SUBSTRAIT_CPP_PROTOBUF_MIN_VERSION} (${_min_ver_normalized}) is required." + ) set(Protobuf_FOUND FALSE) endif() endif() if(Protobuf_FOUND) - set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION "${_protobuf_ver}" CACHE INTERNAL "Protobuf version used for Substrait build") + set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION + "${_protobuf_ver}" + CACHE INTERNAL "Protobuf version used for Substrait build") endif() endif() else() @@ -64,29 +73,41 @@ if(SUBSTRAIT_CPP_USE_SYSTEM_PROTOBUF) set(_SUBSTRAIT_PROTOBUF_PROBE_DIR "${PROTOBUF_INCLUDE_DIR}") else() find_path(_SUBSTRAIT_PROTOBUF_PROBE_DIR - NAMES google/protobuf/stubs/common.h google/protobuf/port_def.inc) + NAMES google/protobuf/stubs/common.h + google/protobuf/port_def.inc) endif() set(_protobuf_module_acceptable FALSE) if(_SUBSTRAIT_PROTOBUF_PROBE_DIR) set(_proto_num "") - if(EXISTS "${_SUBSTRAIT_PROTOBUF_PROBE_DIR}/google/protobuf/stubs/common.h") - file(STRINGS "${_SUBSTRAIT_PROTOBUF_PROBE_DIR}/google/protobuf/stubs/common.h" - _PROTO_DEF_LINE REGEX "#define[ \t]+(GOOGLE_)?PROTOBUF_VERSION[ \t]+[0-9]+") + if(EXISTS + "${_SUBSTRAIT_PROTOBUF_PROBE_DIR}/google/protobuf/stubs/common.h") + file( + STRINGS + "${_SUBSTRAIT_PROTOBUF_PROBE_DIR}/google/protobuf/stubs/common.h" + _PROTO_DEF_LINE + REGEX "#define[ \t]+(GOOGLE_)?PROTOBUF_VERSION[ \t]+[0-9]+") if(_PROTO_DEF_LINE) string(REGEX MATCH "[0-9]+" _proto_num "${_PROTO_DEF_LINE}") endif() endif() - if(NOT _proto_num AND EXISTS "${_SUBSTRAIT_PROTOBUF_PROBE_DIR}/google/protobuf/port_def.inc") - file(STRINGS "${_SUBSTRAIT_PROTOBUF_PROBE_DIR}/google/protobuf/port_def.inc" - _PROTO_DEF_LINE REGEX "#define[ \t]+(GOOGLE_)?PROTOBUF_VERSION[ \t]+[0-9]+") + if(NOT _proto_num + AND EXISTS + "${_SUBSTRAIT_PROTOBUF_PROBE_DIR}/google/protobuf/port_def.inc") + file( + STRINGS + "${_SUBSTRAIT_PROTOBUF_PROBE_DIR}/google/protobuf/port_def.inc" + _PROTO_DEF_LINE + REGEX "#define[ \t]+(GOOGLE_)?PROTOBUF_VERSION[ \t]+[0-9]+") if(_PROTO_DEF_LINE) string(REGEX MATCH "[0-9]+" _proto_num "${_PROTO_DEF_LINE}") endif() endif() if(_proto_num) - substrait_normalize_protobuf_version("${SUBSTRAIT_CPP_PROTOBUF_MIN_VERSION}" _min_norm) + substrait_normalize_protobuf_version( + "${SUBSTRAIT_CPP_PROTOBUF_MIN_VERSION}" _min_norm) if(_min_norm MATCHES "^([0-9]+)\\.([0-9]+)") - math(EXPR _min_proto_num "${CMAKE_MATCH_1} * 1000000 + ${CMAKE_MATCH_2} * 1000") + math(EXPR _min_proto_num + "${CMAKE_MATCH_1} * 1000000 + ${CMAKE_MATCH_2} * 1000") else() set(_min_proto_num 5028000) endif() @@ -102,7 +123,9 @@ if(SUBSTRAIT_CPP_USE_SYSTEM_PROTOBUF) if(NOT _protobuf_ver AND protobuf_VERSION) set(_protobuf_ver "${protobuf_VERSION}") endif() - set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION "${_protobuf_ver}" CACHE INTERNAL "Protobuf version used for Substrait build") + set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION + "${_protobuf_ver}" + CACHE INTERNAL "Protobuf version used for Substrait build") endif() endif() endif() @@ -116,7 +139,7 @@ if(NOT Protobuf_FOUND AND SUBSTRAIT_CPP_FETCH_PROTOBUF) set(protobuf_BUILD_TESTS OFF CACHE BOOL "Disable Protobuf tests" FORCE) - fetchcontent_declare( + FetchContent_Declare( protobuf GIT_REPOSITORY https://github.com/protocolbuffers/protobuf.git GIT_TAG ${SUBSTRAIT_CPP_PROTOBUF_FETCH_TAG} @@ -131,7 +154,7 @@ if(NOT Protobuf_FOUND AND SUBSTRAIT_CPP_FETCH_PROTOBUF) # compilers (e.g. GCC 14+ on Fedora), so fetch a newer Abseil separately and # tell Protobuf to use it via find_package. message(STATUS "Fetching Abseil version ${SUBSTRAIT_CPP_ABSL_FETCH_TAG}") - fetchcontent_declare( + FetchContent_Declare( absl GIT_REPOSITORY https://github.com/abseil/abseil-cpp.git GIT_TAG ${SUBSTRAIT_CPP_ABSL_FETCH_TAG} @@ -149,8 +172,8 @@ if(NOT Protobuf_FOUND AND SUBSTRAIT_CPP_FETCH_PROTOBUF) else() add_compile_options("-w") endif() - fetchcontent_makeavailable(absl protobuf) - fetchcontent_getproperties(Protobuf SOURCE_DIR Protobuf_SOURCE_DIR) + FetchContent_MakeAvailable(absl protobuf) + FetchContent_GetProperties(Protobuf SOURCE_DIR Protobuf_SOURCE_DIR) # This exists from Protobuf version 22.0 and up. include(${Protobuf_SOURCE_DIR}/cmake/protobuf-generate.cmake) if(TARGET libprotobuf AND NOT TARGET protobuf::libprotobuf) @@ -160,12 +183,18 @@ if(NOT Protobuf_FOUND AND SUBSTRAIT_CPP_FETCH_PROTOBUF) add_executable(protobuf::protoc ALIAS protoc) endif() if(DEFINED Protobuf_VERSION AND Protobuf_VERSION) - set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION "${Protobuf_VERSION}" CACHE INTERNAL "Protobuf version used for Substrait build") + set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION + "${Protobuf_VERSION}" + CACHE INTERNAL "Protobuf version used for Substrait build") elseif(DEFINED protobuf_VERSION AND protobuf_VERSION) - set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION "${protobuf_VERSION}" CACHE INTERNAL "Protobuf version used for Substrait build") + set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION + "${protobuf_VERSION}" + CACHE INTERNAL "Protobuf version used for Substrait build") else() string(REGEX REPLACE "^v" "" _tag_ver "${SUBSTRAIT_CPP_PROTOBUF_FETCH_TAG}") - set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION "${_tag_ver}" CACHE INTERNAL "Protobuf version used for Substrait build") + set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION + "${_tag_ver}" + CACHE INTERNAL "Protobuf version used for Substrait build") endif() endif() From 6b4a10bee954bc934e1f8ceeac4f4859c6426941 Mon Sep 17 00:00:00 2001 From: matthew brian white Date: Mon, 23 Nov 2026 13:05:05 +0000 Subject: [PATCH 09/11] build: record C++ Protobuf version and fix root handling in Ubuntu setup Record the C++ Protobuf version (e.g. 5.29.3) for installed-package dependency lookup in CONFIG, module and fetched paths, and recover it from the populated FetchContent source when a parent provides Protobuf via OVERRIDE_FIND_PACKAGE without version metadata. Do not pass --preserve-env when setup-ubuntu.sh runs as root without sudo. --- scripts/setup-ubuntu.sh | 2 +- third_party/protobuf.cmake | 92 +++++++++++++++++++++++++++----------- 2 files changed, 67 insertions(+), 27 deletions(-) diff --git a/scripts/setup-ubuntu.sh b/scripts/setup-ubuntu.sh index 7b7960da..b35efec7 100755 --- a/scripts/setup-ubuntu.sh +++ b/scripts/setup-ubuntu.sh @@ -14,7 +14,7 @@ fi $SUDO apt update # Install all dependencies. -$SUDO --preserve-env apt install -y \ +$SUDO ${SUDO:+--preserve-env} apt install -y \ wget \ g++ \ cmake \ diff --git a/third_party/protobuf.cmake b/third_party/protobuf.cmake index e21f1edb..1a63c752 100644 --- a/third_party/protobuf.cmake +++ b/third_party/protobuf.cmake @@ -21,9 +21,9 @@ set(SUBSTRAIT_CPP_PROTOBUF_MIN_VERSION 5.28 CACHE STRING "Minimum acceptable version of a system Protobuf") -# Helper function to normalize Protobuf versions: -# Upstream Protobuf CONFIG packages report versions 26.x, 27.x, 28.x, 29.x, etc. -# These correspond to language versions 5.26.x, 5.27.x, 5.28.x, 5.29.x. +# Helper function to normalize Protobuf versions: Upstream Protobuf CONFIG +# packages report versions 26.x, 27.x, 28.x, 29.x, etc. These correspond to +# language versions 5.26.x, 5.27.x, 5.28.x, 5.29.x. function(substrait_normalize_protobuf_version IN_VER OUT_VER) set(VER "${IN_VER}") if(VER MATCHES "^([2-9][0-9])(\\..*)?$") @@ -34,17 +34,57 @@ function(substrait_normalize_protobuf_version IN_VER OUT_VER) PARENT_SCOPE) endfunction() +# Records the C++ (language) Protobuf version used for this build, e.g. 5.29.3 +# for the v29.3 release. Installed packages request this version from +# `find_dependency`, and Protobuf's generated version file compares the +# requested major version against the C++ major version (5), not the CONFIG +# package version (29.3.0). +function(substrait_record_protobuf_build_version IN_VER) + substrait_normalize_protobuf_version("${IN_VER}" _normalized) + if(_normalized MATCHES "^([0-9]+\\.[0-9]+\\.[0-9]+)") + set(_normalized "${CMAKE_MATCH_1}") + endif() + set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION + "${_normalized}" + CACHE INTERNAL "Protobuf version used for Substrait build") +endfunction() + +# Reads the C++ Protobuf version from a populated `protobuf` FetchContent +# dependency. Used when the version is not otherwise available, e.g. when a +# parent project provides Protobuf with `OVERRIDE_FIND_PACKAGE`, where the +# generated find_package redirect supplies targets but no version. +function(substrait_protobuf_source_version OUT_VER) + set(${OUT_VER} + "" + PARENT_SCOPE) + include(FetchContent) + FetchContent_GetProperties(protobuf SOURCE_DIR _protobuf_src_dir) + if(_protobuf_src_dir AND EXISTS "${_protobuf_src_dir}/CMakeLists.txt") + file(STRINGS "${_protobuf_src_dir}/CMakeLists.txt" _version_line + REGEX "^[ \t]*set\\([ \t]*protobuf_VERSION_STRING[ \t]+\"[^\"]+\"") + if(_version_line MATCHES "\"([^\"]+)\"") + set(${OUT_VER} + "${CMAKE_MATCH_1}" + PARENT_SCOPE) + endif() + endif() +endfunction() + # First use `find_package`. This allows downstream projects to inject their # version with `FetchContent_Declare(... OVERRIDE_FIND_PACKAGE`. if(SUBSTRAIT_CPP_USE_SYSTEM_PROTOBUF) if(SUBSTRAIT_CPP_FIND_PROTOBUF_CONFIG) find_package(Protobuf CONFIG QUIET) if(Protobuf_FOUND) - # Check version suitability against configured SUBSTRAIT_CPP_PROTOBUF_MIN_VERSION + # Check version suitability against configured + # SUBSTRAIT_CPP_PROTOBUF_MIN_VERSION set(_protobuf_ver "${Protobuf_VERSION}") if(NOT _protobuf_ver AND protobuf_VERSION) set(_protobuf_ver "${protobuf_VERSION}") endif() + if(NOT _protobuf_ver) + substrait_protobuf_source_version(_protobuf_ver) + endif() if(_protobuf_ver) substrait_normalize_protobuf_version( "${SUBSTRAIT_CPP_PROTOBUF_MIN_VERSION}" _min_ver_normalized) @@ -58,15 +98,14 @@ if(SUBSTRAIT_CPP_USE_SYSTEM_PROTOBUF) set(Protobuf_FOUND FALSE) endif() endif() - if(Protobuf_FOUND) - set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION - "${_protobuf_ver}" - CACHE INTERNAL "Protobuf version used for Substrait build") + if(Protobuf_FOUND AND _protobuf_ver) + substrait_record_protobuf_build_version("${_protobuf_ver}") endif() endif() else() - # In module mode, CMake's FindProtobuf creates imported targets before checking version. - # To prevent leaked targets when system Protobuf is too old, probe the header first. + # In module mode, CMake's FindProtobuf creates imported targets before + # checking version. To prevent leaked targets when system Protobuf is too + # old, probe the header first. if(DEFINED Protobuf_INCLUDE_DIR AND Protobuf_INCLUDE_DIR) set(_SUBSTRAIT_PROTOBUF_PROBE_DIR "${Protobuf_INCLUDE_DIR}") elseif(DEFINED PROTOBUF_INCLUDE_DIR AND PROTOBUF_INCLUDE_DIR) @@ -123,9 +162,12 @@ if(SUBSTRAIT_CPP_USE_SYSTEM_PROTOBUF) if(NOT _protobuf_ver AND protobuf_VERSION) set(_protobuf_ver "${protobuf_VERSION}") endif() - set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION - "${_protobuf_ver}" - CACHE INTERNAL "Protobuf version used for Substrait build") + if(NOT _protobuf_ver) + substrait_protobuf_source_version(_protobuf_ver) + endif() + if(_protobuf_ver) + substrait_record_protobuf_build_version("${_protobuf_ver}") + endif() endif() endif() endif() @@ -182,20 +224,18 @@ if(NOT Protobuf_FOUND AND SUBSTRAIT_CPP_FETCH_PROTOBUF) if(TARGET protoc AND NOT TARGET protobuf::protoc) add_executable(protobuf::protoc ALIAS protoc) endif() - if(DEFINED Protobuf_VERSION AND Protobuf_VERSION) - set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION - "${Protobuf_VERSION}" - CACHE INTERNAL "Protobuf version used for Substrait build") - elseif(DEFINED protobuf_VERSION AND protobuf_VERSION) - set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION - "${protobuf_VERSION}" - CACHE INTERNAL "Protobuf version used for Substrait build") - else() - string(REGEX REPLACE "^v" "" _tag_ver "${SUBSTRAIT_CPP_PROTOBUF_FETCH_TAG}") - set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION - "${_tag_ver}" - CACHE INTERNAL "Protobuf version used for Substrait build") + substrait_protobuf_source_version(_fetched_ver) + if(NOT _fetched_ver AND DEFINED Protobuf_VERSION) + set(_fetched_ver "${Protobuf_VERSION}") + endif() + if(NOT _fetched_ver AND DEFINED protobuf_VERSION) + set(_fetched_ver "${protobuf_VERSION}") + endif() + if(NOT _fetched_ver) + string(REGEX REPLACE "^v" "" _fetched_ver + "${SUBSTRAIT_CPP_PROTOBUF_FETCH_TAG}") endif() + substrait_record_protobuf_build_version("${_fetched_ver}") endif() if(NOT TARGET protobuf::libprotobuf) From 45868e689058a6ebc9e11fa5a93a5ce944bfb350 Mon Sep 17 00:00:00 2001 From: matthew brian white Date: Mon, 23 Nov 2026 17:30:44 +0000 Subject: [PATCH 10/11] style: lowercase fetchcontent commands to match CI cmake-format --- third_party/protobuf.cmake | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/third_party/protobuf.cmake b/third_party/protobuf.cmake index 1a63c752..f7727a67 100644 --- a/third_party/protobuf.cmake +++ b/third_party/protobuf.cmake @@ -58,7 +58,7 @@ function(substrait_protobuf_source_version OUT_VER) "" PARENT_SCOPE) include(FetchContent) - FetchContent_GetProperties(protobuf SOURCE_DIR _protobuf_src_dir) + fetchcontent_getproperties(protobuf SOURCE_DIR _protobuf_src_dir) if(_protobuf_src_dir AND EXISTS "${_protobuf_src_dir}/CMakeLists.txt") file(STRINGS "${_protobuf_src_dir}/CMakeLists.txt" _version_line REGEX "^[ \t]*set\\([ \t]*protobuf_VERSION_STRING[ \t]+\"[^\"]+\"") @@ -181,7 +181,7 @@ if(NOT Protobuf_FOUND AND SUBSTRAIT_CPP_FETCH_PROTOBUF) set(protobuf_BUILD_TESTS OFF CACHE BOOL "Disable Protobuf tests" FORCE) - FetchContent_Declare( + fetchcontent_declare( protobuf GIT_REPOSITORY https://github.com/protocolbuffers/protobuf.git GIT_TAG ${SUBSTRAIT_CPP_PROTOBUF_FETCH_TAG} @@ -196,7 +196,7 @@ if(NOT Protobuf_FOUND AND SUBSTRAIT_CPP_FETCH_PROTOBUF) # compilers (e.g. GCC 14+ on Fedora), so fetch a newer Abseil separately and # tell Protobuf to use it via find_package. message(STATUS "Fetching Abseil version ${SUBSTRAIT_CPP_ABSL_FETCH_TAG}") - FetchContent_Declare( + fetchcontent_declare( absl GIT_REPOSITORY https://github.com/abseil/abseil-cpp.git GIT_TAG ${SUBSTRAIT_CPP_ABSL_FETCH_TAG} @@ -214,8 +214,8 @@ if(NOT Protobuf_FOUND AND SUBSTRAIT_CPP_FETCH_PROTOBUF) else() add_compile_options("-w") endif() - FetchContent_MakeAvailable(absl protobuf) - FetchContent_GetProperties(Protobuf SOURCE_DIR Protobuf_SOURCE_DIR) + fetchcontent_makeavailable(absl protobuf) + fetchcontent_getproperties(Protobuf SOURCE_DIR Protobuf_SOURCE_DIR) # This exists from Protobuf version 22.0 and up. include(${Protobuf_SOURCE_DIR}/cmake/protobuf-generate.cmake) if(TARGET libprotobuf AND NOT TARGET protobuf::libprotobuf) From 39e7d72d607764913bd948b71773f5767618392a Mon Sep 17 00:00:00 2001 From: matthew brian white Date: Tue, 24 Nov 2026 12:54:17 +0000 Subject: [PATCH 11/11] build: fall back to fetched Protobuf cleanly and bound system versions - Request the C++ major in CONFIG mode so Protobuf's version file rejects unsuitable packages before loading imported targets. - Only map v26-v29 CONFIG versions to C++ major 5; cap module-mode probe below the next major. - Fail clearly when the fetched Protobuf version cannot be derived. - Allow CLANG_FORMAT override in run-clang-format.sh and document it. --- CMakeLists.txt | 17 +++------- README.md | 4 +++ scripts/run-clang-format.sh | 6 ++-- third_party/protobuf.cmake | 63 +++++++++++++++++++++++++------------ 4 files changed, 56 insertions(+), 34 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ece1ecb5..52749ac5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -99,18 +99,11 @@ string(REPLACE "$(CONFIGURATION)" "$" _SUBSTRAIT_ANTLR4_INSTALL_FILE install(FILES ${_SUBSTRAIT_ANTLR4_INSTALL_FILE} DESTINATION ${CMAKE_INSTALL_LIBDIR}) -if(NOT DEFINED SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION - OR NOT SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION) - if(DEFINED Protobuf_VERSION AND Protobuf_VERSION) - set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION "${Protobuf_VERSION}") - elseif(DEFINED protobuf_VERSION AND protobuf_VERSION) - set(SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION "${protobuf_VERSION}") - else() - message( - FATAL_ERROR - "SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION is not defined after third_party setup." - ) - endif() +if(NOT SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION) + message( + FATAL_ERROR + "SUBSTRAIT_CPP_PROTOBUF_BUILD_VERSION is not defined after third_party setup." + ) endif() configure_package_config_file( diff --git a/README.md b/README.md index 412a5a7f..a51c6a11 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,10 @@ libuuid and libcurl development headers, a Java runtime (needed at build time by ANTLR), `clang-format`/`clang-tidy` and `cmake-format`. The Fedora script also installs GoogleTest from the distribution. +`make format` uses `clang-format-15` to match CI. Fedora does not package that +version under this name, so install it yourself and point the script at it, +e.g. `CLANG_FORMAT=/path/to/clang-format make format`. + Ubuntu: ```shell diff --git a/scripts/run-clang-format.sh b/scripts/run-clang-format.sh index 2161d560..6a487b41 100755 --- a/scripts/run-clang-format.sh +++ b/scripts/run-clang-format.sh @@ -3,9 +3,11 @@ SCRIPTDIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) WORKDIR="$( cd $SCRIPTDIR/.. && pwd )" -CLANG_FORMAT="clang-format-15" +# CI formats with clang-format-15. Set CLANG_FORMAT to point at a clang-format 15 +# binary (e.g. on distributions that do not package it under this name). +CLANG_FORMAT="${CLANG_FORMAT:-clang-format-15}" if ! command -v "$CLANG_FORMAT" &> /dev/null; then - echo "Error: clang-format-15 not found. Please install clang-format-15 to match CI formatting." >&2 + echo "Error: $CLANG_FORMAT not found. Install clang-format 15 (or set CLANG_FORMAT to its path) to match CI formatting." >&2 exit 1 fi diff --git a/third_party/protobuf.cmake b/third_party/protobuf.cmake index f7727a67..3ebdb898 100644 --- a/third_party/protobuf.cmake +++ b/third_party/protobuf.cmake @@ -14,19 +14,22 @@ set(SUBSTRAIT_CPP_ABSL_FETCH_TAG set(SUBSTRAIT_CPP_PROTOBUF_FETCH_TAG ${SUBSTRAIT_CPP_PROTOBUF_DEFAULT_FETCH_TAG} CACHE STRING "Git tag or commit to use for Protobuf FetchContent") -# Protobuf's CMake package version is 5.. for the v. -# releases (e.g. v28.2 is 5.28.2). Older system packages (e.g. 3.19 shipped by -# some distributions) are rejected so that the FetchContent fallback is used. +# Protobuf's C++ (language) version is 5.. for the v26-v29 +# releases (e.g. v28.2 is 5.28.2), while the CMake package version of the same +# release is ..0 (28.2.0). Older system packages (e.g. 3.19 +# shipped by some distributions) are rejected so that the FetchContent fallback +# is used. set(SUBSTRAIT_CPP_PROTOBUF_MIN_VERSION 5.28 CACHE STRING "Minimum acceptable version of a system Protobuf") # Helper function to normalize Protobuf versions: Upstream Protobuf CONFIG -# packages report versions 26.x, 27.x, 28.x, 29.x, etc. These correspond to -# language versions 5.26.x, 5.27.x, 5.28.x, 5.29.x. +# packages report versions 26.x, 27.x, 28.x, 29.x. These correspond to language +# versions 5.26.x, 5.27.x, 5.28.x, 5.29.x. Other releases use a different C++ +# major (4 for v22-v25, 6 for v30 and later), so they are left unchanged. function(substrait_normalize_protobuf_version IN_VER OUT_VER) set(VER "${IN_VER}") - if(VER MATCHES "^([2-9][0-9])(\\..*)?$") + if(VER MATCHES "^(2[6-9])(\\..*)?$") set(VER "5.${VER}") endif() set(${OUT_VER} @@ -74,10 +77,16 @@ endfunction() # version with `FetchContent_Declare(... OVERRIDE_FIND_PACKAGE`. if(SUBSTRAIT_CPP_USE_SYSTEM_PROTOBUF) if(SUBSTRAIT_CPP_FIND_PROTOBUF_CONFIG) - find_package(Protobuf CONFIG QUIET) + # Protobuf's version file rejects a package whose C++ major version (5 for + # v26-v29) differs from the requested one before it loads any imported + # targets, so too-old (3.x, 4.x) or too-new (6.x) packages fall back to the + # fetch cleanly. It cannot check the minor version: it compares the request + # against the package version ..0 (29.3.0 for v29.3). + substrait_normalize_protobuf_version("${SUBSTRAIT_CPP_PROTOBUF_MIN_VERSION}" + _min_ver_normalized) + string(REGEX MATCH "^[0-9]+" _min_major "${_min_ver_normalized}") + find_package(Protobuf ${_min_major} CONFIG QUIET) if(Protobuf_FOUND) - # Check version suitability against configured - # SUBSTRAIT_CPP_PROTOBUF_MIN_VERSION set(_protobuf_ver "${Protobuf_VERSION}") if(NOT _protobuf_ver AND protobuf_VERSION) set(_protobuf_ver "${protobuf_VERSION}") @@ -86,19 +95,21 @@ if(SUBSTRAIT_CPP_USE_SYSTEM_PROTOBUF) substrait_protobuf_source_version(_protobuf_ver) endif() if(_protobuf_ver) - substrait_normalize_protobuf_version( - "${SUBSTRAIT_CPP_PROTOBUF_MIN_VERSION}" _min_ver_normalized) - substrait_normalize_protobuf_version("${_protobuf_ver}" - _protobuf_ver_normalized) - if(_protobuf_ver_normalized VERSION_LESS _min_ver_normalized) + # A CONFIG package version (29.3.0) lacks the C++ major, which is the + # one requested above. + if(_protobuf_ver MATCHES "^[0-9][0-9]\\.") + set(_protobuf_ver "${_min_major}.${_protobuf_ver}") + endif() + if(_protobuf_ver VERSION_LESS _min_ver_normalized) + # The imported targets already exist in this scope, so fetching + # Protobuf now would fail with a duplicate target. message( - STATUS - "Found system Protobuf CONFIG version ${_protobuf_ver} (${_protobuf_ver_normalized}), but at least ${SUBSTRAIT_CPP_PROTOBUF_MIN_VERSION} (${_min_ver_normalized}) is required." + FATAL_ERROR + "Found system Protobuf ${_protobuf_ver}, but at least " + "${_min_ver_normalized} is required. Set " + "SUBSTRAIT_CPP_USE_SYSTEM_PROTOBUF=OFF to fetch Protobuf instead." ) - set(Protobuf_FOUND FALSE) endif() - endif() - if(Protobuf_FOUND AND _protobuf_ver) substrait_record_protobuf_build_version("${_protobuf_ver}") endif() endif() @@ -147,10 +158,15 @@ if(SUBSTRAIT_CPP_USE_SYSTEM_PROTOBUF) if(_min_norm MATCHES "^([0-9]+)\\.([0-9]+)") math(EXPR _min_proto_num "${CMAKE_MATCH_1} * 1000000 + ${CMAKE_MATCH_2} * 1000") + math(EXPR _max_proto_num "(${CMAKE_MATCH_1} + 1) * 1000000") else() set(_min_proto_num 5028000) + set(_max_proto_num 6000000) endif() - if(_proto_num GREATER_EQUAL _min_proto_num) + # The upper bound keeps module mode consistent with CONFIG mode, which + # does not accept a different C++ major version. + if(_proto_num GREATER_EQUAL _min_proto_num AND _proto_num LESS + _max_proto_num) set(_protobuf_module_acceptable TRUE) endif() endif() @@ -234,6 +250,13 @@ if(NOT Protobuf_FOUND AND SUBSTRAIT_CPP_FETCH_PROTOBUF) if(NOT _fetched_ver) string(REGEX REPLACE "^v" "" _fetched_ver "${SUBSTRAIT_CPP_PROTOBUF_FETCH_TAG}") + if(NOT _fetched_ver MATCHES "^[0-9]+\\.[0-9]+(\\.[0-9]+)?$") + message( + FATAL_ERROR + "Cannot derive the Protobuf version from the fetched source or from " + "SUBSTRAIT_CPP_PROTOBUF_FETCH_TAG='${SUBSTRAIT_CPP_PROTOBUF_FETCH_TAG}'." + ) + endif() endif() substrait_record_protobuf_build_version("${_fetched_ver}") endif()