Quellcode durchsuchen

CPack: support archive compression level

Fixes: #18117
AJIOB vor 1 Monat
Ursprung
Commit
0bca70968a

+ 18 - 0
Help/cpack_gen/archive.rst

@@ -199,3 +199,21 @@ CPack generators which are essentially archives at their core. These include:
     Official CMake binaries available on ``cmake.org`` now ship
     with a ``liblzma`` that supports parallel compression.
     Older versions did not.
+
+.. variable:: CPACK_ARCHIVE_COMPRESSION_LEVEL
+
+  .. versionadded:: 4.3
+
+  The compression level to use when compressing the archive.
+
+  :Default: value of :variable:`CPACK_COMPRESSION_LEVEL`
+
+  The compression level should be between ``0`` and ``9``.
+
+  The compression level of the Zstandard-based algorithm can be set
+  between ``0`` and ``19``, except for the ``ZIP_ZSTD`` mode.
+
+  The value ``0`` is used to specify the default compression level.
+  It is selected automatically by the archive library backend and
+  not directly set by CMake itself. The default compression level
+  may vary between archive formats, platforms, etc.

+ 6 - 6
Help/cpack_gen/deb.rst

@@ -313,7 +313,7 @@ List of CPack DEB generator specific variables:
  The compression level used for creating the Debian package.
 
  :Mandatory: No
- :Default: Automatically determined by the compression tool.
+ :Default: value of :variable:`CPACK_COMPRESSION_LEVEL`
 
  This variable allows fine-tuning of the compression ratio and speed for the
  Debian package archive. It controls the numeric compression level passed to
@@ -323,11 +323,11 @@ List of CPack DEB generator specific variables:
 
  The valid range and interpretation depend on the selected compression type:
 
-  - ``gzip``  – level 1–9 (default 6)
-  - ``bzip2`` – level 1–9 (default 9)
-  - ``xz``    – level 1–9 (default 6)
-  - ``lzma``  – level 1–9 (default 6)
-  - ``zstd``  – level 1–19 (default 3)
+  - ``gzip``  – level 1–9
+  - ``bzip2`` – level 1–9
+  - ``xz``    – level 1–9
+  - ``lzma``  – level 1–9
+  - ``zstd``  – level 1–19
 
  Example usage:
 

+ 9 - 0
Help/release/dev/cpack-archive-compression-level.rst

@@ -0,0 +1,9 @@
+cpack-archive-compression-level
+-------------------------------
+
+* :module:`CPack` gained the :variable:`CPACK_COMPRESSION_LEVEL`
+  variable to control the compression level used for creating
+  packages, that supports compression.
+* The :cpack_gen:`CPack Archive Generator` gained a new variable
+  :variable:`CPACK_ARCHIVE_COMPRESSION_LEVEL` to control the
+  compression level used when creating archive packages.

+ 18 - 0
Modules/CPack.cmake

@@ -338,6 +338,24 @@ installers.  The most commonly-used variables are:
 
   Other compression methods ignore this value and use only one thread.
 
+.. variable:: CPACK_COMPRESSION_LEVEL
+
+  .. versionadded:: 4.3
+
+  Select the compression level to use when it's applicable,
+  such as compressing the installer package.
+
+  Some compression methods used by CPack generators such as Debian or Archive
+  may take advantage of different compression levels. The accepted values
+  are in the range ``0`` to ``9``. If you select the ``zstd`` compression method,
+  you can select the compression level between ``0`` and ``19``, except the ``zip``
+  archive format.
+
+  By default ``CPACK_COMPRESSION_LEVEL`` is set to ``0``, which selects the default
+  compression level. It is selected automatically by the archive library backend and
+  not directly set by CMake itself. The default compression level
+  may vary between archive formats, platforms, etc.
+
 Variables for Source Package Generators
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 

+ 3 - 0
Modules/Internal/CPack/CPackDeb.cmake

@@ -810,6 +810,9 @@ function(cpack_deb_prepare_package_vars)
 
   if(NOT DEFINED CPACK_DEBIAN_COMPRESSION_LEVEL)
     set(CPACK_DEBIAN_COMPRESSION_LEVEL "0")
+    if(DEFINED CPACK_COMPRESSION_LEVEL)
+      set(CPACK_DEBIAN_COMPRESSION_LEVEL "${CPACK_COMPRESSION_LEVEL}")
+    endif()
   endif()
 
   # Print out some debug information if we were asked for that

