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
1
+ exception Finally_raised of exn
2
+
3
+ let protect ~(finally : unit -> unit ) work =
4
+ let finally_no_exn () =
5
+ try finally () with e ->
6
+ let bt = Printexc. get_raw_backtrace () in
7
+ Printexc. raise_with_backtrace (Finally_raised e) bt
8
+ in
9
+ match work () with
10
+ | result -> finally_no_exn () ; result
11
+ | exception work_exn ->
12
+ let work_bt = Printexc. get_raw_backtrace () in
13
+ finally_no_exn () ;
14
+ Printexc. raise_with_backtrace work_exn work_bt
9
15
10
16
let read_lines_from_cmd ~max_lines cmd =
11
17
let ic =
@@ -14,29 +20,29 @@ let read_lines_from_cmd ~max_lines cmd =
14
20
Printf. eprintf " read_lines_from_cmd: could not open cmd: '%s'" cmd;
15
21
raise exc
16
22
in
17
- exn_protect ic ~finally: close_in_noerr ~f: (fun ic ->
23
+ protect ~finally: ( fun () -> close_in_noerr ic) (fun () ->
18
24
let rec loop n lines =
19
25
if n < = 0 then List. rev lines
20
26
else
21
27
match input_line ic with
22
28
| line -> loop (n - 1 ) (line :: lines)
23
29
| exception _ ->
24
- Printf. eprintf " read_lines_from_cmd: failed reading line %d, cmd: '%s'"
30
+ Printf. eprintf
31
+ " read_lines_from_cmd: failed reading line %d, cmd: '%s'"
25
32
(max_lines - n + 1 ) cmd;
26
33
raise End_of_file
27
34
in
28
35
loop max_lines [] )
29
36
30
- let opt_map ~default ~f x = match x with
37
+ let opt_map ~default ~f = function
31
38
| Some y -> f y
32
39
| None -> default
40
+
33
41
let opt_is_some = function Some _ -> true | _ -> false
34
42
let getenv_opt s = try Some (Sys. getenv s) with _ -> None
35
43
36
44
let pkg_export =
37
- let has_brewcheck =
38
- opt_is_some (getenv_opt " SQLITE3_OCAML_BREWCHECK" )
39
- in
45
+ let has_brewcheck = opt_is_some (getenv_opt " SQLITE3_OCAML_BREWCHECK" ) in
40
46
if not has_brewcheck then " "
41
47
else
42
48
let cmd = " brew ls sqlite | grep pkgconfig" in
@@ -47,17 +53,18 @@ let pkg_export =
47
53
| _ -> " "
48
54
49
55
let split_ws str =
50
- let l = ref [] in
56
+ let lst = ref [] in
51
57
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
- )
58
+ let len = String. length str in
59
+ while ! i < len do
60
+ let j = try String. index_from str ! i ' ' with Not_found -> len in
61
+ if ! i = j then incr i
62
+ else begin
63
+ lst := String. sub str ! i (j - ! i) :: ! lst;
64
+ i := j + 1 ;
65
+ end
59
66
done ;
60
- List. rev ! l
67
+ List. rev ! lst
61
68
62
69
let () =
63
70
let module C = Configurator. V1 in
0 commit comments