Skip to content

Commit 92ed3c6

Browse files
committed
resolve list of compilation units from build runner
1 parent 33f2c61 commit 92ed3c6

File tree

10 files changed

+39
-4
lines changed

10 files changed

+39
-4
lines changed

src/build_runner/build_runner.zig

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,7 @@ fn extractBuildInformation(
10311031
modules: *std.StringArrayHashMapUnmanaged(shared.BuildConfig.Module),
10321032
module: *std.Build.Module,
10331033
compile: ?*Step.Compile,
1034+
cwd: []const u8,
10341035
) !void {
10351036
const root_source_file = module.root_source_file orelse return;
10361037

@@ -1084,8 +1085,6 @@ fn extractBuildInformation(
10841085
}
10851086
}
10861087

1087-
const cwd = try std.process.getCwdAlloc(allocator);
1088-
10891088
const root_source_file_path = try std.fs.path.resolve(allocator, &.{ cwd, root_source_file.getPath2(module.owner, null) });
10901089

10911090
// All modules with the same root source file are merged. This limitation may be lifted in the future.
@@ -1190,11 +1189,12 @@ fn extractBuildInformation(
11901189
// - modules that are reachable from the "install" step
11911190
// - all other reachable modules
11921191
var modules: std.StringArrayHashMapUnmanaged(BuildConfig.Module) = .empty;
1192+
const cwd = try std.process.getCwdAlloc(arena);
11931193

11941194
for (b.modules.values()) |root_module| {
11951195
const graph = root_module.getGraph();
11961196
for (graph.modules) |module| {
1197-
try helper.processModule(arena, &modules, module, null);
1197+
try helper.processModule(arena, &modules, module, null, cwd);
11981198
}
11991199
}
12001200

@@ -1206,11 +1206,21 @@ fn extractBuildInformation(
12061206
const compile = step.cast(Step.Compile) orelse continue;
12071207
const graph = compile.root_module.getGraph();
12081208
for (graph.modules) |module| {
1209-
try helper.processModule(arena, &modules, module, compile);
1209+
try helper.processModule(arena, &modules, module, compile, cwd);
12101210
}
12111211
}
12121212
}
12131213

1214+
var compilations: std.ArrayList(BuildConfig.Compile) = .empty;
1215+
for (all_steps.keys()) |step| {
1216+
const compile = step.cast(Step.Compile) orelse continue;
1217+
const root_source_file = compile.root_module.root_source_file orelse continue;
1218+
const root_source_file_path = try std.fs.path.resolve(arena, &.{ cwd, root_source_file.getPath2(compile.root_module.owner, null) });
1219+
try compilations.append(arena, .{
1220+
.root_module = root_source_file_path,
1221+
});
1222+
}
1223+
12141224
// Sample `@dependencies` structure:
12151225
// pub const packages = struct {
12161226
// pub const @"1220363c7e27b2d3f39de6ff6e90f9537a0634199860fea237a55ddb1e1717f5d6a5" = struct {
@@ -1255,6 +1265,7 @@ fn extractBuildInformation(
12551265
BuildConfig{
12561266
.dependencies = .{ .map = root_dependencies },
12571267
.modules = .{ .map = modules },
1268+
.compilations = compilations.items,
12581269
.top_level_steps = b.top_level_steps.keys(),
12591270
.available_options = .{ .map = available_options },
12601271
},

src/build_runner/shared.zig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ pub const BuildConfig = struct {
99
/// The key is the `root_source_file`.
1010
/// All modules with the same root source file are merged. This limitation may be lifted in the future.
1111
modules: std.json.ArrayHashMap(Module),
12+
/// List of all compilations units.
13+
compilations: []const Compile,
1214
/// The names of all top level steps.
1315
top_level_steps: []const []const u8,
1416
available_options: std.json.ArrayHashMap(AvailableOption),
@@ -19,6 +21,13 @@ pub const BuildConfig = struct {
1921
include_dirs: []const []const u8,
2022
};
2123

24+
pub const Compile = struct {
25+
/// Key in `BuildConfig.modules`.
26+
root_module: []const u8,
27+
28+
// may contain additional information in the future like `target` or `link_libc`.
29+
};
30+
2231
/// Equivalent to `std.Build.AvailableOption` which is not accessible because it non-pub.
2332
pub const AvailableOption = @FieldType(@FieldType(std.Build, "available_options_map").KV, "value");
2433
};

tests/build_runner_cases/add_module.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"include_dirs": []
88
}
99
},
10+
"compilations": [],
1011
"top_level_steps": [
1112
"install",
1213
"uninstall"

tests/build_runner_cases/define_c_macro.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"include_dirs": []
1010
}
1111
},
12+
"compilations": [],
1213
"top_level_steps": [
1314
"install",
1415
"uninstall"

tests/build_runner_cases/empty.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"dependencies": {},
33
"modules": {},
4+
"compilations": [],
45
"top_level_steps": [
56
"install",
67
"uninstall"

tests/build_runner_cases/module_root_source_file_collision.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
"include_dirs": []
2727
}
2828
},
29+
"compilations": [
30+
{
31+
"root_module": "main.zig"
32+
}
33+
],
2934
"top_level_steps": [
3035
"install",
3136
"uninstall"

tests/build_runner_cases/module_self_import.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"include_dirs": []
1010
}
1111
},
12+
"compilations": [],
1213
"top_level_steps": [
1314
"install",
1415
"uninstall"

tests/build_runner_cases/multiple_module_import_names.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"include_dirs": []
2525
}
2626
},
27+
"compilations": [],
2728
"top_level_steps": [
2829
"install",
2930
"uninstall"

tests/build_runner_cases/public_module_with_generated_file.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"include_dirs": []
88
}
99
},
10+
"compilations": [],
1011
"top_level_steps": [
1112
"install",
1213
"uninstall"

tests/build_runner_check.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ pub fn main() !u8 {
5252
try mod.import_table.map.reIndex(arena);
5353
}
5454

55+
for (new.compilations) |*compile| {
56+
@as(*[]const u8, @constCast(&compile.root_module)).* = try sanitizePath(arena, compile.root_module, cwd, local_cache_dir, global_cache_dir);
57+
}
58+
5559
break :sanitized try std.json.Stringify.valueAlloc(
5660
gpa,
5761
new,

0 commit comments

Comments
 (0)