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