@@ -16,7 +16,7 @@ const FileSource = std.Build.FileSource;
16
16
const PkgConfigPkg = std .Build .PkgConfigPkg ;
17
17
const PkgConfigError = std .Build .PkgConfigError ;
18
18
const ExecError = std .Build .ExecError ;
19
- const Pkg = std .Build .Pkg ;
19
+ const Module = std .Build .Module ;
20
20
const VcpkgRoot = std .Build .VcpkgRoot ;
21
21
const InstallDir = std .Build .InstallDir ;
22
22
const InstallArtifactStep = std .Build .InstallArtifactStep ;
@@ -99,7 +99,7 @@ root_src: ?FileSource,
99
99
out_h_filename : []const u8 ,
100
100
out_lib_filename : []const u8 ,
101
101
out_pdb_filename : []const u8 ,
102
- packages : ArrayList ( Pkg ),
102
+ modules : std . StringArrayHashMap ( * Module ),
103
103
104
104
object_src : []const u8 ,
105
105
@@ -334,7 +334,7 @@ pub fn create(builder: *std.Build, options: Options) *CompileStep {
334
334
.out_pdb_filename = builder .fmt ("{s}.pdb" , .{name }),
335
335
.major_only_filename = null ,
336
336
.name_only_filename = null ,
337
- .packages = ArrayList ( Pkg ).init (builder .allocator ),
337
+ .modules = std . StringArrayHashMap ( * Module ).init (builder .allocator ),
338
338
.include_dirs = ArrayList (IncludeDir ).init (builder .allocator ),
339
339
.link_objects = ArrayList (LinkObject ).init (builder .allocator ),
340
340
.c_macros = ArrayList ([]const u8 ).init (builder .allocator ),
@@ -946,29 +946,29 @@ pub fn addFrameworkPath(self: *CompileStep, dir_path: []const u8) void {
946
946
self .framework_dirs .append (self .builder .dupe (dir_path )) catch @panic ("OOM" );
947
947
}
948
948
949
- pub fn addPackage (self : * CompileStep , package : Pkg ) void {
950
- self .packages .append (self .builder .dupePkg (package )) catch @panic ("OOM" );
951
- self .addRecursiveBuildDeps (package );
949
+ /// Adds a module to be used with `@import` and exposing it in the current
950
+ /// package's module table using `name`.
951
+ pub fn addModule (cs : * CompileStep , name : []const u8 , module : * Module ) void {
952
+ cs .modules .put (cs .builder .dupe (name ), module ) catch @panic ("OOM" );
953
+ cs .addRecursiveBuildDeps (module );
952
954
}
953
955
954
- pub fn addOptions (self : * CompileStep , package_name : []const u8 , options : * OptionsStep ) void {
955
- self .addPackage (options .getPackage (package_name ));
956
+ /// Adds a module to be used with `@import` without exposing it in the current
957
+ /// package's module table.
958
+ pub fn addAnonymousModule (cs : * CompileStep , name : []const u8 , options : std.Build.CreateModuleOptions ) void {
959
+ const module = cs .builder .createModule (options );
960
+ return addModule (cs , name , module );
956
961
}
957
962
958
- fn addRecursiveBuildDeps (self : * CompileStep , package : Pkg ) void {
959
- package .source .addStepDependencies (& self .step );
960
- if (package .dependencies ) | deps | {
961
- for (deps ) | dep | {
962
- self .addRecursiveBuildDeps (dep );
963
- }
964
- }
963
+ pub fn addOptions (cs : * CompileStep , module_name : []const u8 , options : * OptionsStep ) void {
964
+ addModule (cs , module_name , options .createModule ());
965
965
}
966
966
967
- pub fn addPackagePath ( self : * CompileStep , name : [] const u8 , pkg_index_path : [] const u8 ) void {
968
- self . addPackage ( Pkg {
969
- . name = self . builder . dupe ( name ),
970
- . source = .{ . path = self . builder . dupe ( pkg_index_path ) },
971
- });
967
+ fn addRecursiveBuildDeps ( cs : * CompileStep , module : * Module ) void {
968
+ module . source_file . addStepDependencies ( & cs . step );
969
+ for ( module . dependencies . values ()) | dep | {
970
+ cs . addRecursiveBuildDeps ( dep );
971
+ }
972
972
}
973
973
974
974
/// If Vcpkg was found on the system, it will be added to include and lib
@@ -1023,16 +1023,21 @@ fn linkLibraryOrObject(self: *CompileStep, other: *CompileStep) void {
1023
1023
self .include_dirs .append (.{ .other_step = other }) catch @panic ("OOM" );
1024
1024
}
1025
1025
1026
- fn makePackageCmd (self : * CompileStep , pkg : Pkg , zig_args : * ArrayList ([]const u8 )) error {OutOfMemory }! void {
1027
- const builder = self .builder ;
1028
-
1026
+ fn appendModuleArgs (
1027
+ cs : * CompileStep ,
1028
+ zig_args : * ArrayList ([]const u8 ),
1029
+ name : []const u8 ,
1030
+ module : * Module ,
1031
+ ) error {OutOfMemory }! void {
1029
1032
try zig_args .append ("--pkg-begin" );
1030
- try zig_args .append (pkg .name );
1031
- try zig_args .append (builder .pathFromRoot (pkg .source .getPath (self .builder )));
1032
-
1033
- if (pkg .dependencies ) | dependencies | {
1034
- for (dependencies ) | sub_pkg | {
1035
- try self .makePackageCmd (sub_pkg , zig_args );
1033
+ try zig_args .append (name );
1034
+ try zig_args .append (module .builder .pathFromRoot (module .source_file .getPath (module .builder )));
1035
+
1036
+ {
1037
+ const keys = module .dependencies .keys ();
1038
+ for (module .dependencies .values ()) | sub_module , i | {
1039
+ const sub_name = keys [i ];
1040
+ try cs .appendModuleArgs (zig_args , sub_name , sub_module );
1036
1041
}
1037
1042
}
1038
1043
@@ -1563,8 +1568,12 @@ fn make(step: *Step) !void {
1563
1568
try zig_args .append ("--test-no-exec" );
1564
1569
}
1565
1570
1566
- for (self .packages .items ) | pkg | {
1567
- try self .makePackageCmd (pkg , & zig_args );
1571
+ {
1572
+ const keys = self .modules .keys ();
1573
+ for (self .modules .values ()) | module , i | {
1574
+ const name = keys [i ];
1575
+ try self .appendModuleArgs (& zig_args , name , module );
1576
+ }
1568
1577
}
1569
1578
1570
1579
for (self .include_dirs .items ) | include_dir | {
@@ -1942,46 +1951,6 @@ fn getPkgConfigList(self: *std.Build) ![]const PkgConfigPkg {
1942
1951
}
1943
1952
}
1944
1953
1945
- test "addPackage" {
1946
- if (builtin .os .tag == .wasi ) return error .SkipZigTest ;
1947
-
1948
- var arena = std .heap .ArenaAllocator .init (std .testing .allocator );
1949
- defer arena .deinit ();
1950
-
1951
- const host = try NativeTargetInfo .detect (.{});
1952
-
1953
- var builder = try std .Build .create (
1954
- arena .allocator (),
1955
- "test" ,
1956
- "test" ,
1957
- "test" ,
1958
- "test" ,
1959
- host ,
1960
- );
1961
- defer builder .destroy ();
1962
-
1963
- const pkg_dep = Pkg {
1964
- .name = "pkg_dep" ,
1965
- .source = .{ .path = "/not/a/pkg_dep.zig" },
1966
- };
1967
- const pkg_top = Pkg {
1968
- .name = "pkg_dep" ,
1969
- .source = .{ .path = "/not/a/pkg_top.zig" },
1970
- .dependencies = &[_ ]Pkg {pkg_dep },
1971
- };
1972
-
1973
- var exe = builder .addExecutable (.{
1974
- .name = "not_an_executable" ,
1975
- .root_source_file = .{ .path = "/not/an/executable.zig" },
1976
- });
1977
- exe .addPackage (pkg_top );
1978
-
1979
- try std .testing .expectEqual (@as (usize , 1 ), exe .packages .items .len );
1980
-
1981
- const dupe = exe .packages .items [0 ];
1982
- try std .testing .expectEqualStrings (pkg_top .name , dupe .name );
1983
- }
1984
-
1985
1954
fn addFlag (args : * ArrayList ([]const u8 ), comptime name : []const u8 , opt : ? bool ) ! void {
1986
1955
const cond = opt orelse return ;
1987
1956
try args .ensureUnusedCapacity (1 );
0 commit comments