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) {...@@ -8283,7 +8283,7 @@ static Error get_tmp_filename(CodeGen *g, Buf *out, Buf *suffix) {
8283}8283}
82848284
8285// returns true if it was a cache miss8285// 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) {
8287 Error err;8287 Error err;
82888288
8289 Buf *artifact_dir;8289 Buf *artifact_dir;
...@@ -8501,23 +8501,16 @@ static bool gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {...@@ -8501,23 +8501,16 @@ static bool gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {
8501 os_path_join(artifact_dir, final_o_basename, o_final_path);8501 os_path_join(artifact_dir, final_o_basename, o_final_path);
8502 }8502 }
85038503
8504 if (g->enable_cache) {
8505 cache_buf(&g->cache_hash, &digest);
8506 }
8507
8508 g->link_objects.append(o_final_path);8504 g->link_objects.append(o_final_path);
8509 g->caches_to_release.append(cache_hash);8505 g->caches_to_release.append(cache_hash);
8510
8511 return is_cache_miss;
8512}8506}
85138507
8514// returns true if we had any cache misses8508// returns true if we had any cache misses
8515static bool gen_c_objects(CodeGen *g) {8509static void gen_c_objects(CodeGen *g) {
8516 Error err;8510 Error err;
8517 bool any_cache_misses = false;
85188511
8519 if (g->c_source_files.length == 0)8512 if (g->c_source_files.length == 0)
8520 return any_cache_misses;8513 return;
85218514
8522 Buf *self_exe_path = buf_alloc();8515 Buf *self_exe_path = buf_alloc();
8523 if ((err = os_self_exe_path(self_exe_path))) {8516 if ((err = os_self_exe_path(self_exe_path))) {
...@@ -8529,10 +8522,8 @@ static bool gen_c_objects(CodeGen *g) {...@@ -8529,10 +8522,8 @@ static bool gen_c_objects(CodeGen *g) {
85298522
8530 for (size_t c_file_i = 0; c_file_i < g->c_source_files.length; c_file_i += 1) {8523 for (size_t c_file_i = 0; c_file_i < g->c_source_files.length; c_file_i += 1) {
8531 CFile *c_file = g->c_source_files.at(c_file_i);8524 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);8525 gen_c_object(g, self_exe_path, c_file);
8533 any_cache_misses = any_cache_misses || is_cache_miss;
8534 }8526 }
8535 return any_cache_misses;
8536}8527}
85378528
8538void codegen_add_object(CodeGen *g, Buf *object_path) {8529void codegen_add_object(CodeGen *g, Buf *object_path) {
...@@ -9030,7 +9021,7 @@ static void add_cache_pkg(CodeGen *g, CacheHash *ch, ZigPackage *pkg) {...@@ -9030,7 +9021,7 @@ static void add_cache_pkg(CodeGen *g, CacheHash *ch, ZigPackage *pkg) {
90309021
9031// Called before init()9022// Called before init()
9032// is_cache_hit takes into account gen_c_objects9023// 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) {
9034 Error err;9025 Error err;
90359026
9036 Buf *compiler_id;9027 Buf *compiler_id;
...@@ -9052,7 +9043,6 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest, bool *c_obj...@@ -9052,7 +9043,6 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest, bool *c_obj
9052 cache_list_of_buf(ch, g->darwin_frameworks.items, g->darwin_frameworks.length);9043 cache_list_of_buf(ch, g->darwin_frameworks.items, g->darwin_frameworks.length);
9053 cache_list_of_buf(ch, g->rpath_list.items, g->rpath_list.length);9044 cache_list_of_buf(ch, g->rpath_list.items, g->rpath_list.length);
9054 cache_list_of_buf(ch, g->forbidden_libs.items, g->forbidden_libs.length);9045 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);
9056 cache_list_of_file(ch, g->assembly_files.items, g->assembly_files.length);9046 cache_list_of_file(ch, g->assembly_files.items, g->assembly_files.length);
9057 cache_int(ch, g->emit_file_type);9047 cache_int(ch, g->emit_file_type);
9058 cache_int(ch, g->build_mode);9048 cache_int(ch, g->build_mode);
...@@ -9067,6 +9057,10 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest, bool *c_obj...@@ -9067,6 +9057,10 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest, bool *c_obj
9067 cache_bool(ch, g->is_static);9057 cache_bool(ch, g->is_static);
9068 cache_bool(ch, g->strip_debug_symbols);9058 cache_bool(ch, g->strip_debug_symbols);
9069 cache_bool(ch, g->is_test_build);9059 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 }
9070 cache_bool(ch, g->is_single_threaded);9064 cache_bool(ch, g->is_single_threaded);
9071 cache_bool(ch, g->linker_rdynamic);9065 cache_bool(ch, g->linker_rdynamic);
9072 cache_bool(ch, g->each_lib_rpath);9066 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...@@ -9078,8 +9072,6 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest, bool *c_obj
9078 cache_usize(ch, g->version_major);9072 cache_usize(ch, g->version_major);
9079 cache_usize(ch, g->version_minor);9073 cache_usize(ch, g->version_minor);
9080 cache_usize(ch, g->version_patch);9074 cache_usize(ch, g->version_patch);
9081 cache_buf_opt(ch, g->test_filter);
9082 cache_buf_opt(ch, g->test_name_prefix);
9083 cache_list_of_str(ch, g->llvm_argv, g->llvm_argv_len);9075 cache_list_of_str(ch, g->llvm_argv, g->llvm_argv_len);
9084 cache_list_of_str(ch, g->clang_argv, g->clang_argv_len);9076 cache_list_of_str(ch, g->clang_argv, g->clang_argv_len);
9085 cache_list_of_str(ch, g->lib_dirs.items, g->lib_dirs.length);9077 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...@@ -9092,7 +9084,9 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest, bool *c_obj
9092 }9084 }
9093 cache_buf_opt(ch, g->dynamic_linker_path);9085 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
9097 buf_resize(digest, 0);9091 buf_resize(digest, 0);
9098 if ((err = cache_hit(ch, digest)))9092 if ((err = cache_hit(ch, digest)))
...@@ -9199,12 +9193,11 @@ void codegen_build_and_link(CodeGen *g) {...@@ -9199,12 +9193,11 @@ void codegen_build_and_link(CodeGen *g) {
91999193
9200 Buf *artifact_dir = buf_alloc();9194 Buf *artifact_dir = buf_alloc();
9201 Buf digest = BUF_INIT;9195 Buf digest = BUF_INIT;
9202 bool any_c_objects_generated;
9203 if (g->enable_cache) {9196 if (g->enable_cache) {
9204 Buf *manifest_dir = buf_alloc();9197 Buf *manifest_dir = buf_alloc();
9205 os_path_join(g->cache_dir, buf_create_from_str("h"), manifest_dir);9198 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))) {
9208 if (err == ErrorCacheUnavailable) {9201 if (err == ErrorCacheUnavailable) {
9209 // message already printed9202 // message already printed
9210 } else if (err == ErrorNotDir) {9203 } else if (err == ErrorNotDir) {
...@@ -9219,10 +9212,10 @@ void codegen_build_and_link(CodeGen *g) {...@@ -9219,10 +9212,10 @@ void codegen_build_and_link(CodeGen *g) {
9219 os_path_join(g->cache_dir, buf_create_from_str("artifact"), artifact_dir);9212 os_path_join(g->cache_dir, buf_create_from_str("artifact"), artifact_dir);
9220 } else {9213 } else {
9221 // There is a call to this in check_cache9214 // There is a call to this in check_cache
9222 any_c_objects_generated = gen_c_objects(g);9215 gen_c_objects(g);
9223 }9216 }
92249217
9225 if (g->enable_cache && buf_len(&digest) != 0 && !any_c_objects_generated) {9218 if (g->enable_cache && buf_len(&digest) != 0) {
9226 os_path_join(artifact_dir, &digest, &g->artifact_dir);9219 os_path_join(artifact_dir, &digest, &g->artifact_dir);
9227 resolve_out_paths(g);9220 resolve_out_paths(g);
9228 } else {9221 } else {
src/link.cpp+7-16
...@@ -462,16 +462,6 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file) {...@@ -462,16 +462,6 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file) {
462 codegen_add_object(child_gen, buf_create_from_str(init_o));462 codegen_add_object(child_gen, buf_create_from_str(init_o));
463 codegen_build_and_link(child_gen);463 codegen_build_and_link(child_gen);
464 return buf_ptr(&child_gen->output_file_path);464 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);
475 } else if (strcmp(file, "libc_nonshared.a") == 0) {465 } else if (strcmp(file, "libc_nonshared.a") == 0) {
476 CodeGen *child_gen = create_child_codegen(parent, nullptr, OutTypeLib, nullptr);466 CodeGen *child_gen = create_child_codegen(parent, nullptr, OutTypeLib, nullptr);
477 codegen_set_out_name(child_gen, buf_create_from_str("c_nonshared"));467 codegen_set_out_name(child_gen, buf_create_from_str("c_nonshared"));
...@@ -805,21 +795,18 @@ static void construct_linker_job_elf(LinkJob *lj) {...@@ -805,21 +795,18 @@ static void construct_linker_job_elf(LinkJob *lj) {
805 for (size_t i = 0; i < g->link_libs_list.length; i += 1) {795 for (size_t i = 0; i < g->link_libs_list.length; i += 1) {
806 LinkLib *link_lib = g->link_libs_list.at(i);796 LinkLib *link_lib = g->link_libs_list.at(i);
807 if (buf_eql_str(link_lib->name, "c")) {797 if (buf_eql_str(link_lib->name, "c")) {
798 // libc is linked specially
808 continue;799 continue;
809 }800 }
810 if (g->libc == nullptr && target_is_glibc(g)) {801 if (g->libc == nullptr && target_is_glibc(g)) {
811 // glibc802 // these libraries are always linked below when targeting glibc
812 if (buf_eql_str(link_lib->name, "m")) {803 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
814 continue;804 continue;
815 } else if (buf_eql_str(link_lib->name, "pthread")) {805 } 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
817 continue;806 continue;
818 } else if (buf_eql_str(link_lib->name, "dl")) {807 } 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
820 continue;808 continue;
821 } else if (buf_eql_str(link_lib->name, "rt")) {809 } 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
823 continue;810 continue;
824 }811 }
825 }812 }
...@@ -857,7 +844,11 @@ static void construct_linker_job_elf(LinkJob *lj) {...@@ -857,7 +844,11 @@ static void construct_linker_job_elf(LinkJob *lj) {
857 lj->args.append("--no-as-needed");844 lj->args.append("--no-as-needed");
858 } else if (target_is_glibc(g)) {845 } else if (target_is_glibc(g)) {
859 lj->args.append(build_libunwind(g));846 lj->args.append(build_libunwind(g));
860 lj->args.append(get_libc_crt_file(g, "libc.so.6")); // this is our dummy so file847 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));
861 lj->args.append(get_libc_crt_file(g, "libc_nonshared.a"));852 lj->args.append(get_libc_crt_file(g, "libc_nonshared.a"));
862 } else {853 } else {
863 zig_unreachable();854 zig_unreachable();