Commit 1b065103 authored by Emily Trau's avatar Emily Trau
Browse files

minimal-bootstrap.mes: split out mes-libc package

parent e055a75e
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -12,10 +12,10 @@ lib.makeScope newScope (self: with self; {

  inherit (callPackage ./stage0-posix { }) kaem m2libc mescc-tools mescc-tools-extra writeTextFile writeText runCommand;

  nyacc = callPackage ./mes/nyacc.nix { };
  mes = callPackage ./mes { };
  inherit (mes) mes-libc;

  ln-boot = callPackage ./ln-boot { };

  tinycc-with-mes-libc = callPackage ./tinycc/default.nix { };
  tinycc-mes = callPackage ./tinycc/default.nix { };
})
+22 −9
Original line number Diff line number Diff line
@@ -2,9 +2,9 @@
, runCommand
, fetchurl
, writeText
, callPackage
, m2libc
, mescc-tools
, nyacc
}:
let
  pname = "mes";
@@ -15,6 +15,8 @@ let
    sha256 = "0vp8v88zszh1imm3dvdfi3m8cywshdj7xcrsq4cgmss69s2y1nkx";
  };

  nyacc = callPackage ./nyacc.nix { inherit nyacc; };

  config_h = builtins.toFile "config.h" ''
    #undef SYSTEM_LIBC
    #define MES_VERSION "${version}"
@@ -113,23 +115,34 @@ let
    + "lib/linux/lstat.c lib/linux/mkdir.c lib/linux/mknod.c lib/linux/nanosleep.c "
    + "lib/linux/pipe.c lib/linux/readlink.c lib/linux/rename.c lib/linux/setgid.c "
    + "lib/linux/settimer.c lib/linux/setuid.c lib/linux/signal.c lib/linux/sigprogmask.c "
    + "lib/linux/symlink.c");
    # sylink.c already included above in libc_tcc_SOURCES
    # + "lib/linux/symlink.c"
  );
  mes_SOURCES = cc: lib.splitString " " (
    "src/builtins.c src/cc.c src/core.c src/display.c src/eval-apply.c src/gc.c "
    + "src/globals.c src/hash.c src/lib.c src/math.c src/mes.c src/module.c src/posix.c "
    + "src/reader.c src/stack.c src/string.c src/struct.c src/symbol.c src/vector.c");

  compile = sources: lib.concatMapStringsSep "\n" (f: ''CC -c ''${MES_PREFIX}/${f}'') sources;
  replaceExt = ext: source: lib.replaceStrings [".c"] [ext] (builtins.baseNameOf source);
  archive = out: sources: "catm ${out} ${lib.concatMapStringsSep " " (replaceExt ".o") sources}";
  sourceArchive = out: sources: "catm ${out} ${lib.concatMapStringsSep " " (replaceExt ".s") sources}";
  compile = sources:
    lib.concatMapStringsSep
      "\n"
      (f: ''CC -c ''${MES_PREFIX}/${f}'')
      sources;
  replaceExt = ext: source:
    lib.replaceStrings
      [ ".c" ]
      [ ext ]
      (builtins.baseNameOf source);
  archive = out: sources:
    "catm ${out} ${lib.concatMapStringsSep " " (replaceExt ".o") sources}";
  sourceArchive = out: sources:
    "catm ${out} ${lib.concatMapStringsSep " " (replaceExt ".s") sources}";
