Skip to content

Commit 819d26d

Browse files
committed
use builtin.zig instead of adding a new buildpkgs.zig
1 parent c3dc5ed commit 819d26d

File tree

8 files changed

+177
-154
lines changed

8 files changed

+177
-154
lines changed

src/Compilation.zig

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ emit_llvm_ir: ?EmitLoc,
137137
emit_analysis: ?EmitLoc,
138138
emit_docs: ?EmitLoc,
139139

140+
// true to generate the pkg_names array and hasPkg function in builtin.zig
141+
// should only be true when compiling build.zig
142+
enable_builtin_pkg_names: bool,
143+
140144
work_queue_wait_group: WaitGroup,
141145

142146
pub const InnerError = Module.InnerError;
@@ -503,6 +507,7 @@ pub const InitOptions = struct {
503507
test_filter: ?[]const u8 = null,
504508
test_name_prefix: ?[]const u8 = null,
505509
subsystem: ?std.Target.SubSystem = null,
510+
enable_builtin_pkg_names: ?bool = null,
506511
};
507512

508513
fn addPackageTableToCacheHash(
@@ -1120,6 +1125,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
11201125
.test_evented_io = options.test_evented_io,
11211126
.debug_compiler_runtime_libs = options.debug_compiler_runtime_libs,
11221127
.work_queue_wait_group = undefined,
1128+
.enable_builtin_pkg_names = options.enable_builtin_pkg_names orelse false,
11231129
};
11241130
break :comp comp;
11251131
};
@@ -2785,7 +2791,7 @@ fn updateBuiltinZigFile(comp: *Compilation, mod: *Module) !void {
27852791
const tracy = trace(@src());
27862792
defer tracy.end();
27872793

2788-
const source = try comp.generateBuiltinZigSource(comp.gpa);
2794+
const source = try comp.generateBuiltinZigSource(comp.gpa, mod);
27892795
defer comp.gpa.free(source);
27902796
try mod.zig_cache_artifact_directory.handle.writeFile("builtin.zig", source);
27912797
}
@@ -2797,7 +2803,7 @@ pub fn dump_argv(argv: []const []const u8) void {
27972803
std.debug.print("{s}\n", .{argv[argv.len - 1]});
27982804
}
27992805

2800-
pub fn generateBuiltinZigSource(comp: *Compilation, allocator: *Allocator) ![]u8 {
2806+
pub fn generateBuiltinZigSource(comp: *Compilation, allocator: *Allocator, optional_mod: ?*Module) ![]u8 {
28012807
const tracy = trace(@src());
28022808
defer tracy.end();
28032809

@@ -2987,6 +2993,59 @@ pub fn generateBuiltinZigSource(comp: *Compilation, allocator: *Allocator) ![]u8
29872993
}
29882994
}
29892995

2996+
if (comp.enable_builtin_pkg_names) {
2997+
try buffer.writer().writeAll(
2998+
\\
2999+
\\pub const pkg_names = &[_][]const u8 {
3000+
\\
3001+
);
3002+
if (optional_mod) |mod| {
3003+
{
3004+
var pkg_it = mod.root_pkg.table.iterator();
3005+
while (pkg_it.next()) |pkg| {
3006+
try buffer.writer().print(" \"{s}\",\n", .{pkg.key});
3007+
}
3008+
}
3009+
if (mod.root_pkg.table.get("@build")) |build_pkg| {
3010+
var pkg_it = build_pkg.table.iterator();
3011+
while (pkg_it.next()) |pkg| {
3012+
try buffer.writer().print(" \"{s}\",\n", .{pkg.key});
3013+
}
3014+
}
3015+
}
3016+
try buffer.writer().writeAll(
3017+
\\};
3018+
\\
3019+
\\// using .Inline to force this to be comptime
3020+
\\pub fn hasPkg(comptime name: []const u8) callconv(.Inline) bool {
3021+
\\ if (!isComptime()) {
3022+
\\ @compileError("builtin.hasPkg MUST be called with comptime");
3023+
\\ }
3024+
\\ inline for (pkg_names) |has_name| {
3025+
\\ if (@import("std").mem.eql(u8, name, has_name)) return true;
3026+
\\ }
3027+
\\ return false;
3028+
\\}
3029+
\\
3030+
\\// A temporary workaround until https://github.com/ziglang/zig/issues/425
3031+
\\// is implemented and the callconv(.Inline) on the `hasPkg` function forces
3032+
\\// it to be comptime
3033+
\\fn isComptime() bool {
3034+
\\ var t: bool = true;
3035+
\\ const x = if (t) @as(u7, 0) else @as(u8, 0);
3036+
\\ return @TypeOf(x) == u7;
3037+
\\}
3038+
\\
3039+
);
3040+
} else {
3041+
try buffer.writer().writeAll(
3042+
\\
3043+
\\pub const pkg_names = @compileError("builtin.pkg_names is only available in build.zig");
3044+
\\pub const hasPkg = @compileError("builtin.hasPkg is only available in build.zig");
3045+
\\
3046+
);
3047+
}
3048+
29903049
return buffer.toOwnedSlice();
29913050
}
29923051

