Commit d9c4c96bf2

Andrew Kelley <andrew@ziglang.org>
2019-07-12 05:48:13
add -Wno-pragma-pack when targeting windows-gnu
windows.h has files such as pshpack1.h which do #pragma packing, triggering a clang warning. So for this target, this warning is disabled. this commit also improves the error message printed when no libc can be used, printing the "zig triple" rather than the "llvm triple".
1 parent b4bbfe8
src/all_types.hpp
@@ -1804,7 +1804,7 @@ struct CodeGen {
     ZigType *err_tag_type;
     ZigType *test_fn_type;
 
-    Buf triple_str;
+    Buf llvm_triple_str;
     Buf global_asm;
     Buf output_file_path;
     Buf o_file_output_path;
src/codegen.cpp
@@ -199,7 +199,7 @@ CodeGen *codegen_create(Buf *main_pkg_path, Buf *root_src_path, const ZigTarget
         g->link_libs_list.append(g->libc_link_lib);
     }
 
-    get_target_triple(&g->triple_str, g->zig_target);
+    target_triple_llvm(&g->llvm_triple_str, g->zig_target);
     g->pointer_size_bytes = target_arch_pointer_bit_width(g->zig_target->arch) / 8;
 
     if (!target_has_debug_info(g->zig_target)) {
@@ -8103,7 +8103,7 @@ static void init(CodeGen *g) {
     assert(g->root_out_name);
     g->module = LLVMModuleCreateWithName(buf_ptr(g->root_out_name));
 
-    LLVMSetTarget(g->module, buf_ptr(&g->triple_str));
+    LLVMSetTarget(g->module, buf_ptr(&g->llvm_triple_str));
 
     if (target_object_format(g->zig_target) == ZigLLVM_COFF) {
         ZigLLVMAddModuleCodeViewFlag(g->module);
@@ -8113,13 +8113,13 @@ static void init(CodeGen *g) {
 
     LLVMTargetRef target_ref;
     char *err_msg = nullptr;
-    if (LLVMGetTargetFromTriple(buf_ptr(&g->triple_str), &target_ref, &err_msg)) {
+    if (LLVMGetTargetFromTriple(buf_ptr(&g->llvm_triple_str), &target_ref, &err_msg)) {
         fprintf(stderr,
             "Zig is expecting LLVM to understand this target: '%s'\n"
             "However LLVM responded with: \"%s\"\n"
             "Zig is unable to continue. This is a bug in Zig:\n"
             "https://github.com/ziglang/zig/issues/438\n"
-        , buf_ptr(&g->triple_str), err_msg);
+        , buf_ptr(&g->llvm_triple_str), err_msg);
         exit(1);
     }
 
@@ -8153,7 +8153,7 @@ static void init(CodeGen *g) {
         target_specific_features = "";
     }
 
-    g->target_machine = ZigLLVMCreateTargetMachine(target_ref, buf_ptr(&g->triple_str),
+    g->target_machine = ZigLLVMCreateTargetMachine(target_ref, buf_ptr(&g->llvm_triple_str),
             target_specific_cpu_args, target_specific_features, opt_level, reloc_mode,
             LLVMCodeModelDefault, g->function_sections);
 
@@ -8333,12 +8333,12 @@ static void detect_libc(CodeGen *g) {
         !target_os_is_darwin(g->zig_target->os))
     {
         Buf triple_buf = BUF_INIT;
-        get_target_triple(&triple_buf, g->zig_target);
+        target_triple_zig(&triple_buf, g->zig_target);
         fprintf(stderr,
             "Zig is unable to provide a libc for the chosen target '%s'.\n"
             "The target is non-native, so Zig also cannot use the native libc installation.\n"
-            "Choose a target which has a libc available, or provide a libc installation text file.\n"
-            "See `zig libc --help` for more details.\n", buf_ptr(&triple_buf));
+            "Choose a target which has a libc available (see `zig targets`), or\n"
+            "provide a libc installation text file (see `zig libc --help`).\n", buf_ptr(&triple_buf));
         exit(1);
     }
 }
@@ -8397,12 +8397,18 @@ void add_cc_args(CodeGen *g, ZigList<const char *> &args, const char *out_dep_pa
         args.append("-march=native");
     } else {
         args.append("-target");
-        args.append(buf_ptr(&g->triple_str));
+        args.append(buf_ptr(&g->llvm_triple_str));
     }
     if (g->zig_target->os == OsFreestanding) {
         args.append("-ffreestanding");
     }
 
+    // windows.h has files such as pshpack1.h which do #pragma packing, triggering a clang warning.
+    // So for this target, we disable this warning.
+    if (g->zig_target->os == OsWindows && target_abi_is_gnu(g->zig_target->abi)) {
+        args.append("-Wno-pragma-pack");
+    }
+
     if (!g->strip_debug_symbols) {
         args.append("-g");
     }
src/main.cpp
@@ -982,7 +982,7 @@ int main(int argc, char **argv) {
 
     if (target_requires_pic(&target, have_libc) && want_pic == WantPICDisabled) {
         Buf triple_buf = BUF_INIT;
-        get_target_triple(&triple_buf, &target);
+        target_triple_zig(&triple_buf, &target);
         fprintf(stderr, "`--disable-pic` is incompatible with target '%s'\n", buf_ptr(&triple_buf));
         return print_error_usage(arg0);
     }
src/target.cpp
@@ -753,7 +753,16 @@ void init_all_targets(void) {
     LLVMInitializeAllAsmParsers();
 }
 
-void get_target_triple(Buf *triple, const ZigTarget *target) {
+void target_triple_zig(Buf *triple, const ZigTarget *target) {
+    buf_resize(triple, 0);
+    buf_appendf(triple, "%s%s-%s-%s",
+            ZigLLVMGetArchTypeName(target->arch),
+            ZigLLVMGetSubArchTypeName(target->sub_arch),
+            ZigLLVMGetOSTypeName(get_llvm_os_type(target->os)),
+            ZigLLVMGetEnvironmentTypeName(target->abi));
+}
+
+void target_triple_llvm(Buf *triple, const ZigTarget *target) {
     buf_resize(triple, 0);
     buf_appendf(triple, "%s%s-%s-%s-%s",
             ZigLLVMGetArchTypeName(target->arch),
src/target.hpp
@@ -148,7 +148,8 @@ const char *target_oformat_name(ZigLLVM_ObjectFormatType oformat);
 ZigLLVM_ObjectFormatType target_object_format(const ZigTarget *target);
 
 void get_native_target(ZigTarget *target);
-void get_target_triple(Buf *triple, const ZigTarget *target);
+void target_triple_llvm(Buf *triple, const ZigTarget *target);
+void target_triple_zig(Buf *triple, const ZigTarget *target);
 
 void init_all_targets(void);