1
- open Base
2
- open Stdio
1
+ let exn_protect ~finally x ~f =
2
+ try
3
+ let y = f x in
4
+ finally x;
5
+ y
6
+ with e ->
7
+ finally x;
8
+ raise e
3
9
4
10
let read_lines_from_cmd ~max_lines cmd =
5
11
let ic =
6
12
try Unix. open_process_in cmd
7
13
with exc ->
8
- eprintf " read_lines_from_cmd: could not open cmd: '%s'" cmd;
14
+ Printf. eprintf " read_lines_from_cmd: could not open cmd: '%s'" cmd;
9
15
raise exc
10
16
in
11
- Exn. protectx ic ~finally: In_channel. close ~f: (fun ic ->
17
+ exn_protect ic ~finally: close_in_noerr ~f: (fun ic ->
12
18
let rec loop n lines =
13
19
if n < = 0 then List. rev lines
14
20
else
15
- match In_channel. input_line ic with
16
- | Some line -> loop (n - 1 ) (line :: lines)
17
- | None ->
18
- eprintf " read_lines_from_cmd: failed reading line %d, cmd: '%s'"
21
+ match input_line ic with
22
+ | line -> loop (n - 1 ) (line :: lines)
23
+ | exception _ ->
24
+ Printf. eprintf " read_lines_from_cmd: failed reading line %d, cmd: '%s'"
19
25
(max_lines - n + 1 ) cmd;
20
26
raise End_of_file
21
27
in
22
28
loop max_lines [] )
23
29
30
+ let opt_map ~default ~f x = match x with
31
+ | Some y -> f y
32
+ | None -> default
33
+ let opt_is_some = function Some _ -> true | _ -> false
34
+ let getenv_opt s = try Some (Sys. getenv s) with _ -> None
35
+
24
36
let pkg_export =
25
37
let has_brewcheck =
26
- try ignore (Caml.Sys. getenv " SQLITE3_OCAML_BREWCHECK" ); true
27
- with _ -> false
38
+ opt_is_some (getenv_opt " SQLITE3_OCAML_BREWCHECK" )
28
39
in
29
40
if not has_brewcheck then " "
30
41
else
31
42
let cmd = " brew ls sqlite | grep pkgconfig" in
32
43
match read_lines_from_cmd ~max_lines: 1 cmd with
33
- | [fullpath] when String. (fullpath <> " " ) ->
34
- let path = Caml. Filename. dirname fullpath in
44
+ | [fullpath] when not ( String. equal fullpath " " ) ->
45
+ let path = Filename. dirname fullpath in
35
46
Printf. sprintf " PKG_CONFIG_PATH=%s" path
36
47
| _ -> " "
37
48
38
- let split_ws str = List. filter (String. split ~on: ' ' str) ~f: (String. (<> ) " " )
49
+ let split_ws str =
50
+ let l = ref [] in
51
+ let i = ref 0 in
52
+ while ! i < String. length str do
53
+ let j = String. index_from str ! i ' ' in
54
+ if ! i= j then incr i
55
+ else (
56
+ l := String. sub str ! i (j- ! i) :: ! l;
57
+ i := j+ 1 ;
58
+ )
59
+ done ;
60
+ List. rev ! l
39
61
40
62
let () =
41
63
let module C = Configurator. V1 in
42
64
C. main ~name: " sqlite3" (fun c ->
43
65
let is_macosx =
44
- Option. value_map (C. ocaml_config_var c " system" ) ~default: false
66
+ opt_map (C. ocaml_config_var c " system" ) ~default: false
45
67
~f: (function "macosx" -> true | _ -> false )
46
68
in
47
69
let cflags =
@@ -51,8 +73,8 @@ let () =
51
73
let cflags = split_ws cflags in
52
74
if
53
75
is_macosx ||
54
- Option. is_some (
55
- Caml.Sys. getenv_opt " SQLITE3_DISABLE_LOADABLE_EXTENSIONS" )
76
+ opt_is_some (
77
+ getenv_opt " SQLITE3_DISABLE_LOADABLE_EXTENSIONS" )
56
78
then " -DSQLITE3_DISABLE_LOADABLE_EXTENSIONS" :: cflags
57
79
else cflags
58
80
| _ -> failwith " pkg-config failed to return cflags"
0 commit comments