11module C = Configurator. V1
22
3+ type conf = { os_type : string ; major : int ; minor : int }
4+
35type op = Le | Ge
46
7+ type condition =
8+ | Version of op * int * int
9+ | Os_type of string
10+
511type line =
6- | If of op * int * int
7- | Elseif of op * int * int
12+ | If of condition
13+ | Elseif of condition
814 | Else
915 | Endif
1016 | Raw of string
@@ -26,12 +32,15 @@ let prefix ~pre s =
2632 check 0
2733 )
2834
29- let eval ~major ~minor op i j =
30- match op with
31- | Le -> (major, minor) < = (i, j)
32- | Ge -> (major, minor) > = (i, j)
35+ let eval ~conf = function
36+ | Os_type ty -> conf.os_type = ty
37+ | Version (op , i , j ) ->
38+ match op with
39+ | Le -> (conf.major, conf.minor) < = (i, j)
40+ | Ge -> (conf.major, conf.minor) > = (i, j)
41+
3342
34- let preproc_lines ~file ~major ~ minor (ic : in_channel ) : unit =
43+ let preproc_lines ~file ~conf (ic : in_channel ) : unit =
3544 let pos = ref 0 in
3645 let fail msg =
3746 failwith (Printf. sprintf " at line %d in '%s': %s" ! pos file msg)
@@ -46,13 +55,19 @@ let preproc_lines ~file ~major ~minor (ic : in_channel) : unit =
4655 incr pos;
4756 if line' <> " " && line'.[0 ] = '[' then
4857 if prefix line' ~pre: " [@@@ifle" then
49- Scanf. sscanf line' " [@@@ifle %d.%d]" (fun x y -> If (Le , x, y))
58+ Scanf. sscanf line' " [@@@ifle %d.%d]" (fun x y -> If (Version ( Le , x, y) ))
5059 else if prefix line' ~pre: " [@@@ifge" then
51- Scanf. sscanf line' " [@@@ifge %d.%d]" (fun x y -> If (Ge , x, y))
60+ Scanf. sscanf line' " [@@@ifge %d.%d]" (fun x y -> If (Version ( Ge , x, y) ))
5261 else if prefix line' ~pre: " [@@@elifle" then
53- Scanf. sscanf line' " [@@@elifle %d.%d]" (fun x y -> Elseif (Le , x, y))
62+ Scanf. sscanf line' " [@@@elifle %d.%d]" (fun x y -> Elseif (Version ( Le , x, y) ))
5463 else if prefix line' ~pre: " [@@@elifge" then
55- Scanf. sscanf line' " [@@@elifge %d.%d]" (fun x y -> Elseif (Ge , x, y))
64+ Scanf. sscanf line' " [@@@elifge %d.%d]" (fun x y -> Elseif (Version (Ge , x, y)))
65+ else if prefix line' ~pre: " [@@@ifos" then
66+ Scanf. sscanf line' " [@@@ifos %s]" (fun os_type ->
67+ If (Os_type (String. lowercase_ascii os_type)))
68+ else if prefix line' ~pre: " [@@@elifos" then
69+ Scanf. sscanf line' " [@@@elifos %s]" (fun os_type ->
70+ Elseif (Os_type (String. lowercase_ascii os_type)))
5671 else if line' = " [@@@else_]" then
5772 Else
5873 else if line' = " [@@@endif]" then
@@ -67,8 +82,8 @@ let preproc_lines ~file ~major ~minor (ic : in_channel) : unit =
6782 let rec top () =
6883 match parse_line () with
6984 | Eof -> ()
70- | If ( op , i , j ) ->
71- if eval ~major ~minor op i j then (
85+ | If condition ->
86+ if eval ~conf condition then (
7287 pp_pos () ;
7388 cat_block ()
7489 ) else
@@ -99,8 +114,8 @@ let preproc_lines ~file ~major ~minor (ic : in_channel) : unit =
99114 | Endif ->
100115 pp_pos () ;
101116 top ()
102- | Elseif ( op , i , j ) ->
103- if elseok && eval ~major ~minor op i j then (
117+ | Elseif condition ->
118+ if elseok && eval ~conf condition then (
104119 pp_pos () ;
105120 cat_block ()
106121 ) else
@@ -120,9 +135,10 @@ let () =
120135 let c = C. create " main" in
121136 let version = C. ocaml_config_var_exn c " version" in
122137 let major, minor = Scanf. sscanf version " %u.%u" (fun maj min -> maj, min) in
138+ let os_type = String. lowercase_ascii (C. ocaml_config_var_exn c " os_type" ) in
123139
124140 let ic = open_in file in
125- preproc_lines ~file ~major ~ minor ic;
141+ preproc_lines ~file ~conf: {os_type; major; minor} ic;
126142
127143 Printf. printf " (* file preprocessed in %.3fs *)\n " (Unix. gettimeofday () -. t0);
128144 ()
0 commit comments