Skip to content

Commit 09bc4d6

Browse files
committed
build system: addAssembly and addObject functions
for building executables
1 parent afa80da commit 09bc4d6

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

std/build.zig

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,32 @@ pub const LinkStep = struct {
11301130
%%self.object_files.append(file);
11311131
}
11321132

1133+
pub fn addObject(self: &LinkStep, obj: &ObjectStep) {
1134+
self.step.dependOn(&obj.step);
1135+
1136+
const path_to_obj = test (obj.output_path) |explicit_out_path| {
1137+
explicit_out_path
1138+
} else {
1139+
// TODO make it so we always know where this will be
1140+
%%os.path.join(self.builder.allocator, self.builder.out_dir,
1141+
self.builder.fmt("{}{}", obj.name, obj.target.oFileExt()))
1142+
};
1143+
%%self.object_files.append(path_to_obj);
1144+
}
1145+
1146+
pub fn addAssembly(self: &LinkStep, assembly: &AsmStep) {
1147+
self.step.dependOn(&assembly.step);
1148+
1149+
const path_to_obj = test (assembly.output_path) |explicit_out_path| {
1150+
explicit_out_path
1151+
} else {
1152+
// TODO make it so we always know where this will be
1153+
%%os.path.join(self.builder.allocator, self.builder.out_dir,
1154+
self.builder.fmt("{}{}", assembly.name, assembly.target.oFileExt()))
1155+
};
1156+
%%self.object_files.append(path_to_obj);
1157+
}
1158+
11331159
pub fn setTarget(self: &LinkStep, target_arch: Arch, target_os: Os, target_environ: Environ) {
11341160
self.target = Target.Cross {
11351161
CrossTarget {

test/tests.zig

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -349,25 +349,22 @@ pub const CompareOutputContext = struct {
349349

350350
switch (case.special) {
351351
Special.Asm => {
352-
const obj_path = %%os.path.join(b.allocator, "test_artifacts", "test.o");
353352
const annotated_case_name = %%fmt.allocPrint(self.b.allocator, "assemble-and-link {}", case.name);
354353
test (self.test_filter) |filter| {
355354
if (mem.indexOf(u8, annotated_case_name, filter) == null)
356355
return;
357356
}
358357

359-
const obj = b.addAssemble("test", root_src);
360-
obj.setOutputPath(obj_path);
358+
const assembly = b.addAssemble("test", root_src);
361359

362360
for (case.sources.toSliceConst()) |src_file| {
363361
const expanded_src_path = %%os.path.join(b.allocator, "test_artifacts", src_file.filename);
364362
const write_src = b.addWriteFile(expanded_src_path, src_file.source);
365-
obj.step.dependOn(&write_src.step);
363+
assembly.step.dependOn(&write_src.step);
366364
}
367365

368366
const exe = b.addLinkExecutable("test");
369-
exe.step.dependOn(&obj.step);
370-
exe.addObjectFile(obj_path);
367+
exe.addAssembly(assembly);
371368
exe.setOutputPath(exe_path);
372369

373370
const run_and_cmp_output = RunCompareOutputStep.create(self, exe_path, annotated_case_name,

0 commit comments

Comments
 (0)