# zig-0.13.0-search-prefix-new-sub-dirs.patch -rw-r--r-- 3.3 KiB View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Moved here from old version of https://github.com/gentoo/gentoo/pull/37283 .

From: Eric Joldasov <bratishkaerik@landless-city.net>

Patch for the purposes of cross-compiling support in "zig-build".

This does not handle a problem with not separating different search prefixes for binaries and
for libraries, since solving this will require adding a separate flag which is too incompatible
with 9999 version, which I also want to support as much as possible in zig-build.

Add "lib64" and "lib32" directories (depends on "usize" bitsize on system) before "lib"
when using "--search-prefix" so that libraries from lib64/lib32 are not overwritten by
libraries from "lib" which can have different bitwidth.

Ideally it should be unified with logic in std.zig.NativePaths (and maybe aro.toolchains.Linux?),
and have mentioned different search prefixes, so upstream patch will be very different.

diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig
index d18d8de413..ff69585c69 100644
--- a/lib/std/Build/Step/Compile.zig
+++ b/lib/std/Build/Step/Compile.zig
@@ -1595,17 +1595,6 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {
         // This prevents a warning, that should probably be upgraded to an error in Zig's
         // CLI parsing code, when the linker sees an -L directory that does not exist.
 
-        if (prefix_dir.accessZ("lib", .{})) |_| {
-            try zig_args.appendSlice(&.{
-                "-L", b.pathJoin(&.{ search_prefix, "lib" }),
-            });
-        } else |err| switch (err) {
-            error.FileNotFound => {},
-            else => |e| return step.fail("unable to access '{s}/lib' directory: {s}", .{
-                search_prefix, @errorName(e),
-            }),
-        }
-
         if (prefix_dir.accessZ("include", .{})) |_| {
             try zig_args.appendSlice(&.{
                 "-I", b.pathJoin(&.{ search_prefix, "include" }),
@@ -1616,6 +1605,36 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {
                 search_prefix, @errorName(e),
             }),
         }
+
+        blk: {
+            const lib_arch_dir = switch (compile.rootModuleTarget().ptrBitWidth()) {
+                64 => "lib64",
+                32 => "lib32",
+                // Can't find info on whether "lib16" or "lib128" exist, but for Gentoo this should be enough.
+                else => break :blk,
+            };
+            if (prefix_dir.accessZ(lib_arch_dir, .{})) |_| {
+                try zig_args.appendSlice(&.{
+                    "-L", b.pathJoin(&.{ search_prefix, lib_arch_dir }),
+                });
+            } else |err| switch (err) {
+                error.FileNotFound => break :blk,
+                else => |e| return step.fail("unable to access '{s}/{s}' directory: {s}", .{
+                    lib_arch_dir, search_prefix, @errorName(e),
+                }),
+            }
+        }
+
+        if (prefix_dir.accessZ("lib", .{})) |_| {
+            try zig_args.appendSlice(&.{
+                "-L", b.pathJoin(&.{ search_prefix, "lib" }),
+            });
+        } else |err| switch (err) {
+            error.FileNotFound => {},
+            else => |e| return step.fail("unable to access '{s}/lib' directory: {s}", .{
+                search_prefix, @errorName(e),
+            }),
+        }
     }
 
     if (compile.rc_includes != .any) {