Skip to content

Commit 7101533

Browse files
committed
Add os_type label to enable/disable based on Sys.os_type
1 parent f9941da commit 7101533

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

lib/block.ml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ type t = {
111111
contents : string list;
112112
skip : bool;
113113
version_enabled : bool;
114+
os_type_enabled : bool;
114115
set_variables : (string * string) list;
115116
unset_variables : string list;
116117
value : value;
@@ -272,6 +273,16 @@ let version_enabled version =
272273
Label.Relation.compare op (Ocaml_version.compare curr_version v) 0
273274
| None -> true
274275

276+
let os_type_enabled os_type =
277+
match os_type with
278+
| Some (op, v) ->
279+
Label.Relation.compare op
280+
(String.compare
281+
(String.lowercase_ascii Sys.os_type)
282+
(String.lowercase_ascii v))
283+
0
284+
| None -> true
285+
275286
let get_label f (labels : Label.t list) = Util.List.find_map f labels
276287

277288
let label_not_allowed ~loc ~label ~kind =
@@ -296,6 +307,7 @@ type block_config = {
296307
dir : string option;
297308
skip : bool;
298309
version : (Label.Relation.t * Ocaml_version.t) option;
310+
os_type : (Label.Relation.t * string) option;
299311
set_variables : (string * string) list;
300312
unset_variables : string list;
301313
file_inc : string option;
@@ -315,6 +327,7 @@ let get_block_config l =
315327
dir = get_label (function Dir x -> Some x | _ -> None) l;
316328
skip = List.exists (function Label.Skip -> true | _ -> false) l;
317329
version = get_label (function Version (x, y) -> Some (x, y) | _ -> None) l;
330+
os_type = get_label (function Os_type (x, y) -> Some (x, y) | _ -> None) l;
318331
set_variables =
319332
List.filter_map (function Label.Set (v, x) -> Some (v, x) | _ -> None) l;
320333
unset_variables =
@@ -416,6 +429,7 @@ let mk ~loc ~section ~labels ~legacy_labels ~header ~contents ~errors =
416429
| None -> infer_block ~loc ~config ~header ~contents ~errors
417430
in
418431
let+ version_enabled = version_enabled config.version in
432+
let os_type_enabled = os_type_enabled config.os_type in
419433
{
420434
loc;
421435
section;
@@ -425,6 +439,7 @@ let mk ~loc ~section ~labels ~legacy_labels ~header ~contents ~errors =
425439
contents;
426440
skip = config.skip;
427441
version_enabled;
442+
os_type_enabled;
428443
set_variables = config.set_variables;
429444
unset_variables = config.unset_variables;
430445
value;
@@ -471,4 +486,4 @@ let is_active ?section:s t =
471486
| None -> Re.execp (Re.Perl.compile_pat p) "")
472487
| None -> true
473488
in
474-
active && t.version_enabled && not t.skip
489+
active && t.version_enabled && t.os_type_enabled && not t.skip

lib/block.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ type t = {
101101
skip : bool;
102102
version_enabled : bool;
103103
(** Whether the current OCaml version complies with the block's version. *)
104+
os_type_enabled : bool;
105+
(** Whether the current os type complies with the block's version. *)
104106
set_variables : (string * string) list;
105107
unset_variables : string list;
106108
value : value;

lib/label.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ type t =
8989
| Skip
9090
| Non_det of non_det option
9191
| Version of Relation.t * Ocaml_version.t
92+
| Os_type of Relation.t * string
9293
| Set of string * string
9394
| Unset of string
9495
| Block_kind of block_kind
@@ -115,6 +116,7 @@ let pp ppf = function
115116
| Non_det (Some Nd_command) -> Fmt.string ppf "non-deterministic=command"
116117
| Version (op, v) ->
117118
Fmt.pf ppf "version%a%a" Relation.pp op Ocaml_version.pp v
119+
| Os_type (op, v) -> Fmt.pf ppf "os_type%a%s" Relation.pp op v
118120
| Set (v, x) -> Fmt.pf ppf "set-%s=%s" v x
119121
| Unset x -> Fmt.pf ppf "unset-%s" x
120122
| Block_kind bk -> pp_block_kind ppf bk
@@ -170,6 +172,7 @@ let interpret label value =
170172
| Ok v -> Ok (Version (op, v))
171173
| Error (`Msg e) ->
172174
Util.Result.errorf "Invalid `version` label value: %s." e)
175+
| "os_type" -> requires_value ~label ~value (fun op v -> Ok (Os_type (op, v)))
173176
| "non-deterministic" -> (
174177
match value with
175178
| None -> Ok (Non_det None)

lib/label.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type t =
4242
| Skip
4343
| Non_det of non_det option
4444
| Version of Relation.t * Ocaml_version.t
45+
| Os_type of Relation.t * string
4546
| Set of string * string
4647
| Unset of string
4748
| Block_kind of block_kind

0 commit comments

Comments
 (0)