Skip to content

Commit cebdac8

Browse files
committed
Defer config file installation until after validation
Moves export and config file installation steps into the deferred validation function to ensure install-time validation runs before any config files are written. This prevents broken or incomplete config files from being installed if dependency validation fails.
1 parent 13f8ba9 commit cebdac8

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

cmake/cpp-library-install.cmake

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -475,34 +475,24 @@ function(_cpp_library_setup_install)
475475
cmake_language(DEFER DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
476476
CALL _cpp_library_deferred_generate_config)
477477

478-
# Defer install validation setup until after config generation
479-
# This ensures the unverified deps file is created first
478+
# Defer install validation and file installation setup until after config generation
479+
# This ensures:
480+
# 1. The unverified deps file is created first
481+
# 2. Validation install code is registered before export/config file installation
482+
# 3. At install time, validation runs before any config files are written
480483
cmake_language(DEFER DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
481484
CALL _cpp_library_setup_install_validation)
482485

483-
# Install export targets with namespace
484-
# This allows downstream projects to use find_package(package-name)
485-
# and link against namespace::target
486-
install(EXPORT ${ARG_NAME}Targets
487-
FILE ${ARG_PACKAGE_NAME}Targets.cmake
488-
NAMESPACE ${ARG_NAMESPACE}::
489-
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${ARG_PACKAGE_NAME}
490-
)
491-
492-
# Install package config and version files
493-
install(FILES
494-
"${CMAKE_CURRENT_BINARY_DIR}/${ARG_PACKAGE_NAME}Config.cmake"
495-
"${CMAKE_CURRENT_BINARY_DIR}/${ARG_PACKAGE_NAME}ConfigVersion.cmake"
496-
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${ARG_PACKAGE_NAME}
497-
)
498-
499486
endfunction()
500487

501488
# Deferred function to setup install validation after config generation
502489
# This runs after _cpp_library_deferred_generate_config() has created the unverified deps file
490+
# Registers validation BEFORE export/config file installation to prevent broken configs from being written
503491
function(_cpp_library_setup_install_validation)
504492
# Retrieve stored arguments from global properties (set by _cpp_library_setup_install)
493+
get_property(NAME GLOBAL PROPERTY _CPP_LIBRARY_DEFERRED_INSTALL_NAME)
505494
get_property(PACKAGE_NAME GLOBAL PROPERTY _CPP_LIBRARY_DEFERRED_INSTALL_PACKAGE_NAME)
495+
get_property(NAMESPACE GLOBAL PROPERTY _CPP_LIBRARY_DEFERRED_INSTALL_NAMESPACE)
506496
get_property(BINARY_DIR GLOBAL PROPERTY _CPP_LIBRARY_DEFERRED_INSTALL_BINARY_DIR)
507497

508498
# Check if there are unverified dependencies
@@ -522,7 +512,7 @@ function(_cpp_library_setup_install_validation)
522512
if(_UNVERIFIED_DEPS_LIST)
523513
# Parse the unverified dependencies list
524514
string(REPLACE \";\" \"\\n - \" FORMATTED_DEPS \"\${_UNVERIFIED_DEPS_LIST}\")
525-
string(REGEX REPLACE \"\\\\|[^\\n]+\" \"\" FORMATTED_DEPS \"\${FORMATTED_DEPS}\")
515+
string(REGEX REPLACE \"\\\\|[a-zA-Z0-9_:.\\\\- ]+\" \"\" FORMATTED_DEPS \"\${FORMATTED_DEPS}\")
526516
527517
message(FATAL_ERROR
528518
\"cpp-library: Cannot install ${PACKAGE_NAME} - untracked dependencies detected:\\n\"
@@ -545,4 +535,23 @@ function(_cpp_library_setup_install_validation)
545535
message(STATUS \"cpp-library: All dependencies properly tracked for ${PACKAGE_NAME}\")
546536
")
547537
endif()
538+
539+
# Now register the export and config file installations AFTER validation
540+
# This ensures validation runs first at install time, preventing broken configs from being written
541+
542+
# Install export targets with namespace
543+
# This allows downstream projects to use find_package(package-name)
544+
# and link against namespace::target
545+
install(EXPORT ${NAME}Targets
546+
FILE ${PACKAGE_NAME}Targets.cmake
547+
NAMESPACE ${NAMESPACE}::
548+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PACKAGE_NAME}
549+
)
550+
551+
# Install package config and version files
552+
install(FILES
553+
"${BINARY_DIR}/${PACKAGE_NAME}Config.cmake"
554+
"${BINARY_DIR}/${PACKAGE_NAME}ConfigVersion.cmake"
555+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PACKAGE_NAME}
556+
)
548557
endfunction()

0 commit comments

Comments
 (0)