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