瀏覽代碼

Merge topic 'cpack-install-opts'

26e36111d3 CPack: Implement new variable CPACK_CUSTOM_INSTALL_VARIABLES

Acked-by: Kitware Robot <[email protected]>
Merge-request: !6141
Craig Scott 4 年之前
父節點
當前提交
445c73d3fa

+ 1 - 0
Help/manual/cmake-variables.7.rst

@@ -671,6 +671,7 @@ Variables for CPack
 
    /variable/CPACK_ABSOLUTE_DESTINATION_FILES
    /variable/CPACK_COMPONENT_INCLUDE_TOPLEVEL_DIRECTORY
+   /variable/CPACK_CUSTOM_INSTALL_VARIABLES
    /variable/CPACK_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION
    /variable/CPACK_INCLUDE_TOPLEVEL_DIRECTORY
    /variable/CPACK_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS

+ 6 - 0
Help/release/dev/cpack-install-opts.rst

@@ -0,0 +1,6 @@
+cpack-install-opts
+------------------
+
+* The new :variable:`CPACK_CUSTOM_INSTALL_VARIABLES`
+  can be used to set variables in CPack ``cmake_install.cmake``
+  invocations.

+ 42 - 0
Help/variable/CPACK_CUSTOM_INSTALL_VARIABLES.rst

@@ -0,0 +1,42 @@
+CPACK_CUSTOM_INSTALL_VARIABLES
+------------------------------
+
+CPack variables (set via e.g. ``cpack -D``, ``CPackConfig.cmake`` or
+:variable:`CPACK_PROJECT_CONFIG_FILE` scripts) are not directly visible in
+installation scripts.  Instead, one can pass a list of ``varName=value``
+pairs in the ``CPACK_CUSTOM_INSTALL_VARIABLES`` variable.  At install time,
+each list item will result in a variable of the specified name (``varName``)
+being set to the given ``value``.  The ``=`` can be omitted for an empty
+``value``.
+
+``CPACK_CUSTOM_INSTALL_VARIABLES`` allows the packaging installation to be
+influenced by the user or driving script at CPack runtime without having to
+regenerate the install scripts.
+
+Example
+"""""""
+
+.. code-block:: cmake
+
+  install(FILES large.txt DESTINATION data)
+
+  install(CODE [[
+    if(ENABLE_COMPRESSION)
+      # "run-compressor" is a fictional tool that produces
+      # large.txt.xz from large.txt and then removes the input file
+      execute_process(COMMAND run-compressor $ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/large.txt)
+    endif()
+  ]])
+
+With the above example snippet, :manual:`cpack <cpack(1)>` will by default
+run the installation script with ``ENABLE_COMPRESSION`` unset, resulting in
+a package containing the uncompressed ``large.txt``.  This can be overridden
+when invoking :manual:`cpack <cpack(1)>` like so:
+
+.. code-block:: shell
+
+  cpack -D "CPACK_CUSTOM_INSTALL_VARIABLES=ENABLE_COMPRESSION=TRUE"
+
+The installation script will then run with ``ENABLE_COMPRESSION`` set to
+``TRUE``, resulting in a package containing the compressed ``large.txt.xz``
+instead.

+ 17 - 0
Source/CPack/cmCPackGenerator.cxx

@@ -902,6 +902,23 @@ int cmCPackGenerator::InstallCMakeProject(
       this->IsOn("CPACK_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION")) {
     mf.AddDefinition("CMAKE_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION", "1");
   }
+
+  std::vector<std::string> custom_variables;
+  this->MakefileMap->GetDefExpandList("CPACK_CUSTOM_INSTALL_VARIABLES",
+                                      custom_variables);
+
+  for (auto const& custom_variable : custom_variables) {
+    std::string value;
+
+    auto i = custom_variable.find('=');
+
+    if (i != std::string::npos) {
+      value = custom_variable.substr(i + 1);
+    }
+
+    mf.AddDefinition(custom_variable.substr(0, i), value);
+  }
+
   // do installation
   bool res = mf.ReadListFile(installFile);
   // forward definition of CMAKE_ABSOLUTE_DESTINATION_FILES

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

@@ -37,6 +37,7 @@ unset(ENVIRONMENT)
 run_cpack_test(USER_FILELIST "RPM.USER_FILELIST" false "MONOLITHIC")
 run_cpack_test(MD5SUMS "DEB.MD5SUMS" false "MONOLITHIC;COMPONENT")
 run_cpack_test_subtests(CPACK_INSTALL_SCRIPTS "singular;plural;both" "ZIP" false "MONOLITHIC")
+run_cpack_test(CPACK_CUSTOM_INSTALL_VARIABLES "ZIP" false "MONOLITHIC")
 run_cpack_test(DEB_PACKAGE_VERSION_BACK_COMPATIBILITY "DEB.DEB_PACKAGE_VERSION_BACK_COMPATIBILITY" false "MONOLITHIC;COMPONENT")
 run_cpack_test_subtests(EXTERNAL "none;good;good_multi;bad_major;bad_minor;invalid_good;invalid_bad;stage_and_package" "External" false "MONOLITHIC;COMPONENT")
 run_cpack_test_subtests(

+ 7 - 0
Tests/RunCMake/CPack/tests/CPACK_CUSTOM_INSTALL_VARIABLES/ExpectedFiles.cmake

@@ -0,0 +1,7 @@
+set(EXPECTED_FILES_COUNT "1")
+set(EXPECTED_FILE_CONTENT_1_LIST
+  "/foo"
+  "/foo/bar.txt"
+  "/foo/baz.txt"
+  "/foo/foo.txt"
+)

+ 7 - 0
Tests/RunCMake/CPack/tests/CPACK_CUSTOM_INSTALL_VARIABLES/test.cmake

@@ -0,0 +1,7 @@
+set(CPACK_CUSTOM_INSTALL_VARIABLES "FOO=foo.txt" "BAR=bar.txt")
+
+install(CODE [[
+  file(WRITE ${CMAKE_INSTALL_PREFIX}/foo/${FOO})
+  file(WRITE ${CMAKE_INSTALL_PREFIX}/foo/${BAR})
+  file(WRITE ${CMAKE_INSTALL_PREFIX}/foo/baz.txt)
+]])