authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-15 21:47:15-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-15 21:50:56-04:00
log6b36b756eb4b4c26b98d405310680ebe5679d111
tree372ff21670a553664b443e08aae5601187d51bf4
parentb64e6cb8130ca0ef7deca2191a8312edc7f2ce41
signaturelock-open Commit is signed but in an unrecognized format.

fix static builds of zig from requiring c compiler

to be installed when linking libc. When zig links against libc, it requires a dynamic linker path. Usually this can be determined based on the architecture and operating system components of the target. However on some systems this is not correct; because of this zig checks its own dynamic linker. When zig is statically linked, this information is not available, and so it resorts to using cc -print-filename=foo to find the dynamic linker path. Before this commit, Zig incorrectly exited with an error if there was no c compiler installed. Now, Zig falls back to the dynamic linker determined based on the arch and os when no C compiler can be found.

6 files changed, 20 insertions(+), 5 deletions(-)

src/codegen.cpp+1-1
...@@ -8121,7 +8121,7 @@ static void detect_dynamic_linker(CodeGen *g) {...@@ -8121,7 +8121,7 @@ static void detect_dynamic_linker(CodeGen *g) {
8121 for (size_t i = 0; possible_ld_names[i] != NULL; i += 1) {8121 for (size_t i = 0; possible_ld_names[i] != NULL; i += 1) {
8122 const char *lib_name = possible_ld_names[i];8122 const char *lib_name = possible_ld_names[i];
8123 if ((err = zig_libc_cc_print_file_name(lib_name, result, false, true))) {8123 if ((err = zig_libc_cc_print_file_name(lib_name, result, false, true))) {
8124 if (err != ErrorCCompilerCannotFindFile) {8124 if (err != ErrorCCompilerCannotFindFile && err != ErrorNoCCompilerInstalled) {
8125 fprintf(stderr, "Unable to detect native dynamic linker: %s\n", err_str(err));8125 fprintf(stderr, "Unable to detect native dynamic linker: %s\n", err_str(err));
8126 exit(1);8126 exit(1);
8127 }8127 }
src/error.cpp+1
...@@ -54,6 +54,7 @@ const char *err_str(Error err) {...@@ -54,6 +54,7 @@ const char *err_str(Error err) {
54 case ErrorOperationAborted: return "operation aborted";54 case ErrorOperationAborted: return "operation aborted";
55 case ErrorBrokenPipe: return "broken pipe";55 case ErrorBrokenPipe: return "broken pipe";
56 case ErrorNoSpaceLeft: return "no space left";56 case ErrorNoSpaceLeft: return "no space left";
57 case ErrorNoCCompilerInstalled: return "no C compiler installed";
57 }58 }
58 return "(invalid error)";59 return "(invalid error)";
59}60}
src/libc_installation.cpp+2
...@@ -283,6 +283,8 @@ Error zig_libc_cc_print_file_name(const char *o_file, Buf *out, bool want_dirnam...@@ -283,6 +283,8 @@ Error zig_libc_cc_print_file_name(const char *o_file, Buf *out, bool want_dirnam
283 Buf *out_stdout = buf_alloc();283 Buf *out_stdout = buf_alloc();
284 Error err;284 Error err;
285 if ((err = os_exec_process(cc_exe, args, &term, out_stderr, out_stdout))) {285 if ((err = os_exec_process(cc_exe, args, &term, out_stderr, out_stdout))) {
286 if (err == ErrorFileNotFound)
287 return ErrorNoCCompilerInstalled;
286 if (verbose) {288 if (verbose) {
287 fprintf(stderr, "unable to determine libc library path: executing '%s': %s\n", cc_exe, err_str(err));289 fprintf(stderr, "unable to determine libc library path: executing '%s': %s\n", cc_exe, err_str(err));
288 }290 }
src/link.cpp+1
...@@ -34,6 +34,7 @@ static CodeGen *create_child_codegen(CodeGen *parent_gen, Buf *root_src_path, Ou...@@ -34,6 +34,7 @@ static CodeGen *create_child_codegen(CodeGen *parent_gen, Buf *root_src_path, Ou
34 child_gen->verbose_cimport = parent_gen->verbose_cimport;34 child_gen->verbose_cimport = parent_gen->verbose_cimport;
35 child_gen->verbose_cc = parent_gen->verbose_cc;35 child_gen->verbose_cc = parent_gen->verbose_cc;
36 child_gen->llvm_argv = parent_gen->llvm_argv;36 child_gen->llvm_argv = parent_gen->llvm_argv;
37 child_gen->dynamic_linker_path = parent_gen->dynamic_linker_path;
3738
38 codegen_set_strip(child_gen, parent_gen->strip_debug_symbols);39 codegen_set_strip(child_gen, parent_gen->strip_debug_symbols);
39 child_gen->want_pic = parent_gen->have_pic ? WantPICEnabled : WantPICDisabled;40 child_gen->want_pic = parent_gen->have_pic ? WantPICEnabled : WantPICDisabled;
src/os.cpp+14-4
...@@ -791,6 +791,7 @@ static Error os_exec_process_posix(const char *exe, ZigList<const char *> &args,...@@ -791,6 +791,7 @@ static Error os_exec_process_posix(const char *exe, ZigList<const char *> &args,
791 int stdin_pipe[2];791 int stdin_pipe[2];
792 int stdout_pipe[2];792 int stdout_pipe[2];
793 int stderr_pipe[2];793 int stderr_pipe[2];
794 int err_pipe[2];
794795
795 int err;796 int err;
796 if ((err = pipe(stdin_pipe)))797 if ((err = pipe(stdin_pipe)))
...@@ -799,6 +800,8 @@ static Error os_exec_process_posix(const char *exe, ZigList<const char *> &args,...@@ -799,6 +800,8 @@ static Error os_exec_process_posix(const char *exe, ZigList<const char *> &args,
799 zig_panic("pipe failed");800 zig_panic("pipe failed");
800 if ((err = pipe(stderr_pipe)))801 if ((err = pipe(stderr_pipe)))
801 zig_panic("pipe failed");802 zig_panic("pipe failed");
803 if ((err = pipe(err_pipe)))
804 zig_panic("pipe failed");
802805
803 pid_t pid = fork();806 pid_t pid = fork();
804 if (pid == -1)807 if (pid == -1)
...@@ -821,11 +824,12 @@ static Error os_exec_process_posix(const char *exe, ZigList<const char *> &args,...@@ -821,11 +824,12 @@ static Error os_exec_process_posix(const char *exe, ZigList<const char *> &args,
821 argv[i + 1] = args.at(i);824 argv[i + 1] = args.at(i);
822 }825 }
823 execvp(exe, const_cast<char * const *>(argv));826 execvp(exe, const_cast<char * const *>(argv));
827 Error report_err = ErrorUnexpected;
824 if (errno == ENOENT) {828 if (errno == ENOENT) {
825 return ErrorFileNotFound;829 report_err = ErrorFileNotFound;
826 } else {
827 zig_panic("execvp failed: %s", strerror(errno));
828 }830 }
831 write(err_pipe[1], &report_err, sizeof(Error));
832 exit(1);
829 } else {833 } else {
830 // parent834 // parent
831 close(stdin_pipe[0]);835 close(stdin_pipe[0]);
...@@ -847,7 +851,13 @@ static Error os_exec_process_posix(const char *exe, ZigList<const char *> &args,...@@ -847,7 +851,13 @@ static Error os_exec_process_posix(const char *exe, ZigList<const char *> &args,
847851
848 if (err1) return err1;852 if (err1) return err1;
849 if (err2) return err2;853 if (err2) return err2;
850 return ErrorNone;854
855 Error child_err = ErrorNone;
856 write(err_pipe[1], &child_err, sizeof(Error));
857 close(err_pipe[1]);
858 read(err_pipe[0], &child_err, sizeof(Error));
859 close(err_pipe[0]);
860 return child_err;
851 }861 }
852}862}
853#endif863#endif
src/userland.h+1
...@@ -56,6 +56,7 @@ enum Error {...@@ -56,6 +56,7 @@ enum Error {
56 ErrorCacheUnavailable,56 ErrorCacheUnavailable,
57 ErrorPathTooLong,57 ErrorPathTooLong,
58 ErrorCCompilerCannotFindFile,58 ErrorCCompilerCannotFindFile,
59 ErrorNoCCompilerInstalled,
59 ErrorReadingDepFile,60 ErrorReadingDepFile,
60 ErrorInvalidDepFile,61 ErrorInvalidDepFile,
61 ErrorMissingArchitecture,62 ErrorMissingArchitecture,