Skip to content

Fix CXXFLAGS when coverage enabled.#1902

Merged
Dead2 merged 1 commit intozlib-ng:developfrom
mtl1979:fix-flags
Apr 14, 2025
Merged

Fix CXXFLAGS when coverage enabled.#1902
Dead2 merged 1 commit intozlib-ng:developfrom
mtl1979:fix-flags

Conversation

@mtl1979
Copy link
Copy Markdown
Collaborator

@mtl1979 mtl1979 commented Apr 11, 2025

This allows CI to add flags for C++ compiler even when coverage is enabled. According to CMake documentation CXXFLAGS is only used if CMAKE_CXX_FLAGS is unset. Enable C++ support early enough so CMAKE_CXX_FLAGS is correctly populated.

Summary by CodeRabbit

  • Chores
    • Enhanced the build configuration to include new options for enabling C++ support, improving flexibility in the build process. Options added include support for Google Test, fuzz tests, and benchmarks.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 11, 2025

Walkthrough

The changes introduce new options in the CMake configuration for enabling C++ support based on specific conditions. Three options are added: WITH_GTEST, WITH_FUZZERS, and WITH_BENCHMARKS, with default values of ON, OFF, and OFF, respectively. A conditional block checks if any of these options are enabled, executing the enable_language(CXX) command if at least one is true. The previous declarations of these options are removed, ensuring they are defined only once in the new section.

Changes

File Change Summary
CMakeLists.txt Added options WITH_GTEST, WITH_FUZZERS, and WITH_BENCHMARKS. Removed previous declarations of these options. Added a conditional block to check if any of these options are true and calls enable_language(CXX) when the condition is satisfied.

Sequence Diagram(s)

sequenceDiagram
    participant CMake as CMakeLists.txt
    participant Condition as IF Statement
    participant CXX as C++ Language Enabler

    CMake->>Condition: Check WITH_GTEST, WITH_FUZZERS, or WITH_BENCHMARKS flags
    alt One or more flags are true
        Condition->>CXX: Call enable_language(CXX)
    else No flags are true
        Condition->>CMake: Proceed without enabling C++
    end
Loading

Tip

⚡💬 Agentic Chat (Pro Plan, General Availability)
  • We're introducing multi-step agentic chat in review comments and issue comments, within and outside of PR's. This feature enhances review and issue discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments and add commits to existing pull requests.

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c7bce0b and 7bc161c.

