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