From f804be17e80f7c4014332331f56ed02303185cfb Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Thu, 18 Jun 2020 12:16:19 +0100 Subject: [PATCH] Compiler: more conditional compilation based on Environement variable --- compiler/lib/ocaml_compiler.ml | 20 ++++++-------------- compiler/ppx/ppx_optcomp_light.ml | 27 +++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/compiler/lib/ocaml_compiler.ml b/compiler/lib/ocaml_compiler.ml index a0463b231f..e51467f94f 100644 --- a/compiler/lib/ocaml_compiler.ml +++ b/compiler/lib/ocaml_compiler.ml @@ -35,20 +35,12 @@ let rec constant_of_const : _ -> Code.constant = | Const_float_array sl -> let l = List.map ~f:(fun f -> Code.Float (float_of_string f)) sl in Tuple (Obj.double_array_tag, Array.of_list l, Unknown) - (* - For bucklescript, - - uncomment the two branches below - - and comment out the two last ones - {[ - | Const_pointer (i,_) -> - Int (Int32.of_int i) - | Const_block (tag,_,l) -> - let l = Array.of_list (List.map l ~f:constant_of_const) in - Tuple (tag, l, Unknown) - ]} - *) - | Const_pointer i -> Int (Int32.of_int i) - | Const_block (tag, l) -> + | ((Const_pointer (i, _))[@if BUCKLESCRIPT]) -> Int (Int32.of_int i) + | ((Const_block (tag, _, l))[@if BUCKLESCRIPT]) -> + let l = Array.of_list (List.map l ~f:constant_of_const) in + Tuple (tag, l, Unknown) + | ((Const_pointer i)[@ifnot BUCKLESCRIPT]) -> Int (Int32.of_int i) + | ((Const_block (tag, l))[@ifnot BUCKLESCRIPT]) -> let l = Array.of_list (List.map l ~f:constant_of_const) in Tuple (tag, l, Unknown) diff --git a/compiler/ppx/ppx_optcomp_light.ml b/compiler/ppx/ppx_optcomp_light.ml index 5f0d4848de..1433d9cc5b 100644 --- a/compiler/ppx/ppx_optcomp_light.ml +++ b/compiler/ppx/ppx_optcomp_light.ml @@ -81,8 +81,31 @@ let keep loc (attrs : attributes) = try let keep = List.for_all attrs ~f:(function - | { Location.txt = "if"; _ }, attr_payload -> ( + | { Location.txt = ("if" | "ifnot") as ifnot; _ }, attr_payload -> ( + let norm = + match ifnot with + | "if" -> fun x -> x + | "ifnot" -> fun x -> not x + | _ -> assert false + in match attr_payload with + | PStr + [ { pstr_desc = + Pstr_eval + ( { pexp_desc = Pexp_construct ({ txt = Lident ident; _ }, None) + ; _ + } + , [] ) + ; _ + } + ] -> + let b = + match bool_of_string (Sys.getenv ident) with + | true -> true + | false -> false + | exception _ -> false + in + norm b | PStr [ { pstr_desc = Pstr_eval @@ -123,7 +146,7 @@ let keep loc (attrs : attributes) = let op = get_op op in let a = eval a in let b = eval b in - op (Version.compare a b) 0 + norm (op (Version.compare a b) 0) | _ -> raise Invalid) | _ -> true) in