src/main.zig

Lines changed: 2 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1900,7 +1900,7 @@ fn buildOutputType(
19001900
defer if (!comp_destroyed) comp.destroy();
19011901

19021902
if (show_builtin) {
1903-
return std.io.getStdOut().writeAll(try comp.generateBuiltinZigSource(arena));
1903+
return std.io.getStdOut().writeAll(try comp.generateBuiltinZigSource(arena, null));
19041904
}
19051905
if (arg_mode == .translate_c) {
19061906
return cmdTranslateC(comp, arena, have_enable_cache);
@@ -2565,14 +2565,6 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
25652565

25662566
gimmeMoreOfThoseSweetSweetFileDescriptors();
25672567

2568-
var buildpkgs_dir = try generateBuildPkgs(arena, local_cache_directory, build_pkg.table);
2569-
defer buildpkgs_dir.handle.close();
2570-
var buildpkgs_pkg: Package = .{
2571-
.root_src_directory = buildpkgs_dir,
2572-
.root_src_path = "buildpkgs.zig",
2573-
};
2574-
try build_pkg.table.put(arena, "buildpkgs", &buildpkgs_pkg);
2575-
25762568
const cross_target: std.zig.CrossTarget = .{};
25772569
const target_info = try detectNativeTargetInfo(gpa, cross_target);
25782570

@@ -2604,6 +2596,7 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
26042596
.optimize_mode = .Debug,
26052597
.self_exe_path = self_exe_path,
26062598
.thread_pool = &thread_pool,
2599+
.enable_builtin_pkg_names = true,
26072600
}) catch |err| {
26082601
fatal("unable to create compilation: {s}", .{@errorName(err)});
26092602
};
@@ -2640,76 +2633,6 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
26402633
}
26412634
}
26422635

