support linking against MSVC libc

This commit is contained in:
Andrew Kelley 2017-10-01 21:05:08 -04:00
parent 1563574740
commit 4d865e355b
4 changed files with 71 additions and 45 deletions

View File

@ -1,4 +1,6 @@
const c = @cImport({ const c = @cImport({
// See https://github.com/zig-lang/zig/issues/515
@cDefine("_NO_CRT_STDIO_INLINE", "1");
@cInclude("stdio.h"); @cInclude("stdio.h");
@cInclude("string.h"); @cInclude("string.h");
}); });

View File

@ -359,6 +359,10 @@ static void construct_linker_job_coff(LinkJob *lj) {
} else if (g->windows_subsystem_console) { } else if (g->windows_subsystem_console) {
lj->args.append("/SUBSYSTEM:console"); lj->args.append("/SUBSYSTEM:console");
} }
// The commented out stuff is from when we linked with MinGW
// Now that we're linking with LLD it remains to be determined
// how to handle --target-environ gnu
// These comments are a clue
//bool dll = g->out_type == OutTypeLib; //bool dll = g->out_type == OutTypeLib;
//bool shared = !g->is_static && dll; //bool shared = !g->is_static && dll;
@ -385,7 +389,19 @@ static void construct_linker_job_coff(LinkJob *lj) {
lj->args.append(buf_ptr(buf_sprintf("-OUT:%s", buf_ptr(&lj->out_file)))); lj->args.append(buf_ptr(buf_sprintf("-OUT:%s", buf_ptr(&lj->out_file))));
if (lj->link_in_crt) { if (lj->link_in_crt) {
zig_panic("TODO link in c runtime"); const char *lib_str = g->is_static ? "lib" : "";
const char *d_str = (g->build_mode == BuildModeDebug) ? "d" : "";
Buf *cmt_lib_name = buf_sprintf("libcmt%s.lib", d_str);
lj->args.append(buf_ptr(cmt_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));
//if (shared || dll) { //if (shared || dll) {
// lj->args.append(get_libc_file(g, "dllcrt2.o")); // lj->args.append(get_libc_file(g, "dllcrt2.o"));
//} else { //} else {
@ -468,54 +484,54 @@ static void construct_linker_job_coff(LinkJob *lj) {
} }
} }
if (g->libc_link_lib != nullptr) { //if (g->libc_link_lib != nullptr) {
if (g->is_static) { //if (g->is_static) {
lj->args.append("--start-group"); // lj->args.append("--start-group");
} //}
lj->args.append("-lmingw32"); //lj->args.append("-lmingw32");
lj->args.append("-lgcc"); //lj->args.append("-lgcc");
bool is_android = (g->zig_target.env_type == ZigLLVM_Android); //bool is_android = (g->zig_target.env_type == ZigLLVM_Android);
bool is_cyg_ming = is_target_cyg_mingw(&g->zig_target); //bool is_cyg_ming = is_target_cyg_mingw(&g->zig_target);
if (!g->is_static && !is_android) { //if (!g->is_static && !is_android) {
if (!is_cyg_ming) { // if (!is_cyg_ming) {
lj->args.append("--as-needed"); // lj->args.append("--as-needed");
} // }
//lj->args.append("-lgcc_s"); // //lj->args.append("-lgcc_s");
if (!is_cyg_ming) { // if (!is_cyg_ming) {
lj->args.append("--no-as-needed"); // lj->args.append("--no-as-needed");
} // }
} //}
if (g->is_static && !is_android) { //if (g->is_static && !is_android) {
//lj->args.append("-lgcc_eh"); // //lj->args.append("-lgcc_eh");
} //}
if (is_android && !g->is_static) { //if (is_android && !g->is_static) {
lj->args.append("-ldl"); // lj->args.append("-ldl");
} //}
lj->args.append("-lmoldname"); //lj->args.append("-lmoldname");
lj->args.append("-lmingwex"); //lj->args.append("-lmingwex");
lj->args.append("-lmsvcrt"); //lj->args.append("-lmsvcrt");
if (g->windows_subsystem_windows) { //if (g->windows_subsystem_windows) {
lj->args.append("-lgdi32"); // lj->args.append("-lgdi32");
lj->args.append("-lcomdlg32"); // lj->args.append("-lcomdlg32");
} //}
lj->args.append("-ladvapi32"); //lj->args.append("-ladvapi32");
lj->args.append("-lshell32"); //lj->args.append("-lshell32");
lj->args.append("-luser32"); //lj->args.append("-luser32");
lj->args.append("-lkernel32"); //lj->args.append("-lkernel32");
if (g->is_static) { //if (g->is_static) {
lj->args.append("--end-group"); // lj->args.append("--end-group");
} //}
if (lj->link_in_crt) { //if (lj->link_in_crt) {
lj->args.append(get_libc_static_file(g, "crtend.o")); // lj->args.append(get_libc_static_file(g, "crtend.o"));
} //}
} //}
} }

