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

Add the warning flag to the C/C++ compiler invocation #11535

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

MisterDA
Copy link
Contributor

See Sets of warnings for foreign stubs #8597.

There are some preliminary commits around naming in the C/C++ compiler handling code, to align the terminology with the upstream OCaml compiler, then a few fixes, then the gist of it.

I add -Wall to the C/C++ compiler invocation if GCC/clang are detected, and -W2 if MSVC or clang-cl are detected. This should help C/C++ OCaml FFI writers to set warnings on by default, which they tend to forget, and that's a huge mistake. It is hard to add the right set of warnings in Dune files, see below. If they simply add -Wall, they forget about MSVC and clang-cl. Users usually don't know or don't care about these toolchains. Bugs, typos, anything can creep up. Some packages use a discover script with dune-configurator. This is another possible way of forgetting warnings. I believe we should help users catching problems early on by turning on the C/C++ warnings for them.

Note that as long as we don't set the warning as error flag (-Werror on cc or -WX for msvc), compilation can't fail. No workflow should be broken, the user will just see new warnings reported by the compiler. Users should still be able to disable these flags with :standard \ -Wall.

(rule
 (with-stdout-to c_flags.cc (echo "(-Wall)")))

(rule
 (with-stdout-to c_flags.msvc (echo "(/W2)")))

(library
 (name mylib)
 (foreign_stubs
  (language c)
  (flags :standard (:include c_flags.%{ocaml-config:ccomp_type}))
  (names mystubs)))

This PR is best reviewed commit by commit.

cc @voodoos @rgrinberg @nojb

ccomp_type is given by the OCaml compiler. It's either 'cc' or 'msvc',
but could be something else in future versions. The C compiler vendor
is another specialization. This aligns with the OCaml compiler
terminology.

Signed-off-by: Antonin Décimo <[email protected]>
They describe how to call the C/C++ compiler and are not directly tied
to C++ anymore.

Signed-off-by: Antonin Décimo <[email protected]>
clang-cl is a drop-in replacement for MSVC, and exposes the same set
of flags as MSVC. It defines both `_MSC_VER` and `__clang__`, but we
want it do have precedence over clang, just as clang (on Unix) defines
both `__GNUC__` and `__clang__`, which is why we want it to have
precedence over GCC.

Signed-off-by: Antonin Décimo <[email protected]>
@MisterDA MisterDA force-pushed the foreign_stubs-c-cxx-warnings branch from 2bf4465 to d9a876a Compare March 14, 2025 17:11
MSVC has an annoying ‘feature’: it prints a copyright statement
everytime it's called. Fortunately, it can be silenced by passing the
`/nologo` argument. MSVC always prints the name of the file it's
compiling.

Signed-off-by: Antonin Décimo <[email protected]>
Note that as long as we don't set the warning as error flag (-Werror
on cc or -WX for msvc), compilation can't fail. No workflow will be
broken, the user will just see new warnings reported by the compiler.

Signed-off-by: Antonin Décimo <[email protected]>
@MisterDA MisterDA force-pushed the foreign_stubs-c-cxx-warnings branch from d9a876a to 9e082db Compare March 14, 2025 20:39
@nojb
Copy link
Collaborator

nojb commented Mar 15, 2025

I am roughly in favour and I agree that more warnings is better than less warnings. However, certain users will probably be annoyed by the change (I am thinking of certain warnings that are a bit complicated to "fix", eg alloc-size-larger-than; see eg https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85783#c3).

I will try to take a look at the patch later in the week.

@maiste maiste added the c-bindings When dune is trying to interop with C label Mar 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c-bindings When dune is trying to interop with C
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants