From 3628a32d864520e6ed208c056fe51607b37ec4b1 Mon Sep 17 00:00:00 2001 From: sean-parent Date: Sat, 14 Nov 2026 11:29:35 -0800 Subject: [PATCH 1/4] fix: make generated packaging checks dependency-aware Stage enabled directory-local install rules in an isolated test prefix, including dependencies excluded from parent installs. Compile, link, and run a consumer using the staged package while preserving external dependency prefixes and normal installation behavior. Cover excluded and transitive dependencies, static/shared libraries, multi-config Debug, disabled installs, external prefix lists, and stale-installation false positives. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .github/workflows/ci.yml | 6 + cmake/cpp-library-ci.cmake | 47 ++++++ cpp-library.cmake | 2 + templates/.github/workflows/ci.yml.in | 21 +-- templates/test-package.cmake.in | 82 ++++++++++ tests/install/fixtures/nested/CMakeLists.txt | 6 +- .../fixtures/nested/leaf/CMakeLists.txt | 5 + .../fixtures/nested/leaf/extra/CMakeLists.txt | 9 ++ .../nested/leaf/extra/include/stlab/extra.hpp | 5 + tests/install/fixtures/single/CMakeLists.txt | 13 ++ .../fixtures/single/include/stlab/single.hpp | 5 + tests/install/test_package_ci.cmake | 145 ++++++++++++++++++ 12 files changed, 325 insertions(+), 21 deletions(-) create mode 100644 templates/test-package.cmake.in create mode 100644 tests/install/fixtures/nested/leaf/extra/CMakeLists.txt create mode 100644 tests/install/fixtures/nested/leaf/extra/include/stlab/extra.hpp create mode 100644 tests/install/fixtures/single/CMakeLists.txt create mode 100644 tests/install/fixtures/single/include/stlab/single.hpp create mode 100644 tests/install/test_package_ci.cmake diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6b61e62..efb3913 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -26,6 +26,9 @@ jobs: - name: Run nested installation round-trip tests run: cmake -P tests/install/test_nested_install.cmake + - name: Run generated packaging checks + run: cmake -P tests/install/test_package_ci.cmake + - name: Run target type tests run: cmake -P tests/setup/test_target_type.cmake @@ -53,6 +56,9 @@ jobs: - name: Run nested static and shared installation round trips run: cmake -P tests/install/test_nested_install.cmake + - name: Run generated packaging checks + run: cmake -P tests/install/test_package_ci.cmake + clangd: name: clangd (${{ matrix.os }}) strategy: diff --git a/cmake/cpp-library-ci.cmake b/cmake/cpp-library-ci.cmake index 348159a..e23c76a 100644 --- a/cmake/cpp-library-ci.cmake +++ b/cmake/cpp-library-ci.cmake @@ -37,3 +37,50 @@ function(_cpp_library_setup_ci PACKAGE_NAME force_init) message(WARNING "CI template file not found: ${ci_template}") endif() endfunction() + +# Writes directory-local installation commands in dependency-first directory order. +# Complexity: linear in the configured directory tree and emitted path lengths. +function(_cpp_library_write_package_installs destination binary_dir) + get_property(children DIRECTORY "${binary_dir}" PROPERTY SUBDIRECTORIES) + foreach(child IN LISTS children) + get_property(child_binary DIRECTORY "${child}" PROPERTY BINARY_DIR) + _cpp_library_write_package_installs("${destination}" "${child_binary}") + endforeach() + file(APPEND "${destination}" "package_test_install([[${binary_dir}]])\n") +endfunction() + +# Generates an isolated installed-consumer check after the project's directories are configured. +# Complexity: linear in directory paths, plus O(headers * header bases) path comparisons. +function(_cpp_library_generate_package_test target package namespace) + set(PACKAGE_TEST_INSTALLS "${CMAKE_CURRENT_BINARY_DIR}/cpp-library-package-installs.cmake") + file(WRITE "${PACKAGE_TEST_INSTALLS}" "# Auto-generated by cpp-library.\n") + _cpp_library_write_package_installs("${PACKAGE_TEST_INSTALLS}" "${CMAKE_CURRENT_BINARY_DIR}") + + set(PACKAGE_TEST_NAME "${package}") + get_target_property(export_name "${target}" EXPORT_NAME) + if(NOT export_name) + set(export_name "${target}") + endif() + set(PACKAGE_TEST_TARGET "${namespace}::${export_name}") + set(PACKAGE_TEST_SOURCE "") + get_target_property(headers "${target}" HEADER_SET_headers) + get_target_property(bases "${target}" HEADER_DIRS_headers) + foreach(header IN LISTS headers) + foreach(base IN LISTS bases) + cmake_path(IS_PREFIX base "${header}" NORMALIZE included) + if(included) + file(RELATIVE_PATH include_path "${base}" "${header}") + string(APPEND PACKAGE_TEST_SOURCE "#include <${include_path}>\n") + break() + endif() + endforeach() + endforeach() + configure_file("${CPP_LIBRARY_ROOT}/templates/test-package.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/cpp-library-test-package.cmake" @ONLY) +endfunction() + +# Schedules packaging-check generation without running installation or changing install rules. +function(_cpp_library_setup_package_test target package namespace) + cmake_language(EVAL CODE + "cmake_language(DEFER CALL _cpp_library_generate_package_test [[${target}]] [[${package}]] [[${namespace}]])") +endfunction() diff --git a/cpp-library.cmake b/cpp-library.cmake index 1023504..d5c70d3 100644 --- a/cpp-library.cmake +++ b/cpp-library.cmake @@ -301,6 +301,8 @@ function(cpp_library_setup) return() # Early return for lightweight consumer mode endif() + _cpp_library_setup_package_test("${ARG_NAME}" "${PACKAGE_NAME}" "${ARG_NAMESPACE}") + # Copy static template files (like .clang-format, .gitignore, CMakePresets.json, etc.) if(DEFINED CPP_LIBRARY_FORCE_INIT AND CPP_LIBRARY_FORCE_INIT) _cpp_library_copy_templates("${PACKAGE_NAME}" FORCE_INIT) diff --git a/templates/.github/workflows/ci.yml.in b/templates/.github/workflows/ci.yml.in index e76aba3..c6e7a2d 100644 --- a/templates/.github/workflows/ci.yml.in +++ b/templates/.github/workflows/ci.yml.in @@ -75,27 +75,8 @@ jobs: if: ${{ !matrix.cc }} - name: Test find_package - shell: bash run: | - # Create a minimal test to verify the installation works with find_package - mkdir -p ${{ runner.temp }}/test-find-package - cd ${{ runner.temp }}/test-find-package - - # Create test CMakeLists.txt - cat > CMakeLists.txt << EOF - cmake_minimum_required(VERSION 3.20) - project(test-find-package CXX) - - find_package(@PACKAGE_NAME@ REQUIRED) - - message(STATUS "Successfully found @PACKAGE_NAME@") - EOF - - # Convert paths to forward slashes for CMake (works on all platforms) - INSTALL_PREFIX=$(echo '${{ runner.temp }}/install' | sed 's|\\|/|g') - - # Test find_package with CMAKE_PREFIX_PATH - cmake -B build -S . -DCMAKE_PREFIX_PATH="${INSTALL_PREFIX}" + cmake -P build/default/cpp-library-test-package.cmake clang-tidy: runs-on: ubuntu-latest diff --git a/templates/test-package.cmake.in b/templates/test-package.cmake.in new file mode 100644 index 0000000..40c952e --- /dev/null +++ b/templates/test-package.cmake.in @@ -0,0 +1,82 @@ +# SPDX-License-Identifier: BSL-1.0 +# Auto-generated by cpp-library. Run after building the package. +cmake_minimum_required(VERSION 3.24) + +string(RANDOM LENGTH 16 ALPHABET 0123456789abcdef test_id) +set(root "@CMAKE_CURRENT_BINARY_DIR@/package-test-${test_id}") +set(prefix "${root}/prefix") +set(source "${root}/consumer") +set(binary "${root}/build") +file(MAKE_DIRECTORY "${source}") +set(config_args) +set(install_args "-DCMAKE_INSTALL_PREFIX=${prefix}" -DCMAKE_INSTALL_LOCAL_ONLY=TRUE) +if(CONFIG) + list(APPEND config_args --config "${CONFIG}") + list(APPEND install_args "-DCMAKE_INSTALL_CONFIG_NAME=${CONFIG}") +endif() + +# Runs a build tool; failures retain the temporary tree and report the command. +function(package_test_run) + cmake_parse_arguments(PARSE_ARGV 0 command "" "" "") + execute_process(COMMAND ${command_UNPARSED_ARGUMENTS} RESULT_VARIABLE result) + if(NOT result EQUAL 0) + message(FATAL_ERROR "Package check failed (${result}): ${ARGN}\nArtifacts: ${root}") + endif() +endfunction() + +# Stages enabled rules from one configured directory, including excluded dependencies. +function(package_test_install directory) + if(EXISTS "${directory}/cmake_install.cmake") + package_test_run("@CMAKE_COMMAND@" ${install_args} -P "${directory}/cmake_install.cmake") + endif() +endfunction() + +include("@PACKAGE_TEST_INSTALLS@") +file(WRITE "${source}/CMakeLists.txt" [==[ +cmake_minimum_required(VERSION 3.24) +project(package-consumer LANGUAGES CXX) +include(CTest) +find_package(@PACKAGE_TEST_NAME@ CONFIG REQUIRED + PATHS "${PACKAGE_TEST_PREFIX}" NO_DEFAULT_PATH) +cmake_path(IS_PREFIX PACKAGE_TEST_PREFIX "${@PACKAGE_TEST_NAME@_CONFIG}" NORMALIZE staged) +if(NOT staged) + message(FATAL_ERROR "Package check resolved @PACKAGE_TEST_NAME@ outside its staging prefix") +endif() +add_executable(consumer main.cpp) +target_link_libraries(consumer PRIVATE @PACKAGE_TEST_TARGET@) +include("@CPP_LIBRARY_ROOT@/cpp-library.cmake") +cpp_library_copy_runtime_dlls(consumer) +add_test(NAME consumer COMMAND consumer) +]==]) +file(WRITE "${source}/main.cpp" [==[ +@PACKAGE_TEST_SOURCE@ +int main() { return 0; } +]==]) + +set(generator_args -G "@CMAKE_GENERATOR@") +if(NOT "@CMAKE_GENERATOR_PLATFORM@" STREQUAL "") + list(APPEND generator_args -A "@CMAKE_GENERATOR_PLATFORM@") +endif() +if(NOT "@CMAKE_GENERATOR_TOOLSET@" STREQUAL "") + list(APPEND generator_args -T "@CMAKE_GENERATOR_TOOLSET@") +endif() +set(toolchain_args) +if(NOT "@CMAKE_TOOLCHAIN_FILE@" STREQUAL "") + list(APPEND toolchain_args "-DCMAKE_TOOLCHAIN_FILE=@CMAKE_TOOLCHAIN_FILE@") +endif() +package_test_run("@CMAKE_COMMAND@" -S "${source}" -B "${binary}" ${generator_args} + ${toolchain_args} "-DCMAKE_BUILD_TYPE=@CMAKE_BUILD_TYPE@" + "-DCMAKE_CXX_COMPILER=@CMAKE_CXX_COMPILER@" "-DCMAKE_CXX_FLAGS=@CMAKE_CXX_FLAGS@" + "-DCMAKE_EXE_LINKER_FLAGS=@CMAKE_EXE_LINKER_FLAGS@" + "-DPACKAGE_TEST_PREFIX=${prefix}" + "-DCMAKE_PREFIX_PATH=${prefix};@CMAKE_PREFIX_PATH@" + -DCMAKE_FIND_USE_PACKAGE_REGISTRY=OFF -DCMAKE_FIND_USE_SYSTEM_PACKAGE_REGISTRY=OFF) +package_test_run("@CMAKE_COMMAND@" --build "${binary}" ${config_args}) +find_program(ctest NAMES ctest REQUIRED) +set(test_args) +if(CONFIG) + list(APPEND test_args -C "${CONFIG}") +endif() +package_test_run("${ctest}" --test-dir "${binary}" ${test_args} --output-on-failure) +file(REMOVE_RECURSE "${root}") +message(STATUS "Installed @PACKAGE_TEST_NAME@ consumer passed") diff --git a/tests/install/fixtures/nested/CMakeLists.txt b/tests/install/fixtures/nested/CMakeLists.txt index ef901c0..de9b222 100644 --- a/tests/install/fixtures/nested/CMakeLists.txt +++ b/tests/install/fixtures/nested/CMakeLists.txt @@ -19,7 +19,11 @@ cpp_library_setup( NAMESPACE stlab HEADERS parent.hpp ${install_args}) -add_subdirectory(leaf) +if(EXCLUDE_LEAF_FROM_ALL) + add_subdirectory(leaf EXCLUDE_FROM_ALL) +else() + add_subdirectory(leaf) +endif() # Resolve the installed target downstream, even when leaf is not exported in this build. target_link_libraries(parent INTERFACE $ diff --git a/tests/install/fixtures/nested/leaf/CMakeLists.txt b/tests/install/fixtures/nested/leaf/CMakeLists.txt index c37dd13..7586bea 100644 --- a/tests/install/fixtures/nested/leaf/CMakeLists.txt +++ b/tests/install/fixtures/nested/leaf/CMakeLists.txt @@ -11,6 +11,11 @@ cpp_library_setup( HEADERS leaf.hpp SOURCES leaf.cpp ${install_args}) +if(PACKAGE_TEST_EXTRA) + add_subdirectory(extra EXCLUDE_FROM_ALL) + target_link_libraries(leaf PUBLIC stlab::extra) + cpp_library_map_dependency("stlab::extra" "stlab-extra 1.0.0") +endif() get_target_property(leaf_type leaf TYPE) set(LEAF_SHARED 0) if(leaf_type STREQUAL "SHARED_LIBRARY") diff --git a/tests/install/fixtures/nested/leaf/extra/CMakeLists.txt b/tests/install/fixtures/nested/leaf/extra/CMakeLists.txt new file mode 100644 index 0000000..b93986a --- /dev/null +++ b/tests/install/fixtures/nested/leaf/extra/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 3.24) +include("${TOOLKIT_SOURCE}/cpp-library.cmake") +project(extra LANGUAGES CXX) +set(CPP_LIBRARY_VERSION 1.0.0) +cpp_library_setup( + DESCRIPTION "Transitive packaging dependency fixture" + NAMESPACE stlab + HEADERS extra.hpp + INSTALL_OPTION STLAB_LEAF_INSTALL) diff --git a/tests/install/fixtures/nested/leaf/extra/include/stlab/extra.hpp b/tests/install/fixtures/nested/leaf/extra/include/stlab/extra.hpp new file mode 100644 index 0000000..ef98066 --- /dev/null +++ b/tests/install/fixtures/nested/leaf/extra/include/stlab/extra.hpp @@ -0,0 +1,5 @@ +#pragma once + +namespace stlab { +inline constexpr int extra_value = 0; +} diff --git a/tests/install/fixtures/single/CMakeLists.txt b/tests/install/fixtures/single/CMakeLists.txt new file mode 100644 index 0000000..ea7788d --- /dev/null +++ b/tests/install/fixtures/single/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.24) +include("${TOOLKIT_SOURCE}/cpp-library.cmake") +cpp_library_enable_dependency_tracking() +project(single LANGUAGES CXX) +set(CPP_LIBRARY_VERSION 1.0.0) +cpp_library_setup( + DESCRIPTION "Dependency-free packaging fixture" + NAMESPACE stlab + HEADERS single.hpp) +if(EXTERNAL_PACKAGE) + find_package(stlab-extra 1.0.0 CONFIG REQUIRED) + target_link_libraries(single INTERFACE stlab::extra) +endif() diff --git a/tests/install/fixtures/single/include/stlab/single.hpp b/tests/install/fixtures/single/include/stlab/single.hpp new file mode 100644 index 0000000..72f3a9b --- /dev/null +++ b/tests/install/fixtures/single/include/stlab/single.hpp @@ -0,0 +1,5 @@ +#pragma once + +namespace stlab { +inline constexpr int single_value = 42; +} diff --git a/tests/install/test_package_ci.cmake b/tests/install/test_package_ci.cmake new file mode 100644 index 0000000..473768b --- /dev/null +++ b/tests/install/test_package_ci.cmake @@ -0,0 +1,145 @@ +# SPDX-License-Identifier: BSL-1.0 +# +# Run as: cmake -P tests/install/test_package_ci.cmake +cmake_minimum_required(VERSION 3.24) + +get_filename_component(toolkit_source "${CMAKE_CURRENT_LIST_DIR}/../.." ABSOLUTE) +string(RANDOM LENGTH 16 ALPHABET 0123456789abcdef test_id) +set(test_root "${toolkit_source}/build/package-ci-${test_id}") + +# Runs a build tool and reports its output on failure. +function(run_command) + execute_process(COMMAND ${ARGN} + RESULT_VARIABLE result OUTPUT_VARIABLE out ERROR_VARIABLE err) + if(NOT result EQUAL 0) + message(FATAL_ERROR "Command failed (${result}): ${ARGN}\n${out}\n${err}") + endif() +endfunction() + +# Checks packaging without changing the excluded dependency's normal installation. +function(run_case name shared generator config leaf_installed) + set(root "${test_root}/${name} with spaces") + set(source "${root}/source") + set(binary "${root}/build") + set(prefix "${root}/normal-install") + file(MAKE_DIRECTORY "${source}") + file(COPY "${CMAKE_CURRENT_LIST_DIR}/fixtures/nested/" DESTINATION "${source}") + run_command("${CMAKE_COMMAND}" -S "${source}" -B "${binary}" -G "${generator}" + "-DTOOLKIT_SOURCE=${toolkit_source}" "-DBUILD_SHARED_LIBS=${shared}" + "-DCMAKE_BUILD_TYPE=${config}" -DEXCLUDE_LEAF_FROM_ALL=ON + -DSTLAB_PARENT_INSTALL=ON "-DSTLAB_LEAF_INSTALL=${leaf_installed}" ${ARGN}) + run_command("${CMAKE_COMMAND}" --build "${binary}" --target leaf --config "${config}") + run_command("${CMAKE_COMMAND}" --install "${binary}" --prefix "${prefix}" + --config "${config}") + if(EXISTS "${prefix}/lib/cmake/stlab-leaf/stlab-leafConfig.cmake") + message(FATAL_ERROR "Normal parent installation must still exclude the leaf") + endif() + set(consumer "${root}/missing-dependency") + file(MAKE_DIRECTORY "${consumer}") + file(WRITE "${consumer}/CMakeLists.txt" + "cmake_minimum_required(VERSION 3.24)\nproject(consumer LANGUAGES CXX)\nfind_package(stlab-parent REQUIRED)\n") + execute_process(COMMAND "${CMAKE_COMMAND}" -S "${consumer}" -B "${consumer}/build" + -G "${generator}" "-DCMAKE_PREFIX_PATH=${prefix}" + -DCMAKE_FIND_USE_PACKAGE_REGISTRY=OFF -DCMAKE_FIND_USE_SYSTEM_PACKAGE_REGISTRY=OFF + RESULT_VARIABLE result OUTPUT_VARIABLE out ERROR_VARIABLE err) + if(result EQUAL 0 OR NOT "${out}\n${err}" MATCHES "stlab-leaf") + message(FATAL_ERROR "Expected the parent-only install to lack stlab-leaf:\n${out}\n${err}") + endif() + set(check "${binary}/cpp-library-test-package.cmake") + if(NOT EXISTS "${check}") + message(FATAL_ERROR "Missing generated packaging check: ${check}") + endif() + if(leaf_installed) + run_command("${CMAKE_COMMAND}" "-DCONFIG=${config}" -P "${check}") + else() + execute_process(COMMAND "${CMAKE_COMMAND}" "-DCONFIG=${config}" -P "${check}" + RESULT_VARIABLE result OUTPUT_VARIABLE out ERROR_VARIABLE err) + if(result EQUAL 0 OR NOT "${out}\n${err}" MATCHES "stlab-leaf") + message(FATAL_ERROR "Expected disabled leaf installation to fail packaging:\n${out}\n${err}") + endif() + endif() + if(EXISTS "${prefix}/lib/cmake/stlab-leaf/stlab-leafConfig.cmake") + message(FATAL_ERROR "Packaging check must not modify the normal install prefix") + endif() + message(STATUS "PASS ${name}: isolated packaging with an excluded dependency") +endfunction() + +# Checks that packages without dependencies also receive a working generated check. +function(run_single) + set(root "${test_root}/single") + set(source "${root}/source") + set(binary "${root}/build") + 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}" -DCMAKE_BUILD_TYPE=Release) + run_command("${CMAKE_COMMAND}" --build "${binary}") + run_command("${CMAKE_COMMAND}" -P "${binary}/cpp-library-test-package.cmake") + message(STATUS "PASS single: dependency-free generated packaging check") +endfunction() + +# Keeps externally installed dependencies discoverable through a prefix list. +function(run_external) + set(root "${test_root}/external with spaces") + set(dependency "${root}/dependency") + set(external_prefix "${root}/installed dependency") + file(MAKE_DIRECTORY "${dependency}") + file(COPY "${CMAKE_CURRENT_LIST_DIR}/fixtures/nested/leaf/extra/" DESTINATION "${dependency}") + run_command("${CMAKE_COMMAND}" -S "${dependency}" -B "${dependency}/build" -G Ninja + "-DTOOLKIT_SOURCE=${toolkit_source}" -DCMAKE_BUILD_TYPE=Release) + run_command("${CMAKE_COMMAND}" --install "${dependency}/build" --prefix "${external_prefix}") + set(source "${root}/source") + set(binary "${root}/build") + file(MAKE_DIRECTORY "${source}") + file(COPY "${CMAKE_CURRENT_LIST_DIR}/fixtures/single/" DESTINATION "${source}") + execute_process(COMMAND "${CMAKE_COMMAND}" -S "${source}" -B "${binary}" -G Ninja + "-DTOOLKIT_SOURCE=${toolkit_source}" -DCMAKE_BUILD_TYPE=Release -DEXTERNAL_PACKAGE=ON + "-DCMAKE_PREFIX_PATH=${root}/unused;${external_prefix}" + RESULT_VARIABLE result OUTPUT_VARIABLE out ERROR_VARIABLE err) + if(NOT result EQUAL 0) + message(FATAL_ERROR "External dependency fixture failed:\n${out}\n${err}") + endif() + run_command("${CMAKE_COMMAND}" --build "${binary}") + run_command("${CMAKE_COMMAND}" -P "${binary}/cpp-library-test-package.cmake") + message(STATUS "PASS external: semicolon-separated dependency prefixes preserved") +endfunction() + +# Rejects an existing installation when this build did not install its package. +function(run_shadow) + set(root "${test_root}/shadow") + set(source "${root}/source") + set(binary "${root}/build") + set(old_prefix "${root}/previous installation") + 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}" -DCMAKE_BUILD_TYPE=Release) + run_command("${CMAKE_COMMAND}" --install "${binary}" --prefix "${old_prefix}") + run_command("${CMAKE_COMMAND}" -S "${source}" -B "${binary}" -DSTLAB_INSTALL=OFF) + execute_process(COMMAND "${CMAKE_COMMAND}" -E env "CMAKE_PREFIX_PATH=${old_prefix}" + "${CMAKE_COMMAND}" -P "${binary}/cpp-library-test-package.cmake" + RESULT_VARIABLE result OUTPUT_VARIABLE out ERROR_VARIABLE err) + if(result EQUAL 0 OR NOT "${out}\n${err}" MATCHES "stlab-single") + message(FATAL_ERROR "Packaging must reject a previously installed package:\n${out}\n${err}") + endif() + message(STATUS "PASS shadow: previous installation cannot mask missing staged package") +endfunction() + +if(CASE STREQUAL "external") + run_external() +elseif(CASE STREQUAL "shadow") + run_shadow() +elseif(DEFINED CASE) + message(FATAL_ERROR "Unknown packaging case: ${CASE}") +else() + run_case(static OFF Ninja Release ON) + run_case(shared ON Ninja Release ON) + run_case(multi-config OFF "Ninja Multi-Config" Debug ON) + run_case(transitive OFF Ninja Release ON -DPACKAGE_TEST_EXTRA=ON) + run_case(disabled-dependency OFF Ninja Release OFF) + run_single() + run_external() + run_shadow() +endif() +file(REMOVE_RECURSE "${test_root}") +message(STATUS "Generated packaging checks passed!") From 8f104051d0bf63e374de345cb9adff97f77d9735 Mon Sep 17 00:00:00 2001 From: sean-parent Date: Sat, 14 Nov 2026 14:49:24 -0800 Subject: [PATCH 2/4] test: check installed package artifacts in CI Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- cmake/cpp-library-ci.cmake | 46 +---- cpp-library.cmake | 2 +- .../2026-11-14-ci-package-artifact-check.md | 92 ++++++++++ ...-11-14-ci-package-artifact-check-design.md | 36 ++++ templates/.github/workflows/ci.yml.in | 8 +- templates/test-package.cmake.in | 95 +++------- tests/install/test_package_ci.cmake | 169 ++++++++---------- 7 files changed, 231 insertions(+), 217 deletions(-) create mode 100644 docs/superpowers/plans/2026-11-14-ci-package-artifact-check.md create mode 100644 docs/superpowers/specs/2026-11-14-ci-package-artifact-check-design.md diff --git a/cmake/cpp-library-ci.cmake b/cmake/cpp-library-ci.cmake index e23c76a..0bdb361 100644 --- a/cmake/cpp-library-ci.cmake +++ b/cmake/cpp-library-ci.cmake @@ -38,49 +38,17 @@ function(_cpp_library_setup_ci PACKAGE_NAME force_init) endif() endfunction() -# Writes directory-local installation commands in dependency-first directory order. -# Complexity: linear in the configured directory tree and emitted path lengths. -function(_cpp_library_write_package_installs destination binary_dir) - get_property(children DIRECTORY "${binary_dir}" PROPERTY SUBDIRECTORIES) - foreach(child IN LISTS children) - get_property(child_binary DIRECTORY "${child}" PROPERTY BINARY_DIR) - _cpp_library_write_package_installs("${destination}" "${child_binary}") - endforeach() - file(APPEND "${destination}" "package_test_install([[${binary_dir}]])\n") -endfunction() - -# Generates an isolated installed-consumer check after the project's directories are configured. -# Complexity: linear in directory paths, plus O(headers * header bases) path comparisons. -function(_cpp_library_generate_package_test target package namespace) - set(PACKAGE_TEST_INSTALLS "${CMAKE_CURRENT_BINARY_DIR}/cpp-library-package-installs.cmake") - file(WRITE "${PACKAGE_TEST_INSTALLS}" "# Auto-generated by cpp-library.\n") - _cpp_library_write_package_installs("${PACKAGE_TEST_INSTALLS}" "${CMAKE_CURRENT_BINARY_DIR}") - +# Generates an installed-artifact check after the project's directories are configured. +function(_cpp_library_generate_package_test package) + include(GNUInstallDirs) set(PACKAGE_TEST_NAME "${package}") - get_target_property(export_name "${target}" EXPORT_NAME) - if(NOT export_name) - set(export_name "${target}") - endif() - set(PACKAGE_TEST_TARGET "${namespace}::${export_name}") - set(PACKAGE_TEST_SOURCE "") - get_target_property(headers "${target}" HEADER_SET_headers) - get_target_property(bases "${target}" HEADER_DIRS_headers) - foreach(header IN LISTS headers) - foreach(base IN LISTS bases) - cmake_path(IS_PREFIX base "${header}" NORMALIZE included) - if(included) - file(RELATIVE_PATH include_path "${base}" "${header}") - string(APPEND PACKAGE_TEST_SOURCE "#include <${include_path}>\n") - break() - endif() - endforeach() - endforeach() + set(PACKAGE_TEST_INSTALL_LIBDIR "${CMAKE_INSTALL_LIBDIR}") configure_file("${CPP_LIBRARY_ROOT}/templates/test-package.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/cpp-library-test-package.cmake" @ONLY) endfunction() -# Schedules packaging-check generation without running installation or changing install rules. -function(_cpp_library_setup_package_test target package namespace) +# Schedules artifact-check generation without changing install rules. +function(_cpp_library_setup_package_test package) cmake_language(EVAL CODE - "cmake_language(DEFER CALL _cpp_library_generate_package_test [[${target}]] [[${package}]] [[${namespace}]])") + "cmake_language(DEFER CALL _cpp_library_generate_package_test [[${package}]])") endfunction() diff --git a/cpp-library.cmake b/cpp-library.cmake index d5c70d3..17bfde9 100644 --- a/cpp-library.cmake +++ b/cpp-library.cmake @@ -301,7 +301,7 @@ function(cpp_library_setup) return() # Early return for lightweight consumer mode endif() - _cpp_library_setup_package_test("${ARG_NAME}" "${PACKAGE_NAME}" "${ARG_NAMESPACE}") + _cpp_library_setup_package_test("${PACKAGE_NAME}") # Copy static template files (like .clang-format, .gitignore, CMakePresets.json, etc.) if(DEFINED CPP_LIBRARY_FORCE_INIT AND CPP_LIBRARY_FORCE_INIT) diff --git a/docs/superpowers/plans/2026-11-14-ci-package-artifact-check.md b/docs/superpowers/plans/2026-11-14-ci-package-artifact-check.md new file mode 100644 index 0000000..94e7de5 --- /dev/null +++ b/docs/superpowers/plans/2026-11-14-ci-package-artifact-check.md @@ -0,0 +1,92 @@ +# CI Package Artifact Check Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** Replace generated downstream-consumer testing with a check that the normal CI install produced the root package's config, version, and target-export files. + +**Architecture:** Keep the workflow's existing `cmake --install` step and pass its prefix to the generated check script. The script verifies the three local package artifacts under the configured CMake install directory without staging dependencies or configuring a consumer. + +**Tech Stack:** CMake script mode, CMake install rules, generated GitHub Actions workflow. + +**Spec:** `docs/superpowers/specs/2026-11-14-ci-package-artifact-check-design.md` + +## Global Constraints + +- Do not recursively install subdirectories, build a downstream consumer, or call `find_package()`. +- Do not require additional client configuration. +- Leave dependency installation and discovery to the client. +- Keep the existing workflow install step responsible for install-time errors. + +## Review Focus + +- A missing prefix must fail with a useful error rather than succeeding through a self-staged install. +- A missing config, version, or targets file must fail the artifact check. +- A prefix containing spaces must work when passed from the generated workflow. +- A non-default `CMAKE_INSTALL_LIBDIR` must be honored. +- An absolute `CMAKE_INSTALL_LIBDIR` must not let stale files bypass a missing-prefix failure. +- The check must not configure or build a downstream consumer. + +--- + +### Task 1: Check installed package artifacts in generated CI + +**Files:** +- Modify: `cmake/cpp-library-ci.cmake` +- Modify: `cpp-library.cmake` +- Modify: `templates/test-package.cmake.in` +- Modify: `templates/.github/workflows/ci.yml.in` +- Modify: `tests/install/test_package_ci.cmake` + +**Interfaces:** +- The generated script accepts `PACKAGE_TEST_PREFIX` as a `cmake -D` variable. +- The workflow passes the same `${{ runner.temp }}/install` prefix used by + `cmake --install`. +- The generated script checks + `//cmake//` for + `Config.cmake`, `ConfigVersion.cmake`, and + `Targets.cmake`. + +- [x] **Step 1: Rewrite the regression around the normal install prefix** + +Replace the dependency-staging, external-dependency, and shadow-install cases +in `tests/install/test_package_ci.cmake` with focused artifact-check cases. +Configure the `single` fixture with `CMAKE_INSTALL_LIBDIR=lib64` and use an +install prefix whose path contains spaces. Before installing, invoke the +generated check with `PACKAGE_TEST_PREFIX` and require it to fail. Install the +fixture by executing the generated workflow's install command and require the +check to pass. For each required artifact, copy the install tree to a separate +prefix, remove that artifact, and require the check to fail. Also configure a +second fixture with an absolute `CMAKE_INSTALL_LIBDIR`, create stale artifact +files there, and verify a nonexistent install prefix still fails. Do not +configure or build a consumer. + +- [x] **Step 2: Run the regression to confirm it exposes the old behavior** + +Run: `cmake -P tests/install/test_package_ci.cmake` + +Expected: FAIL because the current generated script ignores the supplied prefix +and stages its own package instead of rejecting the missing artifacts. + +- [x] **Step 3: Generate a check script that verifies only local artifacts** + +In `cmake/cpp-library-ci.cmake`, remove recursive install-list generation and +change the internal package-test helper and its `cpp-library.cmake` call site +to pass only the package name. Pass the configured `CMAKE_INSTALL_LIBDIR` to +`templates/test-package.cmake.in`. Replace the staging, consumer configure, +build, and CTest logic in that template with validation of `PACKAGE_TEST_PREFIX` +and explicit existence checks for the three required files. Report the missing +path in each failure. + +- [x] **Step 4: Pass the workflow's existing install prefix to the check** + +Update `templates/.github/workflows/ci.yml.in` so both install steps quote the +`${{ runner.temp }}/install` argument, and the artifact-check step passes that +same prefix as `PACKAGE_TEST_PREFIX`. + +- [x] **Step 5: Run the focused packaging regression** + +Run: `cmake -P tests/install/test_package_ci.cmake` + +Expected: PASS for artifact presence and FAIL cases, including the absent +pre-install prefix, each missing artifact, a path containing spaces, `lib64`, +and stale artifacts under an absolute library directory. diff --git a/docs/superpowers/specs/2026-11-14-ci-package-artifact-check-design.md b/docs/superpowers/specs/2026-11-14-ci-package-artifact-check-design.md new file mode 100644 index 0000000..7e86642 --- /dev/null +++ b/docs/superpowers/specs/2026-11-14-ci-package-artifact-check-design.md @@ -0,0 +1,36 @@ +# CI Package Artifact Check — Design + +## Goal + +Replace the generated downstream-consumer test with a check that the normal CI +install produced the root package's config, version, and exported-target files. +This keeps CI independent of how clients install dependencies; it does not +claim to validate downstream `find_package()` resolution. + +## Scope + +- Keep the generated workflow's normal `cmake --install` step. +- Pass its install prefix to the generated package-check script. +- Check for `Config.cmake`, + `ConfigVersion.cmake`, and + `Targets.cmake` under the package's configured CMake install + directory. +- Add focused regression coverage for present and missing artifacts. + +## Approaches + +- **Stage dependencies and configure a consumer.** Rejected because CI cannot + infer how arbitrary dependencies are installed or discovered without + client-specific inputs. +- **Rely on install success alone.** Simpler, but does not detect a successful + install that omitted the local package files. +- **Check local package artifacts.** Selected because it verifies the root + package's install output without requiring dependency configuration. + +## Constraints and non-goals + +The check must not recursively install subdirectories, build a downstream +consumer, call `find_package()`, or require additional client configuration. +Dependency installation and discovery remain the client's responsibility. +The existing install step remains responsible for reporting install-time +errors. diff --git a/templates/.github/workflows/ci.yml.in b/templates/.github/workflows/ci.yml.in index c6e7a2d..2a1f00c 100644 --- a/templates/.github/workflows/ci.yml.in +++ b/templates/.github/workflows/ci.yml.in @@ -61,7 +61,7 @@ jobs: run: | cmake --preset=default cmake --build --preset=default - cmake --install build/default --prefix ${{ runner.temp }}/install + cmake --install build/default --prefix "${{ runner.temp }}/install" env: CC: ${{ matrix.cc }} CXX: ${{ matrix.cxx }} @@ -71,12 +71,12 @@ jobs: run: | cmake --preset=default cmake --build --preset=default - cmake --install build/default --prefix ${{ runner.temp }}/install + cmake --install build/default --prefix "${{ runner.temp }}/install" if: ${{ !matrix.cc }} - - name: Test find_package + - name: Check installed package artifacts run: | - cmake -P build/default/cpp-library-test-package.cmake + 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/test-package.cmake.in b/templates/test-package.cmake.in index 40c952e..6728d71 100644 --- a/templates/test-package.cmake.in +++ b/templates/test-package.cmake.in @@ -1,82 +1,29 @@ # SPDX-License-Identifier: BSL-1.0 -# Auto-generated by cpp-library. Run after building the package. +# Auto-generated by cpp-library. Run after installing the package. cmake_minimum_required(VERSION 3.24) -string(RANDOM LENGTH 16 ALPHABET 0123456789abcdef test_id) -set(root "@CMAKE_CURRENT_BINARY_DIR@/package-test-${test_id}") -set(prefix "${root}/prefix") -set(source "${root}/consumer") -set(binary "${root}/build") -file(MAKE_DIRECTORY "${source}") -set(config_args) -set(install_args "-DCMAKE_INSTALL_PREFIX=${prefix}" -DCMAKE_INSTALL_LOCAL_ONLY=TRUE) -if(CONFIG) - list(APPEND config_args --config "${CONFIG}") - list(APPEND install_args "-DCMAKE_INSTALL_CONFIG_NAME=${CONFIG}") +if(NOT DEFINED PACKAGE_TEST_PREFIX OR PACKAGE_TEST_PREFIX STREQUAL "") + message(FATAL_ERROR "PACKAGE_TEST_PREFIX must name the install prefix") +endif() +if(NOT IS_DIRECTORY "${PACKAGE_TEST_PREFIX}") + message(FATAL_ERROR "Package install prefix does not exist: ${PACKAGE_TEST_PREFIX}") endif() -# Runs a build tool; failures retain the temporary tree and report the command. -function(package_test_run) - cmake_parse_arguments(PARSE_ARGV 0 command "" "" "") - execute_process(COMMAND ${command_UNPARSED_ARGUMENTS} RESULT_VARIABLE result) - if(NOT result EQUAL 0) - message(FATAL_ERROR "Package check failed (${result}): ${ARGN}\nArtifacts: ${root}") - endif() -endfunction() +set(package_install_dir "@PACKAGE_TEST_INSTALL_LIBDIR@/cmake/@PACKAGE_TEST_NAME@") +if(IS_ABSOLUTE "${package_install_dir}") + set(package_dir "${package_install_dir}") +else() + set(package_dir "${PACKAGE_TEST_PREFIX}/${package_install_dir}") +endif() -# Stages enabled rules from one configured directory, including excluded dependencies. -function(package_test_install directory) - if(EXISTS "${directory}/cmake_install.cmake") - package_test_run("@CMAKE_COMMAND@" ${install_args} -P "${directory}/cmake_install.cmake") +foreach(artifact IN ITEMS + "@PACKAGE_TEST_NAME@Config.cmake" + "@PACKAGE_TEST_NAME@ConfigVersion.cmake" + "@PACKAGE_TEST_NAME@Targets.cmake") + set(artifact_path "${package_dir}/${artifact}") + if(NOT EXISTS "${artifact_path}") + message(FATAL_ERROR "Installed package artifact is missing: ${artifact_path}") endif() -endfunction() - -include("@PACKAGE_TEST_INSTALLS@") -file(WRITE "${source}/CMakeLists.txt" [==[ -cmake_minimum_required(VERSION 3.24) -project(package-consumer LANGUAGES CXX) -include(CTest) -find_package(@PACKAGE_TEST_NAME@ CONFIG REQUIRED - PATHS "${PACKAGE_TEST_PREFIX}" NO_DEFAULT_PATH) -cmake_path(IS_PREFIX PACKAGE_TEST_PREFIX "${@PACKAGE_TEST_NAME@_CONFIG}" NORMALIZE staged) -if(NOT staged) - message(FATAL_ERROR "Package check resolved @PACKAGE_TEST_NAME@ outside its staging prefix") -endif() -add_executable(consumer main.cpp) -target_link_libraries(consumer PRIVATE @PACKAGE_TEST_TARGET@) -include("@CPP_LIBRARY_ROOT@/cpp-library.cmake") -cpp_library_copy_runtime_dlls(consumer) -add_test(NAME consumer COMMAND consumer) -]==]) -file(WRITE "${source}/main.cpp" [==[ -@PACKAGE_TEST_SOURCE@ -int main() { return 0; } -]==]) +endforeach() -set(generator_args -G "@CMAKE_GENERATOR@") -if(NOT "@CMAKE_GENERATOR_PLATFORM@" STREQUAL "") - list(APPEND generator_args -A "@CMAKE_GENERATOR_PLATFORM@") -endif() -if(NOT "@CMAKE_GENERATOR_TOOLSET@" STREQUAL "") - list(APPEND generator_args -T "@CMAKE_GENERATOR_TOOLSET@") -endif() -set(toolchain_args) -if(NOT "@CMAKE_TOOLCHAIN_FILE@" STREQUAL "") - list(APPEND toolchain_args "-DCMAKE_TOOLCHAIN_FILE=@CMAKE_TOOLCHAIN_FILE@") -endif() -package_test_run("@CMAKE_COMMAND@" -S "${source}" -B "${binary}" ${generator_args} - ${toolchain_args} "-DCMAKE_BUILD_TYPE=@CMAKE_BUILD_TYPE@" - "-DCMAKE_CXX_COMPILER=@CMAKE_CXX_COMPILER@" "-DCMAKE_CXX_FLAGS=@CMAKE_CXX_FLAGS@" - "-DCMAKE_EXE_LINKER_FLAGS=@CMAKE_EXE_LINKER_FLAGS@" - "-DPACKAGE_TEST_PREFIX=${prefix}" - "-DCMAKE_PREFIX_PATH=${prefix};@CMAKE_PREFIX_PATH@" - -DCMAKE_FIND_USE_PACKAGE_REGISTRY=OFF -DCMAKE_FIND_USE_SYSTEM_PACKAGE_REGISTRY=OFF) -package_test_run("@CMAKE_COMMAND@" --build "${binary}" ${config_args}) -find_program(ctest NAMES ctest REQUIRED) -set(test_args) -if(CONFIG) - list(APPEND test_args -C "${CONFIG}") -endif() -package_test_run("${ctest}" --test-dir "${binary}" ${test_args} --output-on-failure) -file(REMOVE_RECURSE "${root}") -message(STATUS "Installed @PACKAGE_TEST_NAME@ consumer passed") +message(STATUS "Installed @PACKAGE_TEST_NAME@ artifacts found in ${package_dir}") diff --git a/tests/install/test_package_ci.cmake b/tests/install/test_package_ci.cmake index 473768b..98b4026 100644 --- a/tests/install/test_package_ci.cmake +++ b/tests/install/test_package_ci.cmake @@ -7,7 +7,6 @@ get_filename_component(toolkit_source "${CMAKE_CURRENT_LIST_DIR}/../.." ABSOLUTE string(RANDOM LENGTH 16 ALPHABET 0123456789abcdef test_id) set(test_root "${toolkit_source}/build/package-ci-${test_id}") -# Runs a build tool and reports its output on failure. function(run_command) execute_process(COMMAND ${ARGN} RESULT_VARIABLE result OUTPUT_VARIABLE out ERROR_VARIABLE err) @@ -16,130 +15,102 @@ function(run_command) endif() endfunction() -# Checks packaging without changing the excluded dependency's normal installation. -function(run_case name shared generator config leaf_installed) - set(root "${test_root}/${name} with spaces") - set(source "${root}/source") - set(binary "${root}/build") - set(prefix "${root}/normal-install") - file(MAKE_DIRECTORY "${source}") - file(COPY "${CMAKE_CURRENT_LIST_DIR}/fixtures/nested/" DESTINATION "${source}") - run_command("${CMAKE_COMMAND}" -S "${source}" -B "${binary}" -G "${generator}" - "-DTOOLKIT_SOURCE=${toolkit_source}" "-DBUILD_SHARED_LIBS=${shared}" - "-DCMAKE_BUILD_TYPE=${config}" -DEXCLUDE_LEAF_FROM_ALL=ON - -DSTLAB_PARENT_INSTALL=ON "-DSTLAB_LEAF_INSTALL=${leaf_installed}" ${ARGN}) - run_command("${CMAKE_COMMAND}" --build "${binary}" --target leaf --config "${config}") - run_command("${CMAKE_COMMAND}" --install "${binary}" --prefix "${prefix}" - --config "${config}") - if(EXISTS "${prefix}/lib/cmake/stlab-leaf/stlab-leafConfig.cmake") - message(FATAL_ERROR "Normal parent installation must still exclude the leaf") - endif() - set(consumer "${root}/missing-dependency") - file(MAKE_DIRECTORY "${consumer}") - file(WRITE "${consumer}/CMakeLists.txt" - "cmake_minimum_required(VERSION 3.24)\nproject(consumer LANGUAGES CXX)\nfind_package(stlab-parent REQUIRED)\n") - execute_process(COMMAND "${CMAKE_COMMAND}" -S "${consumer}" -B "${consumer}/build" - -G "${generator}" "-DCMAKE_PREFIX_PATH=${prefix}" - -DCMAKE_FIND_USE_PACKAGE_REGISTRY=OFF -DCMAKE_FIND_USE_SYSTEM_PACKAGE_REGISTRY=OFF +function(expect_check_failure check prefix missing_file) + execute_process( + COMMAND "${CMAKE_COMMAND}" "-DPACKAGE_TEST_PREFIX=${prefix}" -P "${check}" RESULT_VARIABLE result OUTPUT_VARIABLE out ERROR_VARIABLE err) - if(result EQUAL 0 OR NOT "${out}\n${err}" MATCHES "stlab-leaf") - message(FATAL_ERROR "Expected the parent-only install to lack stlab-leaf:\n${out}\n${err}") + if(result EQUAL 0 OR NOT "${out}\n${err}" MATCHES "${missing_file}") + message(FATAL_ERROR + "Expected package check to report ${missing_file} as missing:\n${out}\n${err}") endif() - set(check "${binary}/cpp-library-test-package.cmake") - if(NOT EXISTS "${check}") - message(FATAL_ERROR "Missing generated packaging check: ${check}") +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() - if(leaf_installed) - run_command("${CMAKE_COMMAND}" "-DCONFIG=${config}" -P "${check}") + 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 "${CMAKE_COMMAND}" "-DCONFIG=${config}" -P "${check}" + execute_process(COMMAND /bin/sh -c "${install_command}" RESULT_VARIABLE result OUTPUT_VARIABLE out ERROR_VARIABLE err) - if(result EQUAL 0 OR NOT "${out}\n${err}" MATCHES "stlab-leaf") - message(FATAL_ERROR "Expected disabled leaf installation to fail packaging:\n${out}\n${err}") - endif() endif() - if(EXISTS "${prefix}/lib/cmake/stlab-leaf/stlab-leafConfig.cmake") - message(FATAL_ERROR "Packaging check must not modify the normal install prefix") + if(NOT result EQUAL 0) + message(FATAL_ERROR "Generated workflow install failed (${result}):\n${install_command}\n${out}\n${err}") endif() - message(STATUS "PASS ${name}: isolated packaging with an excluded dependency") endfunction() -# Checks that packages without dependencies also receive a working generated check. function(run_single) set(root "${test_root}/single") set(source "${root}/source") set(binary "${root}/build") + set(prefix "${root}/install prefix") 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}" -DCMAKE_BUILD_TYPE=Release) + "-DTOOLKIT_SOURCE=${toolkit_source}" -DCMAKE_BUILD_TYPE=Release + -DCMAKE_INSTALL_LIBDIR=lib64) run_command("${CMAKE_COMMAND}" --build "${binary}") - run_command("${CMAKE_COMMAND}" -P "${binary}/cpp-library-test-package.cmake") - message(STATUS "PASS single: dependency-free generated packaging check") -endfunction() -# Keeps externally installed dependencies discoverable through a prefix list. -function(run_external) - set(root "${test_root}/external with spaces") - set(dependency "${root}/dependency") - set(external_prefix "${root}/installed dependency") - file(MAKE_DIRECTORY "${dependency}") - file(COPY "${CMAKE_CURRENT_LIST_DIR}/fixtures/nested/leaf/extra/" DESTINATION "${dependency}") - run_command("${CMAKE_COMMAND}" -S "${dependency}" -B "${dependency}/build" -G Ninja - "-DTOOLKIT_SOURCE=${toolkit_source}" -DCMAKE_BUILD_TYPE=Release) - run_command("${CMAKE_COMMAND}" --install "${dependency}/build" --prefix "${external_prefix}") - set(source "${root}/source") - set(binary "${root}/build") - file(MAKE_DIRECTORY "${source}") - file(COPY "${CMAKE_CURRENT_LIST_DIR}/fixtures/single/" DESTINATION "${source}") - execute_process(COMMAND "${CMAKE_COMMAND}" -S "${source}" -B "${binary}" -G Ninja - "-DTOOLKIT_SOURCE=${toolkit_source}" -DCMAKE_BUILD_TYPE=Release -DEXTERNAL_PACKAGE=ON - "-DCMAKE_PREFIX_PATH=${root}/unused;${external_prefix}" - RESULT_VARIABLE result OUTPUT_VARIABLE out ERROR_VARIABLE err) - if(NOT result EQUAL 0) - message(FATAL_ERROR "External dependency fixture failed:\n${out}\n${err}") + set(workflow "${source}/.github/workflows/ci.yml") + if(NOT EXISTS "${workflow}") + message(FATAL_ERROR "Missing generated CI workflow: ${workflow}") endif() - run_command("${CMAKE_COMMAND}" --build "${binary}") - run_command("${CMAKE_COMMAND}" -P "${binary}/cpp-library-test-package.cmake") - message(STATUS "PASS external: semicolon-separated dependency prefixes preserved") + set(check "${binary}/cpp-library-test-package.cmake") + if(NOT EXISTS "${check}") + message(FATAL_ERROR "Missing generated packaging check: ${check}") + endif() + + expect_check_failure("${check}" "${prefix}" "Package install prefix does not exist") + run_workflow_install("${workflow}" "${binary}" "${prefix}") + run_command("${CMAKE_COMMAND}" "-DPACKAGE_TEST_PREFIX=${prefix}" -P "${check}") + + set(package_dir "lib64/cmake/stlab-single") + foreach(artifact IN ITEMS + stlab-singleConfig.cmake + stlab-singleConfigVersion.cmake + stlab-singleTargets.cmake) + set(broken_prefix "${root}/missing ${artifact}") + file(COPY "${prefix}/" DESTINATION "${broken_prefix}") + file(REMOVE "${broken_prefix}/${package_dir}/${artifact}") + expect_check_failure("${check}" "${broken_prefix}" "${artifact}") + endforeach() + + message(STATUS "PASS single: installed package artifacts are checked") endfunction() -# Rejects an existing installation when this build did not install its package. -function(run_shadow) - set(root "${test_root}/shadow") +function(run_absolute_libdir) + set(root "${test_root}/absolute libdir") set(source "${root}/source") set(binary "${root}/build") - set(old_prefix "${root}/previous installation") + set(prefix "${root}/missing prefix") + set(libdir "${root}/absolute install lib") 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}" -DCMAKE_BUILD_TYPE=Release) - run_command("${CMAKE_COMMAND}" --install "${binary}" --prefix "${old_prefix}") - run_command("${CMAKE_COMMAND}" -S "${source}" -B "${binary}" -DSTLAB_INSTALL=OFF) - execute_process(COMMAND "${CMAKE_COMMAND}" -E env "CMAKE_PREFIX_PATH=${old_prefix}" - "${CMAKE_COMMAND}" -P "${binary}/cpp-library-test-package.cmake" - RESULT_VARIABLE result OUTPUT_VARIABLE out ERROR_VARIABLE err) - if(result EQUAL 0 OR NOT "${out}\n${err}" MATCHES "stlab-single") - message(FATAL_ERROR "Packaging must reject a previously installed package:\n${out}\n${err}") - endif() - message(STATUS "PASS shadow: previous installation cannot mask missing staged package") + "-DTOOLKIT_SOURCE=${toolkit_source}" "-DCMAKE_INSTALL_LIBDIR=${libdir}") + + set(check "${binary}/cpp-library-test-package.cmake") + set(package_dir "${libdir}/cmake/stlab-single") + file(MAKE_DIRECTORY "${package_dir}") + foreach(artifact IN ITEMS + stlab-singleConfig.cmake + stlab-singleConfigVersion.cmake + stlab-singleTargets.cmake) + file(WRITE "${package_dir}/${artifact}" "stale artifact") + endforeach() + expect_check_failure("${check}" "${prefix}" "Package install prefix does not exist") endfunction() -if(CASE STREQUAL "external") - run_external() -elseif(CASE STREQUAL "shadow") - run_shadow() -elseif(DEFINED CASE) - message(FATAL_ERROR "Unknown packaging case: ${CASE}") -else() - run_case(static OFF Ninja Release ON) - run_case(shared ON Ninja Release ON) - run_case(multi-config OFF "Ninja Multi-Config" Debug ON) - run_case(transitive OFF Ninja Release ON -DPACKAGE_TEST_EXTRA=ON) - run_case(disabled-dependency OFF Ninja Release OFF) - run_single() - run_external() - run_shadow() -endif() +run_single() +run_absolute_libdir() file(REMOVE_RECURSE "${test_root}") -message(STATUS "Generated packaging checks passed!") +message(STATUS "Generated package artifact checks passed!") From 978311e94186755602fc42f3521939c1aef495fb Mon Sep 17 00:00:00 2001 From: sean-parent Date: Sat, 14 Nov 2026 20:05:54 -0800 Subject: [PATCH 3/4] Fix deferred package artifact validation Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- cmake/cpp-library-ci.cmake | 6 +- templates/test-package.cmake.in | 17 ++++-- tests/install/test_package_ci.cmake | 87 ++++++++++++++++++++++++++--- 3 files changed, 96 insertions(+), 14 deletions(-) diff --git a/cmake/cpp-library-ci.cmake b/cmake/cpp-library-ci.cmake index 0bdb361..833ed50 100644 --- a/cmake/cpp-library-ci.cmake +++ b/cmake/cpp-library-ci.cmake @@ -39,16 +39,16 @@ function(_cpp_library_setup_ci PACKAGE_NAME force_init) endfunction() # Generates an installed-artifact check after the project's directories are configured. -function(_cpp_library_generate_package_test package) +function(_cpp_library_generate_package_test package root) include(GNUInstallDirs) set(PACKAGE_TEST_NAME "${package}") set(PACKAGE_TEST_INSTALL_LIBDIR "${CMAKE_INSTALL_LIBDIR}") - configure_file("${CPP_LIBRARY_ROOT}/templates/test-package.cmake.in" + configure_file("${root}/templates/test-package.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/cpp-library-test-package.cmake" @ONLY) endfunction() # Schedules artifact-check generation without changing install rules. function(_cpp_library_setup_package_test package) cmake_language(EVAL CODE - "cmake_language(DEFER CALL _cpp_library_generate_package_test [[${package}]])") + "cmake_language(DEFER CALL _cpp_library_generate_package_test [[${package}]] [[${CPP_LIBRARY_ROOT}]])") endfunction() diff --git a/templates/test-package.cmake.in b/templates/test-package.cmake.in index 6728d71..24124f5 100644 --- a/templates/test-package.cmake.in +++ b/templates/test-package.cmake.in @@ -5,17 +5,22 @@ cmake_minimum_required(VERSION 3.24) if(NOT DEFINED PACKAGE_TEST_PREFIX OR PACKAGE_TEST_PREFIX STREQUAL "") message(FATAL_ERROR "PACKAGE_TEST_PREFIX must name the install prefix") endif() -if(NOT IS_DIRECTORY "${PACKAGE_TEST_PREFIX}") - message(FATAL_ERROR "Package install prefix does not exist: ${PACKAGE_TEST_PREFIX}") -endif() - set(package_install_dir "@PACKAGE_TEST_INSTALL_LIBDIR@/cmake/@PACKAGE_TEST_NAME@") if(IS_ABSOLUTE "${package_install_dir}") set(package_dir "${package_install_dir}") else() + if(NOT IS_DIRECTORY "${PACKAGE_TEST_PREFIX}") + message(FATAL_ERROR "Package install prefix does not exist: ${PACKAGE_TEST_PREFIX}") + endif() set(package_dir "${PACKAGE_TEST_PREFIX}/${package_install_dir}") endif() +set(manifest "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt") +if(NOT EXISTS "${manifest}") + message(FATAL_ERROR "Package install manifest is missing: ${manifest}") +endif() +file(STRINGS "${manifest}" installed_files) + foreach(artifact IN ITEMS "@PACKAGE_TEST_NAME@Config.cmake" "@PACKAGE_TEST_NAME@ConfigVersion.cmake" @@ -24,6 +29,10 @@ foreach(artifact IN ITEMS if(NOT EXISTS "${artifact_path}") message(FATAL_ERROR "Installed package artifact is missing: ${artifact_path}") endif() + cmake_path(NORMAL_PATH artifact_path) + if(NOT artifact_path IN_LIST installed_files) + message(FATAL_ERROR "Package artifact was not installed: ${artifact_path}") + endif() endforeach() message(STATUS "Installed @PACKAGE_TEST_NAME@ artifacts found in ${package_dir}") diff --git a/tests/install/test_package_ci.cmake b/tests/install/test_package_ci.cmake index 98b4026..16a0a66 100644 --- a/tests/install/test_package_ci.cmake +++ b/tests/install/test_package_ci.cmake @@ -78,10 +78,10 @@ function(run_single) stlab-singleConfig.cmake stlab-singleConfigVersion.cmake stlab-singleTargets.cmake) - set(broken_prefix "${root}/missing ${artifact}") - file(COPY "${prefix}/" DESTINATION "${broken_prefix}") - file(REMOVE "${broken_prefix}/${package_dir}/${artifact}") - expect_check_failure("${check}" "${broken_prefix}" "${artifact}") + set(path "${prefix}/${package_dir}/${artifact}") + file(RENAME "${path}" "${path}.backup") + expect_check_failure("${check}" "${prefix}" "${artifact}") + file(RENAME "${path}.backup" "${path}") endforeach() message(STATUS "PASS single: installed package artifacts are checked") @@ -107,10 +107,83 @@ function(run_absolute_libdir) stlab-singleTargets.cmake) file(WRITE "${package_dir}/${artifact}" "stale artifact") endforeach() - expect_check_failure("${check}" "${prefix}" "Package install prefix does not exist") + expect_check_failure("${check}" "${prefix}" "install_manifest.txt") +endfunction() + +function(run_function_scope) + set(root "${test_root}/function scope") + set(source "${root}/source") + set(binary "${root}/build") + file(MAKE_DIRECTORY "${source}") + file(COPY "${CMAKE_CURRENT_LIST_DIR}/fixtures/single/" DESTINATION "${source}") + file(WRITE "${source}/CMakeLists.txt" [=[ +cmake_minimum_required(VERSION 3.24) +project(single LANGUAGES CXX) +set(CPP_LIBRARY_VERSION 1.0.0) +function(setup_library) + include("${TOOLKIT_SOURCE}/cpp-library.cmake") + cpp_library_setup(DESCRIPTION "fixture" NAMESPACE stlab HEADERS single.hpp) +endfunction() +setup_library() +]=]) + run_command("${CMAKE_COMMAND}" -S "${source}" -B "${binary}" -G Ninja + "-DTOOLKIT_SOURCE=${toolkit_source}") + message(STATUS "PASS function scope: package check is generated") +endfunction() + +function(run_absolute_install stale) + set(root "${test_root}/absolute install ${stale}") + set(source "${root}/source") + set(binary "${root}/build") + set(prefix "${root}/prefix") + set(libdir "${root}/absolute lib") + set(includedir "${root}/absolute include") + file(MAKE_DIRECTORY "${source}") + file(COPY "${CMAKE_CURRENT_LIST_DIR}/fixtures/single/" DESTINATION "${source}") + set(args) + if(stale) + list(APPEND args -DSTLAB_INSTALL=OFF) + file(APPEND "${source}/CMakeLists.txt" [=[ +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/unrelated.txt" "unrelated") +install(FILES "${CMAKE_CURRENT_BINARY_DIR}/unrelated.txt" DESTINATION ".") +]=]) + set(package_dir "${libdir}/cmake/stlab-single") + file(MAKE_DIRECTORY "${package_dir}") + foreach(artifact IN ITEMS stlab-singleConfig.cmake stlab-singleConfigVersion.cmake stlab-singleTargets.cmake) + file(WRITE "${package_dir}/${artifact}" "stale") + endforeach() + endif() + run_command("${CMAKE_COMMAND}" -S "${source}" -B "${binary}" -G Ninja + "-DTOOLKIT_SOURCE=${toolkit_source}" "-DCMAKE_INSTALL_LIBDIR=${libdir}" + "-DCMAKE_INSTALL_INCLUDEDIR=${includedir}" ${args}) + run_command("${CMAKE_COMMAND}" --install "${binary}" --prefix "${prefix}") + set(check "${binary}/cpp-library-test-package.cmake") + if(stale) + expect_check_failure("${check}" "${prefix}" "stlab-singleConfig.cmake") + message(STATUS "PASS absolute install: stale artifacts rejected") + else() + if(EXISTS "${prefix}") + message(FATAL_ERROR "Absolute installation unexpectedly created ${prefix}") + endif() + run_command("${CMAKE_COMMAND}" "-DPACKAGE_TEST_PREFIX=${prefix}" -P "${check}") + message(STATUS "PASS absolute install: installed artifacts accepted") + endif() endfunction() -run_single() -run_absolute_libdir() +if(CASE STREQUAL "scope") + run_function_scope() +elseif(CASE STREQUAL "stale") + run_absolute_install(TRUE) +elseif(CASE STREQUAL "absolute") + run_absolute_install(FALSE) +elseif(DEFINED CASE) + message(FATAL_ERROR "Unknown case: ${CASE}") +else() + run_single() + run_absolute_libdir() + run_function_scope() + run_absolute_install(TRUE) + run_absolute_install(FALSE) +endif() file(REMOVE_RECURSE "${test_root}") message(STATUS "Generated package artifact checks passed!") From e2a55f01f23d788b88e48c7e727c437f8c380f1e Mon Sep 17 00:00:00 2001 From: sean-parent Date: Sat, 14 Nov 2026 20:27:35 -0800 Subject: [PATCH 4/4] Record generated CI packaging invariant Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .scout/memory/index.md | 1 + .../generated-ci-package-artifact-check.md | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 .scout/memory/knowledge/generated-ci-package-artifact-check.md diff --git a/.scout/memory/index.md b/.scout/memory/index.md index c289aeb..a28079d 100644 --- a/.scout/memory/index.md +++ b/.scout/memory/index.md @@ -7,6 +7,7 @@ - [[knowledge/deferred-install-dependency-validation-and-config-generation.md|Deferred Install Dependency Validation and Config Generation]] (pattern, 0.75) `deferred-install` `install-validation` `untracked-dependencies` `cmake-language-defer` `find-dependency` - [[knowledge/dependency-tracking-lifecycle-and-provider-ordering.md|Dependency Tracking Lifecycle and Provider Ordering]] (observation, 0.75) `dependency-tracking` `dependency-provider` `cpp-library-enable-dependency-tracking` `cmake-project-top-level-includes` `set-dependency-provider` - [[knowledge/documentation-setup-and-theme-integration-with-build-docs.md|Documentation Setup and Theme Integration with BUILD_DOCS]] (pattern, 0.75) `build-docs` `doxygen` `doxygen-awesome-css` `documentation-generation` `docs-target` +- [[knowledge/generated-ci-package-artifact-check.md|Generated CI package artifact check]] (decision, 0.95) `ci` `packaging` `cmake` - [[knowledge/target-naming-and-alias-deduplication-convention.md|Target Naming and Alias Deduplication Convention]] (pattern, 0.75) `target-naming` `alias-deduplication` `package-name` `clean-name` `cmake-alias-convention` - [[knowledge/testing-and-example-executable-target-generation-gated-by-build-testing.md|Testing and Example Executable Target Generation Gated by BUILD_TESTING]] (pattern, 0.75) `build-testing` `doctest` `testing-setup` `negative-compilation-tests` `runtime-dll-copy` diff --git a/.scout/memory/knowledge/generated-ci-package-artifact-check.md b/.scout/memory/knowledge/generated-ci-package-artifact-check.md new file mode 100644 index 0000000..0d7d9d8 --- /dev/null +++ b/.scout/memory/knowledge/generated-ci-package-artifact-check.md @@ -0,0 +1,19 @@ +--- +title: "Generated CI package artifact check" +entity_type: decision +confidence: 0.95 +created: 2026-11-15T04:12:07Z +last_accessed: 2026-11-15T04:27:16Z +last_reinforced: 2026-11-15T04:27:16Z +access_count: 2 +sources: + - "templates/.github/workflows/ci.yml.in:76-79" + - "templates/test-package.cmake.in:8-35" + - "cmake/cpp-library-ci.cmake:41-54" +tags: ["ci", "packaging", "cmake"] +tier: knowledge +--- + +# Generated CI package artifact check + +Generated consumer CI deliberately verifies only the root package's installed Config, ConfigVersion and Targets artifacts; dependency installation and downstream find_package resolution remain the consumer's responsibility. The checker requires both file existence and membership in the current CMake install manifest, so stale absolute-destination artifacts cannot pass. Absolute library destinations do not require the otherwise unused install prefix to exist. The deferred generator captures CPP_LIBRARY_ROOT when scheduled because setup may run inside a function whose scope ends before deferred calls execute. Sources: templates/.github/workflows/ci.yml.in:76-79, templates/test-package.cmake.in:8-35, cmake/cpp-library-ci.cmake:41-54.