build(cmake): build bundled GoogleTest with FetchContent

- Add the bundled GoogleTest with FetchContent and OVERRIDE_FIND_PACKAGE,
  so it uses the compiler and flags of the FairMQ build
- Build it in a block() without gmock, install and static analysis
- Replace build_bundled() with fairmq_init_submodule(), which runs
  git submodule update only if the submodule folder is empty, and stops
  with a clear error if it stays empty
- Install the bundled FairCMakeModules into the build directory with its
  tests off, as before
- Use the bundled PicoSHA2 headers directly from the source tree
- With USE_EXTERNAL_GTEST, GTest is a private dependency and no longer
  in FairMQ_PACKAGE_DEPENDENCIES

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Claude
2026-11-09 23:03:53 +00:00
committed by Dennis Klein
parent 08d371b1d4
commit 230a614bb1
7 changed files with 94 additions and 89 deletions

View File

@@ -14,6 +14,38 @@ set(PROJECT_FairCMakeModules_VERSION 0.2)
find_package(FairCMakeModules ${PROJECT_FairCMakeModules_VERSION} QUIET)
if(NOT FairCMakeModules_FOUND AND NOT FairCMakeModules_BUNDLED)
build_bundled(FairCMakeModules extern/FairCMakeModules)
# FairCMakeModules expects to be the top-level project, so it cannot be added
# with add_subdirectory(). Install the bundled copy into the build directory
# at configure time instead. It contains only CMake modules.
message(STATUS "Building bundled FairCMakeModules")
fairmq_init_submodule(extern/FairCMakeModules)
set(_fcm_src ${PROJECT_SOURCE_DIR}/extern/FairCMakeModules)
set(_fcm_bin ${PROJECT_BINARY_DIR}/extern/FairCMakeModules)
set(_fcm_prefix ${PROJECT_BINARY_DIR}/extern/FairCMakeModules_install)
set(_fcm_log ${_fcm_bin}/build.log)
file(MAKE_DIRECTORY ${_fcm_bin})
file(REMOVE ${_fcm_log})
foreach(_fcm_step IN ITEMS configure install)
if(_fcm_step STREQUAL configure)
set(_fcm_cmd ${CMAKE_COMMAND} -S ${_fcm_src} -B ${_fcm_bin} -G ${CMAKE_GENERATOR}
-DCMAKE_INSTALL_PREFIX=${_fcm_prefix}
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DBUILD_TESTING=OFF)
else()
set(_fcm_cmd ${CMAKE_COMMAND} --build ${_fcm_bin} --target install)
endif()
execute_process(COMMAND ${_fcm_cmd}
OUTPUT_VARIABLE _fcm_out
ERROR_VARIABLE _fcm_out
RESULT_VARIABLE _fcm_res
)
file(APPEND ${_fcm_log} "${_fcm_out}")
if(_fcm_res)
message(FATAL_ERROR "Building bundled FairCMakeModules failed. See ${_fcm_log}")
endif()
endforeach()
set(FairCMakeModules_ROOT ${_fcm_prefix} CACHE PATH "Location of bundled package FairCMakeModules")
set(FairCMakeModules_BUNDLED ON CACHE BOOL "Whether bundled FairCMakeModules was used")
message(STATUS "Building bundled FairCMakeModules - done")
find_package(FairCMakeModules REQUIRED)
endif()

View File

