Skip to content

Commit

Permalink
Add c_flags to env profile settings
Browse files Browse the repository at this point in the history
Signed-off-by: Greta Yorsh <gyorsh@janestreet.com>
  • Loading branch information
gretay-js committed Dec 21, 2018
1 parent d6ca965 commit b03c555
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 10 deletions.
6 changes: 6 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,8 @@ 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 = field_oslu "c_flags"
and cxx_flags = field_oslu "cxx_flags"
and env_vars = env_vars_field
and binaries = field ~default:File_bindings.empty "binaries"
(Syntax.since Stanza.syntax (1, 6)
Expand All @@ -47,6 +51,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
54 changes: 54 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,53 @@ let rec ocaml_flags t ~profile ~expander =
in
t.ocaml_flags <- Some flags;
flags



let rec c_flags t ~profile ~expander =
match t.c_flags with
| Some x -> x
| None ->
let default =
match t.inherit_from with
| None -> Build.return ([])
| Some (lazy t) -> c_flags t ~profile ~expander
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 =
match t.cxx_flags with
| Some x -> x
| None ->
let default =
match t.inherit_from with
| None -> Build.return ([])
| Some (lazy t) -> cxx_flags t ~profile ~expander
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
6 changes: 6 additions & 0 deletions src/env_node.mli
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ 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
-> (unit, string list) Build.t

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

val local_binaries
: t
-> profile:string
Expand Down
20 changes: 10 additions & 10 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 ~includes (loc, src, dst) =
let c_flags = SC.c_flags sctx ~dir ~lib (Context.cc_g ctx) 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,17 +180,17 @@ 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 ~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_gather sctx ~dir ~lib (Context.cc_g ctx) 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
Expand Down Expand Up @@ -253,7 +253,7 @@ module Gen (P : Install_rules.Params) = struct
ocamlmklib ~sandbox:true ~custom:false ~targets:[dynamic]
end

let build_o_files lib ~dir ~expander ~requires ~dir_contents =
let build_o_files lib ~dir ~requires ~dir_contents =
let all_dirs = Dir_contents.dirs dir_contents in
let h_files =
List.fold_left all_dirs ~init:[] ~f:(fun acc dc ->
Expand Down Expand Up @@ -287,15 +287,15 @@ 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 ~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 ~includes (resolve_name name ~ext:".cpp")
)

let build_stubs lib ~dir ~expander ~requires ~dir_contents ~vlib_stubs_o_files =
let lib_o_files =
if Library.has_stubs lib then
build_o_files lib ~dir ~expander ~requires ~dir_contents
build_o_files lib ~dir ~requires ~dir_contents
else
[]
in
Expand Down
39 changes: 39 additions & 0 deletions src/super_context.ml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ module External_env = Env

module Env : sig
val ocaml_flags : t -> dir:Path.t -> Ocaml_flags.t
val c_flags : t -> dir:Path.t -> (unit, string list) Build.t
val cxx_flags : t -> dir:Path.t -> (unit, string list) Build.t
val external_ : t -> dir:Path.t -> External_env.t
val artifacts_host : t -> dir:Path.t -> Artifacts.t
val expander : t -> dir:Path.t -> Expander.t
Expand Down Expand Up @@ -151,6 +153,14 @@ end = struct
let ocaml_flags t ~dir =
Env_node.ocaml_flags (get t ~dir)
~profile:(profile t) ~expander:(expander t ~dir)

let c_flags t ~dir =
Env_node.c_flags (get t ~dir)
~profile:(profile t) ~expander:(expander t ~dir)

let cxx_flags t ~dir =
Env_node.cxx_flags (get t ~dir)
~profile:(profile t) ~expander:(expander t ~dir)
end

let expander = Env.expander
Expand Down Expand Up @@ -237,6 +247,35 @@ let ocaml_flags t ~dir (x : Buildable.t) =
~default:(Env.ocaml_flags t ~dir)
~eval:(Expander.expand_and_eval_set expander)

let c_flags t ~dir ~(lib : Library.t) ccg =
let expander = Env.expander t ~dir in
let eval = Expander.expand_and_eval_set expander in
let flags = lib.c_flags in
let default = Env.c_flags t ~dir in
Build.memoize "c flags"
begin
if Ordered_set_lang.Unexpanded.has_special_forms flags then
let c = eval flags ~standard:default in
let open Build.O in (c >>^ fun l -> l @ ccg)
else
eval flags ~standard:(Build.return ccg)
end


let cxx_flags_gather t ~dir ~(lib : Library.t) ccg =
let expander = Env.expander t ~dir in
let eval = Expander.expand_and_eval_set expander in
let flags = lib.cxx_flags in
let default = Env.cxx_flags t ~dir in
Build.memoize "c flags"
begin
if Ordered_set_lang.Unexpanded.has_special_forms flags then
let c = eval flags ~standard:default in
let open Build.O in (c >>^ fun l -> l @ ccg)
else
eval flags ~standard:(Build.return ccg)
end

let local_binaries t ~dir = Env.local_binaries t ~dir

let dump_env t ~dir =
Expand Down
14 changes: 14 additions & 0 deletions src/super_context.mli
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ val ocaml_flags
-> Buildable.t
-> Ocaml_flags.t

val c_flags
: t
-> dir:Path.t
-> lib:Library.t
-> string list
-> (unit, string list) Build.t

val cxx_flags_gather
: t
-> dir:Path.t
-> lib:Library.t
-> string list
-> (unit, string list) Build.t

(** Binaries that are symlinked in the associated .bin directory of [dir]. This
associated directory is [Path.relative dir ".bin"] *)
val local_binaries : t -> dir:Path.t -> string File_bindings.t
Expand Down

0 comments on commit b03c555

Please sign in to comment.