CPack: Introduce pre- and post- build actions

CPack learned the `CPACK_PRE_BUILD_SCRIPTS`, `CPACK_POST_BUILD_SCRIPTS`,
and `CPACK_PACKAGE_FILES` variables.

The first two are lists of scripts to perform
- after pre-install files into a staging directory and before
  producing the resulting packages
- after produsing the packages

The post-build script(s) also get the list of actually produced
packages in the `CPACK_PACKAGE_FILES`.

Issue: #19077
This commit is contained in:
Alex Turbov 2020-06-03 02:10:08 +08:00 committed by Brad King
parent 43b10e2411
commit 915409af49
10 changed files with 104 additions and 6 deletions

View File

@ -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.

View File

@ -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

View File

@ -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;
}

View File

@ -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")

View File

@ -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()

View File

@ -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

View File

@ -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

View File

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

View File

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

View File

@ -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()