Commit 7af412b6b4

Purple <38256064+IamSanjid@users.noreply.github.com>
2024-09-01 05:52:31
Update std.zig.system.NativePaths.detect to support some more flags on NixOS like environment.
Basically detect `-idirafter` flag in `NIX_CFLAGS_COMPILE` and treat it like `-isystem`, also detect `NIX_CFLAGS_LINK` environment variable and treat it like the `NIX_LDFLAGS` . Reference: https://github.com/NixOS/nixpkgs/blob/74eefb4210ef545cebd6a7a25356734b0b49854f/pkgs/build-support/build-fhsenv-chroot/env.nix#L83
1 parent e427ba9
Changed files (1)
lib
std
zig
lib/std/zig/system/NativePaths.zig
@@ -21,9 +21,9 @@ pub fn detect(arena: Allocator, native_target: *const std.Target) !NativePaths {
         var it = mem.tokenizeScalar(u8, nix_cflags_compile, ' ');
         while (true) {
             const word = it.next() orelse break;
-            if (mem.eql(u8, word, "-isystem")) {
+            if (mem.eql(u8, word, "-isystem") or mem.eql(u8, word, "-idirafter")) {
                 const include_path = it.next() orelse {
-                    try self.addWarning("Expected argument after -isystem in NIX_CFLAGS_COMPILE");
+                    try self.addWarningFmt("Expected argument after {s} in NIX_CFLAGS_COMPILE", .{word});
                     break;
                 };
                 try self.addIncludeDir(include_path);
@@ -65,7 +65,7 @@ pub fn detect(arena: Allocator, native_target: *const std.Target) !NativePaths {
                 const lib_path = word[2..];
                 try self.addLibDir(lib_path);
                 try self.addRPath(lib_path);
-            } else if (mem.startsWith(u8, word, "-l")) {
+            } else if (mem.startsWith(u8, word, "-l") or mem.startsWith(u8, word, "-static")) {
                 // Ignore this argument.
             } else {
                 try self.addWarningFmt("Unrecognized C flag from NIX_LDFLAGS: {s}", .{word});
@@ -77,6 +77,38 @@ pub fn detect(arena: Allocator, native_target: *const std.Target) !NativePaths {
         error.EnvironmentVariableNotFound => {},
         error.OutOfMemory => |e| return e,
     }
+    if (process.getEnvVarOwned(arena, "NIX_CFLAGS_LINK")) |nix_cflags_link| {
+        is_nix = true;
+        var it = mem.tokenizeScalar(u8, nix_cflags_link, ' ');
+        while (true) {
+            const word = it.next() orelse break;
+            if (mem.eql(u8, word, "-rpath")) {
+                const rpath = it.next() orelse {
+                    try self.addWarning("Expected argument after -rpath in NIX_CFLAGS_LINK");
+                    break;
+                };
+                try self.addRPath(rpath);
+            } else if (mem.eql(u8, word, "-L") or mem.eql(u8, word, "-l")) {
+                _ = it.next() orelse {
+                    try self.addWarning("Expected argument after -L or -l in NIX_CFLAGS_LINK");
+                    break;
+                };
+            } else if (mem.startsWith(u8, word, "-L")) {
+                const lib_path = word[2..];
+                try self.addLibDir(lib_path);
+                try self.addRPath(lib_path);
+            } else if (mem.startsWith(u8, word, "-l") or mem.startsWith(u8, word, "-static")) {
+                // Ignore this argument.
+            } else {
+                try self.addWarningFmt("Unrecognized C flag from NIX_CFLAGS_LINK: {s}", .{word});
+                break;
+            }
+        }
+    } else |err| switch (err) {
+        error.InvalidWtf8 => unreachable,
+        error.EnvironmentVariableNotFound => {},
+        error.OutOfMemory => |e| return e,
+    }
     if (is_nix) {
         return self;
     }