Skip to content

cmake: honor options set by a parent project - #5077

Merged
Cyan4973 merged 1 commit into
devfrom
cmake-policy-scope
Dec 1, 2026
Merged

Cyan4973 merged 1 commit into
devfrom
cmake-policy-scope

Conversation

@Cyan4973

@Cyan4973 Cyan4973 commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

This affects projects that build zstd with CMake as a subproject, with add_subdirectory(zstd/build/cmake).

Problem

Since v1.6.0, the options such a project sets before add_subdirectory(), like set(ZSTD_BUILD_PROGRAMS OFF) or set(ZSTD_LEGACY_SUPPORT ON), are ignored, with a CMP0077 warning.
Options passed with -D on the command line still work.

cmake_policy(VERSION) moved into ZstdVersion.cmake (49fe2ec), and include() gives it its own policy scope.
So CMP0077 is unset for the rest of build/cmake, and option() overwrites the parent's value.

Fix

include(ZstdVersion NO_POLICY_SCOPE): the policies apply to the whole project again, as in v1.5.7.

Credit to @alexhadd, who found the cause and proposed this fix in #5074.

Test

With cmake 3.31.8 and a parent project that sets ZSTD_BUILD_PROGRAMS OFF and ZSTD_LEGACY_SUPPORT ON:

  • dev: 2 CMP0077 warnings, the programs are built, and the library gets -DZSTD_LEGACY_SUPPORT=0.
  • this PR, and v1.5.7: no warning, no programs, -DZSTD_LEGACY_SUPPORT=5.

The standalone build of build/cmake still configures without warning.
A CI check will follow in #5048, whose parent project passes options with -D, and so doesn't catch this.

Fixes #5074

Since 49fe2ec (v1.6.0), cmake_policy(VERSION) runs inside
include(ZstdVersion), which has its own policy scope. CMP0077 was then
unset for the rest of build/cmake, so option() discarded the values a
parent project set before add_subdirectory(), such as
set(ZSTD_BUILD_PROGRAMS OFF).
NO_POLICY_SCOPE applies the policies to the whole project again, as in
v1.5.7.

Fix suggested by alexhadd in #5074.
@meta-cla meta-cla Bot added the CLA Signed label Nov 30, 2026
@Cyan4973 Cyan4973 self-assigned this Nov 30, 2026
@Cyan4973 Cyan4973 added the build label Nov 30, 2026
@Cyan4973
Cyan4973 merged commit 268cc81 into dev Dec 1, 2026
118 checks passed
@Cyan4973
Cyan4973 deleted the cmake-policy-scope branch December 1, 2026 00:21
Cyan4973 added a commit that referenced this pull request Dec 4, 2026
The parent project of the CMake subdirectory consumer now sets
ZSTD_BUILD_PROGRAMS OFF and ZSTD_LEGACY_SUPPORT ON with set(), before
add_subdirectory(), instead of passing them with -D.
Configuration fails if the zstd program target exists, and the consumer
fails if v0.7 frames are not recognized.

This covers #5077: before it, both options were ignored.
Cyan4973 added a commit that referenced this pull request Dec 4, 2026
The parent project of the CMake subdirectory consumer now sets
ZSTD_BUILD_PROGRAMS OFF and ZSTD_LEGACY_SUPPORT ON with set(), before
add_subdirectory(), instead of passing them with -D.
Configuration fails if the zstd program target exists, and the consumer
fails if v0.7 frames are not recognized.

This covers #5077: before it, both options were ignored.
Cyan4973 added a commit that referenced this pull request Dec 4, 2026
The parent project of the CMake subdirectory consumer now sets
ZSTD_BUILD_PROGRAMS OFF and ZSTD_LEGACY_SUPPORT ON with set(), before
add_subdirectory(), instead of passing them with -D.
Configuration fails if the zstd program target exists, and the consumer
fails if v0.7 frames are not recognized.

This covers #5077: before it, both options were ignored.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CMake: options set by a parent project are ignored since v1.6.0

2 participants