Commit 5a3c02137e

Andrew Kelley <superjoe30@gmail.com>
2018-10-08 19:24:39
support building static libraries
closes #1493 closes #54
1 parent d40c4e7
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");
     }
 
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<const char *> 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);
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:
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
src/zig_llvm.cpp
@@ -32,6 +32,8 @@
 #include <llvm/IR/Verifier.h>
 #include <llvm/InitializePasses.h>
 #include <llvm/MC/SubtargetFeature.h>
+#include <llvm/Object/Archive.h>
+#include <llvm/Object/ArchiveWriter.h>
 #include <llvm/PassRegistry.h>
 #include <llvm/Support/FileSystem.h>
 #include <llvm/Support/TargetParser.h>
@@ -40,8 +42,8 @@
 #include <llvm/Target/TargetMachine.h>
 #include <llvm/Transforms/Coroutines.h>
 #include <llvm/Transforms/IPO.h>
-#include <llvm/Transforms/IPO/PassManagerBuilder.h>
 #include <llvm/Transforms/IPO/AlwaysInliner.h>
+#include <llvm/Transforms/IPO/PassManagerBuilder.h>
 #include <llvm/Transforms/Scalar.h>
 #include <llvm/Transforms/Utils.h>
 
@@ -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<NewArchiveMember, 4> new_members;
+    for (size_t i = 0; i < file_name_count; i += 1) {
+        Expected<NewArchiveMember> 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)
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);
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");