2643-
fn generateBuildPkgs(arena: *Allocator, local_cache_directory: Compilation.Directory, pkg_table: Package.Table) !Compilation.Directory {
2644-
const src = blk: {
2645-
var src_array = std.ArrayList(u8).init(arena);
2646-
defer src_array.deinit();
2647-
const writer = src_array.writer();
2648-
try writer.writeAll(
2649-
\\pub const names = &[_][]const u8 {
2650-
\\
2651-
);
2652-
var pkg_it = pkg_table.iterator();
2653-
while (pkg_it.next()) |pkg| {
2654-
try writer.print(" \"{s}\",\n", .{pkg.key});
2655-
}
2656-
try writer.writeAll(
2657-
\\};
2658-
\\
2659-
\\// using .Inline to force this to be comptime
2660-
\\pub fn has(comptime name: []const u8) callconv(.Inline) bool {
2661-
\\ if (!isComptime()) {
2662-
\\ @compileError("buildpkgs.has must be called with comptime");
2663-
\\ }
2664-
\\ inline for (names) |has_name| {
2665-
\\ if (@import("std").mem.eql(u8, name, has_name)) return true;
2666-
\\ }
2667-
\\ return false;
2668-
\\}
2669-
\\
2670-
\\// A temporary workaround until https://github.com/ziglang/zig/issues/425
2671-
\\// is implemented and the callconv(.Inline) on the `has` function forces
2672-
\\// it to be comptime
2673-
\\fn isComptime() bool {
2674-
\\ var t: bool = true;
2675-
\\ const x = if (t) @as(u7, 0) else @as(u8, 0);
2676-
\\ return @TypeOf(x) == u7;
2677-
\\}
2678-
\\
2679-
);
2680-
break :blk src_array.toOwnedSlice();
2681-
};
2682-
defer arena.free(src);
2683-
2684-
var zig_cache_tmp_dir = try local_cache_directory.handle.makeOpenPath("tmp", .{});
2685-
defer zig_cache_tmp_dir.close();
2686-
var zig_cache_tmp_buildpkgs_dir = try zig_cache_tmp_dir.makeOpenPath("buildpkgs", .{});
2687-
defer zig_cache_tmp_buildpkgs_dir.close();
2688-
2689-
const hex_digest = blk: {
2690-
var hash = Cache.HashHelper { };
2691-
hash.addBytes(src);
2692-
break :blk hash.final();
2693-
};
2694-
const pkg_dir_name: []const u8 = &hex_digest;
2695-
2696-
var buildpkgs_dir = try zig_cache_tmp_buildpkgs_dir.makeOpenPath(pkg_dir_name, .{});
2697-
errdefer buildpkgs_dir.close();
2698-
2699-
if (buildpkgs_dir.access("buildpkgs.zig", .{ .read = true })) { } else |_| {
2700-
{
2701-
const buildpkgs = try buildpkgs_dir.createFile("buildpkgs.zig.tmp", .{});
2702-
defer buildpkgs.close();
2703-
try buildpkgs.writer().writeAll(src);
2704-
}
2705-
try buildpkgs_dir.rename("buildpkgs.zig.tmp", "buildpkgs.zig");
2706-
}
2707-
return Compilation.Directory {
2708-
.path = try local_cache_directory.join(arena, &[_][]const u8 { "tmp", "buildpkgs", pkg_dir_name }),
2709-
.handle = buildpkgs_dir,
2710-
};
2711-
}
2712-
27132636
fn argvCmd(allocator: *Allocator, argv: []const []const u8) ![]u8 {
27142637
var cmd = std.ArrayList(u8).init(allocator);
27152638
defer cmd.deinit();
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const std = @import("std");
2+
const Builder = std.build.Builder;
3+
4+
pub fn build(b: *Builder) !void {
5+
if (comptime @import("std").builtin.hasPkg("androidbuild")) {
6+
std.debug.print("we have the 'androidbuild' package\n", .{});
7+
const androidbuild = @import("androidbuild");
8+
androidbuild.makeApk(b);
9+
} else {
10+
std.debug.print("missing package 'androidbuild'\n", .{});
11+
}
12+
}
Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,64 @@
11
const Builder = @import("std").build.Builder;
2-
const buildpkgs = @import("buildpkgs");
32

43
pub fn build(b: *Builder) void {
5-
if (comptime buildpkgs.has("androidbuild")) {
6-
const androidbuild = @import("androidbuild");
7-
androidbuild.makeApk(b);
8-
} else {
9-
const resolveandroid = b.addExecutable("resolveandroid", "resolveandroid.zig");
10-
resolveandroid.setBuildMode(b.standardReleaseOptions());
11-
const run_resolveandroid = resolveandroid.run();
12-
run_resolveandroid.addArg("expect-pass");
13-
run_resolveandroid.addArg(b.zig_exe);
4+
const test_step = b.step("test", "Resolve android and test the zig build files");
145

15-
const test_build_error = b.addExecutable("test-build-error", "resolveandroid.zig");
16-
test_build_error.setBuildMode(b.standardReleaseOptions());
17-
const run_test_build_error = test_build_error.run();
18-
run_test_build_error.addArg("expect-fail");
19-
run_test_build_error.addArg(b.zig_exe);
20-
run_test_build_error.addArg("--build-file");
21-
run_test_build_error.addArg("invalid-build.zig");
6+
{
7+
const exe = b.addExecutable("without-android", "run.zig");
8+
exe.setBuildMode(b.standardReleaseOptions());
9+
const run = exe.run();
10+
run.addArg("pass");
11+
run.addArg("missing package 'androidbuild'");
12+
run.addArg(b.zig_exe);
13+
run.addArg("build");
14+
run.addArg("--build-file");
15+
run.addArg("androidexample-build.zig");
16+
test_step.dependOn(&run.step);
17+
}
18+
19+
{
20+
const exe = b.addExecutable("with-android", "run.zig");
21+
exe.setBuildMode(b.standardReleaseOptions());
22+
const run = exe.run();
23+
run.addArg("pass");
24+
run.addArg("we have the 'androidbuild' package");
25+
run.addArg(b.zig_exe);
26+
run.addArg("build");
27+
run.addArg("--build-file");
28+
run.addArg("androidexample-build.zig");
29+
run.addArg("--pkg-begin");
30+
run.addArg("androidbuild");
31+
run.addArg("androidbuild.zig");
32+
run.addArg("--pkg-end");
33+
test_step.dependOn(&run.step);
34+
}
35+
36+
{
37+
const exe = b.addExecutable("invalid-build", "run.zig");
38+
exe.setBuildMode(b.standardReleaseOptions());
39+
const run = exe.run();
40+
run.addArg("fail");
41+
run.addArg("builtin.hasPkg MUST be called with comptime");
42+
run.addArg(b.zig_exe);
43+
run.addArg("build");
44+
run.addArg("--build-file");
45+
run.addArg("invalid-build.zig");
46+
run.addArg("--pkg-begin");
47+
run.addArg("androidbuild");
48+
run.addArg("androidbuild.zig");
49+
run.addArg("--pkg-end");
50+
test_step.dependOn(&run.step);
51+
}
2252

23-
const test_step = b.step("test", "Resolve android and test the zig build files");
24-
test_step.dependOn(&run_resolveandroid.step);
25-
test_step.dependOn(&run_test_build_error.step);
53+
{
54+
const exe = b.addExecutable("calling-haspkg-outside-build", "run.zig");
55+
exe.setBuildMode(b.standardReleaseOptions());
56+
const run = exe.run();
57+
run.addArg("fail");
58+
run.addArg("builtin.hasPkg is only available in build.zig");
59+
run.addArg(b.zig_exe);
60+
run.addArg("build-exe");
61+
run.addArg("calling-haspkg-outside-build.zig");
62+
test_step.dependOn(&run.step);
2663
}
2764
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub fn main() void {
2+
if (@import("std").builtin.hasPkg("foo")) {
3+
@compileError("calling hasPkg outside of build.zig should be a compile error");
4+
}
5+
}

test/standalone/build_zig_pkgs/invalid-build.zig

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
//! Currently the user MUST specify comptime when calling buidpkgs.has
1+
//! Currently the user MUST specify comptime when calling builtin.hasPkg
22
//! until https://github.com/ziglang/zig/issues/425 is implemented.
3-
//! This build.zig file tries to call buildpkgs.has without comptime and the
3+
//! This build.zig file tries to call builtin.hasPkg without comptime and the
44
//! test is to make sure it produces a compile error.
55
//!
6-
//! Note that the reason why "has" must be evaluated at comptime is
6+
//! Note that the reason why "hasPkg" must be evaluated at comptime is
77
//! because it will always surround an @import statement. The problem is
88
//! that if they forget to add "comptime" to their call, then their build.zig
99
//! file will "sometimes work" so long as they are building with the necessary
1010
//! packages configured, but then it will fail once the @import is missing
11-
//! which defeats the whole purpose of providing "has" in the first place.
11+
//! which defeats the whole purpose of providing "hasPkg" in the first place.
1212
//!
1313
const Builder = @import("std").build.Builder;
14-
const buildpkgs = @import("buildpkgs");
1514

1615
pub fn build(b: *Builder) void {
1716
// This should be a compile error because it's not marked as comptime
18-
if (buildpkgs.has("androidbuild")) {
17+
if (@import("std").builtin.hasPkg("androidbuild")) {
1918
const androidbuild = @import("androidbuild");
2019
androidbuild.makeApk(b);
2120
}

test/standalone/build_zig_pkgs/resolveandroid.zig

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)