Skip to content

Commit c5d6270

Browse files
committed
discover: read $PKG_CONFIG to fix cross-compilation
1 parent e828f1c commit c5d6270

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

src/config/discover.ml

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,34 @@ let major_minor_from_pgconfig () =
5555
in
5656
Fun.protect ~finally:(fun () -> close_in ic) (fun () -> pg_major_minor ic)
5757

58-
let major_minor_from_pkg_config () =
59-
let ic = Unix.open_process_in "pkg-config --modversion libpq" in
60-
Fun.protect ~finally:(fun () -> close_in ic) @@ fun () ->
61-
let version_line = input_line ic in
62-
(* Typically something like "14.1" *)
63-
match String.split_on_char '.' version_line with
64-
| major :: minor :: _ ->
65-
("-DPG_OCAML_MAJOR_VERSION=" ^ major, "-DPG_OCAML_MINOR_VERSION=" ^ minor)
66-
| _ ->
67-
eprintf "Unable to parse libpq version: %s" version_line;
68-
exit 1
58+
let major_minor_from_pkg_config =
59+
let pkg_config_prefix () =
60+
let pkg_config_args =
61+
match Sys.getenv "PKG_CONFIG_ARGN" with
62+
| s -> String.split_on_char ' ' s
63+
| exception Not_found -> []
64+
in
65+
let bin =
66+
match Sys.getenv "PKG_CONFIG" with
67+
| s -> s
68+
| exception Not_found -> "pkg-config"
69+
in
70+
Format.asprintf "%s %s " bin (String.concat " " pkg_config_args)
71+
in
72+
fun () ->
73+
let ic =
74+
Unix.open_process_in (pkg_config_prefix () ^ " --modversion libpq")
75+
in
76+
Fun.protect ~finally:(fun () -> close_in ic) @@ fun () ->
77+
let version_line = input_line ic in
78+
(* Typically something like "14.1" *)
79+
match String.split_on_char '.' version_line with
80+
| major :: minor :: _ ->
81+
( "-DPG_OCAML_MAJOR_VERSION=" ^ major,
82+
"-DPG_OCAML_MINOR_VERSION=" ^ minor )
83+
| _ ->
84+
eprintf "Unable to parse libpq version: %s" version_line;
85+
exit 1
6986

7087
let from_pgconfig () =
7188
let cmd = "pg_config --includedir --libdir --version" in

0 commit comments

Comments
 (0)