| author | |
| committer | |
| log | 5a3c02137e6a15d3b8c1fb3595d55003ea44139a |
| tree | da14bf7f1a691d133747721f7e84ff87dcbb5512 |
| parent | d40c4e7c896c5dfed9dd35e8c0d2117f7cf5c53c |
| signature | Commit is signed but in an unrecognized format. |
closes #1493
closes #547 files changed, 71 insertions(+), 25 deletions(-)
src/codegen.cpp+3-1| ... | ... | @@ -7487,7 +7487,9 @@ static void gen_root_source(CodeGen *g) { |
| 7487 | 7487 | { |
| 7488 | 7488 | g->bootstrap_import = add_special_code(g, create_bootstrap_pkg(g, g->root_package), "bootstrap.zig"); |
| 7489 | 7489 | } |
| 7490 | if (g->zig_target.os == OsWindows && !g->have_dllmain_crt_startup && g->out_type == OutTypeLib) { | |
| 7490 | if (g->zig_target.os == OsWindows && !g->have_dllmain_crt_startup && | |
| 7491 | g->out_type == OutTypeLib && !g->is_static) | |
| 7492 | { | |
| 7491 | 7493 | g->bootstrap_import = add_special_code(g, create_bootstrap_pkg(g, g->root_package), "bootstrap_lib.zig"); |
| 7492 | 7494 | } |
| 7493 | 7495 |
src/link.cpp+23-20| ... | ... | @@ -29,9 +29,9 @@ static const char *get_libc_static_file(CodeGen *g, const char *file) { |
| 29 | 29 | return buf_ptr(out_buf); |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | static Buf *build_o_raw(CodeGen *parent_gen, const char *oname, Buf *full_path) { | |
| 32 | static Buf *build_a_raw(CodeGen *parent_gen, const char *aname, Buf *full_path) { | |
| 33 | 33 | 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, | |
| 34 | CodeGen *child_gen = codegen_create(full_path, child_target, OutTypeLib, parent_gen->build_mode, | |
| 35 | 35 | parent_gen->zig_lib_dir); |
| 36 | 36 | |
| 37 | 37 | child_gen->out_h_path = nullptr; |
| ... | ... | @@ -43,32 +43,26 @@ static Buf *build_o_raw(CodeGen *parent_gen, const char *oname, Buf *full_path) |
| 43 | 43 | child_gen->verbose_cimport = parent_gen->verbose_cimport; |
| 44 | 44 | |
| 45 | 45 | codegen_set_strip(child_gen, parent_gen->strip_debug_symbols); |
| 46 | codegen_set_is_static(child_gen, parent_gen->is_static); | |
| 46 | codegen_set_is_static(child_gen, true); | |
| 47 | 47 | |
| 48 | codegen_set_out_name(child_gen, buf_create_from_str(oname)); | |
| 48 | codegen_set_out_name(child_gen, buf_create_from_str(aname)); | |
| 49 | 49 | |
| 50 | 50 | codegen_set_errmsg_color(child_gen, parent_gen->err_color); |
| 51 | 51 | |
| 52 | 52 | codegen_set_mmacosx_version_min(child_gen, parent_gen->mmacosx_version_min); |
| 53 | 53 | codegen_set_mios_version_min(child_gen, parent_gen->mios_version_min); |
| 54 | 54 | |
| 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; | |
| 59 | } | |
| 60 | ||
| 61 | 55 | child_gen->enable_cache = true; |
| 62 | 56 | codegen_build_and_link(child_gen); |
| 63 | 57 | return &child_gen->output_file_path; |
| 64 | 58 | } |
| 65 | 59 | |
| 66 | static Buf *build_o(CodeGen *parent_gen, const char *oname) { | |
| 67 | Buf *source_basename = buf_sprintf("%s.zig", oname); | |
| 60 | static Buf *build_a(CodeGen *parent_gen, const char *aname) { | |
| 61 | Buf *source_basename = buf_sprintf("%s.zig", aname); | |
| 68 | 62 | Buf *full_path = buf_alloc(); |
| 69 | 63 | os_path_join(parent_gen->zig_std_special_dir, source_basename, full_path); |
| 70 | 64 | |
| 71 | return build_o_raw(parent_gen, oname, full_path); | |
| 65 | return build_a_raw(parent_gen, aname, full_path); | |
| 72 | 66 | } |
| 73 | 67 | |
| 74 | 68 | static Buf *build_compiler_rt(CodeGen *parent_gen) { |
| ... | ... | @@ -77,7 +71,7 @@ static Buf *build_compiler_rt(CodeGen *parent_gen) { |
| 77 | 71 | Buf *full_path = buf_alloc(); |
| 78 | 72 | os_path_join(dir_path, buf_create_from_str("index.zig"), full_path); |
| 79 | 73 | |
| 80 | return build_o_raw(parent_gen, "compiler_rt", full_path); | |
| 74 | return build_a_raw(parent_gen, "compiler_rt", full_path); | |
| 81 | 75 | } |
| 82 | 76 | |
| 83 | 77 | static const char *get_darwin_arch_string(const ZigTarget *t) { |
| ... | ... | @@ -317,8 +311,8 @@ static void construct_linker_job_elf(LinkJob *lj) { |
| 317 | 311 | |
| 318 | 312 | if (g->out_type == OutTypeExe || g->out_type == OutTypeLib) { |
| 319 | 313 | if (g->libc_link_lib == nullptr) { |
| 320 | Buf *builtin_o_path = build_o(g, "builtin"); | |
| 321 | lj->args.append(buf_ptr(builtin_o_path)); | |
| 314 | Buf *builtin_a_path = build_a(g, "builtin"); | |
| 315 | lj->args.append(buf_ptr(builtin_a_path)); | |
| 322 | 316 | } |
| 323 | 317 | |
| 324 | 318 | // 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) { |
| 548 | 542 | |
| 549 | 543 | if (g->out_type == OutTypeExe || g->out_type == OutTypeLib) { |
| 550 | 544 | if (g->libc_link_lib == nullptr) { |
| 551 | Buf *builtin_o_path = build_o(g, "builtin"); | |
| 552 | lj->args.append(buf_ptr(builtin_o_path)); | |
| 545 | Buf *builtin_a_path = build_a(g, "builtin"); | |
| 546 | lj->args.append(buf_ptr(builtin_a_path)); | |
| 553 | 547 | } |
| 554 | 548 | |
| 555 | 549 | // 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) { |
| 960 | 954 | } |
| 961 | 955 | |
| 962 | 956 | 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); | |
| 957 | ZigList<const char *> file_names = {}; | |
| 958 | for (size_t i = 0; i < g->link_objects.length; i += 1) { | |
| 959 | file_names.append((const char *)buf_ptr(g->link_objects.at(i))); | |
| 960 | } | |
| 961 | ZigLLVM_OSType os_type = get_llvm_os_type(g->zig_target.os); | |
| 962 | codegen_add_time_event(g, "LLVM Link"); | |
| 963 | if (ZigLLVMWriteArchive(buf_ptr(&g->output_file_path), file_names.items, file_names.length, os_type)) { | |
| 964 | fprintf(stderr, "Unable to write archive '%s'\n", buf_ptr(&g->output_file_path)); | |
| 965 | exit(1); | |
| 966 | } | |
| 967 | return; | |
| 965 | 968 | } |
| 966 | 969 | |
| 967 | 970 | lj.link_in_crt = (g->libc_link_lib != nullptr && g->out_type == OutTypeExe); |
src/target.cpp+1-1| ... | ... | @@ -250,7 +250,7 @@ Os get_target_os(size_t index) { |
| 250 | 250 | return os_list[index]; |
| 251 | 251 | } |
| 252 | 252 | |
| 253 | static ZigLLVM_OSType get_llvm_os_type(Os os_type) { | |
| 253 | ZigLLVM_OSType get_llvm_os_type(Os os_type) { | |
| 254 | 254 | switch (os_type) { |
| 255 | 255 | case OsFreestanding: |
| 256 | 256 | 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 |
| 119 | 119 | Buf *target_dynamic_linker(ZigTarget *target); |
| 120 | 120 | |
| 121 | 121 | bool target_can_exec(const ZigTarget *host_target, const ZigTarget *guest_target); |
| 122 | ||
| 122 | ZigLLVM_OSType get_llvm_os_type(Os os_type); | |
| 123 | 123 | |
| 124 | 124 | #endif |
src/zig_llvm.cpp+39-1| ... | ... | @@ -32,6 +32,8 @@ |
| 32 | 32 | #include <llvm/IR/Verifier.h> |
| 33 | 33 | #include <llvm/InitializePasses.h> |
| 34 | 34 | #include <llvm/MC/SubtargetFeature.h> |
| 35 | #include <llvm/Object/Archive.h> | |
| 36 | #include <llvm/Object/ArchiveWriter.h> | |
| 35 | 37 | #include <llvm/PassRegistry.h> |
| 36 | 38 | #include <llvm/Support/FileSystem.h> |
| 37 | 39 | #include <llvm/Support/TargetParser.h> |
| ... | ... | @@ -40,8 +42,8 @@ |
| 40 | 42 | #include <llvm/Target/TargetMachine.h> |
| 41 | 43 | #include <llvm/Transforms/Coroutines.h> |
| 42 | 44 | #include <llvm/Transforms/IPO.h> |
| 43 | #include <llvm/Transforms/IPO/PassManagerBuilder.h> | |
| 44 | 45 | #include <llvm/Transforms/IPO/AlwaysInliner.h> |
| 46 | #include <llvm/Transforms/IPO/PassManagerBuilder.h> | |
| 45 | 47 | #include <llvm/Transforms/Scalar.h> |
| 46 | 48 | #include <llvm/Transforms/Utils.h> |
| 47 | 49 | |
| ... | ... | @@ -854,6 +856,42 @@ class MyOStream: public raw_ostream { |
| 854 | 856 | size_t pos; |
| 855 | 857 | }; |
| 856 | 858 | |
| 859 | bool 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 | ||
| 857 | 895 | |
| 858 | 896 | bool ZigLLDLink(ZigLLVM_ObjectFormatType oformat, const char **args, size_t arg_count, |
| 859 | 897 | 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 |
| 406 | 406 | ZIG_EXTERN_C bool ZigLLDLink(enum ZigLLVM_ObjectFormatType oformat, const char **args, size_t arg_count, |
| 407 | 407 | void (*append_diagnostic)(void *, const char *, size_t), void *context); |
| 408 | 408 | |
| 409 | ZIG_EXTERN_C bool ZigLLVMWriteArchive(const char *archive_name, const char **file_names, size_t file_name_count, | |
| 410 | enum ZigLLVM_OSType os_type); | |
| 411 | ||
| 409 | 412 | ZIG_EXTERN_C void ZigLLVMGetNativeTarget(enum ZigLLVM_ArchType *arch_type, enum ZigLLVM_SubArchType *sub_arch_type, |
| 410 | 413 | enum ZigLLVM_VendorType *vendor_type, enum ZigLLVM_OSType *os_type, enum ZigLLVM_EnvironmentType *environ_type, |
| 411 | 414 | enum ZigLLVM_ObjectFormatType *oformat); |
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. | |
| 2 | 2 | |
| 3 | 3 | const std = @import("std"); |
| 4 | 4 | const builtin = @import("builtin"); |