Thomas Dy 2 лет назад
Сommit
453d51309d
1 измененных файлов с 472 добавлено и 0 удалено
  1. 472 0
      stage0-posix.nix

+ 472 - 0
stage0-posix.nix

@@ -0,0 +1,472 @@
+let
+  system = "x86_64-linux";
+
+  ARCH_DIR = "AMD64";
+  ARCH = "amd64";
+
+  stage0-src = builtins.fetchGit {
+    url = "https://github.com/oriansj/stage0-posix.git";
+    rev = "74f1e36f35c28d7854eddfde963917c404764ede";
+    submodules = true;
+  };
+
+  arch-src = "${stage0-src}/${ARCH_DIR}";
+
+  out = builtins.placeholder "out";
+
+  run = name: args: derivation {
+    inherit name system;
+
+    builder = builtins.head args;
+    args = builtins.tail args;
+  };
+
+  hex0 = run "hex0" [
+    "${stage0-src}/bootstrap-seeds/POSIX/${ARCH_DIR}/hex0-seed"
+    "${arch-src}/hex0_AMD64.hex0"
+    out
+  ];
+
+  hex1 = run "hex1" [
+    hex0
+    "${arch-src}/hex1_AMD64.hex0"
+    out
+  ];
+
+  catm = run "catm" [
+    hex0
+    "${arch-src}/catm_AMD64.hex0"
+    out
+  ];
+
+  hex2-0 = run "hex2-0" [
+    hex1
+    "${arch-src}/hex2_AMD64.hex1"
+    out
+  ];
+
+  arch-elf-hex2 = "${arch-src}/ELF-amd64.hex2";
+
+  cat = name: args: run name ([
+    catm
+    out
+  ] ++ args);
+
+  buildHex2 = name: elf: src: run name [
+    hex2-0
+    (cat "${name}-src" [ elf src ])
+    out
+  ];
+
+  M0 = buildHex2 "M0" arch-elf-hex2 "${arch-src}/M0_AMD64.hex2";
+
+  buildM0 = name: elf: src: buildHex2 name elf (run "${name}-m0" [
+    M0
+    src
+    out
+  ]);
+
+  cc_amd64 = buildM0 "cc_amd64" arch-elf-hex2 "${arch-src}/cc_amd64.M1";
+
+  M2 =
+    let
+      M2-0-c = cat "M2-0.c" [
+        "${stage0-src}/M2libc/amd64/linux/bootstrap.c"
+        "${stage0-src}/M2-Planet/cc.h"
+        "${stage0-src}/M2libc/bootstrappable.c"
+        "${stage0-src}/M2-Planet/cc_globals.c"
+        "${stage0-src}/M2-Planet/cc_reader.c"
+        "${stage0-src}/M2-Planet/cc_strings.c"
+        "${stage0-src}/M2-Planet/cc_types.c"
+        "${stage0-src}/M2-Planet/cc_core.c"
+        "${stage0-src}/M2-Planet/cc_macro.c"
+        "${stage0-src}/M2-Planet/cc.c"
+      ];
+      M2-0-M1 = run "M2-0.M1" [
+        cc_amd64
+        M2-0-c
+        out
+      ];
+      M2-0-0-M1 = cat "M2-0-0.m1" [
+        "${arch-src}/amd64_defs.M1"
+        "${arch-src}/libc-core.M1"
+        M2-0-M1
+      ];
+    in
+    buildM0 "M2" arch-elf-hex2 M2-0-0-M1;
+
+  libc-elf-hex2 = "${stage0-src}/M2libc/amd64/ELF-amd64.hex2";
+  libc-elf-debug-hex2 = "${stage0-src}/M2libc/amd64/ELF-amd64-debug.hex2";
+
+  blood-elf-0 =
+    let
+      blood-elf-0-M1 = run "blood-elf-0.M1" [
+        M2
+        "--architecture" "amd64"
+        "-f" "${stage0-src}/M2libc/amd64/linux/bootstrap.c"
+        "-f" "${stage0-src}/M2libc/bootstrappable.c"
+        "-f" "${stage0-src}/mescc-tools/stringify.c"
+        "-f" "${stage0-src}/mescc-tools/blood-elf.c"
+        "--bootstrap-mode"
+        "-o" out
+      ];
+      blood-elf-0-0-M1 = cat "blood-elf-0-0.M1" [
+        "${arch-src}/amd64_defs.M1"
+        "${arch-src}/libc-core.M1"
+        blood-elf-0-M1
+      ];
+    in
+    buildM0 "blood-elf-0" libc-elf-hex2 blood-elf-0-0-M1;
+
+  runBloodelf = blood-elf: name: src: run name [
+    blood-elf
+    "--64" "--little-endian"
+    "-f" src
+    "-o" out
+  ];
+
+  M1-0 =
+    let
+      M1-macro-0-M1 = run "M1-macro-0.M1" [
+        M2
+        "--architecture" "amd64"
+        "-f" "${stage0-src}/M2libc/amd64/linux/bootstrap.c"
+        "-f" "${stage0-src}/M2libc/bootstrappable.c"
+        "-f" "${stage0-src}/mescc-tools/stringify.c"
+        "-f" "${stage0-src}/mescc-tools/M1-macro.c"
+        "--bootstrap-mode"
+        "--debug"
+        "-o" out
+      ];
+      M1-macro-0-footer-M1 = runBloodelf blood-elf-0 "M1-macro-0-footer.M1" M1-macro-0-M1;
+      M1-macro-0-0-M1 = cat "M1-macro-0-0.M1" [
+        "${arch-src}/amd64_defs.M1"
+        "${arch-src}/libc-core.M1"
+        M1-macro-0-M1
+        M1-macro-0-footer-M1
+      ];
+    in
+    buildM0 "M1-0" libc-elf-debug-hex2 M1-macro-0-0-M1;
+
+  hex2-1 =
+    let
+      hex2_linker-1-M1 = run "hex2_linker-1.M1" [
+        M2
+        "--architecture" "amd64"
+        "-f" "${stage0-src}/M2libc/sys/types.h"
+        "-f" "${stage0-src}/M2libc/stddef.h"
+        "-f" "${stage0-src}/M2libc/amd64/linux/fcntl.c"
+        "-f" "${stage0-src}/M2libc/amd64/linux/unistd.c"
+        "-f" "${stage0-src}/M2libc/amd64/linux/sys/stat.c"
+        "-f" "${stage0-src}/M2libc/stdlib.c"
+        "-f" "${stage0-src}/M2libc/stdio.c"
+        "-f" "${stage0-src}/M2libc/bootstrappable.c"
+        "-f" "${stage0-src}/mescc-tools/hex2.h"
+        "-f" "${stage0-src}/mescc-tools/hex2_linker.c"
+        "-f" "${stage0-src}/mescc-tools/hex2_word.c"
+        "-f" "${stage0-src}/mescc-tools/hex2.c"
+        "--debug"
+        "-o" out
+      ];
+      hex2_linker-1-footer-M1 = runBloodelf blood-elf-0 "hex2_linker-1-footer.M1" hex2_linker-1-M1;
+      hex2_linker-1-hex2 = run "hex2_linker-1.hex2" [
+        M1-0
+        "--architecture" "amd64"
+        "--little-endian"
+        "-f" "${stage0-src}/M2libc/amd64/amd64_defs.M1"
+        "-f" "${stage0-src}/M2libc/amd64/libc-full.M1"
+        "-f" hex2_linker-1-M1
+        "-f" hex2_linker-1-footer-M1
+        "-o" out
+      ];
+    in
+    buildHex2 "hex2-1" libc-elf-debug-hex2 hex2_linker-1-hex2;
+
+  buildM1 = deps: name: srcs:
+    let
+      M1-src = run "${name}.M1" (
+        [ M2 "--architecture" "amd64" ]
+        ++ (builtins.concatMap (x: [ "-f" x ]) srcs)
+        ++ [ "--debug" "-o" out ]
+      );
+      M1-footer-src = runBloodelf deps.blood-elf "${name}-footer.M1" M1-src;
+      hex2-src = run "${name}.hex2" [
+        deps.M1
+        "--architecture" "amd64"
+        "--little-endian"
+        "-f" "${stage0-src}/M2libc/amd64/amd64_defs.M1"
+        "-f" "${stage0-src}/M2libc/amd64/libc-full.M1"
+        "-f" M1-src
+        "-f" M1-footer-src
+        "-o" out
+      ];
+    in
+    run name [
+      deps.hex2
+      "--architecture" "amd64"
+      "--little-endian"
+      "--base-address" "0x00600000"
+      "-f" libc-elf-debug-hex2
+      "-f" hex2-src
+      "-o" out
+    ];
+
+  M1 = buildM1 {
+    M1 = M1-0;
+    hex2 = hex2-1;
+    blood-elf = blood-elf-0;
+  } "M1" [
+    "${stage0-src}/M2libc/sys/types.h"
+    "${stage0-src}/M2libc/stddef.h"
+    "${stage0-src}/M2libc/amd64/linux/fcntl.c"
+    "${stage0-src}/M2libc/amd64/linux/unistd.c"
+    "${stage0-src}/M2libc/string.c"
+    "${stage0-src}/M2libc/stdlib.c"
+    "${stage0-src}/M2libc/stdio.c"
+    "${stage0-src}/M2libc/bootstrappable.c"
+    "${stage0-src}/mescc-tools/stringify.c"
+    "${stage0-src}/mescc-tools/M1-macro.c"
+  ];
+
+  hex2 = buildM1 {
+    inherit M1;
+    hex2 = hex2-1;
+    blood-elf = blood-elf-0;
+  } "hex2" [
+    "${stage0-src}/M2libc/sys/types.h"
+    "${stage0-src}/M2libc/stddef.h"
+    "${stage0-src}/M2libc/amd64/linux/fcntl.c"
+    "${stage0-src}/M2libc/amd64/linux/unistd.c"
+    "${stage0-src}/M2libc/amd64/linux/sys/stat.c"
+    "${stage0-src}/M2libc/stdlib.c"
+    "${stage0-src}/M2libc/stdio.c"
+    "${stage0-src}/M2libc/bootstrappable.c"
+    "${stage0-src}/mescc-tools/hex2.h"
+    "${stage0-src}/mescc-tools/hex2_linker.c"
+    "${stage0-src}/mescc-tools/hex2_word.c"
+    "${stage0-src}/mescc-tools/hex2.c"
+  ];
+
+  buildPhase11 = buildM1 {
+    inherit M1 hex2;
+    blood-elf = blood-elf-0;
+  };
+
+  kaem = buildPhase11 "kaem" [
+    "${stage0-src}/M2libc/sys/types.h"
+    "${stage0-src}/M2libc/stddef.h"
+    "${stage0-src}/M2libc/string.c"
+    "${stage0-src}/M2libc/amd64/linux/fcntl.c"
+    "${stage0-src}/M2libc/amd64/linux/unistd.c"
+    "${stage0-src}/M2libc/stdlib.c"
+    "${stage0-src}/M2libc/stdio.c"
+    "${stage0-src}/M2libc/bootstrappable.c"
+    "${stage0-src}/mescc-tools/Kaem/kaem.h"
+    "${stage0-src}/mescc-tools/Kaem/variable.c"
+    "${stage0-src}/mescc-tools/Kaem/kaem_globals.c"
+    "${stage0-src}/mescc-tools/Kaem/kaem.c"
+  ];
+
+  M2-Mesoplanet = buildPhase11 "M2-Mesoplanet" [
+    "${stage0-src}/M2libc/sys/types.h"
+    "${stage0-src}/M2libc/stddef.h"
+    "${stage0-src}/M2libc/${ARCH}/linux/fcntl.c"
+    "${stage0-src}/M2libc/${ARCH}/linux/unistd.c"
+    "${stage0-src}/M2libc/${ARCH}/linux/sys/stat.c"
+    "${stage0-src}/M2libc/stdlib.c"
+    "${stage0-src}/M2libc/stdio.c"
+    "${stage0-src}/M2libc/string.c"
+    "${stage0-src}/M2libc/bootstrappable.c"
+    "${stage0-src}/M2-Mesoplanet/cc.h"
+	"${stage0-src}/M2-Mesoplanet/cc_globals.c"
+	"${stage0-src}/M2-Mesoplanet/cc_env.c"
+	"${stage0-src}/M2-Mesoplanet/cc_reader.c"
+	"${stage0-src}/M2-Mesoplanet/cc_spawn.c"
+	"${stage0-src}/M2-Mesoplanet/cc_core.c"
+	"${stage0-src}/M2-Mesoplanet/cc_macro.c"
+	"${stage0-src}/M2-Mesoplanet/cc.c"
+  ];
+
+  blood-elf = buildPhase11 "blood-elf" [
+    "${stage0-src}/M2libc/sys/types.h"
+    "${stage0-src}/M2libc/stddef.h"
+    "${stage0-src}/M2libc/${ARCH}/linux/fcntl.c"
+    "${stage0-src}/M2libc/${ARCH}/linux/unistd.c"
+    "${stage0-src}/M2libc/stdlib.c"
+    "${stage0-src}/M2libc/stdio.c"
+    "${stage0-src}/M2libc/bootstrappable.c"
+    "${stage0-src}/mescc-tools/stringify.c"
+	"${stage0-src}/mescc-tools/blood-elf.c"
+  ];
+
+  buildPhase14 = buildM1 {
+    inherit M1 hex2 blood-elf;
+  };
+
+  get_machine = buildPhase14 "get_machine" [
+    "${stage0-src}/M2libc/sys/types.h"
+    "${stage0-src}/M2libc/stddef.h"
+    "${stage0-src}/M2libc/${ARCH}/linux/fcntl.c"
+    "${stage0-src}/M2libc/${ARCH}/linux/unistd.c"
+    "${stage0-src}/M2libc/stdlib.c"
+    "${stage0-src}/M2libc/stdio.c"
+    "${stage0-src}/M2libc/bootstrappable.c"
+    "${stage0-src}/mescc-tools/get_machine.c"
+  ];
+
+  M2-Planet = buildPhase14 "M2-Planet" [
+    "${stage0-src}/M2libc/sys/types.h"
+    "${stage0-src}/M2libc/stddef.h"
+    "${stage0-src}/M2libc/${ARCH}/linux/fcntl.c"
+    "${stage0-src}/M2libc/${ARCH}/linux/unistd.c"
+    "${stage0-src}/M2libc/stdlib.c"
+    "${stage0-src}/M2libc/stdio.c"
+    "${stage0-src}/M2libc/bootstrappable.c"
+    "${stage0-src}/M2-Planet/cc.h"
+    "${stage0-src}/M2-Planet/cc_globals.c"
+    "${stage0-src}/M2-Planet/cc_reader.c"
+    "${stage0-src}/M2-Planet/cc_strings.c"
+    "${stage0-src}/M2-Planet/cc_types.c"
+    "${stage0-src}/M2-Planet/cc_core.c"
+    "${stage0-src}/M2-Planet/cc_macro.c"
+    "${stage0-src}/M2-Planet/cc.c"
+  ];
+
+  # after this point, we rely on PATH so we have to build up a bin dir that we
+  # can use for that so we need mkdir, chmod and cp
+  mkdir0 = buildPhase14 "mkdir0" [
+    "${stage0-src}/M2libc/sys/types.h"
+    "${stage0-src}/M2libc/stddef.h"
+    "${stage0-src}/M2libc/string.c"
+    "${stage0-src}/M2libc/amd64/linux/fcntl.c"
+    "${stage0-src}/M2libc/amd64/linux/unistd.c"
+    "${stage0-src}/M2libc/amd64/linux/sys/stat.c"
+    "${stage0-src}/M2libc/stdlib.c"
+    "${stage0-src}/M2libc/stdio.c"
+    "${stage0-src}/M2libc/bootstrappable.c"
+    "${stage0-src}/mescc-tools-extra/mkdir.c"
+  ];
+
+  chmod0 = buildPhase14 "chmod0" [
+    "${stage0-src}/M2libc/sys/types.h"
+    "${stage0-src}/M2libc/stddef.h"
+    "${stage0-src}/M2libc/string.c"
+    "${stage0-src}/M2libc/amd64/linux/fcntl.c"
+    "${stage0-src}/M2libc/amd64/linux/unistd.c"
+    "${stage0-src}/M2libc/amd64/linux/sys/stat.c"
+    "${stage0-src}/M2libc/stdlib.c"
+    "${stage0-src}/M2libc/stdio.c"
+    "${stage0-src}/M2libc/bootstrappable.c"
+    "${stage0-src}/mescc-tools-extra/chmod.c"
+  ];
+
+  cp0 = buildPhase14 "cp0" [
+    "${stage0-src}/M2libc/sys/types.h"
+    "${stage0-src}/M2libc/stddef.h"
+    "${stage0-src}/M2libc/string.c"
+    "${stage0-src}/M2libc/amd64/linux/fcntl.c"
+    "${stage0-src}/M2libc/amd64/linux/unistd.c"
+    "${stage0-src}/M2libc/stdlib.c"
+    "${stage0-src}/M2libc/stdio.c"
+    "${stage0-src}/M2libc/bootstrappable.c"
+    "${stage0-src}/mescc-tools-extra/cp.c"
+  ];
+
+  makeBin = name: attrs:
+    let
+      getEnv = builtins.replaceStrings ["-"] [""];
+      script = map
+        (x: ''
+          ''${cp0} ''${${getEnv x}} ''${out}/bin/${x}
+          ''${chmod0} 0700 ''${out}/bin/${x}
+        '')
+        (builtins.attrNames attrs);
+
+      env = builtins.listToAttrs (
+        map
+          (x: { name = (getEnv x); value = attrs."${x}"; })
+          (builtins.attrNames attrs));
+    in
+    derivation (env // {
+      inherit system;
+      name = "buildtools0";
+
+      inherit mkdir0 chmod0 cp0;
+
+      builder = kaem;
+
+      args = ["--file" (builtins.toFile "builder.kaem" ''
+        ''${mkdir0} -p ''${out}/bin
+        ${builtins.concatStringsSep "\n" script}
+      '')];
+    });
+
+  buildtools0 = makeBin "buildtools0" {
+    inherit
+      kaem
+      hex2
+      blood-elf
+      get_machine
+      M1
+      M2-Planet
+      M2-Mesoplanet
+      ;
+  };
+
+  buildWithC = name: attrs: script: derivation (attrs // {
+    inherit system name;
+
+    src = stage0-src;
+    mkdir = mkdir0;
+
+    M2LIBC_PATH = "${stage0-src}/M2libc";
+    PATH = "${buildtools0}/bin";
+
+    builder = kaem;
+
+    args = ["--file" (builtins.toFile "builder.kaem" script)];
+  });
+
+  mescc-tools-extra = buildWithC "mescc-tools-extra" {} ''
+    set -ex
+    alias CC="M2-Mesoplanet --architecture ${ARCH} -f"
+
+    BINDIR=''${out}/bin
+    ''${mkdir} -p ''${BINDIR}
+
+    cd ''${src}/mescc-tools-extra
+
+    CC sha256sum.c -o ''${BINDIR}/sha256sum
+    CC match.c -o ''${BINDIR}/match
+    CC mkdir.c -o ''${BINDIR}/mkdir
+    CC untar.c -o ''${BINDIR}/untar
+    CC ungz.c -o ''${BINDIR}/ungz
+    CC catm.c -o ''${BINDIR}/catm
+    CC cp.c -o ''${BINDIR}/cp
+    CC chmod.c -o ''${BINDIR}/chmod
+  '';
+in
+{
+  inherit
+    hex0
+    hex1
+    catm
+    hex2-0
+    M0
+    cc_amd64
+    M2
+    blood-elf-0
+    M1-0
+    hex2-1
+    M1
+    hex2
+    kaem
+    M2-Mesoplanet
+    blood-elf
+    get_machine
+    M2-Planet
+    makeBin
+    buildtools0
+    mescc-tools-extra
+    ;
+}