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