Commit abff1b6884

Andrew Kelley <superjoe30@gmail.com>
2017-11-02 03:47:15
windows: use the same libc search within a compilation unit
1 parent f7837f4
src/all_types.hpp
@@ -1462,10 +1462,11 @@ struct CodeGen {
     bool have_winmain_crt_startup;
     bool have_dllmain_crt_startup;
     bool have_pub_panic;
-    ZigList<Buf*> libc_lib_dirs_list;
     Buf *libc_lib_dir;
     Buf *libc_static_lib_dir;
     Buf *libc_include_dir;
+    Buf *msvc_lib_dir;
+    Buf *kernel32_lib_dir;
     Buf *zig_lib_dir;
     Buf *zig_std_dir;
     Buf *zig_c_headers_dir;
src/analyze.cpp
@@ -3370,23 +3370,27 @@ bool handle_is_ptr(TypeTableEntry *type_entry) {
     zig_unreachable();
 }
 
+static ZigWindowsSDK *get_windows_sdk(CodeGen *g) {
+    if (g->win_sdk == nullptr) {
+        if (os_find_windows_sdk(&g->win_sdk)) {
+            zig_panic("Unable to determine Windows SDK path.");
+        }
+    }
+    assert(g->win_sdk != nullptr);
+    return g->win_sdk;
+}
+
 void find_libc_include_path(CodeGen *g) {
-#ifdef ZIG_OS_WINDOWS
     if (!g->libc_include_dir || buf_len(g->libc_include_dir) == 0) {
-        if (g->win_sdk == nullptr) {
-            if (os_find_windows_sdk(&g->win_sdk)) {
-                zig_panic("Unable to determine Windows SDK path.");
-            }
-        }
+        ZigWindowsSDK *sdk = get_windows_sdk(g);
 
         if (g->zig_target.os == ZigLLVM_Win32) {
-            if (os_get_win32_ucrt_include_path(g->win_sdk, g->libc_include_dir)) {
+            if (os_get_win32_ucrt_include_path(sdk, g->libc_include_dir)) {
                 zig_panic("Unable to determine libc include path.");
             }
         }
     }
-    return;
-#endif
+
     // TODO find libc at runtime for other operating systems
     if(!g->libc_include_dir || buf_len(g->libc_include_dir) == 0) {
         zig_panic("Unable to determine libc include path.");
@@ -3394,39 +3398,34 @@ void find_libc_include_path(CodeGen *g) {
 }
 
 void find_libc_lib_path(CodeGen *g) {
-#ifdef ZIG_OS_WINDOWS
-    if (g->zig_target.os == ZigLLVM_Win32) {
-        if (g->win_sdk == nullptr) {
-            if (os_find_windows_sdk(&g->win_sdk)) {
-                zig_panic("Unable to determine Windows SDK path.");
+    // later we can handle this better by reporting an error via the normal mechanism
+    if (!g->libc_lib_dir || buf_len(g->libc_lib_dir) == 0 ||
+        (g->zig_target.os == ZigLLVM_Win32 && (g->msvc_lib_dir == nullptr || g->kernel32_lib_dir == nullptr)))
+    {
+        if (g->zig_target.os == ZigLLVM_Win32) {
+            ZigWindowsSDK *sdk = get_windows_sdk(g);
+
+            Buf* vc_lib_dir = buf_alloc();
+            if (os_get_win32_vcruntime_path(vc_lib_dir, g->zig_target.arch.arch)) {
+                zig_panic("Unable to determine vcruntime path.");
             }
-        }
 
-        Buf* vc_lib_dir = buf_alloc();
-        if (os_get_win32_vcruntime_path(vc_lib_dir, g->zig_target.arch.arch)) {
-            zig_panic("Unable to determine vcruntime path.");
-        }
+            Buf* ucrt_lib_path = buf_alloc();
+            if (os_get_win32_ucrt_lib_path(sdk, ucrt_lib_path, g->zig_target.arch.arch)) {
+                zig_panic("Unable to determine ucrt path.");
+            }
 
-        Buf* ucrt_lib_path = buf_alloc();
-        if (os_get_win32_ucrt_lib_path(g->win_sdk, ucrt_lib_path, g->zig_target.arch.arch)) {
-            zig_panic("Unable to determine ucrt path.");
-        }
+            Buf* kern_lib_path = buf_alloc();
+            if (os_get_win32_kern32_path(sdk, kern_lib_path, g->zig_target.arch.arch)) {
+                zig_panic("Unable to determine kernel32 path.");
+            }
 
-        Buf* kern_lib_path = buf_alloc();
-        if (os_get_win32_kern32_path(g->win_sdk, kern_lib_path, g->zig_target.arch.arch)) {
-            zig_panic("Unable to determine kernel32 path.");
+            g->msvc_lib_dir = vc_lib_dir;
+            g->libc_lib_dir = ucrt_lib_path;
+            g->kernel32_lib_dir = kern_lib_path;
+        } else {
+            zig_panic("Unable to determine libc lib path.");
         }
-
-        g->libc_lib_dirs_list.append(vc_lib_dir);
-        g->libc_lib_dirs_list.append(ucrt_lib_path);
-        g->libc_lib_dirs_list.append(kern_lib_path);
-    }
-    return;
-#endif
-
-    // later we can handle this better by reporting an error via the normal mechanism
-    if (!g->libc_lib_dir || buf_len(g->libc_lib_dir) == 0) {
-        zig_panic("Unable to determine libc lib path.");
     }
     if (!g->libc_static_lib_dir || buf_len(g->libc_static_lib_dir) == 0) {
         zig_panic("Unable to determine libc static lib path.");
src/codegen.cpp
@@ -114,6 +114,8 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out
         g->libc_lib_dir = buf_create_from_str("");
         g->libc_static_lib_dir = buf_create_from_str("");
         g->libc_include_dir = buf_create_from_str("");
+        g->msvc_lib_dir = nullptr;
+        g->kernel32_lib_dir = nullptr;
         g->each_lib_rpath = false;
     } else {
         // native compilation, we can rely on the configuration stuff
@@ -123,6 +125,8 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out
         g->libc_lib_dir = buf_create_from_str(ZIG_LIBC_LIB_DIR);
         g->libc_static_lib_dir = buf_create_from_str(ZIG_LIBC_STATIC_LIB_DIR);
         g->libc_include_dir = buf_create_from_str(ZIG_LIBC_INCLUDE_DIR);
+        g->msvc_lib_dir = nullptr; // find it at runtime
+        g->kernel32_lib_dir = nullptr; // find it at runtime
 
 #ifdef ZIG_EACH_LIB_RPATH
         g->each_lib_rpath = true;
@@ -221,20 +225,20 @@ void codegen_set_libc_include_dir(CodeGen *g, Buf *libc_include_dir) {
     g->libc_include_dir = libc_include_dir;
 }
 
-void codegen_set_dynamic_linker(CodeGen *g, Buf *dynamic_linker) {
-    g->dynamic_linker = dynamic_linker;
+void codegen_set_msvc_lib_dir(CodeGen *g, Buf *msvc_lib_dir) {
+    g->msvc_lib_dir = msvc_lib_dir;
 }
 
-void codegen_add_lib_dir(CodeGen *g, const char *dir) {
-    g->lib_dirs.append(dir);
+void codegen_set_kernel32_lib_dir(CodeGen *g, Buf *kernel32_lib_dir) {
+    g->kernel32_lib_dir = kernel32_lib_dir;
 }
 
-void codegen_set_ucrt_lib_dir(CodeGen *g, Buf *ucrt_lib_dir) {
-    g->libc_lib_dirs_list.append(ucrt_lib_dir);
+void codegen_set_dynamic_linker(CodeGen *g, Buf *dynamic_linker) {
+    g->dynamic_linker = dynamic_linker;
 }
 
-void codegen_set_kernel32_lib_dir(CodeGen *g, Buf *kernel32_lib_dir) {
-    g->libc_lib_dirs_list.append(kernel32_lib_dir);
+void codegen_add_lib_dir(CodeGen *g, const char *dir) {
+    g->lib_dirs.append(dir);
 }
 
 void codegen_add_rpath(CodeGen *g, const char *name) {
src/codegen.hpp
@@ -30,7 +30,7 @@ void codegen_set_out_name(CodeGen *codegen, Buf *out_name);
 void codegen_set_libc_lib_dir(CodeGen *codegen, Buf *libc_lib_dir);
 void codegen_set_libc_static_lib_dir(CodeGen *g, Buf *libc_static_lib_dir);
 void codegen_set_libc_include_dir(CodeGen *codegen, Buf *libc_include_dir);
-void codegen_set_ucrt_lib_dir(CodeGen *g, Buf *ucrt_lib_dir);
+void codegen_set_msvc_lib_dir(CodeGen *g, Buf *msvc_lib_dir);
 void codegen_set_kernel32_lib_dir(CodeGen *codegen, Buf *kernel32_lib_dir);
 void codegen_set_dynamic_linker(CodeGen *g, Buf *dynamic_linker);
 void codegen_set_windows_subsystem(CodeGen *g, bool mwindows, bool mconsole);
src/link.cpp
@@ -402,11 +402,11 @@ static void construct_linker_job_coff(LinkJob *lj) {
     lj->args.append(buf_ptr(buf_sprintf("-OUT:%s", buf_ptr(&lj->out_file))));
 
     if (g->libc_link_lib != nullptr) {
-        if (g->libc_link_lib != nullptr) {
-            for (uint32_t i = 0; i < g->libc_lib_dirs_list.length; ++i) {
-                lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(g->libc_lib_dirs_list.items[i]))));
-            }
-        }
+        lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(g->msvc_lib_dir))));
+        lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(g->kernel32_lib_dir))));
+
+        lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(g->libc_lib_dir))));
+        lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(g->libc_static_lib_dir))));
     }
 
     if (lj->link_in_crt) {
src/main.cpp
@@ -758,7 +758,7 @@ int main(int argc, char **argv) {
             if (libc_include_dir)
                 codegen_set_libc_include_dir(g, buf_create_from_str(libc_include_dir));
             if (msvc_lib_dir)
-                codegen_set_ucrt_lib_dir(g, buf_create_from_str(msvc_lib_dir));
+                codegen_set_msvc_lib_dir(g, buf_create_from_str(msvc_lib_dir));
             if (kernel32_lib_dir)
                 codegen_set_kernel32_lib_dir(g, buf_create_from_str(kernel32_lib_dir));
             if (dynamic_linker)
src/os.cpp
@@ -1001,8 +1001,8 @@ void os_stderr_set_color(TermColor color) {
 #endif
 }
 
-#if defined ZIG_OS_WINDOWS
 int os_find_windows_sdk(ZigWindowsSDK **out_sdk) {
+#if defined(ZIG_OS_WINDOWS)
     ZigWindowsSDK *result_sdk = allocate<ZigWindowsSDK>(1);
     buf_resize(&result_sdk->path10, 0);
     buf_resize(&result_sdk->path81, 0);
@@ -1099,9 +1099,13 @@ int os_find_windows_sdk(ZigWindowsSDK **out_sdk) {
 
     *out_sdk = result_sdk;
     return 0;
+#else
+    return ErrorFileNotFound;
+#endif
 }
 
 int os_get_win32_vcruntime_path(Buf* output_buf, ZigLLVM_ArchType platform_type) {
+#if defined(ZIG_OS_WINDOWS)
     buf_resize(output_buf, 0);
     //COM Smart Pointerse requires explicit scope
     {
@@ -1226,9 +1230,13 @@ com_done:;
         buf_resize(output_buf, 0);
         return ErrorFileNotFound;
     }
+#else
+    return ErrorFileNotFound;
+#endif
 }
 
 int os_get_win32_ucrt_lib_path(ZigWindowsSDK *sdk, Buf* output_buf, ZigLLVM_ArchType platform_type) {
+#if defined(ZIG_OS_WINDOWS)
     buf_resize(output_buf, 0);
     buf_appendf(output_buf, "%s\\Lib\\%s\\ucrt\\", buf_ptr(&sdk->path10), buf_ptr(&sdk->version10));
     switch (platform_type) {
@@ -1254,9 +1262,13 @@ int os_get_win32_ucrt_lib_path(ZigWindowsSDK *sdk, Buf* output_buf, ZigLLVM_Arch
         buf_resize(output_buf, 0);
         return ErrorFileNotFound;
     }
+#else
+    return ErrorFileNotFound;
+#endif
 }
 
 int os_get_win32_ucrt_include_path(ZigWindowsSDK *sdk, Buf* output_buf) {
+#if defined(ZIG_OS_WINDOWS)
     buf_resize(output_buf, 0);
     buf_appendf(output_buf, "%s\\Include\\%s\\ucrt", buf_ptr(&sdk->path10), buf_ptr(&sdk->version10));
     if (GetFileAttributesA(buf_ptr(output_buf)) != INVALID_FILE_ATTRIBUTES) {
@@ -1266,9 +1278,13 @@ int os_get_win32_ucrt_include_path(ZigWindowsSDK *sdk, Buf* output_buf) {
         buf_resize(output_buf, 0);
         return ErrorFileNotFound;
     }
+#else
+    return ErrorFileNotFound;
+#endif
 }
 
 int os_get_win32_kern32_path(ZigWindowsSDK *sdk, Buf* output_buf, ZigLLVM_ArchType platform_type) {
+#if defined(ZIG_OS_WINDOWS)
     {
         buf_resize(output_buf, 0);
         buf_appendf(output_buf, "%s\\Lib\\%s\\um\\", buf_ptr(&sdk->path10), buf_ptr(&sdk->version10));
@@ -1316,5 +1332,7 @@ int os_get_win32_kern32_path(ZigWindowsSDK *sdk, Buf* output_buf, ZigLLVM_ArchTy
         }
     }
     return ErrorFileNotFound;
-}
+#else
+    return ErrorFileNotFound;
 #endif
+}
src/os.hpp
@@ -76,28 +76,27 @@ bool os_is_sep(uint8_t c);
 
 int os_self_exe_path(Buf *out_path);
 
-#if defined(__APPLE__)
-#define ZIG_OS_DARWIN
-#elif defined(_WIN32)
-#define ZIG_OS_WINDOWS
-#elif defined(__linux__)
-#define ZIG_OS_LINUX
-#else
-#define ZIG_OS_UNKNOWN
-#endif
-
 struct ZigWindowsSDK {
     Buf path10;
     Buf version10;
     Buf path81;
     Buf version81;
 };
-#if defined(ZIG_OS_WINDOWS)
+
 int os_find_windows_sdk(ZigWindowsSDK **out_sdk);
 int os_get_win32_vcruntime_path(Buf *output_buf, ZigLLVM_ArchType platform_type);
 int os_get_win32_ucrt_include_path(ZigWindowsSDK *sdk, Buf *output_buf);
 int os_get_win32_ucrt_lib_path(ZigWindowsSDK *sdk, Buf *output_buf, ZigLLVM_ArchType platform_type);
 int os_get_win32_kern32_path(ZigWindowsSDK *sdk, Buf *output_buf, ZigLLVM_ArchType platform_type);
+
+#if defined(__APPLE__)
+#define ZIG_OS_DARWIN
+#elif defined(_WIN32)
+#define ZIG_OS_WINDOWS
+#elif defined(__linux__)
+#define ZIG_OS_LINUX
+#else
+#define ZIG_OS_UNKNOWN
 #endif
 
 #if defined(__x86_64__)