Commit 4d865e355b

Andrew Kelley <superjoe30@gmail.com>
2017-10-02 03:05:08
support linking against MSVC libc
1 parent 1563574
Changed files (4)
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");
 });
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"));
+        //}
+    //}
 }
 
 
src/parsec.cpp
@@ -36,8 +36,8 @@ struct Alias {
 struct Context {
     ImportTableEntry *import;
     ZigList<ErrorMsg *> *errors;
-    bool warnings_on;
     VisibMod visib_mod;
+    VisibMod export_visib_mod;
     AstNode *root;
     HashMap<const void *, AstNode *, ptr_hash, ptr_eq> decl_table;
     HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> macro_table;
@@ -45,6 +45,7 @@ struct Context {
     ZigList<Alias> aliases;
     ZigList<MacroSymbol> 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<ErrorMsg *> *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<ErrorMsg *> *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]);
     }
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();
 }