diff --git a/.github/workflows/build_test.yml b/.github/workflows/build_test.yml index 0c780d3f..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 @@ -55,18 +57,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..52749ac5 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,12 @@ 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 +77,47 @@ 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) +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 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( + cmake/SubstraitConfig.cmake.in + "${CMAKE_CURRENT_BINARY_DIR}/SubstraitConfig.cmake" + 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} + 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..a51c6a11 100644 --- a/README.md +++ b/README.md @@ -16,15 +16,83 @@ 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 (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 +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 -$ ./scripts/setup-ubuntu.sh +$ ./scripts/setup-ubuntu.sh $ make ``` +Fedora: + +```shell +$ ./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..b79a243a --- /dev/null +++ b/cmake/SubstraitConfig.cmake.in @@ -0,0 +1,39 @@ +# SPDX-License-Identifier: Apache-2.0 + +@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. +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) +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 + "${_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/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/run-clang-format.sh b/scripts/run-clang-format.sh index dc457083..6a487b41 100755 --- a/scripts/run-clang-format.sh +++ b/scripts/run-clang-format.sh @@ -3,4 +3,12 @@ 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 {} \; +# 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 not found. Install clang-format 15 (or set CLANG_FORMAT to its path) to match CI formatting." >&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-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..b35efec7 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 ${SUDO:+--preserve-env} apt install -y \ wget \ g++ \ cmake \ @@ -21,7 +27,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/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 diff --git a/third_party/protobuf.cmake b/third_party/protobuf.cmake index 6eaf60ae..3ebdb898 100644 --- a/third_party/protobuf.cmake +++ b/third_party/protobuf.cmake @@ -8,17 +8,184 @@ 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 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. 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[6-9])(\\..*)?$") + set(VER "5.${VER}") + endif() + set(${OUT_VER} + "${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) + # 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) + 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) + # 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( + 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." + ) + endif() + substrait_record_protobuf_build_version("${_protobuf_ver}") + endif() + endif() else() - find_package(Protobuf) + # 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) + 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) + 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(_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_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") + math(EXPR _max_proto_num "(${CMAKE_MATCH_1} + 1) * 1000000") + else() + set(_min_proto_num 5028000) + set(_max_proto_num 6000000) + endif() + # 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() + 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() + 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() endif() @@ -41,13 +208,29 @@ 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 "" FORCE) + # Needed so the Abseil targets are in an export set for install(EXPORT). + set(ABSL_ENABLE_INSTALL + ON + 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) @@ -57,6 +240,25 @@ if(NOT Protobuf_FOUND AND SUBSTRAIT_CPP_FETCH_PROTOBUF) if(TARGET protoc AND NOT TARGET protobuf::protoc) add_executable(protobuf::protoc ALIAS protoc) endif() + 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}") + 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() if(NOT TARGET protobuf::libprotobuf)