From abe07e601e6d957197124a6819cee0bf51de5f07 Mon Sep 17 00:00:00 2001 From: Lee Cannon Date: Wed, 15 Feb 2023 10:36:06 -0800 Subject: [PATCH] Fix build runner for latest changes (#1005) * build_runner: update to use new caching system * build_runner: switch ci to use 0.10.1 instead of 0.10.0 * build_runner: support older versions * build_runner: ensure depreciated functions and types are handled when they are removed * build_runner: prevent access denied on global cache --- .github/workflows/build_runner.yml | 2 +- src/special/build_runner.zig | 99 +++++++++++++++++++++++------- 2 files changed, 79 insertions(+), 22 deletions(-) diff --git a/.github/workflows/build_runner.yml b/.github/workflows/build_runner.yml index 4ff8de5..aced4f9 100644 --- a/.github/workflows/build_runner.yml +++ b/.github/workflows/build_runner.yml @@ -15,7 +15,7 @@ jobs: check_build_runner: strategy: matrix: - zig_version: [0.9.1, 0.10.0, master] + zig_version: [0.9.1, 0.10.1, master] runs-on: ubuntu-latest diff --git a/src/special/build_runner.zig b/src/special/build_runner.zig index 2b56d9b..b6b79a1 100644 --- a/src/special/build_runner.zig +++ b/src/special/build_runner.zig @@ -2,10 +2,22 @@ const root = @import("@build@"); const std = @import("std"); const log = std.log; const process = std.process; -const Builder = std.build.Builder; -const InstallArtifactStep = std.build.InstallArtifactStep; -const LibExeObjStep = std.build.LibExeObjStep; -const OptionsStep = std.build.OptionsStep; + +// Zig 0.11.0-dev.1524+ +const Build = if (@hasDecl(std, "Build")) std.Build else std.build; + +// Zig 0.11.0-dev.1524+ +const Builder = if (@hasDecl(std, "Build")) std.Build else std.build.Builder; + +// Zig 0.11.0-dev.1637+ +const Cache = if (@hasDecl(Build, "Cache")) std.Build.Cache else void; +const has_cache = Cache != void; + +// Zig 0.11.0-dev.1524+ +const CompileStep = if (@hasDecl(Build, "CompileStep")) Build.CompileStep else Build.LibExeObjStep; + +const InstallArtifactStep = Build.InstallArtifactStep; +const OptionsStep = Build.OptionsStep; pub const BuildConfig = struct { packages: []Pkg, @@ -48,19 +60,64 @@ pub fn main() !void { return error.InvalidArgs; }; + const build_root_directory = if (has_cache) Cache.Directory{ + .path = build_root, + .handle = try std.fs.cwd().openDir(build_root, .{}), + } else build_root; + + const local_cache_directory = if (has_cache) Cache.Directory{ + .path = cache_root, + .handle = try std.fs.cwd().makeOpenPath(cache_root, .{}), + } else cache_root; + + const global_cache_directory = if (has_cache) Cache.Directory{ + .path = global_cache_root, + // I don't know if with the incomming changes around caching whether `openDir` is correct, + // but `makeOpenPath` which zig build runner uses causes access denied + .handle = try std.fs.cwd().openDir(global_cache_root, .{}), + } else global_cache_root; + + var cache = if (has_cache) Cache{ + .gpa = allocator, + .manifest_dir = try local_cache_directory.handle.makeOpenPath("h", .{}), + } else {}; + if (has_cache) { + cache.addPrefix(.{ .path = null, .handle = std.fs.cwd() }); + cache.addPrefix(build_root_directory); + cache.addPrefix(local_cache_directory); + cache.addPrefix(global_cache_directory); + } + const builder = blk: { // Zig 0.11.0-dev.1524+ - if (@hasDecl(std, "Build")) { - const host = try std.zig.system.NativeTargetInfo.detect(.{}); + const does_builder_need_host = @hasDecl(std, "Build"); + + const host = if (does_builder_need_host) try std.zig.system.NativeTargetInfo.detect(.{}) else {}; + + if (does_builder_need_host) { + if (has_cache) { + break :blk try Builder.create( + allocator, + zig_exe, + build_root_directory, + local_cache_directory, + global_cache_directory, + host, + &cache, + ); + } + break :blk try Builder.create( allocator, zig_exe, - build_root, - cache_root, - global_cache_root, + build_root_directory, + local_cache_directory, + global_cache_directory, host, ); - } else break :blk try Builder.create( + } + + break :blk try Builder.create( allocator, zig_exe, build_root, @@ -131,7 +188,7 @@ pub fn main() !void { ); } -fn reifyOptions(step: *std.build.Step) anyerror!void { +fn reifyOptions(step: *Build.Step) anyerror!void { // Support Zig 0.9.1 if (!@hasDecl(OptionsStep, "base_id")) return; @@ -151,7 +208,7 @@ fn processStep( allocator: std.mem.Allocator, packages: *std.ArrayListUnmanaged(BuildConfig.Pkg), include_dirs: *std.StringArrayHashMapUnmanaged(void), - step: *std.build.Step, + step: *Build.Step, ) anyerror!void { if (step.cast(InstallArtifactStep)) |install_exe| { if (install_exe.artifact.root_src) |src| { @@ -164,7 +221,7 @@ fn processStep( try processIncludeDirs(allocator, include_dirs, install_exe.artifact.include_dirs.items); try processPkgConfig(allocator, include_dirs, install_exe.artifact); - if (@hasField(LibExeObjStep, "modules")) { + if (@hasField(CompileStep, "modules")) { var modules_it = install_exe.artifact.modules.iterator(); while (modules_it.next()) |module_entry| { try processModule(allocator, packages, module_entry); @@ -174,7 +231,7 @@ fn processStep( try processPackage(allocator, packages, pkg); } } - } else if (step.cast(LibExeObjStep)) |exe| { + } else if (step.cast(CompileStep)) |exe| { if (exe.root_src) |src| { const maybe_path = switch (src) { .path => |path| path, @@ -184,7 +241,7 @@ fn processStep( } try processIncludeDirs(allocator, include_dirs, exe.include_dirs.items); try processPkgConfig(allocator, include_dirs, exe); - if (@hasField(LibExeObjStep, "modules")) { + if (@hasField(CompileStep, "modules")) { var modules_it = exe.modules.iterator(); while (modules_it.next()) |module_entry| { try processModule(allocator, packages, module_entry); @@ -204,7 +261,7 @@ fn processStep( fn processModule( allocator: std.mem.Allocator, packages: *std.ArrayListUnmanaged(BuildConfig.Pkg), - module: std.StringArrayHashMap(*std.Build.Module).Entry, + module: std.StringArrayHashMap(*Build.Module).Entry, ) !void { for (packages.items) |package| { if (std.mem.eql(u8, package.name, module.key_ptr.*)) return; @@ -228,14 +285,14 @@ fn processModule( fn processPackage( allocator: std.mem.Allocator, packages: *std.ArrayListUnmanaged(BuildConfig.Pkg), - pkg: std.build.Pkg, + pkg: Build.Pkg, ) anyerror!void { for (packages.items) |package| { if (std.mem.eql(u8, package.name, pkg.name)) return; } // Support Zig 0.9.1 - const source = if (@hasField(std.build.Pkg, "source")) pkg.source else pkg.path; + const source = if (@hasField(Build.Pkg, "source")) pkg.source else pkg.path; const maybe_path = switch (source) { .path => |path| path, @@ -256,7 +313,7 @@ fn processPackage( fn processIncludeDirs( allocator: std.mem.Allocator, include_dirs: *std.StringArrayHashMapUnmanaged(void), - dirs: []std.build.LibExeObjStep.IncludeDir, + dirs: []CompileStep.IncludeDir, ) !void { try include_dirs.ensureUnusedCapacity(allocator, dirs.len); @@ -274,7 +331,7 @@ fn processIncludeDirs( fn processPkgConfig( allocator: std.mem.Allocator, include_dirs: *std.StringArrayHashMapUnmanaged(void), - exe: *std.build.LibExeObjStep, + exe: *CompileStep, ) !void { for (exe.link_objects.items) |link_object| { if (link_object != .system_lib) continue; @@ -308,7 +365,7 @@ fn processPkgConfig( fn getPkgConfigIncludes( allocator: std.mem.Allocator, include_dirs: *std.StringArrayHashMapUnmanaged(void), - exe: *std.build.LibExeObjStep, + exe: *CompileStep, name: []const u8, ) !void { if (exe.runPkgConfig(name)) |args| {