From 5a3c02137e6a15d3b8c1fb3595d55003ea44139a Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 8 Oct 2018 13:24:39 -0400 Subject: [PATCH] support building static libraries closes #1493 closes #54 --- src/codegen.cpp | 4 +++- src/link.cpp | 43 +++++++++++++++++++---------------- src/target.cpp | 2 +- src/target.hpp | 2 +- src/zig_llvm.cpp | 40 +++++++++++++++++++++++++++++++- src/zig_llvm.h | 3 +++ std/special/bootstrap_lib.zig | 2 +- 7 files changed, 71 insertions(+), 25 deletions(-) diff --git a/src/codegen.cpp b/src/codegen.cpp index e792fd77aa..fd7a386078 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -7487,7 +7487,9 @@ static void gen_root_source(CodeGen *g) { { g->bootstrap_import = add_special_code(g, create_bootstrap_pkg(g, g->root_package), "bootstrap.zig"); } - if (g->zig_target.os == OsWindows && !g->have_dllmain_crt_startup && g->out_type == OutTypeLib) { + if (g->zig_target.os == OsWindows && !g->have_dllmain_crt_startup && + g->out_type == OutTypeLib && !g->is_static) + { g->bootstrap_import = add_special_code(g, create_bootstrap_pkg(g, g->root_package), "bootstrap_lib.zig"); } diff --git a/src/link.cpp b/src/link.cpp index 8b75f0e783..c961042324 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -29,9 +29,9 @@ static const char *get_libc_static_file(CodeGen *g, const char *file) { return buf_ptr(out_buf); } -static Buf *build_o_raw(CodeGen *parent_gen, const char *oname, Buf *full_path) { +static Buf *build_a_raw(CodeGen *parent_gen, const char *aname, Buf *full_path) { ZigTarget *child_target = parent_gen->is_native_target ? nullptr : &parent_gen->zig_target; - CodeGen *child_gen = codegen_create(full_path, child_target, OutTypeObj, parent_gen->build_mode, + CodeGen *child_gen = codegen_create(full_path, child_target, OutTypeLib, parent_gen->build_mode, parent_gen->zig_lib_dir); child_gen->out_h_path = nullptr; @@ -43,32 +43,26 @@ static Buf *build_o_raw(CodeGen *parent_gen, const char *oname, Buf *full_path) child_gen->verbose_cimport = parent_gen->verbose_cimport; codegen_set_strip(child_gen, parent_gen->strip_debug_symbols); - codegen_set_is_static(child_gen, parent_gen->is_static); + codegen_set_is_static(child_gen, true); - codegen_set_out_name(child_gen, buf_create_from_str(oname)); + codegen_set_out_name(child_gen, buf_create_from_str(aname)); codegen_set_errmsg_color(child_gen, parent_gen->err_color); codegen_set_mmacosx_version_min(child_gen, parent_gen->mmacosx_version_min); codegen_set_mios_version_min(child_gen, parent_gen->mios_version_min); - for (size_t i = 0; i < parent_gen->link_libs_list.length; i += 1) { - LinkLib *link_lib = parent_gen->link_libs_list.at(i); - LinkLib *new_link_lib = codegen_add_link_lib(child_gen, link_lib->name); - new_link_lib->provided_explicitly = link_lib->provided_explicitly; - } - child_gen->enable_cache = true; codegen_build_and_link(child_gen); return &child_gen->output_file_path; } -static Buf *build_o(CodeGen *parent_gen, const char *oname) { - Buf *source_basename = buf_sprintf("%s.zig", oname); +static Buf *build_a(CodeGen *parent_gen, const char *aname) { + Buf *source_basename = buf_sprintf("%s.zig", aname); Buf *full_path = buf_alloc(); os_path_join(parent_gen->zig_std_special_dir, source_basename, full_path); - return build_o_raw(parent_gen, oname, full_path); + return build_a_raw(parent_gen, aname, full_path); } static Buf *build_compiler_rt(CodeGen *parent_gen) { @@ -77,7 +71,7 @@ static Buf *build_compiler_rt(CodeGen *parent_gen) { Buf *full_path = buf_alloc(); os_path_join(dir_path, buf_create_from_str("index.zig"), full_path); - return build_o_raw(parent_gen, "compiler_rt", full_path); + return build_a_raw(parent_gen, "compiler_rt", full_path); } static const char *get_darwin_arch_string(const ZigTarget *t) { @@ -317,8 +311,8 @@ static void construct_linker_job_elf(LinkJob *lj) { if (g->out_type == OutTypeExe || g->out_type == OutTypeLib) { if (g->libc_link_lib == nullptr) { - Buf *builtin_o_path = build_o(g, "builtin"); - lj->args.append(buf_ptr(builtin_o_path)); + Buf *builtin_a_path = build_a(g, "builtin"); + lj->args.append(buf_ptr(builtin_a_path)); } // sometimes libgcc is missing stuff, so we still build compiler_rt and rely on weak linkage @@ -548,8 +542,8 @@ static void construct_linker_job_coff(LinkJob *lj) { if (g->out_type == OutTypeExe || g->out_type == OutTypeLib) { if (g->libc_link_lib == nullptr) { - Buf *builtin_o_path = build_o(g, "builtin"); - lj->args.append(buf_ptr(builtin_o_path)); + Buf *builtin_a_path = build_a(g, "builtin"); + lj->args.append(buf_ptr(builtin_a_path)); } // msvc compiler_rt is missing some stuff, so we still build it and rely on weak linkage @@ -960,8 +954,17 @@ void codegen_link(CodeGen *g) { } if (g->out_type == OutTypeLib && g->is_static) { - fprintf(stderr, "Zig does not yet support creating static libraries\nSee https://github.com/ziglang/zig/issues/1493\n"); - exit(1); + ZigList file_names = {}; + for (size_t i = 0; i < g->link_objects.length; i += 1) { + file_names.append((const char *)buf_ptr(g->link_objects.at(i))); + } + ZigLLVM_OSType os_type = get_llvm_os_type(g->zig_target.os); + codegen_add_time_event(g, "LLVM Link"); + if (ZigLLVMWriteArchive(buf_ptr(&g->output_file_path), file_names.items, file_names.length, os_type)) { + fprintf(stderr, "Unable to write archive '%s'\n", buf_ptr(&g->output_file_path)); + exit(1); + } + return; } lj.link_in_crt = (g->libc_link_lib != nullptr && g->out_type == OutTypeExe); diff --git a/src/target.cpp b/src/target.cpp index 525935310a..25dfa9d3cb 100644 --- a/src/target.cpp +++ b/src/target.cpp @@ -250,7 +250,7 @@ Os get_target_os(size_t index) { return os_list[index]; } -static ZigLLVM_OSType get_llvm_os_type(Os os_type) { +ZigLLVM_OSType get_llvm_os_type(Os os_type) { switch (os_type) { case OsFreestanding: case OsZen: diff --git a/src/target.hpp b/src/target.hpp index 0e0b1f2dc5..a4685fc09e 100644 --- a/src/target.hpp +++ b/src/target.hpp @@ -119,6 +119,6 @@ const char *target_lib_file_ext(ZigTarget *target, bool is_static, size_t versio Buf *target_dynamic_linker(ZigTarget *target); bool target_can_exec(const ZigTarget *host_target, const ZigTarget *guest_target); - +ZigLLVM_OSType get_llvm_os_type(Os os_type); #endif diff --git a/src/zig_llvm.cpp b/src/zig_llvm.cpp index ea270bcb5a..00023f6232 100644 --- a/src/zig_llvm.cpp +++ b/src/zig_llvm.cpp @@ -32,6 +32,8 @@ #include #include #include +#include +#include #include #include #include @@ -40,8 +42,8 @@ #include #include #include -#include #include +#include #include #include @@ -854,6 +856,42 @@ class MyOStream: public raw_ostream { size_t pos; }; +bool ZigLLVMWriteArchive(const char *archive_name, const char **file_names, size_t file_name_count, + ZigLLVM_OSType os_type) +{ + object::Archive::Kind kind; + switch (os_type) { + case ZigLLVM_Win32: + // For some reason llvm-lib passes K_GNU on windows. + // See lib/ToolDrivers/llvm-lib/LibDriver.cpp:168 in libDriverMain + kind = object::Archive::K_GNU; + break; + case ZigLLVM_Linux: + kind = object::Archive::K_GNU; + break; + case ZigLLVM_Darwin: + case ZigLLVM_IOS: + kind = object::Archive::K_DARWIN; + break; + case ZigLLVM_OpenBSD: + case ZigLLVM_FreeBSD: + kind = object::Archive::K_BSD; + break; + default: + kind = object::Archive::K_GNU; + } + SmallVector new_members; + for (size_t i = 0; i < file_name_count; i += 1) { + Expected new_member = NewArchiveMember::getFile(file_names[i], true); + Error err = new_member.takeError(); + if (err) return true; + new_members.push_back(std::move(*new_member)); + } + Error err = writeArchive(archive_name, new_members, true, kind, true, false, nullptr); + if (err) return true; + return false; +} + bool ZigLLDLink(ZigLLVM_ObjectFormatType oformat, const char **args, size_t arg_count, void (*append_diagnostic)(void *, const char *, size_t), void *context) diff --git a/src/zig_llvm.h b/src/zig_llvm.h index a005b3157e..551a4a7448 100644 --- a/src/zig_llvm.h +++ b/src/zig_llvm.h @@ -406,6 +406,9 @@ ZIG_EXTERN_C const char *ZigLLVMGetEnvironmentTypeName(enum ZigLLVM_EnvironmentT ZIG_EXTERN_C bool ZigLLDLink(enum ZigLLVM_ObjectFormatType oformat, const char **args, size_t arg_count, void (*append_diagnostic)(void *, const char *, size_t), void *context); +ZIG_EXTERN_C bool ZigLLVMWriteArchive(const char *archive_name, const char **file_names, size_t file_name_count, + enum ZigLLVM_OSType os_type); + ZIG_EXTERN_C void ZigLLVMGetNativeTarget(enum ZigLLVM_ArchType *arch_type, enum ZigLLVM_SubArchType *sub_arch_type, enum ZigLLVM_VendorType *vendor_type, enum ZigLLVM_OSType *os_type, enum ZigLLVM_EnvironmentType *environ_type, enum ZigLLVM_ObjectFormatType *oformat); diff --git a/std/special/bootstrap_lib.zig b/std/special/bootstrap_lib.zig index f029495cb0..701eee389d 100644 --- a/std/special/bootstrap_lib.zig +++ b/std/special/bootstrap_lib.zig @@ -1,4 +1,4 @@ -// This file is included in the compilation unit when exporting a library on windows. +// This file is included in the compilation unit when exporting a DLL on windows. const std = @import("std"); const builtin = @import("builtin");