CPack: Allow source-relative CPACK_PROJECT_CONFIG_FILE

Resolve relative CPACK_PROJECT_CONFIG_FILE explicitly at config
generation time. Otherwise, it will be resolved at runtime
relative to the CPack execution directory (which could be anything).

Additionally, issue a warning if reading PACK_PROJECT_CONFIG_FILE
fails at runtime.

Fixes: #15522
This commit is contained in:
Nikita Nemkin 2025-03-12 16:12:02 +05:00
parent 4dc16577c5
commit 2d9ae9de96
8 changed files with 27 additions and 1 deletions

View File

@ -572,6 +572,11 @@ function(_cpack_escape_for_cmake var value)
set("${var}" "${escaped}" PARENT_SCOPE)
endfunction()
# Resolve CPACK_PROJECT_CONFIG_FILE relative to the source directory
if(DEFINED CPACK_PROJECT_CONFIG_FILE)
cmake_path(ABSOLUTE_PATH CPACK_PROJECT_CONFIG_FILE)
endif()
# Set the package name
_cpack_set_default(CPACK_PACKAGE_NAME "${CMAKE_PROJECT_NAME}")

View File

@ -1239,7 +1239,11 @@ int cmCPackGenerator::Initialize(std::string const& name, cmMakefile* mf)
// Load the project specific config file
cmValue config = this->GetOption("CPACK_PROJECT_CONFIG_FILE");
if (config) {
mf->ReadListFile(*config);
if (!mf->ReadListFile(*config)) {
cmCPackLogger(cmCPackLog::LOG_WARNING,
"CPACK_PROJECT_CONFIG_FILE not found: " << *config
<< std::endl);
}
}
int result = this->InitializeInternal();
if (cmSystemTools::GetErrorOccurredFlag()) {

View File

@ -0,0 +1 @@
^CPack Warning: CPACK_PROJECT_CONFIG_FILE not found: .*/missing.cmake

View File

@ -0,0 +1,2 @@
set(CPACK_PROJECT_CONFIG_FILE "missing.cmake")
include(CPack)

View File

@ -0,0 +1 @@
project_config_example.cmake is included

View File

@ -0,0 +1,2 @@
set(CPACK_PROJECT_CONFIG_FILE "project_config_example.cmake")
include(CPack)

View File

@ -31,3 +31,13 @@ endfunction()
if(RunCMake_GENERATOR MATCHES "Visual Studio|Xcode")
run_MultiConfig()
endif()
function(run_cpack_test name)
set(RunCMake_TEST_BINARY_DIR "${RunCMake_BINARY_DIR}/${name}-build")
run_cmake(${name})
set(RunCMake_TEST_NO_CLEAN 1)
run_cmake_command(${name}-package ${CMAKE_CPACK_COMMAND} -G ZIP)
endfunction()
run_cpack_test(ProjectConfigMissing)
run_cpack_test(ProjectConfigRelative)

View File

@ -0,0 +1 @@
message(STATUS "project_config_example.cmake is included")