1 # Define the list of system with their properties.
3 # See https://clang.llvm.org/docs/CrossCompilation.html and
4 # http://llvm.org/docs/doxygen/html/Triple_8cpp_source.html especially
5 # Triple::normalize. Parsing should essentially act as a more conservative
6 # version of that last function.
8 # Most of the types below come in "open" and "closed" pairs. The open ones
9 # specify what information we need to know about systems in general, and the
10 # closed ones are sub-types representing the whitelist of systems we support in
13 # Code in the remainder of nixpkgs shouldn't rely on the closed ones in
14 # e.g. exhaustive cases. Its more a sanity check to make sure nobody defines
15 # systems that overlap with existing ones and won't notice something amiss.
22 with (import ./inspect.nix { inherit lib; }).predicates;
25 inherit (lib.options) mergeOneOption;
28 mapAttrs (name: value:
29 assert type.check value;
30 setType type.name ({ inherit name; } // value));
32 # gnu-config will ignore the portion of a triple matching the
33 # regex `e?abi.*$` when determining the validity of a triple. In
34 # other words, `i386-linuxabichickenlips` is a valid triple.
36 let match = builtins.match "(.*)e?abi.*" x;
39 else lib.elemAt match 0;
45 ################################################################################
47 types.openSignificantByte = mkOptionType {
48 name = "significant-byte";
49 description = "Endianness";
50 merge = mergeOneOption;
53 types.significantByte = enum (attrValues significantBytes);
55 significantBytes = setTypes types.openSignificantByte {
60 ################################################################################
62 # Reasonable power of 2
63 types.bitWidth = enum [ 8 16 32 64 128 ];
65 ################################################################################
67 types.openCpuType = mkOptionType {
69 description = "instruction set architecture name and information";
70 merge = mergeOneOption;
71 check = x: types.bitWidth.check x.bits
73 then types.significantByte.check x.significantByte
74 else !(x ? significantByte));
77 types.cpuType = enum (attrValues cpuTypes);
79 cpuTypes = with significantBytes; setTypes types.openCpuType {
80 arm = { bits = 32; significantByte = littleEndian; family = "arm"; };
81 armv5tel = { bits = 32; significantByte = littleEndian; family = "arm"; version = "5"; arch = "armv5t"; };
82 armv6m = { bits = 32; significantByte = littleEndian; family = "arm"; version = "6"; arch = "armv6-m"; };
83 armv6l = { bits = 32; significantByte = littleEndian; family = "arm"; version = "6"; arch = "armv6"; };
84 armv7a = { bits = 32; significantByte = littleEndian; family = "arm"; version = "7"; arch = "armv7-a"; };
85 armv7r = { bits = 32; significantByte = littleEndian; family = "arm"; version = "7"; arch = "armv7-r"; };
86 armv7m = { bits = 32; significantByte = littleEndian; family = "arm"; version = "7"; arch = "armv7-m"; };
87 armv7l = { bits = 32; significantByte = littleEndian; family = "arm"; version = "7"; arch = "armv7"; };
88 armv8a = { bits = 32; significantByte = littleEndian; family = "arm"; version = "8"; arch = "armv8-a"; };
89 armv8r = { bits = 32; significantByte = littleEndian; family = "arm"; version = "8"; arch = "armv8-a"; };
90 armv8m = { bits = 32; significantByte = littleEndian; family = "arm"; version = "8"; arch = "armv8-m"; };
91 aarch64 = { bits = 64; significantByte = littleEndian; family = "arm"; version = "8"; arch = "armv8-a"; };
92 aarch64_be = { bits = 64; significantByte = bigEndian; family = "arm"; version = "8"; arch = "armv8-a"; };
94 i386 = { bits = 32; significantByte = littleEndian; family = "x86"; arch = "i386"; };
95 i486 = { bits = 32; significantByte = littleEndian; family = "x86"; arch = "i486"; };
96 i586 = { bits = 32; significantByte = littleEndian; family = "x86"; arch = "i586"; };
97 i686 = { bits = 32; significantByte = littleEndian; family = "x86"; arch = "i686"; };
98 x86_64 = { bits = 64; significantByte = littleEndian; family = "x86"; arch = "x86-64"; };
100 microblaze = { bits = 32; significantByte = bigEndian; family = "microblaze"; };
101 microblazeel = { bits = 32; significantByte = littleEndian; family = "microblaze"; };
103 mips = { bits = 32; significantByte = bigEndian; family = "mips"; };
104 mipsel = { bits = 32; significantByte = littleEndian; family = "mips"; };
105 mips64 = { bits = 64; significantByte = bigEndian; family = "mips"; };
106 mips64el = { bits = 64; significantByte = littleEndian; family = "mips"; };
108 mmix = { bits = 64; significantByte = bigEndian; family = "mmix"; };
110 m68k = { bits = 32; significantByte = bigEndian; family = "m68k"; };
112 powerpc = { bits = 32; significantByte = bigEndian; family = "power"; };
113 powerpc64 = { bits = 64; significantByte = bigEndian; family = "power"; };
114 powerpc64le = { bits = 64; significantByte = littleEndian; family = "power"; };
115 powerpcle = { bits = 32; significantByte = littleEndian; family = "power"; };
117 riscv32 = { bits = 32; significantByte = littleEndian; family = "riscv"; };
118 riscv64 = { bits = 64; significantByte = littleEndian; family = "riscv"; };
120 s390 = { bits = 32; significantByte = bigEndian; family = "s390"; };
121 s390x = { bits = 64; significantByte = bigEndian; family = "s390"; };
123 sparc = { bits = 32; significantByte = bigEndian; family = "sparc"; };
124 sparc64 = { bits = 64; significantByte = bigEndian; family = "sparc"; };
126 wasm32 = { bits = 32; significantByte = littleEndian; family = "wasm"; };
127 wasm64 = { bits = 64; significantByte = littleEndian; family = "wasm"; };
129 alpha = { bits = 64; significantByte = littleEndian; family = "alpha"; };
131 rx = { bits = 32; significantByte = littleEndian; family = "rx"; };
132 msp430 = { bits = 16; significantByte = littleEndian; family = "msp430"; };
133 avr = { bits = 8; family = "avr"; };
135 vc4 = { bits = 32; significantByte = littleEndian; family = "vc4"; };
137 or1k = { bits = 32; significantByte = bigEndian; family = "or1k"; };
139 loongarch64 = { bits = 64; significantByte = littleEndian; family = "loongarch"; };
141 javascript = { bits = 32; significantByte = littleEndian; family = "javascript"; };
144 # GNU build systems assume that older NetBSD architectures are using a.out.
145 gnuNetBSDDefaultExecFormat = cpu:
146 if (cpu.family == "arm" && cpu.bits == 32) ||
147 (cpu.family == "sparc" && cpu.bits == 32) ||
148 (cpu.family == "m68k" && cpu.bits == 32) ||
149 (cpu.family == "x86" && cpu.bits == 32)
150 then execFormats.aout
151 else execFormats.elf;
153 # Determine when two CPUs are compatible with each other. That is,
154 # can code built for system B run on system A? For that to happen,
155 # the programs that system B accepts must be a subset of the
156 # programs that system A accepts.
158 # We have the following properties of the compatibility relation,
159 # which must be preserved when adding compatibility information for
162 # Every CPU is compatible with itself.
164 # If A is compatible with B and B is compatible with C then A is compatible with C.
166 # Note: Since 22.11 the archs of a mode switching CPU are no longer considered
167 # pairwise compatible. Mode switching implies that binaries built for A
168 # and B respectively can't be executed at the same time.
169 isCompatible = a: b: with cpuTypes; lib.any lib.id [
171 (b == i386 && isCompatible a i486)
172 (b == i486 && isCompatible a i586)
173 (b == i586 && isCompatible a i686)
175 # XXX: Not true in some cases. Like in WSL mode.
176 (b == i686 && isCompatible a x86_64)
179 (b == arm && isCompatible a armv5tel)
182 (b == armv5tel && isCompatible a armv6l)
185 (b == armv6l && isCompatible a armv6m)
186 (b == armv6m && isCompatible a armv7l)
189 (b == armv7l && isCompatible a armv7a)
190 (b == armv7l && isCompatible a armv7r)
191 (b == armv7l && isCompatible a armv7m)
194 (b == aarch64 && a == armv8a)
195 (b == armv8a && isCompatible a aarch64)
196 (b == armv8r && isCompatible a armv8a)
197 (b == armv8m && isCompatible a armv8a)
200 (b == powerpc && isCompatible a powerpc64)
201 (b == powerpcle && isCompatible a powerpc64le)
204 (b == mips && isCompatible a mips64)
205 (b == mipsel && isCompatible a mips64el)
208 (b == riscv32 && isCompatible a riscv64)
211 (b == sparc && isCompatible a sparc64)
214 (b == wasm32 && isCompatible a wasm64)
220 ################################################################################
222 types.openVendor = mkOptionType {
224 description = "vendor for the platform";
225 merge = mergeOneOption;
228 types.vendor = enum (attrValues vendors);
230 vendors = setTypes types.openVendor {
235 # Actually matters, unlocking some MinGW-w64-specific options in GCC. See
236 # bottom of https://sourceforge.net/p/mingw-w64/wiki2/Unicode%20apps/
243 ################################################################################
245 types.openExecFormat = mkOptionType {
246 name = "exec-format";
247 description = "executable container used by the kernel";
248 merge = mergeOneOption;
251 types.execFormat = enum (attrValues execFormats);
253 execFormats = setTypes types.openExecFormat {
263 ################################################################################
265 types.openKernelFamily = mkOptionType {
266 name = "exec-format";
267 description = "executable container used by the kernel";
268 merge = mergeOneOption;
271 types.kernelFamily = enum (attrValues kernelFamilies);
273 kernelFamilies = setTypes types.openKernelFamily {
278 ################################################################################
280 types.openKernel = mkOptionType {
282 description = "kernel name and information";
283 merge = mergeOneOption;
284 check = x: types.execFormat.check x.execFormat
285 && all types.kernelFamily.check (attrValues x.families);
288 types.kernel = enum (attrValues kernels);
290 kernels = with execFormats; with kernelFamilies; setTypes types.openKernel {
291 # TODO(@Ericson2314): Don't want to mass-rebuild yet to keeping 'darwin' as
292 # the normalized name for macOS.
293 macos = { execFormat = macho; families = { inherit darwin; }; name = "darwin"; };
294 ios = { execFormat = macho; families = { inherit darwin; }; };
295 # A tricky thing about FreeBSD is that there is no stable ABI across
296 # versions. That means that putting in the version as part of the
297 # config string is paramount.
298 freebsd12 = { execFormat = elf; families = { inherit bsd; }; name = "freebsd"; version = 12; };
299 freebsd13 = { execFormat = elf; families = { inherit bsd; }; name = "freebsd"; version = 13; };
300 linux = { execFormat = elf; families = { }; };
301 netbsd = { execFormat = elf; families = { inherit bsd; }; };
302 none = { execFormat = unknown; families = { }; };
303 openbsd = { execFormat = elf; families = { inherit bsd; }; };
304 solaris = { execFormat = elf; families = { }; };
305 wasi = { execFormat = wasm; families = { }; };
306 redox = { execFormat = elf; families = { }; };
307 windows = { execFormat = pe; families = { }; };
308 ghcjs = { execFormat = unknown; families = { }; };
309 genode = { execFormat = elf; families = { }; };
310 mmixware = { execFormat = unknown; families = { }; };
312 # 'darwin' is the kernel for all of them. We choose macOS by default.
313 darwin = kernels.macos;
314 watchos = kernels.ios;
316 win32 = kernels.windows;
319 ################################################################################
321 types.openAbi = mkOptionType {
323 description = "binary interface for compiled code and syscalls";
324 merge = mergeOneOption;
327 types.abi = enum (attrValues abis);
329 abis = setTypes types.openAbi {
333 # Note: eabi is specific to ARM and PowerPC.
334 # On PowerPC, this corresponds to PPCEABI.
335 # On ARM, this corresponds to ARMEABI.
336 eabi = { float = "soft"; };
337 eabihf = { float = "hard"; };
339 # Other architectures should use ELF in embedded situations.
345 { assertion = platform: !platform.isAarch32;
347 The "android" ABI is not for 32-bit ARM. Use "androideabi" instead.
353 gnueabi = { float = "soft"; };
354 gnueabihf = { float = "hard"; };
357 { assertion = platform: !platform.isAarch32;
359 The "gnu" ABI is ambiguous on 32-bit ARM. Use "gnueabi" or "gnueabihf" instead.
362 { assertion = platform: with platform; !(isPower64 && isBigEndian);
364 The "gnu" ABI is ambiguous on big-endian 64-bit PowerPC. Use "gnuabielfv2" or "gnuabielfv1" instead.
369 gnuabi64 = { abi = "64"; };
370 muslabi64 = { abi = "64"; };
372 # NOTE: abi=n32 requires a 64-bit MIPS chip! That is not a typo.
373 # It is basically the 64-bit abi with 32-bit pointers. Details:
374 # https://www.linux-mips.org/pub/linux/mips/doc/ABI/MIPS-N32-ABI-Handbook.pdf
375 gnuabin32 = { abi = "n32"; };
376 muslabin32 = { abi = "n32"; };
378 gnuabielfv2 = { abi = "elfv2"; };
379 gnuabielfv1 = { abi = "elfv1"; };
381 musleabi = { float = "soft"; };
382 musleabihf = { float = "hard"; };
385 uclibceabi = { float = "soft"; };
386 uclibceabihf = { float = "hard"; };
392 ################################################################################
394 types.parsedPlatform = mkOptionType {
396 description = "fully parsed representation of llvm- or nix-style platform tuple";
397 merge = mergeOneOption;
398 check = { cpu, vendor, kernel, abi }:
399 types.cpuType.check cpu
400 && types.vendor.check vendor
401 && types.kernel.check kernel
402 && types.abi.check abi;
405 isSystem = isType "system";
407 mkSystem = components:
408 assert types.parsedPlatform.check components;
409 setType "system" components;
411 mkSkeletonFromList = l: {
412 "1" = if elemAt l 0 == "avr"
413 then { cpu = elemAt l 0; kernel = "none"; abi = "unknown"; }
414 else throw "Target specification with 1 components is ambiguous";
415 "2" = # We only do 2-part hacks for things Nix already supports
416 if elemAt l 1 == "cygwin"
417 then { cpu = elemAt l 0; kernel = "windows"; abi = "cygnus"; }
418 # MSVC ought to be the default ABI so this case isn't needed. But then it
419 # becomes difficult to handle the gnu* variants for Aarch32 correctly for
420 # minGW. So it's easier to make gnu* the default for the MinGW, but
421 # hack-in MSVC for the non-MinGW case right here.
422 else if elemAt l 1 == "windows"
423 then { cpu = elemAt l 0; kernel = "windows"; abi = "msvc"; }
424 else if (elemAt l 1) == "elf"
425 then { cpu = elemAt l 0; vendor = "unknown"; kernel = "none"; abi = elemAt l 1; }
426 else { cpu = elemAt l 0; kernel = elemAt l 1; };
428 # cpu-kernel-environment
429 if elemAt l 1 == "linux" ||
430 elem (elemAt l 2) ["eabi" "eabihf" "elf" "gnu"]
438 else if elemAt l 1 == "apple" ||
439 elem (elemAt l 2) [ "wasi" "redox" "mmixware" "ghcjs" "mingw32" ] ||
440 hasPrefix "freebsd" (elemAt l 2) ||
441 hasPrefix "netbsd" (elemAt l 2) ||
442 hasPrefix "genode" (elemAt l 2)
446 kernel = if elemAt l 2 == "mingw32"
447 then "windows" # autotools breaks on -gnu for window
450 else throw "Target specification with 3 components is ambiguous";
451 "4" = { cpu = elemAt l 0; vendor = elemAt l 1; kernel = elemAt l 2; abi = elemAt l 3; };
452 }.${toString (length l)}
453 or (throw "system string has invalid number of hyphen-separated components");
455 # This should revert the job done by config.guess from the gcc compiler.
456 mkSystemFromSkeleton = { cpu
457 , # Optional, but fallback too complex for here.
458 # Inferred below instead.
459 vendor ? assert false; null
461 , # Also inferred below
462 abi ? assert false; null
464 getCpu = name: cpuTypes.${name} or (throw "Unknown CPU type: ${name}");
465 getVendor = name: vendors.${name} or (throw "Unknown vendor: ${name}");
466 getKernel = name: kernels.${name} or (throw "Unknown kernel: ${name}");
467 getAbi = name: abis.${name} or (throw "Unknown ABI: ${name}");
470 cpu = getCpu args.cpu;
472 /**/ if args ? vendor then getVendor args.vendor
473 else if isDarwin parsed then vendors.apple
474 else if isWindows parsed then vendors.pc
475 else vendors.unknown;
476 kernel = if hasPrefix "darwin" args.kernel then getKernel "darwin"
477 else if hasPrefix "netbsd" args.kernel then getKernel "netbsd"
478 else getKernel (removeAbiSuffix args.kernel);
480 /**/ if args ? abi then getAbi args.abi
481 else if isLinux parsed || isWindows parsed then
482 if isAarch32 parsed then
483 if lib.versionAtLeast (parsed.cpu.version or "0") "6"
486 # Default ppc64 BE to ELFv2
487 else if isPower64 parsed && isBigEndian parsed then abis.gnuabielfv2
494 mkSystemFromString = s: mkSystemFromSkeleton (mkSkeletonFromList (lib.splitString "-" s));
497 kernel.name + toString (kernel.version or "");
499 doubleFromSystem = { cpu, kernel, abi, ... }:
500 /**/ if abi == abis.cygnus then "${cpu.name}-cygwin"
501 else if kernel.families ? darwin then "${cpu.name}-darwin"
502 else "${cpu.name}-${kernelName kernel}";
504 tripleFromSystem = { cpu, vendor, kernel, abi, ... } @ sys: assert isSystem sys; let
506 lib.optionalString (kernel.name == "netbsd" &&
507 gnuNetBSDDefaultExecFormat cpu != kernel.execFormat)
508 kernel.execFormat.name;
509 optAbi = lib.optionalString (abi != abis.unknown) "-${abi.name}";
510 in "${cpu.name}-${vendor.name}-${kernelName kernel}${optExecFormat}${optAbi}";
512 ################################################################################