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 c_flags and cxx_flags to env profile settings #1700

Closed
wants to merge 13 commits into from
Closed
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ unreleased
- Generate `.merlin` files that account for normal preprocessors defined using a
subset of the `action` language. (#1768, @rgrinberg)

- Add c_flags and cxx_flags to env profile settings (#1700, @gretay-js)

1.6.2 (05/12/2018)
------------------

Expand Down
4 changes: 4 additions & 0 deletions doc/dune-files.rst
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,10 @@ Fields supported in ``<settings>`` are:

- any OCaml flags field, see `OCaml flags`_ for more details.

- ``(c_flags <flags>)`` and ``(cxx_flags <flags>)``
to specify compilation flags for C and C++ stubs, respectively.
See `library`_ for more details.

- ``(env-vars (<var1> <val1>) .. (<varN> <valN>))``. This will add the
corresponding variables to the environment in which the build commands are
executed, and under which ``dune exec`` runs. At the moment, this mechanism is
Expand Down
8 changes: 8 additions & 0 deletions src/dune_env.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ module Stanza = struct
{ flags : Ordered_set_lang.Unexpanded.t
; ocamlc_flags : Ordered_set_lang.Unexpanded.t
; ocamlopt_flags : Ordered_set_lang.Unexpanded.t
; c_flags : Ordered_set_lang.Unexpanded.t
; cxx_flags : Ordered_set_lang.Unexpanded.t
; env_vars : Env.t
; binaries : File_bindings.Unexpanded.t
}
Expand Down Expand Up @@ -39,6 +41,10 @@ module Stanza = struct
let%map flags = field_oslu "flags"
and ocamlc_flags = field_oslu "ocamlc_flags"
and ocamlopt_flags = field_oslu "ocamlopt_flags"
and c_flags = Ordered_set_lang.Unexpanded.field "c_flags"
~check:(Syntax.since Stanza.syntax (1, 7))
and cxx_flags = Ordered_set_lang.Unexpanded.field "cxx_flags"
~check:(Syntax.since Stanza.syntax (1, 7))
and env_vars = env_vars_field
and binaries = field ~default:File_bindings.empty "binaries"
(Syntax.since Stanza.syntax (1, 6)
Expand All @@ -47,6 +53,8 @@ module Stanza = struct
{ flags
; ocamlc_flags
; ocamlopt_flags
; c_flags
; cxx_flags
; env_vars
; binaries
}
Expand Down
2 changes: 2 additions & 0 deletions src/dune_env.mli
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module Stanza : sig
{ flags : Ordered_set_lang.Unexpanded.t
; ocamlc_flags : Ordered_set_lang.Unexpanded.t
; ocamlopt_flags : Ordered_set_lang.Unexpanded.t
; c_flags : Ordered_set_lang.Unexpanded.t
; cxx_flags : Ordered_set_lang.Unexpanded.t
; env_vars : Env.t
; binaries : File_bindings.Unexpanded.t
}
Expand Down
53 changes: 53 additions & 0 deletions src/env_node.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ type t =
; config : Dune_env.Stanza.t option
; mutable local_binaries : string File_bindings.t option
; mutable ocaml_flags : Ocaml_flags.t option
; mutable c_flags : (unit, string list) Build.t option
; mutable cxx_flags : (unit, string list) Build.t option
; mutable external_ : Env.t option
; mutable artifacts : Artifacts.t option
}
Expand All @@ -19,6 +21,8 @@ let make ~dir ~inherit_from ~scope ~config ~env =
; scope
; config
; ocaml_flags = None
; c_flags = None
; cxx_flags = None
; external_ = env
; artifacts = None
; local_binaries = None
Expand Down Expand Up @@ -109,3 +113,52 @@ let rec ocaml_flags t ~profile ~expander =
in
t.ocaml_flags <- Some flags;
flags

let rec c_flags t ~profile ~expander ~default_context_flags =
match t.c_flags with
| Some x -> x
| None ->
let default =
match t.inherit_from with
| None -> Build.return default_context_flags
| Some (lazy t) -> c_flags t ~profile ~expander ~default_context_flags
in
let flags =
match find_config t ~profile with
| None -> default
| Some cfg ->
let expander = Expander.set_dir expander ~dir:t.dir in
let eval = Expander.expand_and_eval_set expander in
let f = cfg.c_flags in
if Ordered_set_lang.Unexpanded.has_special_forms f then
eval f ~standard:default
else
eval f ~standard:(Build.return [])
in
t.c_flags <- Some flags;
flags


let rec cxx_flags t ~profile ~expander ~default_context_flags =
match t.cxx_flags with
| Some x -> x
| None ->
let default =
match t.inherit_from with
| None -> Build.return default_context_flags
| Some (lazy t) -> cxx_flags t ~profile ~expander ~default_context_flags
in
let flags =
match find_config t ~profile with
| None -> default
| Some cfg ->
let expander = Expander.set_dir expander ~dir:t.dir in
let eval = Expander.expand_and_eval_set expander in
let f = cfg.cxx_flags in
if Ordered_set_lang.Unexpanded.has_special_forms f then
eval f ~standard:default
else
eval f ~standard:(Build.return [])
in
t.cxx_flags <- Some flags;
flags
14 changes: 14 additions & 0 deletions src/env_node.mli
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ val external_ : t -> profile:string -> default:Env.t -> Env.t

val ocaml_flags : t -> profile:string -> expander:Expander.t -> Ocaml_flags.t

val c_flags
: t
-> profile:string
-> expander:Expander.t
-> default_context_flags:string list
-> (unit, string list) Build.t
rgrinberg marked this conversation as resolved.
Show resolved Hide resolved

val cxx_flags
: t
-> profile:string
-> expander:Expander.t
-> default_context_flags:string list
-> (unit, string list) Build.t

val local_binaries
: t
-> profile:string
Expand Down
4 changes: 2 additions & 2 deletions src/expander.ml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ let expand_var_exn t var syn =
(String_with_vars.Var.describe var))

let make ~scope ~(context : Context.t) ~artifacts
~artifacts_host ~cxx_flags =
~artifacts_host =
let expand_var ({ bindings; ocaml_config; env = _; scope
; hidden_env = _
; dir = _ ; artifacts = _; expand_var = _
Expand All @@ -109,7 +109,7 @@ let make ~scope ~(context : Context.t) ~artifacts
in
let ocaml_config = lazy (make_ocaml_config context.ocaml_config) in
let dir = context.build_dir in
let bindings = Pform.Map.create ~context ~cxx_flags in
let bindings = Pform.Map.create ~context in
let env = context.env in
{ dir
; hidden_env = Env.Var.Set.empty
Expand Down
1 change: 0 additions & 1 deletion src/expander.mli
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ val make
-> context:Context.t
-> artifacts:Artifacts.t
-> artifacts_host:Artifacts.t
-> cxx_flags:string list
-> t

val set_env : t -> var:string -> value:string -> t
Expand Down
17 changes: 8 additions & 9 deletions src/lib_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ module Gen (P : Install_rules.Params) = struct
let cctx = Compilation_context.for_wrapped_compat cctx wrapped_compat in
Module_compilation.build_modules cctx ~js_of_ocaml ~dynlink ~dep_graphs

let build_c_file (lib : Library.t) ~expander ~dir ~includes (loc, src, dst) =
let build_c_file (lib : Library.t) ~dir ~expander ~includes (loc, src, dst) =
let c_flags = SC.c_flags sctx ~dir ~expander ~lib in
SC.add_rule sctx ~loc ~dir
(Expander.expand_and_eval_set expander lib.c_flags
~standard:(Build.return (Context.cc_g ctx))
(c_flags
>>>
Build.run
(* We have to execute the rule in the library directory as
Expand All @@ -180,25 +180,24 @@ module Gen (P : Install_rules.Params) = struct
]);
dst

let build_cxx_file (lib : Library.t) ~expander ~dir ~includes (loc, src, dst) =
let build_cxx_file (lib : Library.t) ~dir ~expander ~includes (loc, src, dst) =
let open Arg_spec in
let output_param =
if ctx.ccomp_type = "msvc" then
[Concat ("", [A "/Fo"; Target dst])]
else
[A "-o"; Target dst]
in
let cxx_flags = SC.cxx_flags sctx ~dir ~expander ~lib in
SC.add_rule sctx ~loc ~dir
(Expander.expand_and_eval_set expander lib.cxx_flags
~standard:(Build.return (Context.cc_g ctx))
(cxx_flags
>>>
Build.run
(* We have to execute the rule in the library directory as
the .o is produced in the current directory *)
~dir:(Path.parent_exn src)
(SC.resolve_program ~loc:None ~dir sctx ctx.c_compiler)
([ S [A "-I"; Path ctx.stdlib_dir]
; As (SC.cxx_flags sctx)
; includes
; Dyn (fun cxx_flags -> As cxx_flags)
] @ output_param @
Expand Down Expand Up @@ -288,9 +287,9 @@ module Gen (P : Install_rules.Params) = struct
]
in
List.map lib.c_names ~f:(fun name ->
build_c_file lib ~expander ~dir ~includes (resolve_name name ~ext:".c")
build_c_file lib ~dir ~expander ~includes (resolve_name name ~ext:".c")
) @ List.map lib.cxx_names ~f:(fun name ->
build_cxx_file lib ~expander ~dir ~includes (resolve_name name ~ext:".cpp")
build_cxx_file lib ~dir ~expander ~includes (resolve_name name ~ext:".cpp")
)

let build_stubs lib ~dir ~expander ~requires ~dir_contents ~vlib_stubs_o_files =
Expand Down
4 changes: 3 additions & 1 deletion src/pform.ml
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ module Map = struct
; "env", since ~version:(1, 4) Macro.Env
]

let create ~(context : Context.t) ~cxx_flags =
let create ~(context : Context.t) =
let ocamlopt =
match context.ocamlopt with
| None -> Path.relative context.ocaml_bin "ocamlopt"
Expand All @@ -168,6 +168,8 @@ module Map = struct
| Some p -> path p
in
let cflags = context.ocamlc_cflags in
let cxx_flags = List.filter context.ocamlc_cflags
~f:(fun s -> not (String.is_prefix s ~prefix:"-std=")) in
let strings s = values (Value.L.strings s) in
let lowercased =
[ "cpp" , strings (context.c_compiler :: cflags @ ["-E"])
Expand Down
2 changes: 1 addition & 1 deletion src/pform.mli
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ end
module Map : sig
type t

val create : context:Context.t -> cxx_flags:string list -> t
val create : context:Context.t -> t

val superpose : t -> t -> t

Expand Down
Loading