summary refs log tree commit diff
path: root/lib/systems/parse.nix
diff options
context:
space:
mode:
authorJohn Ericson <Ericson2314@Yahoo.com>2017-02-08 21:27:22 -0500
committerJohn Ericson <Ericson2314@Yahoo.com>2017-04-17 17:13:01 -0400
commit22277893923cdf26004d83b608b7e1c3ca7030fb (patch)
treeacfb682574364c067efd5916caa67b1a0d431955 /lib/systems/parse.nix
parentb477851f3427e3353d530ab0d47b63fe2686dd4b (diff)
downloadnixlib-22277893923cdf26004d83b608b7e1c3ca7030fb.tar
nixlib-22277893923cdf26004d83b608b7e1c3ca7030fb.tar.gz
nixlib-22277893923cdf26004d83b608b7e1c3ca7030fb.tar.bz2
nixlib-22277893923cdf26004d83b608b7e1c3ca7030fb.tar.lz
nixlib-22277893923cdf26004d83b608b7e1c3ca7030fb.tar.xz
nixlib-22277893923cdf26004d83b608b7e1c3ca7030fb.tar.zst
nixlib-22277893923cdf26004d83b608b7e1c3ca7030fb.zip
lib: Collect system/platform related files
Previously, platforms was a random thing in top-level
Diffstat (limited to 'lib/systems/parse.nix')
-rw-r--r--lib/systems/parse.nix126
1 files changed, 126 insertions, 0 deletions
diff --git a/lib/systems/parse.nix b/lib/systems/parse.nix
new file mode 100644
index 000000000000..410995f8a5d7
--- /dev/null
+++ b/lib/systems/parse.nix
@@ -0,0 +1,126 @@
+# Define the list of system with their properties.  Only systems tested for
+# Nixpkgs are listed below
+
+with import ../lists.nix;
+with import ../types.nix;
+with import ../attrsets.nix;
+
+let
+  lib = import ./default.nix;
+  setTypes = type:
+    mapAttrs (name: value:
+      setType type ({inherit name;} // value)
+    );
+in
+
+rec {
+
+  isSignificantByte = isType "significant-byte";
+  significantBytes = setTypes "significant-byte" {
+    bigEndian = {};
+    littleEndian = {};
+  };
+
+
+  isCpuType = x: isType "cpu-type" x
+    && elem x.bits [8 16 32 64 128]
+    && (8 < x.bits -> isSignificantByte x.significantByte);
+
+  cpuTypes = with significantBytes;
+    setTypes "cpu-type" {
+      arm =      { bits = 32; significantByte = littleEndian; };
+      armv5tel = { bits = 32; significantByte = littleEndian; };
+      armv7l   = { bits = 32; significantByte = littleEndian; };
+      i686 =     { bits = 32; significantByte = littleEndian; };
+      powerpc =  { bits = 32; significantByte = bigEndian; };
+      x86_64 =   { bits = 64; significantByte = littleEndian; };
+    };
+
+
+  isExecFormat = isType "exec-format";
+  execFormats = setTypes "exec-format" {
+    aout = {}; # a.out
+    elf = {};
+    macho = {};
+    pe = {};
+    unknow = {};
+  };
+
+
+  isKernel = isType "kernel";
+  kernels = with execFormats;
+    setTypes "kernel" {
+      cygwin =  { execFormat = pe; };
+      darwin =  { execFormat = macho; };
+      freebsd = { execFormat = elf; };
+      linux =   { execFormat = elf; };
+      netbsd =  { execFormat = elf; };
+      none =    { execFormat = unknow; };
+      openbsd = { execFormat = elf; };
+      win32 =   { execFormat = pe; };
+    };
+
+
+  isArchitecture = isType "architecture";
+  architectures = setTypes "architecture" {
+    apple = {};
+    pc = {};
+    unknow = {};
+  };
+
+
+  isSystem = x: isType "system" x
+    && isCpuType x.cpu
+    && isArchitecture x.arch
+    && isKernel x.kernel;
+
+  mkSystem = {
+    cpu ? cpuTypes.i686,
+    arch ? architectures.pc,
+    kernel ? kernels.linux,
+    name ? "${cpu.name}-${arch.name}-${kernel.name}"
+  }: setType "system" {
+    inherit name cpu arch kernel;
+  };
+
+
+  is64Bit = matchAttrs { cpu = { bits = 64; }; };
+  isDarwin = matchAttrs { kernel = kernels.darwin; };
+  isi686 = matchAttrs { cpu = cpuTypes.i686; };
+  isLinux = matchAttrs { kernel = kernels.linux; };
+
+
+  # This should revert the job done by config.guess from the gcc compiler.
+  mkSystemFromString = s: let
+    l = lib.splitString "-" s;
+
+    getCpu = name:
+      attrByPath [name] (throw "Unknow cpuType `${name}'.")
+        cpuTypes;
+    getArch = name:
+      attrByPath [name] (throw "Unknow architecture `${name}'.")
+        architectures;
+    getKernel = name:
+      attrByPath [name] (throw "Unknow kernel `${name}'.")
+        kernels;
+
+    system =
+      if builtins.length l == 2 then
+        mkSystem rec {
+          name = s;
+          cpu = getCpu (head l);
+          arch =
+            if isDarwin system
+            then architectures.apple
+            else architectures.pc;
+          kernel = getKernel (head (tail l));
+        }
+      else
+        mkSystem {
+          name = s;
+          cpu = getCpu (head l);
+          arch = getArch (head (tail l));
+          kernel = getKernel (head (tail (tail l)));
+        };
+  in assert isSystem system; system;
+}