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