authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-23 16:01:41-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-24 00:13:54-04:00
log609207801a570508e58d9e02b6ce551fbf1dca30
tree55a32a7357a32ca14d9406f90963b1bddbae4c07
parent8fa6ca6daaca6f22f09ef22ff496f4dc010138ff

make "gnu" (mingw-w64) the default C ABI on Windows

Closes #6565

7 files changed, 31 insertions(+), 305 deletions(-)

lib/std/target.zig+22-13
......@@ -498,8 +498,8 @@ pub const Target = struct {
498498 .netbsd,
499499 .hurd,
500500 .haiku,
501 => return .gnu,
502501 .windows,
502 => return .gnu,
503503 .uefi,
504504 => return .msvc,
505505 .linux,
......@@ -1258,15 +1258,18 @@ pub const Target = struct {
12581258 return linuxTripleSimple(allocator, self.cpu.arch, self.os.tag, self.abi);
12591259 }
12601260
1261 pub fn oFileExt_cpu_arch_abi(cpu_arch: Cpu.Arch, abi: Abi) [:0]const u8 {
1262 switch (abi) {
1263 .msvc => return ".obj",
1261 pub fn oFileExt_os_abi(os_tag: Os.Tag, abi: Abi) [:0]const u8 {
1262 if (abi == .msvc) {
1263 return ".obj";
1264 }
1265 switch (os_tag) {
1266 .windows, .uefi => return ".obj",
12641267 else => return ".o",
12651268 }
12661269 }
12671270
12681271 pub fn oFileExt(self: Target) [:0]const u8 {
1269 return oFileExt_cpu_arch_abi(self.cpu.arch, self.abi);
1272 return oFileExt_os_abi(self.os.tag, self.abi);
12701273 }
12711274
12721275 pub fn exeFileExtSimple(cpu_arch: Cpu.Arch, os_tag: Os.Tag) [:0]const u8 {
......@@ -1285,30 +1288,36 @@ pub const Target = struct {
12851288 return exeFileExtSimple(self.cpu.arch, self.os.tag);
12861289 }
12871290
1288 pub fn staticLibSuffix_cpu_arch_abi(cpu_arch: Cpu.Arch, abi: Abi) [:0]const u8 {
1289 switch (abi) {
1290 .msvc => return ".lib",
1291 pub fn staticLibSuffix_os_abi(os_tag: Os.Tag, abi: Abi) [:0]const u8 {
1292 if (abi == .msvc) {
1293 return ".lib";
1294 }
1295 switch (os_tag) {
1296 .windows, .uefi => return ".lib",
12911297 else => return ".a",
12921298 }
12931299 }
12941300
12951301 pub fn staticLibSuffix(self: Target) [:0]const u8 {
1296 return staticLibSuffix_cpu_arch_abi(self.cpu.arch, self.abi);
1302 return staticLibSuffix_os_abi(self.os.tag, self.abi);
12971303 }
12981304
12991305 pub fn dynamicLibSuffix(self: Target) [:0]const u8 {
13001306 return self.os.tag.dynamicLibSuffix();
13011307 }
13021308
1303 pub fn libPrefix_cpu_arch_abi(cpu_arch: Cpu.Arch, abi: Abi) [:0]const u8 {
1304 switch (abi) {
1305 .msvc => return "",
1309 pub fn libPrefix_os_abi(os_tag: Os.Tag, abi: Abi) [:0]const u8 {
1310 if (abi == .msvc) {
1311 return "";
1312 }
1313 switch (os_tag) {
1314 .windows, .uefi => return "",
13061315 else => return "lib",
13071316 }
13081317 }
13091318
13101319 pub fn libPrefix(self: Target) [:0]const u8 {
1311 return libPrefix_cpu_arch_abi(self.cpu.arch, self.abi);
1320 return libPrefix_os_abi(self.os.tag, self.abi);
13121321 }
13131322
13141323 pub fn getObjectFormatSimple(os_tag: Os.Tag, cpu_arch: Cpu.Arch) ObjectFormat {
lib/std/zig/cross_target.zig+3-3
......@@ -473,7 +473,7 @@ pub const CrossTarget = struct {
473473 }
474474
475475 pub fn oFileExt(self: CrossTarget) [:0]const u8 {
476 return Target.oFileExt_cpu_arch_abi(self.getCpuArch(), self.getAbi());
476 return Target.oFileExt_os_abi(self.getOsTag(), self.getAbi());
477477 }
478478
479479 pub fn exeFileExt(self: CrossTarget) [:0]const u8 {
......@@ -481,7 +481,7 @@ pub const CrossTarget = struct {
481481 }
482482
483483 pub fn staticLibSuffix(self: CrossTarget) [:0]const u8 {
484 return Target.staticLibSuffix_cpu_arch_abi(self.getCpuArch(), self.getAbi());
484 return Target.staticLibSuffix_os_abi(self.getOsTag(), self.getAbi());
485485 }
486486
487487 pub fn dynamicLibSuffix(self: CrossTarget) [:0]const u8 {
......@@ -489,7 +489,7 @@ pub const CrossTarget = struct {
489489 }
490490
491491 pub fn libPrefix(self: CrossTarget) [:0]const u8 {
492 return Target.libPrefix_cpu_arch_abi(self.getCpuArch(), self.getAbi());
492 return Target.libPrefix_os_abi(self.getOsTag(), self.getAbi());
493493 }
494494
495495 pub fn isNativeCpu(self: CrossTarget) bool {
src/Compilation.zig+1-1
......@@ -3164,7 +3164,7 @@ fn detectLibCIncludeDirs(
31643164
31653165 // If linking system libraries and targeting the native abi, default to
31663166 // using the system libc installation.
3167 if (link_system_libs and is_native_abi) {
3167 if (link_system_libs and is_native_abi and !target.isMinGW()) {
31683168 const libc = try arena.create(LibCInstallation);
31693169 libc.* = try LibCInstallation.findNative(.{ .allocator = arena, .verbose = true });
31703170 return detectLibCFromLibCInstallation(arena, target, libc);
src/libc_installation.zig+2-3
......@@ -8,7 +8,6 @@ const build_options = @import("build_options");
88
99const is_darwin = Target.current.isDarwin();
1010const is_windows = Target.current.os.tag == .windows;
11const is_gnu = Target.current.isGnu();
1211const is_haiku = Target.current.os.tag == .haiku;
1312
1413const log = std.log.scoped(.libc_installation);
......@@ -100,14 +99,14 @@ pub const LibCInstallation = struct {
10099 log.err("crt_dir may not be empty for {s}\n", .{@tagName(Target.current.os.tag)});
101100 return error.ParseError;
102101 }
103 if (self.msvc_lib_dir == null and is_windows and !is_gnu) {
102 if (self.msvc_lib_dir == null and is_windows) {
104103 log.err("msvc_lib_dir may not be empty for {s}-{s}\n", .{
105104 @tagName(Target.current.os.tag),
106105 @tagName(Target.current.abi),
107106 });
108107 return error.ParseError;
109108 }
110 if (self.kernel32_lib_dir == null and is_windows and !is_gnu) {
109 if (self.kernel32_lib_dir == null and is_windows) {
111110 log.err("kernel32_lib_dir may not be empty for {s}-{s}\n", .{
112111 @tagName(Target.current.os.tag),
113112 @tagName(Target.current.abi),
src/link/Coff.zig+2-2
......@@ -1137,14 +1137,14 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
11371137 }
11381138
11391139 if (is_dyn_lib) {
1140 try argv.append(try comp.get_libc_crt_file(arena, "dllcrt2.o"));
1140 try argv.append(try comp.get_libc_crt_file(arena, "dllcrt2.obj"));
11411141 if (target.cpu.arch == .i386) {
11421142 try argv.append("-ALTERNATENAME:__DllMainCRTStartup@12=_DllMainCRTStartup@12");
11431143 } else {
11441144 try argv.append("-ALTERNATENAME:_DllMainCRTStartup=DllMainCRTStartup");
11451145 }
11461146 } else {
1147 try argv.append(try comp.get_libc_crt_file(arena, "crt2.o"));
1147 try argv.append(try comp.get_libc_crt_file(arena, "crt2.obj"));
11481148 }
11491149
11501150 try argv.append(try comp.get_libc_crt_file(arena, "mingw32.lib"));
src/stage1/target.cpp+1-268
......@@ -756,8 +756,7 @@ bool target_allows_addr_zero(const ZigTarget *target) {
756756
757757const char *target_o_file_ext(const ZigTarget *target) {
758758 if (target->abi == ZigLLVM_MSVC ||
759 (target->os == OsWindows && !target_abi_is_gnu(target->abi)) ||
760 target->os == OsUefi)
759 target->os == OsWindows || target->os == OsUefi)
761760 {
762761 return ".obj";
763762 } else {
......@@ -777,29 +776,6 @@ bool target_is_android(const ZigTarget *target) {
777776 return target->abi == ZigLLVM_Android;
778777}
779778
780bool target_can_exec(const ZigTarget *host_target, const ZigTarget *guest_target) {
781 assert(host_target != nullptr);
782
783 if (guest_target == nullptr) {
784 // null guest target means that the guest target is native
785 return true;
786 }
787
788 if (guest_target->os == host_target->os && guest_target->arch == host_target->arch) {
789 // OS and arch match
790 return true;
791 }
792
793 if (guest_target->os == OsWindows && host_target->os == OsWindows &&
794 host_target->arch == ZigLLVM_x86_64 && guest_target->arch == ZigLLVM_x86)
795 {
796 // 64-bit windows can run 32-bit programs
797 return true;
798 }
799
800 return false;
801}
802
803779const char *arch_stack_pointer_register_name(ZigLLVM_ArchType arch) {
804780 switch (arch) {
805781 case ZigLLVM_UnknownArch:
......@@ -946,21 +922,6 @@ bool target_has_valgrind_support(const ZigTarget *target) {
946922 zig_unreachable();
947923}
948924
949bool target_os_requires_libc(Os os) {
950 // On Darwin, we always link libSystem which contains libc.
951 // Similarly on FreeBSD and NetBSD we always link system libc
952 // since this is the stable syscall interface.
953 return (target_os_is_darwin(os) || os == OsFreeBSD || os == OsNetBSD || os == OsDragonFly);
954}
955
956bool target_is_glibc(const ZigTarget *target) {
957 return target->os == OsLinux && target_abi_is_gnu(target->abi);
958}
959
960bool target_is_musl(const ZigTarget *target) {
961 return target->os == OsLinux && target_abi_is_musl(target->abi);
962}
963
964925bool target_is_wasm(const ZigTarget *target) {
965926 return target->arch == ZigLLVM_wasm32 || target->arch == ZigLLVM_wasm64;
966927}
......@@ -1019,238 +980,10 @@ ZigLLVM_EnvironmentType target_default_abi(ZigLLVM_ArchType arch, Os os) {
1019980 zig_unreachable();
1020981}
1021982
1022bool target_abi_is_gnu(ZigLLVM_EnvironmentType abi) {
1023 switch (abi) {
1024 case ZigLLVM_GNU:
1025 case ZigLLVM_GNUABIN32:
1026 case ZigLLVM_GNUABI64:
1027 case ZigLLVM_GNUEABI:
1028 case ZigLLVM_GNUEABIHF:
1029 case ZigLLVM_GNUX32:
1030 return true;
1031 default:
1032 return false;
1033 }
1034}
1035
1036bool target_abi_is_musl(ZigLLVM_EnvironmentType abi) {
1037 switch (abi) {
1038 case ZigLLVM_Musl:
1039 case ZigLLVM_MuslEABI:
1040 case ZigLLVM_MuslEABIHF:
1041 return true;
1042 default:
1043 return false;
1044 }
1045}
1046
1047struct AvailableLibC {
1048 ZigLLVM_ArchType arch;
1049 Os os;
1050 ZigLLVM_EnvironmentType abi;
1051};
1052
1053static const AvailableLibC libcs_available[] = {
1054 {ZigLLVM_aarch64_be, OsLinux, ZigLLVM_GNU},
1055 {ZigLLVM_aarch64_be, OsLinux, ZigLLVM_Musl},
1056 {ZigLLVM_aarch64_be, OsWindows, ZigLLVM_GNU},
1057 {ZigLLVM_aarch64, OsLinux, ZigLLVM_GNU},
1058 {ZigLLVM_aarch64, OsLinux, ZigLLVM_Musl},
1059 {ZigLLVM_aarch64, OsWindows, ZigLLVM_GNU},
1060 {ZigLLVM_armeb, OsLinux, ZigLLVM_GNUEABI},
1061 {ZigLLVM_armeb, OsLinux, ZigLLVM_GNUEABIHF},
1062 {ZigLLVM_armeb, OsLinux, ZigLLVM_MuslEABI},
1063 {ZigLLVM_armeb, OsLinux, ZigLLVM_MuslEABIHF},
1064 {ZigLLVM_armeb, OsWindows, ZigLLVM_GNU},
1065 {ZigLLVM_arm, OsLinux, ZigLLVM_GNUEABI},
1066 {ZigLLVM_arm, OsLinux, ZigLLVM_GNUEABIHF},
1067 {ZigLLVM_arm, OsLinux, ZigLLVM_MuslEABI},
1068 {ZigLLVM_arm, OsLinux, ZigLLVM_MuslEABIHF},
1069 {ZigLLVM_arm, OsWindows, ZigLLVM_GNU},
1070 {ZigLLVM_csky, OsLinux, ZigLLVM_GNUEABI},
1071 {ZigLLVM_csky, OsLinux, ZigLLVM_GNUEABIHF},
1072 {ZigLLVM_x86, OsLinux, ZigLLVM_GNU},
1073 {ZigLLVM_x86, OsLinux, ZigLLVM_Musl},
1074 {ZigLLVM_x86, OsWindows, ZigLLVM_GNU},
1075 {ZigLLVM_mips64el, OsLinux, ZigLLVM_GNUABI64},
1076 {ZigLLVM_mips64el, OsLinux, ZigLLVM_GNUABIN32},
1077 {ZigLLVM_mips64el, OsLinux, ZigLLVM_Musl},
1078 {ZigLLVM_mips64, OsLinux, ZigLLVM_GNUABI64},
1079 {ZigLLVM_mips64, OsLinux, ZigLLVM_GNUABIN32},
1080 {ZigLLVM_mips64, OsLinux, ZigLLVM_Musl},
1081 {ZigLLVM_mipsel, OsLinux, ZigLLVM_GNU},
1082 {ZigLLVM_mipsel, OsLinux, ZigLLVM_Musl},
1083 {ZigLLVM_mips, OsLinux, ZigLLVM_GNU},
1084 {ZigLLVM_mips, OsLinux, ZigLLVM_Musl},
1085 {ZigLLVM_ppc64le, OsLinux, ZigLLVM_GNU},
1086 {ZigLLVM_ppc64le, OsLinux, ZigLLVM_Musl},
1087 {ZigLLVM_ppc64, OsLinux, ZigLLVM_GNU},
1088 {ZigLLVM_ppc64, OsLinux, ZigLLVM_Musl},
1089 {ZigLLVM_ppc, OsLinux, ZigLLVM_GNU},
1090 {ZigLLVM_ppc, OsLinux, ZigLLVM_Musl},
1091 {ZigLLVM_riscv64, OsLinux, ZigLLVM_GNU},
1092 {ZigLLVM_riscv64, OsLinux, ZigLLVM_Musl},
1093 {ZigLLVM_systemz, OsLinux, ZigLLVM_GNU},
1094 {ZigLLVM_systemz, OsLinux, ZigLLVM_Musl},
1095 {ZigLLVM_sparc, OsLinux, ZigLLVM_GNU},
1096 {ZigLLVM_sparcv9, OsLinux, ZigLLVM_GNU},
1097 {ZigLLVM_wasm32, OsFreestanding, ZigLLVM_Musl},
1098 {ZigLLVM_x86_64, OsLinux, ZigLLVM_GNU},
1099 {ZigLLVM_x86_64, OsLinux, ZigLLVM_GNUX32},
1100 {ZigLLVM_x86_64, OsLinux, ZigLLVM_Musl},
1101 {ZigLLVM_x86_64, OsWindows, ZigLLVM_GNU},
1102};
1103
1104bool target_can_build_libc(const ZigTarget *target) {
1105 for (size_t i = 0; i < array_length(libcs_available); i += 1) {
1106 if (target->arch == libcs_available[i].arch &&
1107 target->os == libcs_available[i].os &&
1108 target->abi == libcs_available[i].abi)
1109 {
1110 return true;
1111 }
1112 }
1113 return false;
1114}
1115
1116const char *target_libc_generic_name(const ZigTarget *target) {
1117 if (target->os == OsWindows) {
1118 return "mingw";
1119 }
1120 switch (target->abi) {
1121 case ZigLLVM_GNU:
1122 case ZigLLVM_GNUABIN32:
1123 case ZigLLVM_GNUABI64:
1124 case ZigLLVM_GNUEABI:
1125 case ZigLLVM_GNUEABIHF:
1126 case ZigLLVM_GNUX32:
1127 case ZigLLVM_GNUILP32:
1128 return "glibc";
1129 case ZigLLVM_Musl:
1130 case ZigLLVM_MuslEABI:
1131 case ZigLLVM_MuslEABIHF:
1132 case ZigLLVM_UnknownEnvironment:
1133 return "musl";
1134 case ZigLLVM_CODE16:
1135 case ZigLLVM_EABI:
1136 case ZigLLVM_EABIHF:
1137 case ZigLLVM_Android:
1138 case ZigLLVM_MSVC:
1139 case ZigLLVM_Itanium:
1140 case ZigLLVM_Cygnus:
1141 case ZigLLVM_CoreCLR:
1142 case ZigLLVM_Simulator:
1143 case ZigLLVM_MacABI:
1144 zig_unreachable();
1145 }
1146 zig_unreachable();
1147}
1148
1149bool target_is_libc_lib_name(const ZigTarget *target, const char *name) {
1150 auto equal = str_eql_str;
1151 if (target->os == OsMacOSX)
1152 equal = str_eql_str_ignore_case;
1153
1154 if (equal(name, "c"))
1155 return true;
1156
1157 if (target_abi_is_gnu(target->abi) && target->os == OsWindows) {
1158 // mingw-w64
1159
1160 if (equal(name, "m"))
1161 return true;
1162
1163 return false;
1164 }
1165
1166 if (target_abi_is_gnu(target->abi) || target_abi_is_musl(target->abi) || target_os_is_darwin(target->os)) {
1167 if (equal(name, "m"))
1168 return true;
1169 if (equal(name, "rt"))
1170 return true;
1171 if (equal(name, "pthread"))
1172 return true;
1173 if (equal(name, "crypt"))
1174 return true;
1175 if (equal(name, "util"))
1176 return true;
1177 if (equal(name, "xnet"))
1178 return true;
1179 if (equal(name, "resolv"))
1180 return true;
1181 if (equal(name, "dl"))
1182 return true;
1183 }
1184
1185 if (target_os_is_darwin(target->os) && equal(name, "System"))
1186 return true;
1187
1188 return false;
1189}
1190
1191bool target_is_libcpp_lib_name(const ZigTarget *target, const char *name) {
1192 if (strcmp(name, "c++") == 0 || strcmp(name, "c++abi") == 0)
1193 return true;
1194
1195 return false;
1196}
1197
1198size_t target_libc_count(void) {
1199 return array_length(libcs_available);
1200}
1201
1202void target_libc_enum(size_t index, ZigTarget *out_target) {
1203 assert(index < array_length(libcs_available));
1204 out_target->arch = libcs_available[index].arch;
1205 out_target->os = libcs_available[index].os;
1206 out_target->abi = libcs_available[index].abi;
1207 out_target->is_native_os = false;
1208 out_target->is_native_cpu = false;
1209}
1210
1211983bool target_has_debug_info(const ZigTarget *target) {
1212984 return !target_is_wasm(target);
1213985}
1214986
1215const char *target_arch_musl_name(ZigLLVM_ArchType arch) {
1216 switch (arch) {
1217 case ZigLLVM_aarch64:
1218 case ZigLLVM_aarch64_be:
1219 return "aarch64";
1220 case ZigLLVM_arm:
1221 case ZigLLVM_armeb:
1222 return "arm";
1223 case ZigLLVM_mips:
1224 case ZigLLVM_mipsel:
1225 return "mips";
1226 case ZigLLVM_mips64el:
1227 case ZigLLVM_mips64:
1228 return "mips64";
1229 case ZigLLVM_ppc:
1230 return "powerpc";
1231 case ZigLLVM_ppc64:
1232 case ZigLLVM_ppc64le:
1233 return "powerpc64";
1234 case ZigLLVM_systemz:
1235 return "s390x";
1236 case ZigLLVM_x86:
1237 return "i386";
1238 case ZigLLVM_x86_64:
1239 return "x86_64";
1240 case ZigLLVM_riscv64:
1241 return "riscv64";
1242 default:
1243 zig_unreachable();
1244 }
1245}
1246
1247bool target_libc_needs_crti_crtn(const ZigTarget *target) {
1248 if (target->arch == ZigLLVM_riscv32 || target->arch == ZigLLVM_riscv64 || target_is_android(target)) {
1249 return false;
1250 }
1251 return true;
1252}
1253
1254987bool target_is_riscv(const ZigTarget *target) {
1255988 return target->arch == ZigLLVM_riscv32 || target->arch == ZigLLVM_riscv64;
1256989}
src/stage1/target.hpp-15
......@@ -66,7 +66,6 @@ const char *target_o_file_ext(const ZigTarget *target);
6666const char *target_asm_file_ext(const ZigTarget *target);
6767const char *target_llvm_ir_file_ext(const ZigTarget *target);
6868
69bool target_can_exec(const ZigTarget *host_target, const ZigTarget *guest_target);
7069ZigLLVM_OSType get_llvm_os_type(Os os_type);
7170
7271bool target_is_arm(const ZigTarget *target);
......@@ -75,29 +74,15 @@ bool target_is_ppc(const ZigTarget *target);
7574bool target_allows_addr_zero(const ZigTarget *target);
7675bool target_has_valgrind_support(const ZigTarget *target);
7776bool target_os_is_darwin(Os os);
78bool target_os_requires_libc(Os os);
79bool target_can_build_libc(const ZigTarget *target);
80const char *target_libc_generic_name(const ZigTarget *target);
81bool target_is_libc_lib_name(const ZigTarget *target, const char *name);
82bool target_is_libcpp_lib_name(const ZigTarget *target, const char *name);
83bool target_abi_is_gnu(ZigLLVM_EnvironmentType abi);
84bool target_abi_is_musl(ZigLLVM_EnvironmentType abi);
85bool target_is_glibc(const ZigTarget *target);
86bool target_is_musl(const ZigTarget *target);
8777bool target_is_wasm(const ZigTarget *target);
8878bool target_is_riscv(const ZigTarget *target);
8979bool target_is_sparc(const ZigTarget *target);
9080bool target_is_android(const ZigTarget *target);
9181bool target_has_debug_info(const ZigTarget *target);
92const char *target_arch_musl_name(ZigLLVM_ArchType arch);
9382
9483uint32_t target_arch_pointer_bit_width(ZigLLVM_ArchType arch);
9584uint32_t target_arch_largest_atomic_bits(ZigLLVM_ArchType arch);
9685
97size_t target_libc_count(void);
98void target_libc_enum(size_t index, ZigTarget *out_target);
99bool target_libc_needs_crti_crtn(const ZigTarget *target);
100
10186unsigned target_fn_ptr_align(const ZigTarget *target);
10287unsigned target_fn_align(const ZigTarget *target);
10388