From f407109070be2a187c71cc2350ac6d9b7d64a3d0 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 27 Mar 2020 12:38:52 -0400 Subject: [PATCH] zig c++: get it working with musl and mingw-w64 --- lib/libc/mingw/misc/_create_locale.c | 33 +++ lib/libc/mingw/misc/_free_locale.c | 31 +++ lib/libcxx/src/filesystem/int128_builtins.cpp | 54 ---- lib/libcxxabi/src/stdlib_new_delete.cpp | 262 ------------------ src-self-hosted/stage2.zig | 4 + src/all_types.hpp | 2 + src/analyze.hpp | 1 + src/codegen.cpp | 27 +- src/install_files.h | 2 - src/link.cpp | 64 ++++- src/main.cpp | 18 ++ src/stage2.h | 4 + src/target.cpp | 7 + src/target.hpp | 1 + tools/update_clang_options.zig | 16 ++ 15 files changed, 199 insertions(+), 327 deletions(-) create mode 100644 lib/libc/mingw/misc/_create_locale.c create mode 100644 lib/libc/mingw/misc/_free_locale.c delete mode 100644 lib/libcxx/src/filesystem/int128_builtins.cpp delete mode 100644 lib/libcxxabi/src/stdlib_new_delete.cpp diff --git a/lib/libc/mingw/misc/_create_locale.c b/lib/libc/mingw/misc/_create_locale.c new file mode 100644 index 0000000000..00947b3ac1 --- /dev/null +++ b/lib/libc/mingw/misc/_create_locale.c @@ -0,0 +1,33 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include +#include +#include + +static _locale_t __cdecl init_func(int category, const char *locale); +_locale_t (__cdecl *__MINGW_IMP_SYMBOL(_create_locale))(int, const char *) = init_func; + +static _locale_t __cdecl null_func(int category, const char *locale) +{ + (void)category; + (void)locale; + return NULL; +} + +static _locale_t __cdecl init_func(int category, const char *locale) +{ + HMODULE msvcrt = __mingw_get_msvcrt_handle(); + _locale_t (__cdecl *func)(int, const char *) = NULL; + + if (msvcrt) + func = (void*)GetProcAddress(msvcrt, "_create_locale"); + + if (!func) + func = null_func; + + return (__MINGW_IMP_SYMBOL(_create_locale) = func)(category, locale); +} diff --git a/lib/libc/mingw/misc/_free_locale.c b/lib/libc/mingw/misc/_free_locale.c new file mode 100644 index 0000000000..d94e2274fa --- /dev/null +++ b/lib/libc/mingw/misc/_free_locale.c @@ -0,0 +1,31 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include +#include +#include + +static void __cdecl init_func(_locale_t locale); +void (__cdecl *__MINGW_IMP_SYMBOL(_free_locale))(_locale_t) = init_func; + +static void __cdecl stub_func(_locale_t locale) +{ + (void)locale; +} + +static void __cdecl init_func(_locale_t locale) +{ + HMODULE msvcrt = __mingw_get_msvcrt_handle(); + void (__cdecl *func)(_locale_t) = NULL; + + if (msvcrt) + func = (void*)GetProcAddress(msvcrt, "_free_locale"); + + if (!func) + func = stub_func; + + (__MINGW_IMP_SYMBOL(_free_locale) = func)(locale); +} diff --git a/lib/libcxx/src/filesystem/int128_builtins.cpp b/lib/libcxx/src/filesystem/int128_builtins.cpp deleted file mode 100644 index ed531ee145..0000000000 --- a/lib/libcxx/src/filesystem/int128_builtins.cpp +++ /dev/null @@ -1,54 +0,0 @@ -/*===-- int128_builtins.cpp - Implement __muloti4 --------------------------=== - * - * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. - * See https://llvm.org/LICENSE.txt for license information. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - * - * ===----------------------------------------------------------------------=== - * - * This file implements __muloti4, and is stolen from the compiler_rt library. - * - * FIXME: we steal and re-compile it into filesystem, which uses __int128_t, - * and requires this builtin when sanitized. See llvm.org/PR30643 - * - * ===----------------------------------------------------------------------=== - */ -#include "__config" -#include "climits" - -#if !defined(_LIBCPP_HAS_NO_INT128) - -extern "C" __attribute__((no_sanitize("undefined"))) _LIBCPP_FUNC_VIS -__int128_t __muloti4(__int128_t a, __int128_t b, int* overflow) { - const int N = (int)(sizeof(__int128_t) * CHAR_BIT); - const __int128_t MIN = (__int128_t)1 << (N - 1); - const __int128_t MAX = ~MIN; - *overflow = 0; - __int128_t result = a * b; - if (a == MIN) { - if (b != 0 && b != 1) - *overflow = 1; - return result; - } - if (b == MIN) { - if (a != 0 && a != 1) - *overflow = 1; - return result; - } - __int128_t sa = a >> (N - 1); - __int128_t abs_a = (a ^ sa) - sa; - __int128_t sb = b >> (N - 1); - __int128_t abs_b = (b ^ sb) - sb; - if (abs_a < 2 || abs_b < 2) - return result; - if (sa == sb) { - if (abs_a > MAX / abs_b) - *overflow = 1; - } else { - if (abs_a > MIN / -abs_b) - *overflow = 1; - } - return result; -} - -#endif diff --git a/lib/libcxxabi/src/stdlib_new_delete.cpp b/lib/libcxxabi/src/stdlib_new_delete.cpp deleted file mode 100644 index 698c5f7c29..0000000000 --- a/lib/libcxxabi/src/stdlib_new_delete.cpp +++ /dev/null @@ -1,262 +0,0 @@ -//===--------------------- stdlib_new_delete.cpp --------------------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -// -// This file implements the new and delete operators. -//===----------------------------------------------------------------------===// - -#define _LIBCPP_BUILDING_LIBRARY -#include "__cxxabi_config.h" -#include -#include - -#if !defined(_THROW_BAD_ALLOC) || !defined(_NOEXCEPT) || !defined(_LIBCXXABI_WEAK) -#error The _THROW_BAD_ALLOC, _NOEXCEPT, and _LIBCXXABI_WEAK libc++ macros must \ - already be defined by libc++. -#endif -// Implement all new and delete operators as weak definitions -// in this shared library, so that they can be overridden by programs -// that define non-weak copies of the functions. - -_LIBCXXABI_WEAK -void * -operator new(std::size_t size) _THROW_BAD_ALLOC -{ - if (size == 0) - size = 1; - void* p; - while ((p = ::malloc(size)) == 0) - { - // If malloc fails and there is a new_handler, - // call it to try free up memory. - std::new_handler nh = std::get_new_handler(); - if (nh) - nh(); - else -#ifndef _LIBCXXABI_NO_EXCEPTIONS - throw std::bad_alloc(); -#else - break; -#endif - } - return p; -} - -_LIBCXXABI_WEAK -void* -operator new(size_t size, const std::nothrow_t&) _NOEXCEPT -{ - void* p = 0; -#ifndef _LIBCXXABI_NO_EXCEPTIONS - try - { -#endif // _LIBCXXABI_NO_EXCEPTIONS - p = ::operator new(size); -#ifndef _LIBCXXABI_NO_EXCEPTIONS - } - catch (...) - { - } -#endif // _LIBCXXABI_NO_EXCEPTIONS - return p; -} - -_LIBCXXABI_WEAK -void* -operator new[](size_t size) _THROW_BAD_ALLOC -{ - return ::operator new(size); -} - -_LIBCXXABI_WEAK -void* -operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT -{ - void* p = 0; -#ifndef _LIBCXXABI_NO_EXCEPTIONS - try - { -#endif // _LIBCXXABI_NO_EXCEPTIONS - p = ::operator new[](size); -#ifndef _LIBCXXABI_NO_EXCEPTIONS - } - catch (...) - { - } -#endif // _LIBCXXABI_NO_EXCEPTIONS - return p; -} - -_LIBCXXABI_WEAK -void -operator delete(void* ptr) _NOEXCEPT -{ - if (ptr) - ::free(ptr); -} - -_LIBCXXABI_WEAK -void -operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT -{ - ::operator delete(ptr); -} - -_LIBCXXABI_WEAK -void -operator delete(void* ptr, size_t) _NOEXCEPT -{ - ::operator delete(ptr); -} - -_LIBCXXABI_WEAK -void -operator delete[] (void* ptr) _NOEXCEPT -{ - ::operator delete(ptr); -} - -_LIBCXXABI_WEAK -void -operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT -{ - ::operator delete[](ptr); -} - -_LIBCXXABI_WEAK -void -operator delete[] (void* ptr, size_t) _NOEXCEPT -{ - ::operator delete[](ptr); -} - -#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) - -_LIBCXXABI_WEAK -void * -operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC -{ - if (size == 0) - size = 1; - if (static_cast(alignment) < sizeof(void*)) - alignment = std::align_val_t(sizeof(void*)); - void* p; -#if defined(_LIBCPP_WIN32API) - while ((p = _aligned_malloc(size, static_cast(alignment))) == nullptr) -#else - while (::posix_memalign(&p, static_cast(alignment), size) != 0) -#endif - { - // If posix_memalign fails and there is a new_handler, - // call it to try free up memory. - std::new_handler nh = std::get_new_handler(); - if (nh) - nh(); - else { -#ifndef _LIBCXXABI_NO_EXCEPTIONS - throw std::bad_alloc(); -#else - p = nullptr; // posix_memalign doesn't initialize 'p' on failure - break; -#endif - } - } - return p; -} - -_LIBCXXABI_WEAK -void* -operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT -{ - void* p = 0; -#ifndef _LIBCXXABI_NO_EXCEPTIONS - try - { -#endif // _LIBCXXABI_NO_EXCEPTIONS - p = ::operator new(size, alignment); -#ifndef _LIBCXXABI_NO_EXCEPTIONS - } - catch (...) - { - } -#endif // _LIBCXXABI_NO_EXCEPTIONS - return p; -} - -_LIBCXXABI_WEAK -void* -operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC -{ - return ::operator new(size, alignment); -} - -_LIBCXXABI_WEAK -void* -operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT -{ - void* p = 0; -#ifndef _LIBCXXABI_NO_EXCEPTIONS - try - { -#endif // _LIBCXXABI_NO_EXCEPTIONS - p = ::operator new[](size, alignment); -#ifndef _LIBCXXABI_NO_EXCEPTIONS - } - catch (...) - { - } -#endif // _LIBCXXABI_NO_EXCEPTIONS - return p; -} - -_LIBCXXABI_WEAK -void -operator delete(void* ptr, std::align_val_t) _NOEXCEPT -{ - if (ptr) -#if defined(_LIBCPP_WIN32API) - ::_aligned_free(ptr); -#else - ::free(ptr); -#endif -} - -_LIBCXXABI_WEAK -void -operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT -{ - ::operator delete(ptr, alignment); -} - -_LIBCXXABI_WEAK -void -operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT -{ - ::operator delete(ptr, alignment); -} - -_LIBCXXABI_WEAK -void -operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT -{ - ::operator delete(ptr, alignment); -} - -_LIBCXXABI_WEAK -void -operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT -{ - ::operator delete[](ptr, alignment); -} - -_LIBCXXABI_WEAK -void -operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT -{ - ::operator delete[](ptr, alignment); -} - -#endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION diff --git a/src-self-hosted/stage2.zig b/src-self-hosted/stage2.zig index aa4d0ad492..028dc21df9 100644 --- a/src-self-hosted/stage2.zig +++ b/src-self-hosted/stage2.zig @@ -1283,6 +1283,10 @@ pub const ClangArgIterator = extern struct { sanitize, linker_script, verbose_cmds, + exceptions, + no_exceptions, + rtti, + no_rtti, }; fn init(argv: []const [*:0]const u8) ClangArgIterator { diff --git a/src/all_types.hpp b/src/all_types.hpp index f51a9c0572..6c7b465025 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -2262,6 +2262,8 @@ struct CodeGen { bool emit_asm; bool emit_llvm_ir; bool test_is_evented; + bool cpp_rtti; + bool cpp_exceptions; CodeModel code_model; Buf *root_out_name; diff --git a/src/analyze.hpp b/src/analyze.hpp index a7828663b0..ee069427bd 100644 --- a/src/analyze.hpp +++ b/src/analyze.hpp @@ -260,6 +260,7 @@ ZigLLVMDIType *get_llvm_di_type(CodeGen *g, ZigType *type); enum CSourceKind { CSourceKindAsm, CSourceKindC, + CSourceKindCpp, }; void add_cc_args(CodeGen *g, ZigList &args, const char *out_dep_path, bool translate_c, diff --git a/src/codegen.cpp b/src/codegen.cpp index d9e011c4f3..f02253fb1a 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -8448,6 +8448,8 @@ static bool detect_dynamic_link(CodeGen *g) { LinkLib *link_lib = g->link_libs_list.at(i); if (target_is_libc_lib_name(g->zig_target, buf_ptr(link_lib->name))) continue; + if (target_is_libcpp_lib_name(g->zig_target, buf_ptr(link_lib->name))) + continue; return true; } return false; @@ -8782,6 +8784,8 @@ static Error define_builtin_compile_vars(CodeGen *g) { cache_bool(&cache_hash, g->is_test_build); cache_bool(&cache_hash, g->is_single_threaded); cache_bool(&cache_hash, g->test_is_evented); + cache_bool(&cache_hash, g->cpp_rtti); + cache_bool(&cache_hash, g->cpp_exceptions); cache_int(&cache_hash, g->code_model); cache_int(&cache_hash, g->zig_target->is_native_os); cache_int(&cache_hash, g->zig_target->is_native_cpu); @@ -9181,7 +9185,7 @@ void add_cc_args(CodeGen *g, ZigList &args, const char *out_dep_pa } if (translate_c) { - if (source_kind == CSourceKindC) { + if (source_kind != CSourceKindAsm) { // this gives us access to preprocessing entities, presumably at // the cost of performance args.append("-Xclang"); @@ -9213,6 +9217,12 @@ void add_cc_args(CodeGen *g, ZigList &args, const char *out_dep_pa args.append("-isystem"); args.append(libcxx_include_path); + + if (target_abi_is_musl(g->zig_target->abi)) { + args.append("-D_LIBCPP_HAS_MUSL_LIBC"); + } + args.append("-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS"); + args.append("-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS"); } // According to Rich Felker libc headers are supposed to go before C language headers. @@ -9232,6 +9242,7 @@ void add_cc_args(CodeGen *g, ZigList &args, const char *out_dep_pa switch (source_kind) { case CSourceKindC: + case CSourceKindCpp: if (g->zig_target->llvm_cpu_name != nullptr) { args.append("-Xclang"); args.append("-target-cpu"); @@ -9248,6 +9259,14 @@ void add_cc_args(CodeGen *g, ZigList &args, const char *out_dep_pa case CSourceKindAsm: break; } + if (source_kind == CSourceKindCpp) { + if (!g->cpp_rtti) { + args.append("-fno-rtti"); + } + if (!g->cpp_exceptions) { + args.append("-fno-exceptions"); + } + } for (size_t i = 0; i < g->zig_target->llvm_cpu_features_asm_len; i += 1) { args.append(g->zig_target->llvm_cpu_features_asm_ptr[i]); } @@ -9695,6 +9714,8 @@ Error create_c_object_cache(CodeGen *g, CacheHash **out_cache_hash, bool verbose cache_bool(cache_hash, g->have_sanitize_c); cache_bool(cache_hash, want_valgrind_support(g)); cache_bool(cache_hash, g->function_sections); + cache_bool(cache_hash, g->cpp_rtti); + cache_bool(cache_hash, g->cpp_exceptions); cache_int(cache_hash, g->code_model); for (size_t arg_i = 0; arg_i < g->clang_argv_len; arg_i += 1) { @@ -10529,6 +10550,8 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) { cache_bool(ch, g->emit_bin); cache_bool(ch, g->emit_llvm_ir); cache_bool(ch, g->emit_asm); + cache_bool(ch, g->cpp_rtti); + cache_bool(ch, g->cpp_exceptions); cache_usize(ch, g->version_major); cache_usize(ch, g->version_minor); cache_usize(ch, g->version_patch); @@ -10833,6 +10856,8 @@ CodeGen *create_child_codegen(CodeGen *parent_gen, Buf *root_src_path, OutType o parent_gen->build_mode, parent_gen->zig_lib_dir, libc, get_global_cache_dir(), false, child_progress_node); child_gen->root_out_name = buf_create_from_str(name); child_gen->disable_gen_h = true; + child_gen->cpp_rtti = parent_gen->cpp_rtti; + child_gen->cpp_exceptions = parent_gen->cpp_exceptions; child_gen->want_stack_check = WantStackCheckDisabled; child_gen->want_sanitize_c = WantCSanitizeDisabled; child_gen->verbose_tokenize = parent_gen->verbose_tokenize; diff --git a/src/install_files.h b/src/install_files.h index e3a5f95763..8e7431145e 100644 --- a/src/install_files.h +++ b/src/install_files.h @@ -1859,7 +1859,6 @@ static const char *ZIG_LIBCXXABI_FILES[] = { "src/fallback_malloc.cpp", "src/private_typeinfo.cpp", "src/stdlib_exception.cpp", -"src/stdlib_new_delete.cpp", "src/stdlib_stdexcept.cpp", "src/stdlib_typeinfo.cpp", }; @@ -1875,7 +1874,6 @@ static const char *ZIG_LIBCXX_FILES[] = { "src/exception.cpp", "src/experimental/memory_resource.cpp", "src/filesystem/directory_iterator.cpp", -"src/filesystem/int128_builtins.cpp", "src/filesystem/operations.cpp", "src/functional.cpp", "src/future.cpp", diff --git a/src/link.cpp b/src/link.cpp index 9dfc01af8f..d9b784129d 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -14,6 +14,8 @@ #include "glibc.hpp" static const char *msvcrt_common_src[] = { + "misc" OS_SEP "_create_locale.c", + "misc" OS_SEP "_free_locale.c", "misc" OS_SEP "onexit_table.c", "misc" OS_SEP "register_tls_atexit.c", "stdio" OS_SEP "acrt_iob_func.c", @@ -632,12 +634,17 @@ static const char *build_libunwind(CodeGen *parent, Stage2ProgressNode *progress } c_file->args.append("-I"); c_file->args.append(path_from_libunwind(parent, "include")); - c_file->args.append("-fPIC"); + if (target_supports_fpic(parent->zig_target)) { + c_file->args.append("-fPIC"); + } c_file->args.append("-D_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS"); c_file->args.append("-Wa,--noexecstack"); - if (parent->zig_target->is_native_os && parent->zig_target->is_native_cpu) { - c_file->args.append("-D_LIBUNWIND_IS_NATIVE_ONLY"); - } + + // This is intentionally always defined because the macro definition means, should it only + // build for the target specified by compiler defines. Since we pass -target the compiler + // defines will be correct. + c_file->args.append("-D_LIBUNWIND_IS_NATIVE_ONLY"); + if (parent->build_mode == BuildModeDebug) { c_file->args.append("-D_DEBUG"); } @@ -1129,6 +1136,12 @@ static const char *build_libcxxabi(CodeGen *parent, Stage2ProgressNode *progress c_file->args.append("-D_LIBCPP_DISABLE_EXTERN_TEMPLATE"); c_file->args.append("-D_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS"); c_file->args.append("-D_LIBCXXABI_BUILDING_LIBRARY"); + c_file->args.append("-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS"); + c_file->args.append("-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS"); + + if (target_abi_is_musl(parent->zig_target->abi)) { + c_file->args.append("-D_LIBCPP_HAS_MUSL_LIBC"); + } c_file->args.append("-I"); c_file->args.append(cxxabi_include_path); @@ -1138,7 +1151,9 @@ static const char *build_libcxxabi(CodeGen *parent, Stage2ProgressNode *progress c_file->args.append("-O3"); c_file->args.append("-DNDEBUG"); - c_file->args.append("-fPIC"); + if (target_supports_fpic(parent->zig_target)) { + c_file->args.append("-fPIC"); + } c_file->args.append("-nostdinc++"); c_file->args.append("-fstrict-aliasing"); c_file->args.append("-funwind-tables"); @@ -1170,8 +1185,15 @@ static const char *build_libcxx(CodeGen *parent, Stage2ProgressNode *progress_no const char *rel_src_path = ZIG_LIBCXX_FILES[i]; Buf *src_path_buf = buf_create_from_str(rel_src_path); - if (buf_starts_with_str(src_path_buf, "src/support/win32") && parent->zig_target->os != OsWindows) { - continue; + if (parent->zig_target->os == OsWindows) { + // filesystem stuff isn't supported on Windows + if (buf_starts_with_str(src_path_buf, "src/filesystem/")) { + continue; + } + } else { + if (buf_starts_with_str(src_path_buf, "src/support/win32/")) { + continue; + } } CFile *c_file = heap::c_allocator.create(); @@ -1182,6 +1204,12 @@ static const char *build_libcxx(CodeGen *parent, Stage2ProgressNode *progress_no c_file->args.append("-D_LIBCPP_BUILDING_LIBRARY"); c_file->args.append("-D_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER"); c_file->args.append("-DLIBCXX_BUILDING_LIBCXXABI"); + c_file->args.append("-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS"); + c_file->args.append("-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS"); + + if (target_abi_is_musl(parent->zig_target->abi)) { + c_file->args.append("-D_LIBCPP_HAS_MUSL_LIBC"); + } c_file->args.append("-I"); c_file->args.append(cxx_include_path); @@ -1191,7 +1219,9 @@ static const char *build_libcxx(CodeGen *parent, Stage2ProgressNode *progress_no c_file->args.append("-O3"); c_file->args.append("-DNDEBUG"); - c_file->args.append("-fPIC"); + if (target_supports_fpic(parent->zig_target)) { + c_file->args.append("-fPIC"); + } c_file->args.append("-nostdinc++"); c_file->args.append("-fvisibility-inlines-hidden"); c_file->args.append("-std=c++14"); @@ -1923,6 +1953,8 @@ static void construct_linker_job_elf(LinkJob *lj) { lj->args.append(build_libunwind(g, lj->build_dep_prog_node)); } lj->args.append(build_musl(g, lj->build_dep_prog_node)); + } else if (g->libcpp_link_lib != nullptr) { + lj->args.append(build_libunwind(g, lj->build_dep_prog_node)); } else { zig_unreachable(); } @@ -2411,6 +2443,13 @@ static void construct_linker_job_coff(LinkJob *lj) { break; } + // libc++ dep + if (g->libcpp_link_lib != nullptr && g->out_type != OutTypeObj) { + lj->args.append(build_libcxxabi(g, lj->build_dep_prog_node)); + lj->args.append(build_libcxx(g, lj->build_dep_prog_node)); + lj->args.append(build_libunwind(g, lj->build_dep_prog_node)); + } + if (g->out_type == OutTypeExe || (g->out_type == OutTypeLib && g->is_dynamic)) { if (g->libc_link_lib == nullptr && !g->is_dummy_so) { Buf *libc_a_path = build_c(g, OutTypeLib, lj->build_dep_prog_node); @@ -2427,6 +2466,9 @@ static void construct_linker_job_coff(LinkJob *lj) { if (buf_eql_str(link_lib->name, "c")) { continue; } + if (buf_eql_str(link_lib->name, "c++") || buf_eql_str(link_lib->name, "c++abi")) { + continue; + } bool is_sys_lib = is_mingw_link_lib(link_lib->name); if (have_windows_dll_import_libs && is_sys_lib) { continue; @@ -2562,6 +2604,12 @@ static void construct_linker_job_macho(LinkJob *lj) { lj->args.append((const char *)buf_ptr(g->link_objects.at(i))); } + // libc++ dep + if (g->libcpp_link_lib != nullptr && g->out_type != OutTypeObj) { + lj->args.append(build_libcxxabi(g, lj->build_dep_prog_node)); + lj->args.append(build_libcxx(g, lj->build_dep_prog_node)); + } + // compiler_rt on darwin is missing some stuff, so we still build it and rely on LinkOnce if (g->out_type == OutTypeExe || is_dyn_lib) { Buf *compiler_rt_o_path = build_compiler_rt(g, OutTypeLib, lj->build_dep_prog_node); diff --git a/src/main.cpp b/src/main.cpp index ae44185d04..d2936c3b52 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -458,6 +458,8 @@ static int main0(int argc, char **argv) { bool only_preprocess = false; bool ensure_libc_on_non_freestanding = false; bool ensure_libcpp_on_non_freestanding = false; + bool cpp_rtti = true; + bool cpp_exceptions = true; ZigList llvm_argv = {0}; llvm_argv.append("zig (LLVM option parsing)"); @@ -720,6 +722,18 @@ static int main0(int argc, char **argv) { verbose_cc = true; verbose_link = true; break; + case Stage2ClangArgExceptions: + cpp_exceptions = true; + break; + case Stage2ClangArgNoExceptions: + cpp_exceptions = false; + break; + case Stage2ClangArgRtti: + cpp_rtti = true; + break; + case Stage2ClangArgNoRtti: + cpp_rtti = false; + break; } } // Parse linker args @@ -1303,6 +1317,8 @@ static int main0(int argc, char **argv) { LinkLib *link_lib = codegen_add_link_lib(g, buf_create_from_str(link_libs.at(i))); link_lib->provided_explicitly = true; } + g->cpp_rtti = cpp_rtti; + g->cpp_exceptions = cpp_exceptions; g->subsystem = subsystem; g->valgrind_support = valgrind_support; g->want_pic = want_pic; @@ -1449,6 +1465,8 @@ static int main0(int argc, char **argv) { } CodeGen *g = codegen_create(main_pkg_path, zig_root_source_file, &target, out_type, build_mode, override_lib_dir, libc, cache_dir_buf, cmd == CmdTest, root_progress_node); + g->cpp_rtti = cpp_rtti; + g->cpp_exceptions = cpp_exceptions; if (llvm_argv.length >= 2) codegen_set_llvm_argv(g, llvm_argv.items + 1, llvm_argv.length - 2); g->valgrind_support = valgrind_support; g->link_eh_frame_hdr = link_eh_frame_hdr; diff --git a/src/stage2.h b/src/stage2.h index 42ef1ba612..9549f45ffe 100644 --- a/src/stage2.h +++ b/src/stage2.h @@ -344,6 +344,10 @@ enum Stage2ClangArg { Stage2ClangArgSanitize, Stage2ClangArgLinkerScript, Stage2ClangArgVerboseCmds, + Stage2ClangArgExceptions, + Stage2ClangArgNoExceptions, + Stage2ClangArgRtti, + Stage2ClangArgNoRtti, }; // ABI warning diff --git a/src/target.cpp b/src/target.cpp index 63360fc029..59b1addc33 100644 --- a/src/target.cpp +++ b/src/target.cpp @@ -1229,6 +1229,13 @@ bool target_is_libc_lib_name(const ZigTarget *target, const char *name) { return false; } +bool target_is_libcpp_lib_name(const ZigTarget *target, const char *name) { + if (strcmp(name, "c++") == 0 || strcmp(name, "c++abi") == 0) + return true; + + return false; +} + size_t target_libc_count(void) { return array_length(libcs_available); } diff --git a/src/target.hpp b/src/target.hpp index 01f2c6b168..c3f8530e02 100644 --- a/src/target.hpp +++ b/src/target.hpp @@ -102,6 +102,7 @@ bool target_os_requires_libc(Os os); bool target_can_build_libc(const ZigTarget *target); const char *target_libc_generic_name(const ZigTarget *target); bool target_is_libc_lib_name(const ZigTarget *target, const char *name); +bool target_is_libcpp_lib_name(const ZigTarget *target, const char *name); bool target_supports_fpic(const ZigTarget *target); bool target_supports_clang_march_native(const ZigTarget *target); bool target_requires_pic(const ZigTarget *target, bool linking_libc); diff --git a/tools/update_clang_options.zig b/tools/update_clang_options.zig index 46f2275b69..94dee90142 100644 --- a/tools/update_clang_options.zig +++ b/tools/update_clang_options.zig @@ -158,6 +158,22 @@ const known_options = [_]KnownOpt{ .name = "###", .ident = "verbose_cmds", }, + .{ + .name = "fexceptions", + .ident = "exceptions", + }, + .{ + .name = "fno-exceptions", + .ident = "no_exceptions", + }, + .{ + .name = "frtti", + .ident = "rtti", + }, + .{ + .name = "fno-rtti", + .ident = "no_rtti", + }, }; const blacklisted_options = [_][]const u8{};