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"; };
73 armv6m = { bits = 32; significantByte = littleEndian; family = "arm"; version = "6"; };
74 armv6l = { bits = 32; significantByte = littleEndian; family = "arm"; version = "6"; };
75 armv7a = { bits = 32; significantByte = littleEndian; family = "arm"; version = "7"; };
76 armv7r = { bits = 32; significantByte = littleEndian; family = "arm"; version = "7"; };
77 armv7m = { bits = 32; significantByte = littleEndian; family = "arm"; version = "7"; };
78 armv7l = { bits = 32; significantByte = littleEndian; family = "arm"; version = "7"; };
79 armv8a = { bits = 32; significantByte = littleEndian; family = "arm"; version = "8"; };
80 armv8r = { bits = 32; significantByte = littleEndian; family = "arm"; version = "8"; };
81 armv8m = { bits = 32; significantByte = littleEndian; family = "arm"; version = "8"; };
82 aarch64 = { bits = 64; significantByte = littleEndian; family = "arm"; version = "8"; };
83 aarch64_be = { bits = 64; significantByte = bigEndian; family = "arm"; version = "8"; };
84
85 i386 = { bits = 32; significantByte = littleEndian; family = "x86"; };
86 i486 = { bits = 32; significantByte = littleEndian; family = "x86"; };
87 i586 = { bits = 32; significantByte = littleEndian; family = "x86"; };
88 i686 = { bits = 32; significantByte = littleEndian; family = "x86"; };
89 x86_64 = { bits = 64; significantByte = littleEndian; family = "x86"; };
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 avr = { bits = 8; family = "avr"; };
113
114 js = { bits = 32; significantByte = littleEndian; family = "js"; };
115 };
116
117 ################################################################################
118
119 types.openVendor = mkOptionType {
120 name = "vendor";
121 description = "vendor for the platform";
122 merge = mergeOneOption;
123 };
124
125 types.vendor = enum (attrValues vendors);
126
127 vendors = setTypes types.openVendor {
128 apple = {};
129 pc = {};
130
131 none = {};
132 unknown = {};
133 };
134
135 ################################################################################
136
137 types.openExecFormat = mkOptionType {
138 name = "exec-format";
139 description = "executable container used by the kernel";
140 merge = mergeOneOption;
141 };
142
143 types.execFormat = enum (attrValues execFormats);
144
145 execFormats = setTypes types.openExecFormat {
146 aout = {}; # a.out
147 elf = {};
148 macho = {};
149 pe = {};
150
151 unknown = {};
152 };
153
154 ################################################################################
155
156 types.openKernelFamily = mkOptionType {
157 name = "exec-format";
158 description = "executable container used by the kernel";
159 merge = mergeOneOption;
160 };
161
162 types.kernelFamily = enum (attrValues kernelFamilies);
163
164 kernelFamilies = setTypes types.openKernelFamily {
165 bsd = {};
166 darwin = {};
167 };
168
169 ################################################################################
170
171 types.openKernel = mkOptionType {
172 name = "kernel";
173 description = "kernel name and information";
174 merge = mergeOneOption;
175 check = x: types.execFormat.check x.execFormat
176 && all types.kernelFamily.check (attrValues x.families);
177 };
178
179 types.kernel = enum (attrValues kernels);
180
181 kernels = with execFormats; with kernelFamilies; setTypes types.openKernel {
182 # TODO(@Ericson2314): Don't want to mass-rebuild yet to keeping 'darwin' as
183 # the nnormalized name for macOS.
184 macos = { execFormat = macho; families = { inherit darwin; }; name = "darwin"; };
185 ios = { execFormat = macho; families = { inherit darwin; }; };
186 freebsd = { execFormat = elf; families = { inherit bsd; }; };
187 linux = { execFormat = elf; families = { }; };
188 netbsd = { execFormat = elf; families = { inherit bsd; }; };
189 none = { execFormat = unknown; families = { }; };
190 openbsd = { execFormat = elf; families = { inherit bsd; }; };
191 solaris = { execFormat = elf; families = { }; };
192 windows = { execFormat = pe; families = { }; };
193 ghcjs = { execFormat = unknown; families = { }; };
194 } // { # aliases
195 # 'darwin' is the kernel for all of them. We choose macOS by default.
196 darwin = kernels.macos;
197 watchos = kernels.ios;
198 tvos = kernels.ios;
199 win32 = kernels.windows;
200 };
201
202 ################################################################################
203
204 types.openAbi = mkOptionType {
205 name = "abi";
206 description = "binary interface for compiled code and syscalls";
207 merge = mergeOneOption;
208 };
209
210 types.abi = enum (attrValues abis);
211
212 abis = setTypes types.openAbi {
213 cygnus = {};
214 msvc = {};
215
216 # Note: eabi is specific to ARM and PowerPC.
217 # On PowerPC, this corresponds to PPCEABI.
218 # On ARM, this corresponds to ARMEABI.
219 eabi = { float = "soft"; };
220 eabihf = { float = "hard"; };
221
222 # Other architectures should use ELF in embedded situations.
223 elf = {};
224
225 androideabi = {};
226 android = {
227 assertions = [
228 { assertion = platform: !platform.isAarch32;
229 message = ''
230 The "android" ABI is not for 32-bit ARM. Use "androideabi" instead.
231 '';
232 }
233 ];
234 };
235
236 gnueabi = { float = "soft"; };
237 gnueabihf = { float = "hard"; };
238 gnu = {
239 assertions = [
240 { assertion = platform: !platform.isAarch32;
241 message = ''
242 The "gnu" ABI is ambiguous on 32-bit ARM. Use "gnueabi" or "gnueabihf" instead.
243 '';
244 }
245 ];
246 };
247
248 musleabi = { float = "soft"; };
249 musleabihf = { float = "hard"; };
250 musl = {};
251
252 uclibceabihf = { float = "soft"; };
253 uclibceabi = { float = "hard"; };
254 uclibc = {};
255
256 unknown = {};
257 };
258
259 ################################################################################
260
261 types.parsedPlatform = mkOptionType {
262 name = "system";
263 description = "fully parsed representation of llvm- or nix-style platform tuple";
264 merge = mergeOneOption;
265 check = { cpu, vendor, kernel, abi }:
266 types.cpuType.check cpu
267 && types.vendor.check vendor
268 && types.kernel.check kernel
269 && types.abi.check abi;
270 };
271
272 isSystem = isType "system";
273
274 mkSystem = components:
275 assert types.parsedPlatform.check components;
276 setType "system" components;
277
278 mkSkeletonFromList = l: {
279 "1" = if elemAt l 0 == "avr"
280 then { cpu = elemAt l 0; kernel = "none"; abi = "unknown"; }
281 else throw "Target specification with 1 components is ambiguous";
282 "2" = # We only do 2-part hacks for things Nix already supports
283 if elemAt l 1 == "cygwin"
284 then { cpu = elemAt l 0; kernel = "windows"; abi = "cygnus"; }
285 # MSVC ought to be the default ABI so this case isn't needed. But then it
286 # becomes difficult to handle the gnu* variants for Aarch32 correctly for
287 # minGW. So it's easier to make gnu* the default for the MinGW, but
288 # hack-in MSVC for the non-MinGW case right here.
289 else if elemAt l 1 == "windows"
290 then { cpu = elemAt l 0; kernel = "windows"; abi = "msvc"; }
291 else if (elemAt l 1) == "elf"
292 then { cpu = elemAt l 0; vendor = "unknown"; kernel = "none"; abi = elemAt l 1; }
293 else { cpu = elemAt l 0; kernel = elemAt l 1; };
294 "3" = # Awkwards hacks, beware!
295 if elemAt l 1 == "apple"
296 then { cpu = elemAt l 0; vendor = "apple"; kernel = elemAt l 2; }
297 else if (elemAt l 1 == "linux") || (elemAt l 2 == "gnu")
298 then { cpu = elemAt l 0; kernel = elemAt l 1; abi = elemAt l 2; }
299 else if (elemAt l 2 == "mingw32") # autotools breaks on -gnu for window
300 then { cpu = elemAt l 0; vendor = elemAt l 1; kernel = "windows"; }
301 else if hasPrefix "netbsd" (elemAt l 2)
302 then { cpu = elemAt l 0; vendor = elemAt l 1; kernel = elemAt l 2; }
303 else if (elem (elemAt l 2) ["eabi" "eabihf" "elf"])
304 then { cpu = elemAt l 0; vendor = "unknown"; kernel = elemAt l 1; abi = elemAt l 2; }
305 else if (elemAt l 2 == "ghcjs")
306 then { cpu = elemAt l 0; vendor = "unknown"; kernel = elemAt l 2; }
307 else throw "Target specification with 3 components is ambiguous";
308 "4" = { cpu = elemAt l 0; vendor = elemAt l 1; kernel = elemAt l 2; abi = elemAt l 3; };
309 }.${toString (length l)}
310 or (throw "system string has invalid number of hyphen-separated components");
311
312 # This should revert the job done by config.guess from the gcc compiler.
313 mkSystemFromSkeleton = { cpu
314 , # Optional, but fallback too complex for here.
315 # Inferred below instead.
316 vendor ? assert false; null
317 , kernel
318 , # Also inferred below
319 abi ? assert false; null
320 } @ args: let
321 getCpu = name: cpuTypes.${name} or (throw "Unknown CPU type: ${name}");
322 getVendor = name: vendors.${name} or (throw "Unknown vendor: ${name}");
323 getKernel = name: kernels.${name} or (throw "Unknown kernel: ${name}");
324 getAbi = name: abis.${name} or (throw "Unknown ABI: ${name}");
325
326 parsed = rec {
327 cpu = getCpu args.cpu;
328 vendor =
329 /**/ if args ? vendor then getVendor args.vendor
330 else if isDarwin parsed then vendors.apple
331 else if isWindows parsed then vendors.pc
332 else vendors.unknown;
333 kernel = if hasPrefix "darwin" args.kernel then getKernel "darwin"
334 else if hasPrefix "netbsd" args.kernel then getKernel "netbsd"
335 else getKernel args.kernel;
336 abi =
337 /**/ if args ? abi then getAbi args.abi
338 else if isLinux parsed || isWindows parsed then
339 if isAarch32 parsed then
340 if lib.versionAtLeast (parsed.cpu.version or "0") "6"
341 then abis.gnueabihf
342 else abis.gnueabi
343 else abis.gnu
344 else abis.unknown;
345 };
346
347 in mkSystem parsed;
348
349 mkSystemFromString = s: mkSystemFromSkeleton (mkSkeletonFromList (lib.splitString "-" s));
350
351 doubleFromSystem = { cpu, vendor, kernel, abi, ... }:
352 /**/ if abi == abis.cygnus then "${cpu.name}-cygwin"
353 else if kernel.families ? darwin then "${cpu.name}-darwin"
354 else "${cpu.name}-${kernel.name}";
355
356 tripleFromSystem = { cpu, vendor, kernel, abi, ... } @ sys: assert isSystem sys; let
357 optAbi = lib.optionalString (abi != abis.unknown) "-${abi.name}";
358 in "${cpu.name}-${vendor.name}-${kernel.name}${optAbi}";
359
360 ################################################################################
361
362}