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