in
runCommand "${pname}-${version}" {
  inherit pname version;

  passthru = {
    mesPrefix = "/share/mes-${version}";
    libcSources = libc_SOURCES "gcc" ++ libc_gnu_SOURCES "gcc";
  passthru.mes-libc = callPackage ./libc.nix {
    inherit libc_SOURCES libc_gnu_SOURCES;
  };

  meta = with lib; {
+60 −0
Original line number Diff line number Diff line
{ lib
, runCommand
, ln-boot
, mes
, libc_SOURCES
, libc_gnu_SOURCES
, mes-libc
}:
let
  pname = "mes-libc";
  inherit (mes) version;

  # Concatenate all source files into a convenient bundle
  # "gcc" variants (eg. "lib/linux/x86-mes-gcc") can also be used with tinycc
  sources = libc_SOURCES "gcc" ++ libc_gnu_SOURCES "gcc";
  # Passing this many arguments is too much for kaem so we need to split
  # the operation in two
  firstLibc = lib.take 100 sources;
  lastLibc = lib.drop 100 sources;
in runCommand "${pname}-${version}" {
  inherit pname version;

  nativeBuildInputs = [ ln-boot ];

  passthru.CFLAGS = "-DHAVE_CONFIG_H=1 -I${mes-libc}/include -I${mes-libc}/include/linux/x86";

  meta = with lib; {
    description = "The Mes C Library";
    homepage = "https://www.gnu.org/software/mes";
    license = licenses.gpl3Plus;
    maintainers = with maintainers; [ emilytrau ];
    platforms = [ "i686-linux" ];
  };
} ''
  mkdir -p ''${out}/lib
  PREFIX=${mes}/share/mes-${version}
  cd ''${PREFIX}

  # mescc compiled libc.a
  mkdir ''${out}/lib/x86-mes
  cp lib/x86-mes/libc.a ''${out}/lib/x86-mes

  # libc.c
  catm ''${TMPDIR}/first.c ${lib.concatStringsSep " " firstLibc}
  catm ''${out}/lib/libc.c ''${TMPDIR}/first.c ${lib.concatStringsSep " " lastLibc}

  # crt{1,n,i}.c
  cp lib/linux/x86-mes-gcc/crt1.c ''${out}/lib
  cp lib/linux/x86-mes-gcc/crtn.c ''${out}/lib
  cp lib/linux/x86-mes-gcc/crti.c ''${out}/lib

  # libtcc1.c
  cp lib/libtcc1.c ''${out}/lib

  # getopt.c
  cp lib/posix/getopt.c ''${out}/lib

  # Install headers
  ln -s ''${PREFIX}/include ''${out}/include
''
+31 −47
Original line number Diff line number Diff line
@@ -11,12 +11,13 @@
, runCommand
, fetchurl
, mes
, buildTinyccN
, mes-libc
, buildTinyccN
}:
let
  version = "unstable-2023-04-20";
  rev = "80114c4da6b17fbaabb399cc29f427e368309bc8";

  tarball = fetchurl {
    url = "https://gitlab.com/janneke/tinycc/-/archive/${rev}/tinycc-${rev}.tar.gz";
    sha256 = "1a0cw9a62qc76qqn5sjmp3xrbbvsz2dxrw21lrnx9q0s74mwaxbq";
@@ -36,10 +37,8 @@ let
    platforms = [ "i686-linux" ];
  };

  mes-tcc = runCommand "mes-tcc-${version}" {} ''
    # Create config.h
  tinycc-boot-mes = runCommand "tinycc-boot-mes-${version}" {} ''
    catm config.h

    ${mes}/bin/mes --no-auto-compile -e main ${mes}/bin/mescc.scm -- \
      -S \
      -o tcc.s \
@@ -52,8 +51,8 @@ let
      -D CONFIG_SYSROOT=\"\" \
      -D CONFIG_TCC_CRTPREFIX=\"''${out}/lib\" \
      -D CONFIG_TCC_ELFINTERP=\"/mes/loader\" \
      -D CONFIG_TCC_SYSINCLUDEPATHS=\"${mes}${mes.mesPrefix}/include\" \
      -D TCC_LIBGCC=\"${mes}${mes.mesPrefix}/lib/x86-mes/libc.a\" \
      -D CONFIG_TCC_SYSINCLUDEPATHS=\"${mes-libc}/include\" \
      -D TCC_LIBGCC=\"${mes-libc}/lib/x86-mes/libc.a\" \
      -D CONFIG_TCC_LIBTCC1_MES=0 \
      -D CONFIG_TCCBOOT=1 \
      -D CONFIG_TCC_STATIC=1 \
@@ -62,49 +61,34 @@ let
      -D TCC_VERSION=\"${version}\" \
      -D ONE_SOURCE=1 \
      ${src}/tcc.c

    mkdir -p ''${out}/bin
    ${mes}/bin/mes --no-auto-compile -e main ${mes}/bin/mescc.scm -- \
      -l c+tcc \
      -o ''${out}/bin/tcc \
      tcc.s

    # Quick test
    ''${out}/bin/tcc -version

    # Recompile the mes C library
    # Recompile libc: crt{1,n,i}, libtcc.a, libc.a, libgetopt.a
    mkdir -p ''${out}/lib
    cd ${mes}${mes.mesPrefix}

    # crt1.o
    ''${out}/bin/tcc -c -D HAVE_CONFIG_H=1 -I include -I include/linux/x86 -o ''${out}/lib/crt1.o lib/linux/x86-mes-gcc/crt1.c

    # crtn.o
    ''${out}/bin/tcc -c -D HAVE_CONFIG_H=1 -I include -I include/linux/x86 -o ''${out}/lib/crtn.o lib/linux/x86-mes-gcc/crtn.c

    # crti.o
    ''${out}/bin/tcc -c -D HAVE_CONFIG_H=1 -I include -I include/linux/x86 -o ''${out}/lib/crti.o lib/linux/x86-mes-gcc/crti.c

    # libc+gcc.a
    ''${out}/bin/tcc -c -D HAVE_CONFIG_H=1 -I include -I include/linux/x86 -o ''${TMPDIR}/mes-libc.o ${mes-libc}
    ''${out}/bin/tcc -ar cr ''${out}/lib/libc.a ''${TMPDIR}/mes-libc.o

    # libtcc1.a
    ''${out}/bin/tcc -c -D HAVE_CONFIG_H=1 -I include -I include/linux/x86 -o ''${TMPDIR}/libtcc1.o lib/libtcc1.c
    ''${out}/bin/tcc -ar cr ''${out}/lib/libtcc1.a ''${TMPDIR}/libtcc1.o

    # libgetopt.a
    ''${out}/bin/tcc -c -D HAVE_CONFIG_H=1 -I include -I include/linux/x86 -o ''${TMPDIR}/getopt.o lib/posix/getopt.c
    ''${out}/bin/tcc -ar cr ''${out}/lib/libgetopt.a ''${TMPDIR}/getopt.o
    ''${out}/bin/tcc ${mes-libc.CFLAGS} -c -o ''${out}/lib/crt1.o ${mes-libc}/lib/crt1.c
    ''${out}/bin/tcc ${mes-libc.CFLAGS} -c -o ''${out}/lib/crtn.o ${mes-libc}/lib/crtn.c
    ''${out}/bin/tcc ${mes-libc.CFLAGS} -c -o ''${out}/lib/crti.o ${mes-libc}/lib/crti.c
    ''${out}/bin/tcc ${mes-libc.CFLAGS} -c -o libc.o ${mes-libc}/lib/libc.c
    ''${out}/bin/tcc -ar cr ''${out}/lib/libc.a libc.o
    ''${out}/bin/tcc ${mes-libc.CFLAGS} -c -o libtcc1.o ${mes-libc}/lib/libtcc1.c
    ''${out}/bin/tcc -ar cr ''${out}/lib/libtcc1.a libtcc1.o
    ''${out}/bin/tcc ${mes-libc.CFLAGS} -c -o getopt.o ${mes-libc}/lib/getopt.c
    ''${out}/bin/tcc -ar cr ''${out}/lib/libgetopt.a getopt.o
  '';

  # Bootstrap stage build flags obtained from
  # https://gitlab.com/janneke/tinycc/-/blob/80114c4da6b17fbaabb399cc29f427e368309bc8/boot.sh

  boot0-tcc = buildTinyccN {
    pname = "boot0-tcc";
  tinycc-boot0 = buildTinyccN {
    pname = "tinycc-boot0";
    inherit src version meta;
    prev = mes-tcc;
    prev = tinycc-boot-mes;
    buildOptions = [
      "-D HAVE_LONG_LONG_STUB=1"
      "-D HAVE_SETJMP=1"
@@ -114,10 +98,10 @@ let
    ];
  };

  boot1-tcc = buildTinyccN {
    pname = "boot1-tcc";
  tinycc-boot1 = buildTinyccN {
    pname = "tinycc-boot1";
    inherit src version meta;
    prev = boot0-tcc;
    prev = tinycc-boot0;
    buildOptions = [
      "-D HAVE_BITFIELD=1"
      "-D HAVE_LONG_LONG=1"
@@ -128,10 +112,10 @@ let
    ];
  };

  boot2-tcc = buildTinyccN {
    pname = "boot2-tcc";
  tinycc-boot2 = buildTinyccN {
    pname = "tinycc-boot2";
    inherit src version meta;
    prev = boot1-tcc;
    prev = tinycc-boot1;
    buildOptions = [
      "-D HAVE_BITFIELD=1"
      "-D HAVE_FLOAT_STUB=1"
@@ -144,10 +128,10 @@ let
    ];
  };

  boot3-tcc = buildTinyccN {
    pname = "boot3-tcc";
  tinycc-boot3 = buildTinyccN {
    pname = "tinycc-boot3";
    inherit src version meta;
    prev = boot2-tcc;
    prev = tinycc-boot2;
    buildOptions = [
      "-D HAVE_BITFIELD=1"
      "-D HAVE_FLOAT=1"
@@ -160,10 +144,10 @@ let
    ];
  };

  boot4-tcc = buildTinyccN {
    pname = "boot4-tcc";
  tinycc-bootstrappable = buildTinyccN {
    pname = "tinycc-bootstrappable";
    inherit src version meta;
    prev = boot3-tcc;
    prev = tinycc-boot3;
    buildOptions = [
      "-D HAVE_BITFIELD=1"
      "-D HAVE_FLOAT=1"
@@ -176,4 +160,4 @@ let
    ];
  };
in
boot4-tcc
tinycc-bootstrappable
+31 −36
Original line number Diff line number Diff line
@@ -9,11 +9,15 @@
, fetchurl
, callPackage
, mes
, mes-libc
, ln-boot
}:
let
  version = "unstable-2023-04-20";
  rev = "86f3d8e33105435946383aee52487b5ddf918140";

  tarball = fetchurl {
    url = "https://repo.or.cz/tinycc.git/snapshot/86f3d8e33105435946383aee52487b5ddf918140.tar.gz";
    url = "https://repo.or.cz/tinycc.git/snapshot/${rev}.tar.gz";
    sha256 = "11idrvbwfgj1d03crv994mpbbbyg63j1k64lw1gjy7mkiifw2xap";
  };
  src = (runCommand "tinycc-${version}-source" {} ''
@@ -21,7 +25,7 @@ let
    mkdir -p ''${out}
    cd ''${out}
    untar --file ''${NIX_BUILD_TOP}/tinycc.tar
  '') + "/tinycc-86f3d8e";
  '') + "/tinycc-${builtins.substring 0 7 rev}";

  meta = with lib; {
    description = "Small, fast, and embeddable C compiler and interpreter";
@@ -31,19 +35,6 @@ let
    platforms = [ "i686-linux" ];
  };

  # Concatenate all source files into a convenient bundle
  mes-libc =
    let
      # Passing this many arguments is too much for kaem so we need to
      # split the operation in two
      firstLibc = lib.take 100 mes.libcSources;
      lastLibc = lib.drop 100 mes.libcSources;
    in runCommand "mes-libc-${version}.c" {} ''
      cd ${mes}${mes.mesPrefix}
      catm ''${TMPDIR}/first.c ${lib.concatStringsSep " " firstLibc}
      catm ''${out} ''${TMPDIR}/first.c ${lib.concatStringsSep " " lastLibc}
    '';

  buildTinyccN = {
    pname,
    version,
@@ -59,9 +50,10 @@ let
    in
    runCommand "${pname}-${version}" {
      inherit pname version meta;
      nativeBuildInputs = [ ln-boot ];
    } ''
      catm config.h
      mkdir -p ''${out}/bin ''${out}/lib
      mkdir -p ''${out}/bin
      ${prev}/bin/tcc \
        -g \
        -v \
@@ -76,7 +68,7 @@ let
        -D CONFIG_TCC_CRTPREFIX=\"''${out}/lib\" \
        -D CONFIG_TCC_ELFINTERP=\"\" \
        -D CONFIG_TCC_LIBPATHS=\"''${out}/lib\" \
        -D CONFIG_TCC_SYSINCLUDEPATHS=\"${mes}${mes.mesPrefix}/include:${src}/include\" \
        -D CONFIG_TCC_SYSINCLUDEPATHS=\"${mes-libc}/include:${src}/include\" \
        -D TCC_LIBGCC=\"libc.a\" \
        -D TCC_LIBTCC1=\"libtcc1.a\" \
        -D CONFIG_TCCBOOT=1 \
@@ -90,31 +82,34 @@ let

      ''${out}/bin/tcc -v

      cd ${mes}${mes.mesPrefix}
      # Recompile libc: crt{1,n,i}, libtcc.a, libc.a, libgetopt.a
      ''${out}/bin/tcc -c -D HAVE_CONFIG_H=1 -I include -I include/linux/x86 -o ''${out}/lib/crt1.o lib/linux/x86-mes-gcc/crt1.c
      ''${out}/bin/tcc -c -D HAVE_CONFIG_H=1 -I include -I include/linux/x86 -o ''${out}/lib/crtn.o lib/linux/x86-mes-gcc/crtn.c
      ''${out}/bin/tcc -c -D HAVE_CONFIG_H=1 -I include -I include/linux/x86 -o ''${out}/lib/crti.o lib/linux/x86-mes-gcc/crti.c
      ''${out}/bin/tcc -c -D TCC_TARGET_I386=1 ${libtccOptions} -o ''${TMPDIR}/libtcc1.o ${src}/lib/libtcc1.c
      ''${out}/bin/tcc -ar cr ''${out}/lib/libtcc1.a ''${TMPDIR}/libtcc1.o
      ''${out}/bin/tcc -c -D HAVE_CONFIG_H=1 -I include -I include/linux/x86 -o ''${TMPDIR}/mes-libc.o ${mes-libc}
      ''${out}/bin/tcc -ar cr ''${out}/lib/libc.a ''${TMPDIR}/mes-libc.o
      ''${out}/bin/tcc -c -D HAVE_CONFIG_H=1 -I include -I include/linux/x86 -o ''${TMPDIR}/getopt.o lib/posix/getopt.c
      ''${out}/bin/tcc -ar cr ''${out}/lib/libgetopt.a ''${TMPDIR}/getopt.o
      mkdir -p ''${out}/lib
      ''${out}/bin/tcc ${mes-libc.CFLAGS} -c -o ''${out}/lib/crt1.o ${mes-libc}/lib/crt1.c
      ''${out}/bin/tcc ${mes-libc.CFLAGS} -c -o ''${out}/lib/crtn.o ${mes-libc}/lib/crtn.c
      ''${out}/bin/tcc ${mes-libc.CFLAGS} -c -o ''${out}/lib/crti.o ${mes-libc}/lib/crti.c
      ''${out}/bin/tcc -c -D TCC_TARGET_I386=1 ${libtccOptions} -o libtcc1.o ${src}/lib/libtcc1.c
      ''${out}/bin/tcc -ar cr ''${out}/lib/libtcc1.a libtcc1.o
      ''${out}/bin/tcc ${mes-libc.CFLAGS} -c -o libc.o ${mes-libc}/lib/libc.c
      ''${out}/bin/tcc -ar cr ''${out}/lib/libc.a libc.o
      ''${out}/bin/tcc ${mes-libc.CFLAGS} -c -o getopt.o ${mes-libc}/lib/getopt.c
      ''${out}/bin/tcc -ar cr ''${out}/lib/libgetopt.a getopt.o

      # Install headers
      ln -s ${mes-libc}/include ''${out}/include
    '';

  boot4-tcc = callPackage ./bootstrappable.nix { inherit buildTinyccN mes-libc; };
  tinycc-bootstrappable = callPackage ./bootstrappable.nix { inherit buildTinyccN; };

  tccdefs = runCommand "tccdefs-${version}" {} ''
    mkdir ''${out}
    ${boot4-tcc}/bin/tcc -static -DC2STR -o c2str ${src}/conftest.c
    ${tinycc-bootstrappable}/bin/tcc -static -DC2STR -o c2str ${src}/conftest.c
    ./c2str ${src}/include/tccdefs.h ''${out}/tccdefs_.h
  '';

  boot5-tcc = buildTinyccN {
    pname = "boot5-tcc";
  tinycc-mes-boot = buildTinyccN {
    pname = "tinycc-mes-boot";
    inherit src version meta;
    prev = boot4-tcc;
    prev = tinycc-bootstrappable;
    buildOptions = [
      "-D HAVE_BITFIELD=1"
      "-D HAVE_FLOAT=1"
@@ -133,10 +128,10 @@ let
    ];
  };

  tinycc-with-mes-libc = buildTinyccN {
    pname = "tinycc-with-mes-libc";
  tinycc-mes = buildTinyccN {
    pname = "tinycc-mes";
    inherit src version meta;
    prev = boot5-tcc;
    prev = tinycc-mes-boot;
    buildOptions = [
      "-std=c99"
      "-D HAVE_BITFIELD=1"
@@ -156,4 +151,4 @@ let
    ];
  };
in
tinycc-with-mes-libc
tinycc-mes