Skip to content

Commit 38a04a2

Browse files
committed
zig build: when compiling C files put .o files in cache dir
See #328
1 parent 43e7ac8 commit 38a04a2

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

std/build.zig

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ pub const Builder = struct {
284284
return &top_level_step.step;
285285
}
286286
}
287-
%%io.stderr.printf("Cannot run step '{}' because it does not exist.", name);
287+
%%io.stderr.printf("Cannot run step '{}' because it does not exist\n", name);
288288
return error.InvalidStepName;
289289
}
290290

@@ -529,6 +529,13 @@ pub const Builder = struct {
529529

530530
}
531531

532+
pub fn makePath(self: &Builder, path: []const u8) -> %void {
533+
os.makePath(self.allocator, path) %% |err| {
534+
%%io.stderr.printf("Unable to create path {}: {}\n", path, @errorName(err));
535+
return err;
536+
};
537+
}
538+
532539
pub fn installCLibrary(self: &Builder, lib: &CLibrary) -> &InstallCLibraryStep {
533540
const install_step = %%self.allocator.create(InstallCLibraryStep);
534541
*install_step = InstallCLibraryStep.init(self, lib);
@@ -1090,11 +1097,13 @@ pub const CLibrary = struct {
10901097
%%cc_args.append("-c");
10911098
%%cc_args.append(source_file);
10921099

1093-
// TODO don't dump the .o file in the same place as the source file
1094-
const o_file = builder.fmt("{}{}", source_file, self.target.oFileExt());
1095-
defer builder.allocator.free(o_file);
1100+
const rel_src_path = %%os.path.relative(builder.allocator, builder.build_root, source_file);
1101+
const cache_o_src = %%os.path.join(builder.allocator, builder.cache_root, rel_src_path);
1102+
const cache_o_dir = os.path.dirname(cache_o_src);
1103+
%return builder.makePath(cache_o_dir);
1104+
const cache_o_file = builder.fmt("{}{}", cache_o_src, self.target.oFileExt());
10961105
%%cc_args.append("-o");
1097-
%%cc_args.append(o_file);
1106+
%%cc_args.append(cache_o_file);
10981107

10991108
for (self.cflags.toSliceConst()) |cflag| {
11001109
%%cc_args.append(cflag);
@@ -1107,7 +1116,7 @@ pub const CLibrary = struct {
11071116

11081117
%return builder.spawnChild(cc, cc_args.toSliceConst());
11091118

1110-
%%self.object_files.append(o_file);
1119+
%%self.object_files.append(cache_o_file);
11111120
}
11121121

11131122
if (self.static) {
@@ -1248,10 +1257,13 @@ pub const CExecutable = struct {
12481257
%%cc_args.append(builder.pathFromRoot(source_file));
12491258

12501259
// TODO don't dump the .o file in the same place as the source file
1251-
const o_file = builder.fmt("{}{}", source_file, self.target.oFileExt());
1252-
defer builder.allocator.free(o_file);
1260+
const rel_src_path = %%os.path.relative(builder.allocator, builder.build_root, source_file);
1261+
const cache_o_src = %%os.path.join(builder.allocator, builder.cache_root, rel_src_path);
1262+
const cache_o_dir = os.path.dirname(cache_o_src);
1263+
%return builder.makePath(cache_o_dir);
1264+
const cache_o_file = builder.fmt("{}{}", cache_o_src, self.target.oFileExt());
12531265
%%cc_args.append("-o");
1254-
%%cc_args.append(o_file);
1266+
%%cc_args.append(cache_o_file);
12551267

12561268
for (self.cflags.toSliceConst()) |cflag| {
12571269
%%cc_args.append(cflag);
@@ -1264,7 +1276,7 @@ pub const CExecutable = struct {
12641276

12651277
%return builder.spawnChild(cc, cc_args.toSliceConst());
12661278

1267-
%%self.object_files.append(o_file);
1279+
%%self.object_files.append(cache_o_file);
12681280
}
12691281

12701282
%%cc_args.resize(0);

std/os/path.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub const delimiter = switch (@compileVar("os")) {
1717
/// Naively combines a series of paths with the native path seperator.
1818
/// Allocates memory for the result, which must be freed by the caller.
1919
pub fn join(allocator: &Allocator, paths: ...) -> %[]u8 {
20-
assert(paths.len >= 2);
20+
comptime assert(paths.len >= 2);
2121
var total_paths_len: usize = paths.len; // 1 slash per path
2222
{
2323
comptime var path_i = 0;

std/special/build_runner.zig

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,12 @@ pub fn main() -> %void {
9595
if (builder.validateUserInputDidItFail())
9696
return usage(&builder, true, &io.stderr);
9797

98-
%return builder.make(targets.toSliceConst());
98+
builder.make(targets.toSliceConst()) %% |err| {
99+
if (err == error.InvalidStepName) {
100+
return usage(&builder, true, &io.stderr);
101+
}
102+
return err;
103+
};
99104
}
100105

101106
fn usage(builder: &Builder, already_ran_build: bool, out_stream: &io.OutStream) -> %void {

0 commit comments

Comments
 (0)