diff --git a/CHANGES.md b/CHANGES.md index 93d77f848b9..aacd5c4ca1c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -69,7 +69,8 @@ unreleased `%{ocaml-config:os_type}` (#1764, @diml) - Allow `enabled_if` fields in `library` stanzas, restricted to the - `%{os_type}` variable (#1764, @diml) + `%{os_type}`, `%{model}`, `%{architecture}`, `%{system}` variables (#1764, + #2164 @diml, @rgrinberg) 1.9.3 (06/05/2019) ------------------ diff --git a/doc/dune-files.rst b/doc/dune-files.rst index 42282082ad7..42a9fe9621c 100644 --- a/doc/dune-files.rst +++ b/doc/dune-files.rst @@ -1097,6 +1097,12 @@ Dune supports the following variables: workspace file) - ``os_type`` is the type of the OS the build is targetting. This is the same as ``ocaml-config:os_type`` +- ``architecture`` is the type of the architecture the build is targetting. This + is the same as ``ocaml-config:architecture`` +- ``model`` is the type of the cpu the build is targetting. This is + the same as ``ocaml-config:model`` +- ``system`` is the name of the OS the build is targetting. This is the same as + ``ocaml-config:system`` In addition, ``(action ...)`` fields support the following special variables: diff --git a/src/context.ml b/src/context.ml index 31ebfdc1009..1ad25a87154 100644 --- a/src/context.ml +++ b/src/context.ml @@ -85,6 +85,7 @@ type t = ; ext_dll : string ; ext_exe : string ; os_type : string + ; model : string ; default_executable_name : string ; host : string ; target : string @@ -473,6 +474,7 @@ let create ~(kind : Kind.t) ~path ~env ~env_nodes ~name ~merlin ~targets ; ext_dll = Ocaml_config.ext_dll ocfg ; ext_exe = Ocaml_config.ext_exe ocfg ; os_type = Ocaml_config.os_type ocfg + ; model = Ocaml_config.model ocfg ; default_executable_name = Ocaml_config.default_executable_name ocfg ; host = Ocaml_config.host ocfg ; target = Ocaml_config.target ocfg @@ -703,4 +705,7 @@ let lib_config t = ; ext_obj = t.ext_obj ; ext_lib = t.ext_lib ; os_type = t.os_type + ; architecture = t.architecture + ; system = t.system + ; model = t.model } diff --git a/src/context.mli b/src/context.mli index ca91ee55866..8d8d1167de8 100644 --- a/src/context.mli +++ b/src/context.mli @@ -109,6 +109,7 @@ type t = ; ext_dll : string ; ext_exe : string ; os_type : string + ; model : string ; default_executable_name : string ; host : string ; target : string diff --git a/src/dune_file.ml b/src/dune_file.ml index e6f5dbb290f..5f08ec56fd6 100644 --- a/src/dune_file.ml +++ b/src/dune_file.ml @@ -1028,11 +1028,14 @@ module Library = struct Blang.fold_vars enabled_if ~init:() ~f:(fun var () -> match String_with_vars.Var.name var, String_with_vars.Var.payload var with - | "os_type", None -> () + | var, None when + List.mem var ~set:Lib_config.allowed_in_enabled_if -> () | _ -> Errors.fail (String_with_vars.Var.loc var) - "Only the 'os_type' variable is allowed in the 'enabled_if' \ - field of libraries."); + "Only %s are allowed in the 'enabled_if' \ + field of libraries." + (String.enumerate_and Lib_config.allowed_in_enabled_if) + ); { name ; public ; synopsis diff --git a/src/lib_config.ml b/src/lib_config.ml index fc75bfa9e29..6226c5c3000 100644 --- a/src/lib_config.ml +++ b/src/lib_config.ml @@ -5,4 +5,23 @@ type t = ; ext_lib : string ; ext_obj : string ; os_type : string + ; architecture : string + ; system : string + ; model : string } + +let var_map = + [ "architecture", (fun t -> t.architecture) + ; "system", (fun t -> t.system) + ; "model", (fun t -> t.model) + ; "os_type", (fun t -> t.os_type) + ] + +let allowed_in_enabled_if = List.map ~f:fst var_map + +let get_for_enabled_if t ~var = + match List.assoc var_map var with + | Some f -> f t + | None -> + Exn.code_error "Lib_config.get_for_enabled_if: var not allowed" + ["var", Sexp.Encoder.string var] diff --git a/src/lib_config.mli b/src/lib_config.mli new file mode 100644 index 00000000000..bfc18afe529 --- /dev/null +++ b/src/lib_config.mli @@ -0,0 +1,13 @@ +type t = + { has_native : bool + ; ext_lib : string + ; ext_obj : string + ; os_type : string + ; architecture : string + ; system : string + ; model : string + } + +val allowed_in_enabled_if : string list + +val get_for_enabled_if : t -> var:string -> string diff --git a/src/lib_info.ml b/src/lib_info.ml index 8f4eadd7027..ee0f15ba14d 100644 --- a/src/lib_info.ml +++ b/src/lib_info.ml @@ -93,7 +93,8 @@ let user_written_deps t = ~f:(fun acc s -> Dune_file.Lib_dep.Direct s :: acc) let of_library_stanza ~dir - ~lib_config:{ Lib_config.has_native; ext_lib; ext_obj; os_type} + ~lib_config:({ Lib_config.has_native; ext_lib; ext_obj; _ } + as lib_config) (conf : Dune_file.Library.t) = let (_loc, lib_name) = conf.name in let obj_dir = @@ -166,7 +167,9 @@ let of_library_stanza ~dir Blang.eval conf.enabled_if ~dir ~f:(fun v _ver -> match String_with_vars.Var.name v, String_with_vars.Var.payload v with - | "os_type", None -> Some [String os_type] + | var, None -> + let value = Lib_config.get_for_enabled_if lib_config ~var in + Some [String value] | _ -> None) in if not enabled_if_result then diff --git a/src/pform.ml b/src/pform.ml index 089c12dcff0..fcf2b784845 100644 --- a/src/pform.ml +++ b/src/pform.ml @@ -210,6 +210,12 @@ module Map = struct ; "ROOT" , renamed_in ~version:(1, 0) ~new_name:"workspace_root" ; "os_type" , since ~version:(1, 10) (Var.Values [String context.os_type]) + ; "architecture" , since ~version:(1, 10) + (Var.Values [String context.architecture]) + ; "system" , since ~version:(1, 10) + (Var.Values [String context.system]) + ; "model" , since ~version:(1, 10) + (Var.Values [String context.model]) ] in { vars =