authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-10-16 10:20:52-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-10-16 12:21:46-04:00
logcbb6d2239f84ee13279655fc591530a23301d188
treec89c4ecb59a0a91521ff32b81755ac56fb9336d7
parent8cfb0cfbcee8161c52f71bccc3cf1b8d988f83b0

look for libc at runtime on windows

See #539 before we close the issue we should also detect MSVC 2017 but this gets us started with supporting MSVC 2015

6 files changed, 91 insertions(+), 5 deletions(-)

src/all_types.hpp+2
......@@ -1461,6 +1461,8 @@ struct CodeGen {
14611461 Buf *libc_lib_dir;
14621462 Buf *libc_static_lib_dir;
14631463 Buf *libc_include_dir;
1464 Buf *msvc_lib_dir;
1465 Buf *kernel32_lib_dir;
14641466 Buf *zig_lib_dir;
14651467 Buf *zig_std_dir;
14661468 Buf *zig_c_headers_dir;
src/analyze.cpp+55
......@@ -3359,6 +3359,61 @@ void find_libc_include_path(CodeGen *g) {
33593359}
33603360
33613361void find_libc_lib_path(CodeGen *g) {
3362#ifdef ZIG_OS_WINDOWS
3363 if (!g->msvc_lib_dir && g->zig_target.os == ZigLLVM_Win32) {
3364 Buf *msvc_lib_dir;
3365 if (g->zig_target.arch.arch == ZigLLVM_arm) {
3366 msvc_lib_dir = buf_create_from_str("C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\lib\\arm");
3367 } else if (g->zig_target.arch.arch == ZigLLVM_x86_64) {
3368 msvc_lib_dir = buf_create_from_str("C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\lib\\amd64");
3369 } else if (g->zig_target.arch.arch == ZigLLVM_x86) {
3370 msvc_lib_dir = buf_create_from_str("C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\lib");
3371 } else {
3372 zig_panic("unable to determine msvc lib path");
3373 }
3374 Buf *test_path = buf_alloc();
3375 os_path_join(msvc_lib_dir, buf_create_from_str("vcruntime.lib"), test_path);
3376 bool result;
3377 int err;
3378 if ((err = os_file_exists(test_path, &result))) {
3379 result = false;
3380 }
3381 if (result) {
3382 g->msvc_lib_dir = msvc_lib_dir;
3383 } else {
3384 zig_panic("Unable to determine msvc lib path.");
3385 }
3386 }
3387
3388 if (!g->kernel32_lib_dir && g->zig_target.os == ZigLLVM_Win32) {
3389 Buf *kernel32_lib_dir;
3390 if (g->zig_target.arch.arch == ZigLLVM_arm) {
3391 kernel32_lib_dir = buf_create_from_str(
3392 "C:\\Program Files (x86)\\Windows Kits\\8.1\\Lib\\winv6.3\\um\\arm");
3393 } else if (g->zig_target.arch.arch == ZigLLVM_x86_64) {
3394 kernel32_lib_dir = buf_create_from_str(
3395 "C:\\Program Files (x86)\\Windows Kits\\8.1\\Lib\\winv6.3\\um\\x64");
3396 } else if (g->zig_target.arch.arch == ZigLLVM_x86) {
3397 kernel32_lib_dir = buf_create_from_str(
3398 "C:\\Program Files (x86)\\Windows Kits\\8.1\\Lib\\winv6.3\\um\\x86");
3399 } else {
3400 zig_panic("unable to determine kernel32 lib path");
3401 }
3402 Buf *test_path = buf_alloc();
3403 os_path_join(kernel32_lib_dir, buf_create_from_str("kernel32.lib"), test_path);
3404 bool result;
3405 int err;
3406 if ((err = os_file_exists(test_path, &result))) {
3407 result = false;
3408 }
3409 if (result) {
3410 g->kernel32_lib_dir = kernel32_lib_dir;
3411 } else {
3412 zig_panic("Unable to determine kernel32 lib path.");
3413 }
3414 }
3415#endif
3416
33623417 // later we can handle this better by reporting an error via the normal mechanism
33633418 if (!g->libc_lib_dir || buf_len(g->libc_lib_dir) == 0) {
33643419 zig_panic("Unable to determine libc lib path.");
src/codegen.cpp+12
......@@ -117,6 +117,8 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out
117117 g->libc_lib_dir = buf_create_from_str("");
118118 g->libc_static_lib_dir = buf_create_from_str("");
119119 g->libc_include_dir = buf_create_from_str("");
120 g->msvc_lib_dir = nullptr;
121 g->kernel32_lib_dir = nullptr;
120122 g->each_lib_rpath = false;
121123 } else {
122124 // native compilation, we can rely on the configuration stuff
......@@ -127,6 +129,8 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out
127129 g->libc_lib_dir = buf_create_from_str(ZIG_LIBC_LIB_DIR);
128130 g->libc_static_lib_dir = buf_create_from_str(ZIG_LIBC_STATIC_LIB_DIR);
129131 g->libc_include_dir = buf_create_from_str(ZIG_LIBC_INCLUDE_DIR);
132 g->msvc_lib_dir = nullptr; // find it at runtime
133 g->kernel32_lib_dir = nullptr; // find it at runtime
130134#ifdef ZIG_EACH_LIB_RPATH
131135 g->each_lib_rpath = true;
132136#endif
......@@ -228,6 +232,14 @@ void codegen_set_libc_include_dir(CodeGen *g, Buf *libc_include_dir) {
228232 g->libc_include_dir = libc_include_dir;
229233}
230234
235void codegen_set_msvc_lib_dir(CodeGen *g, Buf *msvc_lib_dir) {
236 g->msvc_lib_dir = msvc_lib_dir;
237}
238
239void codegen_set_kernel32_lib_dir(CodeGen *g, Buf *kernel32_lib_dir) {
240 g->kernel32_lib_dir = kernel32_lib_dir;
241}
242
231243void codegen_set_dynamic_linker(CodeGen *g, Buf *dynamic_linker) {
232244 g->dynamic_linker = dynamic_linker;
233245}
src/codegen.hpp+2
......@@ -31,6 +31,8 @@ void codegen_set_out_name(CodeGen *codegen, Buf *out_name);
3131void codegen_set_libc_lib_dir(CodeGen *codegen, Buf *libc_lib_dir);
3232void codegen_set_libc_static_lib_dir(CodeGen *g, Buf *libc_static_lib_dir);
3333void codegen_set_libc_include_dir(CodeGen *codegen, Buf *libc_include_dir);
34void codegen_set_msvc_lib_dir(CodeGen *codegen, Buf *msvc_lib_dir);
35void codegen_set_kernel32_lib_dir(CodeGen *codegen, Buf *kernel32_lib_dir);
3436void codegen_set_dynamic_linker(CodeGen *g, Buf *dynamic_linker);
3537void codegen_set_windows_subsystem(CodeGen *g, bool mwindows, bool mconsole);
3638void codegen_set_windows_unicode(CodeGen *g, bool municode);
src/link.cpp+8-5
......@@ -397,6 +397,14 @@ static void construct_linker_job_coff(LinkJob *lj) {
397397
398398 lj->args.append(buf_ptr(buf_sprintf("-OUT:%s", buf_ptr(&lj->out_file))));
399399
400 if (g->libc_link_lib != nullptr) {
401 lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(g->msvc_lib_dir))));
402 lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(g->kernel32_lib_dir))));
403
404 lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(g->libc_lib_dir))));
405 lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(g->libc_static_lib_dir))));
406 }
407
400408 if (lj->link_in_crt) {
401409 const char *lib_str = g->is_static ? "lib" : "";
402410 const char *d_str = (g->build_mode == BuildModeDebug) ? "d" : "";
......@@ -441,11 +449,6 @@ static void construct_linker_job_coff(LinkJob *lj) {
441449 lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", lib_dir)));
442450 }
443451
444 if (g->libc_link_lib != nullptr) {
445 lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(g->libc_lib_dir))));
446 lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(g->libc_static_lib_dir))));
447 }
448
449452 for (size_t i = 0; i < g->link_objects.length; i += 1) {
450453 lj->args.append((const char *)buf_ptr(g->link_objects.at(i)));
451454 }
src/main.cpp+12
......@@ -59,6 +59,8 @@ static int usage(const char *arg0) {
5959 " --each-lib-rpath add rpath for each used dynamic library\n"
6060 " --libc-lib-dir [path] directory where libc crt1.o resides\n"
6161 " --libc-static-lib-dir [path] directory where libc crtbegin.o resides\n"
62 " --msvc-lib-dir [path] (windows) directory where vcruntime.lib resides\n"
63 " --kernel32-lib-dir [path] (windows) directory where kernel32.lib resides\n"
6264 " --library [lib] link against lib\n"
6365 " --library-path [dir] add a directory to the library search path\n"
6466 " --linker-script [path] use a custom linker script\n"
......@@ -279,6 +281,8 @@ int main(int argc, char **argv) {
279281 const char *libc_lib_dir = nullptr;
280282 const char *libc_static_lib_dir = nullptr;
281283 const char *libc_include_dir = nullptr;
284 const char *msvc_lib_dir = nullptr;
285 const char *kernel32_lib_dir = nullptr;
282286 const char *zig_install_prefix = nullptr;
283287 const char *dynamic_linker = nullptr;
284288 ZigList<const char *> clang_argv = {0};
......@@ -518,6 +522,10 @@ int main(int argc, char **argv) {
518522 libc_static_lib_dir = argv[i];
519523 } else if (strcmp(arg, "--libc-include-dir") == 0) {
520524 libc_include_dir = argv[i];
525 } else if (strcmp(arg, "--msvc-lib-dir") == 0) {
526 msvc_lib_dir = argv[i];
527 } else if (strcmp(arg, "--kernel32-lib-dir") == 0) {
528 kernel32_lib_dir = argv[i];
521529 } else if (strcmp(arg, "--zig-install-prefix") == 0) {
522530 zig_install_prefix = argv[i];
523531 } else if (strcmp(arg, "--dynamic-linker") == 0) {
......@@ -728,6 +736,10 @@ int main(int argc, char **argv) {
728736 codegen_set_libc_static_lib_dir(g, buf_create_from_str(libc_static_lib_dir));
729737 if (libc_include_dir)
730738 codegen_set_libc_include_dir(g, buf_create_from_str(libc_include_dir));
739 if (msvc_lib_dir)
740 codegen_set_msvc_lib_dir(g, buf_create_from_str(msvc_lib_dir));
741 if (kernel32_lib_dir)
742 codegen_set_kernel32_lib_dir(g, buf_create_from_str(kernel32_lib_dir));
731743 if (dynamic_linker)
732744 codegen_set_dynamic_linker(g, buf_create_from_str(dynamic_linker));
733745 codegen_set_verbose(g, verbose);