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 @@
11const c = @cImport({
2 // See https://github.com/zig-lang/zig/issues/515
3 @cDefine("_NO_CRT_STDIO_INLINE", "1");
24 @cInclude("stdio.h");
35 @cInclude("string.h");
46});
src/link.cpp+58-42
......@@ -359,6 +359,10 @@ static void construct_linker_job_coff(LinkJob *lj) {
359359 } else if (g->windows_subsystem_console) {
360360 lj->args.append("/SUBSYSTEM:console");
361361 }
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
363367 //bool dll = g->out_type == OutTypeLib;
364368 //bool shared = !g->is_static && dll;
......@@ -385,7 +389,19 @@ static void construct_linker_job_coff(LinkJob *lj) {
385389 lj->args.append(buf_ptr(buf_sprintf("-OUT:%s", buf_ptr(&lj->out_file))));
386390
387391 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
389405 //if (shared || dll) {
390406 // lj->args.append(get_libc_file(g, "dllcrt2.o"));
391407 //} else {
......@@ -468,54 +484,54 @@ static void construct_linker_job_coff(LinkJob *lj) {
468484 }
469485 }
470486
471 if (g->libc_link_lib != nullptr) {
472 if (g->is_static) {
473 lj->args.append("--start-group");
474 }
487 //if (g->libc_link_lib != nullptr) {
488 //if (g->is_static) {
489 // lj->args.append("--start-group");
490 //}
475491
476 lj->args.append("-lmingw32");
492 //lj->args.append("-lmingw32");
477493
478 lj->args.append("-lgcc");
479 bool is_android = (g->zig_target.env_type == ZigLLVM_Android);
480 bool is_cyg_ming = is_target_cyg_mingw(&g->zig_target);
481 if (!g->is_static && !is_android) {
482 if (!is_cyg_ming) {
483 lj->args.append("--as-needed");
484 }
485 //lj->args.append("-lgcc_s");
486 if (!is_cyg_ming) {
487 lj->args.append("--no-as-needed");
488 }
489 }
490 if (g->is_static && !is_android) {
491 //lj->args.append("-lgcc_eh");
492 }
493 if (is_android && !g->is_static) {
494 lj->args.append("-ldl");
495 }
494 //lj->args.append("-lgcc");
495 //bool is_android = (g->zig_target.env_type == ZigLLVM_Android);
496 //bool is_cyg_ming = is_target_cyg_mingw(&g->zig_target);
497 //if (!g->is_static && !is_android) {
498 // if (!is_cyg_ming) {
499 // lj->args.append("--as-needed");
500 // }
501 // //lj->args.append("-lgcc_s");
502 // if (!is_cyg_ming) {
503 // lj->args.append("--no-as-needed");
504 // }
505 //}
506 //if (g->is_static && !is_android) {
507 // //lj->args.append("-lgcc_eh");
508 //}
509 //if (is_android && !g->is_static) {
510 // lj->args.append("-ldl");
511 //}
496512
497 lj->args.append("-lmoldname");
498 lj->args.append("-lmingwex");
499 lj->args.append("-lmsvcrt");
513 //lj->args.append("-lmoldname");
514 //lj->args.append("-lmingwex");
515 //lj->args.append("-lmsvcrt");
500516
501517
502 if (g->windows_subsystem_windows) {
503 lj->args.append("-lgdi32");
504 lj->args.append("-lcomdlg32");
505 }
506 lj->args.append("-ladvapi32");
507 lj->args.append("-lshell32");
508 lj->args.append("-luser32");
509 lj->args.append("-lkernel32");
518 //if (g->windows_subsystem_windows) {
519 // lj->args.append("-lgdi32");
520 // lj->args.append("-lcomdlg32");
521 //}
522 //lj->args.append("-ladvapi32");
523 //lj->args.append("-lshell32");
524 //lj->args.append("-luser32");
525 //lj->args.append("-lkernel32");
510526
511 if (g->is_static) {
512 lj->args.append("--end-group");
513 }
527 //if (g->is_static) {
528 // lj->args.append("--end-group");
529 //}
514530
515 if (lj->link_in_crt) {
516 lj->args.append(get_libc_static_file(g, "crtend.o"));
517 }
518 }
531 //if (lj->link_in_crt) {
532 // lj->args.append(get_libc_static_file(g, "crtend.o"));
533 //}
534 //}
519535}
520536
521537
src/parsec.cpp+10-3
......@@ -36,8 +36,8 @@ struct Alias {
3636struct Context {
3737 ImportTableEntry *import;
3838 ZigList<ErrorMsg *> *errors;
39 bool warnings_on;
4039 VisibMod visib_mod;
40 VisibMod export_visib_mod;
4141 AstNode *root;
4242 HashMap<const void *, AstNode *, ptr_hash, ptr_eq> decl_table;
4343 HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> macro_table;
......@@ -45,6 +45,7 @@ struct Context {
4545 ZigList<Alias> aliases;
4646 ZigList<MacroSymbol> macro_symbols;
4747 AstNode *source_node;
48 bool warnings_on;
4849
4950 CodeGen *codegen;
5051 ASTContext *ctx;
......@@ -2489,7 +2490,7 @@ static void visit_fn_decl(Context *c, const FunctionDecl *fn_decl) {
24892490
24902491 StorageClass sc = fn_decl->getStorageClass();
24912492 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;
24932494 } else if (sc == SC_Extern || sc == SC_Static) {
24942495 proto_node->data.fn_proto.visib_mod = c->visib_mod;
24952496 } else if (sc == SC_PrivateExtern) {
......@@ -3173,7 +3174,8 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const ch
31733174 c->warnings_on = codegen->verbose;
31743175 c->import = import;
31753176 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;
31773179 c->decl_table.init(8);
31783180 c->macro_table.init(8);
31793181 c->ptr_params.init(8);
......@@ -3210,6 +3212,11 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const ch
32103212 clang_argv.append("-isystem");
32113213 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
32133220 for (size_t i = 0; i < codegen->clang_argv_len; i += 1) {
32143221 clang_argv.append(codegen->clang_argv[i]);
32153222 }
src/util.cpp+1
......@@ -16,6 +16,7 @@ void zig_panic(const char *format, ...) {
1616 va_start(ap, format);
1717 vfprintf(stderr, format, ap);
1818 fprintf(stderr, "\n");
19 fflush(stderr);
1920 va_end(ap);
2021 abort();
2122}