From 2d9ae9de96f5d2206d758fd410124f292a3b8237 Mon Sep 17 00:00:00 2001 From: Nikita Nemkin Date: Wed, 12 Mar 2025 16:12:02 +0500 Subject: 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 --- Modules/CPack.cmake | 5 +++++ Source/CPack/cmCPackGenerator.cxx | 6 +++++- .../CPackCommandLine/ProjectConfigMissing-package-stderr.txt | 1 + Tests/RunCMake/CPackCommandLine/ProjectConfigMissing.cmake | 2 ++ .../CPackCommandLine/ProjectConfigRelative-package-stdout.txt | 1 + Tests/RunCMake/CPackCommandLine/ProjectConfigRelative.cmake | 2 ++ Tests/RunCMake/CPackCommandLine/RunCMakeTest.cmake | 10 ++++++++++ Tests/RunCMake/CPackCommandLine/project_config_example.cmake | 1 + 8 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 Tests/RunCMake/CPackCommandLine/ProjectConfigMissing-package-stderr.txt create mode 100644 Tests/RunCMake/CPackCommandLine/ProjectConfigMissing.cmake create mode 100644 Tests/RunCMake/CPackCommandLine/ProjectConfigRelative-package-stdout.txt create mode 100644 Tests/RunCMake/CPackCommandLine/ProjectConfigRelative.cmake create mode 100644 Tests/RunCMake/CPackCommandLine/project_config_example.cmake diff --git a/Modules/CPack.cmake b/Modules/CPack.cmake index 026b8c0..803332b 100644 --- a/Modules/CPack.cmake +++ b/Modules/CPack.cmake @@ -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}") diff --git a/Source/CPack/cmCPackGenerator.cxx b/Source/CPack/cmCPackGenerator.cxx index f725580..fac06bf 100644 --- a/Source/CPack/cmCPackGenerator.cxx +++ b/Source/CPack/cmCPackGenerator.cxx @@ -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()) { diff --git a/Tests/RunCMake/CPackCommandLine/ProjectConfigMissing-package-stderr.txt b/Tests/RunCMake/CPackCommandLine/ProjectConfigMissing-package-stderr.txt new file mode 100644 index 0000000..40ec10d --- /dev/null +++ b/Tests/RunCMake/CPackCommandLine/ProjectConfigMissing-package-stderr.txt @@ -0,0 +1 @@ +^CPack Warning: CPACK_PROJECT_CONFIG_FILE not found: .*/missing.cmake diff --git a/Tests/RunCMake/CPackCommandLine/ProjectConfigMissing.cmake b/Tests/RunCMake/CPackCommandLine/ProjectConfigMissing.cmake new file mode 100644 index 0000000..23632f3 --- /dev/null +++ b/Tests/RunCMake/CPackCommandLine/ProjectConfigMissing.cmake @@ -0,0 +1,2 @@ +set(CPACK_PROJECT_CONFIG_FILE "missing.cmake") +include(CPack) diff --git a/Tests/RunCMake/CPackCommandLine/ProjectConfigRelative-package-stdout.txt b/Tests/RunCMake/CPackCommandLine/ProjectConfigRelative-package-stdout.txt new file mode 100644 index 0000000..1771741 --- /dev/null +++ b/Tests/RunCMake/CPackCommandLine/ProjectConfigRelative-package-stdout.txt @@ -0,0 +1 @@ +project_config_example.cmake is included diff --git a/Tests/RunCMake/CPackCommandLine/ProjectConfigRelative.cmake b/Tests/RunCMake/CPackCommandLine/ProjectConfigRelative.cmake new file mode 100644 index 0000000..c2a6894 --- /dev/null +++ b/Tests/RunCMake/CPackCommandLine/ProjectConfigRelative.cmake @@ -0,0 +1,2 @@ +set(CPACK_PROJECT_CONFIG_FILE "project_config_example.cmake") +include(CPack) diff --git a/Tests/RunCMake/CPackCommandLine/RunCMakeTest.cmake b/Tests/RunCMake/CPackCommandLine/RunCMakeTest.cmake index 53f4e4f..4c2e83a 100644 --- a/Tests/RunCMake/CPackCommandLine/RunCMakeTest.cmake +++ b/Tests/RunCMake/CPackCommandLine/RunCMakeTest.cmake @@ -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) diff --git a/Tests/RunCMake/CPackCommandLine/project_config_example.cmake b/Tests/RunCMake/CPackCommandLine/project_config_example.cmake new file mode 100644 index 0000000..aa476ae --- /dev/null +++ b/Tests/RunCMake/CPackCommandLine/project_config_example.cmake @@ -0,0 +1 @@ +message(STATUS "project_config_example.cmake is included") -- cgit v0.12