Skip to content

Commit

Permalink
Merge pull request #2472 from ocaml/simplify-dune_env-option
Browse files Browse the repository at this point in the history
Remove the option in Dune_env.Stanza.t option
  • Loading branch information
bobot authored Jul 30, 2019
2 parents dcccb59 + 33967cc commit ed64846
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 70 deletions.
4 changes: 2 additions & 2 deletions doc/dune-files.rst
Original file line number Diff line number Diff line change
Expand Up @@ -810,8 +810,8 @@ Fields supported in ``<settings>`` are:

- ``(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
only supported in ``dune-workspace`` files.
executed, and under which ``dune exec`` runs.


- ``(binaries <filepath> (<filepath> as <name>))``. This will make the binary at
``<filepath>`` as ``<name>``. If the ``<name>`` isn't provided, then it will
Expand Down
17 changes: 4 additions & 13 deletions src/context.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,14 @@ end

module Env_nodes = struct
type t =
{ context: Dune_env.Stanza.t option
; workspace: Dune_env.Stanza.t option
{ context: Dune_env.Stanza.t
; workspace: Dune_env.Stanza.t
}

let extra_env ~profile env_nodes =
let make_env l =
let open Option.O in
Option.value
~default:Env.empty
(let* stanza = l in
let+ env = Dune_env.Stanza.find stanza ~profile
in
env.env_vars)
in
Env.extend_env
(make_env env_nodes.context)
(make_env env_nodes.workspace)
(Dune_env.Stanza.find env_nodes.context ~profile).env_vars
(Dune_env.Stanza.find env_nodes.workspace ~profile).env_vars
end

type t =
Expand Down
4 changes: 2 additions & 2 deletions src/context.mli
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ end

module Env_nodes : sig
type t =
{ context: Dune_env.Stanza.t option
; workspace: Dune_env.Stanza.t option
{ context: Dune_env.Stanza.t
; workspace: Dune_env.Stanza.t
}
end

Expand Down
11 changes: 11 additions & 0 deletions src/dune_env.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ module Stanza = struct
; inline_tests : Inline_tests.t option
}

let empty_config = {
flags = Ocaml_flags.Spec.standard
; c_flags = C.Kind.Dict.make_both Ordered_set_lang.Unexpanded.standard
; env_vars = Env.empty
; binaries = []
; inline_tests = None
}

type pattern =
| Profile of string
| Any
Expand Down Expand Up @@ -100,7 +108,10 @@ module Stanza = struct
in
{ loc; rules }

let empty = { loc = Loc.none; rules = [] }

let find t ~profile =
Option.value ~default:empty_config @@
List.find_map t.rules ~f:(fun (pat, cfg) ->
match pat with
| Any -> Some cfg
Expand Down
4 changes: 3 additions & 1 deletion src/dune_env.mli
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ module Stanza : sig

val decode : t Dune_lang.Decoder.t

val find : t -> profile:string -> config option
val empty : t

val find : t -> profile:string -> config
end

type stanza +=
Expand Down
56 changes: 23 additions & 33 deletions src/env_node.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type t =
{ dir : Path.Build.t
; inherit_from : t Lazy.t option
; scope : Scope.t
; config : Dune_env.Stanza.t option
; config : Dune_env.Stanza.t
; mutable local_binaries : File_binding.Expanded.t list option
; mutable ocaml_flags : Ocaml_flags.t option
; mutable c_flags : (unit, string list) Build.t C.Kind.Dict.t option
Expand All @@ -29,8 +29,7 @@ let make ~dir ~inherit_from ~scope ~config =
}

let find_config t ~profile =
let open Option.O in
t.config >>= Dune_env.Stanza.find ~profile
Dune_env.Stanza.find t.config ~profile

let rec local_binaries t ~profile ~expander =
match t.local_binaries with
Expand All @@ -42,14 +41,11 @@ let rec local_binaries t ~profile ~expander =
| Some (lazy t) -> local_binaries t ~profile ~expander
in
let local_binaries =
match find_config t ~profile with
| None -> default
| Some cfg ->
default @
List.map cfg.binaries
~f:(File_binding.Unexpanded.expand ~dir:t.dir ~f:(fun template ->
Expander.expand expander ~mode:Single ~template
|> Value.to_string ~dir:(Path.build t.dir)))
default @
List.map (find_config t ~profile).binaries
~f:(File_binding.Unexpanded.expand ~dir:t.dir ~f:(fun template ->
Expander.expand expander ~mode:Single ~template
|> Value.to_string ~dir:(Path.build t.dir)))
in
t.local_binaries <- Some local_binaries;
local_binaries
Expand All @@ -64,11 +60,9 @@ let rec external_ t ~profile ~default =
| Some (lazy t) -> external_ t ~default ~profile
in
let (env, have_binaries) =
match find_config t ~profile with
| None -> (default, false)
| Some cfg ->
( Env.extend_env default cfg.env_vars
, not (List.is_empty cfg.binaries)
let cfg = find_config t ~profile in
( Env.extend_env default cfg.env_vars
, not (List.is_empty cfg.binaries)
)
in
let env =
Expand Down Expand Up @@ -113,14 +107,12 @@ let rec ocaml_flags t ~profile ~expander =
| Some (lazy t) -> ocaml_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
Ocaml_flags.make
~spec:cfg.flags
~default
~eval:(Expander.expand_and_eval_set expander)
let cfg = find_config t ~profile in
let expander = Expander.set_dir expander ~dir:t.dir in
Ocaml_flags.make
~spec:cfg.flags
~default
~eval:(Expander.expand_and_eval_set expander)
in
t.ocaml_flags <- Some flags;
flags
Expand All @@ -131,7 +123,7 @@ let rec inline_tests t ~profile =
| None ->
let state : Dune_env.Stanza.Inline_tests.t =
match find_config t ~profile with
| None | Some {inline_tests = None; _} ->
| {inline_tests = None; _} ->
begin match t.inherit_from with
| None ->
if profile = "release" then
Expand All @@ -140,7 +132,7 @@ let rec inline_tests t ~profile =
Enabled
| Some (lazy t) -> inline_tests t ~profile
end
| Some {inline_tests = Some s; _} -> s
| {inline_tests = Some s; _} -> s
in
t.inline_tests <- Some state;
state
Expand All @@ -155,13 +147,11 @@ let rec c_flags t ~profile ~expander ~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
C.Kind.Dict.mapi cfg.c_flags ~f:(fun ~kind f ->
let default = C.Kind.Dict.get default kind in
Expander.expand_and_eval_set expander f ~standard:default)
let cfg = find_config t ~profile in
let expander = Expander.set_dir expander ~dir:t.dir in
C.Kind.Dict.mapi cfg.c_flags ~f:(fun ~kind f ->
let default = C.Kind.Dict.get default kind in
Expander.expand_and_eval_set expander f ~standard:default)
in
t.c_flags <- Some flags;
flags
2 changes: 1 addition & 1 deletion src/env_node.mli
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ val make
: dir:Path.Build.t
-> inherit_from:t Lazy.t option
-> scope:Scope.t
-> config:Dune_env.Stanza.t option
-> config:Dune_env.Stanza.t
-> t

val scope : t -> Scope.t
Expand Down
5 changes: 5 additions & 0 deletions src/ocaml_flags.ml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ type 'a t' =
module Spec = struct
type t = Ordered_set_lang.Unexpanded.t t'

let standard = {
common = Ordered_set_lang.Unexpanded.standard
; specific = Mode.Dict.make_both Ordered_set_lang.Unexpanded.standard
}

let decode =
let open Dune_lang.Decoder in
let field_oslu = Ordered_set_lang.Unexpanded.field in
Expand Down
1 change: 1 addition & 0 deletions src/ocaml_flags.mli
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type t
module Spec : sig
type t
val decode : t Dune_lang.Decoder.fields_parser
val standard : t
end

val make
Expand Down
14 changes: 4 additions & 10 deletions src/super_context.ml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ end = struct
include Env_context

let get_env_stanza t ~dir =
Option.value ~default:Dune_env.Stanza.empty @@
let open Option.O in
let* stanza = Path.Build.Map.find t.stanzas_per_dir dir in
List.find_map stanza.data ~f:(function
Expand Down Expand Up @@ -460,16 +461,9 @@ let create
~inherit_from
~config
in
match context.env_nodes with
| { context = None; workspace = None } ->
make ~config:(Some { loc = Loc.none; rules = [] }) ~inherit_from:None
| { context = Some _ as config; workspace = None }
| { context = None; workspace = Some _ as config } ->
make ~config ~inherit_from:None
| { context = Some _ as context ; workspace = Some _ as workspace } ->
make ~config:context
~inherit_from:(Some (lazy (make ~inherit_from:None
~config:workspace)))
make ~config:context.env_nodes.context
~inherit_from:(Some (lazy (make ~inherit_from:None
~config:context.env_nodes.workspace)))
)
in
let artifacts =
Expand Down
10 changes: 5 additions & 5 deletions src/workspace.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ open Dune_lang.Decoder
let syntax = Stanza.syntax

let env_field =
field_o "env"
field "env" ~default:Dune_env.Stanza.empty
(Syntax.since syntax (1, 1) >>>
Dune_env.Stanza.decode)

Expand Down Expand Up @@ -50,7 +50,7 @@ module Context = struct
{ loc : Loc.t
; profile : string
; targets : Target.t list
; env : Dune_env.Stanza.t option
; env : Dune_env.Stanza.t
; toolchain : string option
; name : string
; host_context : string option
Expand Down Expand Up @@ -182,7 +182,7 @@ module Context = struct
~default:Config.default_build_profile
; name = "default"
; host_context = None
; env = None
; env = Dune_env.Stanza.empty
; toolchain = None
; paths = []
}
Expand All @@ -191,7 +191,7 @@ end
type t =
{ merlin_context : string option
; contexts : Context.t list
; env : Dune_env.Stanza.t option
; env : Dune_env.Stanza.t
}

include Versioned_file.Make(struct type t = unit end)
Expand Down Expand Up @@ -289,7 +289,7 @@ let t ?x ?profile () = fields (t ?x ?profile ())
let default ?x ?profile () =
{ merlin_context = Some "default"
; contexts = [Context.default ?x ?profile ()]
; env = None
; env = Dune_env.Stanza.empty
}

let load ?x ?profile p =
Expand Down
6 changes: 3 additions & 3 deletions src/workspace.mli
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Context : sig
{ loc : Loc.t
; profile : string
; targets : Target.t list
; env : Dune_env.Stanza.t option
; env : Dune_env.Stanza.t
; toolchain : string option
; name : string
; host_context : string option
Expand All @@ -40,7 +40,7 @@ module Context : sig

val name : t -> string

val env : t -> Dune_env.Stanza.t option
val env : t -> Dune_env.Stanza.t

val host_context : t -> string option
end
Expand All @@ -51,7 +51,7 @@ end
type t = private
{ merlin_context : string option
; contexts : Context.t list
; env : Dune_env.Stanza.t option
; env : Dune_env.Stanza.t
}

val load : ?x:string -> ?profile:string -> Path.t -> t
Expand Down

0 comments on commit ed64846

Please sign in to comment.