authorgravatar for 38256064+IamSanjid@users.noreply.github.comPurple <38256064+IamSanjid@users.noreply.github.com> 2024-09-01 09:52:31+06:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-11-27 20:49:55+01:00
log7af412b6b442214572559bec86ca1756cda3a2cf
tree8064c601c20a66eef87989535c1cb6576c338306
parente427ba9cd59dc876d27e4a71104b5156460ca618

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 files changed, 35 insertions(+), 3 deletions(-)

lib/std/zig/system/NativePaths.zig+35-3
...@@ -21,9 +21,9 @@ pub fn detect(arena: Allocator, native_target: *const std.Target) !NativePaths {...@@ -21,9 +21,9 @@ pub fn detect(arena: Allocator, native_target: *const std.Target) !NativePaths {
21 var it = mem.tokenizeScalar(u8, nix_cflags_compile, ' ');21 var it = mem.tokenizeScalar(u8, nix_cflags_compile, ' ');
22 while (true) {22 while (true) {
23 const word = it.next() orelse break;23 const word = it.next() orelse break;
24 if (mem.eql(u8, word, "-isystem")) {24 if (mem.eql(u8, word, "-isystem") or mem.eql(u8, word, "-idirafter")) {
25 const include_path = it.next() orelse {25 const include_path = it.next() orelse {
26 try self.addWarning("Expected argument after -isystem in NIX_CFLAGS_COMPILE");26 try self.addWarningFmt("Expected argument after {s} in NIX_CFLAGS_COMPILE", .{word});
27 break;27 break;
28 };28 };
29 try self.addIncludeDir(include_path);29 try self.addIncludeDir(include_path);
...@@ -65,7 +65,7 @@ pub fn detect(arena: Allocator, native_target: *const std.Target) !NativePaths {...@@ -65,7 +65,7 @@ pub fn detect(arena: Allocator, native_target: *const std.Target) !NativePaths {
65 const lib_path = word[2..];65 const lib_path = word[2..];
66 try self.addLibDir(lib_path);66 try self.addLibDir(lib_path);
67 try self.addRPath(lib_path);67 try self.addRPath(lib_path);
68 } else if (mem.startsWith(u8, word, "-l")) {68 } else if (mem.startsWith(u8, word, "-l") or mem.startsWith(u8, word, "-static")) {
69 // Ignore this argument.69 // Ignore this argument.
70 } else {70 } else {
71 try self.addWarningFmt("Unrecognized C flag from NIX_LDFLAGS: {s}", .{word});71 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 {...@@ -77,6 +77,38 @@ pub fn detect(arena: Allocator, native_target: *const std.Target) !NativePaths {
77 error.EnvironmentVariableNotFound => {},77 error.EnvironmentVariableNotFound => {},
78 error.OutOfMemory => |e| return e,78 error.OutOfMemory => |e| return e,
79 }79 }
80 if (process.getEnvVarOwned(arena, "NIX_CFLAGS_LINK")) |nix_cflags_link| {
81 is_nix = true;
82 var it = mem.tokenizeScalar(u8, nix_cflags_link, ' ');
83 while (true) {
84 const word = it.next() orelse break;
85 if (mem.eql(u8, word, "-rpath")) {
86 const rpath = it.next() orelse {
87 try self.addWarning("Expected argument after -rpath in NIX_CFLAGS_LINK");
88 break;
89 };
90 try self.addRPath(rpath);
91 } else if (mem.eql(u8, word, "-L") or mem.eql(u8, word, "-l")) {
92 _ = it.next() orelse {
93 try self.addWarning("Expected argument after -L or -l in NIX_CFLAGS_LINK");
94 break;
95 };
96 } else if (mem.startsWith(u8, word, "-L")) {
97 const lib_path = word[2..];
98 try self.addLibDir(lib_path);
99 try self.addRPath(lib_path);
100 } else if (mem.startsWith(u8, word, "-l") or mem.startsWith(u8, word, "-static")) {
101 // Ignore this argument.
102 } else {
103 try self.addWarningFmt("Unrecognized C flag from NIX_CFLAGS_LINK: {s}", .{word});
104 break;
105 }
106 }
107 } else |err| switch (err) {
108 error.InvalidWtf8 => unreachable,
109 error.EnvironmentVariableNotFound => {},
110 error.OutOfMemory => |e| return e,
111 }
80 if (is_nix) {112 if (is_nix) {
81 return self;113 return self;
82 }114 }