From 1fbeaaf7181c3d2abb5ec24f6d178cedf2c4d7dd Mon Sep 17 00:00:00 2001
From: Pierre Boutillier <pierre.boutillier@laposte.net>
Date: Wed, 18 Sep 2024 12:10:15 +0200
Subject: [PATCH] Configurator: add by default --personality=@target@ to
 pkgconf calls

Specifying a personality is only useful when cross-compiling (which you do when you use mingw on Windows) but
(nearly) never harmful as pkgconf installs a default.personality which it uses for all triplets (that parses
therefore the nearly) it hasn't a explicit personality for.

Signed-off-by: Pierre Boutillier <pierre.boutillier@laposte.net>
---
 otherlibs/configurator/src/v1.ml                  | 15 ++++++++++++---
 .../test/blackbox-tests/pkg-config-args.t/run.t   | 10 +++++++---
 2 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/otherlibs/configurator/src/v1.ml b/otherlibs/configurator/src/v1.ml
index 30abf75660bd..ddd8446fb144 100644
--- a/otherlibs/configurator/src/v1.ml
+++ b/otherlibs/configurator/src/v1.ml
@@ -658,21 +658,30 @@ module Pkg_config = struct
     }
 
   let get c =
-    let pkg_config_args =
+    let get_pkg_config_args default =
       match Sys.getenv "PKG_CONFIG_ARGN" with
       | s -> String.split ~on:' ' s
-      | exception Not_found -> []
+      | exception Not_found -> default
     in
     match Sys.getenv "PKG_CONFIG" with
     | s ->
       Option.map (which c s) ~f:(fun pkg_config ->
+        let pkg_config_args = get_pkg_config_args [] in
         { pkg_config; pkg_config_args; configurator = c })
     | exception Not_found ->
       (match which c "pkgconf" with
        | None ->
          Option.map (which c "pkg-config") ~f:(fun pkg_config ->
+           let pkg_config_args = get_pkg_config_args [] in
            { pkg_config; pkg_config_args; configurator = c })
-       | Some pkg_config -> Some { pkg_config; pkg_config_args; configurator = c })
+       | Some pkg_config ->
+         let pkg_config_args =
+           get_pkg_config_args
+             (match ocaml_config_var c "target" with
+              | None -> []
+              | Some target -> [ "--personality"; target ])
+         in
+         Some { pkg_config; pkg_config_args; configurator = c })
   ;;
 
   type package_conf =
diff --git a/otherlibs/configurator/test/blackbox-tests/pkg-config-args.t/run.t b/otherlibs/configurator/test/blackbox-tests/pkg-config-args.t/run.t
index e4b743559856..e468970b0d4c 100644
--- a/otherlibs/configurator/test/blackbox-tests/pkg-config-args.t/run.t
+++ b/otherlibs/configurator/test/blackbox-tests/pkg-config-args.t/run.t
@@ -1,18 +1,22 @@
 These tests show that setting `PKG_CONFIG_ARGN` passes extra args to `pkg-config`
 
-  $ dune build 2>&1 | awk '/run:.*bin\/pkgconf/{a=1}/stderr/{a=0}a'
+  $ dune build 2>&1 | awk '/run:.*bin\/pkgconf/{a=1}/stderr/{a=0}a' | sed s/$(ocamlc -config | sed -n "/^target:/ {s/target: //; p; }")/\$TARGET/g
   run: $TESTCASE_ROOT/_build/default/.bin/pkgconf --print-errors dummy-pkg
   -> process exited with code 0
   -> stdout:
    | dummy-pkg
-  run: $TESTCASE_ROOT/_build/default/.bin/pkgconf --cflags dummy-pkg
+  run: $TESTCASE_ROOT/_build/default/.bin/pkgconf --personality $TARGET --cflags dummy-pkg
   -> process exited with code 0
   -> stdout:
+   | --personality
+   | $TARGET
    | --cflags
    | dummy-pkg
-  run: $TESTCASE_ROOT/_build/default/.bin/pkgconf --libs dummy-pkg
+  run: $TESTCASE_ROOT/_build/default/.bin/pkgconf --personality $TARGET --libs dummy-pkg
   -> process exited with code 0
   -> stdout:
+   | --personality
+   | $TARGET
    | --libs
    | dummy-pkg