Unverified Commit 47d50c95 authored by John Ericson's avatar John Ericson Committed by GitHub
Browse files

Merge pull request #227914 from emilytrau/trusting-trust

stdenv: begin work on 256b bootstrap i.e. Trusting Trust
parents c66d3f2a c5432d1f
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
{ lib
, config
, buildPlatform
, hostPlatform
}:

lib.makeScope
  # Prevent using top-level attrs to protect against introducing dependency on
  # non-bootstrap packages by mistake. Any top-level inputs must be explicitly
  # declared here.
  (extra: lib.callPackageWith ({ inherit lib config buildPlatform hostPlatform; } // extra))
  (self: with self; {
    inherit (callPackage ./utils.nix { }) fetchurl derivationWithMeta writeTextFile writeText runCommand;

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

    mes = callPackage ./mes { };
    mes-libc = callPackage ./mes/libc.nix { };

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

    tinycc-bootstrappable = callPackage ./tinycc/bootstrappable.nix { };
    tinycc-mes = callPackage ./tinycc/mes.nix { };
  })
+27 −0
Original line number Diff line number Diff line
{ lib
, runCommand
, mes
}:
let
  pname = "ln-boot";
  version = "unstable-2023-05-01";

  src = ./ln.c;
in
runCommand "${pname}-${version}" {
  inherit pname version;

  meta = with lib; {
    description = "Basic tool for creating symbolic links";
    license = licenses.mit;
    maintainers = with maintainers; [ emilytrau ];
    mainProgram = "ln";
    platforms = platforms.unix;
  };
} ''
  mkdir -p ''${out}/bin
  ${mes}/bin/mes --no-auto-compile -e main ${mes}/bin/mescc.scm -- \
    -lc+tcc \
    -o ''${out}/bin/ln \
    ${src}
''
+17 −0
Original line number Diff line number Diff line
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char** argv)
{
  if (argc != 4 || strcmp(argv[1], "-s")) {
    fputs("Usage: ", stdout);
    fputs(argv[0], stdout);
    fputs("ln -s TARGET LINK_NAME\n", stdout);
    exit(EXIT_FAILURE);
  }

  symlink(argv[2], argv[3]);
  exit(EXIT_SUCCESS);
}
+189 −0
Original line number Diff line number Diff line
{ lib
, runCommand
, fetchurl
, writeText
, callPackage
, m2libc
, mescc-tools
}:
let
  pname = "mes";
  version = "0.24.2";

  src = fetchurl {
    url = "mirror://gnu/mes/mes-${version}.tar.gz";
    sha256 = "0vp8v88zszh1imm3dvdfi3m8cywshdj7xcrsq4cgmss69s2y1nkx";
  };

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

  config_h = builtins.toFile "config.h" ''
    #undef SYSTEM_LIBC
    #define MES_VERSION "${version}"
  '';

  sources = (import ./sources.nix).x86.linux.mescc;
  inherit (sources) libc_mini_SOURCES libmescc_SOURCES libc_SOURCES mes_SOURCES;

  # add symlink() to libc+tcc so we can use it in ln-boot
  libc_tcc_SOURCES = sources.libc_tcc_SOURCES ++ [ "lib/linux/symlink.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}";
in
runCommand "${pname}-${version}" {
  inherit pname version;

  passthru = { inherit src nyacc; };

  meta = with lib; {
    description = "Scheme interpreter and C compiler for bootstrapping";
    homepage = "https://www.gnu.org/software/mes";
    license = licenses.gpl3Plus;
    maintainers = with maintainers; [ emilytrau ];
    platforms = [ "i686-linux" ];
  };
}
# Maintenance note:
# Build steps have been adapted from build-aux/bootstrap.sh.in
# as well as the live-bootstrap project
# https://github.com/fosslinux/live-bootstrap/blob/1bc4296091c51f53a5598050c8956d16e945b0f5/sysa/mes-0.24.2/mes-0.24.2.kaem
''
  # Unpack source
  ungz --file ${src} --output mes.tar
  mkdir ''${out} ''${out}/bin ''${out}/share
  cd ''${out}/share
  untar --non-strict --file ''${NIX_BUILD_TOP}/mes.tar # ignore symlinks

  MES_PREFIX=''${out}/share/mes-${version}
  LIBDIR=''${MES_PREFIX}/lib

  cd ''${MES_PREFIX}

  cp ${config_h} include/mes/config.h

  mkdir include/arch
  cp include/linux/x86/syscall.h include/arch/syscall.h
  cp include/linux/x86/kernel-stat.h include/arch/kernel-stat.h

  # Remove pregenerated files
  rm mes/module/mes/psyntax.pp mes/module/mes/psyntax.pp.header

  # These files are symlinked in the repo
  cp mes/module/srfi/srfi-9-struct.mes mes/module/srfi/srfi-9.mes
  cp mes/module/srfi/srfi-9/gnu-struct.mes mes/module/srfi/srfi-9/gnu.mes

  # Fixes to support newer M2-Planet
  catm x86_defs.M1 ${m2libc}/x86/x86_defs.M1 lib/m2/x86/x86_defs.M1
  cp x86_defs.M1 lib/m2/x86/x86_defs.M1
  rm x86_defs.M1

  # Remove environment impurities
  __GUILE_LOAD_PATH="\"''${MES_PREFIX}/mes/module:''${MES_PREFIX}/module:${nyacc.guilePath}\""
  boot0_scm=mes/module/mes/boot-0.scm
  guile_mes=mes/module/mes/guile.mes
  replace --file ''${boot0_scm} --output ''${boot0_scm} --match-on "(getenv \"GUILE_LOAD_PATH\")" --replace-with ''${__GUILE_LOAD_PATH}
  replace --file ''${guile_mes} --output ''${guile_mes} --match-on "(getenv \"GUILE_LOAD_PATH\")" --replace-with ''${__GUILE_LOAD_PATH}

  module_mescc_scm=module/mescc/mescc.scm
  replace --file ''${module_mescc_scm} --output ''${module_mescc_scm} --match-on "(getenv \"M1\")" --replace-with "\"${mescc-tools}/bin/M1\""
  replace --file ''${module_mescc_scm} --output ''${module_mescc_scm} --match-on "(getenv \"HEX2\")" --replace-with "\"${mescc-tools}/bin/hex2\""
  replace --file ''${module_mescc_scm} --output ''${module_mescc_scm} --match-on "(getenv \"BLOOD_ELF\")" --replace-with "\"${mescc-tools}/bin/blood-elf\""
  replace --file ''${module_mescc_scm} --output ''${module_mescc_scm} --match-on "(getenv \"srcdest\")" --replace-with "\"''${MES_PREFIX}\""

  mes_c=src/mes.c
  replace --file ''${mes_c} --output ''${mes_c} --match-on "getenv (\"MES_PREFIX\")" --replace-with "\"''${MES_PREFIX}\""
  replace --file ''${mes_c} --output ''${mes_c} --match-on "getenv (\"srcdest\")" --replace-with "\"''${MES_PREFIX}\""

  # Increase runtime resource limits
  gc_c=src/gc.c
  replace --file ''${gc_c} --output ''${gc_c} --match-on "getenv (\"MES_ARENA\")" --replace-with "\"100000000\""
  replace --file ''${gc_c} --output ''${gc_c} --match-on "getenv (\"MES_MAX_ARENA\")" --replace-with "\"100000000\""
  replace --file ''${gc_c} --output ''${gc_c} --match-on "getenv (\"MES_STACK\")" --replace-with "\"6000000\""

  # Create mescc.scm
  mescc_in=scripts/mescc.scm.in
  replace --file ''${mescc_in} --output ''${mescc_in} --match-on "(getenv \"MES_PREFIX\")" --replace-with "\"''${MES_PREFIX}\""
  replace --file ''${mescc_in} --output ''${mescc_in} --match-on "(getenv \"includedir\")" --replace-with "\"''${MES_PREFIX}/include\""
  replace --file ''${mescc_in} --output ''${mescc_in} --match-on "(getenv \"libdir\")" --replace-with "\"''${MES_PREFIX}/lib\""
  replace --file ''${mescc_in} --output ''${mescc_in} --match-on @prefix@ --replace-with ''${MES_PREFIX}
  replace --file ''${mescc_in} --output ''${mescc_in} --match-on @VERSION@ --replace-with ${version}
  replace --file ''${mescc_in} --output ''${mescc_in} --match-on @mes_cpu@ --replace-with x86
  replace --file ''${mescc_in} --output ''${mescc_in} --match-on @mes_kernel@ --replace-with linux
  cp ''${mescc_in} ''${out}/bin/mescc.scm

  # Build mes-m2
  mes_cpu=x86
  stage0_cpu=x86
  kaem --verbose --strict --file kaem.run
  cp bin/mes-m2 ''${out}/bin/mes-m2
  chmod 555 ''${out}/bin/mes-m2


  # Recompile Mes and Mes C library using mes-m2 bootstrapped Mes
  cd ''${NIX_BUILD_TOP}
  alias CC="''${out}/bin/mes-m2 -e main ''${out}/bin/mescc.scm -- -D HAVE_CONFIG_H=1 -I ''${MES_PREFIX}/include -I ''${MES_PREFIX}/include/linux/x86"
  mkdir -p ''${LIBDIR}/x86-mes

  # crt1.o
  CC -c ''${MES_PREFIX}/lib/linux/x86-mes-mescc/crt1.c
  cp crt1.o ''${LIBDIR}/x86-mes
  cp crt1.s ''${LIBDIR}/x86-mes

  # libc-mini.a
  ${compile libc_mini_SOURCES}
  ${archive "libc-mini.a" libc_mini_SOURCES}
  ${sourceArchive "libc-mini.s" libc_mini_SOURCES}
  cp libc-mini.a ''${LIBDIR}/x86-mes
  cp libc-mini.s ''${LIBDIR}/x86-mes

  # libmescc.a
  ${compile libmescc_SOURCES}
  ${archive "libmescc.a" libmescc_SOURCES}
  ${sourceArchive "libmescc.s" libmescc_SOURCES}
  cp libmescc.a ''${LIBDIR}/x86-mes
  cp libmescc.s ''${LIBDIR}/x86-mes

  # libc.a
  ${compile libc_SOURCES}
  ${archive "libc.a" libc_SOURCES}
  ${sourceArchive "libc.s" libc_SOURCES}
  cp libc.a ''${LIBDIR}/x86-mes
  cp libc.s ''${LIBDIR}/x86-mes

  # libc+tcc.a
  # optimisation: don't recompile common libc sources
  ${compile (lib.subtractLists libc_SOURCES libc_tcc_SOURCES)}
  ${archive "libc+tcc.a" libc_tcc_SOURCES}
  ${sourceArchive "libc+tcc.s" libc_tcc_SOURCES}
  cp libc+tcc.a ''${LIBDIR}/x86-mes
  cp libc+tcc.s ''${LIBDIR}/x86-mes

  # Build mes itself
  ${compile mes_SOURCES}
  ''${out}/bin/mes-m2 -e main ''${out}/bin/mescc.scm -- \
    --base-address 0x08048000 \
    -L ''${MES_PREFIX}/lib \
    -L . \
    -lc \
    -lmescc \
    -nostdlib \
    -o ''${out}/bin/mes \
    crt1.o \
    ${lib.concatMapStringsSep " " (replaceExt ".o") mes_SOURCES}

  # Check
  ''${out}/bin/mes --version
''
+95 −0
Original line number Diff line number Diff line
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p bash coreutils gnutar

# Generate a sources.nix for a version of GNU mes. Creates lists of source files
# from build-aux/configure-lib.sh.
#
# You may point this tool at a manually downloaded tarball, but more ideal is
# using the source tarball from Nixpkgs. For example:
#
# MES_TARBALL="$(nix-build --no-link -A minimal-bootstrap.mes.src ../../../../..)"
# ./gen-sources.sh "$MES_TARBALL" > ./new-sources.nix

set -eu

# Supported platforms
ARCHS="x86"
KERNELS="linux"
COMPILERS="mescc gcc"


format() {
  echo -n "[ "
  # Terrible hack to convert a newline-delimited string to space-delimited
  echo $* | xargs printf '"%s" '
  echo -n "]"
}

gen_sources() {
  # Configuration variables used by configure-lib.sh
  export mes_libc=mes
  export mes_cpu=$1
  export mes_kernel=$2
  export compiler=$3

  # Populate source file lists
  source $CONFIGURE_LIB_SH

  cat <<EOF
  $mes_cpu.$mes_kernel.$compiler = {
    libc_mini_SOURCES = $(format $libc_mini_SOURCES);
    libmescc_SOURCES  = $(format $libmescc_SOURCES);
    libtcc1_SOURCES   = $(format $libtcc1_SOURCES);
    libc_SOURCES      = $(format $libc_SOURCES);
    libc_tcc_SOURCES  = $(format $libc_tcc_SOURCES);
    libc_gnu_SOURCES  = $(format $libc_gnu_SOURCES);
    mes_SOURCES       = $(format $mes_SOURCES);
  };
EOF
}


MES_TARBALL=$1
if [ ! -f $MES_TARBALL ]; then
    echo "Provide path to mes-x.x.x.tar.gz as first argument" >&2
    exit 1
fi
echo "Generating sources.nix from $MES_TARBALL" >&2

TMP=$(mktemp -d)
cd $TMP
echo "Workdir: $TMP" >&2

echo "Extracting $MES_TARBALL" >&2
tar --strip-components 1 -xf $MES_TARBALL

CONFIGURE_LIB_SH="$TMP/build-aux/configure-lib.sh"
if [ ! -f $CONFIGURE_LIB_SH ]; then
    echo "Could not find mes's configure-lib.sh script at $CONFIGURE_LIB_SH" >&2
    exit 1
fi

# Create dummy config expected by configure-lib.sh
touch config.sh
chmod +x config.sh


echo "Configuring with $CONFIGURE_LIB_SH" >&2

cat <<EOF
# This file is generated by ./gen-sources.sh.
# Do not edit!
{
EOF

for arch in $ARCHS; do
  for kernel in $KERNELS; do
    for compiler in $COMPILERS; do
      gen_sources $arch $kernel $compiler
    done
  done
done

cat <<EOF
}
EOF
Loading