Skip to content

Commit e690b07

Browse files
authored
Merge pull request #48 from anmonteiro/anmonteiro/discover-pkg-config
Port the config discovery script to pkg-config
2 parents c14d830 + 42c42e9 commit e690b07

File tree

1 file changed

+61
-37
lines changed

1 file changed

+61
-37
lines changed

src/config/discover.ml

Lines changed: 61 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
module C = Configurator.V1
2+
13
open Printf
24

35
let find_number ~pos str =
@@ -21,43 +23,65 @@ let find_number ~pos str =
2123
let number_lst, next = loop ~pos in
2224
String.concat "" number_lst, next
2325

24-
let () =
25-
let module C = Configurator.V1 in
26-
C.main ~name:"postgresql" (fun _c ->
27-
let cmd = "pg_config --includedir --libdir --version" in
28-
let ic =
29-
try Unix.open_process_in cmd
30-
with exc -> eprintf "could not open pg_config, cmd: '%s'" cmd; raise exc
26+
let pg_major_minor ic =
27+
let line = input_line ic in
28+
let print_fail () =
29+
eprintf "Unable to find versions from line '%s'" line
30+
in
31+
let exit_fail () = print_fail (); exit 1 in
32+
try
33+
let first_space = String.index line ' ' in
34+
let major, next = find_number ~pos:first_space line in
35+
let minor =
36+
(* Can also handle release candidates *)
37+
let c = line.[next] in
38+
if c = '.' then fst (find_number ~pos:next line)
39+
else if c <> 'r' || line.[next + 1] <> 'c' then exit_fail ()
40+
else "0"
3141
in
32-
Fun.protect ~finally:(fun () -> close_in ic) (fun () ->
33-
let pgsql_includedir = "-I" ^ input_line ic in
34-
let pgsql_libdir = "-L" ^ input_line ic in
35-
let major, minor =
36-
let line = input_line ic in
37-
let print_fail () =
38-
eprintf "Unable to find versions from line '%s', cmd: '%s'" line cmd
39-
in
40-
let exit_fail () = print_fail (); exit 1 in
41-
try
42-
let first_space = String.index line ' ' in
43-
let major, next = find_number ~pos:first_space line in
44-
let minor =
45-
(* Can also handle release candidates *)
46-
let c = line.[next] in
47-
if c = '.' then fst (find_number ~pos:next line)
48-
else if c <> 'r' || line.[next + 1] <> 'c' then exit_fail ()
49-
else "0"
50-
in
51-
if major = "" || minor = "" then exit_fail ()
52-
else
53-
"-DPG_OCAML_MAJOR_VERSION=" ^ major,
54-
"-DPG_OCAML_MINOR_VERSION=" ^ minor
55-
with exn -> print_fail (); raise exn
42+
if major = "" || minor = "" then exit_fail ()
43+
else
44+
"-DPG_OCAML_MAJOR_VERSION=" ^ major,
45+
"-DPG_OCAML_MINOR_VERSION=" ^ minor
46+
with exn -> print_fail (); raise exn
47+
48+
let major_minor_from_pgconfig () =
49+
let cmd = "pg_config --version" in
50+
let ic =
51+
try Unix.open_process_in cmd
52+
with exc -> eprintf "could not open pg_config, cmd: '%s'" cmd; raise exc
53+
in
54+
Fun.protect ~finally:(fun () -> close_in ic) (fun () -> pg_major_minor ic)
55+
56+
let from_pgconfig () =
57+
let cmd = "pg_config --includedir --libdir --version" in
58+
let ic =
59+
try Unix.open_process_in cmd
60+
with exc -> eprintf "could not open pg_config, cmd: '%s'" cmd; raise exc
61+
in
62+
Fun.protect ~finally:(fun () -> close_in ic) (fun () ->
63+
let pgsql_includedir = "-I" ^ input_line ic in
64+
let pgsql_libdir = "-L" ^ input_line ic in
65+
let major, minor = pg_major_minor ic in
66+
{ C.Pkg_config.cflags = [pgsql_includedir; major; minor]
67+
; libs = [pgsql_libdir; "-lpq"]
68+
})
69+
70+
let () =
71+
C.main ~name:"postgresql" (fun c ->
72+
let conf =
73+
match C.Pkg_config.get c with
74+
| Some pc ->
75+
begin match
76+
C.Pkg_config.query pc ~package:"libpq"
77+
with
78+
| Some conf ->
79+
let major, minor = major_minor_from_pgconfig () in
80+
{ conf with
81+
C.Pkg_config.cflags = major :: minor :: conf.cflags }
82+
| None -> { C.Pkg_config.cflags = []; libs = [] }
83+
end
84+
| None -> from_pgconfig ()
5685
in
57-
let conf = {
58-
C.Pkg_config.
59-
cflags = [pgsql_includedir; major; minor];
60-
libs = [pgsql_libdir; "-lpq"];
61-
} in
6286
C.Flags.write_sexp "c_flags.sexp" conf.cflags;
63-
C.Flags.write_sexp "c_library_flags.sexp" conf.libs))
87+
C.Flags.write_sexp "c_library_flags.sexp" conf.libs)

0 commit comments

Comments
 (0)