From 80c90add63c44f2e06c73c13400d044a81078602 Mon Sep 17 00:00:00 2001
From: Eric Joldasov <bratishkaerik@landless-city.net>
Date: Wed, 8 May 2024 23:21:34 +0500
Subject: [PATCH 1/5] zig build: respect `PKG_CONFIG` environment variable

[Upstream commit: d263f1ec0eb988f0e4ed1859351f5040f590996b]

`PKG_CONFIG` environment variable is used to override path to
pkg-config executable, for example when it's name is prepended by
target triple for cross-compilation purposes:

```
PKG_CONFIG=/usr/bin/aarch64-unknown-linux-gnu-pkgconf zig build
```

Signed-off-by: Eric Joldasov <bratishkaerik@landless-city.net>
---
 lib/std/Build/Step/Compile.zig | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig
index 5bf0805e79..58ff3a0df6 100644
--- a/lib/std/Build/Step/Compile.zig
+++ b/lib/std/Build/Step/Compile.zig
@@ -703,8 +703,9 @@ fn runPkgConfig(self: *Compile, lib_name: []const u8) !PkgConfigResult {
     };
 
     var code: u8 = undefined;
+    const pkg_config_exe = b.graph.env_map.get("PKG_CONFIG") orelse "pkg-config";
     const stdout = if (b.runAllowFail(&[_][]const u8{
-        "pkg-config",
+        pkg_config_exe,
         pkg_name,
         "--cflags",
         "--libs",
@@ -1824,7 +1825,8 @@ pub fn doAtomicSymLinks(
 }
 
 fn execPkgConfigList(self: *std.Build, out_code: *u8) (PkgConfigError || RunError)![]const PkgConfigPkg {
-    const stdout = try self.runAllowFail(&[_][]const u8{ "pkg-config", "--list-all" }, out_code, .Ignore);
+    const pkg_config_exe = self.graph.env_map.get("PKG_CONFIG") orelse "pkg-config";
+    const stdout = try self.runAllowFail(&[_][]const u8{ pkg_config_exe, "--list-all" }, out_code, .Ignore);
     var list = ArrayList(PkgConfigPkg).init(self.allocator);
     errdefer list.deinit();
     var line_it = mem.tokenizeAny(u8, stdout, "\r\n");
-- 
2.46.0

