diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md index d331360..beac84c 100644 --- a/.github/copilot-instructions.md +++ b/.github/copilot-instructions.md @@ -20,10 +20,10 @@ The consumer lifecycle is order-sensitive: 1. Load CPM and this toolkit, then call `cpp_library_enable_dependency_tracking()` **before `project()`**. It registers the provider through `CMAKE_PROJECT_TOP_LEVEL_INCLUDES`; CMake 3.24+ is required. -2. After `project()`, declare dependencies through `find_package()` or CPM. - Call `include(CTest)` at directory scope before `cpp_library_setup()` when +2. After `project()`, call `include(CTest)` at directory scope before `cpp_library_setup()` when supplying `TESTS` or `EXAMPLES`. Setup requires `DESCRIPTION`, `NAMESPACE`, - and `HEADERS`. + and `HEADERS`. Declare dependencies through `find_package()` or CPM after + `cpp_library_setup()`. 3. Setup creates the library target and optional installation rules. Only top-level projects proceed to template generation, tests, examples, and docs. `BUILD_TESTING` gates both tests and examples; `BUILD_DOCS` gates documentation. @@ -104,7 +104,8 @@ cmake --build --preset docs ``` The `clang-tidy` preset requires clang-tidy and runs analysis during compilation. -These presets come from `templates/CMakePresets.json` and apply to consumers. +These presets come from `templates/CMakePresets.json.in` and apply to consumers. +Source-build presets disable the resolved installation option; `install` enables it. `cmake --preset init` enables `CPP_LIBRARY_FORCE_INIT` and overwrites generated consumer templates. diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1287484..64d714b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -290,7 +290,7 @@ jobs: - name: Validate template files run: | # Check that all template files exist - test -f templates/CMakePresets.json + test -f templates/CMakePresets.json.in test -f templates/Config.cmake.in test -f templates/Doxyfile.in test -f templates/custom.css diff --git a/README.md b/README.md index 19b2353..b4e9b6d 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Modern CMake template for C++ libraries with comprehensive infrastructure. - **Testing**: Integrated [doctest](https://github.com/doctest/doctest) with CTest and compile-fail test support - **Documentation**: [Doxygen](https://www.doxygen.nl/) with [doxygen-awesome-css](https://github.com/jothepro/doxygen-awesome-css) theme - **Development Tools**: [clangd](https://clangd.llvm.org/) integration, CMakePresets.json, [clang-tidy](https://clang.llvm.org/extra/clang-tidy/) support -- **CI/CD**: [GitHub Actions](https://docs.github.com/en/actions) workflows with multi-platform testing and installation verification +- **CI/CD**: [GitHub Actions](https://docs.github.com/en/actions) workflows with multi-platform source builds and testing - **Dependency Management**: [CPM.cmake](https://github.com/cpm-cmake/CPM.cmake) integration ## Quick reference @@ -31,9 +31,12 @@ Use this order in your top-level `CMakeLists.txt`: | --- | --- | | 1 | Load CPM and cpp-library, as shown in [Manual Setup](#manual-setup). | | 2 | Call [`cpp_library_enable_dependency_tracking()`](#cpp_library_enable_dependency_tracking) **before `project()`**. | -| 3 | Call `project(... LANGUAGES CXX)`, then declare dependencies with `CPMAddPackage()` or `find_package()`. | +| 3 | Call `project(... LANGUAGES CXX)`. | | 4 | Call `include(CTest)` at directory scope to define `BUILD_TESTING`. | | 5 | Call [`cpp_library_setup()`](#cpp_library_setup), using the [file-resolution conventions](#path-conventions). | +| 6 | Declare dependencies with `CPMAddPackage()` or `find_package()`, then link them to your library. | + +Declare dependencies after `cpp_library_setup()`. For custom target setup, see [`cpp_library_set_version()`](#cpp_library_set_version). For non-namespaced dependency targets, see [dependency mapping](#non-namespaced-target-error). @@ -223,7 +226,11 @@ against dependencies fetched from source in the same build is not supported. Source consumption requires no special build/install-interface wrappers. CMake checks installation exports during configuration/generation, before `cmake --install` runs. For source-only builds without installed dependencies, -disable the component's installation option. +disable the component's installation option. The generated `default`, `test`, +`docs`, `clang-tidy`, and `init` presets do this automatically; the `install` +preset enables it. Presets use your package's installation-option name, including +a custom `INSTALL_OPTION`. Configuring without a preset preserves the +`PROJECT_IS_TOP_LEVEL` default. If system-wide deployment or a package manager requires installation, use: @@ -244,10 +251,18 @@ rather than falling back to source. It does not verify your project's installed package config or dependency closure. See the [CPM.cmake documentation](https://github.com/cpm-cmake/CPM.cmake#cpm_use_local_packages). -The generated CI package check verifies only that the root package's -`Config.cmake`, `ConfigVersion.cmake`, and `Targets.cmake` files exist and appear in -the install manifest. It does not load the package or check that its dependencies -are installed. Report installation failures if you encounter them. +Generated CI builds and tests from source; it does not test installation. +If you want installation testing, provide a separate workflow that prepares +dependencies and installs your package. No installation-specific changes to your +`CMakeLists.txt` are required. See [Test installation in your own workflow](docs/installation-testing.md) +for a cel-euclid example with GLM and the CEL packages. + +cpp-library generates `cpp-library-test-package.cmake` in your build directory +for optional artifact checking. It checks that the root package's `Config.cmake`, +`ConfigVersion.cmake`, and `Targets.cmake` files exist and appear in the install +manifest. It does not load the package or check its dependency closure. +To update existing generated CI and presets, use the +[template regeneration procedure](#updating-cpp-library). **Controlling installation**: The uppercase `${NAMESPACE}_INSTALL` option controls whether installation is enabled (defaults to `PROJECT_IS_TOP_LEVEL`). Use `-D${NAMESPACE}_INSTALL=ON/OFF` to override: diff --git a/cmake/cpp-library-install.cmake b/cmake/cpp-library-install.cmake index a3603cd..16b6d8c 100644 --- a/cmake/cpp-library-install.cmake +++ b/cmake/cpp-library-install.cmake @@ -443,6 +443,29 @@ function(_cpp_library_deferred_generate_config TARGET_NAME) message(STATUS "cpp-library: Generated ${ARG_PACKAGE_NAME}Config.cmake with dependencies") endfunction() +# Rejects setup after a descendant project using the same installation switch. +# Repeated setup in one project is allowed; ancestor marking takes O(directory depth). +function(_cpp_library_check_install_order INSTALL_OPTION) + set(property "_CPP_LIBRARY_INSTALL_ORDER_${INSTALL_OPTION}") + get_property(already_setup DIRECTORY "${PROJECT_SOURCE_DIR}" PROPERTY "${property}_SETUP") + if(already_setup) + return() + endif() + get_property(descendant DIRECTORY "${PROJECT_SOURCE_DIR}" PROPERTY "${property}_DESCENDANT") + if(NOT "${descendant}" STREQUAL "") + message(FATAL_ERROR + "cpp-library: ${descendant} called cpp_library_setup() before ${PROJECT_NAME}; " + "both use ${INSTALL_OPTION}. " + "Call cpp_library_setup() before loading dependencies in ${PROJECT_NAME}.") + endif() + set_property(DIRECTORY "${PROJECT_SOURCE_DIR}" PROPERTY "${property}_SETUP" TRUE) + get_directory_property(parent DIRECTORY "${PROJECT_SOURCE_DIR}" PARENT_DIRECTORY) + while(parent) + set_property(DIRECTORY "${parent}" PROPERTY "${property}_DESCENDANT" "${PROJECT_NAME}") + get_directory_property(parent DIRECTORY "${parent}" PARENT_DIRECTORY) + endwhile() +endfunction() + # Configures CMake install rules for library target and package config files. # - Precondition: NAME, PACKAGE_NAME, VERSION, and NAMESPACE specified; target NAME exists # - Postcondition: install rules created for target, config files, and export with NAMESPACE:: prefix @@ -489,6 +512,8 @@ function(_cpp_library_setup_install) string(TOUPPER "${ARG_NAMESPACE}" NAMESPACE_UPPER) set(install_option "${NAMESPACE_UPPER}_INSTALL") endif() + _cpp_library_check_install_order("${install_option}") + set_property(TARGET "${ARG_NAME}" PROPERTY _CPP_LIBRARY_INSTALL_OPTION "${install_option}") option(${install_option} "Enable installation of ${ARG_PACKAGE_NAME}" ${PROJECT_IS_TOP_LEVEL}) # Check if installation is enabled diff --git a/cmake/cpp-library-setup.cmake b/cmake/cpp-library-setup.cmake index 2a4cb9f..fabecae 100644 --- a/cmake/cpp-library-setup.cmake +++ b/cmake/cpp-library-setup.cmake @@ -148,13 +148,16 @@ function(_cpp_library_setup_core) endfunction() -# Copies template files (.clang-format, .gitignore, etc.) to project root if not present. -# - Precondition: PACKAGE_NAME must be passed as first parameter +# Copies static templates and configures project-specific presets and CI if missing. +# - Precondition: PACKAGE_NAME and INSTALL_OPTION identify the current project's package and switch # - Postcondition: missing template files copied to project, CI workflow configured with PACKAGE_NAME substitution # - With FORCE_INIT: overwrites existing files function(_cpp_library_copy_templates PACKAGE_NAME) set(options FORCE_INIT) - cmake_parse_arguments(ARG "${options}" "" "" ${ARGN}) + cmake_parse_arguments(ARG "${options}" "INSTALL_OPTION" "" ${ARGN}) + if(NOT ARG_INSTALL_OPTION) + message(FATAL_ERROR "_cpp_library_copy_templates: INSTALL_OPTION is required") + endif() # List of static template files to copy set(TEMPLATE_FILES @@ -165,7 +168,6 @@ function(_cpp_library_copy_templates PACKAGE_NAME) ".vscode/settings.json" ".vscode/tasks.json" "docs/index.html" - "CMakePresets.json" ) foreach(template_file IN LISTS TEMPLATE_FILES) @@ -181,6 +183,14 @@ function(_cpp_library_copy_templates PACKAGE_NAME) message(WARNING "Template file not found: ${source_file}") endif() endforeach() + + set(preset_dest "${CMAKE_CURRENT_SOURCE_DIR}/CMakePresets.json") + if(NOT EXISTS "${preset_dest}" OR ARG_FORCE_INIT) + set(CPP_LIBRARY_INSTALL_OPTION "${ARG_INSTALL_OPTION}") + configure_file("${CPP_LIBRARY_ROOT}/templates/CMakePresets.json.in" + "${preset_dest}" @ONLY) + message(STATUS "Configured template file: CMakePresets.json") + endif() # Setup CI workflow with PACKAGE_NAME substitution _cpp_library_setup_ci("${PACKAGE_NAME}" ${ARG_FORCE_INIT}) diff --git a/cpp-library.cmake b/cpp-library.cmake index 101132b..8ee71ff 100644 --- a/cpp-library.cmake +++ b/cpp-library.cmake @@ -336,11 +336,12 @@ function(cpp_library_setup) _cpp_library_setup_package_test("${PACKAGE_NAME}") - # Copy static template files (like .clang-format, .gitignore, CMakePresets.json, etc.) + # Copy static templates and configure project-specific presets and CI. + get_target_property(install_option "${ARG_NAME}" _CPP_LIBRARY_INSTALL_OPTION) if(DEFINED CPP_LIBRARY_FORCE_INIT AND CPP_LIBRARY_FORCE_INIT) - _cpp_library_copy_templates("${PACKAGE_NAME}" FORCE_INIT) + _cpp_library_copy_templates("${PACKAGE_NAME}" INSTALL_OPTION "${install_option}" FORCE_INIT) else() - _cpp_library_copy_templates("${PACKAGE_NAME}") + _cpp_library_copy_templates("${PACKAGE_NAME}" INSTALL_OPTION "${install_option}") endif() # Download doctest if we'll need it for tests or examples diff --git a/docs/examples/cel-euclid-install.yml b/docs/examples/cel-euclid-install.yml new file mode 100644 index 0000000..8c7b835 --- /dev/null +++ b/docs/examples/cel-euclid-install.yml @@ -0,0 +1,83 @@ +# Copy into cel-euclid's .github/workflows/install.yml. +name: Test installation + +on: + workflow_dispatch: + +permissions: + contents: read + +jobs: + install: + runs-on: ubuntu-latest + defaults: + run: + shell: bash + env: + INSTALL_PREFIX: ${{ runner.temp }}/cel-install + CMAKE_BUILD_PARALLEL_LEVEL: 2 + + steps: + # [DEPENDENCY] https://github.com/actions/checkout/releases + - uses: actions/checkout@v7 + with: + fetch-depth: 0 + + - name: Check out GLM 1.0.3 + uses: actions/checkout@v7 + with: + repository: g-truc/glm + ref: 8d1fd52e5ab5590e2c81768ace50c72bae28f2ed + path: .install-deps/glm + + - name: Check out cel-runtime 1.1.2 + uses: actions/checkout@v7 + with: + repository: stlab/cel-runtime + ref: a819042cfa3fab8ede82052518f2b7f5175af5ad + path: .install-deps/cel-runtime + fetch-depth: 0 + + - name: Check out cel-parser 1.1.1 + uses: actions/checkout@v7 + with: + repository: stlab/cel-parser + ref: 3ba7e1990551817427872b9dc88d7ebed32b9cf8 + path: .install-deps/cel-parser + fetch-depth: 0 + + - name: Install GLM + run: | + cmake -S .install-deps/glm -B "$RUNNER_TEMP/build-glm" -G Ninja \ + -DCMAKE_BUILD_TYPE=Release \ + -DGLM_BUILD_TESTS=OFF -DGLM_BUILD_INSTALL=ON \ + "-DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX" + cmake --build "$RUNNER_TEMP/build-glm" + cmake --install "$RUNNER_TEMP/build-glm" + + - name: Install CEL dependencies + run: | + for package in cel-runtime cel-parser; do + cmake -S ".install-deps/$package" \ + -B "$RUNNER_TEMP/build-$package" -G Ninja \ + -DCMAKE_BUILD_TYPE=Release \ + -DBUILD_TESTING=OFF -DCEL_INSTALL=ON \ + -DCPM_USE_LOCAL_PACKAGES=ON \ + "-DCMAKE_PREFIX_PATH=$INSTALL_PREFIX" \ + "-DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX" + cmake --build "$RUNNER_TEMP/build-$package" + cmake --install "$RUNNER_TEMP/build-$package" + done + + - name: Build and install cel-euclid + run: | + cmake --preset install -DCEL_INSTALL=ON \ + "-DCMAKE_PREFIX_PATH=$INSTALL_PREFIX" \ + "-DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX" + cmake --build --preset install + cmake --install build/install + + - name: Check installed package artifacts + run: | + cmake "-DPACKAGE_TEST_PREFIX=$INSTALL_PREFIX" \ + -P build/install/cpp-library-test-package.cmake diff --git a/docs/installation-testing.md b/docs/installation-testing.md new file mode 100644 index 0000000..4eb189b --- /dev/null +++ b/docs/installation-testing.md @@ -0,0 +1,59 @@ +# Test installation in your own workflow + +If you maintain a library that needs installation testing, provide a separate +GitHub Actions workflow. Generated cpp-library CI tests source builds, not +installation. You do not need installation-specific dependency declarations +in your library's `CMakeLists.txt`. + +## Before you begin + +Declare dependencies after `cpp_library_setup()`. Generate your consumer +presets and CI with cpp-library, and use ordinary `target_link_libraries()` +calls for dependencies. + +Identify every required dependency, its version, and how to install it +independently. Source locations alone do not describe installation recipes. +Dependencies must be available as installed packages before you configure +your library for installation. + +## Add a workflow + +Copy the [cel-euclid installation workflow](examples/cel-euclid-install.yml) to +`.github/workflows/install.yml` in cel-euclid. For another library, replace the +dependency checkouts, their installation commands, and the installation-option +name with those for your project. + +The example runs on manual dispatch and performs these steps: + +1. Check out the client and pinned dependency sources. Fetch tags for CEL + packages so cpp-library discovers their release versions. +2. Build and install GLM 1.0.3 in its own build with `GLM_BUILD_INSTALL=ON`. +3. Build and install cel-runtime 1.1.2, then cel-parser 1.1.1, in separate builds. +4. Configure cel-euclid with its `install` preset and the installation prefix + in `CMAKE_PREFIX_PATH`, then build and install it. +5. Run the existing cpp-library package-artifact check. + +The GLM-specific setting belongs in GLM's installation command, not the +client's `CPMAddPackage()` declaration. The workflow accounts for cel-runtime +and cel-parser as well as GLM: all required packages must be installed even +when only GLM is maintained outside the CEL project. + +Run the workflow from your repository's **Actions** tab. During configuration, +confirm that CPM reports local packages for the installed dependencies rather +than fetching them into the client's build. If it fetches one, check the +installed version and `CMAKE_PREFIX_PATH`. + +`CPM_USE_LOCAL_PACKAGES=ON` prefers installed packages; it does not prohibit +source fallback. The workflow still fetches cpp-library itself through CPM. +Do not set `CPM_LOCAL_PACKAGES_ONLY` globally unless you also arrange for that +toolkit dependency to be satisfied. + +## Check the result + +The final step prints `Installed cel-euclid artifacts found` with the package +directory. A missing package artifact or an artifact absent from the install +manifest fails the check. + +This verifies the root package's installation artifacts only. It does not +compile an installed consumer or guarantee that downstream dependency +resolution succeeds. diff --git a/templates/.github/workflows/ci.yml.in b/templates/.github/workflows/ci.yml.in index 2a1f00c..4b96604 100644 --- a/templates/.github/workflows/ci.yml.in +++ b/templates/.github/workflows/ci.yml.in @@ -57,27 +57,6 @@ jobs: - name: Test run: ctest --preset=test - - name: Build and Install - run: | - cmake --preset=default - cmake --build --preset=default - cmake --install build/default --prefix "${{ runner.temp }}/install" - env: - CC: ${{ matrix.cc }} - CXX: ${{ matrix.cxx }} - if: ${{ matrix.cc }} - - - name: Build and Install - run: | - cmake --preset=default - cmake --build --preset=default - cmake --install build/default --prefix "${{ runner.temp }}/install" - if: ${{ !matrix.cc }} - - - name: Check installed package artifacts - run: | - cmake "-DPACKAGE_TEST_PREFIX=${{ runner.temp }}/install" -P build/default/cpp-library-test-package.cmake - clang-tidy: runs-on: ubuntu-latest diff --git a/templates/CMakePresets.json b/templates/CMakePresets.json.in similarity index 93% rename from templates/CMakePresets.json rename to templates/CMakePresets.json.in index f6d7e7a..1c6166d 100644 --- a/templates/CMakePresets.json +++ b/templates/CMakePresets.json.in @@ -8,6 +8,7 @@ "binaryDir": "${sourceDir}/build/default", "generator": "Ninja", "cacheVariables": { + "@CPP_LIBRARY_INSTALL_OPTION@": "OFF", "CMAKE_BUILD_TYPE": "Release", "BUILD_TESTING": "OFF", "CMAKE_EXPORT_COMPILE_COMMANDS": "ON", @@ -22,6 +23,7 @@ "binaryDir": "${sourceDir}/build/test", "generator": "Ninja", "cacheVariables": { + "@CPP_LIBRARY_INSTALL_OPTION@": "OFF", "CMAKE_BUILD_TYPE": "Debug", "BUILD_TESTING": "ON", "CMAKE_EXPORT_COMPILE_COMMANDS": "ON", @@ -36,6 +38,7 @@ "binaryDir": "${sourceDir}/build/docs", "generator": "Ninja", "cacheVariables": { + "@CPP_LIBRARY_INSTALL_OPTION@": "OFF", "CMAKE_BUILD_TYPE": "Release", "BUILD_TESTING": "OFF", "BUILD_DOCS": "ON", @@ -51,6 +54,7 @@ "binaryDir": "${sourceDir}/build/clang-tidy", "generator": "Ninja", "cacheVariables": { + "@CPP_LIBRARY_INSTALL_OPTION@": "OFF", "CMAKE_BUILD_TYPE": "Debug", "BUILD_TESTING": "ON", "CMAKE_EXPORT_COMPILE_COMMANDS": "ON", @@ -66,6 +70,7 @@ "binaryDir": "${sourceDir}/build/init", "generator": "Ninja", "cacheVariables": { + "@CPP_LIBRARY_INSTALL_OPTION@": "OFF", "CMAKE_BUILD_TYPE": "Release", "BUILD_TESTING": "OFF", "CMAKE_EXPORT_COMPILE_COMMANDS": "ON", @@ -81,6 +86,7 @@ "binaryDir": "${sourceDir}/build/install", "generator": "Ninja", "cacheVariables": { + "@CPP_LIBRARY_INSTALL_OPTION@": "ON", "CMAKE_BUILD_TYPE": "Release", "BUILD_TESTING": "OFF", "CMAKE_EXPORT_COMPILE_COMMANDS": "ON", diff --git a/tests/install/fixtures/nested/CMakeLists.txt b/tests/install/fixtures/nested/CMakeLists.txt index 3fd5d5f..0f4032e 100644 --- a/tests/install/fixtures/nested/CMakeLists.txt +++ b/tests/install/fixtures/nested/CMakeLists.txt @@ -28,16 +28,27 @@ elseif(COLLIDING_LICENSE_FILES) elseif(DUPLICATE_LICENSE_FILES) list(APPEND license_args LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE") endif() +if(NOT DEFINED LEAF_SOURCE_DIR) + set(LEAF_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/leaf") +endif() +set(leaf_args "${LEAF_SOURCE_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/leaf") +if(EXCLUDE_LEAF_FROM_ALL) + list(APPEND leaf_args EXCLUDE_FROM_ALL) +endif() +if(DEPENDENCIES_FIRST) + add_subdirectory(${leaf_args}) + if(SIBLING_PROJECT) + add_subdirectory("${LEAF_SOURCE_DIR}/extra" sibling) + endif() +endif() cpp_library_setup( DESCRIPTION "Parent package fixture" NAMESPACE stlab HEADERS parent.hpp ${license_args} ${install_args}) -if(EXCLUDE_LEAF_FROM_ALL) - add_subdirectory(leaf EXCLUDE_FROM_ALL) -else() - add_subdirectory(leaf) +if(NOT DEPENDENCIES_FIRST) + add_subdirectory(${leaf_args}) endif() if(INVALID_PACKAGE STREQUAL "parent") add_library(UnknownParent::dep INTERFACE IMPORTED) diff --git a/tests/install/fixtures/single/CMakeLists.txt b/tests/install/fixtures/single/CMakeLists.txt index ea7788d..9f471ab 100644 --- a/tests/install/fixtures/single/CMakeLists.txt +++ b/tests/install/fixtures/single/CMakeLists.txt @@ -3,10 +3,20 @@ include("${TOOLKIT_SOURCE}/cpp-library.cmake") cpp_library_enable_dependency_tracking() project(single LANGUAGES CXX) set(CPP_LIBRARY_VERSION 1.0.0) +set(install_args) +if(CUSTOM_INSTALL_OPTION) + list(APPEND install_args INSTALL_OPTION "${CUSTOM_INSTALL_OPTION}") +endif() cpp_library_setup( DESCRIPTION "Dependency-free packaging fixture" NAMESPACE stlab - HEADERS single.hpp) + HEADERS single.hpp + ${install_args}) +if(SOURCE_ONLY_DEPENDENCY) + add_library(source_only_dependency INTERFACE) + cpp_library_map_dependency(source_only_dependency "SourceOnlyDependency 1.0.0") + target_link_libraries(single INTERFACE source_only_dependency) +endif() if(EXTERNAL_PACKAGE) find_package(stlab-extra 1.0.0 CONFIG REQUIRED) target_link_libraries(single INTERFACE stlab::extra) diff --git a/tests/install/test_nested_install.cmake b/tests/install/test_nested_install.cmake index 52a5643..62e1c57 100644 --- a/tests/install/test_nested_install.cmake +++ b/tests/install/test_nested_install.cmake @@ -183,9 +183,62 @@ function(run_invalid_case invalid clean target) message(STATUS "PASS invalid-${invalid}: isolated '${diagnostic}' / '${target}'") endfunction() +# Checks setup ordering without adding installation or consumer tests. +function(run_order_case name) + set(case_root "${test_root}/${name}") + set(source "${case_root}/source") + set(binary "${case_root}/build") + file(MAKE_DIRECTORY "${source}") + file(COPY "${CMAKE_CURRENT_LIST_DIR}/fixtures/nested/" DESTINATION "${source}") + set(args -DDEPENDENCIES_FIRST=ON -DLEGACY_OPTIONS=ON) + set(expect_failure TRUE) + set(attempts 1) + if(name STREQUAL "order-explicit-on") + list(APPEND args -DSTLAB_INSTALL=ON) + elseif(name STREQUAL "order-explicit-off") + list(APPEND args -DSTLAB_INSTALL=OFF) + elseif(name STREQUAL "order-reconfigure") + set(attempts 2) + elseif(name STREQUAL "order-custom" OR name STREQUAL "order-siblings") + set(args -DDEPENDENCIES_FIRST=ON) + set(expect_failure FALSE) + if(name STREQUAL "order-siblings") + list(APPEND args -DSIBLING_PROJECT=ON) + endif() + elseif(name STREQUAL "order-external-source") + file(RENAME "${source}/leaf" "${case_root}/external-leaf") + list(APPEND args "-DLEAF_SOURCE_DIR=${case_root}/external-leaf") + endif() + foreach(attempt RANGE 1 ${attempts}) + execute_process( + COMMAND "${CMAKE_COMMAND}" -S "${source}" -B "${binary}" -G Ninja + "-DTOOLKIT_SOURCE=${toolkit_source}" ${args} + RESULT_VARIABLE result OUTPUT_VARIABLE out ERROR_VARIABLE err) + set(output "${out}\n${err}") + if(expect_failure) + if(result EQUAL 0) + message(FATAL_ERROR "${name}: reversed shared-option setup order was accepted") + endif() + string(REGEX REPLACE "[ \r\n]+" " " output "${output}") + foreach(part IN ITEMS "parent" "leaf" "STLAB_INSTALL" + "Call cpp_library_setup() before loading dependencies") + string(FIND "${output}" "${part}" found) + if(found LESS 0) + message(FATAL_ERROR "${name}: missing '${part}' in diagnostic:\n${output}") + endif() + endforeach() + elseif(NOT result EQUAL 0) + message(FATAL_ERROR "${name}: independent projects rejected:\n${output}") + endif() + endforeach() + message(STATUS "PASS ${name}: setup ordering") +endfunction() + # Separate child script invocations let all expected RED failures be inspected in one run. if(DEFINED CASE) - if(CASE STREQUAL "both") + if(CASE MATCHES "^order-") + run_order_case("${CASE}") + elseif(CASE STREQUAL "both") run_case(both TRUE TRUE OFF -DSTLAB_PARENT_INSTALL=ON -DSTLAB_LEAF_INSTALL=ON -DLEGACY_OFF=ON) elseif(CASE STREQUAL "both-shared") run_case(both-shared TRUE TRUE ON -DSTLAB_PARENT_INSTALL=ON -DSTLAB_LEAF_INSTALL=ON) @@ -251,7 +304,9 @@ if(DEFINED CASE) return() endif() -foreach(case both both-shared parent-only leaf-only custom-defaults legacy-on legacy-off legacy-defaults +foreach(case order-default order-explicit-on order-explicit-off order-reconfigure + order-custom order-siblings order-external-source + both both-shared parent-only leaf-only custom-defaults legacy-on legacy-off legacy-defaults custom-datadir no-licenses no-leaf-license disabled-missing-license duplicate-license-files colliding-license-files invalid-parent invalid-leaf missing-option missing-license empty-license-files) diff --git a/tests/install/test_package_ci.cmake b/tests/install/test_package_ci.cmake index ec4973f..a4e96bc 100644 --- a/tests/install/test_package_ci.cmake +++ b/tests/install/test_package_ci.cmake @@ -25,29 +25,6 @@ function(expect_check_failure check prefix missing_file) endif() endfunction() -function(run_workflow_install workflow binary prefix) - file(READ "${workflow}" workflow_text) - string(REGEX MATCH "cmake --install build/default --prefix [^\n]*" - install_command "${workflow_text}") - if(NOT install_command) - message(FATAL_ERROR "Generated workflow has no install command: ${workflow}") - endif() - string(REPLACE "build/default" "\"${binary}\"" install_command "${install_command}") - string(REPLACE "\${{ runner.temp }}/install" "${prefix}" install_command "${install_command}") - - if(WIN32) - find_program(shell NAMES pwsh powershell REQUIRED) - execute_process(COMMAND "${shell}" -NoProfile -Command "${install_command}" - RESULT_VARIABLE result OUTPUT_VARIABLE out ERROR_VARIABLE err) - else() - execute_process(COMMAND /bin/sh -c "${install_command}" - RESULT_VARIABLE result OUTPUT_VARIABLE out ERROR_VARIABLE err) - endif() - if(NOT result EQUAL 0) - message(FATAL_ERROR "Generated workflow install failed (${result}):\n${install_command}\n${out}\n${err}") - endif() -endfunction() - function(run_single) set(root "${test_root}/single") set(source "${root}/source") @@ -72,7 +49,7 @@ function(run_single) expect_check_failure("${check}" "${prefix}" "Package install prefix does not exist") set(workflow_prefix "${root}/./install prefix") cmake_path(NATIVE_PATH workflow_prefix workflow_prefix) - run_workflow_install("${workflow}" "${binary}" "${workflow_prefix}") + run_command("${CMAKE_COMMAND}" --install "${binary}" --prefix "${workflow_prefix}") run_command("${CMAKE_COMMAND}" "-DPACKAGE_TEST_PREFIX=${prefix}" -P "${check}") set(package_dir "lib64/cmake/stlab-single") @@ -89,6 +66,81 @@ function(run_single) message(STATUS "PASS single: installed package artifacts are checked") endfunction() +# Exercises source-only presets and preserves consumer-owned template changes. +function(run_source_presets custom) + set(root "${test_root}/presets-${custom}") + set(source "${root}/source") + set(binary "${root}/bootstrap") + set(install_option STLAB_INSTALL) + set(args) + if(custom) + set(install_option SINGLE_PACKAGE_INSTALL) + list(APPEND args "-DCUSTOM_INSTALL_OPTION=${install_option}") + endif() + file(MAKE_DIRECTORY "${source}") + file(COPY "${CMAKE_CURRENT_LIST_DIR}/fixtures/single/" DESTINATION "${source}") + run_command("${CMAKE_COMMAND}" -S "${source}" -B "${binary}" -G Ninja + "-DTOOLKIT_SOURCE=${toolkit_source}" "-D${install_option}=OFF" ${args}) + + foreach(preset default test) + run_command("${CMAKE_COMMAND}" -E chdir "${source}" + "${CMAKE_COMMAND}" --preset "${preset}" "-DTOOLKIT_SOURCE=${toolkit_source}" + -DSOURCE_ONLY_DEPENDENCY=ON ${args}) + run_command("${CMAKE_COMMAND}" --build "${source}/build/${preset}") + file(READ "${source}/build/${preset}/cmake_install.cmake" install_script) + if(install_script MATCHES "stlab-single") + message(FATAL_ERROR "${preset}: source-only preset registered package installation") + endif() + endforeach() + + set(presets "${source}/CMakePresets.json") + set(workflow "${source}/.github/workflows/ci.yml") + file(READ "${presets}" original_presets) + string(JSON count LENGTH "${original_presets}" configurePresets) + math(EXPR last "${count} - 1") + foreach(index RANGE 0 ${last}) + string(JSON name GET "${original_presets}" configurePresets ${index} name) + string(JSON value GET "${original_presets}" configurePresets ${index} + cacheVariables "${install_option}") + set(expected OFF) + if(name STREQUAL "install") + set(expected ON) + endif() + if(NOT value STREQUAL expected) + message(FATAL_ERROR "${name}: expected ${install_option}=${expected}, got ${value}") + endif() + endforeach() + file(READ "${workflow}" original_workflow) + if(original_workflow MATCHES "cmake --install|cpp-library-test-package") + message(FATAL_ERROR "Generated CI still installs or checks package artifacts") + endif() + + file(APPEND "${presets}" "\n ") + file(APPEND "${workflow}" "\n# consumer-owned workflow\n") + run_command("${CMAKE_COMMAND}" -S "${source}" -B "${binary}") + file(READ "${presets}" preserved_presets) + file(READ "${workflow}" preserved_workflow) + if(NOT preserved_presets STREQUAL "${original_presets}\n " + OR NOT preserved_workflow STREQUAL "${original_workflow}\n# consumer-owned workflow\n") + message(FATAL_ERROR "Consumer templates changed without force initialization") + endif() + run_command("${CMAKE_COMMAND}" -E chdir "${source}" + "${CMAKE_COMMAND}" --preset init "-DTOOLKIT_SOURCE=${toolkit_source}" ${args}) + file(READ "${presets}" regenerated_presets) + file(READ "${workflow}" regenerated_workflow) + if(NOT regenerated_presets STREQUAL original_presets + OR NOT regenerated_workflow STREQUAL original_workflow) + message(FATAL_ERROR "Force initialization did not regenerate consumer templates") + endif() + run_command("${CMAKE_COMMAND}" -E chdir "${source}" + "${CMAKE_COMMAND}" --preset install "-DTOOLKIT_SOURCE=${toolkit_source}" ${args}) + run_command("${CMAKE_COMMAND}" --install "${source}/build/install" + --prefix "${root}/install") + run_command("${CMAKE_COMMAND}" "-DPACKAGE_TEST_PREFIX=${root}/install" + -P "${source}/build/install/cpp-library-test-package.cmake") + message(STATUS "PASS presets-${custom}: source-only builds and explicit installation") +endfunction() + function(run_absolute_libdir) set(root "${test_root}/absolute libdir") set(source "${root}/source") @@ -181,6 +233,8 @@ elseif(CASE STREQUAL "absolute") elseif(DEFINED CASE) message(FATAL_ERROR "Unknown case: ${CASE}") else() + run_source_presets(FALSE) + run_source_presets(TRUE) run_single() run_absolute_libdir() run_function_scope()