authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-10-01 21:05:08-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-10-01 21:05:08-04:00
log4d865e355b9fc6b9f679f57e12865b05f6ee6759
treee9b7d6d716d5b9eafa36ad6111024809a59f11cb
parent15635747406162b9c92b84a72314068e374196fe

support linking against MSVC libc


4 files changed, 71 insertions(+), 45 deletions(-)

example/hello_world/hello_libc.zig+2
...@@ -1,4 +1,6 @@...@@ -1,4 +1,6 @@
1const c = @cImport({1const c = @cImport({
2 // See https://github.com/zig-lang/zig/issues/515
3 @cDefine("_NO_CRT_STDIO_INLINE", "1");
2 @cInclude("stdio.h");4 @cInclude("stdio.h");
3 @cInclude("string.h");5 @cInclude("string.h");
4});6});
src/link.cpp+58-42
...@@ -359,6 +359,10 @@ static void construct_linker_job_coff(LinkJob *lj) {...@@ -359,6 +359,10 @@ static void construct_linker_job_coff(LinkJob *lj) {
359 } else if (g->windows_subsystem_console) {359 } else if (g->windows_subsystem_console) {
360 lj->args.append("/SUBSYSTEM:console");360 lj->args.append("/SUBSYSTEM:console");
361 }361 }
362 // The commented out stuff is from when we linked with MinGW
363 // Now that we're linking with LLD it remains to be determined
364 // how to handle --target-environ gnu
365 // These comments are a clue
362366
363 //bool dll = g->out_type == OutTypeLib;367 //bool dll = g->out_type == OutTypeLib;
364 //bool shared = !g->is_static && dll;368 //bool shared = !g->is_static && dll;
...@@ -385,7 +389,19 @@ static void construct_linker_job_coff(LinkJob *lj) {...@@ -385,7 +389,19 @@ static void construct_linker_job_coff(LinkJob *lj) {
385 lj->args.append(buf_ptr(buf_sprintf("-OUT:%s", buf_ptr(&lj->out_file))));389 lj->args.append(buf_ptr(buf_sprintf("-OUT:%s", buf_ptr(&lj->out_file))));
386390
387 if (lj->link_in_crt) {391 if (lj->link_in_crt) {
388 zig_panic("TODO link in c runtime");392 const char *lib_str = g->is_static ? "lib" : "";
393 const char *d_str = (g->build_mode == BuildModeDebug) ? "d" : "";
394
395 Buf *cmt_lib_name = buf_sprintf("libcmt%s.lib", d_str);
396 lj->args.append(buf_ptr(cmt_lib_name));
397
398 Buf *vcruntime_lib_name = buf_sprintf("%svcruntime%s.lib", lib_str, d_str);
399 lj->args.append(buf_ptr(vcruntime_lib_name));
400
401 Buf *crt_lib_name = buf_sprintf("%sucrt%s.lib", lib_str, d_str);
402 lj->args.append(buf_ptr(crt_lib_name));
403
404
389 //if (shared || dll) {405 //if (shared || dll) {
390 // lj->args.append(get_libc_file(g, "dllcrt2.o"));406 // lj->args.append(get_libc_file(g, "dllcrt2.o"));
391 //} else {407 //} else {
...@@ -468,54 +484,54 @@ static void construct_linker_job_coff(LinkJob *lj) {...@@ -468,54 +484,54 @@ static void construct_linker_job_coff(LinkJob *lj) {
468 }484 }
469 }485 }
470486
471 if (g->libc_link_lib != nullptr) {487 //if (g->libc_link_lib != nullptr) {
472 if (g->is_static) {488 //if (g->is_static) {
473 lj->args.append("--start-group");489 // lj->args.append("--start-group");
474 }490 //}
475491
476 lj->args.append("-lmingw32");492 //lj->args.append("-lmingw32");
477493
478 lj->args.append("-lgcc");494 //lj->args.append("-lgcc");
479 bool is_android = (g->zig_target.env_type == ZigLLVM_Android);495 //bool is_android = (g->zig_target.env_type == ZigLLVM_Android);
480 bool is_cyg_ming = is_target_cyg_mingw(&g->zig_target);496 //bool is_cyg_ming = is_target_cyg_mingw(&g->zig_target);
481 if (!g->is_static && !is_android) {497 //if (!g->is_static && !is_android) {
482 if (!is_cyg_ming) {498 // if (!is_cyg_ming) {
483 lj->args.append("--as-needed");499 // lj->args.append("--as-needed");
484 }500 // }
485 //lj->args.append("-lgcc_s");501 // //lj->args.append("-lgcc_s");
486 if (!is_cyg_ming) {502 // if (!is_cyg_ming) {
487 lj->args.append("--no-as-needed");503 // lj->args.append("--no-as-needed");
488 }504 // }
489 }505 //}
490 if (g->is_static && !is_android) {506 //if (g->is_static && !is_android) {
491 //lj->args.append("-lgcc_eh");507 // //lj->args.append("-lgcc_eh");
492 }508 //}
493 if (is_android && !g->is_static) {509 //if (is_android && !g->is_static) {
494 lj->args.append("-ldl");510 // lj->args.append("-ldl");
495 }511 //}
496512
497 lj->args.append("-lmoldname");513 //lj->args.append("-lmoldname");
498 lj->args.append("-lmingwex");514 //lj->args.append("-lmingwex");
499 lj->args.append("-lmsvcrt");515 //lj->args.append("-lmsvcrt");
500516
501517
502 if (g->windows_subsystem_windows) {518 //if (g->windows_subsystem_windows) {
503 lj->args.append("-lgdi32");519 // lj->args.append("-lgdi32");
504 lj->args.append("-lcomdlg32");520 // lj->args.append("-lcomdlg32");
505 }521 //}
506 lj->args.append("-ladvapi32");522 //lj->args.append("-ladvapi32");
507 lj->args.append("-lshell32");523 //lj->args.append("-lshell32");
508 lj->args.append("-luser32");524 //lj->args.append("-luser32");
509 lj->args.append("-lkernel32");525 //lj->args.append("-lkernel32");
510526
511 if (g->is_static) {527 //if (g->is_static) {
512 lj->args.append("--end-group");528 // lj->args.append("--end-group");
513 }529 //}
514530
515 if (lj->link_in_crt) {531 //if (lj->link_in_crt) {
516 lj->args.append(get_libc_static_file(g, "crtend.o"));532 // lj->args.append(get_libc_static_file(g, "crtend.o"));
517 }533 //}
518 }534 //}
519}535}
520536
521537
src/parsec.cpp+10-3
...@@ -36,8 +36,8 @@ struct Alias {...@@ -36,8 +36,8 @@ struct Alias {
36struct Context {36struct Context {
37 ImportTableEntry *import;37 ImportTableEntry *import;
38 ZigList<ErrorMsg *> *errors;38 ZigList<ErrorMsg *> *errors;
39 bool warnings_on;
40 VisibMod visib_mod;39 VisibMod visib_mod;
40 VisibMod export_visib_mod;
41 AstNode *root;41 AstNode *root;
42 HashMap<const void *, AstNode *, ptr_hash, ptr_eq> decl_table;42 HashMap<const void *, AstNode *, ptr_hash, ptr_eq> decl_table;
43 HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> macro_table;43 HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> macro_table;
...@@ -45,6 +45,7 @@ struct Context {...@@ -45,6 +45,7 @@ struct Context {
45 ZigList<Alias> aliases;45 ZigList<Alias> aliases;
46 ZigList<MacroSymbol> macro_symbols;46 ZigList<MacroSymbol> macro_symbols;
47 AstNode *source_node;47 AstNode *source_node;
48 bool warnings_on;
4849
49 CodeGen *codegen;50 CodeGen *codegen;
50 ASTContext *ctx;51 ASTContext *ctx;
...@@ -2489,7 +2490,7 @@ static void visit_fn_decl(Context *c, const FunctionDecl *fn_decl) {...@@ -2489,7 +2490,7 @@ static void visit_fn_decl(Context *c, const FunctionDecl *fn_decl) {
24892490
2490 StorageClass sc = fn_decl->getStorageClass();2491 StorageClass sc = fn_decl->getStorageClass();
2491 if (sc == SC_None) {2492 if (sc == SC_None) {
2492 proto_node->data.fn_proto.visib_mod = fn_decl->hasBody() ? VisibModExport : c->visib_mod;2493 proto_node->data.fn_proto.visib_mod = fn_decl->hasBody() ? c->export_visib_mod : c->visib_mod;
2493 } else if (sc == SC_Extern || sc == SC_Static) {2494 } else if (sc == SC_Extern || sc == SC_Static) {
2494 proto_node->data.fn_proto.visib_mod = c->visib_mod;2495 proto_node->data.fn_proto.visib_mod = c->visib_mod;
2495 } else if (sc == SC_PrivateExtern) {2496 } else if (sc == SC_PrivateExtern) {
...@@ -3173,7 +3174,8 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const ch...@@ -3173,7 +3174,8 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const ch
3173 c->warnings_on = codegen->verbose;3174 c->warnings_on = codegen->verbose;
3174 c->import = import;3175 c->import = import;
3175 c->errors = errors;3176 c->errors = errors;
3176 c->visib_mod = VisibModPub;3177 c->visib_mod = (source_node == nullptr) ? VisibModPrivate : VisibModPub;
3178 c->export_visib_mod = (source_node == nullptr) ? VisibModExport : VisibModPub;
3177 c->decl_table.init(8);3179 c->decl_table.init(8);
3178 c->macro_table.init(8);3180 c->macro_table.init(8);
3179 c->ptr_params.init(8);3181 c->ptr_params.init(8);
...@@ -3210,6 +3212,11 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const ch...@@ -3210,6 +3212,11 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const ch
3210 clang_argv.append("-isystem");3212 clang_argv.append("-isystem");
3211 clang_argv.append(buf_ptr(codegen->libc_include_dir));3213 clang_argv.append(buf_ptr(codegen->libc_include_dir));
32123214
3215 // windows c runtime requires -D_DEBUG if using debug libraries
3216 if (codegen->build_mode == BuildModeDebug) {
3217 clang_argv.append("-D_DEBUG");
3218 }
3219
3213 for (size_t i = 0; i < codegen->clang_argv_len; i += 1) {3220 for (size_t i = 0; i < codegen->clang_argv_len; i += 1) {
3214 clang_argv.append(codegen->clang_argv[i]);3221 clang_argv.append(codegen->clang_argv[i]);
3215 }3222 }
src/util.cpp+1
...@@ -16,6 +16,7 @@ void zig_panic(const char *format, ...) {...@@ -16,6 +16,7 @@ void zig_panic(const char *format, ...) {
16 va_start(ap, format);16 va_start(ap, format);
17 vfprintf(stderr, format, ap);17 vfprintf(stderr, format, ap);
18 fprintf(stderr, "\n");18 fprintf(stderr, "\n");
19 fflush(stderr);
19 va_end(ap);20 va_end(ap);
20 abort();21 abort();
21}22}