authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-13 23:59:36-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-13 23:59:36-04:00
log66d7370facc63648eb85fb7f1b94753ab0823ff3
tree3b65163734d31ca35bdaee43d7bb95f916f39940
parent66e76a0209586000a78fe896071e73202a80b81f
signature Commit is signed but in an unrecognized format.

special case when doing build-obj with just one source file

When building an object file from only one source file, instead of having a two-stage cache system, we special case it and use the cache directory that the .o file is output to as the final cache directory for all the build artifacts. When there are more than 1 source file, the linker has to merge objects into one, and so the two stage approach makes sens. But in the case of only one source file, this prevents needlessly copying the object file. This commit fixes an issue with the previous one, where zig with cache enabled would print a directory that actually did not have any build artifacts in it.

1 files changed, 16 insertions(+), 8 deletions(-)

src/codegen.cpp+16-8
......@@ -10482,10 +10482,6 @@ static void resolve_out_paths(CodeGen *g) {
1048210482 case OutTypeUnknown:
1048310483 zig_unreachable();
1048410484 case OutTypeObj:
10485 if (g->enable_cache && g->link_objects.length == 1 && !need_llvm_module(g)) {
10486 buf_init_from_buf(&g->bin_file_output_path, g->link_objects.at(0));
10487 return;
10488 }
1048910485 if (need_llvm_module(g) && g->link_objects.length != 0 && !g->enable_cache &&
1049010486 buf_eql_buf(o_basename, out_basename))
1049110487 {
......@@ -10580,6 +10576,20 @@ static void output_type_information(CodeGen *g) {
1058010576 }
1058110577}
1058210578
10579static bool main_output_dir_is_just_one_c_object(CodeGen *g) {
10580 return g->enable_cache && g->link_objects.length == 1 && !need_llvm_module(g);
10581}
10582
10583static void init_output_dir(CodeGen *g, Buf *digest) {
10584 if (main_output_dir_is_just_one_c_object(g)) {
10585 g->output_dir = buf_alloc();
10586 os_path_dirname(g->link_objects.at(0), g->output_dir);
10587 } else {
10588 g->output_dir = buf_sprintf("%s" OS_SEP CACHE_OUT_SUBDIR OS_SEP "%s",
10589 buf_ptr(g->cache_dir), buf_ptr(digest));
10590 }
10591}
10592
1058310593void codegen_build_and_link(CodeGen *g) {
1058410594 Error err;
1058510595 assert(g->out_type != OutTypeUnknown);
......@@ -10622,8 +10632,7 @@ void codegen_build_and_link(CodeGen *g) {
1062210632 }
1062310633
1062410634 if (g->enable_cache && buf_len(&digest) != 0) {
10625 g->output_dir = buf_sprintf("%s" OS_SEP CACHE_OUT_SUBDIR OS_SEP "%s",
10626 buf_ptr(g->cache_dir), buf_ptr(&digest));
10635 init_output_dir(g, &digest);
1062710636 resolve_out_paths(g);
1062810637 } else {
1062910638 if (need_llvm_module(g)) {
......@@ -10644,8 +10653,7 @@ void codegen_build_and_link(CodeGen *g) {
1064410653 exit(1);
1064510654 }
1064610655 }
10647 g->output_dir = buf_sprintf("%s" OS_SEP CACHE_OUT_SUBDIR OS_SEP "%s",
10648 buf_ptr(g->cache_dir), buf_ptr(&digest));
10656 init_output_dir(g, &digest);
1064910657
1065010658 if ((err = os_make_path(g->output_dir))) {
1065110659 fprintf(stderr, "Unable to create output directory: %s\n", err_str(err));