authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-25 19:10:48-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-25 19:10:48-05:00
log0f54728cf0e282ca6578bb8a55df3409541c1a7f
treef877dea324eb7a39a1797eb107505e5a09a7d342
parentaab8e13529cc558e0be48c57fda0e22b67b9be39
signaturelock-open Commit is signed but in an unrecognized format.

fix not finding libgcc_s when looking for native libc

closes #2011

4 files changed, 56 insertions(+), 25 deletions(-)

src/codegen.cpp+1
...@@ -8815,6 +8815,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {...@@ -8815,6 +8815,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
8815 cache_list_of_str(ch, g->lib_dirs.items, g->lib_dirs.length);8815 cache_list_of_str(ch, g->lib_dirs.items, g->lib_dirs.length);
8816 if (g->libc) {8816 if (g->libc) {
8817 cache_buf(ch, &g->libc->include_dir);8817 cache_buf(ch, &g->libc->include_dir);
8818 cache_buf(ch, &g->libc->crt_dir);
8818 cache_buf(ch, &g->libc->lib_dir);8819 cache_buf(ch, &g->libc->lib_dir);
8819 cache_buf(ch, &g->libc->static_lib_dir);8820 cache_buf(ch, &g->libc->static_lib_dir);
8820 cache_buf(ch, &g->libc->msvc_lib_dir);8821 cache_buf(ch, &g->libc->msvc_lib_dir);
src/libc_installation.cpp+42-18
...@@ -10,10 +10,9 @@...@@ -10,10 +10,9 @@
10#include "windows_sdk.h"10#include "windows_sdk.h"
11#include "target.hpp"11#include "target.hpp"
1212
13static const size_t zig_libc_keys_len = 6;
14
15static const char *zig_libc_keys[] = {13static const char *zig_libc_keys[] = {
16 "include_dir",14 "include_dir",
15 "crt_dir",
17 "lib_dir",16 "lib_dir",
18 "static_lib_dir",17 "static_lib_dir",
19 "msvc_lib_dir",18 "msvc_lib_dir",
...@@ -21,6 +20,8 @@ static const char *zig_libc_keys[] = {...@@ -21,6 +20,8 @@ static const char *zig_libc_keys[] = {
21 "dynamic_linker_path",20 "dynamic_linker_path",
22};21};
2322
23static const size_t zig_libc_keys_len = array_length(zig_libc_keys);
24
24static bool zig_libc_match_key(Slice<uint8_t> name, Slice<uint8_t> value, bool *found_keys,25static bool zig_libc_match_key(Slice<uint8_t> name, Slice<uint8_t> value, bool *found_keys,
25 size_t index, Buf *field_ptr)26 size_t index, Buf *field_ptr)
26{27{
...@@ -33,6 +34,7 @@ static bool zig_libc_match_key(Slice<uint8_t> name, Slice<uint8_t> value, bool *...@@ -33,6 +34,7 @@ static bool zig_libc_match_key(Slice<uint8_t> name, Slice<uint8_t> value, bool *
33static void zig_libc_init_empty(ZigLibCInstallation *libc) {34static void zig_libc_init_empty(ZigLibCInstallation *libc) {
34 *libc = {};35 *libc = {};
35 buf_init_from_str(&libc->include_dir, "");36 buf_init_from_str(&libc->include_dir, "");
37 buf_init_from_str(&libc->crt_dir, "");
36 buf_init_from_str(&libc->lib_dir, "");38 buf_init_from_str(&libc->lib_dir, "");
37 buf_init_from_str(&libc->static_lib_dir, "");39 buf_init_from_str(&libc->static_lib_dir, "");
38 buf_init_from_str(&libc->msvc_lib_dir, "");40 buf_init_from_str(&libc->msvc_lib_dir, "");
...@@ -44,7 +46,7 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget...@@ -44,7 +46,7 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget
44 Error err;46 Error err;
45 zig_libc_init_empty(libc);47 zig_libc_init_empty(libc);
4648
47 bool found_keys[6] = {}; // zig_libc_keys_len49 bool found_keys[array_length(zig_libc_keys)] = {}; // zig_libc_keys_len
4850
49 Buf *contents = buf_alloc();51 Buf *contents = buf_alloc();
50 if ((err = os_fetch_file_path(libc_file, contents, false))) {52 if ((err = os_fetch_file_path(libc_file, contents, false))) {
...@@ -74,11 +76,12 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget...@@ -74,11 +76,12 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget
74 Slice<uint8_t> value = SplitIterator_rest(&line_it);76 Slice<uint8_t> value = SplitIterator_rest(&line_it);
75 bool match = false;77 bool match = false;
76 match = match || zig_libc_match_key(name, value, found_keys, 0, &libc->include_dir);78 match = match || zig_libc_match_key(name, value, found_keys, 0, &libc->include_dir);
77 match = match || zig_libc_match_key(name, value, found_keys, 1, &libc->lib_dir);79 match = match || zig_libc_match_key(name, value, found_keys, 1, &libc->crt_dir);
78 match = match || zig_libc_match_key(name, value, found_keys, 2, &libc->static_lib_dir);80 match = match || zig_libc_match_key(name, value, found_keys, 2, &libc->lib_dir);
79 match = match || zig_libc_match_key(name, value, found_keys, 3, &libc->msvc_lib_dir);81 match = match || zig_libc_match_key(name, value, found_keys, 3, &libc->static_lib_dir);
80 match = match || zig_libc_match_key(name, value, found_keys, 4, &libc->kernel32_lib_dir);82 match = match || zig_libc_match_key(name, value, found_keys, 4, &libc->msvc_lib_dir);
81 match = match || zig_libc_match_key(name, value, found_keys, 5, &libc->dynamic_linker_path);83 match = match || zig_libc_match_key(name, value, found_keys, 5, &libc->kernel32_lib_dir);
84 match = match || zig_libc_match_key(name, value, found_keys, 6, &libc->dynamic_linker_path);
82 }85 }
8386
84 for (size_t i = 0; i < zig_libc_keys_len; i += 1) {87 for (size_t i = 0; i < zig_libc_keys_len; i += 1) {
...@@ -97,8 +100,17 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget...@@ -97,8 +100,17 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget
97 return ErrorSemanticAnalyzeFail;100 return ErrorSemanticAnalyzeFail;
98 }101 }
99102
100 if (buf_len(&libc->lib_dir) == 0) {103 if (buf_len(&libc->crt_dir) == 0) {
101 if (!target_is_darwin(target)) {104 if (!target_is_darwin(target)) {
105 if (verbose) {
106 fprintf(stderr, "crt_dir may not be empty for %s\n", get_target_os_name(target->os));
107 }
108 return ErrorSemanticAnalyzeFail;
109 }
110 }
111
112 if (buf_len(&libc->lib_dir) == 0) {
113 if (!target_is_darwin(target) && target->os != OsWindows) {
102 if (verbose) {114 if (verbose) {
103 fprintf(stderr, "lib_dir may not be empty for %s\n", get_target_os_name(target->os));115 fprintf(stderr, "lib_dir may not be empty for %s\n", get_target_os_name(target->os));
104 }116 }
...@@ -134,7 +146,7 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget...@@ -134,7 +146,7 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget
134 }146 }
135147
136 if (buf_len(&libc->dynamic_linker_path) == 0) {148 if (buf_len(&libc->dynamic_linker_path) == 0) {
137 if (target->os == OsLinux) {149 if (!target_is_darwin(target) && target->os != OsWindows) {
138 if (verbose) {150 if (verbose) {
139 fprintf(stderr, "dynamic_linker_path may not be empty for %s\n", get_target_os_name(target->os));151 fprintf(stderr, "dynamic_linker_path may not be empty for %s\n", get_target_os_name(target->os));
140 }152 }
...@@ -156,11 +168,11 @@ static Error zig_libc_find_native_include_dir_windows(ZigLibCInstallation *self,...@@ -156,11 +168,11 @@ static Error zig_libc_find_native_include_dir_windows(ZigLibCInstallation *self,
156 }168 }
157 return ErrorNone;169 return ErrorNone;
158}170}
159static Error zig_libc_find_lib_dir_windows(ZigLibCInstallation *self, ZigWindowsSDK *sdk, ZigTarget *target,171static Error zig_libc_find_crt_dir_windows(ZigLibCInstallation *self, ZigWindowsSDK *sdk, ZigTarget *target,
160 bool verbose)172 bool verbose)
161{173{
162 Error err;174 Error err;
163 if ((err = os_get_win32_ucrt_lib_path(sdk, &self->lib_dir, target->arch.arch))) {175 if ((err = os_get_win32_ucrt_lib_path(sdk, &self->crt_dir, target->arch.arch))) {
164 if (verbose) {176 if (verbose) {
165 fprintf(stderr, "Unable to determine ucrt path: %s\n", err_str(err));177 fprintf(stderr, "Unable to determine ucrt path: %s\n", err_str(err));
166 }178 }
...@@ -291,8 +303,11 @@ static Error zig_libc_cc_print_file_name(const char *o_file, Buf *out, bool want...@@ -291,8 +303,11 @@ static Error zig_libc_cc_print_file_name(const char *o_file, Buf *out, bool want
291 }303 }
292 return ErrorNone;304 return ErrorNone;
293}305}
306static Error zig_libc_find_native_crt_dir_posix(ZigLibCInstallation *self, bool verbose) {
307 return zig_libc_cc_print_file_name("crt1.o", &self->crt_dir, true, verbose);
308}
294static Error zig_libc_find_native_lib_dir_posix(ZigLibCInstallation *self, bool verbose) {309static Error zig_libc_find_native_lib_dir_posix(ZigLibCInstallation *self, bool verbose) {
295 return zig_libc_cc_print_file_name("crt1.o", &self->lib_dir, true, verbose);310 return zig_libc_cc_print_file_name("libgcc_s.so", &self->lib_dir, true, verbose);
296}311}
297312
298static Error zig_libc_find_native_static_lib_dir_posix(ZigLibCInstallation *self, bool verbose) {313static Error zig_libc_find_native_static_lib_dir_posix(ZigLibCInstallation *self, bool verbose) {
...@@ -328,16 +343,21 @@ static Error zig_libc_find_native_dynamic_linker_posix(ZigLibCInstallation *self...@@ -328,16 +343,21 @@ static Error zig_libc_find_native_dynamic_linker_posix(ZigLibCInstallation *self
328void zig_libc_render(ZigLibCInstallation *self, FILE *file) {343void zig_libc_render(ZigLibCInstallation *self, FILE *file) {
329 fprintf(file,344 fprintf(file,
330 "# The directory that contains `stdlib.h`.\n"345 "# The directory that contains `stdlib.h`.\n"
331 "# On Linux, can be found with: `cc -E -Wp,-v -xc /dev/null`\n"346 "# On POSIX, can be found with: `cc -E -Wp,-v -xc /dev/null`\n"
332 "include_dir=%s\n"347 "include_dir=%s\n"
333 "\n"348 "\n"
334 "# The directory that contains `crt1.o`.\n"349 "# The directory that contains `crt1.o`.\n"
335 "# On Linux, can be found with `cc -print-file-name=crt1.o`.\n"350 "# On POSIX, can be found with `cc -print-file-name=crt1.o`.\n"
336 "# Not needed when targeting MacOS.\n"351 "# Not needed when targeting MacOS.\n"
352 "crt_dir=%s\n"
353 "\n"
354 "# The directory that contains `libgcc_s.so`.\n"
355 "# On POSIX, can be found with `cc -print-file-name=libgcc_s.so`.\n"
356 "# Not needed when targeting MacOS or Windows.\n"
337 "lib_dir=%s\n"357 "lib_dir=%s\n"
338 "\n"358 "\n"
339 "# The directory that contains `crtbegin.o`.\n"359 "# The directory that contains `crtbegin.o`.\n"
340 "# On Linux, can be found with `cc -print-file-name=crtbegin.o`.\n"360 "# On POSIX, can be found with `cc -print-file-name=crtbegin.o`.\n"
341 "# Not needed when targeting MacOS or Windows.\n"361 "# Not needed when targeting MacOS or Windows.\n"
342 "static_lib_dir=%s\n"362 "static_lib_dir=%s\n"
343 "\n"363 "\n"
...@@ -350,11 +370,12 @@ void zig_libc_render(ZigLibCInstallation *self, FILE *file) {...@@ -350,11 +370,12 @@ void zig_libc_render(ZigLibCInstallation *self, FILE *file) {
350 "kernel32_lib_dir=%s\n"370 "kernel32_lib_dir=%s\n"
351 "\n"371 "\n"
352 "# The full path to the dynamic linker, on the target system.\n"372 "# The full path to the dynamic linker, on the target system.\n"
353 "# Only needed when targeting Linux.\n"373 "# Not needed when targeting MacOS or Windows.\n"
354 "dynamic_linker_path=%s\n"374 "dynamic_linker_path=%s\n"
355 "\n"375 "\n"
356 ,376 ,
357 buf_ptr(&self->include_dir),377 buf_ptr(&self->include_dir),
378 buf_ptr(&self->crt_dir),
358 buf_ptr(&self->lib_dir),379 buf_ptr(&self->lib_dir),
359 buf_ptr(&self->static_lib_dir),380 buf_ptr(&self->static_lib_dir),
360 buf_ptr(&self->msvc_lib_dir),381 buf_ptr(&self->msvc_lib_dir),
...@@ -378,7 +399,7 @@ Error zig_libc_find_native(ZigLibCInstallation *self, bool verbose) {...@@ -378,7 +399,7 @@ Error zig_libc_find_native(ZigLibCInstallation *self, bool verbose) {
378 return err;399 return err;
379 if ((err = zig_libc_find_native_include_dir_windows(self, sdk, verbose)))400 if ((err = zig_libc_find_native_include_dir_windows(self, sdk, verbose)))
380 return err;401 return err;
381 if ((err = zig_libc_find_lib_dir_windows(self, sdk, &native_target, verbose)))402 if ((err = zig_libc_find_crt_dir_windows(self, sdk, &native_target, verbose)))
382 return err;403 return err;
383 return ErrorNone;404 return ErrorNone;
384 case ZigFindWindowsSdkErrorOutOfMemory:405 case ZigFindWindowsSdkErrorOutOfMemory:
...@@ -393,9 +414,12 @@ Error zig_libc_find_native(ZigLibCInstallation *self, bool verbose) {...@@ -393,9 +414,12 @@ Error zig_libc_find_native(ZigLibCInstallation *self, bool verbose) {
393 if ((err = zig_libc_find_native_include_dir_posix(self, verbose)))414 if ((err = zig_libc_find_native_include_dir_posix(self, verbose)))
394 return err;415 return err;
395#if defined(ZIG_OS_FREEBSD) || defined(ZIG_OS_NETBSD)416#if defined(ZIG_OS_FREEBSD) || defined(ZIG_OS_NETBSD)
417 buf_init_from_str(&self->crt_dir, "/usr/lib");
396 buf_init_from_str(&self->lib_dir, "/usr/lib");418 buf_init_from_str(&self->lib_dir, "/usr/lib");
397 buf_init_from_str(&self->static_lib_dir, "/usr/lib");419 buf_init_from_str(&self->static_lib_dir, "/usr/lib");
398#elif !defined(ZIG_OS_DARWIN)420#elif !defined(ZIG_OS_DARWIN)
421 if ((err = zig_libc_find_native_crt_dir_posix(self, verbose)))
422 return err;
399 if ((err = zig_libc_find_native_lib_dir_posix(self, verbose)))423 if ((err = zig_libc_find_native_lib_dir_posix(self, verbose)))
400 return err;424 return err;
401 if ((err = zig_libc_find_native_static_lib_dir_posix(self, verbose)))425 if ((err = zig_libc_find_native_static_lib_dir_posix(self, verbose)))
src/libc_installation.hpp+1
...@@ -17,6 +17,7 @@...@@ -17,6 +17,7 @@
17// Must be synchronized with zig_libc_keys17// Must be synchronized with zig_libc_keys
18struct ZigLibCInstallation {18struct ZigLibCInstallation {
19 Buf include_dir;19 Buf include_dir;
20 Buf crt_dir;
20 Buf lib_dir;21 Buf lib_dir;
21 Buf static_lib_dir;22 Buf static_lib_dir;
22 Buf msvc_lib_dir;23 Buf msvc_lib_dir;
src/link.cpp+12-7
...@@ -17,10 +17,10 @@ struct LinkJob {...@@ -17,10 +17,10 @@ struct LinkJob {
17 HashMap<Buf *, bool, buf_hash, buf_eql_buf> rpath_table;17 HashMap<Buf *, bool, buf_hash, buf_eql_buf> rpath_table;
18};18};
1919
20static const char *get_libc_file(CodeGen *g, const char *file) {20static const char *get_libc_crt_file(CodeGen *g, const char *file) {
21 assert(g->libc != nullptr);21 assert(g->libc != nullptr);
22 Buf *out_buf = buf_alloc();22 Buf *out_buf = buf_alloc();
23 os_path_join(&g->libc->lib_dir, buf_create_from_str(file), out_buf);23 os_path_join(&g->libc->crt_dir, buf_create_from_str(file), out_buf);
24 return buf_ptr(out_buf);24 return buf_ptr(out_buf);
25}25}
2626
...@@ -224,8 +224,8 @@ static void construct_linker_job_elf(LinkJob *lj) {...@@ -224,8 +224,8 @@ static void construct_linker_job_elf(LinkJob *lj) {
224 crt1o = "Scrt1.o";224 crt1o = "Scrt1.o";
225 crtbegino = "crtbegin.o";225 crtbegino = "crtbegin.o";
226 }226 }
227 lj->args.append(get_libc_file(g, crt1o));227 lj->args.append(get_libc_crt_file(g, crt1o));
228 lj->args.append(get_libc_file(g, "crti.o"));228 lj->args.append(get_libc_crt_file(g, "crti.o"));
229 lj->args.append(get_libc_static_file(g, crtbegino));229 lj->args.append(get_libc_static_file(g, crtbegino));
230 }230 }
231231
...@@ -263,7 +263,12 @@ static void construct_linker_job_elf(LinkJob *lj) {...@@ -263,7 +263,12 @@ static void construct_linker_job_elf(LinkJob *lj) {
263 if (g->libc_link_lib != nullptr) {263 if (g->libc_link_lib != nullptr) {
264 assert(g->libc != nullptr);264 assert(g->libc != nullptr);
265 lj->args.append("-L");265 lj->args.append("-L");
266 lj->args.append(buf_ptr(&g->libc->lib_dir));266 lj->args.append(buf_ptr(&g->libc->crt_dir));
267
268 if (!buf_eql_buf(&g->libc->crt_dir, &g->libc->lib_dir)) {
269 lj->args.append("-L");
270 lj->args.append(buf_ptr(&g->libc->lib_dir));
271 }
267272
268 lj->args.append("-L");273 lj->args.append("-L");
269 lj->args.append(buf_ptr(&g->libc->static_lib_dir));274 lj->args.append(buf_ptr(&g->libc->static_lib_dir));
...@@ -340,7 +345,7 @@ static void construct_linker_job_elf(LinkJob *lj) {...@@ -340,7 +345,7 @@ static void construct_linker_job_elf(LinkJob *lj) {
340 // crt end345 // crt end
341 if (lj->link_in_crt) {346 if (lj->link_in_crt) {
342 lj->args.append(get_libc_static_file(g, "crtend.o"));347 lj->args.append(get_libc_static_file(g, "crtend.o"));
343 lj->args.append(get_libc_file(g, "crtn.o"));348 lj->args.append(get_libc_crt_file(g, "crtn.o"));
344 }349 }
345350
346 if (!g->zig_target->is_native) {351 if (!g->zig_target->is_native) {
...@@ -597,7 +602,7 @@ static void construct_linker_job_coff(LinkJob *lj) {...@@ -597,7 +602,7 @@ static void construct_linker_job_coff(LinkJob *lj) {
597602
598 lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(&g->libc->msvc_lib_dir))));603 lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(&g->libc->msvc_lib_dir))));
599 lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(&g->libc->kernel32_lib_dir))));604 lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(&g->libc->kernel32_lib_dir))));
600 lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(&g->libc->lib_dir))));605 lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(&g->libc->crt_dir))));
601 }606 }
602607
603 if (is_library && !g->is_static) {608 if (is_library && !g->is_static) {