1# Define the list of system with their properties.
2#
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.
7#
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
11# practice.
12#
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.
16#
17{ lib }:
18with lib.lists;
19with lib.types;
20with lib.attrsets;
21with lib.strings;
22with (import ./inspect.nix { inherit lib; }).predicates;
23
24let
25 inherit (lib.options) mergeOneOption;
26
27 setTypes = type:
28 mapAttrs (name: value:
29 assert type.check value;
30 setType type.name ({ inherit name; } // value));
31
32in
33
34rec {
35
36 ################################################################################
37
38 types.openSignificantByte = mkOptionType {
39 name = "significant-byte";
40 description = "Endianness";
41 merge = mergeOneOption;
42 };
43
44 types.significantByte = enum (attrValues significantBytes);
45
46 significantBytes = setTypes types.openSignificantByte {
47 bigEndian = {};
48 littleEndian = {};
49 };
50
51 ################################################################################
52
53 # Reasonable power of 2
54 types.bitWidth = enum [ 8 16 32 64 128 ];
55
56 ################################################################################
57
58 types.openCpuType = mkOptionType {
59 name = "cpu-type";
60 description = "instruction set architecture name and information";
61 merge = mergeOneOption;
62 check = x: types.bitWidth.check x.bits
63 && (if 8 < x.bits
64 then types.significantByte.check x.significantByte
65 else !(x ? significantByte));
66 };
67
68 types.cpuType = enum (attrValues cpuTypes);
69
70 cpuTypes = with significantBytes; setTypes types.openCpuType {
71 arm = { bits = 32; significantByte = littleEndian; family = "arm"; };
72 armv5tel = { bits = 32; significantByte = littleEndian; family = "arm"; version = "5"; arch = "armv5t"; };
73 armv6m = { bits = 32; significantByte = littleEndian; family = "arm"; version = "6"; arch = "armv6-m"; };
74 armv6l = { bits = 32; significantByte = littleEndian; family = "arm"; version = "6"; arch = "armv6"; };
75 armv7a = { bits = 32; significantByte = littleEndian; family = "arm"; version = "7"; arch = "armv7-a"; };
76 armv7r = { bits = 32; significantByte = littleEndian; family = "arm"; version = "7"; arch = "armv7-r"; };
77 armv7m = { bits = 32; significantByte = littleEndian; family = "arm"; version = "7"; arch = "armv7-m"; };
78 armv7l = { bits = 32; significantByte = littleEndian; family = "arm"; version = "7"; arch = "armv7"; };
79 armv8a = { bits = 32; significantByte = littleEndian; family = "arm"; version = "8"; arch = "armv8-a"; };
80 armv8r = { bits = 32; significantByte = littleEndian; family = "arm"; version = "8"; arch = "armv8-a"; };
81 armv8m = { bits = 32; significantByte = littleEndian; family = "arm"; version = "8"; arch = "armv8-m"; };
82 aarch64 = { bits = 64; significantByte = littleEndian; family = "arm"; version = "8"; arch = "armv8-a"; };
83 aarch64_be = { bits = 64; significantByte = bigEndian; family = "arm"; version = "8"; arch = "armv8-a"; };
84
85 i386 = { bits = 32; significantByte = littleEndian; family = "x86"; arch = "i386"; };
86 i486 = { bits = 32; significantByte = littleEndian; family = "x86"; arch = "i486"; };
87 i586 = { bits = 32; significantByte = littleEndian; family = "x86"; arch = "i586"; };
88 i686 = { bits = 32; significantByte = littleEndian; family = "x86"; arch = "i686"; };
89 x86_64 = { bits = 64; significantByte = littleEndian; family = "x86"; arch = "x86-64"; };
90
91 microblaze = { bits = 32; significantByte = bigEndian; family = "microblaze"; };
92 microblazeel = { bits = 32; significantByte = littleEndian; family = "microblaze"; };
93
94 mips = { bits = 32; significantByte = bigEndian; family = "mips"; };
95 mipsel = { bits = 32; significantByte = littleEndian; family = "mips"; };
96 mips64 = { bits = 64; significantByte = bigEndian; family = "mips"; };
97 mips64el = { bits = 64; significantByte = littleEndian; family = "mips"; };
98
99 mmix = { bits = 64; significantByte = bigEndian; family = "mmix"; };
100
101 m68k = { bits = 32; significantByte = bigEndian; family = "m68k"; };
102
103 powerpc = { bits = 32; significantByte = bigEndian; family = "power"; };
104 powerpc64 = { bits = 64; significantByte = bigEndian; family = "power"; };
105 powerpc64le = { bits = 64; significantByte = littleEndian; family = "power"; };
106 powerpcle = { bits = 32; significantByte = littleEndian; family = "power"; };
107
108 riscv32 = { bits = 32; significantByte = littleEndian; family = "riscv"; };
109 riscv64 = { bits = 64; significantByte = littleEndian; family = "riscv"; };
110
111 s390 = { bits = 32; significantByte = bigEndian; family = "s390"; };
112 s390x = { bits = 64; significantByte = bigEndian; family = "s390"; };
113
114 sparc = { bits = 32; significantByte = bigEndian; family = "sparc"; };
115 sparc64 = { bits = 64; significantByte = bigEndian; family = "sparc"; };
116
117 wasm32 = { bits = 32; significantByte = littleEndian; family = "wasm"; };
118 wasm64 = { bits = 64; significantByte = littleEndian; family = "wasm"; };
119
120 alpha = { bits = 64; significantByte = littleEndian; family = "alpha"; };
121
122 rx = { bits = 32; significantByte = littleEndian; family = "rx"; };
123 msp430 = { bits = 16; significantByte = littleEndian; family = "msp430"; };
124 avr = { bits = 8; family = "avr"; };
125
126 vc4 = { bits = 32; significantByte = littleEndian; family = "vc4"; };
127
128 or1k = { bits = 32; significantByte = bigEndian; family = "or1k"; };
129
130 loongarch64 = { bits = 64; significantByte = littleEndian; family = "loongarch"; };
131
132 javascript = { bits = 32; significantByte = littleEndian; family = "javascript"; };
133 };
134
135 # GNU build systems assume that older NetBSD architectures are using a.out.
136 gnuNetBSDDefaultExecFormat = cpu:
137 if (cpu.family == "arm" && cpu.bits == 32) ||
138 (cpu.family == "sparc" && cpu.bits == 32) ||
139 (cpu.family == "m68k" && cpu.bits == 32) ||
140 (cpu.family == "x86" && cpu.bits == 32)
141 then execFormats.aout
142 else execFormats.elf;
143
144 # Determine when two CPUs are compatible with each other. That is,
145 # can code built for system B run on system A? For that to happen,
146 # the programs that system B accepts must be a subset of the
147 # programs that system A accepts.
148 #
149 # We have the following properties of the compatibility relation,
150 # which must be preserved when adding compatibility information for
151 # additional CPUs.
152 # - (reflexivity)
153 # Every CPU is compatible with itself.
154 # - (transitivity)
155 # If A is compatible with B and B is compatible with C then A is compatible with C.
156 #
157 # Note: Since 22.11 the archs of a mode switching CPU are no longer considered
158 # pairwise compatible. Mode switching implies that binaries built for A
159 # and B respectively can't be executed at the same time.
160 isCompatible = a: b: with cpuTypes; lib.any lib.id [
161 # x86
162 (b == i386 && isCompatible a i486)
163 (b == i486 && isCompatible a i586)
164 (b == i586 && isCompatible a i686)
165
166 # XXX: Not true in some cases. Like in WSL mode.
167 (b == i686 && isCompatible a x86_64)
168
169 # ARMv4
170 (b == arm && isCompatible a armv5tel)
171
172 # ARMv5
173 (b == armv5tel && isCompatible a armv6l)
174
175 # ARMv6
176 (b == armv6l && isCompatible a armv6m)
177 (b == armv6m && isCompatible a armv7l)
178
179 # ARMv7
180 (b == armv7l && isCompatible a armv7a)
181 (b == armv7l && isCompatible a armv7r)
182 (b == armv7l && isCompatible a armv7m)
183
184 # ARMv8
185 (b == aarch64 && a == armv8a)
186 (b == armv8a && isCompatible a aarch64)
187 (b == armv8r && isCompatible a armv8a)
188 (b == armv8m && isCompatible a armv8a)
189
190 # PowerPC
191 (b == powerpc && isCompatible a powerpc64)
192 (b == powerpcle && isCompatible a powerpc64le)
193
194 # MIPS
195 (b == mips && isCompatible a mips64)
196 (b == mipsel && isCompatible a mips64el)
197
198 # RISCV
199 (b == riscv32 && isCompatible a riscv64)
200
201 # SPARC
202 (b == sparc && isCompatible a sparc64)
203
204 # WASM
205 (b == wasm32 && isCompatible a wasm64)
206
207 # identity
208 (b == a)
209 ];
210
211 ################################################################################
212
213 types.openVendor = mkOptionType {
214 name = "vendor";
215 description = "vendor for the platform";
216 merge = mergeOneOption;
217 };
218
219 types.vendor = enum (attrValues vendors);
220
221 vendors = setTypes types.openVendor {
222 apple = {};
223 pc = {};
224 # Actually matters, unlocking some MinGW-w64-specific options in GCC. See
225 # bottom of https://sourceforge.net/p/mingw-w64/wiki2/Unicode%20apps/
226 w64 = {};
227
228 none = {};
229 unknown = {};
230 };
231
232 ################################################################################
233
234 types.openExecFormat = mkOptionType {
235 name = "exec-format";
236 description = "executable container used by the kernel";
237 merge = mergeOneOption;
238 };
239
240 types.execFormat = enum (attrValues execFormats);
241
242 execFormats = setTypes types.openExecFormat {
243 aout = {}; # a.out
244 elf = {};
245 macho = {};
246 pe = {};
247 wasm = {};
248
249 unknown = {};
250 };
251
252 ################################################################################
253
254 types.openKernelFamily = mkOptionType {
255 name = "exec-format";
256 description = "executable container used by the kernel";
257 merge = mergeOneOption;
258 };
259
260 types.kernelFamily = enum (attrValues kernelFamilies);
261
262 kernelFamilies = setTypes types.openKernelFamily {
263 bsd = {};
264 darwin = {};
265 };
266
267 ################################################################################
268
269 types.openKernel = mkOptionType {
270 name = "kernel";
271 description = "kernel name and information";
272 merge = mergeOneOption;
273 check = x: types.execFormat.check x.execFormat
274 && all types.kernelFamily.check (attrValues x.families);
275 };
276
277 types.kernel = enum (attrValues kernels);
278
279 kernels = with execFormats; with kernelFamilies; setTypes types.openKernel {
280 # TODO(@Ericson2314): Don't want to mass-rebuild yet to keeping 'darwin' as
281 # the normalized name for macOS.
282 macos = { execFormat = macho; families = { inherit darwin; }; name = "darwin"; };
283 ios = { execFormat = macho; families = { inherit darwin; }; };
284 # A tricky thing about FreeBSD is that there is no stable ABI across
285 # versions. That means that putting in the version as part of the
286 # config string is paramount.
287 freebsd12 = { execFormat = elf; families = { inherit bsd; }; name = "freebsd"; version = 12; };
288 freebsd13 = { execFormat = elf; families = { inherit bsd; }; name = "freebsd"; version = 13; };
289 linux = { execFormat = elf; families = { }; };
290 netbsd = { execFormat = elf; families = { inherit bsd; }; };
291 none = { execFormat = unknown; families = { }; };
292 openbsd = { execFormat = elf; families = { inherit bsd; }; };
293 solaris = { execFormat = elf; families = { }; };
294 wasi = { execFormat = wasm; families = { }; };
295 redox = { execFormat = elf; families = { }; };
296 windows = { execFormat = pe; families = { }; };
297 ghcjs = { execFormat = unknown; families = { }; };
298 genode = { execFormat = elf; families = { }; };
299 mmixware = { execFormat = unknown; families = { }; };
300 } // { # aliases
301 # 'darwin' is the kernel for all of them. We choose macOS by default.
302 darwin = kernels.macos;
303 watchos = kernels.ios;
304 tvos = kernels.ios;
305 win32 = kernels.windows;
306 };
307
308 ################################################################################
309
310 types.openAbi = mkOptionType {
311 name = "abi";
312 description = "binary interface for compiled code and syscalls";
313 merge = mergeOneOption;
314 };
315
316 types.abi = enum (attrValues abis);
317
318 abis = setTypes types.openAbi {
319 cygnus = {};
320 msvc = {};
321
322 # Note: eabi is specific to ARM and PowerPC.
323 # On PowerPC, this corresponds to PPCEABI.
324 # On ARM, this corresponds to ARMEABI.
325 eabi = { float = "soft"; };
326 eabihf = { float = "hard"; };
327
328 # Other architectures should use ELF in embedded situations.
329 elf = {};
330
331 androideabi = {};
332 android = {
333 assertions = [
334 { assertion = platform: !platform.isAarch32;
335 message = ''
336 The "android" ABI is not for 32-bit ARM. Use "androideabi" instead.
337 '';
338 }
339 ];
340 };
341
342 gnueabi = { float = "soft"; };
343 gnueabihf = { float = "hard"; };
344 gnu = {
345 assertions = [
346 { assertion = platform: !platform.isAarch32;
347 message = ''
348 The "gnu" ABI is ambiguous on 32-bit ARM. Use "gnueabi" or "gnueabihf" instead.
349 '';
350 }
351 { assertion = platform: with platform; !(isPower64 && isBigEndian);
352 message = ''
353 The "gnu" ABI is ambiguous on big-endian 64-bit PowerPC. Use "gnuabielfv2" or "gnuabielfv1" instead.
354 '';
355 }
356 ];
357 };
358 gnuabi64 = { abi = "64"; };
359 muslabi64 = { abi = "64"; };
360
361 # NOTE: abi=n32 requires a 64-bit MIPS chip! That is not a typo.
362 # It is basically the 64-bit abi with 32-bit pointers. Details:
363 # https://www.linux-mips.org/pub/linux/mips/doc/ABI/MIPS-N32-ABI-Handbook.pdf
364 gnuabin32 = { abi = "n32"; };
365 muslabin32 = { abi = "n32"; };
366
367 gnuabielfv2 = { abi = "elfv2"; };
368 gnuabielfv1 = { abi = "elfv1"; };
369
370 musleabi = { float = "soft"; };
371 musleabihf = { float = "hard"; };
372 musl = {};
373
374 uclibceabi = { float = "soft"; };
375 uclibceabihf = { float = "hard"; };
376 uclibc = {};
377
378 unknown = {};
379 };
380
381 ################################################################################
382
383 types.parsedPlatform = mkOptionType {
384 name = "system";
385 description = "fully parsed representation of llvm- or nix-style platform tuple";
386 merge = mergeOneOption;
387 check = { cpu, vendor, kernel, abi }:
388 types.cpuType.check cpu
389 && types.vendor.check vendor
390 && types.kernel.check kernel
391 && types.abi.check abi;
392 };
393
394 isSystem = isType "system";
395
396 mkSystem = components:
397 assert types.parsedPlatform.check components;
398 setType "system" components;
399
400 mkSkeletonFromList = l: {
401 "1" = if elemAt l 0 == "avr"
402 then { cpu = elemAt l 0; kernel = "none"; abi = "unknown"; }
403 else throw "Target specification with 1 components is ambiguous";
404 "2" = # We only do 2-part hacks for things Nix already supports
405 if elemAt l 1 == "cygwin"
406 then { cpu = elemAt l 0; kernel = "windows"; abi = "cygnus"; }
407 # MSVC ought to be the default ABI so this case isn't needed. But then it
408 # becomes difficult to handle the gnu* variants for Aarch32 correctly for
409 # minGW. So it's easier to make gnu* the default for the MinGW, but
410 # hack-in MSVC for the non-MinGW case right here.
411 else if elemAt l 1 == "windows"
412 then { cpu = elemAt l 0; kernel = "windows"; abi = "msvc"; }
413 else if (elemAt l 1) == "elf"
414 then { cpu = elemAt l 0; vendor = "unknown"; kernel = "none"; abi = elemAt l 1; }
415 else { cpu = elemAt l 0; kernel = elemAt l 1; };
416 "3" =
417 # cpu-kernel-environment
418 if elemAt l 1 == "linux" ||
419 elem (elemAt l 2) ["eabi" "eabihf" "elf" "gnu"]
420 then {
421 cpu = elemAt l 0;
422 kernel = elemAt l 1;
423 abi = elemAt l 2;
424 vendor = "unknown";
425 }
426 # cpu-vendor-os
427 else if elemAt l 1 == "apple" ||
428 elem (elemAt l 2) [ "wasi" "redox" "mmixware" "ghcjs" "mingw32" ] ||
429 hasPrefix "freebsd" (elemAt l 2) ||
430 hasPrefix "netbsd" (elemAt l 2) ||
431 hasPrefix "genode" (elemAt l 2)
432 then {
433 cpu = elemAt l 0;
434 vendor = elemAt l 1;
435 kernel = if elemAt l 2 == "mingw32"
436 then "windows" # autotools breaks on -gnu for window
437 else elemAt l 2;
438 }
439 else throw "Target specification with 3 components is ambiguous";
440 "4" = { cpu = elemAt l 0; vendor = elemAt l 1; kernel = elemAt l 2; abi = elemAt l 3; };
441 }.${toString (length l)}
442 or (throw "system string has invalid number of hyphen-separated components");
443
444 # This should revert the job done by config.guess from the gcc compiler.
445 mkSystemFromSkeleton = { cpu
446 , # Optional, but fallback too complex for here.
447 # Inferred below instead.
448 vendor ? assert false; null
449 , kernel
450 , # Also inferred below
451 abi ? assert false; null
452 } @ args: let
453 getCpu = name: cpuTypes.${name} or (throw "Unknown CPU type: ${name}");
454 getVendor = name: vendors.${name} or (throw "Unknown vendor: ${name}");
455 getKernel = name: kernels.${name} or (throw "Unknown kernel: ${name}");
456 getAbi = name: abis.${name} or (throw "Unknown ABI: ${name}");
457
458 parsed = {
459 cpu = getCpu args.cpu;
460 vendor =
461 /**/ if args ? vendor then getVendor args.vendor
462 else if isDarwin parsed then vendors.apple
463 else if isWindows parsed then vendors.pc
464 else vendors.unknown;
465 kernel = if hasPrefix "darwin" args.kernel then getKernel "darwin"
466 else if hasPrefix "netbsd" args.kernel then getKernel "netbsd"
467 else getKernel args.kernel;
468 abi =
469 /**/ if args ? abi then getAbi args.abi
470 else if isLinux parsed || isWindows parsed then
471 if isAarch32 parsed then
472 if lib.versionAtLeast (parsed.cpu.version or "0") "6"
473 then abis.gnueabihf
474 else abis.gnueabi
475 # Default ppc64 BE to ELFv2
476 else if isPower64 parsed && isBigEndian parsed then abis.gnuabielfv2
477 else abis.gnu
478 else abis.unknown;
479 };
480
481 in mkSystem parsed;
482
483 mkSystemFromString = s: mkSystemFromSkeleton (mkSkeletonFromList (lib.splitString "-" s));
484
485 kernelName = kernel:
486 kernel.name + toString (kernel.version or "");
487
488 doubleFromSystem = { cpu, kernel, abi, ... }:
489 /**/ if abi == abis.cygnus then "${cpu.name}-cygwin"
490 else if kernel.families ? darwin then "${cpu.name}-darwin"
491 else "${cpu.name}-${kernelName kernel}";
492
493 tripleFromSystem = { cpu, vendor, kernel, abi, ... } @ sys: assert isSystem sys; let
494 optExecFormat =
495 lib.optionalString (kernel.name == "netbsd" &&
496 gnuNetBSDDefaultExecFormat cpu != kernel.execFormat)
497 kernel.execFormat.name;
498 optAbi = lib.optionalString (abi != abis.unknown) "-${abi.name}";
499 in "${cpu.name}-${vendor.name}-${kernelName kernel}${optExecFormat}${optAbi}";
500
501 ################################################################################
502
503}