authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-07 15:50:49-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-07 15:50:49-05:00
logbbbcf16ef9be53d0156312c5ecadb0b65491ba81
tree76318ec3bf9afabe09f1019ca9b0731ef1a8007a
parent2caf39c96169be8eede11f2ec9887923b650315f
signature Commit is signed but in an unrecognized format.

fix linking glibc: caching static libs and

handle linking pthread, rt, dl, m better

2 files changed, 22 insertions(+), 38 deletions(-)

src/codegen.cpp+15-22
......@@ -8283,7 +8283,7 @@ static Error get_tmp_filename(CodeGen *g, Buf *out, Buf *suffix) {
82838283}
82848284
82858285// returns true if it was a cache miss
8286static bool gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {
8286static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {
82878287 Error err;
82888288
82898289 Buf *artifact_dir;
......@@ -8501,23 +8501,16 @@ static bool gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {
85018501 os_path_join(artifact_dir, final_o_basename, o_final_path);
85028502 }
85038503
8504 if (g->enable_cache) {
8505 cache_buf(&g->cache_hash, &digest);
8506 }
8507
85088504 g->link_objects.append(o_final_path);
85098505 g->caches_to_release.append(cache_hash);
8510
8511 return is_cache_miss;
85128506}
85138507
85148508// returns true if we had any cache misses
8515static bool gen_c_objects(CodeGen *g) {
8509static void gen_c_objects(CodeGen *g) {
85168510 Error err;
8517 bool any_cache_misses = false;
85188511
85198512 if (g->c_source_files.length == 0)
8520 return any_cache_misses;
8513 return;
85218514
85228515 Buf *self_exe_path = buf_alloc();
85238516 if ((err = os_self_exe_path(self_exe_path))) {
......@@ -8529,10 +8522,8 @@ static bool gen_c_objects(CodeGen *g) {
85298522
85308523 for (size_t c_file_i = 0; c_file_i < g->c_source_files.length; c_file_i += 1) {
85318524 CFile *c_file = g->c_source_files.at(c_file_i);
8532 bool is_cache_miss = gen_c_object(g, self_exe_path, c_file);
8533 any_cache_misses = any_cache_misses || is_cache_miss;
8525 gen_c_object(g, self_exe_path, c_file);
85348526 }
8535 return any_cache_misses;
85368527}
85378528
85388529void codegen_add_object(CodeGen *g, Buf *object_path) {
......@@ -9030,7 +9021,7 @@ static void add_cache_pkg(CodeGen *g, CacheHash *ch, ZigPackage *pkg) {
90309021
90319022// Called before init()
90329023// is_cache_hit takes into account gen_c_objects
9033static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest, bool *c_objects_generated) {
9024static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
90349025 Error err;
90359026
90369027 Buf *compiler_id;
......@@ -9052,7 +9043,6 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest, bool *c_obj
90529043 cache_list_of_buf(ch, g->darwin_frameworks.items, g->darwin_frameworks.length);
90539044 cache_list_of_buf(ch, g->rpath_list.items, g->rpath_list.length);
90549045 cache_list_of_buf(ch, g->forbidden_libs.items, g->forbidden_libs.length);
9055 cache_list_of_file(ch, g->link_objects.items, g->link_objects.length);
90569046 cache_list_of_file(ch, g->assembly_files.items, g->assembly_files.length);
90579047 cache_int(ch, g->emit_file_type);
90589048 cache_int(ch, g->build_mode);
......@@ -9067,6 +9057,10 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest, bool *c_obj
90679057 cache_bool(ch, g->is_static);
90689058 cache_bool(ch, g->strip_debug_symbols);
90699059 cache_bool(ch, g->is_test_build);
9060 if (g->is_test_build) {
9061 cache_buf_opt(ch, g->test_filter);
9062 cache_buf_opt(ch, g->test_name_prefix);
9063 }
90709064 cache_bool(ch, g->is_single_threaded);
90719065 cache_bool(ch, g->linker_rdynamic);
90729066 cache_bool(ch, g->each_lib_rpath);
......@@ -9078,8 +9072,6 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest, bool *c_obj
90789072 cache_usize(ch, g->version_major);
90799073 cache_usize(ch, g->version_minor);
90809074 cache_usize(ch, g->version_patch);
9081 cache_buf_opt(ch, g->test_filter);
9082 cache_buf_opt(ch, g->test_name_prefix);
90839075 cache_list_of_str(ch, g->llvm_argv, g->llvm_argv_len);
90849076 cache_list_of_str(ch, g->clang_argv, g->clang_argv_len);
90859077 cache_list_of_str(ch, g->lib_dirs.items, g->lib_dirs.length);
......@@ -9092,7 +9084,9 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest, bool *c_obj
90929084 }
90939085 cache_buf_opt(ch, g->dynamic_linker_path);
90949086
9095 *c_objects_generated = gen_c_objects(g);
9087 // gen_c_objects appends objects to g->link_objects which we want to include in the hash
9088 gen_c_objects(g);
9089 cache_list_of_file(ch, g->link_objects.items, g->link_objects.length);
90969090
90979091 buf_resize(digest, 0);
90989092 if ((err = cache_hit(ch, digest)))
......@@ -9199,12 +9193,11 @@ void codegen_build_and_link(CodeGen *g) {
91999193
92009194 Buf *artifact_dir = buf_alloc();
92019195 Buf digest = BUF_INIT;
9202 bool any_c_objects_generated;
92039196 if (g->enable_cache) {
92049197 Buf *manifest_dir = buf_alloc();
92059198 os_path_join(g->cache_dir, buf_create_from_str("h"), manifest_dir);
92069199
9207 if ((err = check_cache(g, manifest_dir, &digest, &any_c_objects_generated))) {
9200 if ((err = check_cache(g, manifest_dir, &digest))) {
92089201 if (err == ErrorCacheUnavailable) {
92099202 // message already printed
92109203 } else if (err == ErrorNotDir) {
......@@ -9219,10 +9212,10 @@ void codegen_build_and_link(CodeGen *g) {
92199212 os_path_join(g->cache_dir, buf_create_from_str("artifact"), artifact_dir);
92209213 } else {
92219214 // There is a call to this in check_cache
9222 any_c_objects_generated = gen_c_objects(g);
9215 gen_c_objects(g);
92239216 }
92249217
9225 if (g->enable_cache && buf_len(&digest) != 0 && !any_c_objects_generated) {
9218 if (g->enable_cache && buf_len(&digest) != 0) {
92269219 os_path_join(artifact_dir, &digest, &g->artifact_dir);
92279220 resolve_out_paths(g);
92289221 } else {
src/link.cpp+7-16
......@@ -462,16 +462,6 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file) {
462462 codegen_add_object(child_gen, buf_create_from_str(init_o));
463463 codegen_build_and_link(child_gen);
464464 return buf_ptr(&child_gen->output_file_path);
465 } else if (strcmp(file, "libc.so.6") == 0) {
466 return build_dummy_so(parent, "c", 6);
467 } else if (strcmp(file, "libm.so.6") == 0) {
468 return build_dummy_so(parent, "m", 6);
469 } else if (strcmp(file, "libpthread.so.0") == 0) {
470 return build_dummy_so(parent, "pthread", 0);
471 } else if (strcmp(file, "librt.so.1") == 0) {
472 return build_dummy_so(parent, "rt", 1);
473 } else if (strcmp(file, "libdl.so.2") == 0) {
474 return build_dummy_so(parent, "dl", 2);
475465 } else if (strcmp(file, "libc_nonshared.a") == 0) {
476466 CodeGen *child_gen = create_child_codegen(parent, nullptr, OutTypeLib, nullptr);
477467 codegen_set_out_name(child_gen, buf_create_from_str("c_nonshared"));
......@@ -805,21 +795,18 @@ static void construct_linker_job_elf(LinkJob *lj) {
805795 for (size_t i = 0; i < g->link_libs_list.length; i += 1) {
806796 LinkLib *link_lib = g->link_libs_list.at(i);
807797 if (buf_eql_str(link_lib->name, "c")) {
798 // libc is linked specially
808799 continue;
809800 }
810801 if (g->libc == nullptr && target_is_glibc(g)) {
811 // glibc
802 // these libraries are always linked below when targeting glibc
812803 if (buf_eql_str(link_lib->name, "m")) {
813 lj->args.append(get_libc_crt_file(g, "libm.so.6")); // this is our dummy so file
814804 continue;
815805 } else if (buf_eql_str(link_lib->name, "pthread")) {
816 lj->args.append(get_libc_crt_file(g, "libpthread.so.0")); // this is our dummy so file
817806 continue;
818807 } else if (buf_eql_str(link_lib->name, "dl")) {
819 lj->args.append(get_libc_crt_file(g, "libdl.so.2")); // this is our dummy so file
820808 continue;
821809 } else if (buf_eql_str(link_lib->name, "rt")) {
822 lj->args.append(get_libc_crt_file(g, "librt.so.1")); // this is our dummy so file
823810 continue;
824811 }
825812 }
......@@ -857,7 +844,11 @@ static void construct_linker_job_elf(LinkJob *lj) {
857844 lj->args.append("--no-as-needed");
858845 } else if (target_is_glibc(g)) {
859846 lj->args.append(build_libunwind(g));
860 lj->args.append(get_libc_crt_file(g, "libc.so.6")); // this is our dummy so file
847 lj->args.append(build_dummy_so(g, "c", 6));
848 lj->args.append(build_dummy_so(g, "m", 6));
849 lj->args.append(build_dummy_so(g, "pthread", 0));
850 lj->args.append(build_dummy_so(g, "dl", 2));
851 lj->args.append(build_dummy_so(g, "rt", 1));
861852 lj->args.append(get_libc_crt_file(g, "libc_nonshared.a"));
862853 } else {
863854 zig_unreachable();