Commit ba144b366c
Changed files (4)
src/codegen.cpp
@@ -8275,7 +8275,8 @@ static Error get_tmp_filename(CodeGen *g, Buf *out, Buf *suffix) {
return ErrorNone;
}
-static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {
+// returns true if it was a cache miss
+static bool gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {
Error err;
Buf *artifact_dir;
@@ -8346,8 +8347,8 @@ static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {
}
exit(1);
}
- if (buf_len(&digest) == 0) {
- // cache miss
+ bool is_cache_miss = (buf_len(&digest) == 0);
+ if (is_cache_miss) {
// we can't know the digest until we do the C compiler invocation, so we
// need a tmp filename.
Buf *out_obj_path = buf_alloc();
@@ -8428,10 +8429,12 @@ static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {
}
break;
case BuildModeFastRelease:
+ args.append("-DNDEBUG");
args.append("-O2");
args.append("-fno-stack-protector");
break;
case BuildModeSmallRelease:
+ args.append("-DNDEBUG");
args.append("-Os");
args.append("-fno-stack-protector");
break;
@@ -8504,13 +8507,17 @@ static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {
g->link_objects.append(o_final_path);
g->caches_to_release.append(cache_hash);
+
+ return is_cache_miss;
}
-static void gen_c_objects(CodeGen *g) {
+// returns true if we had any cache misses
+static bool gen_c_objects(CodeGen *g) {
Error err;
+ bool any_cache_misses = false;
if (g->c_source_files.length == 0)
- return;
+ return any_cache_misses;
Buf *self_exe_path = buf_alloc();
if ((err = os_self_exe_path(self_exe_path))) {
@@ -8522,8 +8529,10 @@ static void gen_c_objects(CodeGen *g) {
for (size_t c_file_i = 0; c_file_i < g->c_source_files.length; c_file_i += 1) {
CFile *c_file = g->c_source_files.at(c_file_i);
- gen_c_object(g, self_exe_path, c_file);
+ bool is_cache_miss = gen_c_object(g, self_exe_path, c_file);
+ any_cache_misses = any_cache_misses || is_cache_miss;
}
+ return any_cache_misses;
}
void codegen_add_object(CodeGen *g, Buf *object_path) {
@@ -9020,7 +9029,8 @@ static void add_cache_pkg(CodeGen *g, CacheHash *ch, ZigPackage *pkg) {
}
// Called before init()
-static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
+// is_cache_hit takes into account gen_c_objects
+static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest, bool *c_objects_generated) {
Error err;
Buf *compiler_id;
@@ -9081,7 +9091,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
}
cache_buf(ch, g->dynamic_linker_path);
- gen_c_objects(g);
+ *c_objects_generated = gen_c_objects(g);
buf_resize(digest, 0);
if ((err = cache_hit(ch, digest)))
@@ -9188,11 +9198,12 @@ void codegen_build_and_link(CodeGen *g) {
Buf *artifact_dir = buf_alloc();
Buf digest = BUF_INIT;
+ bool any_c_objects_generated;
if (g->enable_cache) {
Buf *manifest_dir = buf_alloc();
os_path_join(g->cache_dir, buf_create_from_str("build"), manifest_dir);
- if ((err = check_cache(g, manifest_dir, &digest))) {
+ if ((err = check_cache(g, manifest_dir, &digest, &any_c_objects_generated))) {
if (err == ErrorCacheUnavailable) {
// message already printed
} else {
@@ -9204,24 +9215,28 @@ void codegen_build_and_link(CodeGen *g) {
os_path_join(g->cache_dir, buf_create_from_str("artifact"), artifact_dir);
} else {
// There is a call to this in check_cache
- gen_c_objects(g);
+ any_c_objects_generated = gen_c_objects(g);
}
- if (g->enable_cache && buf_len(&digest) != 0) {
+ if (g->enable_cache && buf_len(&digest) != 0 && !any_c_objects_generated) {
os_path_join(artifact_dir, &digest, &g->artifact_dir);
resolve_out_paths(g);
} else {
- init(g);
+ if (need_llvm_module(g)) {
+ init(g);
- codegen_add_time_event(g, "Semantic Analysis");
+ codegen_add_time_event(g, "Semantic Analysis");
- gen_global_asm(g);
- gen_root_source(g);
+ gen_global_asm(g);
+ gen_root_source(g);
+ }
if (g->enable_cache) {
- if ((err = cache_final(&g->cache_hash, &digest))) {
- fprintf(stderr, "Unable to finalize cache hash: %s\n", err_str(err));
- exit(1);
+ if (buf_len(&digest) == 0) {
+ if ((err = cache_final(&g->cache_hash, &digest))) {
+ fprintf(stderr, "Unable to finalize cache hash: %s\n", err_str(err));
+ exit(1);
+ }
}
os_path_join(artifact_dir, &digest, &g->artifact_dir);
} else {
@@ -9233,9 +9248,9 @@ void codegen_build_and_link(CodeGen *g) {
}
resolve_out_paths(g);
- codegen_add_time_event(g, "Code Generation");
-
if (need_llvm_module(g)) {
+ codegen_add_time_event(g, "Code Generation");
+
do_code_gen(g);
codegen_add_time_event(g, "LLVM Emit Output");
zig_llvm_emit_output(g);
src/compiler.cpp
@@ -10,6 +10,7 @@ static Buf saved_lib_dir = BUF_INIT;
static Buf saved_special_dir = BUF_INIT;
static Buf saved_std_dir = BUF_INIT;
static Buf saved_dynamic_linker_path = BUF_INIT;
+static bool searched_for_dyn_linker = false;
Buf *get_stage1_cache_path(void) {
if (saved_stage1_path.list.length != 0) {
@@ -24,6 +25,35 @@ Buf *get_stage1_cache_path(void) {
return &saved_stage1_path;
}
+static void detect_dynamic_linker(Buf *lib_path) {
+#if defined(ZIG_OS_LINUX) && defined(ZIG_ARCH_X86_64)
+ if (buf_ends_with_str(lib_path, "ld-linux-x86-64.so.2")) {
+ buf_init_from_buf(&saved_dynamic_linker_path, lib_path);
+ } else if (buf_ends_with_str(lib_path, "ld-musl-x86-64.so.1")) {
+ buf_init_from_buf(&saved_dynamic_linker_path, lib_path);
+ }
+#endif
+}
+
+Buf *get_self_dynamic_linker_path(void) {
+ for (;;) {
+ if (saved_dynamic_linker_path.list.length != 0) {
+ return &saved_dynamic_linker_path;
+ }
+ if (searched_for_dyn_linker)
+ return nullptr;
+ ZigList<Buf *> lib_paths = {};
+ Error err;
+ if ((err = os_self_exe_shared_libs(lib_paths)))
+ return nullptr;
+ for (size_t i = 0; i < lib_paths.length; i += 1) {
+ Buf *lib_path = lib_paths.at(i);
+ detect_dynamic_linker(lib_path);
+ }
+ searched_for_dyn_linker = true;
+ }
+}
+
Error get_compiler_id(Buf **result) {
if (saved_compiler_id.list.length != 0) {
*result = &saved_compiler_id;
@@ -57,15 +87,9 @@ Error get_compiler_id(Buf **result) {
return err;
for (size_t i = 0; i < lib_paths.length; i += 1) {
Buf *lib_path = lib_paths.at(i);
+ detect_dynamic_linker(lib_path);
if ((err = cache_add_file(ch, lib_path)))
return err;
-#if defined(ZIG_OS_LINUX) && defined(ZIG_ARCH_X86_64)
- if (buf_ends_with_str(lib_path, "ld-linux-x86-64.so.2")) {
- buf_init_from_buf(&saved_dynamic_linker_path, lib_path);
- } else if (buf_ends_with_str(lib_path, "ld-musl-x86-64.so.1")) {
- buf_init_from_buf(&saved_dynamic_linker_path, lib_path);
- }
-#endif
}
if ((err = cache_final(ch, &saved_compiler_id)))
return err;
@@ -76,12 +100,6 @@ Error get_compiler_id(Buf **result) {
return ErrorNone;
}
-Buf *get_self_dynamic_linker_path(void) {
- Buf *dontcare;
- (void)get_compiler_id(&dontcare); // for the side-effects of caching the dynamic linker path
- return (saved_dynamic_linker_path.list.length == 0) ? nullptr : &saved_dynamic_linker_path;
-}
-
static bool test_zig_install_prefix(Buf *test_path, Buf *out_zig_lib_dir) {
Buf lib_buf = BUF_INIT;
buf_init_from_str(&lib_buf, "lib");
src/link.cpp
@@ -62,14 +62,22 @@ static const char *build_libc_object(CodeGen *parent_gen, const char *name, CFil
return buf_ptr(&child_gen->output_file_path);
}
-static const char *path_from_libc(CodeGen *g, const char *subpath) {
- Buf *libc_dir = buf_alloc();
- os_path_join(g->zig_lib_dir, buf_create_from_str("libc"), libc_dir);
+static const char *path_from_zig_lib(CodeGen *g, const char *dir, const char *subpath) {
+ Buf *dir1 = buf_alloc();
+ os_path_join(g->zig_lib_dir, buf_create_from_str(dir), dir1);
Buf *result = buf_alloc();
- os_path_join(libc_dir, buf_create_from_str(subpath), result);
+ os_path_join(dir1, buf_create_from_str(subpath), result);
return buf_ptr(result);
}
+static const char *path_from_libc(CodeGen *g, const char *subpath) {
+ return path_from_zig_lib(g, "libc", subpath);
+}
+
+static const char *path_from_libunwind(CodeGen *g, const char *subpath) {
+ return path_from_zig_lib(g, "libunwind", subpath);
+}
+
static const char *build_dummy_so(CodeGen *parent, const char *name, size_t major_version) {
Buf *glibc_dummy_root_src = buf_sprintf("%s" OS_SEP "libc" OS_SEP "dummy" OS_SEP "%s.zig",
buf_ptr(parent->zig_lib_dir), name);
@@ -81,6 +89,70 @@ static const char *build_dummy_so(CodeGen *parent, const char *name, size_t majo
return buf_ptr(&child_gen->output_file_path);
}
+static const char *build_libunwind(CodeGen *parent) {
+ CodeGen *child_gen = create_child_codegen(parent, nullptr, OutTypeLib, nullptr);
+ codegen_set_out_name(child_gen, buf_create_from_str("unwind"));
+ child_gen->is_static = true;
+ LinkLib *new_link_lib = codegen_add_link_lib(child_gen, buf_create_from_str("c"));
+ new_link_lib->provided_explicitly = false;
+ enum SrcKind {
+ SrcCpp,
+ SrcC,
+ SrcAsm,
+ };
+ static const struct {
+ const char *path;
+ SrcKind kind;
+ } unwind_src[] = {
+ {"src" OS_SEP "libunwind.cpp", SrcCpp},
+ {"src" OS_SEP "Unwind-EHABI.cpp", SrcCpp},
+ {"src" OS_SEP "Unwind-seh.cpp", SrcCpp},
+
+ {"src" OS_SEP "UnwindLevel1.c", SrcC},
+ {"src" OS_SEP "UnwindLevel1-gcc-ext.c", SrcC},
+ {"src" OS_SEP "Unwind-sjlj.c", SrcC},
+
+ {"src" OS_SEP "UnwindRegistersRestore.S", SrcAsm},
+ {"src" OS_SEP "UnwindRegistersSave.S", SrcAsm},
+ };
+ ZigList<CFile *> c_source_files = {0};
+ for (size_t i = 0; i < array_length(unwind_src); i += 1) {
+ CFile *c_file = allocate<CFile>(1);
+ c_file->source_path = path_from_libunwind(parent, unwind_src[i].path);
+ switch (unwind_src[i].kind) {
+ case SrcC:
+ c_file->args.append("-std=c99");
+ break;
+ case SrcCpp:
+ c_file->args.append("-fno-rtti");
+ c_file->args.append("-I");
+ c_file->args.append(path_from_zig_lib(parent, "libcxx", "include"));
+ break;
+ case SrcAsm:
+ break;
+ }
+ c_file->args.append("-I");
+ c_file->args.append(path_from_libunwind(parent, "include"));
+ c_file->args.append("-fPIC");
+ c_file->args.append("-D_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS");
+ if (parent->zig_target->is_native) {
+ c_file->args.append("-D_LIBUNWIND_IS_NATIVE_ONLY");
+ }
+ if (parent->build_mode == BuildModeDebug) {
+ c_file->args.append("-D_DEBUG");
+ } else {
+ c_file->args.append("-NDEBUG");
+ }
+ if (parent->is_single_threaded) {
+ c_file->args.append("-D_LIBUNWIND_HAS_NO_THREADS");
+ }
+ c_source_files.append(c_file);
+ }
+ child_gen->c_source_files = c_source_files;
+ codegen_build_and_link(child_gen);
+ return buf_ptr(&child_gen->output_file_path);
+}
+
static const char *get_libc_crt_file(CodeGen *parent, const char *file) {
if (parent->libc == nullptr && target_is_glibc(parent)) {
if (strcmp(file, "crti.o") == 0) {
@@ -611,6 +683,7 @@ static void construct_linker_job_elf(LinkJob *lj) {
lj->args.append("-lgcc_s");
lj->args.append("--no-as-needed");
} else if (target_is_glibc(g)) {
+ lj->args.append(build_libunwind(g));
lj->args.append(get_libc_crt_file(g, "libc.so.6")); // this is our dummy so file
lj->args.append(get_libc_crt_file(g, "libc_nonshared.a"));
} else {
@@ -1192,6 +1265,13 @@ void codegen_link(CodeGen *g) {
}
ZigLLVM_OSType os_type = get_llvm_os_type(g->zig_target->os);
codegen_add_time_event(g, "LLVM Link");
+ if (g->verbose_link) {
+ fprintf(stderr, "ar rcs %s", buf_ptr(&g->output_file_path));
+ for (size_t i = 0; i < g->link_objects.length; i += 1) {
+ fprintf(stderr, " %s", (const char *)buf_ptr(g->link_objects.at(i)));
+ }
+ fprintf(stderr, "\n");
+ }
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);
CMakeLists.txt
@@ -1408,6 +1408,219 @@ set(ZIG_LIBC_FILES
"glibc/time/time.h"
)
+set(ZIG_LIBUNWIND_FILES
+ "include/__libunwind_config.h"
+ "include/libunwind.h"
+ "include/mach-o/compact_unwind_encoding.h"
+ "include/unwind.h"
+ "src/AddressSpace.hpp"
+ "src/CompactUnwinder.hpp"
+ "src/DwarfInstructions.hpp"
+ "src/DwarfParser.hpp"
+ "src/EHHeaderParser.hpp"
+ "src/RWMutex.hpp"
+ "src/Registers.hpp"
+ "src/Unwind-EHABI.cpp"
+ "src/Unwind-EHABI.h"
+ "src/Unwind-seh.cpp"
+ "src/Unwind-sjlj.c"
+ "src/UnwindCursor.hpp"
+ "src/UnwindLevel1-gcc-ext.c"
+ "src/UnwindLevel1.c"
+ "src/UnwindRegistersRestore.S"
+ "src/UnwindRegistersSave.S"
+ "src/Unwind_AppleExtras.cpp"
+ "src/assembly.h"
+ "src/config.h"
+ "src/dwarf2.h"
+ "src/libunwind.cpp"
+ "src/libunwind_ext.h"
+)
+
+set(ZIG_LIBCXX_FILES
+ "include/__bit_reference"
+ "include/__bsd_locale_defaults.h"
+ "include/__bsd_locale_fallbacks.h"
+ "include/__config"
+ "include/__config_site.in"
+ "include/__debug"
+ "include/__errc"
+ "include/__functional_03"
+ "include/__functional_base"
+ "include/__functional_base_03"
+ "include/__hash_table"
+ "include/__libcpp_version"
+ "include/__locale"
+ "include/__mutex_base"
+ "include/__node_handle"
+ "include/__nullptr"
+ "include/__split_buffer"
+ "include/__sso_allocator"
+ "include/__std_stream"
+ "include/__string"
+ "include/__threading_support"
+ "include/__tree"
+ "include/__tuple"
+ "include/__undef_macros"
+ "include/algorithm"
+ "include/any"
+ "include/array"
+ "include/atomic"
+ "include/bit"
+ "include/bitset"
+ "include/cassert"
+ "include/ccomplex"
+ "include/cctype"
+ "include/cerrno"
+ "include/cfenv"
+ "include/cfloat"
+ "include/charconv"
+ "include/chrono"
+ "include/cinttypes"
+ "include/ciso646"
+ "include/climits"
+ "include/clocale"
+ "include/cmath"
+ "include/codecvt"
+ "include/compare"
+ "include/complex"
+ "include/complex.h"
+ "include/condition_variable"
+ "include/csetjmp"
+ "include/csignal"
+ "include/cstdarg"
+ "include/cstdbool"
+ "include/cstddef"
+ "include/cstdint"
+ "include/cstdio"
+ "include/cstdlib"
+ "include/cstring"
+ "include/ctgmath"
+ "include/ctime"
+ "include/ctype.h"
+ "include/cwchar"
+ "include/cwctype"
+ "include/deque"
+ "include/errno.h"
+ "include/exception"
+ "include/experimental/__config"
+ "include/experimental/__memory"
+ "include/experimental/algorithm"
+ "include/experimental/any"
+ "include/experimental/chrono"
+ "include/experimental/coroutine"
+ "include/experimental/deque"
+ "include/experimental/filesystem"
+ "include/experimental/forward_list"
+ "include/experimental/functional"
+ "include/experimental/iterator"
+ "include/experimental/list"
+ "include/experimental/map"
+ "include/experimental/memory_resource"
+ "include/experimental/numeric"
+ "include/experimental/optional"
+ "include/experimental/propagate_const"
+ "include/experimental/ratio"
+ "include/experimental/regex"
+ "include/experimental/set"
+ "include/experimental/simd"
+ "include/experimental/string"
+ "include/experimental/string_view"
+ "include/experimental/system_error"
+ "include/experimental/tuple"
+ "include/experimental/type_traits"
+ "include/experimental/unordered_map"
+ "include/experimental/unordered_set"
+ "include/experimental/utility"
+ "include/experimental/vector"
+ "include/ext/__hash"
+ "include/ext/hash_map"
+ "include/ext/hash_set"
+ "include/filesystem"
+ "include/float.h"
+ "include/forward_list"
+ "include/fstream"
+ "include/functional"
+ "include/future"
+ "include/initializer_list"
+ "include/inttypes.h"
+ "include/iomanip"
+ "include/ios"
+ "include/iosfwd"
+ "include/iostream"
+ "include/istream"
+ "include/iterator"
+ "include/limits"
+ "include/limits.h"
+ "include/list"
+ "include/locale"
+ "include/locale.h"
+ "include/map"
+ "include/math.h"
+ "include/memory"
+ "include/module.modulemap"
+ "include/mutex"
+ "include/new"
+ "include/numeric"
+ "include/optional"
+ "include/ostream"
+ "include/queue"
+ "include/random"
+ "include/ratio"
+ "include/regex"
+ "include/scoped_allocator"
+ "include/set"
+ "include/setjmp.h"
+ "include/shared_mutex"
+ "include/span"
+ "include/sstream"
+ "include/stack"
+ "include/stdbool.h"
+ "include/stddef.h"
+ "include/stdexcept"
+ "include/stdint.h"
+ "include/stdio.h"
+ "include/stdlib.h"
+ "include/streambuf"
+ "include/string"
+ "include/string.h"
+ "include/string_view"
+ "include/strstream"
+ "include/support/android/locale_bionic.h"
+ "include/support/fuchsia/xlocale.h"
+ "include/support/ibm/limits.h"
+ "include/support/ibm/locale_mgmt_aix.h"
+ "include/support/ibm/support.h"
+ "include/support/ibm/xlocale.h"
+ "include/support/musl/xlocale.h"
+ "include/support/newlib/xlocale.h"
+ "include/support/solaris/floatingpoint.h"
+ "include/support/solaris/wchar.h"
+ "include/support/solaris/xlocale.h"
+ "include/support/win32/limits_msvc_win32.h"
+ "include/support/win32/locale_win32.h"
+ "include/support/xlocale/__nop_locale_mgmt.h"
+ "include/support/xlocale/__posix_l_fallback.h"
+ "include/support/xlocale/__strtonum_fallback.h"
+ "include/support/xlocale/xlocale.h"
+ "include/system_error"
+ "include/tgmath.h"
+ "include/thread"
+ "include/tuple"
+ "include/type_traits"
+ "include/typeindex"
+ "include/typeinfo"
+ "include/unordered_map"
+ "include/unordered_set"
+ "include/utility"
+ "include/valarray"
+ "include/variant"
+ "include/vector"
+ "include/version"
+ "include/wchar.h"
+ "include/wctype.h"
+)
+
if(MSVC)
set(MSVC_DIA_SDK_DIR "$ENV{VSINSTALLDIR}DIA SDK")
if (IS_DIRECTORY ${MSVC_DIA_SDK_DIR})
@@ -1419,6 +1632,8 @@ endif()
set(ZIG_LIB_DIR "lib/zig")
set(C_HEADERS_DEST "${ZIG_LIB_DIR}/include")
set(LIBC_FILES_DEST "${ZIG_LIB_DIR}/libc")
+set(LIBUNWIND_FILES_DEST "${ZIG_LIB_DIR}/libunwind")
+set(LIBCXX_FILES_DEST "${ZIG_LIB_DIR}/libcxx")
set(ZIG_STD_DEST "${ZIG_LIB_DIR}/std")
set(CONFIGURE_OUT_FILE "${CMAKE_BINARY_DIR}/config.h")
configure_file (
@@ -1527,6 +1742,15 @@ foreach(file ${ZIG_LIBC_FILES})
install(FILES "${CMAKE_SOURCE_DIR}/libc/${file}" DESTINATION "${file_dir}")
endforeach()
+foreach(file ${ZIG_LIBUNWIND_FILES})
+ get_filename_component(file_dir "${LIBUNWIND_FILES_DEST}/${file}" DIRECTORY)
+ install(FILES "${CMAKE_SOURCE_DIR}/libunwind/${file}" DESTINATION "${file_dir}")
+endforeach()
+
+foreach(file ${ZIG_LIBCXX_FILES})
+ get_filename_component(file_dir "${LIBCXX_FILES_DEST}/${file}" DIRECTORY)
+ install(FILES "${CMAKE_SOURCE_DIR}/libcxx/${file}" DESTINATION "${file_dir}")
+endforeach()
install(FILES "${CMAKE_SOURCE_DIR}/src-self-hosted/arg.zig" DESTINATION "${ZIG_STD_DEST}/special/fmt/")
install(FILES "${CMAKE_SOURCE_DIR}/src-self-hosted/main.zig" DESTINATION "${ZIG_STD_DEST}/special/fmt/")