Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

maint: Enable -Werror compiler flag for GCC, Clang and AppleClang #3611

Merged
merged 19 commits into from
Nov 21, 2024

Conversation

mathbunnyru
Copy link
Contributor

@mathbunnyru mathbunnyru commented Nov 18, 2024

I think we can now enable -Werror flag relatively easily when building with GCC, Clang and AppleClang.

I tested locally on my M2 Mac with the following compilers:

  • Apple clang version 16.0.0 (clang-1600.0.26.4)
  • clang version 17.0.6
  • gcc-14 (Homebrew GCC 14.2.0_1) 14.2.0

In some places I turned errors back to warnings, in some - disabled, I will explain choices using commenting.

Fix: #3542

# This file uses capturing structured bindings, which was fixed in C++20
set_source_files_properties(
${LIBMAMBA_SOURCE_DIR}/download/mirror_impl.cpp
PROPERTIES COMPILE_FLAGS -Wno-c++20-extensions
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is nothing wrong with our code and nothing to change there. So we can safely ignore the warning we're using C++20 feature (capturing structured binding).
This can't hide any real problems, so here this warning/error will disappear.

Copy link
Member

@JohanMabille JohanMabille Nov 21, 2024

Choose a reason for hiding this comment

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

Technically, this construction is C++20, not C++17. It works with gcc and msvc though, and the warning is still here with Clang, even if we pass the C++20 option (that's a known bug), so I agree that we can ignore it.

# GCC reports dangling reference when using std::invoke with data members
set_source_files_properties(
${LIBMAMBA_SOURCE_DIR}/solver/problems_graph.cpp
PROPERTIES COMPILE_FLAGS -Wno-error=dangling-reference
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This might be useful warning/error, so I'm turning it back to being warning for this file.

Copy link
Member

Choose a reason for hiding this comment

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

Is there any way to make that change as part of the source code instead of here (like disabling warnings)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, fortunately it happens in functions located near each other, so we can only write pragmas once.
Thanks!

using int_t = Int;
return static_cast<int_t>(condition) * true_val
+ (int_t(1) - static_cast<int_t>(condition)) * false_val;
return condition ? true_val : false_val;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this is significantly easier to read and doesn't deal with arithmetics at all (which was giving warnings in some cases).

// At the same time `sigset_t` might be `unsigned int`
// This causes compiler warning
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added comment explaining why this happens above
This doesn't happen in our code, but in the system code
But because sigaddset might be implemented as a macro, it's treated as our code.

Because, there is no bug, I completely disable this warning/error.
But I didn't want to disable it for the whole file, so I created a small function, where I applied this behaviour.

@@ -193,7 +193,7 @@ namespace mamba::solver::libsolv
return { std::cref(it->second) };
}

return specs::Channel::resolve(std::move(uc), channel_params())
return specs::Channel::resolve(uc, channel_params())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's const specs::UnresolvedChannel& uc, so no point in std::move(uc)

# This file uses capturing structured bindings, which was fixed in C++20
set_source_files_properties(
src/libmambapy/bindings/legacy.cpp
PROPERTIES COMPILE_FLAGS -Wno-error=deprecated-declarations
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I turned this back to warning. Sometimes it's useful to see when we're using deprecated functions, so let's have it as a warning.

Copy link
Member

Choose a reason for hiding this comment

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

It makes sense for that code or even the whole library. 👍🏽

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For now, let's keep it for this file. If needed later, we can add this flag to the whole repo/lib I suppose.
It's better not to use deprecated functions, when possible.

// This function is only used in `assert()` expressions
// That's why it might get reported as unused in Release builds
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I explained above why this happens.
It's completely ok to have a function which is only used in assert statements.
So, I silence this warning/error

Comment on lines 1 to 7
if(APPLE)
# It seems that there are some flags added to CXXFLAGS during `conda-build`. It might be a bug
# in https://github.com/conda-forge/clang-compiler-activation-feedstock. One way to fix this is
# to turn these errors to warnings or silence them (using `-Wno-unused-command-line-argument`
# and `-Wno-ignored-optimization-argument`)

# But we will go another way: we will remove wrong options from CMAKE_CXX_FLAGS
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the most tricky part.

I think we have some flags, which are not part of this project and we inherit it during conda-build.

This is how current main looks like: https://github.com/mamba-org/mamba/actions/runs/11896974442/job/33150163684#step:7:1203

x86_64-apple-darwin13.4: warning: -framework CoreFoundation: 'linker' input unused [-Wunused-command-line-argument]
x86_64-apple-darwin13.4: warning: -framework CoreServices: 'linker' input unused [-Wunused-command-line-argument]
x86_64-apple-darwin13.4: warning: -framework Security: 'linker' input unused [-Wunused-command-line-argument]
x86_64-apple-darwin13.4: warning: -framework Kerberos: 'linker' input unused [-Wunused-command-line-argument]
x86_64-apple-darwin13.4: warning: optimization flag '-fno-merge-constants' is not supported [-Wignored-optimization-argument]

I think it comes from here INFO: activate_clangxx_osx-64.sh made the following environmental changes::
https://github.com/mamba-org/mamba/actions/runs/11896974442/job/33150163684#step:7:1123

I'm not 100% sure about this and might be completely wrong.
If someone more experienced could take a look at this, it would be great.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Trying another approach right now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It worked; this one didn't due to some strange linking errors.

@mathbunnyru mathbunnyru force-pushed the gcc_clang_werror branch 2 times, most recently from c24700c to 5e77197 Compare November 18, 2024 21:35
@jjerphan jjerphan added the release::bug_fixes For PRs fixing bugs label Nov 20, 2024
Copy link
Member

@jjerphan jjerphan left a comment

Choose a reason for hiding this comment

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

LGTM, modulo a few comments.

Comment on lines 112 to 114
# It seems that there are some wrong flags added to CXXFLAGS during `conda-build`. And then
# these flags are used to compile our source code. It might be a bug in
# https://github.com/conda-forge/clang-compiler-activation-feedstock.
Copy link
Member

Choose a reason for hiding this comment

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

Can you give more details, context or evidence about this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I rewrote the comment and added more details.

Note, this is not caused by my PR, it already happens in main
But because we use warnings, not errors, no one noticed:
https://github.com/mamba-org/mamba/actions/runs/11935345040/job/33266465568#step:7:1202

Unfortunately, I couldn't understand exactly why this happens.
But I noticed INFO: activate_clangxx_osx-64.sh made the following environmental changes: a few lines above.
And then we have lines showing erroneous changes in CXXFLAGS.
That's why I think it happens in the mentioned lib.

If someone wants to dig deeper, that would be great.

if(MSVC)
option(MAMBA_WARNING_AS_ERROR "Treat compiler warnings as errors" OFF)
else()
option(MAMBA_WARNING_AS_ERROR "Treat compiler warnings as errors" ON)
Copy link
Member

Choose a reason for hiding this comment

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

I think this is a good idea even if this will probably will make some untested toolchain fail.

@@ -93,10 +93,21 @@ namespace solv

namespace
{
// This function is only used in `assert()` expressions
// That's why it might get reported as unused in Release builds
#if defined(__GNUC__) && !defined(__clang__)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added these #if-like statements everywhere - warnings only need to be ignored if they actually happen.
And it also prevents MSVC from giving a warning that unknown pragma is used.

@mathbunnyru mathbunnyru reopened this Nov 20, 2024
@mathbunnyru
Copy link
Contributor Author

@jjerphan @Klaim I think I fixed all the comments (and a bit more), so please, take a look.

Copy link
Member

@Klaim Klaim left a comment

Choose a reason for hiding this comment

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

LGTM 👍🏽

Copy link
Member

@jjerphan jjerphan left a comment

Choose a reason for hiding this comment

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

LGTM.

Comment on lines +112 to +113
# It seems that these flags are leaked to CXXFLAGS: `-framework CoreFoundation` `-framework
# CoreServices` `-framework Security` `-framework Kerberos` `-fno-merge-constants`
Copy link
Member

@jjerphan jjerphan Nov 21, 2024

Choose a reason for hiding this comment

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

How is this comment related to the elements bellow? Is it when you build locally on macOS? Can you precise where those are leaked in #3624 (comment)?

See also those lines on the feedstock which make use of them.

The use and presence of those flags must be clarified at a point, but this is orthogonal to the goal of this PR to me.

@jjerphan jjerphan merged commit 0a8128d into mamba-org:main Nov 21, 2024
63 of 64 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release::bug_fixes For PRs fixing bugs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Address warnings while building mamba/micromamba from source
4 participants