Skip to content

Commit f71f60e

Browse files
committed
feature: share os family of vars with rules
These variables are available to packaepackage rules. Might as well make them available to all build rules for uniformity and convenience. <!-- ps-id: 2bbdfc8b-79b1-4bf6-9dd7-4fe7c7cb0771 --> Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
1 parent 82bbef1 commit f71f60e

8 files changed

Lines changed: 85 additions & 26 deletions

File tree

doc/changes/11863.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- Introduce `%{os}`,`%{os_version}`, `%{os_distribution}`, and `%{os_family}`
2+
percent forms. These have the same values as their opam counterparts.
3+
(#11863, @rgrinberg)
4+

src/dune_lang/pform.ml

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@ open Import
22
module Payload = Template.Pform.Payload
33

44
module Var = struct
5+
module Os = struct
6+
type t =
7+
| Os
8+
| Os_version
9+
| Os_distribution
10+
| Os_family
11+
12+
let all = [ Os; Os_version; Os_distribution; Os_family ]
13+
14+
let to_string = function
15+
| Os -> "os"
16+
| Os_version -> "os_version"
17+
| Os_distribution -> "os_distribution"
18+
| Os_family -> "os_family"
19+
;;
20+
21+
let to_dyn t = Dyn.variant (to_string t) []
22+
end
23+
524
module Pkg = struct
625
module Section = struct
726
type t =
@@ -42,10 +61,7 @@ module Var = struct
4261

4362
type t =
4463
| Switch
45-
| Os
46-
| Os_version
47-
| Os_distribution
48-
| Os_family
64+
| Os of Os.t
4965
| Build
5066
| Prefix
5167
| User
@@ -58,11 +74,8 @@ module Var = struct
5874
let compare = Poly.compare
5975

6076
let encode_to_latest_dune_lang_version = function
77+
| Os s -> Os.to_string s
6178
| Switch -> "switch"
62-
| Os -> "os"
63-
| Os_version -> "os_version"
64-
| Os_distribution -> "os_distribution"
65-
| Os_family -> "os_family"
6679
| Build -> "build"
6780
| Prefix -> "prefix"
6881
| User -> "user"
@@ -107,6 +120,7 @@ module Var = struct
107120
| Profile
108121
| Context_name
109122
| Os_type
123+
| Os of Os.t
110124
| Architecture
111125
| Arch_sixtyfour
112126
| System
@@ -174,6 +188,7 @@ module Var = struct
174188
| Corrected_suffix -> variant "Corrected_suffix" []
175189
| Inline_tests -> variant "Inline_tests" []
176190
| Toolchain -> variant "Toolchain" []
191+
| Os os -> Os.to_dyn os
177192
| Pkg pkg -> Pkg.to_dyn pkg)
178193
;;
179194

@@ -184,10 +199,10 @@ module Var = struct
184199
(match name with
185200
| "make" -> Some Make
186201
| "switch" -> Some (Pkg Switch)
187-
| "os" -> Some (Pkg Os)
188-
| "os-version" -> Some (Pkg Os_version)
189-
| "os-distribution" -> Some (Pkg Os_distribution)
190-
| "os-family" -> Some (Pkg Os_family)
202+
| "os" -> Some (Pkg (Os Os))
203+
| "os-version" -> Some (Pkg (Os Os_version))
204+
| "os-distribution" -> Some (Pkg (Os Os_distribution))
205+
| "os-family" -> Some (Pkg (Os Os_family))
191206
| "build" -> Some (Pkg Build)
192207
| "prefix" -> Some (Pkg Prefix)
193208
| "user" -> Some (Pkg User)
@@ -484,6 +499,7 @@ let encode_to_latest_dune_lang_version t =
484499
| Corrected_suffix -> Some "corrected-suffix"
485500
| Inline_tests -> Some "inline_tests"
486501
| Toolchain -> Some "toolchain"
502+
| Os os -> Some (Var.Os.to_string os)
487503
| Pkg pkg -> Some (Var.Pkg.encode_to_latest_dune_lang_version pkg)
488504
with
489505
| None -> Pform_was_deleted
@@ -554,10 +570,10 @@ module Env = struct
554570
let vars =
555571
let pkg =
556572
[ "switch", Var.Pkg.Switch
557-
; "os", Os
558-
; "os_version", Os_version
559-
; "os_distribution", Os_distribution
560-
; "os_family", Os_family
573+
; "os", Os Os
574+
; "os_version", Os Os_version
575+
; "os_distribution", Os Os_distribution
576+
; "os_family", Os Os_family
561577
; "build", Build
562578
; "prefix", Prefix
563579
; "user", User
@@ -685,7 +701,11 @@ module Env = struct
685701
; "toolchains", since ~version:(3, 0) Var.Toolchain
686702
]
687703
in
688-
String.Map.of_list_exn (List.concat [ lowercased; uppercased; other ])
704+
let os =
705+
List.map Var.Os.all ~f:(fun v ->
706+
Var.Os.to_string v, since ~version:(3, 20) (Var.Os v))
707+
in
708+
String.Map.of_list_exn (List.concat [ lowercased; uppercased; other; os ])
689709
in
690710
fun syntax_version -> { syntax_version; vars; macros }
691711
;;

src/dune_lang/pform.mli

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ open Import
22
module Payload = Template.Pform.Payload
33

44
module Var : sig
5+
module Os : sig
6+
type t =
7+
| Os
8+
| Os_version
9+
| Os_distribution
10+
| Os_family
11+
end
12+
513
module Pkg : sig
614
module Section : sig
715
type t =
@@ -21,10 +29,7 @@ module Var : sig
2129

2230
type t =
2331
| Switch
24-
| Os
25-
| Os_version
26-
| Os_distribution
27-
| Os_family
32+
| Os of Os.t
2833
| Build
2934
| Prefix
3035
| User
@@ -69,6 +74,7 @@ module Var : sig
6974
| Profile
7075
| Context_name
7176
| Os_type
77+
| Os of Os.t
7278
| Architecture
7379
| Arch_sixtyfour
7480
| System

src/dune_rules/expander.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,10 @@ let expand_pform_var (context : Context.t) ~dir ~source (var : Pform.Var.t) =
516516
(let+ ocaml = ocaml in
517517
lib_config_var var ocaml.lib_config)
518518
|> static
519+
| Os v ->
520+
static
521+
(let+ v = Lock_dir.Sys_vars.(os poll v) in
522+
[ Value.String (Option.value v ~default:"") ])
519523
| Ext_exe
520524
| Cpp
521525
| Pa_cpp

src/dune_rules/lock_dir.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ module Sys_vars = struct
1313
; sys_ocaml_version : string option Memo.Lazy.t
1414
}
1515

