Skip to content

Commit 70a38c1

Browse files
committed
remove deps on base and stdio for config
1 parent 456e311 commit 70a38c1

File tree

2 files changed

+40
-19
lines changed

2 files changed

+40
-19
lines changed

src/config/discover.ml

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,69 @@
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
39

410
let read_lines_from_cmd ~max_lines cmd =
511
let ic =
612
try Unix.open_process_in cmd
713
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;
915
raise exc
1016
in
11-
Exn.protectx ic ~finally:In_channel.close ~f:(fun ic ->
17+
exn_protect ic ~finally:close_in_noerr ~f:(fun ic ->
1218
let rec loop n lines =
1319
if n <= 0 then List.rev lines
1420
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'"
1925
(max_lines - n + 1) cmd;
2026
raise End_of_file
2127
in
2228
loop max_lines [])
2329

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+
2436
let pkg_export =
2537
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")
2839
in
2940
if not has_brewcheck then ""
3041
else
3142
let cmd = "brew ls sqlite | grep pkgconfig" in
3243
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
3546
Printf.sprintf "PKG_CONFIG_PATH=%s" path
3647
| _ -> ""
3748

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
3961

4062
let () =
4163
let module C = Configurator.V1 in
4264
C.main ~name:"sqlite3" (fun c ->
4365
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
4567
~f:(function "macosx" -> true | _ -> false)
4668
in
4769
let cflags =
@@ -51,8 +73,8 @@ let () =
5173
let cflags = split_ws cflags in
5274
if
5375
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")
5678
then "-DSQLITE3_DISABLE_LOADABLE_EXTENSIONS" :: cflags
5779
else cflags
5880
| _ -> failwith "pkg-config failed to return cflags"

src/config/dune

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
(executables
2-
(names discover)
3-
(libraries base stdio dune.configurator)
4-
)
2+
(names discover)
3+
(libraries dune.configurator))

0 commit comments

Comments
 (0)