Explorar o código

Merge topic 'cpack-pre-and-post-scripts'

915409af49 CPack: Introduce pre- and post- build actions

Acked-by: Kitware Robot <[email protected]>
Merge-request: !4846
Brad King %!s(int64=5) %!d(string=hai) anos
pai
achega
f2c1debe40

+ 5 - 0
Help/release/dev/cpack-pre-and-post-build-scripts.rst

@@ -0,0 +1,5 @@
+cpack-pre-and-post-build-scripts
+--------------------------------
+
+* CPack learned the :variable:`CPACK_PRE_BUILD_SCRIPTS`, :variable:`CPACK_POST_BUILD_SCRIPTS`,
+  and :variable:`CPACK_PACKAGE_FILES` variables.

+ 15 - 0
Modules/CPack.cmake

@@ -386,6 +386,21 @@ The following variables are for advanced uses of CPack:
   select the CPack generator(s) to be used when building the ``package``
   target or when running :manual:`cpack <cpack(1)>` without the ``-G`` option.
 
+.. variable:: CPACK_PRE_BUILD_SCRIPTS
+
+  List of CMake scripts to execute after CPack has installed the files to
+  be packed into a staging directory and before producing the result
+  packages.
+
+.. variable:: CPACK_POST_BUILD_SCRIPTS
+
+  List of CMake scripts to execute after CPack has produced the result
+  packages and before copying them back to a build directory.
+
+.. variable:: CPACK_PACKAGE_FILES
+
+  List of resulting package files passed to the ``CPACK_POST_BUILD_SCRIPTS``.
+
 #]=======================================================================]
 
 # Define this var in order to avoid (or warn) concerning multiple inclusion

+ 44 - 6
Source/CPack/cmCPackGenerator.cxx

@@ -264,6 +264,23 @@ int cmCPackGenerator::InstallProject()
     return 0;
   }
 
+  // Run pre-build actions
+  const char* preBuildScripts = this->GetOption("CPACK_PRE_BUILD_SCRIPTS");
+  if (preBuildScripts) {
+    const auto scripts = cmExpandedList(preBuildScripts, false);
+    for (const auto& script : scripts) {
+      cmCPackLogger(cmCPackLog::LOG_OUTPUT,
+                    "Executing pre-build script: " << script << std::endl);
+
+      if (!this->MakefileMap->ReadListFile(script)) {
+        cmCPackLogger(cmCPackLog::LOG_ERROR,
+                      "The pre-build script not found: " << script
+                                                         << std::endl);
+        return 0;
+      }
+    }
+  }
+
   if (setDestDir) {
     cmSystemTools::PutEnv("DESTDIR=");
   }
@@ -333,7 +350,8 @@ int cmCPackGenerator::InstallProjectViaInstalledDirectories(
     if (installDirectoriesVector.size() % 2 != 0) {
       cmCPackLogger(
         cmCPackLog::LOG_ERROR,
-        "CPACK_INSTALLED_DIRECTORIES should contain pairs of <directory> and "
+        "CPACK_INSTALLED_DIRECTORIES should contain pairs of <directory> "
+        "and "
         "<subdirectory>. The <subdirectory> can be '.' to be installed in "
         "the toplevel directory of installation."
           << std::endl);
@@ -475,10 +493,10 @@ int cmCPackGenerator::InstallProjectViaInstallScript(
                     "- Install script: " << installScript << std::endl);
 
       if (setDestDir) {
-        // For DESTDIR based packaging, use the *project* CMAKE_INSTALL_PREFIX
-        // underneath the tempInstallDirectory. The value of the project's
-        // CMAKE_INSTALL_PREFIX is sent in here as the value of the
-        // CPACK_INSTALL_PREFIX variable.
+        // For DESTDIR based packaging, use the *project*
+        // CMAKE_INSTALL_PREFIX underneath the tempInstallDirectory. The
+        // value of the project's CMAKE_INSTALL_PREFIX is sent in here as the
+        // value of the CPACK_INSTALL_PREFIX variable.
 
         std::string dir;
         if (this->GetOption("CPACK_INSTALL_PREFIX")) {
@@ -1076,6 +1094,25 @@ int cmCPackGenerator::DoPackage()
       return 0;
     }
   }
+  // Run post-build actions
+  const char* postBuildScripts = this->GetOption("CPACK_POST_BUILD_SCRIPTS");
+  if (postBuildScripts) {
+    this->MakefileMap->AddDefinition("CPACK_PACKAGE_FILES",
+                                     cmJoin(this->packageFileNames, ";"));
+
+    const auto scripts = cmExpandedList(postBuildScripts, false);
+    for (const auto& script : scripts) {
+      cmCPackLogger(cmCPackLog::LOG_OUTPUT,
+                    "Executing post-build script: " << script << std::endl);
+
+      if (!this->MakefileMap->ReadListFile(script)) {
+        cmCPackLogger(cmCPackLog::LOG_ERROR,
+                      "The post-build script not found: " << script
+                                                          << std::endl);
+        return 0;
+      }
+    }
+  }
 
   /* Prepare checksum algorithm*/
   const char* algo = this->GetOption("CPACK_PACKAGE_CHECKSUM");
@@ -1378,7 +1415,8 @@ int cmCPackGenerator::PrepareGroupingKind()
           << std::endl);
   }
 
