@@ -137,9 +137,9 @@ emit_llvm_ir: ?EmitLoc,
137
137
emit_analysis : ? EmitLoc ,
138
138
emit_docs : ? EmitLoc ,
139
139
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 ,
140
+ /// enables the "buildpkgs" package that exposes what packages are available to the module.
141
+ /// this should only be enabled for build.zig and its package dependencies.
142
+ enable_buildpkgs : bool ,
143
143
144
144
work_queue_wait_group : WaitGroup ,
145
145
@@ -507,7 +507,7 @@ pub const InitOptions = struct {
507
507
test_filter : ? []const u8 = null ,
508
508
test_name_prefix : ? []const u8 = null ,
509
509
subsystem : ? std.Target.SubSystem = null ,
510
- enable_builtin_pkg_names : ? bool = null ,
510
+ enable_buildpkgs : ? bool = false ,
511
511
};
512
512
513
513
fn addPackageTableToCacheHash (
@@ -909,6 +909,14 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
909
909
artifact_sub_dir ,
910
910
};
911
911
912
+ if (options .enable_buildpkgs orelse false ) {
913
+ if (root_pkg .table .get ("@build" )) | build_pkg | {
914
+ try addBuildpkgs (gpa , arena , zig_cache_artifact_directory , "buildpkgs" , build_pkg );
915
+ } else {
916
+ fatal ("enable_buildpkgs should only be enabled for build.zig" , .{});
917
+ }
918
+ }
919
+
912
920
// TODO when we implement serialization and deserialization of incremental compilation metadata,
913
921
// this is where we would load it. We have open a handle to the directory where
914
922
// the output either already is, or will be.
@@ -1125,7 +1133,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
1125
1133
.test_evented_io = options .test_evented_io ,
1126
1134
.debug_compiler_runtime_libs = options .debug_compiler_runtime_libs ,
1127
1135
.work_queue_wait_group = undefined ,
1128
- .enable_builtin_pkg_names = options .enable_builtin_pkg_names orelse false ,
1136
+ .enable_buildpkgs = options .enable_buildpkgs orelse false ,
1129
1137
};
1130
1138
break :comp comp ;
1131
1139
};
@@ -2791,7 +2799,7 @@ fn updateBuiltinZigFile(comp: *Compilation, mod: *Module) !void {
2791
2799
const tracy = trace (@src ());
2792
2800
defer tracy .end ();
2793
2801
2794
- const source = try comp .generateBuiltinZigSource (comp .gpa , mod );
2802
+ const source = try comp .generateBuiltinZigSource (comp .gpa );
2795
2803
defer comp .gpa .free (source );
2796
2804
try mod .zig_cache_artifact_directory .handle .writeFile ("builtin.zig" , source );
2797
2805
}
@@ -2803,7 +2811,7 @@ pub fn dump_argv(argv: []const []const u8) void {
2803
2811
std .debug .print ("{s}\n " , .{argv [argv .len - 1 ]});
2804
2812
}
2805
2813
2806
- pub fn generateBuiltinZigSource (comp : * Compilation , allocator : * Allocator , optional_mod : ? * Module ) ! []u8 {
2814
+ pub fn generateBuiltinZigSource (comp : * Compilation , allocator : * Allocator ) ! []u8 {
2807
2815
const tracy = trace (@src ());
2808
2816
defer tracy .end ();
2809
2817
@@ -2993,59 +3001,76 @@ pub fn generateBuiltinZigSource(comp: *Compilation, allocator: *Allocator, optio
2993
3001
}
2994
3002
}
2995
3003
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
- // TODO: this isn't right, each package should have it's
3004
- // own set of sub-packages
3005
- try recursiveWritePackageNames (buffer .writer (), mod .root_pkg );
3006
- }
3007
- try buffer .writer ().writeAll (
3008
- \\};
3009
- \\
3010
- \\// using .Inline to force this to be comptime
3011
- \\pub fn hasPkg(comptime name: []const u8) callconv(.Inline) bool {
3012
- \\ if (!isComptime()) {
3013
- \\ @compileError("builtin.hasPkg MUST be called with comptime");
3014
- \\ }
3015
- \\ inline for (pkg_names) |has_name| {
3016
- \\ if (@import("std").mem.eql(u8, name, has_name)) return true;
3017
- \\ }
3018
- \\ return false;
3019
- \\}
3020
- \\
3021
- \\// A temporary workaround until https://github.com/ziglang/zig/issues/425
3022
- \\// is implemented and the callconv(.Inline) on the `hasPkg` function forces
3023
- \\// it to be comptime
3024
- \\fn isComptime() bool {
3025
- \\ var t: bool = true;
3026
- \\ const x = if (t) @as(u7, 0) else @as(u8, 0);
3027
- \\ return @TypeOf(x) == u7;
3028
- \\}
3029
- \\
3030
- );
3031
- } else {
3032
- try buffer .writer ().writeAll (
3033
- \\
3034
- \\pub const pkg_names = @compileError("builtin.pkg_names is only available in build.zig");
3035
- \\pub const hasPkg = @compileError("builtin.hasPkg is only available in build.zig");
3036
- \\
3037
- );
3004
+ return buffer .toOwnedSlice ();
3005
+ }
3006
+
3007
+ fn addBuildpkgs (gpa : * Allocator , arena : * Allocator , dir : Directory , name : []const u8 , pkg : * Package ) anyerror ! void {
3008
+ var pkg_dir = Directory {
3009
+ .path = try dir .join (gpa , &[_ ][]const u8 { name }),
3010
+ .handle = try dir .handle .makeOpenPath (name , .{}),
3011
+ };
3012
+ defer pkg_dir .handle .close ();
3013
+ var sub_pkg_it = pkg .table .iterator ();
3014
+ while (sub_pkg_it .next ()) | sub_pkg | {
3015
+ try addBuildpkgs (gpa , arena , pkg_dir , sub_pkg .key , sub_pkg .value );
3038
3016
}
3039
3017
3040
- return buffer .toOwnedSlice ();
3018
+ const source = try generateBuildpkgsZigSource (gpa , pkg );
3019
+ defer gpa .free (source );
3020
+ try pkg_dir .handle .writeFile ("buildpkgs.zig" , source );
3021
+
3022
+ try pkg .table .put (arena , "buildpkgs" , Package .create (gpa , pkg_dir .path , "buildpkgs.zig" ) catch | err | {
3023
+ fatal ("Failed to create buildpkgs Package at path {s}: {s}" , .{ pkg_dir .path , @errorName (err ) });
3024
+ });
3041
3025
}
3042
3026
3043
- fn recursiveWritePackageNames (writer : anytype , pkg : * const Package ) anyerror ! void {
3027
+ pub fn generateBuildpkgsZigSource (allocator : * Allocator , pkg : * const Package ) ! []u8 {
3028
+ const tracy = trace (@src ());
3029
+ defer tracy .end ();
3030
+
3031
+ var buffer = std .ArrayList (u8 ).init (allocator );
3032
+ defer buffer .deinit ();
3033
+
3034
+ try buffer .writer ().writeAll (
3035
+ \\
3036
+ \\pub const names = &[_][]const u8 {
3037
+ \\
3038
+ );
3039
+ //std.log.info("writePackageNames: begin (path={s}, dir={s})", .{
3040
+ // pkg.root_src_path,
3041
+ // if (pkg.root_src_directory.path) |p| p else "<none>",
3042
+ //});
3044
3043
var pkg_it = pkg .table .iterator ();
3045
3044
while (pkg_it .next ()) | sub_pkg | {
3046
- try writer . print ( " \" {s}\" , \n " , .{sub_pkg .key });
3047
- try recursiveWritePackageNames ( writer , sub_pkg .value );
3045
+ //std.log.info("writePackageNames: name= {s}", .{sub_pkg.key});
3046
+ try buffer . writer (). print ( " \" {s} \" , \n " , .{ sub_pkg .key } );
3048
3047
}
3048
+ try buffer .writer ().writeAll (
3049
+ \\};
3050
+ \\
3051
+ \\// using .Inline to force this to be comptime
3052
+ \\pub fn has(comptime name: []const u8) callconv(.Inline) bool {
3053
+ \\ if (!isComptime()) {
3054
+ \\ @compileError("buildpkgs.has MUST be called with comptime");
3055
+ \\ }
3056
+ \\ inline for (names) |has_name| {
3057
+ \\ if (@import("std").mem.eql(u8, name, has_name)) return true;
3058
+ \\ }
3059
+ \\ return false;
3060
+ \\}
3061
+ \\
3062
+ \\// A temporary workaround until https://github.com/ziglang/zig/issues/425
3063
+ \\// is implemented and the callconv(.Inline) on the `has` function forces
3064
+ \\// it to be comptime
3065
+ \\fn isComptime() bool {
3066
+ \\ var t: bool = true;
3067
+ \\ const x = if (t) @as(u7, 0) else @as(u8, 0);
3068
+ \\ return @TypeOf(x) == u7;
3069
+ \\}
3070
+ \\
3071
+ );
3072
+
3073
+ return buffer .toOwnedSlice ();
3049
3074
}
3050
3075
3051
3076
pub fn updateSubCompilation (sub_compilation : * Compilation ) ! void {
0 commit comments