From 915409af490c253924acbb58ea97c809ff8e631c Mon Sep 17 00:00:00 2001 From: Alex Turbov Date: Wed, 3 Jun 2020 02:10:08 +0800 Subject: 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 --- .../dev/cpack-pre-and-post-build-scripts.rst | 5 +++ Modules/CPack.cmake | 15 +++++++ Source/CPack/cmCPackGenerator.cxx | 50 +++++++++++++++++++--- Tests/RunCMake/CPack/RunCMakeTest.cmake | 1 + .../tests/PRE_POST_SCRIPTS/ExpectedFiles.cmake | 19 ++++++++ .../PRE_POST_SCRIPTS/ZIP_COMPONENT-stdout.txt | 4 ++ .../PRE_POST_SCRIPTS/ZIP_MONOLITHIC-stdout.txt | 4 ++ .../CPack/tests/PRE_POST_SCRIPTS/post.cmake | 2 + .../CPack/tests/PRE_POST_SCRIPTS/pre.cmake | 1 + .../CPack/tests/PRE_POST_SCRIPTS/test.cmake | 9 ++++ 10 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 Help/release/dev/cpack-pre-and-post-build-scripts.rst create mode 100644 Tests/RunCMake/CPack/tests/PRE_POST_SCRIPTS/ExpectedFiles.cmake create mode 100644 Tests/RunCMake/CPack/tests/PRE_POST_SCRIPTS/ZIP_COMPONENT-stdout.txt create mode 100644 Tests/RunCMake/CPack/tests/PRE_POST_SCRIPTS/ZIP_MONOLITHIC-stdout.txt create mode 100644 Tests/RunCMake/CPack/tests/PRE_POST_SCRIPTS/post.cmake create mode 100644 Tests/RunCMake/CPack/tests/PRE_POST_SCRIPTS/pre.cmake create mode 100644 Tests/RunCMake/CPack/tests/PRE_POST_SCRIPTS/test.cmake diff --git a/Help/release/dev/cpack-pre-and-post-build-scripts.rst b/Help/release/dev/cpack-pre-and-post-build-scripts.rst new file mode 100644 index 0000000..bf7958b --- /dev/null +++ b/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. diff --git a/Modules/CPack.cmake b/Modules/CPack.cmake index 6234b9d..553b966 100644 --- a/Modules/CPack.cmake +++ b/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 ` 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 diff --git a/Source/CPack/cmCPackGenerator.cxx b/Source/CPack/cmCPackGenerator.cxx index 288dc58..7990504 100644 --- a/Source/CPack/cmCPackGenerator.cxx +++ b/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 and " + "CPACK_INSTALLED_DIRECTORIES should contain pairs of " + "and " ". The 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; } diff --git a/Tests/RunCMake/CPack/RunCMakeTest.cmake b/Tests/RunCMake/CPack/RunCMakeTest.cmake index 064b4dc..831f430 100644 --- a/Tests/RunCMake/CPack/RunCMakeTest.cmake +++ b/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") diff --git a/Tests/RunCMake/CPack/tests/PRE_POST_SCRIPTS/ExpectedFiles.cmake b/Tests/RunCMake/CPack/tests/PRE_POST_SCRIPTS/ExpectedFiles.cmake new file mode 100644 index 0000000..63a36a3 --- /dev/null +++ b/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() diff --git a/Tests/RunCMake/CPack/tests/PRE_POST_SCRIPTS/ZIP_COMPONENT-stdout.txt b/Tests/RunCMake/CPack/tests/PRE_POST_SCRIPTS/ZIP_COMPONENT-stdout.txt new file mode 100644 index 0000000..319d0da --- /dev/null +++ b/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 diff --git a/Tests/RunCMake/CPack/tests/PRE_POST_SCRIPTS/ZIP_MONOLITHIC-stdout.txt b/Tests/RunCMake/CPack/tests/PRE_POST_SCRIPTS/ZIP_MONOLITHIC-stdout.txt new file mode 100644 index 0000000..632c4d1 --- /dev/null +++ b/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 diff --git a/Tests/RunCMake/CPack/tests/PRE_POST_SCRIPTS/post.cmake b/Tests/RunCMake/CPack/tests/PRE_POST_SCRIPTS/post.cmake new file mode 100644 index 0000000..cf0b149 --- /dev/null +++ b/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}") diff --git a/Tests/RunCMake/CPack/tests/PRE_POST_SCRIPTS/pre.cmake b/Tests/RunCMake/CPack/tests/PRE_POST_SCRIPTS/pre.cmake new file mode 100644 index 0000000..b04aa6b --- /dev/null +++ b/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}") diff --git a/Tests/RunCMake/CPack/tests/PRE_POST_SCRIPTS/test.cmake b/Tests/RunCMake/CPack/tests/PRE_POST_SCRIPTS/test.cmake new file mode 100644 index 0000000..f1b6d5f --- /dev/null +++ b/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() -- cgit v0.12