From 670eb20dd34e1fd60c3b37597278c10e8573407e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonin=20D=C3=A9cimo?= Date: Fri, 15 Jan 2021 13:31:02 +0100 Subject: [PATCH 1/2] Export C/C++ compiler found in Cxx_flags MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Antonin Décimo --- src/dune_rules/cxx_flags.ml | 7 ++++++- src/dune_rules/cxx_flags.mli | 12 +++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/dune_rules/cxx_flags.ml b/src/dune_rules/cxx_flags.ml index d65d7648248..8db8ef2d6bb 100644 --- a/src/dune_rules/cxx_flags.ml +++ b/src/dune_rules/cxx_flags.ml @@ -46,8 +46,13 @@ let check_warn = function ] | _ -> () -let get_flags ~for_ ctx = +let ccomp_type ctx = let open Action_builder.O in let+ ccomp_type = ccomp_type ctx.Context.build_dir in check_warn ccomp_type; + ccomp_type + +let get_flags ~for_ ctx = + let open Action_builder.O in + let+ ccomp_type = ccomp_type ctx in base_cxx_flags ~for_ ccomp_type diff --git a/src/dune_rules/cxx_flags.mli b/src/dune_rules/cxx_flags.mli index aa503351a7d..73f7a00014f 100644 --- a/src/dune_rules/cxx_flags.mli +++ b/src/dune_rules/cxx_flags.mli @@ -7,10 +7,20 @@ type phase = | Compile | Link +(** The detected compiler *) +type ccomp_type = + | Gcc + | Msvc + | Clang + | Other of string + (** The name of the file created in the .dune folder after calling the C preprocessor *) val preprocessed_filename : string -(** [get_flags c_compiler] returns the necessary flags to turn this compiler +(** [ccomp_type ctx] returns the C/C++ compiler type. *) +val ccomp_type : Context.t -> ccomp_type Action_builder.t + +(** [get_flags for_:phase ctx] returns the necessary flags to turn this compiler into a c++ compiler for some of the most common compilers *) val get_flags : for_:phase -> Context.t -> string list Action_builder.t From e062d6591608fd62d347e426faf476768af5797b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonin=20D=C3=A9cimo?= Date: Thu, 3 Nov 2022 16:53:04 +0100 Subject: [PATCH 2/2] Use colored output with GCC and Clang MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The flag -fdiagnostics-color=always is added to the :standard set of flags for C stubs, which can be easily removed by the user if needed. Signed-off-by: Antonin Décimo --- CHANGES.md | 4 ++ src/dune_rules/cxx_flags.ml | 5 ++ src/dune_rules/cxx_flags.mli | 4 ++ src/dune_rules/super_context.ml | 21 +++++--- .../c-flags-diagnostics-color.t/dune-project | 1 + .../c-flags-diagnostics-color.t/run.t | 53 +++++++++++++++++++ .../c-flags-diagnostics-color.t/stub.c | 1 + 7 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 test/blackbox-tests/test-cases/c-flags-diagnostics-color.t/dune-project create mode 100644 test/blackbox-tests/test-cases/c-flags-diagnostics-color.t/run.t create mode 100644 test/blackbox-tests/test-cases/c-flags-diagnostics-color.t/stub.c diff --git a/CHANGES.md b/CHANGES.md index e154f1e2e1e..62748560ace 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,10 @@ Unreleased ---------- +- Use colored output with GCC and Clang when compiling C stubs. The + flag `-fdiagnostics-color=always` is added to the `:standard` set of + flags. (#4083, @MisterDA) + - Fix the parsing of decimal and hexadecimal escape literals in `dune`, `dune-package`, and other dune s-expression based files (#6710, @shym) diff --git a/src/dune_rules/cxx_flags.ml b/src/dune_rules/cxx_flags.ml index 8db8ef2d6bb..92ef4ebc499 100644 --- a/src/dune_rules/cxx_flags.ml +++ b/src/dune_rules/cxx_flags.ml @@ -20,6 +20,11 @@ let base_cxx_flags ~for_ cc = | Msvc, Link -> [] | Other _, (Link | Compile) -> [] +let fdiagnostics_color = function + | (Gcc | Clang) when Lazy.force Ansi_color.stderr_supports_color -> + [ "-fdiagnostics-color=always" ] + | _ -> [] + let preprocessed_filename = "ccomp" let ccomp_type build_dir = diff --git a/src/dune_rules/cxx_flags.mli b/src/dune_rules/cxx_flags.mli index 73f7a00014f..edf7c26c36e 100644 --- a/src/dune_rules/cxx_flags.mli +++ b/src/dune_rules/cxx_flags.mli @@ -24,3 +24,7 @@ val ccomp_type : Context.t -> ccomp_type Action_builder.t (** [get_flags for_:phase ctx] returns the necessary flags to turn this compiler into a c++ compiler for some of the most common compilers *) val get_flags : for_:phase -> Context.t -> string list Action_builder.t + +(** [fdiagnostics_color cc] returns the flags activating color diagnostics for + the C/C++ compiler, if supported. *) +val fdiagnostics_color : ccomp_type -> string list diff --git a/src/dune_rules/super_context.ml b/src/dune_rules/super_context.ml index ef880bca5a1..6866e767774 100644 --- a/src/dune_rules/super_context.ml +++ b/src/dune_rules/super_context.ml @@ -7,16 +7,23 @@ let default_context_flags (ctx : Context.t) ~project = in let c, cxx = match Dune_project.use_standard_c_and_cxx_flags project with - | None | Some false -> - (Action_builder.return cflags, Action_builder.return cxxflags) + | None | Some false -> Action_builder.(return cflags, return cxxflags) | Some true -> - let c = cflags @ Ocaml_config.ocamlc_cppflags ctx.ocaml_config in + let open Action_builder.O in + let c = + let+ cc = Cxx_flags.ccomp_type ctx in + let fdiagnostics_color = Cxx_flags.fdiagnostics_color cc in + cflags + @ Ocaml_config.ocamlc_cppflags ctx.ocaml_config + @ fdiagnostics_color + in let cxx = - let open Action_builder.O in - let+ db_flags = Cxx_flags.get_flags ~for_:Compile ctx in - db_flags @ cxxflags + let+ cc = Cxx_flags.ccomp_type ctx + and+ db_flags = Cxx_flags.get_flags ~for_:Compile ctx in + let fdiagnostics_color = Cxx_flags.fdiagnostics_color cc in + db_flags @ cxxflags @ fdiagnostics_color in - (Action_builder.return c, cxx) + (c, cxx) in Foreign_language.Dict.make ~c ~cxx diff --git a/test/blackbox-tests/test-cases/c-flags-diagnostics-color.t/dune-project b/test/blackbox-tests/test-cases/c-flags-diagnostics-color.t/dune-project new file mode 100644 index 00000000000..1863cf14648 --- /dev/null +++ b/test/blackbox-tests/test-cases/c-flags-diagnostics-color.t/dune-project @@ -0,0 +1 @@ +(lang dune 3.5) diff --git a/test/blackbox-tests/test-cases/c-flags-diagnostics-color.t/run.t b/test/blackbox-tests/test-cases/c-flags-diagnostics-color.t/run.t new file mode 100644 index 00000000000..1489d1f8c21 --- /dev/null +++ b/test/blackbox-tests/test-cases/c-flags-diagnostics-color.t/run.t @@ -0,0 +1,53 @@ +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) +================================================= + + $ cat >dune < (library + > (name test) + > (foreign_stubs (language c) (names stub))) + > EOF + + $ CLICOLOR_FORCE=1 dune rules -m stub.o | grep -ce "-fdiagnostics-color=always" + 1 + +test, color enabled, color flag disabled +======================================== + + $ cat >dune < (library + > (name test) + > (foreign_stubs + > (flags :standard \ -fdiagnostics-color=always) + > (language c) (names stub))) + > EOF + + $ CLICOLOR_FORCE=1 dune rules -m stub.o | grep -ce "-fdiagnostics-color=always" + 0 + [1] + +test, color disabled, color flag default (enabled) +================================================== + + $ cat >dune < (library + > (name test) + > (foreign_stubs + > (language c) (names stub))) + > EOF + + $ CLICOLOR=0 dune rules -m stub.o | grep -ce "-fdiagnostics-color=always" + 0 + [1] diff --git a/test/blackbox-tests/test-cases/c-flags-diagnostics-color.t/stub.c b/test/blackbox-tests/test-cases/c-flags-diagnostics-color.t/stub.c new file mode 100644 index 00000000000..74f8f99516b --- /dev/null +++ b/test/blackbox-tests/test-cases/c-flags-diagnostics-color.t/stub.c @@ -0,0 +1 @@ +#error "error message"