diff --git a/example/hello_world/hello_libc.zig b/example/hello_world/hello_libc.zig index 22c0ee784d..ea8aec1e4f 100644 --- a/example/hello_world/hello_libc.zig +++ b/example/hello_world/hello_libc.zig @@ -1,4 +1,6 @@ const c = @cImport({ + // See https://github.com/zig-lang/zig/issues/515 + @cDefine("_NO_CRT_STDIO_INLINE", "1"); @cInclude("stdio.h"); @cInclude("string.h"); }); diff --git a/src/link.cpp b/src/link.cpp index 4327b5f8cf..47ed96a575 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -359,6 +359,10 @@ static void construct_linker_job_coff(LinkJob *lj) { } else if (g->windows_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 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)))); 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) { // lj->args.append(get_libc_file(g, "dllcrt2.o")); //} else { @@ -468,54 +484,54 @@ static void construct_linker_job_coff(LinkJob *lj) { } } - if (g->libc_link_lib != nullptr) { - if (g->is_static) { - lj->args.append("--start-group"); - } + //if (g->libc_link_lib != nullptr) { + //if (g->is_static) { + // lj->args.append("--start-group"); + //} - lj->args.append("-lmingw32"); + //lj->args.append("-lmingw32"); - lj->args.append("-lgcc"); - bool is_android = (g->zig_target.env_type == ZigLLVM_Android); - bool is_cyg_ming = is_target_cyg_mingw(&g->zig_target); - if (!g->is_static && !is_android) { - if (!is_cyg_ming) { - lj->args.append("--as-needed"); - } - //lj->args.append("-lgcc_s"); - if (!is_cyg_ming) { - lj->args.append("--no-as-needed"); - } - } - if (g->is_static && !is_android) { - //lj->args.append("-lgcc_eh"); - } - if (is_android && !g->is_static) { - lj->args.append("-ldl"); - } + //lj->args.append("-lgcc"); + //bool is_android = (g->zig_target.env_type == ZigLLVM_Android); + //bool is_cyg_ming = is_target_cyg_mingw(&g->zig_target); + //if (!g->is_static && !is_android) { + // if (!is_cyg_ming) { + // lj->args.append("--as-needed"); + // } + // //lj->args.append("-lgcc_s"); + // if (!is_cyg_ming) { + // lj->args.append("--no-as-needed"); + // } + //} + //if (g->is_static && !is_android) { + // //lj->args.append("-lgcc_eh"); + //} + //if (is_android && !g->is_static) { + // lj->args.append("-ldl"); + //} - lj->args.append("-lmoldname"); - lj->args.append("-lmingwex"); - lj->args.append("-lmsvcrt"); + //lj->args.append("-lmoldname"); + //lj->args.append("-lmingwex"); + //lj->args.append("-lmsvcrt"); - if (g->windows_subsystem_windows) { - lj->args.append("-lgdi32"); - lj->args.append("-lcomdlg32"); - } - lj->args.append("-ladvapi32"); - lj->args.append("-lshell32"); - lj->args.append("-luser32"); - lj->args.append("-lkernel32"); + //if (g->windows_subsystem_windows) { + // lj->args.append("-lgdi32"); + // lj->args.append("-lcomdlg32"); + //} + //lj->args.append("-ladvapi32"); + //lj->args.append("-lshell32"); + //lj->args.append("-luser32"); + //lj->args.append("-lkernel32"); - if (g->is_static) { - lj->args.append("--end-group"); - } + //if (g->is_static) { + // lj->args.append("--end-group"); + //} - if (lj->link_in_crt) { - lj->args.append(get_libc_static_file(g, "crtend.o")); - } - } + //if (lj->link_in_crt) { + // lj->args.append(get_libc_static_file(g, "crtend.o")); + //} + //} } diff --git a/src/parsec.cpp b/src/parsec.cpp index 9ebd99c712..6a5c14c70f 100644 --- a/src/parsec.cpp +++ b/src/parsec.cpp @@ -36,8 +36,8 @@ struct Alias { struct Context { ImportTableEntry *import; ZigList *errors; - bool warnings_on; VisibMod visib_mod; + VisibMod export_visib_mod; AstNode *root; HashMap decl_table; HashMap macro_table; @@ -45,6 +45,7 @@ struct Context { ZigList aliases; ZigList macro_symbols; AstNode *source_node; + bool warnings_on; CodeGen *codegen; ASTContext *ctx; @@ -2489,7 +2490,7 @@ static void visit_fn_decl(Context *c, const FunctionDecl *fn_decl) { StorageClass sc = fn_decl->getStorageClass(); 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) { proto_node->data.fn_proto.visib_mod = c->visib_mod; } else if (sc == SC_PrivateExtern) { @@ -3173,7 +3174,8 @@ int parse_h_file(ImportTableEntry *import, ZigList *errors, const ch c->warnings_on = codegen->verbose; c->import = import; 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->macro_table.init(8); c->ptr_params.init(8); @@ -3210,6 +3212,11 @@ int parse_h_file(ImportTableEntry *import, ZigList *errors, const ch clang_argv.append("-isystem"); 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) { clang_argv.append(codegen->clang_argv[i]); } diff --git a/src/util.cpp b/src/util.cpp index 402c66aa3b..cafd2c3d85 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -16,6 +16,7 @@ void zig_panic(const char *format, ...) { va_start(ap, format); vfprintf(stderr, format, ap); fprintf(stderr, "\n"); + fflush(stderr); va_end(ap); abort(); }