Unverified Commit 4798d49c authored by Artturi's avatar Artturi Committed by GitHub
Browse files

Merge pull request #255797 from emilytrau/minimal-musl-1.1

parents 31c64720 1340efd4
Loading
Loading
Loading
Loading
+11 −1
Original line number Diff line number Diff line
@@ -144,9 +144,14 @@ lib.makeScope
    mes = lib.recurseIntoAttrs (callPackage ./mes { });
    mes-libc = callPackage ./mes/libc.nix { };

    musl11 = callPackage ./musl/1.1.nix {
      bash = bash_2_05;
      tinycc = tinycc-mes;
      gnused = gnused-mes;
    };

    musl = callPackage ./musl {
      gcc = gcc46;
      gawk = gawk-mes;
    };

    stage0-posix = callPackage ./stage0-posix { };
@@ -155,6 +160,10 @@ lib.makeScope

    tinycc-bootstrappable = lib.recurseIntoAttrs (callPackage ./tinycc/bootstrappable.nix { });
    tinycc-mes = lib.recurseIntoAttrs (callPackage ./tinycc/mes.nix { });
    tinycc-musl = lib.recurseIntoAttrs (callPackage ./tinycc/musl.nix {
      bash = bash_2_05;
      musl = musl11;
    });

    xz = callPackage ./xz {
      bash = bash_2_05;
@@ -187,6 +196,7 @@ lib.makeScope
      echo ${mes.compiler.tests.get-version}
      echo ${musl.tests.hello-world}
      echo ${tinycc-mes.compiler.tests.chain}
      echo ${tinycc-musl.compiler.tests.hello-world}
      echo ${xz.tests.get-version}
      mkdir ''${out}
    '';
+114 −0
Original line number Diff line number Diff line
{ lib
, buildPlatform
, hostPlatform
, fetchurl
, bash
, tinycc
, gnumake
, gnupatch
, gnused
, gnugrep
, gnutar
, gzip
}:

let
  inherit (import ./common.nix { inherit lib; }) pname meta;
  version = "1.1.24";

  src = fetchurl {
    url = "https://musl.libc.org/releases/musl-${version}.tar.gz";
    hash = "sha256-E3DJqBKyzyp9koAlEMygBYzDfmanvt1wBR8KNAFQIqM=";
  };

  # Thanks to the live-bootstrap project!
  # See https://github.com/fosslinux/live-bootstrap/blob/d98f97e21413efc32c770d0356f1feda66025686/sysa/musl-1.1.24/musl-1.1.24.sh
  liveBootstrap = "https://github.com/fosslinux/live-bootstrap/raw/d98f97e21413efc32c770d0356f1feda66025686/sysa/musl-1.1.24";
  patches = [
    (fetchurl {
      url = "${liveBootstrap}/patches/avoid_set_thread_area.patch";
      hash = "sha256-TsbBZXk4/KMZG9EKi7cF+sullVXrxlizLNH0UHGXsPs=";
    })
    (fetchurl {
      url = "${liveBootstrap}/patches/avoid_sys_clone.patch";
      hash = "sha256-/ZmH64J57MmbxdfQ4RNjamAiBdkImMTlHsHdgV4gMj4=";
    })
    (fetchurl {
      url = "${liveBootstrap}/patches/fenv.patch";
      hash = "sha256-vMVGjoN4deAJW5gsSqA207SJqAbvhrnOsGK49DdEiTI=";
    })
    (fetchurl {
      url = "${liveBootstrap}/patches/makefile.patch";
      hash = "sha256-03iYBAUnsrEdLIIhhhq5mM6BGnPn2EfUmIHu51opxbw=";
    })
    (fetchurl {
      url = "${liveBootstrap}/patches/musl_weak_symbols.patch";
      hash = "sha256-/d9a2eUkpe9uyi1ye6T4CiYc9MR3FZ9na0Gb90+g4v0=";
    })
    (fetchurl {
      url = "${liveBootstrap}/patches/set_thread_area.patch";
      hash = "sha256-RIZYqbbRSx4X/0iFUhriwwBRmoXVR295GNBUjf2UrM0=";
    })
    (fetchurl {
      url = "${liveBootstrap}/patches/sigsetjmp.patch";
      hash = "sha256-wd2Aev1zPJXy3q933aiup5p1IMKzVJBquAyl3gbK4PU=";
    })
    # FIXME: this patch causes the build to fail
    # (fetchurl {
    #   url = "${liveBootstrap}/patches/stdio_flush_on_exit.patch";
    #   hash = "sha256-/z5ze3h3QTysay8nRvyvwPv3pmTcKptdkBIaMCoeLDg=";
    # })
    (fetchurl {
      url = "${liveBootstrap}/patches/va_list.patch";
      hash = "sha256-UmcMIl+YCi3wIeVvjbsCyqFlkyYsM4ECNwTfXP+s7vg=";
    })
  ];
in
bash.runCommand "${pname}-${version}" {
  inherit pname version meta;

  nativeBuildInputs = [
    tinycc.compiler
    gnumake
    gnupatch
    gnused
    gnugrep
    gnutar
    gzip
  ];
} ''
  # Unpack
  tar xzf ${src}
  cd musl-${version}

  # Patch
  ${lib.concatMapStringsSep "\n" (f: "patch -Np0 -i ${f}") patches}
  # tcc does not support complex types
  rm -rf src/complex
  # Configure fails without this
  mkdir -p /dev
  # https://github.com/ZilchOS/bootstrap-from-tcc/blob/2e0c68c36b3437386f786d619bc9a16177f2e149/using-nix/2a3-intermediate-musl.nix
  sed -i 's|/bin/sh|${bash}/bin/bash|' \
    tools/*.sh
  chmod 755 tools/*.sh
  # patch popen/system to search in PATH instead of hardcoding /bin/sh
  sed -i 's|posix_spawn(&pid, "/bin/sh",|posix_spawnp(\&pid, "sh",|' \
    src/stdio/popen.c src/process/system.c
  sed -i 's|execl("/bin/sh", "sh", "-c",|execlp("sh", "-c",|'\
    src/misc/wordexp.c

  # Configure
  bash ./configure \
    --prefix=$out \
    --build=${buildPlatform.config} \
    --host=${hostPlatform.config} \
    --disable-shared \
    CC=tcc

  # Build
  make AR="tcc -ar" RANLIB=true CFLAGS="-DSYSCALL_NO_TLS"

  # Install
  make install
  cp ${tinycc.libs}/lib/libtcc1.a $out/lib
''
+13 −0
Original line number Diff line number Diff line
{ lib }:

{
  pname = "musl";

  meta = with lib; {
    description = "An efficient, small, quality libc implementation";
    homepage = "https://musl.libc.org";
    license = licenses.mit;
    maintainers = teams.minimal-bootstrap.members;
    platforms = platforms.unix;
  };
}
+2 −12
Original line number Diff line number Diff line
@@ -8,12 +8,11 @@
, gnumake
, gnugrep
, gnused
, gawk
, gnutar
, gzip
}:
let
  pname = "musl";
  inherit (import ./common.nix { inherit lib; }) pname meta;
  version = "1.2.4";

  src = fetchurl {
@@ -22,7 +21,7 @@ let
  };
in
bash.runCommand "${pname}-${version}" {
  inherit pname version;
  inherit pname version meta;

  nativeBuildInputs = [
    gcc
@@ -30,7 +29,6 @@ bash.runCommand "${pname}-${version}" {
    gnumake
    gnused
    gnugrep
    gawk
    gnutar
    gzip
  ];
@@ -50,14 +48,6 @@ bash.runCommand "${pname}-${version}" {
        ./test
        mkdir $out
      '';

  meta = with lib; {
    description = "An efficient, small, quality libc implementation";
    homepage = "https://musl.libc.org";
    license = licenses.mit;
    maintainers = teams.minimal-bootstrap.members;
    platforms = platforms.unix;
  };
} ''
  # Unpack
  tar xzf ${src}
+155 −0
Original line number Diff line number Diff line
# Build steps adapted from https://github.com/fosslinux/live-bootstrap/blob/1bc4296091c51f53a5598050c8956d16e945b0f5/sysa/tcc-0.9.27/tcc-musl-pass1.sh
#
# SPDX-FileCopyrightText: 2021-22 fosslinux <fosslinux@aussies.space>
#
# SPDX-License-Identifier: GPL-3.0-or-later

{ lib
, fetchurl
, callPackage
, bash
, tinycc-bootstrappable
, musl
, gnupatch
, gnutar
, bzip2
}:
let
  pname = "tinycc-musl";
  version = "0.9.27";

  src = fetchurl {
    url = "https://download.savannah.gnu.org/releases/tinycc/tcc-${version}.tar.bz2";
    hash = "sha256-3iOvePypDOMt/y3UWzQysjNHQLubt7Bb9g/b/Dls65w=";
  };

  # Thanks to the live-bootstrap project!
  # See https://github.com/fosslinux/live-bootstrap/blob/424aa5be38a3023aa6842883a3954599b1597986/sysa/tcc-0.9.27/tcc-musl-pass1.sh
  liveBootstrap = "https://github.com/fosslinux/live-bootstrap/raw/424aa5be38a3023aa6842883a3954599b1597986/sysa/tcc-0.9.27";
  patches = [
    (fetchurl {
      url = "${liveBootstrap}/patches/ignore-duplicate-symbols.patch";
      hash = "sha256-6Js8HkzjYlA8ETxeEYRWu+03OJI60NvR5h1QPkcMTlQ=";
    })
    (fetchurl {
      url = "${liveBootstrap}/patches/ignore-static-inside-array.patch";
      hash = "sha256-IF4RevLGjzRBuYqhuyG7+x6SVljzMAsYRKicNsmtbDY=";
    })
    (fetchurl {
      url = "${liveBootstrap}/patches/static-link.patch";
      hash = "sha256-gX/hJ9a/0Zg29KIBUme+mOA8WrPQvp0SvojP8DN9mSI=";
    })
  ];

  meta = with lib; {
      description = "Small, fast, and embeddable C compiler and interpreter";
      homepage = "http://savannah.nongnu.org/projects/tinycc";
      license = licenses.lgpl21Only;
      maintainers = teams.minimal-bootstrap.members;
      platforms = [ "i686-linux" ];
    };

  tinycc-musl = bash.runCommand "${pname}-${version}" {
    inherit pname version meta;

    nativeBuildInputs = [
      tinycc-bootstrappable.compiler
      gnupatch
      gnutar
      bzip2
    ];
  } ''
    # Unpack
    cp ${src} tinycc.tar.bz2
    bunzip2 tinycc.tar.bz2
    tar xf tinycc.tar
    rm tinycc.tar
    cd tcc-${version}

    # Patch
    ${lib.concatMapStringsSep "\n" (f: "patch -Np0 -i ${f}") patches}

    # Configure
    touch config.h

    # Build
    # We first have to recompile using tcc-0.9.26 as tcc-0.9.27 is not self-hosting,
    # but when linked with musl it is.
    ln -s ${musl}/lib/libtcc1.a ./libtcc1.a

    tcc -v \
      -static \
      -o tcc-musl \
      -D TCC_TARGET_I386=1 \
      -D CONFIG_TCCDIR=\"\" \
      -D CONFIG_TCC_CRTPREFIX=\"{B}\" \
      -D CONFIG_TCC_ELFINTERP=\"/musl/loader\" \
      -D CONFIG_TCC_LIBPATHS=\"{B}\" \
      -D CONFIG_TCC_SYSINCLUDEPATHS=\"${musl}/include\" \
      -D TCC_LIBGCC=\"libc.a\" \
      -D TCC_LIBTCC1=\"libtcc1.a\" \
      -D CONFIG_TCC_STATIC=1 \
      -D CONFIG_USE_LIBGCC=1 \
      -D TCC_VERSION=\"0.9.27\" \
      -D ONE_SOURCE=1 \
      -B . \
      -B ${tinycc-bootstrappable.libs}/lib \
      tcc.c
    # libtcc1.a
    rm -f libtcc1.a
    tcc -c -D HAVE_CONFIG_H=1 lib/libtcc1.c
    tcc -ar cr libtcc1.a libtcc1.o

    # Rebuild tcc-musl with itself
    ./tcc-musl \
      -v \
      -static \
      -o tcc-musl \
      -D TCC_TARGET_I386=1 \
      -D CONFIG_TCCDIR=\"\" \
      -D CONFIG_TCC_CRTPREFIX=\"{B}\" \
      -D CONFIG_TCC_ELFINTERP=\"/musl/loader\" \
      -D CONFIG_TCC_LIBPATHS=\"{B}\" \
      -D CONFIG_TCC_SYSINCLUDEPATHS=\"${musl}/include\" \
      -D TCC_LIBGCC=\"libc.a\" \
      -D TCC_LIBTCC1=\"libtcc1.a\" \
      -D CONFIG_TCC_STATIC=1 \
      -D CONFIG_USE_LIBGCC=1 \
      -D TCC_VERSION=\"0.9.27\" \
      -D ONE_SOURCE=1 \
      -B . \
      -B ${musl}/lib \
      tcc.c
    # libtcc1.a
    rm -f libtcc1.a
    ./tcc-musl -c -D HAVE_CONFIG_H=1 lib/libtcc1.c
    ./tcc-musl -ar cr libtcc1.a libtcc1.o

    # Install
    install -D tcc-musl $out/bin/tcc
    install -Dm444 libtcc1.a $out/lib/libtcc1.a
  '';
in
{
  compiler = bash.runCommand "${pname}-${version}-compiler" {
    inherit pname version meta;
    passthru.tests.hello-world = result:
      bash.runCommand "${pname}-simple-program-${version}" {} ''
        cat <<EOF >> test.c
        #include <stdio.h>
        int main() {
          printf("Hello World!\n");
          return 0;
        }
        EOF
        ${result}/bin/tcc -v -static -B${musl}/lib -o test test.c
        ./test
        mkdir $out
      '';
    passthru.tinycc-musl = tinycc-musl;
  } "install -D ${tinycc-musl}/bin/tcc $out/bin/tcc";

  libs = bash.runCommand "${pname}-${version}-libs" {
    inherit pname version meta;
  } "install -D ${tinycc-musl}/lib/libtcc1.a $out/lib/libtcc1.a";
}