@@ -1,5 +1,5 @@
################################################################################
# Copyright (C) 2018-2021 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH #
# Copyright (C) 2018-2026 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH #
# #
# This software is distributed under the terms of the #
# GNU Lesser General Public Licence (LGPL) version 3, #
@@ -10,82 +10,32 @@ include_guard(GLOBAL)
find_package(Git QUIET)
# TODO Deduplicate code
macro(exec cmd)
list(JOIN ARGN " " args)
file(APPEND ${${package}_BUILD_LOGFILE} ">>> ${cmd} ${args}\n")
execute_process(COMMAND ${cmd} ${ARGN}
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
OUTPUT_VARIABLE log
ERROR_VARIABLE log
RESULT_VARIABLE res
)
file(APPEND ${${package}_BUILD_LOGFILE} ${log})
if(res)
message(FATAL_ERROR "${res} \nSee also \"${${package}_BUILD_LOGFILE}\"")
endif()
endmacro()
macro(exec_source cmd)
list(JOIN ARGN " " args)
file(APPEND ${${package}_BUILD_LOGFILE} ">>> ${cmd} ${args}\n")
execute_process(COMMAND ${cmd} ${ARGN}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
OUTPUT_VARIABLE log
ERROR_VARIABLE log
RESULT_VARIABLE res
)
file(APPEND ${${package}_BUILD_LOGFILE} ${log})
if(res)
message(FATAL_ERROR "${res} \nSee also \"${${package}_BUILD_LOGFILE}\"")
endif()
endmacro()
function(build_bundled package bundle)
message(STATUS "Building bundled ${package}")
set(${package}_SOURCE_DIR ${PROJECT_SOURCE_DIR}/${bundle})
set(${package}_BINARY_DIR ${PROJECT_BINARY_DIR}/${bundle})
file(MAKE_DIRECTORY ${${package}_BINARY_DIR})
set(${package}_BUILD_LOGFILE ${${package}_BINARY_DIR}/build.log)
file(REMOVE ${${package}_BUILD_LOGFILE})
if(GIT_EXECUTABLE)
exec_source(${GIT_EXECUTABLE} submodule update --init -- ${${package}_SOURCE_DIR})
# fairmq_init_submodule(<path>)
#
# Checks out the Git submodule <path> (relative to PROJECT_SOURCE_DIR) if its
# folder is empty. Stops with an error if the folder stays empty.
function(fairmq_init_submodule path)
set(dir "${PROJECT_SOURCE_DIR}/${path}")
file(GLOB content "${dir}/*")
if(content)
return()
endif()
if(${package} STREQUAL GTest)
set(${package}_INSTALL_DIR ${PROJECT_BINARY_DIR}/${bundle}_install)
file(MAKE_DIRECTORY ${${package}_INSTALL_DIR})
set(${package}_PREFIX ${${package}_INSTALL_DIR})
exec(${CMAKE_COMMAND} -S ${${package}_SOURCE_DIR} -B ${${package}_BINARY_DIR} -G ${CMAKE_GENERATOR}
-DCMAKE_INSTALL_PREFIX=${${package}_INSTALL_DIR} -DBUILD_GMOCK=OFF
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
if(GIT_EXECUTABLE AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
message(STATUS "Checking out submodule ${path}")
execute_process(
COMMAND ${GIT_EXECUTABLE} submodule update --init -- ${path}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
OUTPUT_VARIABLE log
ERROR_VARIABLE log
RESULT_VARIABLE res
)
exec(${CMAKE_COMMAND} --build ${${package}_BINARY_DIR})
exec(${CMAKE_COMMAND} --build ${${package}_BINARY_DIR} --target install)
elseif(${package} STREQUAL FairCMakeModules)
set(${package}_INSTALL_DIR ${PROJECT_BINARY_DIR}/${bundle}_install)
file(MAKE_DIRECTORY ${${package}_INSTALL_DIR})
set(${package}_PREFIX ${${package}_INSTALL_DIR})
exec(${CMAKE_COMMAND} -S ${${package}_SOURCE_DIR} -B ${${package}_BINARY_DIR} -G ${CMAKE_GENERATOR}
-DCMAKE_INSTALL_PREFIX=${${package}_INSTALL_DIR}
)
exec(${CMAKE_COMMAND} --build ${${package}_BINARY_DIR})
exec(${CMAKE_COMMAND} --build ${${package}_BINARY_DIR} --target install)
elseif(${package} STREQUAL PicoSHA2)
set(${package}_PREFIX ${${package}_SOURCE_DIR})
file(GLOB content "${dir}/*")
endif()
set(${package}_ROOT ${${package}_PREFIX} CACHE PATH "Location of bundle package ${package}")
set(${package}_BUNDLED ON CACHE BOOL "Whether bundled ${package} was used")
message(STATUS "Building bundled ${package} - done")
if(NOT content)
message(FATAL_ERROR "The bundled package in ${path} is missing. "
"Run `git submodule update --init` in ${PROJECT_SOURCE_DIR}, "
"or install the package. ${log}")
endif()
endfunction()

View File

@@ -39,20 +39,41 @@ endif()
if(BUILD_FAIRMQ)
find_package2(PRIVATE ZeroMQ REQUIRED VERSION 4.1.4)
if(NOT PicoSHA2_BUNDLED)
build_bundled(PicoSHA2 extern/PicoSHA2)
endif()
# Header-only, always bundled
fairmq_init_submodule(extern/PicoSHA2)
set(PicoSHA2_ROOT ${PROJECT_SOURCE_DIR}/extern/PicoSHA2)
set(PicoSHA2_BUNDLED ON)
find_package2(BUNDLED PicoSHA2 REQUIRED)
set(PicoSHA2_VERSION "1.0.0")
set(PicoSHA2_PREFIX "<bundled>")
endif()
if(BUILD_TESTING)
if(NOT GTest_FOUND AND NOT GTest_BUNDLED AND NOT USE_EXTERNAL_GTEST)
build_bundled(GTest extern/googletest)
endif()
find_package2(BUNDLED GTest REQUIRED)
if(GTest_BUNDLED)
if(USE_EXTERNAL_GTEST)
find_package2(PRIVATE GTest REQUIRED)
else()
# Build the bundled GoogleTest as part of this build, with the same
# compiler and flags. OVERRIDE_FIND_PACKAGE lets find_package(GTest)
# below find it.
fairmq_init_submodule(extern/googletest)
include(FetchContent)
FetchContent_Declare(GTest
SOURCE_DIR ${PROJECT_SOURCE_DIR}/extern/googletest
OVERRIDE_FIND_PACKAGE
)
block()
set(BUILD_GMOCK OFF)
set(INSTALL_GTEST OFF)
set(BUILD_SHARED_LIBS OFF)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# Do not analyse third-party code
set(CMAKE_CXX_CLANG_TIDY "")
set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE "")
set(CMAKE_CXX_CPPLINT "")
FetchContent_MakeAvailable(GTest)
endblock()
set(GTest_BUNDLED ON)
find_package2(BUNDLED GTest REQUIRED)
set(GTest_VERSION "Dec 26 2024 @7d76a23")
set(GTest_PREFIX "<bundled>")
endif()

View File

@@ -10,7 +10,6 @@ include_guard(GLOBAL)
include(FairCMakeModules)
include(FairFindPackage2)
include(FairMQBundlePackageHelper)
function(generate_package_components)
list(JOIN PROJECT_PACKAGE_COMPONENTS " " COMPS)

View File

@@ -59,7 +59,7 @@ The error also occurs when the value `ON` is in the cache from an earlier config
| Boost | 1.66 or later, but not 1.88 | `BUILD_FAIRMQ` or `BUILD_TIDY_TOOL` | public |
| ZeroMQ | 4.1.4 or later | `BUILD_FAIRMQ` | private |
| PicoSHA2 | bundled | `BUILD_FAIRMQ` | bundled |
| GoogleTest | bundled | `BUILD_TESTING` | bundled, or public with `USE_EXTERNAL_GTEST` |
| GoogleTest | bundled | `BUILD_TESTING` | bundled, or private with `USE_EXTERNAL_GTEST` |
| LLVM, Clang, CLI11 | any | `BUILD_TIDY_TOOL` | private |
| FairCMakeModules | 0.2 or later | always | private, bundled if CMake does not find it |
@@ -69,7 +69,10 @@ The kinds have these effects:
- **Public**: the installed package records the dependency. A project that uses FairMQ must find the package too. See [The installed package](#the-installed-package).
- **Private**: only the FairMQ build uses the package.
- **Bundled**: the code is in a Git submodule in `extern/`. The configure step runs `git submodule update --init` for it. Then it builds GoogleTest and FairCMakeModules in the build directory under `extern/`. The log of this build is `build.log` in the same directory. PicoSHA2 is a header-only library.
- **Bundled**: the code is in a Git submodule in `extern/`. If the folder of the submodule is empty, the configure step runs `git submodule update --init` for it. If the folder stays empty, the configure step stops with an error.
- FairCMakeModules: the configure step installs the bundled copy into `extern/FairCMakeModules_install` in the build directory. The log is `extern/FairCMakeModules/build.log` in the build directory.
- GoogleTest: the build compiles the bundled copy as part of the FairMQ build, with `FetchContent`. Thus it uses the same compiler and flags. It is not installed.
- PicoSHA2: a header-only library.
Boost 1.88 replaced the Boost.Process API with version 2. With Boost 1.88, the configure step stops with an error. With Boost 1.89 or later, FairMQ uses the header `boost/process/v1.hpp`.
@@ -239,7 +242,7 @@ The package does not search its dependencies. Your project must find Boost, Fair
| `FairMQ_BUILD_TYPE` | The `CMAKE_BUILD_TYPE` of the FairMQ build. |
| `FairMQ_BUILD_TYPE_UPPER` | The same value in upper case. |
| `FairMQ_CXX_FLAGS` | `CMAKE_CXX_FLAGS` and `CMAKE_CXX_FLAGS_<BUILD_TYPE>` of the FairMQ build, as a list. |
| `FairMQ_PACKAGE_DEPENDENCIES` | The public dependencies: `Threads`, `FairLogger`, `Boost`, and the public dependencies that these packages declare. With `BUILD_TESTING` and `USE_EXTERNAL_GTEST`, also `GTest`. |
| `FairMQ_PACKAGE_DEPENDENCIES` | The public dependencies: `Threads`, `FairLogger`, `Boost`, and the public dependencies that these packages declare. |
| `FairMQ_<dep>_VERSION` | The minimum version of `<dep>` that the build required, for example `FairMQ_FairLogger_VERSION` is `1.6.0`. |
| `FairMQ_<dep>_COMPONENTS` | The components of `<dep>` that the build required, for example the five components in `FairMQ_Boost_COMPONENTS`. |
| `FairMQ_<component>_FOUND` | `TRUE` for each [component](#components) of the installation. |

View File

@@ -232,7 +232,7 @@ The CMake code of FairMQ uses `PROJECT_SOURCE_DIR` and `PROJECT_BINARY_DIR` for
Some things still depend on the Git repository of FairMQ:
- `cmake/GitHelper.cmake` reads the version with `git describe` in the directory of the FairMQ `CMakeLists.txt`, and only if it has a `.git` entry. Without it, the version is `0.0.0.0`.
- The bundled copies of GoogleTest, PicoSHA2 and FairCMakeModules are Git submodules. A source archive from GitHub has no `.git` folder and the folders in `extern/` are empty. If FairCMakeModules is not installed, the configure step stops when it tries to build the bundled copy: `git submodule update` fails with `fatal: not a git repository`. The log is `extern/FairCMakeModules/build.log` in the build directory. Use a Git clone with the tags (see [Install](/start/install/#problems-that-can-occur)) and its submodules.
- The bundled copies of GoogleTest, PicoSHA2 and FairCMakeModules are Git submodules. A source archive from GitHub has no `.git` folder and the folders in `extern/` are empty. If FairCMakeModules is not installed, the configure step stops with the error `The bundled package in extern/FairCMakeModules is missing`. Use a Git clone with the tags (see [Install](/start/install/#problems-that-can-occur)) and its submodules.
`FetchContent_MakeAvailable(FairMQ)` adds the downloaded source with `add_subdirectory()`. For a Git clone, use `GIT_SUBMODULES`. The FairMQ project did not test `FetchContent`.

View File

@@ -26,7 +26,7 @@ The CI of FairMQ builds and tests FairMQ on Linux (Ubuntu 24.04) with GCC 12, 13
| git | — | CMake reads the version of FairMQ from git and gets the bundled copies with git. |
| LLVM, Clang, [CLI11](https://github.com/CLIUtils/CLI11) | — | Only for the tool `fairmq-tidy`. |
The bundled copies are git submodules in the folder `extern/` of the repository. When CMake needs a bundled copy, it runs `git submodule update --init` for it.
The bundled copies are git submodules in the folder `extern/` of the repository. When CMake needs a bundled copy and its folder is empty, CMake runs `git submodule update --init` for it.
## Install FairMQ