-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect common C compilers and add default CXX flags (#3802)
* Use C preprocessor to detect C compiler * Add CXX flags for most common compilers. * Add option 'use_standard_c_and_cxx_flags' to dune-project * Emit a warning when failing to detect the C compiler Signed-off-by: Ulysse Gérard <[email protected]>
- Loading branch information
Showing
16 changed files
with
235 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
open! Stdune | ||
open Dune_engine | ||
|
||
type ccomp_type = | ||
| Gcc | ||
| Msvc | ||
| Clang | ||
| Other of string | ||
|
||
let base_cxx_flags = function | ||
| Gcc -> [ "-x"; "c++"; "-lstdc++"; "-shared-libgcc" ] | ||
| Clang -> [ "-x"; "c++" ] | ||
| Msvc -> [ "/TP" ] | ||
| _ -> [] | ||
|
||
let preprocessed_filename = "ccomp" | ||
|
||
let ccomp_type dir = | ||
let open Build.O in | ||
let filepath = | ||
Path.Build.(relative (relative dir ".dune") preprocessed_filename) | ||
in | ||
let+ ccomp = Build.contents (Path.build filepath) in | ||
match String.trim ccomp with | ||
| "clang" -> Clang | ||
| "gcc" -> Gcc | ||
| "msvc" -> Msvc | ||
| s -> Other s | ||
|
||
let check_warn = function | ||
| Other s -> | ||
User_warning.emit | ||
[ Pp.textf | ||
"Dune was not able to automatically infer the C compiler in use: \ | ||
\"%s\". Please open an issue on github to help us improve this \ | ||
feature." | ||
s | ||
] | ||
| _ -> () | ||
|
||
let get_flags dir = | ||
let open Build.O in | ||
let+ ccomp_type = ccomp_type dir in | ||
check_warn ccomp_type; | ||
base_cxx_flags ccomp_type |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
(** This module contains a small database of flags that is used when compiling C | ||
and C++ stubs. *) | ||
open! Stdune | ||
|
||
open Dune_engine | ||
|
||
(** 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 | ||
into a c++ compiler for some of the most common compilers *) | ||
val get_flags : Path.Build.t -> string list Build.t |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
open! Stdune | ||
open! Dune_engine | ||
|
||
let header_file_content = | ||
{| | ||
#if defined( __clang__ ) | ||
#define CCOMP clang | ||
#elif defined( _MSC_VER ) | ||
#define CCOMP msvc | ||
#elif defined( __GNUC__ ) | ||
#define CCOMP gcc | ||
#else | ||
#define CCOMP other | ||
#endif | ||
|
||
CCOMP | ||
|} | ||
|
||
let rules ~sctx ~dir = | ||
let file = Path.Build.relative dir Cxx_flags.preprocessed_filename in | ||
let ocfg = (Super_context.context sctx).ocaml_config in | ||
let prog = | ||
Super_context.resolve_program sctx ~dir ~loc:None | ||
(Ocaml_config.c_compiler ocfg) | ||
in | ||
(* let tmp = Path.External.of_string (Filename.get_temp_dir_name ()) in *) | ||
let header_file = Path.Build.relative dir "header_check.h" in | ||
let write_test_file = Action.write_file header_file header_file_content in | ||
let args = | ||
let open Command.Args in | ||
[ ( match Ocaml_config.ccomp_type ocfg with | ||
| Msvc -> A "/EP" | ||
| Other _ -> As [ "-E"; "-P" ] ) | ||
; A Path.(to_absolute_filename (build header_file)) | ||
] | ||
in | ||
let action = | ||
let open Build.With_targets.O in | ||
let+ run_preprocessor = | ||
Command.run ~dir:(Path.build dir) ~stdout_to:file prog args | ||
in | ||
Action.progn [ write_test_file; run_preprocessor ] | ||
in | ||
Super_context.add_rule sctx ~dir action |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
(** Preprocessing-based C compiler detection *) | ||
|
||
val rules : sctx:Super_context.t -> dir:Stdune.Path.Build.t -> unit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#include <caml/mlvalues.h> | ||
extern "C" value baz(value unit) { return Val_int(2046); } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
(library | ||
(name quad) | ||
(modules quad) | ||
(foreign_stubs (language cxx) (names baz))) | ||
|
||
(executable | ||
(name main) | ||
(libraries quad) | ||
(modules main)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
let () = Printf.printf "%d" (Quad.quad ()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
external baz : unit -> int = "baz" | ||
let quad x = baz x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
Default: use_standard_c_and_cxx_flags = false | ||
============================================= | ||
|
||
$ cat >dune-project <<EOF | ||
> (lang dune 2.8) | ||
> EOF | ||
|
||
> The flags that Dune should use | ||
$ GCCF="-x c++ -lstdc++ -shared-libgcc" | ||
$ ClangF="-x c++" | ||
$ MsvcF="/TP" | ||
|
||
> Check that compiler detection is done | ||
$ dune build .dune/ccomp | ||
|
||
$ cat _build/default/.dune/ccomp | | ||
> grep -ce "clang\|gcc\|msvc" | ||
1 | ||
|
||
> No specific flags added | ||
$ dune rules baz.o | tr -s '\n' ' ' | | ||
> grep -ce "$GCCF\|$ClangF|$MsvcF" | ||
0 | ||
[1] | ||
|
||
|
||
With use_standard_c_and_cxx_flags = false | ||
========================================= | ||
|
||
$ cat >dune-project <<EOF | ||
> (lang dune 2.8) | ||
> (use_standard_c_and_cxx_flags false) | ||
> EOF | ||
|
||
> No specific flags added | ||
$ dune rules baz.o | tr -s '\n' ' ' | | ||
> grep -ce "$GCCF\|$ClangF|$MsvcF" | ||
0 | ||
[1] | ||
|
||
With use_standard_c_and_cxx_flags = true | ||
======================================== | ||
|
||
$ cat >dune-project <<EOF | ||
> (lang dune 2.8) | ||
> (use_standard_c_and_cxx_flags true) | ||
> EOF | ||
|
||
> Check that compiler detection is done | ||
$ dune build .dune/ccomp | ||
|
||
$ cat _build/default/.dune/ccomp | | ||
> grep -ce "clang\|gcc\|msvc" | ||
1 | ||
|
||
> Specific flags added | ||
$ dune rules baz.o | tr -s '\n' ' ' | | ||
> grep -ce "$GCCF\|$ClangF\|$MsvcF" | ||
1 | ||
|
||
$ dune exec ./main.exe | ||
2046 |