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

cmake: Adjust diagnostic flags for clang-cl #1647

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

hebasto
Copy link
Member

@hebasto hebasto commented Dec 13, 2024

When building with clang-cl on Windows, the output is cluttered with warning messages because compiler diagnostic flags are not applied correctly:

> cmake -B build -G Ninja -DCMAKE_C_COMPILER="C:\Users\hebasto\Downloads\clang+llvm-18.1.8-x86_64-pc-windows-msvc\bin\clang-cl.exe"
> cmake --build build
[1/16] Building C object src\CMakeFiles\bench.dir\bench.c.obj
In file included from C:\Users\hebasto\secp256k1\src\bench.c:11:
C:\Users\hebasto\secp256k1\src\util.h(34,13): warning: unused function 'print_buf_plain' [-Wunused-function]
   34 | static void print_buf_plain(const unsigned char *buf, size_t len) {
      |             ^~~~~~~~~~~~~~~
1 warning generated.
[2/16] Building C object src\CMakeFiles\secp256k1_precomputed.dir\precomputed_ecmult_gen.c.obj
In file included from C:\Users\hebasto\secp256k1\src\precomputed_ecmult_gen.c:3:
In file included from C:\Users\hebasto\secp256k1\src\group.h:10:
In file included from C:\Users\hebasto\secp256k1\src\field.h:10:
C:\Users\hebasto\secp256k1\src\util.h(34,13): warning: unused function 'print_buf_plain' [-Wunused-function]
   34 | static void print_buf_plain(const unsigned char *buf, size_t len) {
      |             ^~~~~~~~~~~~~~~

This PR resolves this issue.


Additional note for reviewers: The VS builtin clang can also be used assuming that the following VS components are installed:

photo_2024-12-12_12-38-17

The user can generate a build system on Windows as follows:

  • Using the default "Visual Studio" generator:
cmake -B build -T ClangCL
  • Using the "Ninja" generator:
cmake -B build -G Ninja -DCMAKE_C_COMPILER=clang-cl

Required for downstream projects which aim to build with clang-cl (see bitcoin/bitcoin#31456).

@real-or-random
Copy link
Contributor

I guess the patch is trivial enough not to bother, but I'm not entirely sure if I understand the purpose of this. Couldn't you build using clang on Windows directly? Or is the purpose of this PR to help users who want to use Visual Studio but with clang instead of MSVC? My thinking is that if you use Visual Studio for an application that depends on libsecp256k1, you don't bother how libsecp256k1 itself is built.

@hebasto hebasto changed the title cmake: Enable compiling with clang-cl cmake: Adjust diagnostic flags for clang-cl Dec 13, 2024
@hebasto
Copy link
Member Author

hebasto commented Dec 13, 2024

I guess the patch is trivial enough not to bother, but I'm not entirely sure if I understand the purpose of this. Couldn't you build using clang on Windows directly? Or is the purpose of this PR to help users who want to use Visual Studio but with clang instead of MSVC? My thinking is that if you use Visual Studio for an application that depends on libsecp256k1, you don't bother how libsecp256k1 itself is built.

My apologies for any confusion. The PR description has been reworked.

Copy link
Contributor

@real-or-random real-or-random left a comment

Choose a reason for hiding this comment

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

utACK 4c50d73

Copy link
Contributor

@theuni theuni left a comment

Choose a reason for hiding this comment

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

utACK other than the nit.

try_append_c_flags(-Wall) # GCC >= 2.95 and probably many other compilers.
endif()
if(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
# Keep the following commands ordered lexicographically.
Copy link
Contributor

@theuni theuni Dec 16, 2024

Choose a reason for hiding this comment

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

I'd prefer to set /W3 here and -Wall with the other try_append_c_flags for consistency, as I don't see why it needs to be special-cased.

Edit: Retracted, see below.

Copy link
Member Author

Choose a reason for hiding this comment

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

According to the docs, the /Wall option in clang-cl, which is equivalent to -Wall, enables Clang's core -Weverything option.

We use /W3, which enables Clang's core -Wall option.

Copy link
Contributor

Choose a reason for hiding this comment

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

Wow. Confirmed: https://godbolt.org/z/WEr3qGeox

That's... hugely unexpected. It really doesn't give me much faith in the proper forwarding of other options :(

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.

4 participants