16+
let os t (v : Dune_lang.Pform.Var.Os.t) =
17+
Memo.Lazy.force
18+
(match v with
19+
| Os -> t.os
20+
| Os_version -> t.os_version
21+
| Os_distribution -> t.os_distribution
22+
| Os_family -> t.os_family)
23+
;;
24+
1625
let poll =
1726
let vars =
1827
lazy

src/dune_rules/lock_dir.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module Sys_vars : sig
2020
; sys_ocaml_version : string option Memo.Lazy.t
2121
}
2222

23+
val os : t -> Dune_lang.Pform.Var.Os.t -> string option Memo.t
2324
val poll : t
2425
val solver_env : unit -> Dune_pkg.Solver_env.t Memo.t
2526
end

src/dune_rules/pkg_rules.ml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ open Memo.O
33

44
include struct
55
open Dune_pkg
6-
module Sys_poll = Sys_poll
76
module Package_variable = Package_variable
87
module Substs = Substs
98
module Checksum = Checksum
@@ -642,10 +641,10 @@ module Action_expander = struct
642641
let expand_pkg (paths : Path.t Paths.t) (pform : Pform.Var.Pkg.t) =
643642
match pform with
644643
| Switch -> Memo.return [ Value.String "dune" ]
645-
| Os -> sys_poll_var (fun { os; _ } -> os)
646-
| Os_version -> sys_poll_var (fun { os_version; _ } -> os_version)
647-
| Os_distribution -> sys_poll_var (fun { os_distribution; _ } -> os_distribution)
648-
| Os_family -> sys_poll_var (fun { os_family; _ } -> os_family)
644+
| Os Os -> sys_poll_var (fun { os; _ } -> os)
645+
| Os Os_version -> sys_poll_var (fun { os_version; _ } -> os_version)
646+
| Os Os_distribution -> sys_poll_var (fun { os_distribution; _ } -> os_distribution)
647+
| Os Os_family -> sys_poll_var (fun { os_family; _ } -> os_family)
649648
| Sys_ocaml_version ->
650649
sys_poll_var (fun { sys_ocaml_version; _ } -> sys_ocaml_version)
651650
| Build -> Memo.return [ Value.Dir paths.source_dir ]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Demonstrate the following variables: %{os}, %{os_version}, %{os_distribution}, %{os_family}
2+
3+
$ cat >dune-project <<EOF
4+
> (lang dune 3.20)
5+
> EOF
6+
7+
$ cat >dune <<EOF
8+
> (rule (write-file out "%{os}\n%{os_version}\n%{os_distribution}\n%{os_family}\n"))
9+
> EOF
10+
11+
$ dune build out
12+
13+
The values are not going to be portable, so we just count how many we get:
14+
15+
$ grep -c '.' _build/default/out
16+
4

0 commit comments

Comments
 (0)