@@ -8,6 +8,11 @@ const BuildOptions = struct {
88 optimize : std.builtin.OptimizeMode ,
99 target : Build.ResolvedTarget ,
1010 library_type : LibraryType ,
11+ // There are some issues with path lookups when using --sysroot. Pass as a build option instead.
12+ sysroot : ? []const u8 ,
13+ // When building static libraries for Windows, zig's compiler-rt needs to be bundled.
14+ // For some reason, setting "bundle_compiler_rt" to true doesn't produce a static library that works with NativeAOT.
15+ // As a work-around, we manually build the compiler_rt.zig file.
1116 compiler_rt_path : ? []const u8 ,
1217};
1318
@@ -75,45 +80,38 @@ pub fn compile(b: *Build, options: BuildOptions) !void {
7580 }
7681 },
7782 .ios = > {
78- if (b .sysroot == null or b .sysroot .? .len == 0 ) {
83+ if (options .sysroot == null or options .sysroot .? .len == 0 ) {
7984 @panic ("A --sysroot path to an IOS SDK needs to be provided when compiling for IOS." );
8085 }
8186
82- lib .addSystemFrameworkPath (.{ .cwd_relative = b .pathJoin (&.{ b .sysroot .? , "/System/Library/Frameworks" }) });
83- lib .addSystemIncludePath (.{ .cwd_relative = b .pathJoin (&.{ b .sysroot .? , "/usr/include" }) });
84- lib .addLibraryPath (.{ .cwd_relative = "/usr/lib" });
87+ lib .addSystemFrameworkPath (.{ .cwd_relative = b .pathJoin (&.{ options .sysroot .? , "/System/Library/Frameworks" }) });
88+ lib .addSystemIncludePath (.{ .cwd_relative = b .pathJoin (&.{ options .sysroot .? , "/usr/include" }) });
89+ lib .addLibraryPath (.{ .cwd_relative = b . pathJoin (&.{ options . sysroot .? , "/usr/lib" }) });
8590 },
8691 .emscripten = > {
87- if (b .sysroot == null or b .sysroot .? .len == 0 ) {
92+ if (options .sysroot == null or options .sysroot .? .len == 0 ) {
8893 @panic ("A --sysroot path to an emscripten cache needs to be provided when compiling for wasm." );
8994 }
9095
91- lib .addSystemIncludePath (.{ .cwd_relative = b .pathJoin (&.{ b .sysroot .? , "/sysroot /include" }) });
96+ lib .addSystemIncludePath (.{ .cwd_relative = b .pathJoin (&.{ options .sysroot .? , "/include" }) });
9297 },
9398 .linux = > {
9499 if (options .target .result .abi == .android ) {
95- if (b .sysroot == null or b .sysroot .? .len == 0 ) {
100+ if (options .sysroot == null or options .sysroot .? .len == 0 ) {
96101 @panic ("A --sysroot path to an Android NDK needs to be provided when compiling for Android." );
97102 }
98103
99- const host_tuple = switch (builtin .target .os .tag ) {
100- .linux = > "linux-x86_64" ,
101- .windows = > "windows-x86_64" ,
102- .macos = > "darwin-x86_64" ,
103- else = > @panic ("unsupported host OS" ),
104- };
105-
106104 const triple = switch (options .target .result .cpu .arch ) {
107105 .aarch64 = > "aarch64-linux-android" ,
108106 .x86_64 = > "x86_64-linux-android" ,
109107 else = > @panic ("Unsupported Android architecture" ),
110108 };
109+
111110 const android_api_level : []const u8 = "21" ;
112111
113- const android_sysroot = b .pathJoin (&.{ b .sysroot .? , "/toolchains/llvm/prebuilt/" , host_tuple , "/sysroot" });
114- const android_lib_path = b .pathJoin (&.{ "/usr/lib/" , triple , android_api_level });
115- const android_include_path = b .pathJoin (&.{ android_sysroot , "/usr/include" });
116- const android_system_include_path = b .pathJoin (&.{ android_sysroot , "/usr/include/" , triple });
112+ const android_lib_path = b .pathJoin (&.{ options .sysroot .? , "/usr/lib/" , triple , android_api_level });
113+ const android_include_path = b .pathJoin (&.{ options .sysroot .? , "/usr/include" });
114+ const android_system_include_path = b .pathJoin (&.{ options .sysroot .? , "/usr/include/" , triple });
117115
118116 lib .addLibraryPath (.{ .cwd_relative = android_lib_path });
119117
@@ -145,9 +143,7 @@ pub fn build(b: *Build) !void {
145143 .optimize = b .standardOptimizeOption (.{}),
146144 .target = b .standardTargetOptions (.{}),
147145 .library_type = b .option (LibraryType , "library-type" , "Compile as a static or shared library." ) orelse LibraryType .Shared ,
148- // When building static libraries for Windows, zig's compiler-rt needs to be bundled.
149- // For some reason, setting "bundle_compiler_rt" to true doesn't produce a static library that works with NativeAOT.
150- // As a work-around, we manually build the compiler_rt.zig file.
146+ .sysroot = b .option ([]const u8 , "sysroot" , "Path to sysroot." ) orelse null ,
151147 .compiler_rt_path = b .option ([]const u8 , "compiler-rt-path" , "Path to the compiler_rt file." ) orelse null ,
152148 });
153149}
0 commit comments