From c5d6270b8e4bb5d1ffca2484f85b33488a2d9e5a Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Sun, 8 Dec 2024 15:48:09 -0800 Subject: [PATCH] discover: read `$PKG_CONFIG` to fix cross-compilation --- src/config/discover.ml | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/src/config/discover.ml b/src/config/discover.ml index 4359edd..ccc8333 100644 --- a/src/config/discover.ml +++ b/src/config/discover.ml @@ -55,17 +55,34 @@ let major_minor_from_pgconfig () = in Fun.protect ~finally:(fun () -> close_in ic) (fun () -> pg_major_minor ic) -let major_minor_from_pkg_config () = - let ic = Unix.open_process_in "pkg-config --modversion libpq" in - Fun.protect ~finally:(fun () -> close_in ic) @@ fun () -> - let version_line = input_line ic in - (* Typically something like "14.1" *) - match String.split_on_char '.' version_line with - | major :: minor :: _ -> - ("-DPG_OCAML_MAJOR_VERSION=" ^ major, "-DPG_OCAML_MINOR_VERSION=" ^ minor) - | _ -> - eprintf "Unable to parse libpq version: %s" version_line; - exit 1 +let major_minor_from_pkg_config = + let pkg_config_prefix () = + let pkg_config_args = + match Sys.getenv "PKG_CONFIG_ARGN" with + | s -> String.split_on_char ' ' s + | exception Not_found -> [] + in + let bin = + match Sys.getenv "PKG_CONFIG" with + | s -> s + | exception Not_found -> "pkg-config" + in + Format.asprintf "%s %s " bin (String.concat " " pkg_config_args) + in + fun () -> + let ic = + Unix.open_process_in (pkg_config_prefix () ^ " --modversion libpq") + in + Fun.protect ~finally:(fun () -> close_in ic) @@ fun () -> + let version_line = input_line ic in + (* Typically something like "14.1" *) + match String.split_on_char '.' version_line with + | major :: minor :: _ -> + ( "-DPG_OCAML_MAJOR_VERSION=" ^ major, + "-DPG_OCAML_MINOR_VERSION=" ^ minor ) + | _ -> + eprintf "Unable to parse libpq version: %s" version_line; + exit 1 let from_pgconfig () = let cmd = "pg_config --includedir --libdir --version" in