From c5509a07cacfea16b4fb3c8d552a33083b713697 Mon Sep 17 00:00:00 2001 From: Wink Saville Date: Mon, 24 Sep 2018 09:33:45 -0700 Subject: Ignore class-memaccess error for gcc 8 and above MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On Arch Linux the current default compiler is gcc 8.2.1 and this change is needed to ignore the following errors: In file included from /home/wink/local/include/llvm/ADT/STLExtras.h:21, from /home/wink/local/include/llvm/ADT/StringRef.h:13, from /home/wink/local/include/llvm/ADT/StringMap.h:17, from /home/wink/local/include/llvm/Support/Host.h:17, from /home/wink/local/include/llvm/ADT/Hashing.h:49, from /home/wink/local/include/llvm/ADT/ArrayRef.h:13, from /home/wink/local/include/llvm/ADT/APFloat.h:21, from /home/wink/local/include/clang/AST/APValue.h:18, from /home/wink/local/include/clang/AST/Decl.h:17, from /home/wink/local/include/clang/AST/ASTTypeTraits.h:20, from /home/wink/local/include/clang/AST/ASTContext.h:18, from /home/wink/local/include/clang/Frontend/ASTUnit.h:18, from /home/wink/prgs/ziglang/zig/src/translate_c.cpp:18: /home/wink/local/include/llvm/ADT/SmallVector.h: In instantiation of ‘void llvm::SmallVectorTemplateBase::push_back(const T&) [with T = std::pair]’: /home/wink/local/include/llvm/Support/Allocator.h:249:33: required from ‘void* llvm::BumpPtrAllocatorImpl::Allocate(size_t, size_t) [with AllocatorT = llvm::MallocAllocator; long unsigned int SlabSize = 4096; long unsigned int SizeThreshold = 4096; size_t = long unsigned int]’ /home/wink/local/include/clang/AST/ASTContext.h:659:42: required from here /home/wink/local/include/llvm/ADT/SmallVector.h:313:11: error: ‘void* memcpy(void*, const void*, size_t)’ writing to an object of type ‘struct std::pair’ with no trivial copy-assignment; use copy-assignment or copy-initialization instead [-Werror=class-memaccess] memcpy(this->end(), &Elt, sizeof(T)); ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from /usr/include/c++/8.2.1/utility:70, from /home/wink/local/include/llvm/Support/type_traits.h:19, from /home/wink/local/include/llvm/Support/Casting.h:19, from /home/wink/local/include/clang/Basic/LLVM.h:22, from /home/wink/local/include/clang/AST/APValue.h:17, from /home/wink/local/include/clang/AST/Decl.h:17, from /home/wink/local/include/clang/AST/ASTTypeTraits.h:20, from /home/wink/local/include/clang/AST/ASTContext.h:18, from /home/wink/local/include/clang/Frontend/ASTUnit.h:18, from /home/wink/prgs/ziglang/zig/src/translate_c.cpp:18: /usr/include/c++/8.2.1/bits/stl_pair.h:198:12: note: ‘struct std::pair’ declared here struct pair ^~~~ --- src/zig_llvm.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/zig_llvm.cpp') diff --git a/src/zig_llvm.cpp b/src/zig_llvm.cpp index ad8edb4cda..8e9fe65d47 100644 --- a/src/zig_llvm.cpp +++ b/src/zig_llvm.cpp @@ -15,6 +15,11 @@ #include "zig_llvm.h" +#if __GNUC__ >= 8 +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wclass-memaccess" +#endif + #include #include #include @@ -42,6 +47,10 @@ #include +#if __GNUC__ >= 8 +#pragma GCC diagnostic pop +#endif + #include #include -- cgit v1.2.3 From e6446dfc86e42ccea93760d99cf4b421265cfb0e Mon Sep 17 00:00:00 2001 From: emekoi Date: Sun, 30 Sep 2018 01:34:38 -0500 Subject: fixed native target detection --- CMakeLists.txt | 3 +++ src/zig_llvm.cpp | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) (limited to 'src/zig_llvm.cpp') diff --git a/CMakeLists.txt b/CMakeLists.txt index 22f365aafe..bba4dd6881 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -211,6 +211,9 @@ else() set(ZIG_LLD_COMPILE_FLAGS "-std=c++11 -D_CRT_SECURE_NO_WARNINGS /w") else() set(ZIG_LLD_COMPILE_FLAGS "-std=c++11 -fno-exceptions -fno-rtti -Wno-comment") + if(MINGW) + set(ZIG_LLD_COMPILE_FLAGS "${ZIG_LLD_COMPILE_FLAGS} -D__STDC_FORMAT_MACROS -D__USE_MINGW_ANSI_STDIO -Wno-pedantic-ms-format") + endif() endif() set_target_properties(embedded_lld_lib PROPERTIES COMPILE_FLAGS ${ZIG_LLD_COMPILE_FLAGS} diff --git a/src/zig_llvm.cpp b/src/zig_llvm.cpp index 8e9fe65d47..ea270bcb5a 100644 --- a/src/zig_llvm.cpp +++ b/src/zig_llvm.cpp @@ -712,7 +712,7 @@ void ZigLLVMGetNativeTarget(ZigLLVM_ArchType *arch_type, ZigLLVM_SubArchType *su ZigLLVM_ObjectFormatType *oformat) { char *native_triple = LLVMGetDefaultTargetTriple(); - Triple triple(native_triple); + Triple triple(Triple::normalize(native_triple)); *arch_type = (ZigLLVM_ArchType)triple.getArch(); *sub_arch_type = (ZigLLVM_SubArchType)triple.getSubArch(); -- cgit v1.2.3 From 5a3c02137e6a15d3b8c1fb3595d55003ea44139a Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 8 Oct 2018 13:24:39 -0400 Subject: 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(-) (limited to 'src/zig_llvm.cpp') 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"); -- cgit v1.2.3