-  // if user specified packaging method, override the default packaging method
+  // if user specified packaging method, override the default packaging
+  // method
   if (method != UNKNOWN_COMPONENT_PACKAGE_METHOD) {
     componentPackageMethod = method;
   }

+ 1 - 0
Tests/RunCMake/CPack/RunCMakeTest.cmake

@@ -46,3 +46,4 @@ run_cpack_test_subtests(
   "MONOLITHIC;COMPONENT"
 )
 run_cpack_test(PROJECT_META "RPM.PROJECT_META;DEB.PROJECT_META" false "MONOLITHIC;COMPONENT")
+run_cpack_test_package_target(PRE_POST_SCRIPTS "ZIP" false "MONOLITHIC;COMPONENT")

+ 19 - 0
Tests/RunCMake/CPack/tests/PRE_POST_SCRIPTS/ExpectedFiles.cmake

@@ -0,0 +1,19 @@
+set(SATU "/satu;/satu/CMakeLists.txt")
+set(DUA "/dua;/dua/CMakeLists.txt")
+
+if(GENERATOR_TYPE STREQUAL ZIP)
+    set(_ext "zip")
+elseif(GENERATOR_TYPE STREQUAL TGZ)
+    set(_ext "tar.gz")
+endif()
+
+if(PACKAGING_TYPE STREQUAL "COMPONENT")
+    set(EXPECTED_FILES_COUNT "2")
+    set(EXPECTED_FILE_1 "*-satu.${_ext}")
+    set(EXPECTED_FILE_CONTENT_1_LIST ${SATU})
+    set(EXPECTED_FILE_2 "*-dua.${_ext}")
+    set(EXPECTED_FILE_CONTENT_2_LIST ${DUA})
+else()
+    set(EXPECTED_FILES_COUNT "1")
+    set(EXPECTED_FILE_CONTENT_1_LIST ${SATU} ${DUA})
+endif()

+ 4 - 0
Tests/RunCMake/CPack/tests/PRE_POST_SCRIPTS/ZIP_COMPONENT-stdout.txt

@@ -0,0 +1,4 @@
+-- The message from .*/Tests/RunCMake/CPack/tests/PRE_POST_SCRIPTS/pre\.cmake and generator ZIP
+.*
+-- The message from .*/Tests/RunCMake/CPack/tests/PRE_POST_SCRIPTS/post\.cmake and generator ZIP
+-- Built packages: .*/_CPack_Packages/.*/pre_post_scripts-.*-dua.zip;.*/_CPack_Packages/.*/pre_post_scripts-.*-satu.zip

+ 4 - 0
Tests/RunCMake/CPack/tests/PRE_POST_SCRIPTS/ZIP_MONOLITHIC-stdout.txt

@@ -0,0 +1,4 @@
+-- The message from .*/Tests/RunCMake/CPack/tests/PRE_POST_SCRIPTS/pre\.cmake and generator ZIP
+.*
+-- The message from .*/Tests/RunCMake/CPack/tests/PRE_POST_SCRIPTS/post\.cmake and generator ZIP
+-- Built packages: .*/_CPack_Packages/.*/pre_post_scripts-0\.1\.1-.*\.zip

+ 2 - 0
Tests/RunCMake/CPack/tests/PRE_POST_SCRIPTS/post.cmake

@@ -0,0 +1,2 @@
+message(STATUS "The message from ${CMAKE_CURRENT_LIST_FILE} and generator ${CPACK_GENERATOR}")
+message(STATUS "Built packages: ${CPACK_PACKAGE_FILES}")

+ 1 - 0
Tests/RunCMake/CPack/tests/PRE_POST_SCRIPTS/pre.cmake

@@ -0,0 +1 @@
+message(STATUS "The message from ${CMAKE_CURRENT_LIST_FILE} and generator ${CPACK_GENERATOR}")

+ 9 - 0
Tests/RunCMake/CPack/tests/PRE_POST_SCRIPTS/test.cmake

@@ -0,0 +1,9 @@
+install(FILES CMakeLists.txt DESTINATION satu COMPONENT satu)
+install(FILES CMakeLists.txt DESTINATION dua COMPONENT dua)
+
+set(CPACK_PRE_BUILD_SCRIPTS "${CMAKE_CURRENT_LIST_DIR}/pre.cmake")
+set(CPACK_POST_BUILD_SCRIPTS "${CMAKE_CURRENT_LIST_DIR}/post.cmake")
+
+if(PACKAGING_TYPE STREQUAL "COMPONENT")
+  set(CPACK_COMPONENTS_ALL satu dua)
+endif()