authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-10-09 13:20:27-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2018-10-09 13:20:27-04:00
log9e38a81230bcaefb50fae610c071005449a7abfb
treef706728a6b853da0b66379539018645df583519b
parentd40c4e7c896c5dfed9dd35e8c0d2117f7cf5c53c
parent81c6f087241b6b7a1c12de72a2061cb02df0f93f
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #1647 from ziglang/static-libs

Support building static libraries

11 files changed, 149 insertions(+), 54 deletions(-)

src/all_types.hpp+1
......@@ -1763,6 +1763,7 @@ struct CodeGen {
17631763 bool linker_rdynamic;
17641764 bool no_rosegment_workaround;
17651765 bool each_lib_rpath;
1766 bool disable_pic;
17661767
17671768 Buf *mmacosx_version_min;
17681769 Buf *mios_version_min;
src/codegen.cpp+8-2
......@@ -7228,7 +7228,10 @@ static void init(CodeGen *g) {
72287228 bool is_optimized = g->build_mode != BuildModeDebug;
72297229 LLVMCodeGenOptLevel opt_level = is_optimized ? LLVMCodeGenLevelAggressive : LLVMCodeGenLevelNone;
72307230
7231 LLVMRelocMode reloc_mode = g->is_static ? LLVMRelocStatic : LLVMRelocPIC;
7231 if (g->out_type == OutTypeExe && g->is_static) {
7232 g->disable_pic = true;
7233 }
7234 LLVMRelocMode reloc_mode = g->disable_pic ? LLVMRelocStatic : LLVMRelocPIC;
72327235
72337236 const char *target_specific_cpu_args;
72347237 const char *target_specific_features;
......@@ -7487,7 +7490,9 @@ static void gen_root_source(CodeGen *g) {
74877490 {
74887491 g->bootstrap_import = add_special_code(g, create_bootstrap_pkg(g, g->root_package), "bootstrap.zig");
74897492 }
7490 if (g->zig_target.os == OsWindows && !g->have_dllmain_crt_startup && g->out_type == OutTypeLib) {
7493 if (g->zig_target.os == OsWindows && !g->have_dllmain_crt_startup &&
7494 g->out_type == OutTypeLib && !g->is_static)
7495 {
74917496 g->bootstrap_import = add_special_code(g, create_bootstrap_pkg(g, g->root_package), "bootstrap_lib.zig");
74927497 }
74937498
......@@ -8045,6 +8050,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
80458050 cache_bool(ch, g->linker_rdynamic);
80468051 cache_bool(ch, g->no_rosegment_workaround);
80478052 cache_bool(ch, g->each_lib_rpath);
8053 cache_bool(ch, g->disable_pic);
80488054 cache_buf_opt(ch, g->mmacosx_version_min);
80498055 cache_buf_opt(ch, g->mios_version_min);
80508056 cache_usize(ch, g->version_major);
src/link.cpp+55-28
......@@ -29,10 +29,20 @@ static const char *get_libc_static_file(CodeGen *g, const char *file) {
2929 return buf_ptr(out_buf);
3030}
3131
32static Buf *build_o_raw(CodeGen *parent_gen, const char *oname, Buf *full_path) {
32static Buf *build_a_raw(CodeGen *parent_gen, const char *aname, Buf *full_path) {
3333 ZigTarget *child_target = parent_gen->is_native_target ? nullptr : &parent_gen->zig_target;
34 CodeGen *child_gen = codegen_create(full_path, child_target, OutTypeObj, parent_gen->build_mode,
35 parent_gen->zig_lib_dir);
34
35 // The Mach-O LLD code is not well maintained, and trips an assertion
36 // when we link compiler_rt and builtin as libraries rather than objects.
37 // Here we workaround this by having compiler_rt and builtin be objects.
38 // TODO write our own linker. https://github.com/ziglang/zig/issues/1535
39 OutType child_out_type = OutTypeLib;
40 if (parent_gen->zig_target.os == OsMacOSX) {
41 child_out_type = OutTypeObj;
42 }
43
44 CodeGen *child_gen = codegen_create(full_path, child_target, child_out_type,
45 parent_gen->build_mode, parent_gen->zig_lib_dir);
3646
3747 child_gen->out_h_path = nullptr;
3848 child_gen->verbose_tokenize = parent_gen->verbose_tokenize;
......@@ -43,19 +53,22 @@ static Buf *build_o_raw(CodeGen *parent_gen, const char *oname, Buf *full_path)
4353 child_gen->verbose_cimport = parent_gen->verbose_cimport;
4454
4555 codegen_set_strip(child_gen, parent_gen->strip_debug_symbols);
46 codegen_set_is_static(child_gen, parent_gen->is_static);
56 codegen_set_is_static(child_gen, true);
57 child_gen->disable_pic = parent_gen->disable_pic;
4758
48 codegen_set_out_name(child_gen, buf_create_from_str(oname));
59 codegen_set_out_name(child_gen, buf_create_from_str(aname));
4960
5061 codegen_set_errmsg_color(child_gen, parent_gen->err_color);
5162
5263 codegen_set_mmacosx_version_min(child_gen, parent_gen->mmacosx_version_min);
5364 codegen_set_mios_version_min(child_gen, parent_gen->mios_version_min);
5465
55 for (size_t i = 0; i < parent_gen->link_libs_list.length; i += 1) {
56 LinkLib *link_lib = parent_gen->link_libs_list.at(i);
57 LinkLib *new_link_lib = codegen_add_link_lib(child_gen, link_lib->name);
58 new_link_lib->provided_explicitly = link_lib->provided_explicitly;
66 // This is so that compiler_rt and builtin libraries know whether they
67 // will eventually be linked with libc. They make different decisions
68 // about what to export depending on whether libc is linked.
69 if (parent_gen->libc_link_lib != nullptr) {
70 LinkLib *new_link_lib = codegen_add_link_lib(child_gen, parent_gen->libc_link_lib->name);
71 new_link_lib->provided_explicitly = parent_gen->libc_link_lib->provided_explicitly;
5972 }
6073
6174 child_gen->enable_cache = true;
......@@ -63,12 +76,12 @@ static Buf *build_o_raw(CodeGen *parent_gen, const char *oname, Buf *full_path)
6376 return &child_gen->output_file_path;
6477}
6578
66static Buf *build_o(CodeGen *parent_gen, const char *oname) {
67 Buf *source_basename = buf_sprintf("%s.zig", oname);
79static Buf *build_a(CodeGen *parent_gen, const char *aname) {
80 Buf *source_basename = buf_sprintf("%s.zig", aname);
6881 Buf *full_path = buf_alloc();
6982 os_path_join(parent_gen->zig_std_special_dir, source_basename, full_path);
7083
71 return build_o_raw(parent_gen, oname, full_path);
84 return build_a_raw(parent_gen, aname, full_path);
7285}
7386
7487static Buf *build_compiler_rt(CodeGen *parent_gen) {
......@@ -77,7 +90,7 @@ static Buf *build_compiler_rt(CodeGen *parent_gen) {
7790 Buf *full_path = buf_alloc();
7891 os_path_join(dir_path, buf_create_from_str("index.zig"), full_path);
7992
80 return build_o_raw(parent_gen, "compiler_rt", full_path);
93 return build_a_raw(parent_gen, "compiler_rt", full_path);
8194}
8295
8396static const char *get_darwin_arch_string(const ZigTarget *t) {
......@@ -197,6 +210,8 @@ static Buf *get_dynamic_linker_path(CodeGen *g) {
197210static void construct_linker_job_elf(LinkJob *lj) {
198211 CodeGen *g = lj->codegen;
199212
213 lj->args.append("-error-limit=0");
214
200215 if (g->libc_link_lib != nullptr) {
201216 find_libc_lib_path(g);
202217 }
......@@ -215,10 +230,9 @@ static void construct_linker_job_elf(LinkJob *lj) {
215230 lj->args.append(getLDMOption(&g->zig_target));
216231
217232 bool is_lib = g->out_type == OutTypeLib;
218 bool is_static = g->is_static || (!is_lib && g->link_libs_list.length == 0);
219 bool shared = !is_static && is_lib;
233 bool shared = !g->is_static && is_lib;
220234 Buf *soname = nullptr;
221 if (is_static) {
235 if (g->is_static) {
222236 if (g->zig_target.arch.arch == ZigLLVM_arm || g->zig_target.arch.arch == ZigLLVM_armeb ||
223237 g->zig_target.arch.arch == ZigLLVM_thumb || g->zig_target.arch.arch == ZigLLVM_thumbeb)
224238 {
......@@ -242,7 +256,7 @@ static void construct_linker_job_elf(LinkJob *lj) {
242256 if (lj->link_in_crt) {
243257 const char *crt1o;
244258 const char *crtbegino;
245 if (is_static) {
259 if (g->is_static) {
246260 crt1o = "crt1.o";
247261 crtbegino = "crtbeginT.o";
248262 } else {
......@@ -293,7 +307,7 @@ static void construct_linker_job_elf(LinkJob *lj) {
293307 lj->args.append(buf_ptr(g->libc_static_lib_dir));
294308 }
295309
296 if (!is_static) {
310 if (!g->is_static) {
297311 if (g->dynamic_linker != nullptr) {
298312 assert(buf_len(g->dynamic_linker) != 0);
299313 lj->args.append("-dynamic-linker");
......@@ -315,10 +329,10 @@ static void construct_linker_job_elf(LinkJob *lj) {
315329 lj->args.append((const char *)buf_ptr(g->link_objects.at(i)));
316330 }
317331
318 if (g->out_type == OutTypeExe || g->out_type == OutTypeLib) {
332 if (g->out_type == OutTypeExe || (g->out_type == OutTypeLib && !g->is_static)) {
319333 if (g->libc_link_lib == nullptr) {
320 Buf *builtin_o_path = build_o(g, "builtin");
321 lj->args.append(buf_ptr(builtin_o_path));
334 Buf *builtin_a_path = build_a(g, "builtin");
335 lj->args.append(buf_ptr(builtin_a_path));
322336 }
323337
324338 // sometimes libgcc is missing stuff, so we still build compiler_rt and rely on weak linkage
......@@ -345,7 +359,7 @@ static void construct_linker_job_elf(LinkJob *lj) {
345359
346360 // libc dep
347361 if (g->libc_link_lib != nullptr) {
348 if (is_static) {
362 if (g->is_static) {
349363 lj->args.append("--start-group");
350364 lj->args.append("-lgcc");
351365 lj->args.append("-lgcc_eh");
......@@ -387,6 +401,7 @@ static void construct_linker_job_elf(LinkJob *lj) {
387401static void construct_linker_job_wasm(LinkJob *lj) {
388402 CodeGen *g = lj->codegen;
389403
404 lj->args.append("-error-limit=0");
390405 lj->args.append("--no-entry"); // So lld doesn't look for _start.
391406 lj->args.append("-o");
392407 lj->args.append(buf_ptr(&g->output_file_path));
......@@ -425,6 +440,8 @@ static bool zig_lld_link(ZigLLVM_ObjectFormatType oformat, const char **args, si
425440static void construct_linker_job_coff(LinkJob *lj) {
426441 CodeGen *g = lj->codegen;
427442
443 lj->args.append("/ERRORLIMIT:0");
444
428445 if (g->libc_link_lib != nullptr) {
429446 find_libc_lib_path(g);
430447 }
......@@ -546,10 +563,10 @@ static void construct_linker_job_coff(LinkJob *lj) {
546563 lj->args.append((const char *)buf_ptr(g->link_objects.at(i)));
547564 }
548565
549 if (g->out_type == OutTypeExe || g->out_type == OutTypeLib) {
566 if (g->out_type == OutTypeExe || (g->out_type == OutTypeLib && !g->is_static)) {
550567 if (g->libc_link_lib == nullptr) {
551 Buf *builtin_o_path = build_o(g, "builtin");
552 lj->args.append(buf_ptr(builtin_o_path));
568 Buf *builtin_a_path = build_a(g, "builtin");
569 lj->args.append(buf_ptr(builtin_a_path));
553570 }
554571
555572 // msvc compiler_rt is missing some stuff, so we still build it and rely on weak linkage
......@@ -761,6 +778,7 @@ static bool darwin_version_lt(DarwinPlatform *platform, int major, int minor) {
761778static void construct_linker_job_macho(LinkJob *lj) {
762779 CodeGen *g = lj->codegen;
763780
781 lj->args.append("-error-limit=0");
764782 lj->args.append("-demangle");
765783
766784 if (g->linker_rdynamic) {
......@@ -878,7 +896,7 @@ static void construct_linker_job_macho(LinkJob *lj) {
878896 }
879897
880898 // compiler_rt on darwin is missing some stuff, so we still build it and rely on LinkOnce
881 if (g->out_type == OutTypeExe || g->out_type == OutTypeLib) {
899 if (g->out_type == OutTypeExe || (g->out_type == OutTypeLib && !g->is_static)) {
882900 Buf *compiler_rt_o_path = build_compiler_rt(g);
883901 lj->args.append(buf_ptr(compiler_rt_o_path));
884902 }
......@@ -960,8 +978,17 @@ void codegen_link(CodeGen *g) {
960978 }
961979
962980 if (g->out_type == OutTypeLib && g->is_static) {
963 fprintf(stderr, "Zig does not yet support creating static libraries\nSee https://github.com/ziglang/zig/issues/1493\n");
964 exit(1);
981 ZigList<const char *> file_names = {};
982 for (size_t i = 0; i < g->link_objects.length; i += 1) {
983 file_names.append((const char *)buf_ptr(g->link_objects.at(i)));
984 }
985 ZigLLVM_OSType os_type = get_llvm_os_type(g->zig_target.os);
986 codegen_add_time_event(g, "LLVM Link");
987 if (ZigLLVMWriteArchive(buf_ptr(&g->output_file_path), file_names.items, file_names.length, os_type)) {
988 fprintf(stderr, "Unable to write archive '%s'\n", buf_ptr(&g->output_file_path));
989 exit(1);
990 }
991 return;
965992 }
966993
967994 lj.link_in_crt = (g->libc_link_lib != nullptr && g->out_type == OutTypeExe);
src/main.cpp+12
......@@ -47,6 +47,7 @@ static int print_full_usage(const char *arg0) {
4747 " --cache-dir [path] override the cache directory\n"
4848 " --cache [auto|off|on] build in global cache, print out paths to stdout\n"
4949 " --color [auto|off|on] enable or disable colored error messages\n"
50 " --disable-pic disable Position Independent Code for libraries\n"
5051 " --emit [asm|bin|llvm-ir] emit a specific file format as compilation output\n"
5152 " -ftime-report print timing diagnostics\n"
5253 " --libc-include-dir [path] directory where libc stdlib.h resides\n"
......@@ -386,6 +387,7 @@ int main(int argc, char **argv) {
386387 size_t ver_minor = 0;
387388 size_t ver_patch = 0;
388389 bool timing_info = false;
390 bool disable_pic = false;
389391 const char *cache_dir = nullptr;
390392 CliPkg *cur_pkg = allocate<CliPkg>(1);
391393 BuildMode build_mode = BuildModeDebug;
......@@ -556,6 +558,8 @@ int main(int argc, char **argv) {
556558 each_lib_rpath = true;
557559 } else if (strcmp(arg, "-ftime-report") == 0) {
558560 timing_info = true;
561 } else if (strcmp(arg, "--disable-pic") == 0) {
562 disable_pic = true;
559563 } else if (strcmp(arg, "--test-cmd-bin") == 0) {
560564 test_exec_args.append(nullptr);
561565 } else if (arg[1] == 'L' && arg[2] != 0) {
......@@ -849,6 +853,14 @@ int main(int argc, char **argv) {
849853 buf_out_name = buf_create_from_str("run");
850854 }
851855 CodeGen *g = codegen_create(zig_root_source_file, target, out_type, build_mode, get_zig_lib_dir());
856 if (disable_pic) {
857 if (out_type != OutTypeLib || !is_static) {
858 fprintf(stderr, "--disable-pic only applies to static libraries");
859 return EXIT_FAILURE;
860 }
861 g->disable_pic = true;
862 }
863
852864 g->enable_time_report = timing_info;
853865 buf_init_from_str(&g->cache_dir, cache_dir ? cache_dir : default_zig_cache_name);
854866 codegen_set_out_name(g, buf_out_name);
src/os.cpp+22-18
......@@ -46,6 +46,7 @@ typedef SSIZE_T ssize_t;
4646#include <sys/wait.h>
4747#include <fcntl.h>
4848#include <limits.h>
49#include <spawn.h>
4950
5051#endif
5152
......@@ -70,6 +71,12 @@ static clock_serv_t cclock;
7071#include <errno.h>
7172#include <time.h>
7273
74// Apple doesn't provide the environ global variable
75#if defined(__APPLE__) && !defined(environ)
76#include <crt_externs.h>
77#define environ (*_NSGetEnviron())
78#endif
79
7380#if defined(ZIG_OS_POSIX)
7481static void populate_termination(Termination *term, int status) {
7582 if (WIFEXITED(status)) {
......@@ -88,25 +95,22 @@ static void populate_termination(Termination *term, int status) {
8895}
8996
9097static void os_spawn_process_posix(const char *exe, ZigList<const char *> &args, Termination *term) {
91 pid_t pid = fork();
92 if (pid == -1)
93 zig_panic("fork failed: %s", strerror(errno));
94 if (pid == 0) {
95 // child
96 const char **argv = allocate<const char *>(args.length + 2);
97 argv[0] = exe;
98 argv[args.length + 1] = nullptr;
99 for (size_t i = 0; i < args.length; i += 1) {
100 argv[i + 1] = args.at(i);
101 }
102 execvp(exe, const_cast<char * const *>(argv));
103 zig_panic("execvp failed: %s", strerror(errno));
104 } else {
105 // parent
106 int status;
107 waitpid(pid, &status, 0);
108 populate_termination(term, status);
98 const char **argv = allocate<const char *>(args.length + 2);
99 argv[0] = exe;
100 argv[args.length + 1] = nullptr;
101 for (size_t i = 0; i < args.length; i += 1) {
102 argv[i + 1] = args.at(i);
109103 }
104
105 pid_t pid;
106 int rc = posix_spawn(&pid, exe, nullptr, nullptr, const_cast<char *const*>(argv), environ);
107 if (rc != 0) {
108 zig_panic("posix_spawn failed: %s", strerror(rc));
109 }
110
111 int status;
112 waitpid(pid, &status, 0);
113 populate_termination(term, status);
110114}
111115#endif
112116
src/target.cpp+1-1
......@@ -250,7 +250,7 @@ Os get_target_os(size_t index) {
250250 return os_list[index];
251251}
252252
253static ZigLLVM_OSType get_llvm_os_type(Os os_type) {
253ZigLLVM_OSType get_llvm_os_type(Os os_type) {
254254 switch (os_type) {
255255 case OsFreestanding:
256256 case OsZen:
src/target.hpp+1-1
......@@ -119,6 +119,6 @@ const char *target_lib_file_ext(ZigTarget *target, bool is_static, size_t versio
119119Buf *target_dynamic_linker(ZigTarget *target);
120120
121121bool target_can_exec(const ZigTarget *host_target, const ZigTarget *guest_target);
122
122ZigLLVM_OSType get_llvm_os_type(Os os_type);
123123
124124#endif
src/zig_llvm.cpp+39-1
......@@ -32,6 +32,8 @@
3232#include <llvm/IR/Verifier.h>
3333#include <llvm/InitializePasses.h>
3434#include <llvm/MC/SubtargetFeature.h>
35#include <llvm/Object/Archive.h>
36#include <llvm/Object/ArchiveWriter.h>
3537#include <llvm/PassRegistry.h>
3638#include <llvm/Support/FileSystem.h>
3739#include <llvm/Support/TargetParser.h>
......@@ -40,8 +42,8 @@
4042#include <llvm/Target/TargetMachine.h>
4143#include <llvm/Transforms/Coroutines.h>
4244#include <llvm/Transforms/IPO.h>
43#include <llvm/Transforms/IPO/PassManagerBuilder.h>
4445#include <llvm/Transforms/IPO/AlwaysInliner.h>
46#include <llvm/Transforms/IPO/PassManagerBuilder.h>
4547#include <llvm/Transforms/Scalar.h>
4648#include <llvm/Transforms/Utils.h>
4749
......@@ -854,6 +856,42 @@ class MyOStream: public raw_ostream {
854856 size_t pos;
855857};
856858
859bool ZigLLVMWriteArchive(const char *archive_name, const char **file_names, size_t file_name_count,
860 ZigLLVM_OSType os_type)
861{
862 object::Archive::Kind kind;
863 switch (os_type) {
864 case ZigLLVM_Win32:
865 // For some reason llvm-lib passes K_GNU on windows.
866 // See lib/ToolDrivers/llvm-lib/LibDriver.cpp:168 in libDriverMain
867 kind = object::Archive::K_GNU;
868 break;
869 case ZigLLVM_Linux:
870 kind = object::Archive::K_GNU;
871 break;
872 case ZigLLVM_Darwin:
873 case ZigLLVM_IOS:
874 kind = object::Archive::K_DARWIN;
875 break;
876 case ZigLLVM_OpenBSD:
877 case ZigLLVM_FreeBSD:
878 kind = object::Archive::K_BSD;
879 break;
880 default:
881 kind = object::Archive::K_GNU;
882 }
883 SmallVector<NewArchiveMember, 4> new_members;
884 for (size_t i = 0; i < file_name_count; i += 1) {
885 Expected<NewArchiveMember> new_member = NewArchiveMember::getFile(file_names[i], true);
886 Error err = new_member.takeError();
887 if (err) return true;
888 new_members.push_back(std::move(*new_member));
889 }
890 Error err = writeArchive(archive_name, new_members, true, kind, true, false, nullptr);
891 if (err) return true;
892 return false;
893}
894
857895
858896bool ZigLLDLink(ZigLLVM_ObjectFormatType oformat, const char **args, size_t arg_count,
859897 void (*append_diagnostic)(void *, const char *, size_t), void *context)
src/zig_llvm.h+3
......@@ -406,6 +406,9 @@ ZIG_EXTERN_C const char *ZigLLVMGetEnvironmentTypeName(enum ZigLLVM_EnvironmentT
406406ZIG_EXTERN_C bool ZigLLDLink(enum ZigLLVM_ObjectFormatType oformat, const char **args, size_t arg_count,
407407 void (*append_diagnostic)(void *, const char *, size_t), void *context);
408408
409ZIG_EXTERN_C bool ZigLLVMWriteArchive(const char *archive_name, const char **file_names, size_t file_name_count,
410 enum ZigLLVM_OSType os_type);
411
409412ZIG_EXTERN_C void ZigLLVMGetNativeTarget(enum ZigLLVM_ArchType *arch_type, enum ZigLLVM_SubArchType *sub_arch_type,
410413 enum ZigLLVM_VendorType *vendor_type, enum ZigLLVM_OSType *os_type, enum ZigLLVM_EnvironmentType *environ_type,
411414 enum ZigLLVM_ObjectFormatType *oformat);
std/special/bootstrap.zig+6-2
......@@ -66,7 +66,9 @@ fn posixCallMainAndExit() noreturn {
6666 std.os.posix.exit(callMainWithArgs(argc, argv, envp));
6767}
6868
69fn callMainWithArgs(argc: usize, argv: [*][*]u8, envp: [][*]u8) u8 {
69// This is marked inline because for some reason LLVM in release mode fails to inline it,
70// and we want fewer call frames in stack traces.
71inline fn callMainWithArgs(argc: usize, argv: [*][*]u8, envp: [][*]u8) u8 {
7072 std.os.ArgIteratorPosix.raw = argv[0..argc];
7173 std.os.posix_environ_raw = envp;
7274 return callMain();
......@@ -79,7 +81,9 @@ extern fn main(c_argc: i32, c_argv: [*][*]u8, c_envp: [*]?[*]u8) i32 {
7981 return callMainWithArgs(@intCast(usize, c_argc), c_argv, envp);
8082}
8183
82fn callMain() u8 {
84// This is marked inline because for some reason LLVM in release mode fails to inline it,
85// and we want fewer call frames in stack traces.
86inline fn callMain() u8 {
8387 switch (@typeId(@typeOf(root.main).ReturnType)) {
8488 builtin.TypeId.NoReturn => {
8589 root.main();
std/special/bootstrap_lib.zig+1-1
......@@ -1,4 +1,4 @@
1// This file is included in the compilation unit when exporting a library on windows.
1// This file is included in the compilation unit when exporting a DLL on windows.
22
33const std = @import("std");
44const builtin = @import("builtin");