Skip to content

Commit f908775

Browse files
authored
Compiler: more conditional compilation based on Environement variable (#1026)
1 parent a736bb1 commit f908775

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

compiler/lib/ocaml_compiler.ml

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,12 @@ let rec constant_of_const : _ -> Code.constant =
3535
| Const_float_array sl ->
3636
let l = List.map ~f:(fun f -> Code.Float (float_of_string f)) sl in
3737
Tuple (Obj.double_array_tag, Array.of_list l, Unknown)
38-
(*
39-
For bucklescript,
40-
- uncomment the two branches below
41-
- and comment out the two last ones
42-
{[
43-
| Const_pointer (i,_) ->
44-
Int (Int32.of_int i)
45-
| Const_block (tag,_,l) ->
46-
let l = Array.of_list (List.map l ~f:constant_of_const) in
47-
Tuple (tag, l, Unknown)
48-
]}
49-
*)
50-
| Const_pointer i -> Int (Int32.of_int i)
51-
| Const_block (tag, l) ->
38+
| ((Const_pointer (i, _))[@if BUCKLESCRIPT]) -> Int (Int32.of_int i)
39+
| ((Const_block (tag, _, l))[@if BUCKLESCRIPT]) ->
40+
let l = Array.of_list (List.map l ~f:constant_of_const) in
41+
Tuple (tag, l, Unknown)
42+
| ((Const_pointer i)[@ifnot BUCKLESCRIPT]) -> Int (Int32.of_int i)
43+
| ((Const_block (tag, l))[@ifnot BUCKLESCRIPT]) ->
5244
let l = Array.of_list (List.map l ~f:constant_of_const) in
5345
Tuple (tag, l, Unknown)
5446

compiler/ppx/ppx_optcomp_light.ml

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,31 @@ let keep loc (attrs : attributes) =
8181
try
8282
let keep =
8383
List.for_all attrs ~f:(function
84-
| { Location.txt = "if"; _ }, attr_payload -> (
84+
| { Location.txt = ("if" | "ifnot") as ifnot; _ }, attr_payload -> (
85+
let norm =
86+
match ifnot with
87+
| "if" -> fun x -> x
88+
| "ifnot" -> fun x -> not x
89+
| _ -> assert false
90+
in
8591
match attr_payload with
92+
| PStr
93+
[ { pstr_desc =
94+
Pstr_eval
95+
( { pexp_desc = Pexp_construct ({ txt = Lident ident; _ }, None)
96+
; _
97+
}
98+
, [] )
99+
; _
100+
}
101+
] ->
102+
let b =
103+
match bool_of_string (Sys.getenv ident) with
104+
| true -> true
105+
| false -> false
106+
| exception _ -> false
107+
in
108+
norm b
86109
| PStr
87110
[ { pstr_desc =
88111
Pstr_eval
@@ -123,7 +146,7 @@ let keep loc (attrs : attributes) =
123146
let op = get_op op in
124147
let a = eval a in
125148
let b = eval b in
126-
op (Version.compare a b) 0
149+
norm (op (Version.compare a b) 0)
127150
| _ -> raise Invalid)
128151
| _ -> true)
129152
in

0 commit comments

Comments
 (0)