+ 16 - 1
Source/CPack/cmCPackArchiveGenerator.cxx

@@ -392,7 +392,8 @@ int cmCPackArchiveGenerator::addOneComponentToArchive(
                     << (filename) << ">." << std::endl);                      \
     return 0;                                                                 \
   }                                                                           \
-  cmArchiveWrite archive(gf, this->Compress, this->ArchiveFormat, 0,          \
+  cmArchiveWrite archive(gf, this->Compress, this->ArchiveFormat,             \
+                         this->GetCompressionLevel(),                         \
                          this->GetThreadCount());                             \
   if (this->UID >= 0 && this->GID >= 0) {                                     \
     archive.SetUIDAndGID(this->UID, this->GID);                               \
@@ -595,3 +596,17 @@ int cmCPackArchiveGenerator::GetThreadCount() const
 
   return threads;
 }
+
+int cmCPackArchiveGenerator::GetCompressionLevel() const
+{
+  int level = 0;
+
+  // CPACK_ARCHIVE_COMPRESSION_LEVEL overrides CPACK_COMPRESSION_LEVEL
+  if (cmValue v = this->GetOptionIfSet("CPACK_ARCHIVE_COMPRESSION_LEVEL")) {
+    level = std::stoi(*v);
+  } else if (cmValue v2 = this->GetOptionIfSet("CPACK_COMPRESSION_LEVEL")) {
+    level = std::stoi(*v2);
+  }
+
+  return level;
+}

+ 1 - 0
Source/CPack/cmCPackArchiveGenerator.h

@@ -105,6 +105,7 @@ private:
   }
 
   int GetThreadCount() const;
+  int GetCompressionLevel() const;
 
 private:
   cmArchiveWrite::Compress Compress;

+ 1 - 0
Tests/RunCMake/CPack/7Z_DEFLATE/packaging_COMPONENT_default.cmake

@@ -1 +1,2 @@
 set(CPACK_ARCHIVE_COMPONENT_INSTALL "ON")
+set(CPACK_COMPRESSION_LEVEL 7)

+ 1 - 0
Tests/RunCMake/CPack/7Z_LZMA2/packaging_COMPONENT_default.cmake

@@ -1 +1,2 @@
 set(CPACK_ARCHIVE_COMPONENT_INSTALL "ON")
+set(CPACK_ARCHIVE_COMPRESSION_LEVEL 9)

+ 1 - 0
Tests/RunCMake/CPack/7Z_PPMD/packaging_COMPONENT_default.cmake

@@ -1 +1,2 @@
 set(CPACK_ARCHIVE_COMPONENT_INSTALL "ON")
+set(CPACK_COMPRESSION_LEVEL 2)

+ 1 - 0
Tests/RunCMake/CPack/7Z_ZSTD/packaging_COMPONENT_default.cmake

@@ -1 +1,2 @@
 set(CPACK_ARCHIVE_COMPONENT_INSTALL "ON")
+set(CPACK_ARCHIVE_COMPRESSION_LEVEL 19)

+ 1 - 0
Tests/RunCMake/CPack/TGZ/packaging_COMPONENT_default.cmake

@@ -1 +1,2 @@
 set(CPACK_ARCHIVE_COMPONENT_INSTALL "ON")
+set(CPACK_COMPRESSION_LEVEL 1)

+ 1 - 0
Tests/RunCMake/CPack/TXZ/packaging_COMPONENT_default.cmake

@@ -1 +1,2 @@
 set(CPACK_ARCHIVE_COMPONENT_INSTALL "ON")
+set(CPACK_ARCHIVE_COMPRESSION_LEVEL 4)

+ 1 - 0
Tests/RunCMake/CPack/ZIP_DEFLATE/packaging_COMPONENT_default.cmake

@@ -1 +1,2 @@
 set(CPACK_ARCHIVE_COMPONENT_INSTALL "ON")
+set(CPACK_ARCHIVE_COMPRESSION_LEVEL 4)

+ 1 - 0
Tests/RunCMake/CPack/ZIP_LZMA2/packaging_COMPONENT_default.cmake

@@ -1 +1,2 @@
 set(CPACK_ARCHIVE_COMPONENT_INSTALL "ON")
+set(CPACK_ARCHIVE_COMPRESSION_LEVEL 8)