View File

@ -36,8 +36,8 @@ struct Alias {
struct Context { struct Context {
ImportTableEntry *import; ImportTableEntry *import;
ZigList<ErrorMsg *> *errors; ZigList<ErrorMsg *> *errors;
bool warnings_on;
VisibMod visib_mod; VisibMod visib_mod;
VisibMod export_visib_mod;
AstNode *root; AstNode *root;
HashMap<const void *, AstNode *, ptr_hash, ptr_eq> decl_table; HashMap<const void *, AstNode *, ptr_hash, ptr_eq> decl_table;
HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> macro_table; HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> macro_table;
@ -45,6 +45,7 @@ struct Context {
ZigList<Alias> aliases; ZigList<Alias> aliases;
ZigList<MacroSymbol> macro_symbols; ZigList<MacroSymbol> macro_symbols;
AstNode *source_node; AstNode *source_node;
bool warnings_on;
CodeGen *codegen; CodeGen *codegen;
ASTContext *ctx; ASTContext *ctx;
@ -2489,7 +2490,7 @@ static void visit_fn_decl(Context *c, const FunctionDecl *fn_decl) {
StorageClass sc = fn_decl->getStorageClass(); StorageClass sc = fn_decl->getStorageClass();
if (sc == SC_None) { if (sc == SC_None) {
proto_node->data.fn_proto.visib_mod = fn_decl->hasBody() ? VisibModExport : c->visib_mod; proto_node->data.fn_proto.visib_mod = fn_decl->hasBody() ? c->export_visib_mod : c->visib_mod;
} else if (sc == SC_Extern || sc == SC_Static) { } else if (sc == SC_Extern || sc == SC_Static) {
proto_node->data.fn_proto.visib_mod = c->visib_mod; proto_node->data.fn_proto.visib_mod = c->visib_mod;
} else if (sc == SC_PrivateExtern) { } else if (sc == SC_PrivateExtern) {
@ -3173,7 +3174,8 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const ch
c->warnings_on = codegen->verbose; c->warnings_on = codegen->verbose;
c->import = import; c->import = import;
c->errors = errors; c->errors = errors;
c->visib_mod = VisibModPub; c->visib_mod = (source_node == nullptr) ? VisibModPrivate : VisibModPub;
c->export_visib_mod = (source_node == nullptr) ? VisibModExport : VisibModPub;
c->decl_table.init(8); c->decl_table.init(8);
c->macro_table.init(8); c->macro_table.init(8);
c->ptr_params.init(8); c->ptr_params.init(8);
@ -3210,6 +3212,11 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const ch
clang_argv.append("-isystem"); clang_argv.append("-isystem");
clang_argv.append(buf_ptr(codegen->libc_include_dir)); clang_argv.append(buf_ptr(codegen->libc_include_dir));
// windows c runtime requires -D_DEBUG if using debug libraries
if (codegen->build_mode == BuildModeDebug) {
clang_argv.append("-D_DEBUG");
}
for (size_t i = 0; i < codegen->clang_argv_len; i += 1) { for (size_t i = 0; i < codegen->clang_argv_len; i += 1) {
clang_argv.append(codegen->clang_argv[i]); clang_argv.append(codegen->clang_argv[i]);
} }

View File

@ -16,6 +16,7 @@ void zig_panic(const char *format, ...) {
va_start(ap, format); va_start(ap, format);
vfprintf(stderr, format, ap); vfprintf(stderr, format, ap);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
fflush(stderr);
va_end(ap); va_end(ap);
abort(); abort();
} }