From 0bdc85181cbe1954dac7e96c5ab4b2fe209b9098 Mon Sep 17 00:00:00 2001 From: Jay Weisskopf Date: Wed, 11 Sep 2019 00:25:10 -0400 Subject: [PATCH] Create user-specified `output-dir` Fixes #2637 --- src/codegen.cpp | 9 +++++++-- test/cli.zig | 10 ++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/codegen.cpp b/src/codegen.cpp index 098441496442aab484656bb129ba86717a43cde8..9937cba1c6bfec4424ff8d1374fcbe32edbf7809 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -10238,8 +10238,13 @@ void codegen_build_and_link(CodeGen *g) { Error err; assert(g->out_type != OutTypeUnknown); - if (!g->enable_cache && g->output_dir == nullptr) { - g->output_dir = buf_create_from_str("."); + if (!g->enable_cache) { + if (g->output_dir == nullptr) { + g->output_dir = buf_create_from_str("."); + } else if ((err = os_make_path(g->output_dir))) { + fprintf(stderr, "Unable to create output directory: %s\n", err_str(err)); + exit(1); + } } g->have_dynamic_link = detect_dynamic_link(g); diff --git a/test/cli.zig b/test/cli.zig index 6d0b09785f2895bfbc0fa375ba70cedade09f3bf..8287c41b8105656e2f9f38599bfeb031ab41edb4 100644 --- a/test/cli.zig +++ b/test/cli.zig @@ -34,6 +34,7 @@ pub fn main() !void { testZigInitLib, testZigInitExe, testGodboltApi, + testMissingOutputPath, }; for (test_fns) |testFn| { try fs.deleteTree(a, dir_path); @@ -129,3 +130,12 @@ fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) anyerror!void { testing.expect(std.mem.indexOf(u8, out_asm, "mov\teax, edi") != null); testing.expect(std.mem.indexOf(u8, out_asm, "imul\teax, edi") != null); } + +fn testMissingOutputPath(zig_exe: []const u8, dir_path: []const u8) !void { + _ = try exec(dir_path, [_][]const u8{ zig_exe, "init-exe" }); + const output_path = try fs.path.join(a, [_][]const u8{ "does", "not", "exist" }); + const source_path = try fs.path.join(a, [_][]const u8{ "src", "main.zig" }); + _ = try exec(dir_path, [_][]const u8{ + zig_exe, "build-exe", source_path, "--output-dir", output_path + }); +} -- 2.54.0