2013-04-18 08:06:33 -07:00
|
|
|
(* This filter implements the following extensions:
|
2012-06-29 02:36:32 -07:00
|
|
|
|
2013-04-18 08:06:33 -07:00
|
|
|
In structures:
|
2012-06-29 03:04:17 -07:00
|
|
|
|
2013-04-18 08:06:33 -07:00
|
|
|
[%%IFDEF X]
|
|
|
|
... --> included if the environment variable X is defined
|
|
|
|
[%%ELSE]
|
|
|
|
... --> included if the environment variable X is undefined
|
|
|
|
[%%END]
|
2012-06-29 03:04:17 -07:00
|
|
|
|
2013-04-18 08:06:33 -07:00
|
|
|
|
|
|
|
In expressions:
|
|
|
|
|
|
|
|
[%GETENV X] ---> the string literal representing the compile-time value
|
2012-06-29 03:04:17 -07:00
|
|
|
of environment variable X
|
2013-04-18 08:28:19 -07:00
|
|
|
|
|
|
|
|
|
|
|
In variant type declarations:
|
|
|
|
|
|
|
|
type t =
|
|
|
|
..
|
|
|
|
| C [@IFDEF X] of ... --> the constructor is kept only if X is defined
|
|
|
|
|
|
|
|
|
|
|
|
In match clauses (function/match...with/try...with):
|
|
|
|
|
|
|
|
|
|
|
|
P when [%IFDEF X] -> E --> the case is kept only if X is defined
|
|
|
|
|
2012-06-29 02:36:32 -07:00
|
|
|
*)
|
|
|
|
|
2013-04-18 08:06:33 -07:00
|
|
|
open Ast_helper
|
2013-07-22 07:58:15 -07:00
|
|
|
open! Asttypes
|
2012-06-29 02:36:32 -07:00
|
|
|
open Parsetree
|
|
|
|
open Longident
|
|
|
|
|
2013-04-19 00:40:57 -07:00
|
|
|
let getenv loc arg =
|
2013-04-18 08:06:33 -07:00
|
|
|
match arg with
|
2013-07-22 07:58:15 -07:00
|
|
|
| PStr [{pstr_desc=Pstr_eval({pexp_desc = Pexp_construct ({txt = Lident sym; _}, None); _}, _); _}] ->
|
2013-04-18 08:06:33 -07:00
|
|
|
(try Sys.getenv sym with Not_found -> "")
|
2013-04-19 00:40:57 -07:00
|
|
|
| _ ->
|
2013-04-18 08:06:33 -07:00
|
|
|
Format.eprintf "%a** IFDEF: bad syntax."
|
|
|
|
Location.print_error loc;
|
|
|
|
exit 2
|
|
|
|
|
|
|
|
let empty_str_item = Str.include_ (Mod.structure [])
|
2012-06-29 03:04:17 -07:00
|
|
|
|
2013-09-26 01:03:49 -07:00
|
|
|
let ifdef _args =
|
2013-09-25 08:14:38 -07:00
|
|
|
let stack = ref [] in
|
|
|
|
let eval_attributes =
|
|
|
|
List.for_all
|
|
|
|
(function
|
|
|
|
| {txt="IFDEF"; loc}, arg -> getenv loc arg <> ""
|
|
|
|
| {txt="IFNDEF"; loc}, arg -> getenv loc arg = ""
|
|
|
|
| _ -> true)
|
|
|
|
in
|
|
|
|
let filter_constr cd = eval_attributes cd.pcd_attributes in
|
|
|
|
let open Ast_mapper in
|
|
|
|
let super = default_mapper in
|
|
|
|
{
|
|
|
|
super with
|
|
|
|
|
|
|
|
type_declaration =
|
|
|
|
(fun this td ->
|
|
|
|
let td =
|
|
|
|
match td with
|
|
|
|
| {ptype_kind = Ptype_variant cstrs; _} as td ->
|
|
|
|
{td
|
|
|
|
with ptype_kind = Ptype_variant(List.filter filter_constr cstrs)}
|
|
|
|
| td -> td
|
|
|
|
in
|
|
|
|
super.type_declaration this td
|
|
|
|
);
|
|
|
|
|
|
|
|
cases =
|
|
|
|
(fun this l ->
|
|
|
|
let l =
|
|
|
|
List.fold_right
|
|
|
|
(fun c rest ->
|
|
|
|
match c with
|
|
|
|
| {pc_guard=Some {pexp_desc=Pexp_extension({txt="IFDEF";loc}, arg); _}; _} ->
|
|
|
|
if getenv loc arg = "" then rest else {c with pc_guard=None} :: rest
|
|
|
|
| c -> c :: rest
|
|
|
|
) l []
|
|
|
|
in
|
|
|
|
super.cases this l
|
|
|
|
);
|
|
|
|
|
|
|
|
structure_item =
|
|
|
|
(fun this i ->
|
|
|
|
match i.pstr_desc, !stack with
|
|
|
|
| Pstr_extension(({txt="IFDEF";loc}, arg), _), _ ->
|
|
|
|
stack := (getenv loc arg <> "") :: !stack;
|
|
|
|
empty_str_item
|
|
|
|
| Pstr_extension(({txt="ELSE";loc=_}, _), _), (hd :: tl) ->
|
|
|
|
stack := not hd :: tl;
|
|
|
|
empty_str_item
|
|
|
|
| Pstr_extension(({txt="END";loc=_}, _), _), _ :: tl ->
|
|
|
|
stack := tl;
|
|
|
|
empty_str_item
|
|
|
|
| Pstr_extension(({txt="ELSE"|"END";loc}, _), _), [] ->
|
|
|
|
Format.printf "%a** IFDEF: mo matching [%%%%IFDEF]"
|
|
|
|
Location.print_error loc;
|
|
|
|
exit 2
|
|
|
|
| _, (true :: _ | []) -> super.structure_item this i
|
|
|
|
| _, false :: _ -> empty_str_item
|
|
|
|
);
|
|
|
|
|
|
|
|
expr =
|
|
|
|
(fun this -> function
|
|
|
|
| {pexp_desc = Pexp_extension({txt="GETENV";loc=l}, arg);
|
|
|
|
pexp_loc = loc; _} ->
|
|
|
|
Exp.constant ~loc (Const_string (getenv l arg, None))
|
|
|
|
| x -> super.expr this x
|
|
|
|
);
|
|
|
|
}
|
2012-06-29 02:36:32 -07:00
|
|
|
|
2013-09-26 01:03:49 -07:00
|
|
|
let () = Ast_mapper.run_main ifdef
|