Skip to content

Commit

Permalink
Always pass -fdiagnostics-color=always to cc-style compilers to a…
Browse files Browse the repository at this point in the history
…void useless recompilations when Dune's stderr is redirected (ocaml#10883)

Signed-off-by: Nicolás Ojeda Bär <n.oje.bar@gmail.com>
  • Loading branch information
nojb authored and anmonteiro committed Nov 17, 2024
1 parent 1ea5550 commit a1dd271
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
2 changes: 2 additions & 0 deletions doc/changes/10883.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Fix an issue where C stubs would be rebuilt whenever the stderr of Dune was
redirected. (#10883, @nojb)
3 changes: 1 addition & 2 deletions src/dune_rules/cxx_flags.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ let base_cxx_flags ~for_ cc =
;;

let fdiagnostics_color = function
| (Gcc | Clang) when Lazy.force Ansi_color.stderr_supports_color ->
[ "-fdiagnostics-color=always" ]
| Gcc | Clang -> [ "-fdiagnostics-color=always" ]
| _ -> []
;;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
This test won't work with MSVC as it's using GCC and clang CLI
parameters.

We have to force Dune outputting colors, for that we use
`CLICOLOR_FORCE=1`.

The flag should be present in the command, as we want it in the
:standard set.

We don't actually test that the compiler outputs colors, just that
Dune correctly passes the flag when required, and doesn't when it's
not.

test, color enabled, color flag default (enabled)
=================================================
test that we pass the flag
==========================

$ cat >dune <<EOF
> (library
> (name test)
> (foreign_stubs (language c) (names stub)))
> EOF

$ CLICOLOR_FORCE=1 dune rules -m stub.o | grep -ce "-fdiagnostics-color=always"
$ dune rules -m stub.o | grep -ce "-fdiagnostics-color=always"
1

test, color enabled, color flag disabled
========================================
test color flag disabled
========================

$ cat >dune <<EOF
> (library
Expand All @@ -34,12 +31,12 @@ test, color enabled, color flag disabled
> (language c) (names stub)))
> EOF

$ CLICOLOR_FORCE=1 dune rules -m stub.o | grep -ce "-fdiagnostics-color=always"
$ dune rules -m stub.o | grep -ce "-fdiagnostics-color=always"
0
[1]

test, color disabled, color flag default (enabled)
==================================================
test that we correctly filter out the color codes
=================================================

$ cat >dune <<EOF
> (library
Expand All @@ -48,6 +45,7 @@ test, color disabled, color flag default (enabled)
> (language c) (names stub)))
> EOF

$ CLICOLOR=0 dune rules -m stub.o | grep -ce "-fdiagnostics-color=always"
0
We check there is no ESC [ sequence.

$ dune build 2>&1 | grep `printf '\033\\['`
[1]
4 changes: 2 additions & 2 deletions test/blackbox-tests/test-cases/variables/var-cc.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ No env
> (action (echo %{cc})))
> EOF
$ dune build @cc28 | sed "s,${O_CC} ${O_CCF} ${O_CCPPF},OK,"
$ dune build @cc28 | sed "s,${O_CC} ${O_CCF} ${O_CCPPF} -fdiagnostics-color=always,OK,"
OK
With added env flags
$ cat >> dune <<'EOF'
> (env (_ (c_flags :standard -fPIC)))
> EOF
$ dune build @cc28 | sed "s,${O_CC} ${O_CCF} ${O_CCPPF} -fPIC,OK,"
$ dune build @cc28 | sed "s,${O_CC} ${O_CCF} ${O_CCPPF} -fdiagnostics-color=always -fPIC,OK,"
OK
With redefining env flags
Expand Down

0 comments on commit a1dd271

Please sign in to comment.