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) {
88158815 cache_list_of_str(ch, g->lib_dirs.items, g->lib_dirs.length);
88168816 if (g->libc) {
88178817 cache_buf(ch, &g->libc->include_dir);
8818 cache_buf(ch, &g->libc->crt_dir);
88188819 cache_buf(ch, &g->libc->lib_dir);
88198820 cache_buf(ch, &g->libc->static_lib_dir);
88208821 cache_buf(ch, &g->libc->msvc_lib_dir);
src/libc_installation.cpp+42-18
......@@ -10,10 +10,9 @@
1010#include "windows_sdk.h"
1111#include "target.hpp"
1212
13static const size_t zig_libc_keys_len = 6;
14
1513static const char *zig_libc_keys[] = {
1614 "include_dir",
15 "crt_dir",
1716 "lib_dir",
1817 "static_lib_dir",
1918 "msvc_lib_dir",
......@@ -21,6 +20,8 @@ static const char *zig_libc_keys[] = {
2120 "dynamic_linker_path",
2221};
2322
23static const size_t zig_libc_keys_len = array_length(zig_libc_keys);
24
2425static bool zig_libc_match_key(Slice<uint8_t> name, Slice<uint8_t> value, bool *found_keys,
2526 size_t index, Buf *field_ptr)
2627{
......@@ -33,6 +34,7 @@ static bool zig_libc_match_key(Slice<uint8_t> name, Slice<uint8_t> value, bool *
3334static void zig_libc_init_empty(ZigLibCInstallation *libc) {
3435 *libc = {};
3536 buf_init_from_str(&libc->include_dir, "");
37 buf_init_from_str(&libc->crt_dir, "");
3638 buf_init_from_str(&libc->lib_dir, "");
3739 buf_init_from_str(&libc->static_lib_dir, "");
3840 buf_init_from_str(&libc->msvc_lib_dir, "");
......@@ -44,7 +46,7 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget
4446 Error err;
4547 zig_libc_init_empty(libc);
4648
47 bool found_keys[6] = {}; // zig_libc_keys_len
49 bool found_keys[array_length(zig_libc_keys)] = {}; // zig_libc_keys_len
4850
4951 Buf *contents = buf_alloc();
5052 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
7476 Slice<uint8_t> value = SplitIterator_rest(&line_it);
7577 bool match = false;
7678 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);
78 match = match || zig_libc_match_key(name, value, found_keys, 2, &libc->static_lib_dir);
79 match = match || zig_libc_match_key(name, value, found_keys, 3, &libc->msvc_lib_dir);
80 match = match || zig_libc_match_key(name, value, found_keys, 4, &libc->kernel32_lib_dir);
81 match = match || zig_libc_match_key(name, value, found_keys, 5, &libc->dynamic_linker_path);
79 match = match || zig_libc_match_key(name, value, found_keys, 1, &libc->crt_dir);
80 match = match || zig_libc_match_key(name, value, found_keys, 2, &libc->lib_dir);
81 match = match || zig_libc_match_key(name, value, found_keys, 3, &libc->static_lib_dir);
82 match = match || zig_libc_match_key(name, value, found_keys, 4, &libc->msvc_lib_dir);
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);
8285 }
8386
8487 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
97100 return ErrorSemanticAnalyzeFail;
98101 }
99102
100 if (buf_len(&libc->lib_dir) == 0) {
103 if (buf_len(&libc->crt_dir) == 0) {
101104 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) {
102114 if (verbose) {
103115 fprintf(stderr, "lib_dir may not be empty for %s\n", get_target_os_name(target->os));
104116 }
......@@ -134,7 +146,7 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget
134146 }
135147
136148 if (buf_len(&libc->dynamic_linker_path) == 0) {
137 if (target->os == OsLinux) {
149 if (!target_is_darwin(target) && target->os != OsWindows) {
138150 if (verbose) {
139151 fprintf(stderr, "dynamic_linker_path may not be empty for %s\n", get_target_os_name(target->os));
140152 }
......@@ -156,11 +168,11 @@ static Error zig_libc_find_native_include_dir_windows(ZigLibCInstallation *self,
156168 }
157169 return ErrorNone;
158170}
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,
160172 bool verbose)
161173{
162174 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))) {
164176 if (verbose) {
165177 fprintf(stderr, "Unable to determine ucrt path: %s\n", err_str(err));
166178 }
......@@ -291,8 +303,11 @@ static Error zig_libc_cc_print_file_name(const char *o_file, Buf *out, bool want
291303 }
292304 return ErrorNone;
293305}
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}
294309static 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);
296311}
297312
298313static 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
328343void zig_libc_render(ZigLibCInstallation *self, FILE *file) {
329344 fprintf(file,
330345 "# 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"
332347 "include_dir=%s\n"
333348 "\n"
334349 "# 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"
336351 "# 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"
337357 "lib_dir=%s\n"
338358 "\n"
339359 "# 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"
341361 "# Not needed when targeting MacOS or Windows.\n"
342362 "static_lib_dir=%s\n"
343363 "\n"
......@@ -350,11 +370,12 @@ void zig_libc_render(ZigLibCInstallation *self, FILE *file) {
350370 "kernel32_lib_dir=%s\n"
351371 "\n"
352372 "# 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"
354374 "dynamic_linker_path=%s\n"
355375 "\n"
356376 ,
357377 buf_ptr(&self->include_dir),
378 buf_ptr(&self->crt_dir),
358379 buf_ptr(&self->lib_dir),
359380 buf_ptr(&self->static_lib_dir),
360381 buf_ptr(&self->msvc_lib_dir),
......@@ -378,7 +399,7 @@ Error zig_libc_find_native(ZigLibCInstallation *self, bool verbose) {
378399 return err;
379400 if ((err = zig_libc_find_native_include_dir_windows(self, sdk, verbose)))
380401 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)))
382403 return err;
383404 return ErrorNone;
384405 case ZigFindWindowsSdkErrorOutOfMemory:
......@@ -393,9 +414,12 @@ Error zig_libc_find_native(ZigLibCInstallation *self, bool verbose) {
393414 if ((err = zig_libc_find_native_include_dir_posix(self, verbose)))
394415 return err;
395416#if defined(ZIG_OS_FREEBSD) || defined(ZIG_OS_NETBSD)
417 buf_init_from_str(&self->crt_dir, "/usr/lib");
396418 buf_init_from_str(&self->lib_dir, "/usr/lib");
397419 buf_init_from_str(&self->static_lib_dir, "/usr/lib");
398420#elif !defined(ZIG_OS_DARWIN)
421 if ((err = zig_libc_find_native_crt_dir_posix(self, verbose)))
422 return err;
399423 if ((err = zig_libc_find_native_lib_dir_posix(self, verbose)))
400424 return err;
401425 if ((err = zig_libc_find_native_static_lib_dir_posix(self, verbose)))
src/libc_installation.hpp+1
......@@ -17,6 +17,7 @@
1717// Must be synchronized with zig_libc_keys
1818struct ZigLibCInstallation {
1919 Buf include_dir;
20 Buf crt_dir;
2021 Buf lib_dir;
2122 Buf static_lib_dir;
2223 Buf msvc_lib_dir;
src/link.cpp+12-7
......@@ -17,10 +17,10 @@ struct LinkJob {
1717 HashMap<Buf *, bool, buf_hash, buf_eql_buf> rpath_table;
1818};
1919
20static const char *get_libc_file(CodeGen *g, const char *file) {
20static const char *get_libc_crt_file(CodeGen *g, const char *file) {
2121 assert(g->libc != nullptr);
2222 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);
2424 return buf_ptr(out_buf);
2525}
2626
......@@ -224,8 +224,8 @@ static void construct_linker_job_elf(LinkJob *lj) {
224224 crt1o = "Scrt1.o";
225225 crtbegino = "crtbegin.o";
226226 }
227 lj->args.append(get_libc_file(g, crt1o));
228 lj->args.append(get_libc_file(g, "crti.o"));
227 lj->args.append(get_libc_crt_file(g, crt1o));
228 lj->args.append(get_libc_crt_file(g, "crti.o"));
229229 lj->args.append(get_libc_static_file(g, crtbegino));
230230 }
231231
......@@ -263,7 +263,12 @@ static void construct_linker_job_elf(LinkJob *lj) {
263263 if (g->libc_link_lib != nullptr) {
264264 assert(g->libc != nullptr);
265265 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
268273 lj->args.append("-L");
269274 lj->args.append(buf_ptr(&g->libc->static_lib_dir));
......@@ -340,7 +345,7 @@ static void construct_linker_job_elf(LinkJob *lj) {
340345 // crt end
341346 if (lj->link_in_crt) {
342347 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"));
344349 }
345350
346351 if (!g->zig_target->is_native) {
......@@ -597,7 +602,7 @@ static void construct_linker_job_coff(LinkJob *lj) {
597602
598603 lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(&g->libc->msvc_lib_dir))));
599604 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))));
601606 }
602607
603608 if (is_library && !g->is_static) {