Skip to content

[cmake] Simplify C++ standard enforcement using target_compile_features#8915

Merged
sbc100 merged 1 commit into
mainfrom
cmake-cxx-standard
Jul 17, 2026
Merged

[cmake] Simplify C++ standard enforcement using target_compile_features#8915
sbc100 merged 1 commit into
mainfrom
cmake-cxx-standard

Conversation

@sbc100

@sbc100 sbc100 commented Jul 17, 2026

Copy link
Copy Markdown
Member

Require C++20 directly on the binaryen target by adding
cxx_std_20 to target_compile_features. This enables CMake to
automatically enforce that the compiler supports C++20 and propagates
the requirement to all consumers linking against binaryen.

Remove the manual elseif(CMAKE_CXX_STANDARD LESS ...) check and error
message from CMakeLists.txt since CMake now natively handles standard
validation and error reporting.

Also remove the if(NOT CMAKE_CXX_STANDARD) condition around
set(CMAKE_CXX_STANDARD 20) and its explanatory comment. The old
comment suggested the conditional allowed embedding in parent projects
with a higher default standard. However, variable assignments like
set(CMAKE_CXX_STANDARD 20) in a subdirectory (via add_subdirectory)
are strictly directory-scoped and do not leak into the parent project.
Furthermore, with target_compile_features enforcing cxx_std_20 on
binaryen, any parent target linking against binaryen at a higher C++
standard (e.g., C++23) cleanly satisfies the feature requirement.
Therefore, unconditionally setting CMAKE_CXX_STANDARD 20 for internal
Binaryen targets is completely safe and avoids inheriting incompatible
standards (< 20) from parent projects.

@sbc100
sbc100 requested a review from a team as a code owner July 17, 2026 18:32
@sbc100
sbc100 requested review from stevenfontanella and removed request for a team July 17, 2026 18:32
Require C++20 directly on the `binaryen` target by adding
`cxx_std_20` to `target_compile_features`. This enables CMake to
automatically enforce that the compiler supports C++20 and propagates
the requirement to all consumers linking against `binaryen`.

Remove the manual `elseif(CMAKE_CXX_STANDARD LESS ...)` check and error
message from `CMakeLists.txt` since CMake now natively handles standard
validation and error reporting.

Also remove the `if(NOT CMAKE_CXX_STANDARD)` condition around
`set(CMAKE_CXX_STANDARD 20)` and its explanatory comment. The old
comment suggested the conditional allowed embedding in parent projects
with a higher default standard. However, variable assignments like
`set(CMAKE_CXX_STANDARD 20)` in a subdirectory (via `add_subdirectory`)
are strictly directory-scoped and do not leak into the parent project.
Furthermore, with `target_compile_features` enforcing `cxx_std_20` on
`binaryen`, any parent target linking against `binaryen` at a higher C++
standard (e.g., C++23) cleanly satisfies the feature requirement.
Therefore, unconditionally setting `CMAKE_CXX_STANDARD 20` for internal
Binaryen targets is completely safe and avoids inheriting incompatible
standards (< 20) from parent projects.
@sbc100
sbc100 force-pushed the cmake-cxx-standard branch from 1e91c41 to 2daf1b8 Compare July 17, 2026 18:40
@sbc100
sbc100 requested a review from tlively July 17, 2026 18:41

@tlively tlively left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice simplification! I'll just have to take the commit message's word that this is all correct.

@sbc100
sbc100 merged commit ebd7454 into main Jul 17, 2026
16 checks passed
@sbc100
sbc100 deleted the cmake-cxx-standard branch July 17, 2026 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants