CPack: support archive compression level

Fixes: #18117
This commit is contained in:
AJIOB 2025-12-22 12:58:14 +03:00 committed by Brad King
parent 54f926c740
commit 0bca70968a
15 changed files with 79 additions and 7 deletions

View File

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

View File

@ -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 19 (default 6)
- ``bzip2`` level 19 (default 9)
- ``xz`` level 19 (default 6)
- ``lzma`` level 19 (default 6)
- ``zstd`` level 119 (default 3)
- ``gzip`` level 19
- ``bzip2`` level 19
- ``xz`` level 19
- ``lzma`` level 19
- ``zstd`` level 119
Example usage:

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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