authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-04-30 13:01:35-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-04-30 13:01:35-04:00
log38a04a267c11fdedc62102ab8e5989cafa3073ef
tree3570767db1501a3f51b386870b12139aa575096a
parent43e7ac8418172e16def3ed8abf12e303738d7569

zig build: when compiling C files put .o files in cache dir

See #328

3 files changed, 29 insertions(+), 12 deletions(-)

std/build.zig+22-10
...@@ -284,7 +284,7 @@ pub const Builder = struct {...@@ -284,7 +284,7 @@ pub const Builder = struct {
284 return &top_level_step.step;284 return &top_level_step.step;
285 }285 }
286 }286 }
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);
288 return error.InvalidStepName;288 return error.InvalidStepName;
289 }289 }
290290
...@@ -529,6 +529,13 @@ pub const Builder = struct {...@@ -529,6 +529,13 @@ pub const Builder = struct {
529529
530 }530 }
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
532 pub fn installCLibrary(self: &Builder, lib: &CLibrary) -> &InstallCLibraryStep {539 pub fn installCLibrary(self: &Builder, lib: &CLibrary) -> &InstallCLibraryStep {
533 const install_step = %%self.allocator.create(InstallCLibraryStep);540 const install_step = %%self.allocator.create(InstallCLibraryStep);
534 *install_step = InstallCLibraryStep.init(self, lib);541 *install_step = InstallCLibraryStep.init(self, lib);
...@@ -1090,11 +1097,13 @@ pub const CLibrary = struct {...@@ -1090,11 +1097,13 @@ pub const CLibrary = struct {
1090 %%cc_args.append("-c");1097 %%cc_args.append("-c");
1091 %%cc_args.append(source_file);1098 %%cc_args.append(source_file);
10921099
1093 // TODO don't dump the .o file in the same place as the source file1100 const rel_src_path = %%os.path.relative(builder.allocator, builder.build_root, source_file);
1094 const o_file = builder.fmt("{}{}", source_file, self.target.oFileExt());1101 const cache_o_src = %%os.path.join(builder.allocator, builder.cache_root, rel_src_path);
1095 defer builder.allocator.free(o_file);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());
1096 %%cc_args.append("-o");1105 %%cc_args.append("-o");
1097 %%cc_args.append(o_file);1106 %%cc_args.append(cache_o_file);
10981107
1099 for (self.cflags.toSliceConst()) |cflag| {1108 for (self.cflags.toSliceConst()) |cflag| {
1100 %%cc_args.append(cflag);1109 %%cc_args.append(cflag);
...@@ -1107,7 +1116,7 @@ pub const CLibrary = struct {...@@ -1107,7 +1116,7 @@ pub const CLibrary = struct {
11071116
1108 %return builder.spawnChild(cc, cc_args.toSliceConst());1117 %return builder.spawnChild(cc, cc_args.toSliceConst());
11091118
1110 %%self.object_files.append(o_file);1119 %%self.object_files.append(cache_o_file);
1111 }1120 }
11121121
1113 if (self.static) {1122 if (self.static) {
...@@ -1248,10 +1257,13 @@ pub const CExecutable = struct {...@@ -1248,10 +1257,13 @@ pub const CExecutable = struct {
1248 %%cc_args.append(builder.pathFromRoot(source_file));1257 %%cc_args.append(builder.pathFromRoot(source_file));
12491258
1250 // TODO don't dump the .o file in the same place as the source file1259 // 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());1260 const rel_src_path = %%os.path.relative(builder.allocator, builder.build_root, source_file);
1252 defer builder.allocator.free(o_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());
1253 %%cc_args.append("-o");1265 %%cc_args.append("-o");
1254 %%cc_args.append(o_file);1266 %%cc_args.append(cache_o_file);
12551267
1256 for (self.cflags.toSliceConst()) |cflag| {1268 for (self.cflags.toSliceConst()) |cflag| {
1257 %%cc_args.append(cflag);1269 %%cc_args.append(cflag);
...@@ -1264,7 +1276,7 @@ pub const CExecutable = struct {...@@ -1264,7 +1276,7 @@ pub const CExecutable = struct {
12641276
1265 %return builder.spawnChild(cc, cc_args.toSliceConst());1277 %return builder.spawnChild(cc, cc_args.toSliceConst());
12661278
1267 %%self.object_files.append(o_file);1279 %%self.object_files.append(cache_o_file);
1268 }1280 }
12691281
1270 %%cc_args.resize(0);1282 %%cc_args.resize(0);
std/os/path.zig+1-1
...@@ -17,7 +17,7 @@ pub const delimiter = switch (@compileVar("os")) {...@@ -17,7 +17,7 @@ pub const delimiter = switch (@compileVar("os")) {
17/// Naively combines a series of paths with the native path seperator.17/// Naively combines a series of paths with the native path seperator.
18/// Allocates memory for the result, which must be freed by the caller.18/// Allocates memory for the result, which must be freed by the caller.
19pub fn join(allocator: &Allocator, paths: ...) -> %[]u8 {19pub fn join(allocator: &Allocator, paths: ...) -> %[]u8 {
20 assert(paths.len >= 2);20 comptime assert(paths.len >= 2);
21 var total_paths_len: usize = paths.len; // 1 slash per path21 var total_paths_len: usize = paths.len; // 1 slash per path
22 {22 {
23 comptime var path_i = 0;23 comptime var path_i = 0;
std/special/build_runner.zig+6-1
...@@ -95,7 +95,12 @@ pub fn main() -> %void {...@@ -95,7 +95,12 @@ pub fn main() -> %void {
95 if (builder.validateUserInputDidItFail())95 if (builder.validateUserInputDidItFail())
96 return usage(&builder, true, &io.stderr);96 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 };
99}104}
100105
101fn usage(builder: &Builder, already_ran_build: bool, out_stream: &io.OutStream) -> %void {106fn usage(builder: &Builder, already_ran_build: bool, out_stream: &io.OutStream) -> %void {