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