CMake Zstd compression

Zstd is an open file compression standard. Zstd has become widely used and is incorporated in the Linux kernel. We use Zstd for data archiving particularly for large files where size and speed are a concern. CMake ≥ 3.15 supports Zstd compression. CMake 3.18 added file(ARCHIVE_CREATE) and file(ARCHIVE_EXTRACT) that also support Zstd. Zstd is vendored into CMake, so there is no need to worry about system shared libraries for Zstd.

The current method to create “archive” of all files under a directory “in_dir” currently requires syntax like:

# need working_directory ${in} to avoid computer-specific relative paths
# use . not ${in} as last argument to avoid more relative path issues

set(archive "my.zst")
set(in_dir "data/")

execute_process(
  COMMAND ${CMAKE_COMMAND} -E tar c ${archive} --zstd .
  WORKING_DIRECTORY ${in_dir})

The syntax to create traditional .zip files is very similar:

set(archive "my.zip")

execute_process(
    COMMAND ${CMAKE_COMMAND} -E tar c ${archive} --format=zip .
    WORKING_DIRECTORY ${in_dir})

The newer interface below is not yet usable because there’s no control over nuisance computer-specific relative directories added. The Kitware contributor intends to fix this by adding a WORKING_DIRECTORY–check to see if this has been done yet.

set(archive "my.zst")
set(in_dir "data/")

file(ARCHIVE_CREATE
  OUTPUT ${archive}
  PATHS ${in_dir}
  COMPRESSION Zstd
  COMPRESSION_LEVEL 3)
# level is arbitrary, bigger is more compressed

NOTE: The “FORMAT” option is not used for Zstd.