From ecd2332bb5c74739ade40f1b9eaf412b4642a36f Mon Sep 17 00:00:00 2001 From: emekoi Date: Fri, 29 Mar 2019 19:25:47 -0500 Subject: [PATCH 1/6] made lld flags on windows consistent --- src/link.cpp | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/link.cpp b/src/link.cpp index c2d46f0ac6..65f4060105 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -1122,14 +1122,14 @@ static bool zig_lld_link(ZigLLVM_ObjectFormatType oformat, const char **args, si } static void add_uefi_link_args(LinkJob *lj) { - lj->args.append("/BASE:0"); - lj->args.append("/ENTRY:EfiMain"); - lj->args.append("/OPT:REF"); - lj->args.append("/SAFESEH:NO"); - lj->args.append("/MERGE:.rdata=.data"); - lj->args.append("/ALIGN:32"); - lj->args.append("/NODEFAULTLIB"); - lj->args.append("/SECTION:.xdata,D"); + lj->args.append("-BASE:0"); + lj->args.append("-ENTRY:EfiMain"); + lj->args.append("-OPT:REF"); + lj->args.append("-SAFESEH:NO"); + lj->args.append("-MERGE:.rdata=.data"); + lj->args.append("-ALIGN:32"); + lj->args.append("-NODEFAULTLIB"); + lj->args.append("-SECTION:.xdata,D"); } static void add_nt_link_args(LinkJob *lj, bool is_library) { @@ -1163,12 +1163,12 @@ static void add_nt_link_args(LinkJob *lj, bool is_library) { lj->args.append("kernel32.lib"); lj->args.append("ntdll.lib"); } else { - lj->args.append("/NODEFAULTLIB"); + lj->args.append("-NODEFAULTLIB"); if (!is_library) { if (g->have_winmain) { - lj->args.append("/ENTRY:WinMain"); + lj->args.append("-ENTRY:WinMain"); } else { - lj->args.append("/ENTRY:WinMainCRTStartup"); + lj->args.append("-ENTRY:WinMainCRTStartup"); } } } @@ -1178,17 +1178,17 @@ static void construct_linker_job_coff(LinkJob *lj) { Error err; CodeGen *g = lj->codegen; - lj->args.append("/ERRORLIMIT:0"); + lj->args.append("-ERRORLIMIT:0"); - lj->args.append("/NOLOGO"); + lj->args.append("-NOLOGO"); if (!g->strip_debug_symbols) { - lj->args.append("/DEBUG"); + lj->args.append("-DEBUG"); } if (g->out_type == OutTypeExe) { // TODO compile time stack upper bound detection - lj->args.append("/STACK:16777216"); + lj->args.append("-STACK:16777216"); } coff_append_machine_arg(g, &lj->args); @@ -1203,35 +1203,35 @@ static void construct_linker_job_coff(LinkJob *lj) { } break; case TargetSubsystemConsole: - lj->args.append("/SUBSYSTEM:console"); + lj->args.append("-SUBSYSTEM:console"); add_nt_link_args(lj, is_library); break; case TargetSubsystemEfiApplication: - lj->args.append("/SUBSYSTEM:efi_application"); + lj->args.append("-SUBSYSTEM:efi_application"); add_uefi_link_args(lj); break; case TargetSubsystemEfiBootServiceDriver: - lj->args.append("/SUBSYSTEM:efi_boot_service_driver"); + lj->args.append("-SUBSYSTEM:efi_boot_service_driver"); add_uefi_link_args(lj); break; case TargetSubsystemEfiRom: - lj->args.append("/SUBSYSTEM:efi_rom"); + lj->args.append("-SUBSYSTEM:efi_rom"); add_uefi_link_args(lj); break; case TargetSubsystemEfiRuntimeDriver: - lj->args.append("/SUBSYSTEM:efi_runtime_driver"); + lj->args.append("-SUBSYSTEM:efi_runtime_driver"); add_uefi_link_args(lj); break; case TargetSubsystemNative: - lj->args.append("/SUBSYSTEM:native"); + lj->args.append("-SUBSYSTEM:native"); add_nt_link_args(lj, is_library); break; case TargetSubsystemPosix: - lj->args.append("/SUBSYSTEM:posix"); + lj->args.append("-SUBSYSTEM:posix"); add_nt_link_args(lj, is_library); break; case TargetSubsystemWindows: - lj->args.append("/SUBSYSTEM:windows"); + lj->args.append("-SUBSYSTEM:windows"); add_nt_link_args(lj, is_library); break; } From 6cc443ee457d06ea23713bb78ab832d886029d57 Mon Sep 17 00:00:00 2001 From: emekoi Date: Fri, 29 Mar 2019 20:37:15 -0500 Subject: [PATCH 2/6] added code for linking libc on mingw --- src/link.cpp | 198 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 143 insertions(+), 55 deletions(-) diff --git a/src/link.cpp b/src/link.cpp index 65f4060105..d7532d96e3 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -1132,40 +1132,114 @@ static void add_uefi_link_args(LinkJob *lj) { lj->args.append("-SECTION:.xdata,D"); } -static void add_nt_link_args(LinkJob *lj, bool is_library) { +static void add_msvc_link_args(LinkJob *lj, bool is_library) { CodeGen *g = lj->codegen; - if (lj->link_in_crt) { - // TODO: https://github.com/ziglang/zig/issues/2064 - bool is_dynamic = true; // g->is_dynamic; - const char *lib_str = is_dynamic ? "" : "lib"; - const char *d_str = (g->build_mode == BuildModeDebug) ? "d" : ""; + // TODO: https://github.com/ziglang/zig/issues/2064 + bool is_dynamic = true; // g->is_dynamic; + const char *lib_str = is_dynamic ? "" : "lib"; + const char *d_str = (g->build_mode == BuildModeDebug) ? "d" : ""; - if (!is_dynamic) { - Buf *cmt_lib_name = buf_sprintf("libcmt%s.lib", d_str); - lj->args.append(buf_ptr(cmt_lib_name)); + if (!is_dynamic) { + Buf *cmt_lib_name = buf_sprintf("libcmt%s.lib", d_str); + lj->args.append(buf_ptr(cmt_lib_name)); + } else { + Buf *msvcrt_lib_name = buf_sprintf("msvcrt%s.lib", d_str); + lj->args.append(buf_ptr(msvcrt_lib_name)); + } + + Buf *vcruntime_lib_name = buf_sprintf("%svcruntime%s.lib", lib_str, d_str); + lj->args.append(buf_ptr(vcruntime_lib_name)); + + Buf *crt_lib_name = buf_sprintf("%sucrt%s.lib", lib_str, d_str); + lj->args.append(buf_ptr(crt_lib_name)); + + //Visual C++ 2015 Conformance Changes + //https://msdn.microsoft.com/en-us/library/bb531344.aspx + lj->args.append("legacy_stdio_definitions.lib"); + + // msvcrt depends on kernel32 and ntdll + lj->args.append("kernel32.lib"); + lj->args.append("ntdll.lib"); +} + +static const char *get_libc_file(ZigLibCInstallation *lib, const char *file) { + Buf *out_buf = buf_alloc(); + os_path_join(&lib->crt_dir, buf_create_from_str(file), out_buf); + return buf_ptr(out_buf); +} + +static const char *get_libc_static_file(ZigLibCInstallation *lib, const char *file) { + Buf *static_crt_dir = buf_alloc(); + Buf *out_buf = buf_alloc(); + if (zig_libc_cc_print_file_name(file, static_crt_dir, true, true) != ErrorNone) { + abort(); + } + os_path_join(static_crt_dir, buf_create_from_str(file), out_buf); + return buf_ptr(out_buf); +} + +static void add_gnu_link_args(LinkJob *lj, bool is_library) { + CodeGen *g = lj->codegen; + + bool is_dll = g->out_type == OutTypeLib && g->is_dynamic; + + if (g->zig_target->arch == ZigLLVM_x86) { + lj->args.append("-ALTERNATENAME:__image_base__=___ImageBase"); + } else { + lj->args.append("-ALTERNATENAME:__image_base__=__ImageBase"); + } + + if (is_dll) { + lj->args.append(get_libc_file(g->libc, "dllcrt2.o")); + } else { + lj->args.append(get_libc_file(g->libc, "crt2.o")); + } + + lj->args.append(get_libc_static_file(g->libc, "crtbegin.o")); + + if (g->libc_link_lib != nullptr) { + lj->args.append("libmingw32.a"); + + if (is_dll) { + lj->args.append(get_libc_static_file(g->libc, "libgcc_s.a")); + lj->args.append(get_libc_static_file(g->libc, "libgcc.a")); } else { - Buf *msvcrt_lib_name = buf_sprintf("msvcrt%s.lib", d_str); - lj->args.append(buf_ptr(msvcrt_lib_name)); + lj->args.append(get_libc_static_file(g->libc, "libgcc.a")); + lj->args.append(get_libc_static_file(g->libc, "libgcc_eh.a")); } - Buf *vcruntime_lib_name = buf_sprintf("%svcruntime%s.lib", lib_str, d_str); - lj->args.append(buf_ptr(vcruntime_lib_name)); + lj->args.append(get_libc_static_file(g->libc, "libssp.a")); + lj->args.append("libmoldname.a"); + lj->args.append("libmingwex.a"); + lj->args.append("libmsvcrt.a"); - Buf *crt_lib_name = buf_sprintf("%sucrt%s.lib", lib_str, d_str); - lj->args.append(buf_ptr(crt_lib_name)); + if (g->subsystem == TargetSubsystemWindows) { + lj->args.append("libgdi32.a"); + lj->args.append("libcomdlg32.a"); + } - //Visual C++ 2015 Conformance Changes - //https://msdn.microsoft.com/en-us/library/bb531344.aspx - lj->args.append("legacy_stdio_definitions.lib"); + lj->args.append("libadvapi32.a"); + lj->args.append("libadvapi32.a"); + lj->args.append("libshell32.a"); + lj->args.append("libuser32.a"); + lj->args.append("libkernel32.a"); - // msvcrt depends on kernel32 and ntdll - lj->args.append("kernel32.lib"); - lj->args.append("ntdll.lib"); + lj->args.append(get_libc_static_file(g->libc, "crtend.o")); + } +} + +static void add_win_link_args(LinkJob *lj, bool is_library) { + if (lj->link_in_crt) { + if (target_abi_is_gnu(lj->codegen->zig_target->abi)) { + add_gnu_link_args(lj, is_library); + } else { + add_msvc_link_args(lj, is_library); + } } else { lj->args.append("-NODEFAULTLIB"); if (!is_library) { - if (g->have_winmain) { + if (lj->codegen->have_winmain) { lj->args.append("-ENTRY:WinMain"); } else { lj->args.append("-ENTRY:WinMainCRTStartup"); @@ -1194,17 +1268,46 @@ static void construct_linker_job_coff(LinkJob *lj) { coff_append_machine_arg(g, &lj->args); bool is_library = g->out_type == OutTypeLib; + if (is_library && g->is_dynamic) { + lj->args.append("-DLL"); + } + + lj->args.append(buf_ptr(buf_sprintf("-OUT:%s", buf_ptr(&g->output_file_path)))); + + if (g->libc_link_lib != nullptr) { + assert(g->libc != nullptr); + + lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(&g->libc->crt_dir)))); + + if (target_abi_is_gnu(g->zig_target->abi)) { + lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(&g->libc->sys_include_dir)))); + lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(&g->libc->include_dir)))); + } else { + lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(&g->libc->msvc_lib_dir)))); + lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(&g->libc->kernel32_lib_dir)))); + } + } + + for (size_t i = 0; i < g->lib_dirs.length; i += 1) { + const char *lib_dir = g->lib_dirs.at(i); + lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", lib_dir))); + } + + for (size_t i = 0; i < g->link_objects.length; i += 1) { + lj->args.append((const char *)buf_ptr(g->link_objects.at(i))); + } + switch (g->subsystem) { case TargetSubsystemAuto: if (g->zig_target->os == OsUefi) { add_uefi_link_args(lj); } else { - add_nt_link_args(lj, is_library); + add_win_link_args(lj, is_library); } break; case TargetSubsystemConsole: lj->args.append("-SUBSYSTEM:console"); - add_nt_link_args(lj, is_library); + add_win_link_args(lj, is_library); break; case TargetSubsystemEfiApplication: lj->args.append("-SUBSYSTEM:efi_application"); @@ -1224,41 +1327,18 @@ static void construct_linker_job_coff(LinkJob *lj) { break; case TargetSubsystemNative: lj->args.append("-SUBSYSTEM:native"); - add_nt_link_args(lj, is_library); + add_win_link_args(lj, is_library); break; case TargetSubsystemPosix: lj->args.append("-SUBSYSTEM:posix"); - add_nt_link_args(lj, is_library); + add_win_link_args(lj, is_library); break; case TargetSubsystemWindows: lj->args.append("-SUBSYSTEM:windows"); - add_nt_link_args(lj, is_library); + add_win_link_args(lj, is_library); break; } - lj->args.append(buf_ptr(buf_sprintf("-OUT:%s", buf_ptr(&g->output_file_path)))); - - if (g->libc_link_lib != nullptr) { - assert(g->libc != nullptr); - - lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(&g->libc->msvc_lib_dir)))); - lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(&g->libc->kernel32_lib_dir)))); - lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(&g->libc->crt_dir)))); - } - - if (is_library && g->is_dynamic) { - lj->args.append("-DLL"); - } - - for (size_t i = 0; i < g->lib_dirs.length; i += 1) { - const char *lib_dir = g->lib_dirs.at(i); - lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", lib_dir))); - } - - for (size_t i = 0; i < g->link_objects.length; i += 1) { - lj->args.append((const char *)buf_ptr(g->link_objects.at(i))); - } - if (g->out_type == OutTypeExe || (g->out_type == OutTypeLib && g->is_dynamic)) { if (g->libc_link_lib == nullptr && !g->is_dummy_so) { Buf *builtin_a_path = build_a(g, "builtin"); @@ -1278,11 +1358,19 @@ static void construct_linker_job_coff(LinkJob *lj) { continue; } if (link_lib->provided_explicitly) { - if (lj->codegen->zig_target->abi == ZigLLVM_GNU) { - Buf *arg = buf_sprintf("-l%s", buf_ptr(link_lib->name)); - lj->args.append(buf_ptr(arg)); - } - else { + if (target_abi_is_gnu(lj->codegen->zig_target->abi)) { + static const char *test_names[3] = { "lib%s.dll.a", "lib%s.a", nullptr }; + + for (size_t i = 0; test_names[i] != nullptr; i++) { + Buf *test_path = buf_sprintf(test_names[i], buf_ptr(link_lib->name)); + bool exists = false; + if (os_file_exists(test_path, &exists) != ErrorNone) { + zig_panic("link: unable to check if file exists: %s", buf_ptr(test_path)); + } else if (exists) { + lj->args.append(buf_ptr(test_path)); + } + } + } else { lj->args.append(buf_ptr(link_lib->name)); } } else { From 0bd4901a174786a61fadf484f3b8932169533cd4 Mon Sep 17 00:00:00 2001 From: emekoi Date: Sat, 30 Mar 2019 00:06:04 -0500 Subject: [PATCH 3/6] fixed linking of system libraries on mingw --- src/libc_installation.hpp | 2 -- src/link.cpp | 24 ++++++++++++++++-------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/libc_installation.hpp b/src/libc_installation.hpp index 765ae4ec56..171cc963bf 100644 --- a/src/libc_installation.hpp +++ b/src/libc_installation.hpp @@ -29,8 +29,6 @@ void zig_libc_render(ZigLibCInstallation *self, FILE *file); Error ATTRIBUTE_MUST_USE zig_libc_find_native(ZigLibCInstallation *self, bool verbose); -#if defined(ZIG_OS_LINUX) || defined(ZIG_OS_WINDOWS) Error zig_libc_cc_print_file_name(const char *o_file, Buf *out, bool want_dirname, bool verbose); -#endif #endif diff --git a/src/link.cpp b/src/link.cpp index d7532d96e3..00d70d5e63 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -1359,17 +1359,25 @@ static void construct_linker_job_coff(LinkJob *lj) { } if (link_lib->provided_explicitly) { if (target_abi_is_gnu(lj->codegen->zig_target->abi)) { - static const char *test_names[3] = { "lib%s.dll.a", "lib%s.a", nullptr }; + static const char *test_names[3] = { "%s\\lib%s.dll.a", "%s\\lib%s.a", nullptr }; + bool exists = false; - for (size_t i = 0; test_names[i] != nullptr; i++) { - Buf *test_path = buf_sprintf(test_names[i], buf_ptr(link_lib->name)); - bool exists = false; - if (os_file_exists(test_path, &exists) != ErrorNone) { - zig_panic("link: unable to check if file exists: %s", buf_ptr(test_path)); - } else if (exists) { - lj->args.append(buf_ptr(test_path)); + for (size_t i = 0; test_names[i] != nullptr && !exists; i++) { + for (size_t j = 0; j < g->lib_dirs.length; j += 1) { + const char *lib_dir = g->lib_dirs.at(j); + Buf *test_path = buf_sprintf(test_names[i], lib_dir, buf_ptr(link_lib->name)); + if (os_file_exists(test_path, &exists) != ErrorNone) { + zig_panic("link: unable to check if file exists: %s", buf_ptr(test_path)); + } else if (exists) { + lj->args.append(buf_ptr(test_path)); + break; + } } } + + if (!exists) { + zig_panic("link: unable to find library: %s", buf_ptr(link_lib->name)); + } } else { lj->args.append(buf_ptr(link_lib->name)); } From f4c5fa7ff46ad088e437dba237518df76736f087 Mon Sep 17 00:00:00 2001 From: emekoi Date: Sat, 20 Apr 2019 00:12:33 -0500 Subject: [PATCH 4/6] renamed add_gnu_link_args --- src/link.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/link.cpp b/src/link.cpp index 00d70d5e63..db5494ee54 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -1179,7 +1179,7 @@ static const char *get_libc_static_file(ZigLibCInstallation *lib, const char *fi return buf_ptr(out_buf); } -static void add_gnu_link_args(LinkJob *lj, bool is_library) { +static void add_mingw_link_args(LinkJob *lj, bool is_library) { CodeGen *g = lj->codegen; bool is_dll = g->out_type == OutTypeLib && g->is_dynamic; @@ -1232,7 +1232,7 @@ static void add_gnu_link_args(LinkJob *lj, bool is_library) { static void add_win_link_args(LinkJob *lj, bool is_library) { if (lj->link_in_crt) { if (target_abi_is_gnu(lj->codegen->zig_target->abi)) { - add_gnu_link_args(lj, is_library); + add_mingw_link_args(lj, is_library); } else { add_msvc_link_args(lj, is_library); } From dfada0cc77cb77a337baa784e4e96c7a94d217bb Mon Sep 17 00:00:00 2001 From: emekoi Date: Sat, 27 Apr 2019 00:24:26 -0500 Subject: [PATCH 5/6] added static_crt_dir to libc file --- src/libc_installation.cpp | 36 +++++++++++++++--- src/libc_installation.hpp | 3 +- src/link.cpp | 79 ++++++++++++++------------------------- 3 files changed, 59 insertions(+), 59 deletions(-) diff --git a/src/libc_installation.cpp b/src/libc_installation.cpp index 0862ffec9b..243b8d6400 100644 --- a/src/libc_installation.cpp +++ b/src/libc_installation.cpp @@ -14,6 +14,7 @@ static const char *zig_libc_keys[] = { "include_dir", "sys_include_dir", "crt_dir", + "static_crt_dir", "msvc_lib_dir", "kernel32_lib_dir", }; @@ -34,6 +35,7 @@ static void zig_libc_init_empty(ZigLibCInstallation *libc) { buf_init_from_str(&libc->include_dir, ""); buf_init_from_str(&libc->sys_include_dir, ""); buf_init_from_str(&libc->crt_dir, ""); + buf_init_from_str(&libc->static_crt_dir, ""); buf_init_from_str(&libc->msvc_lib_dir, ""); buf_init_from_str(&libc->kernel32_lib_dir, ""); } @@ -74,8 +76,9 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget match = match || zig_libc_match_key(name, value, found_keys, 0, &libc->include_dir); match = match || zig_libc_match_key(name, value, found_keys, 1, &libc->sys_include_dir); match = match || zig_libc_match_key(name, value, found_keys, 2, &libc->crt_dir); - match = match || zig_libc_match_key(name, value, found_keys, 3, &libc->msvc_lib_dir); - match = match || zig_libc_match_key(name, value, found_keys, 4, &libc->kernel32_lib_dir); + match = match || zig_libc_match_key(name, value, found_keys, 3, &libc->static_crt_dir); + match = match || zig_libc_match_key(name, value, found_keys, 4, &libc->msvc_lib_dir); + match = match || zig_libc_match_key(name, value, found_keys, 5, &libc->kernel32_lib_dir); } for (size_t i = 0; i < zig_libc_keys_len; i += 1) { @@ -110,6 +113,15 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget } } + if (buf_len(&libc->static_crt_dir) == 0) { + if (target->os == OsWindows && target_abi_is_gnu(target->abi)) { + if (verbose) { + fprintf(stderr, "static_crt_dir may not be empty for %s\n", target_os_name(target->os)); + } + return ErrorSemanticAnalyzeFail; + } + } + if (buf_len(&libc->msvc_lib_dir) == 0) { if (target->os == OsWindows && !target_abi_is_gnu(target->abi)) { if (verbose) { @@ -261,7 +273,7 @@ static Error zig_libc_find_native_include_dir_posix(ZigLibCInstallation *self, b return ErrorFileNotFound; } -Error zig_libc_cc_print_file_name(const char *o_file, Buf *out, bool want_dirname, bool verbose) { +static Error zig_libc_cc_print_file_name(const char *o_file, Buf *out, bool want_dirname, bool verbose) { const char *cc_exe = getenv("CC"); cc_exe = (cc_exe == nullptr) ? CC_EXE : cc_exe; ZigList args = {}; @@ -308,6 +320,10 @@ static Error zig_libc_find_native_crt_dir_posix(ZigLibCInstallation *self, bool return zig_libc_cc_print_file_name("crt1.o", &self->crt_dir, true, verbose); } +static Error zig_libc_find_native_static_crt_dir_posix(ZigLibCInstallation *self, bool verbose) { + return zig_libc_cc_print_file_name("crtbegin.o", &self->static_crt_dir, true, verbose); +} + #if defined(ZIG_OS_WINDOWS) static Error zig_libc_find_native_include_dir_windows(ZigLibCInstallation *self, ZigWindowsSDK *sdk, bool verbose) { Error err; @@ -320,7 +336,7 @@ static Error zig_libc_find_native_include_dir_windows(ZigLibCInstallation *self, return ErrorNone; } -static Error zig_libc_find_crt_dir_windows(ZigLibCInstallation *self, ZigWindowsSDK *sdk, ZigTarget *target, +static Error zig_libc_find_native_crt_dir_windows(ZigLibCInstallation *self, ZigWindowsSDK *sdk, ZigTarget *target, bool verbose) { Error err; @@ -396,11 +412,16 @@ void zig_libc_render(ZigLibCInstallation *self, FILE *file) { "# On POSIX it's the directory that includes `sys/errno.h`.\n" "sys_include_dir=%s\n" "\n" - "# The directory that contains `crt1.o`.\n" + "# The directory that contains `crt1.o` or `crt2.o`.\n" "# On POSIX, can be found with `cc -print-file-name=crt1.o`.\n" "# Not needed when targeting MacOS.\n" "crt_dir=%s\n" "\n" + "# The directory that contains `crtbegin.o`.\n" + "# On POSIX, can be found with `cc -print-file-name=crtbegin.o`.\n" + "# Not needed when targeting MacOS.\n" + "static_crt_dir=%s\n" + "\n" "# The directory that contains `vcruntime.lib`.\n" "# Only needed when targeting MSVC on Windows.\n" "msvc_lib_dir=%s\n" @@ -413,6 +434,7 @@ void zig_libc_render(ZigLibCInstallation *self, FILE *file) { buf_ptr(&self->include_dir), buf_ptr(&self->sys_include_dir), buf_ptr(&self->crt_dir), + buf_ptr(&self->static_crt_dir), buf_ptr(&self->msvc_lib_dir), buf_ptr(&self->kernel32_lib_dir) ); @@ -429,6 +451,8 @@ Error zig_libc_find_native(ZigLibCInstallation *self, bool verbose) { return err; if ((err = zig_libc_find_native_crt_dir_posix(self, verbose))) return err; + if ((err = zig_libc_find_native_static_crt_dir_posix(self, verbose))) + return err; return ErrorNone; } else { ZigWindowsSDK *sdk; @@ -442,7 +466,7 @@ Error zig_libc_find_native(ZigLibCInstallation *self, bool verbose) { return err; if ((err = zig_libc_find_native_include_dir_windows(self, sdk, verbose))) return err; - if ((err = zig_libc_find_crt_dir_windows(self, sdk, &native_target, verbose))) + if ((err = zig_libc_find_native_crt_dir_windows(self, sdk, &native_target, verbose))) return err; return ErrorNone; case ZigFindWindowsSdkErrorOutOfMemory: diff --git a/src/libc_installation.hpp b/src/libc_installation.hpp index 171cc963bf..da06ac39ee 100644 --- a/src/libc_installation.hpp +++ b/src/libc_installation.hpp @@ -19,6 +19,7 @@ struct ZigLibCInstallation { Buf include_dir; Buf sys_include_dir; Buf crt_dir; + Buf static_crt_dir; Buf msvc_lib_dir; Buf kernel32_lib_dir; }; @@ -29,6 +30,4 @@ void zig_libc_render(ZigLibCInstallation *self, FILE *file); Error ATTRIBUTE_MUST_USE zig_libc_find_native(ZigLibCInstallation *self, bool verbose); -Error zig_libc_cc_print_file_name(const char *o_file, Buf *out, bool want_dirname, bool verbose); - #endif diff --git a/src/link.cpp b/src/link.cpp index db5494ee54..ad1a60065d 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -1170,12 +1170,8 @@ static const char *get_libc_file(ZigLibCInstallation *lib, const char *file) { } static const char *get_libc_static_file(ZigLibCInstallation *lib, const char *file) { - Buf *static_crt_dir = buf_alloc(); Buf *out_buf = buf_alloc(); - if (zig_libc_cc_print_file_name(file, static_crt_dir, true, true) != ErrorNone) { - abort(); - } - os_path_join(static_crt_dir, buf_create_from_str(file), out_buf); + os_path_join(&lib->static_crt_dir, buf_create_from_str(file), out_buf); return buf_ptr(out_buf); } @@ -1198,35 +1194,33 @@ static void add_mingw_link_args(LinkJob *lj, bool is_library) { lj->args.append(get_libc_static_file(g->libc, "crtbegin.o")); - if (g->libc_link_lib != nullptr) { - lj->args.append("libmingw32.a"); + lj->args.append(get_libc_file(g->libc, "libmingw32.a")); - if (is_dll) { - lj->args.append(get_libc_static_file(g->libc, "libgcc_s.a")); - lj->args.append(get_libc_static_file(g->libc, "libgcc.a")); - } else { - lj->args.append(get_libc_static_file(g->libc, "libgcc.a")); - lj->args.append(get_libc_static_file(g->libc, "libgcc_eh.a")); - } - - lj->args.append(get_libc_static_file(g->libc, "libssp.a")); - lj->args.append("libmoldname.a"); - lj->args.append("libmingwex.a"); - lj->args.append("libmsvcrt.a"); - - if (g->subsystem == TargetSubsystemWindows) { - lj->args.append("libgdi32.a"); - lj->args.append("libcomdlg32.a"); - } - - lj->args.append("libadvapi32.a"); - lj->args.append("libadvapi32.a"); - lj->args.append("libshell32.a"); - lj->args.append("libuser32.a"); - lj->args.append("libkernel32.a"); - - lj->args.append(get_libc_static_file(g->libc, "crtend.o")); + if (is_dll) { + lj->args.append(get_libc_static_file(g->libc, "libgcc_s.a")); + lj->args.append(get_libc_static_file(g->libc, "libgcc.a")); + } else { + lj->args.append(get_libc_static_file(g->libc, "libgcc.a")); + lj->args.append(get_libc_static_file(g->libc, "libgcc_eh.a")); } + + lj->args.append(get_libc_static_file(g->libc, "libssp.a")); + lj->args.append(get_libc_file(g->libc, "libmoldname.a")); + lj->args.append(get_libc_file(g->libc, "libmingwex.a")); + lj->args.append(get_libc_file(g->libc, "libmsvcrt.a")); + + if (g->subsystem == TargetSubsystemWindows) { + lj->args.append(get_libc_file(g->libc, "libgdi32.a")); + lj->args.append(get_libc_file(g->libc, "libcomdlg32.a")); + } + + lj->args.append(get_libc_file(g->libc, "libadvapi32.a")); + lj->args.append(get_libc_file(g->libc, "libadvapi32.a")); + lj->args.append(get_libc_file(g->libc, "libshell32.a")); + lj->args.append(get_libc_file(g->libc, "libuser32.a")); + lj->args.append(get_libc_file(g->libc, "libkernel32.a")); + + lj->args.append(get_libc_static_file(g->libc, "crtend.o")); } static void add_win_link_args(LinkJob *lj, bool is_library) { @@ -1359,25 +1353,8 @@ static void construct_linker_job_coff(LinkJob *lj) { } if (link_lib->provided_explicitly) { if (target_abi_is_gnu(lj->codegen->zig_target->abi)) { - static const char *test_names[3] = { "%s\\lib%s.dll.a", "%s\\lib%s.a", nullptr }; - bool exists = false; - - for (size_t i = 0; test_names[i] != nullptr && !exists; i++) { - for (size_t j = 0; j < g->lib_dirs.length; j += 1) { - const char *lib_dir = g->lib_dirs.at(j); - Buf *test_path = buf_sprintf(test_names[i], lib_dir, buf_ptr(link_lib->name)); - if (os_file_exists(test_path, &exists) != ErrorNone) { - zig_panic("link: unable to check if file exists: %s", buf_ptr(test_path)); - } else if (exists) { - lj->args.append(buf_ptr(test_path)); - break; - } - } - } - - if (!exists) { - zig_panic("link: unable to find library: %s", buf_ptr(link_lib->name)); - } + Buf *lib_name = buf_sprintf("lib%s.a", buf_ptr(link_lib->name)); + lj->args.append(buf_ptr(lib_name)); } else { lj->args.append(buf_ptr(link_lib->name)); } From 6057513cc753905a592e4810e6eb79ffb397cf73 Mon Sep 17 00:00:00 2001 From: emekoi Date: Sat, 27 Apr 2019 16:17:07 -0500 Subject: [PATCH 6/6] fixed visibility of zig_libc_cc_print_file_name --- src/libc_installation.cpp | 2 +- src/libc_installation.hpp | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libc_installation.cpp b/src/libc_installation.cpp index 243b8d6400..f44f3c6e77 100644 --- a/src/libc_installation.cpp +++ b/src/libc_installation.cpp @@ -273,7 +273,7 @@ static Error zig_libc_find_native_include_dir_posix(ZigLibCInstallation *self, b return ErrorFileNotFound; } -static Error zig_libc_cc_print_file_name(const char *o_file, Buf *out, bool want_dirname, bool verbose) { +Error zig_libc_cc_print_file_name(const char *o_file, Buf *out, bool want_dirname, bool verbose) { const char *cc_exe = getenv("CC"); cc_exe = (cc_exe == nullptr) ? CC_EXE : cc_exe; ZigList args = {}; diff --git a/src/libc_installation.hpp b/src/libc_installation.hpp index da06ac39ee..8ecad7ce61 100644 --- a/src/libc_installation.hpp +++ b/src/libc_installation.hpp @@ -30,4 +30,6 @@ void zig_libc_render(ZigLibCInstallation *self, FILE *file); Error ATTRIBUTE_MUST_USE zig_libc_find_native(ZigLibCInstallation *self, bool verbose); +Error zig_libc_cc_print_file_name(const char *o_file, Buf *out, bool want_dirname, bool verbose); + #endif