📒 Files selected for processing (1)
  • CMakeLists.txt (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • CMakeLists.txt

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@phprus
Copy link
Copy Markdown
Contributor

phprus commented Apr 11, 2025

According to CMake documentation CFLAGS is only used if CMAKE_C_FLAGS is unset and CXXFLAGS is only used if CMAKE_CXX_FLAGS is unset.

https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_FLAGS.html:

CMAKE_C_FLAGS: Initialized by the CFLAGS environment variable.
CMAKE_CXX_FLAGS: Initialized by the CXXFLAGS environment variable.

As I see, CMAKE_*_FLAGS are overridden only in:

$ export CMAKE_ARGS="-DCMAKE_C_FLAGS=-m32" CFLAGS=-m32 LDFLAGS=-m32

cmake-args: -DCMAKE_C_FLAGS=-m32 -DCMAKE_CXX_FLAGS=-m32

cmake-args: -DCMAKE_C_FLAGS=-m32 -DCMAKE_CXX_FLAGS=-m32 -DCMAKE_SHARED_LINKER_FLAGS=-m32 -DCMAKE_EXE_LINKER_FLAGS=-m32

Maybe it makes sense to fix these workflows instead of duplicate C*FLAGS value in CMAKE_*_FLAGS?

@mtl1979
Copy link
Copy Markdown
Collaborator Author

mtl1979 commented Apr 11, 2025

Maybe it makes sense to fix these workflows instead of duplicate CFLAGS value in CMAKE__FLAGS?

pkgcheck.sh use both configure and cmake, so it is special case.

The issue is with cmake populating CMAKE_C_FLAGS and CMAKE_CXX_FLAGS too late. So only way to set the variables early enough is to use CFLAGS and CXXFLAGS when coverity scan is enabled. Without the workaround both variables are always set to -O0 despite what was passed to cmake.

When coverity scan is not enabled, there is no issue and the workaround is skipped.

@phprus
Copy link
Copy Markdown
Contributor

phprus commented Apr 11, 2025

The issue is with cmake populating CMAKE_C_FLAGS and CMAKE_CXX_FLAGS too late.

What do you mean by too late?
I thought that it was initialized during the project(LANGUAGE) call. Or was I wrong?

@mtl1979
Copy link
Copy Markdown
Collaborator Author

mtl1979 commented Apr 11, 2025

The issue is with cmake populating CMAKE_C_FLAGS and CMAKE_CXX_FLAGS too late.

What do you mean by too late? I thought that it was initialized during the project(LANGUAGE) call. Or was I wrong?

Clearly when I and dead2 tested, those variables were empty when cmake hit the function in the PR.

See for example https://github.com/zlib-ng/zlib-ng/actions/runs/14375470240/job/40306746506?pr=1892

@nmoinvaz
Copy link
Copy Markdown
Member

This may be why I had to turn off gtest for PPC64LE ci run.

@mtl1979
Copy link
Copy Markdown
Collaborator Author

mtl1979 commented Apr 11, 2025

This may be why I had to turn off gtest for PPC64LE ci run.

You can verify that locally by re-enabling gtest for PPC64LE after checking out branch from this PR.

@nmoinvaz
Copy link
Copy Markdown
Member

You can verify that locally by re-enabling gtest for PPC64LE after checking out branch from this PR.

Nope this PR doesn't fix that.

@mtl1979
Copy link
Copy Markdown
Collaborator Author

mtl1979 commented Apr 11, 2025

Nope this PR doesn't fix that.

At least this workaround allows enabling benchmarks on 32-bit ARM with hard-float on CI.

Copy link
Copy Markdown
Member

@Dead2 Dead2 left a comment

Choose a reason for hiding this comment

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

LGTM

@phprus
Copy link
Copy Markdown
Contributor

phprus commented Apr 12, 2025

See for example https://github.com/zlib-ng/zlib-ng/actions/runs/14375470240/job/40306746506?pr=1892

I have one idea. I'll check it now and write the result...

@phprus
Copy link
Copy Markdown
Contributor

phprus commented Apr 12, 2025

The issue is with cmake populating CMAKE_C_FLAGS and CMAKE_CXX_FLAGS too late.

What do you mean by too late? I thought that it was initialized during the project(LANGUAGE) call. Or was I wrong?

Clearly when I and dead2 tested, those variables were empty when cmake hit the function in the PR.

See for example https://github.com/zlib-ng/zlib-ng/actions/runs/14375470240/job/40306746506?pr=1892

@mtl1979 , @Dead2 , this is incorrect PR!

The C++ language is enabled too late. After the CMAKE_CXX_* variables are used.

We need to enable C++ directly in project().
Or conditionally, but BEFORE the line:

include(cmake/detect-arch.cmake)

See fix commit phprus@0667808
and CI: https://github.com/phprus/zlib-ng/actions/runs/14418138299/job/40437352696

@mtl1979
Copy link
Copy Markdown
Collaborator Author

mtl1979 commented Apr 12, 2025

The C++ language is enabled too late. After the CMAKE_CXX_* variables are used.

We need to enable C++ directly in project(). Or conditionally, but BEFORE the line:

include(cmake/detect-arch.cmake)

See fix commit phprus@0667808 and CI: https://github.com/phprus/zlib-ng/actions/runs/14418138299/job/40437352696

A lot of operating systems don't come with C++ compiler installed by default. I'd rather enable C++ only when it's actually needed...

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.38%. Comparing base (e4a31e0) to head (7bc161c).
Report is 1 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1902      +/-   ##
===========================================
- Coverage    83.42%   83.38%   -0.04%     
===========================================
  Files          144      144              
  Lines        12948    12948              
  Branches      2857     2857              
===========================================
- Hits         10802    10797       -5     
+ Misses        1202     1201       -1     
- Partials       944      950       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
CMakeLists.txt (1)

50-54: Enabling C++ Early for Proper Flag Initialization
The newly added block correctly calls enable_language(CXX) when either WITH_GTEST or WITH_BENCHMARKS is enabled. This is an important step to ensure that the C++ compiler flags (CXXFLAGS) are properly initialized—especially critical when code coverage is enabled and the default behavior (i.e. using only CFLAGS and not propagating CXXFLAGS) might otherwise lead to errors in test configurations.

It would be helpful to add or update an inline comment to clarify that this conditional is chosen because test targets (typically using gtest or benchmarks) require a C++ compiler to be configured early, which in turn forces CMake to initialize CXXFLAGS from the CXXFLAGS environment variable. Additionally, consider whether other cases (for example, when code coverage is enabled in a context that uses C++ features) might also require C++ support—if so, extending the condition (e.g. to include a check for WITH_CODE_COVERAGE) might be warranted.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d1c2360 and 4807153.

📒 Files selected for processing (3)
  • CMakeLists.txt (1 hunks)
  • test/CMakeLists.txt (0 hunks)
  • test/benchmarks/CMakeLists.txt (0 hunks)
💤 Files with no reviewable changes (2)
  • test/benchmarks/CMakeLists.txt
  • test/CMakeLists.txt
🧰 Additional context used
🪛 GitHub Actions: Package Check
CMakeLists.txt

[error] 1-1: CMake Error in test/CMakeLists.txt: No known features for CXX compiler version.

🪛 GitHub Actions: Static Analysis
CMakeLists.txt

[error] 1-1: CMake Error in test/CMakeLists.txt: No known features for CXX compiler version.


[warning] 1509-1509: CMake Warning: Building to source folder is not recommended. Cpack will be unable to generate source package.

⏰ Context from checks skipped due to timeout of 90000ms (48)
  • GitHub Check: macOS Clang Native Instructions (ARM64)
  • GitHub Check: Ubuntu GCC AARCH64 Compat No Opt UBSAN
  • GitHub Check: Ubuntu GCC AARCH64 No NEON UBSAN
  • GitHub Check: Ubuntu GCC -O3 OSB
  • GitHub Check: Ubuntu GCC Symbol Prefix
  • GitHub Check: macOS Clang Native Instructions (ARM64)
  • GitHub Check: Ubuntu GCC AARCH64 Compat No Opt UBSAN
  • GitHub Check: Ubuntu GCC AARCH64 No NEON UBSAN
  • GitHub Check: Ubuntu GCC -O3 OSB
  • GitHub Check: Ubuntu GCC Symbol Prefix
  • GitHub Check: macOS Clang Native Instructions (ARM64)
  • GitHub Check: Ubuntu GCC AARCH64 Compat No Opt UBSAN
  • GitHub Check: Ubuntu GCC AARCH64 No NEON UBSAN
  • GitHub Check: Ubuntu GCC -O3 OSB
  • GitHub Check: Ubuntu GCC Symbol Prefix
  • GitHub Check: macOS Clang Native Instructions (ARM64)
  • GitHub Check: Ubuntu GCC AARCH64 Compat No Opt UBSAN
  • GitHub Check: Ubuntu GCC AARCH64 No NEON UBSAN
  • GitHub Check: Ubuntu GCC -O3 OSB
  • GitHub Check: Ubuntu GCC Symbol Prefix
  • GitHub Check: macOS Clang Native Instructions (ARM64)
  • GitHub Check: Ubuntu GCC AARCH64 Compat No Opt UBSAN
  • GitHub Check: Ubuntu GCC AARCH64 No NEON UBSAN
  • GitHub Check: Ubuntu GCC -O3 OSB
  • GitHub Check: Ubuntu GCC Symbol Prefix
  • GitHub Check: macOS Clang Native Instructions (ARM64)
  • GitHub Check: Ubuntu GCC AARCH64 Compat No Opt UBSAN
  • GitHub Check: Ubuntu GCC AARCH64 No NEON UBSAN
  • GitHub Check: Ubuntu GCC -O3 OSB
  • GitHub Check: Ubuntu GCC Symbol Prefix
  • GitHub Check: macOS Clang Native Instructions (ARM64)
  • GitHub Check: Ubuntu GCC AARCH64 Compat No Opt UBSAN
  • GitHub Check: Ubuntu GCC AARCH64 No NEON UBSAN
  • GitHub Check: Ubuntu GCC -O3 OSB
  • GitHub Check: Ubuntu GCC Symbol Prefix
  • GitHub Check: macOS Clang Native Instructions (ARM64)
  • GitHub Check: Ubuntu GCC AARCH64 Compat No Opt UBSAN
  • GitHub Check: Ubuntu GCC AARCH64 No NEON UBSAN
  • GitHub Check: Ubuntu GCC -O3 OSB
  • GitHub Check: Ubuntu GCC Symbol Prefix
  • GitHub Check: macOS Clang Native Instructions (ARM64)
  • GitHub Check: Ubuntu GCC AARCH64 Compat No Opt UBSAN
  • GitHub Check: Ubuntu GCC AARCH64 No NEON UBSAN
  • GitHub Check: Ubuntu GCC Symbol Prefix
  • GitHub Check: macOS Clang Native Instructions (ARM64)
  • GitHub Check: Ubuntu GCC AARCH64 Compat No Opt UBSAN
  • GitHub Check: Ubuntu GCC AARCH64 No NEON UBSAN
  • GitHub Check: Ubuntu GCC Symbol Prefix

@phprus
Copy link
Copy Markdown
Contributor

phprus commented Apr 12, 2025

A lot of operating systems don't come with C++ compiler installed by default. I'd rather enable C++ only when it's actually needed...

Yes, you are right.
I suggest enabling C++ earlier only if needed: phprus@b0832f2

The previous commit was a proof of concept only.

@mtl1979 mtl1979 force-pushed the fix-flags branch 2 times, most recently from 034321d to c7bce0b Compare April 12, 2025 18:42
@mtl1979 mtl1979 changed the title Fix CFLAGS and CXXFLAGS when coverage enabled. Fix CXXFLAGS when coverage enabled. Apr 12, 2025
@mtl1979 mtl1979 requested a review from Dead2 April 12, 2025 18:49
Copy link
Copy Markdown
Member

@Dead2 Dead2 left a comment

Choose a reason for hiding this comment

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

Yeah, this looks like a better solution 👍

@mtl1979
Copy link
Copy Markdown
Collaborator Author

mtl1979 commented Apr 12, 2025

Yeah, this looks like a better solution 👍

Once this PR is merged, #1892 needs to be rebased for CI to pass on all except EL9...

@Dead2 Dead2 merged commit eb76eca into zlib-ng:develop Apr 14, 2025
147 of 150 checks passed
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.

4 participants