@@ -4,6 +4,16 @@
44# http://llvm.org/docs/doxygen/html/Triple_8cpp_source.html especially
55# Triple::normalize. Parsing should essentially act as a more conservative
66# 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+#
717{ lib }:
818with lib.lists;
919with lib.types;
@@ -11,29 +21,52 @@ with lib.attrsets;
1121with (import ./inspect.nix { inherit lib; }).predicates;
1222
1323let
14− setTypesAssert = type: pred:
24+ inherit (lib.options) mergeOneOption;
25+
26+ setTypes = type:
1527 mapAttrs (name: value:
16− assert pred value;
17− setType type ({ inherit name; } // value));
18− setTypes = type: setTypesAssert type (_: true);
28+ assert type.check value;
29+ setType type.name ({ inherit name; } // value));
1930
2031in
2132
2233rec {
2334
24− isSignificantByte = isType "significant-byte";
25− significantBytes = setTypes "significant-byte" {
35+ ################################################################################
36+
37+ types.openSignifiantByte = mkOptionType {
38+ name = "significant-byte";
39+ description = "Endianness";
40+ merge = mergeOneOption;
41+ };
42+
43+ types.significantByte = enum (attrValues significantBytes);
44+
45+ significantBytes = setTypes types.openSignifiantByte {
2646 bigEndian = {};
2747 littleEndian = {};
2848 };
2949
30− isCpuType = isType "cpu-type";
31− cpuTypes = with significantBytes; setTypesAssert "cpu-type"
32− (x: elem x.bits [8 16 32 64 128]
33− && (if 8 < x.bits
34− then isSignificantByte x.significantByte
35− else !(x ? significantByte)))
36− {
50+ ################################################################################
51+
52+ # Reasonable power of 2
53+ types.bitWidth = enum [ 8 16 32 64 128 ];
54+
55+ ################################################################################
56+
57+ types.openCpuType = mkOptionType {
58+ name = "cpu-type";
59+ description = "instruction set architecture name and information";
60+ merge = mergeOneOption;
61+ check = x: types.bitWidth.check x.bits
62+ && (if 8 < x.bits
63+ then types.significantByte.check x.significantByte
64+ else !(x ? significantByte));
65+ };
66+
67+ types.cpuType = enum (attrValues cpuTypes);
68+
69+ cpuTypes = with significantBytes; setTypes types.openCpuType {
3770 arm = { bits = 32; significantByte = littleEndian; family = "arm"; };
3871 armv5tel = { bits = 32; significantByte = littleEndian; family = "arm"; };
3972 armv6l = { bits = 32; significantByte = littleEndian; family = "arm"; };
@@ -50,16 +83,34 @@ rec {
5083 wasm64 = { bits = 64; significantByte = littleEndian; family = "wasm"; };
5184 };
5285
53− isVendor = isType "vendor";
54− vendors = setTypes "vendor" {
86+ ################################################################################
87+
88+ types.openVendor = mkOptionType {
89+ name = "vendor";
90+ description = "vendor for the platform";
91+ merge = mergeOneOption;
92+ };
93+
94+ types.vendor = enum (attrValues vendors);
95+
96+ vendors = setTypes types.openVendor {
5597 apple = {};
5698 pc = {};
5799
58100 unknown = {};
59101 };
60102
61− isExecFormat = isType "exec-format";
62− execFormats = setTypes "exec-format" {
103+ ################################################################################
104+
105+ types.openExecFormat = mkOptionType {
106+ name = "exec-format";
107+ description = "executable container used by the kernel";
108+ merge = mergeOneOption;
109+ };
110+
111+ types.execFormat = enum (attrValues execFormats);
112+
113+ execFormats = setTypes types.openExecFormat {
63114 aout = {}; # a.out
64115 elf = {};
65116 macho = {};
@@ -68,15 +119,33 @@ rec {
68119 unknown = {};
69120 };
70121
71− isKernelFamily = isType "kernel-family";
72− kernelFamilies = setTypes "kernel-family" {
122+ ################################################################################
123+
124+ types.openKernelFamily = mkOptionType {
125+ name = "exec-format";
126+ description = "executable container used by the kernel";
127+ merge = mergeOneOption;
128+ };
129+
130+ types.kernelFamily = enum (attrValues kernelFamilies);
131+
132+ kernelFamilies = setTypes types.openKernelFamily {
73133 bsd = {};
74134 };
75135
76− isKernel = x: isType "kernel" x;
77− kernels = with execFormats; with kernelFamilies; setTypesAssert "kernel"
78− (x: isExecFormat x.execFormat && all isKernelFamily (attrValues x.families))
79− {
136+ ################################################################################
137+
138+ types.openKernel = mkOptionType {
139+ name = "kernel";
140+ description = "kernel name and information";
141+ merge = mergeOneOption;
142+ check = x: types.execFormat.check x.execFormat
143+ && all types.kernelFamily.check (attrValues x.families);
144+ };
145+
146+ types.kernel = enum (attrValues kernels);
147+
148+ kernels = with execFormats; with kernelFamilies; setTypes types.openKernel {
80149 darwin = { execFormat = macho; families = { }; };
81150 freebsd = { execFormat = elf; families = { inherit bsd; }; };
82151 hurd = { execFormat = elf; families = { }; };
@@ -93,8 +162,17 @@ rec {
93162 win32 = kernels.windows;
94163 };
95164
96− isAbi = isType "abi";
97− abis = setTypes "abi" {
165+ ################################################################################
166+
167+ types.openAbi = mkOptionType {
168+ name = "abi";
169+ description = "binary interface for compiled code and syscalls";
170+ merge = mergeOneOption;
171+ };
172+
173+ types.abi = enum (attrValues abis);
174+
175+ abis = setTypes types.openAbi {
98176 cygnus = {};
99177 gnu = {};
100178 msvc = {};
@@ -106,12 +184,24 @@ rec {
106184 unknown = {};
107185 };
108186
187+ ################################################################################
188+
189+ types.system = mkOptionType {
190+ name = "system";
191+ description = "fully parsed representation of llvm- or nix-style platform tuple";
192+ merge = mergeOneOption;
193+ check = { cpu, vendor, kernel, abi }:
194+ types.cpuType.check cpu
195+ && types.vendor.check vendor
196+ && types.kernel.check kernel
197+ && types.abi.check abi;
198+ };
199+
109200 isSystem = isType "system";
110− mkSystem = { cpu, vendor, kernel, abi }:
111− assert isCpuType cpu && isVendor vendor && isKernel kernel && isAbi abi;
112− setType "system" {
113− inherit cpu vendor kernel abi;
114− };
201+
202+ mkSystem = components:
203+ assert types.system.check components;
204+ setType "system" components;
115205
116206 mkSkeletonFromList = l: {
117207 "2" = # We only do 2-part hacks for things Nix already supports
@@ -174,4 +264,6 @@ rec {
174264 optAbi = lib.optionalString (abi != abis.unknown) "-${abi.name}";
175265 in "${cpu.name}-${vendor.name}-${kernel.name}${optAbi}";
176266
267+ ################################################################################
268+
177269}