Browse Source

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
Nikita Nemkin 8 months ago
parent
commit
2d9ae9de96

+ 5 - 0
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}")
 

+ 5 - 1
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()) {

+ 1 - 0
Tests/RunCMake/CPackCommandLine/ProjectConfigMissing-package-stderr.txt

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

+ 2 - 0
Tests/RunCMake/CPackCommandLine/ProjectConfigMissing.cmake

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

+ 1 - 0
Tests/RunCMake/CPackCommandLine/ProjectConfigRelative-package-stdout.txt

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

+ 2 - 0
Tests/RunCMake/CPackCommandLine/ProjectConfigRelative.cmake

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

+ 10 - 0
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)

+ 1 - 0
Tests/RunCMake/CPackCommandLine/project_config_example.cmake

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