Merge pull request #231894 from Ericson2314/parallel-bootstrap

minimal-bootstrap: Parallelize + separate compilers & libs
This commit is contained in:
John Ericson 2023-05-16 09:38:20 -04:00 committed by GitHub
commit a4c5ee1f49
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 368 additions and 262 deletions

View file

@ -20,11 +20,18 @@ lib.makeScope
mes = callPackage ./mes { }; mes = callPackage ./mes { };
mes-libc = callPackage ./mes/libc.nix { }; mes-libc = callPackage ./mes/libc.nix { };
inherit (callPackage ./stage0-posix { }) kaem m2libc mescc-tools mescc-tools-extra; stage0-posix = callPackage ./stage0-posix { };
inherit (self.stage0-posix) kaem m2libc mescc-tools mescc-tools-extra;
tinycc-bootstrappable = callPackage ./tinycc/bootstrappable.nix { }; tinycc-bootstrappable = callPackage ./tinycc/bootstrappable.nix { };
tinycc-mes = callPackage ./tinycc/mes.nix { }; tinycc-mes = callPackage ./tinycc/mes.nix { };
inherit (callPackage ./utils.nix { }) fetchurl derivationWithMeta writeTextFile writeText; inherit (callPackage ./utils.nix { }) fetchurl derivationWithMeta writeTextFile writeText;
test = kaem.runCommand "minimal-bootstrap-test" {} ''
echo ${mes.compiler.tests.get-version}
echo ${tinycc-mes.compiler.tests.chain}
mkdir ''${out}
'';
}) })

View file

@ -45,7 +45,7 @@ let
./configure \ ./configure \
--build i686-pc-linux-gnu \ --build i686-pc-linux-gnu \
--host i686-pc-linux-gnu \ --host i686-pc-linux-gnu \
CC="${tinycc-mes}/bin/tcc -static" \ CC="${tinycc.compiler}/bin/tcc -B ${tinycc.libs}/lib -static" \
ac_cv_func_dup=no ac_cv_func_dup=no
- `ac_cv_func_dup` disabled as mes-libc doesn't implement tmpfile() - `ac_cv_func_dup` disabled as mes-libc doesn't implement tmpfile()
@ -148,7 +148,7 @@ in
kaem.runCommand "${pname}-${version}" { kaem.runCommand "${pname}-${version}" {
inherit pname version; inherit pname version;
nativeBuildInputs = [ tinycc gnupatch ]; nativeBuildInputs = [ tinycc.compiler gnupatch ];
meta = with lib; { meta = with lib; {
description = "A tool to control the generation of non-source files from sources"; description = "A tool to control the generation of non-source files from sources";
@ -174,7 +174,7 @@ kaem.runCommand "${pname}-${version}" {
cp lib/fnmatch.in.h lib/fnmatch.h cp lib/fnmatch.in.h lib/fnmatch.h
# Compile # Compile
alias CC="tcc ${lib.concatStringsSep " " CFLAGS}" alias CC="tcc -B ${tinycc.libs}/lib ${lib.concatStringsSep " " CFLAGS}"
${lib.concatMapStringsSep "\n" (f: "CC -c ${f}") sources} ${lib.concatMapStringsSep "\n" (f: "CC -c ${f}") sources}
# Link # Link

View file

@ -70,7 +70,7 @@ in
kaem.runCommand "${pname}-${version}" { kaem.runCommand "${pname}-${version}" {
inherit pname version; inherit pname version;
nativeBuildInputs = [ tinycc ]; nativeBuildInputs = [ tinycc.compiler ];
meta = with lib; { meta = with lib; {
description = "GNU Patch, a program to apply differences to files"; description = "GNU Patch, a program to apply differences to files";
@ -91,7 +91,7 @@ kaem.runCommand "${pname}-${version}" {
catm config.h catm config.h
# Build # Build
alias CC="tcc ${lib.concatStringsSep " " CFLAGS}" alias CC="tcc -B ${tinycc.libs}/lib ${lib.concatStringsSep " " CFLAGS}"
${lib.concatMapStringsSep "\n" (f: "CC -c ${f}") sources} ${lib.concatMapStringsSep "\n" (f: "CC -c ${f}") sources}
# Link # Link

View file

@ -20,7 +20,8 @@ kaem.runCommand "${pname}-${version}" {
}; };
} '' } ''
mkdir -p ''${out}/bin mkdir -p ''${out}/bin
${mes}/bin/mes --no-auto-compile -e main ${mes}/bin/mescc.scm -- \ ${mes.compiler}/bin/mes --no-auto-compile -e main ${mes.srcPost.bin}/bin/mescc.scm -- \
-L ${mes.libs}/lib \
-lc+tcc \ -lc+tcc \
-o ''${out}/bin/ln \ -o ''${out}/bin/ln \
${src} ${src}

View file

@ -6,6 +6,12 @@
, m2libc , m2libc
, mescc-tools , mescc-tools
}: }:
# 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
let let
pname = "mes"; pname = "mes";
version = "0.24.2"; version = "0.24.2";
@ -28,26 +34,6 @@ let
# add symlink() to libc+tcc so we can use it in ln-boot # add symlink() to libc+tcc so we can use it in ln-boot
libc_tcc_SOURCES = sources.libc_tcc_SOURCES ++ [ "lib/linux/symlink.c" ]; 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
kaem.runCommand "${pname}-${version}" {
inherit pname version;
passthru = { inherit src nyacc; };
meta = with lib; { meta = with lib; {
description = "Scheme interpreter and C compiler for bootstrapping"; description = "Scheme interpreter and C compiler for bootstrapping";
homepage = "https://www.gnu.org/software/mes"; homepage = "https://www.gnu.org/software/mes";
@ -55,135 +41,199 @@ kaem.runCommand "${pname}-${version}" {
maintainers = with maintainers; [ emilytrau ]; maintainers = with maintainers; [ emilytrau ];
platforms = [ "i686-linux" ]; 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} srcPost = kaem.runCommand "${pname}-src-${version}" {
LIBDIR=''${MES_PREFIX}/lib outputs = [ "out" "bin" ];
inherit meta;
} ''
# Unpack source
ungz --file ${src} --output mes.tar
mkdir ''${out}
cd ''${out}
untar --non-strict --file ''${NIX_BUILD_TOP}/mes.tar # ignore symlinks
cd ''${MES_PREFIX} MES_PREFIX=''${out}/mes-${version}
cp ${config_h} include/mes/config.h cd ''${MES_PREFIX}
mkdir include/arch cp ${config_h} include/mes/config.h
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 mkdir include/arch
rm mes/module/mes/psyntax.pp mes/module/mes/psyntax.pp.header cp include/linux/x86/syscall.h include/arch/syscall.h
cp include/linux/x86/kernel-stat.h include/arch/kernel-stat.h
# These files are symlinked in the repo # Remove pregenerated files
cp mes/module/srfi/srfi-9-struct.mes mes/module/srfi/srfi-9.mes rm mes/module/mes/psyntax.pp mes/module/mes/psyntax.pp.header
cp mes/module/srfi/srfi-9/gnu-struct.mes mes/module/srfi/srfi-9/gnu.mes
# Fixes to support newer M2-Planet # These files are symlinked in the repo
catm x86_defs.M1 ${m2libc}/x86/x86_defs.M1 lib/m2/x86/x86_defs.M1 cp mes/module/srfi/srfi-9-struct.mes mes/module/srfi/srfi-9.mes
cp x86_defs.M1 lib/m2/x86/x86_defs.M1 cp mes/module/srfi/srfi-9/gnu-struct.mes mes/module/srfi/srfi-9/gnu.mes
rm x86_defs.M1
# Remove environment impurities # Fixes to support newer M2-Planet
__GUILE_LOAD_PATH="\"''${MES_PREFIX}/mes/module:''${MES_PREFIX}/module:${nyacc.guilePath}\"" catm x86_defs.M1 ${m2libc}/x86/x86_defs.M1 lib/m2/x86/x86_defs.M1
boot0_scm=mes/module/mes/boot-0.scm cp x86_defs.M1 lib/m2/x86/x86_defs.M1
guile_mes=mes/module/mes/guile.mes rm x86_defs.M1
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 # Remove environment impurities
replace --file ''${module_mescc_scm} --output ''${module_mescc_scm} --match-on "(getenv \"M1\")" --replace-with "\"${mescc-tools}/bin/M1\"" __GUILE_LOAD_PATH="\"''${MES_PREFIX}/mes/module:''${MES_PREFIX}/module:${nyacc.guilePath}\""
replace --file ''${module_mescc_scm} --output ''${module_mescc_scm} --match-on "(getenv \"HEX2\")" --replace-with "\"${mescc-tools}/bin/hex2\"" boot0_scm=mes/module/mes/boot-0.scm
replace --file ''${module_mescc_scm} --output ''${module_mescc_scm} --match-on "(getenv \"BLOOD_ELF\")" --replace-with "\"${mescc-tools}/bin/blood-elf\"" guile_mes=mes/module/mes/guile.mes
replace --file ''${module_mescc_scm} --output ''${module_mescc_scm} --match-on "(getenv \"srcdest\")" --replace-with "\"''${MES_PREFIX}\"" 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}
mes_c=src/mes.c module_mescc_scm=module/mescc/mescc.scm
replace --file ''${mes_c} --output ''${mes_c} --match-on "getenv (\"MES_PREFIX\")" --replace-with "\"''${MES_PREFIX}\"" replace --file ''${module_mescc_scm} --output ''${module_mescc_scm} --match-on "(getenv \"M1\")" --replace-with "\"${mescc-tools}/bin/M1\""
replace --file ''${mes_c} --output ''${mes_c} --match-on "getenv (\"srcdest\")" --replace-with "\"''${MES_PREFIX}\"" 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}\""
# Increase runtime resource limits mes_c=src/mes.c
gc_c=src/gc.c replace --file ''${mes_c} --output ''${mes_c} --match-on "getenv (\"MES_PREFIX\")" --replace-with "\"''${MES_PREFIX}\""
replace --file ''${gc_c} --output ''${gc_c} --match-on "getenv (\"MES_ARENA\")" --replace-with "\"100000000\"" replace --file ''${mes_c} --output ''${mes_c} --match-on "getenv (\"srcdest\")" --replace-with "\"''${MES_PREFIX}\""
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 # Increase runtime resource limits
mescc_in=scripts/mescc.scm.in gc_c=src/gc.c
replace --file ''${mescc_in} --output ''${mescc_in} --match-on "(getenv \"MES_PREFIX\")" --replace-with "\"''${MES_PREFIX}\"" replace --file ''${gc_c} --output ''${gc_c} --match-on "getenv (\"MES_ARENA\")" --replace-with "\"100000000\""
replace --file ''${mescc_in} --output ''${mescc_in} --match-on "(getenv \"includedir\")" --replace-with "\"''${MES_PREFIX}/include\"" replace --file ''${gc_c} --output ''${gc_c} --match-on "getenv (\"MES_MAX_ARENA\")" --replace-with "\"100000000\""
replace --file ''${mescc_in} --output ''${mescc_in} --match-on "(getenv \"libdir\")" --replace-with "\"''${MES_PREFIX}/lib\"" replace --file ''${gc_c} --output ''${gc_c} --match-on "getenv (\"MES_STACK\")" --replace-with "\"6000000\""
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 # Create mescc.scm
mes_cpu=x86 mescc_in=scripts/mescc.scm.in
stage0_cpu=x86 replace --file ''${mescc_in} --output ''${mescc_in} --match-on "(getenv \"MES_PREFIX\")" --replace-with "\"''${MES_PREFIX}\""
kaem --verbose --strict --file kaem.run replace --file ''${mescc_in} --output ''${mescc_in} --match-on "(getenv \"includedir\")" --replace-with "\"''${MES_PREFIX}/include\""
cp bin/mes-m2 ''${out}/bin/mes-m2 replace --file ''${mescc_in} --output ''${mescc_in} --match-on "(getenv \"libdir\")" --replace-with "\"''${MES_PREFIX}/lib\""
chmod 555 ''${out}/bin/mes-m2 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
mkdir -p ''${bin}/bin
cp ''${mescc_in} ''${bin}/bin/mescc.scm
# Build mes-m2
mes_cpu=x86
stage0_cpu=x86
kaem --verbose --strict --file kaem.run
cp bin/mes-m2 ''${bin}/bin/mes-m2
chmod 555 ''${bin}/bin/mes-m2
'';
srcPrefix = "${srcPost.out}/mes-${version}";
cc = "${srcPost.bin}/bin/mes-m2";
ccArgs = [
"-e" "main"
"${srcPost.bin}/bin/mescc.scm"
"--"
"-D" "HAVE_CONFIG_H=1"
"-I" "${srcPrefix}/include"
"-I" "${srcPrefix}/include/linux/x86"
];
CC = toString ([ cc ] ++ ccArgs);
stripExt = source:
lib.replaceStrings
[ ".c" ]
[ "" ]
(builtins.baseNameOf source);
compile = source: kaem.runCommand (stripExt source) {} ''
mkdir ''${out}
cd ''${out}
${CC} -c ${srcPrefix}/${source}
'';
crt1 = compile "/lib/linux/x86-mes-mescc/crt1.c";
getRes = suffix: res: "${res}/${res.name}${suffix}";
archive = out: sources:
"catm ${out} ${lib.concatMapStringsSep " " (getRes ".o") sources}";
sourceArchive = out: sources:
"catm ${out} ${lib.concatMapStringsSep " " (getRes ".s") sources}";
mkLib = libname: sources: let
os = map compile sources;
in kaem.runCommand "${pname}-${libname}-${version}" {
inherit meta;
} ''
LIBDIR=''${out}/lib
mkdir -p ''${LIBDIR}
cd ''${LIBDIR}
${archive "${libname}.a" os}
${sourceArchive "${libname}.s" os}
'';
libc-mini = mkLib "libc-mini" libc_mini_SOURCES;
libmescc = mkLib "libmescc" libmescc_SOURCES;
libc = mkLib "libc" libc_SOURCES;
libc_tcc = mkLib "libc+tcc" libc_tcc_SOURCES;
# Recompile Mes and Mes C library using mes-m2 bootstrapped Mes # Recompile Mes and Mes C library using mes-m2 bootstrapped Mes
cd ''${NIX_BUILD_TOP} libs = kaem.runCommand "${pname}-m2-libs-${version}" {
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" inherit pname version;
mkdir -p ''${LIBDIR}/x86-mes
# crt1.o passthru.tests.get-version = result: kaem.runCommand "${pname}-get-version-${version}" {} ''
CC -c ''${MES_PREFIX}/lib/linux/x86-mes-mescc/crt1.c ${result}/bin/mes --version
cp crt1.o ''${LIBDIR}/x86-mes mkdir ''${out}
cp crt1.s ''${LIBDIR}/x86-mes '';
# libc-mini.a inherit meta;
${compile libc_mini_SOURCES} }
${archive "libc-mini.a" libc_mini_SOURCES} ''
${sourceArchive "libc-mini.s" libc_mini_SOURCES} LIBDIR=''${out}/lib
cp libc-mini.a ''${LIBDIR}/x86-mes mkdir -p ''${out} ''${LIBDIR}
cp libc-mini.s ''${LIBDIR}/x86-mes
# libmescc.a mkdir -p ''${LIBDIR}/x86-mes
${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 # crt1.o
${compile libc_SOURCES} cp ${crt1}/crt1.o ''${LIBDIR}/x86-mes
${archive "libc.a" libc_SOURCES} cp ${crt1}/crt1.s ''${LIBDIR}/x86-mes
${sourceArchive "libc.s" libc_SOURCES}
cp libc.a ''${LIBDIR}/x86-mes
cp libc.s ''${LIBDIR}/x86-mes
# libc+tcc.a # libc-mini.a
# optimisation: don't recompile common libc sources cp ${libc-mini}/lib/libc-mini.a ''${LIBDIR}/x86-mes
${compile (lib.subtractLists libc_SOURCES libc_tcc_SOURCES)} cp ${libc-mini}/lib/libc-mini.s ''${LIBDIR}/x86-mes
${archive "libc+tcc.a" libc_tcc_SOURCES}
${sourceArchive "libc+tcc.s" libc_tcc_SOURCES} # libmescc.a
cp libc+tcc.a ''${LIBDIR}/x86-mes cp ${libmescc}/lib/libmescc.a ''${LIBDIR}/x86-mes
cp libc+tcc.s ''${LIBDIR}/x86-mes cp ${libmescc}/lib/libmescc.s ''${LIBDIR}/x86-mes
# libc.a
cp ${libc}/lib/libc.a ''${LIBDIR}/x86-mes
cp ${libc}/lib/libc.s ''${LIBDIR}/x86-mes
# libc+tcc.a
cp ${libc_tcc}/lib/libc+tcc.a ''${LIBDIR}/x86-mes
cp ${libc_tcc}/lib/libc+tcc.s ''${LIBDIR}/x86-mes
'';
# Build mes itself # Build mes itself
${compile mes_SOURCES} compiler = kaem.runCommand "${pname}-${version}" {
''${out}/bin/mes-m2 -e main ''${out}/bin/mescc.scm -- \ inherit pname version;
--base-address 0x08048000 \
-L ''${MES_PREFIX}/lib \
-L . \
-lc \
-lmescc \
-nostdlib \
-o ''${out}/bin/mes \
crt1.o \
${lib.concatMapStringsSep " " (replaceExt ".o") mes_SOURCES}
# Check passthru.tests.get-version = result: kaem.runCommand "${pname}-get-version-${version}" {} ''
''${out}/bin/mes --version ${result}/bin/mes --version
'' mkdir ''${out}
'';
inherit meta;
}
''
mkdir -p ''${out}/bin
${srcPost.bin}/bin/mes-m2 -e main ${srcPost.bin}/bin/mescc.scm -- \
--base-address 0x08048000 \
-L ''${srcPrefix}/lib \
-L ${libs}/lib \
-lc \
-lmescc \
-nostdlib \
-o ''${out}/bin/mes \
${libs}/lib/x86-mes/crt1.o \
${lib.concatMapStringsSep " " (getRes ".o") (map compile mes_SOURCES)}
'';
in {
inherit srcPost srcPrefix nyacc;
inherit compiler libs;
}

View file

@ -6,13 +6,11 @@
}: }:
let let
pname = "mes-libc"; pname = "mes-libc";
inherit (mes) version; inherit (mes.compiler) version;
sources = (import ./sources.nix).x86.linux.gcc; sources = (import ./sources.nix).x86.linux.gcc;
inherit (sources) libtcc1_SOURCES libc_gnu_SOURCES; inherit (sources) libtcc1_SOURCES libc_gnu_SOURCES;
prefix = "${mes}/share/mes-${version}";
# Concatenate all source files into a convenient bundle # Concatenate all source files into a convenient bundle
# "gcc" variants of source files (eg. "lib/linux/x86-mes-gcc") can also be # "gcc" variants of source files (eg. "lib/linux/x86-mes-gcc") can also be
# compiled by tinycc # compiled by tinycc
@ -37,11 +35,10 @@ kaem.runCommand "${pname}-${version}" {
platforms = [ "i686-linux" ]; platforms = [ "i686-linux" ];
}; };
} '' } ''
cd ${prefix} cd ${mes.srcPrefix}
# mescc compiled libc.a # mescc compiled libc.a
mkdir -p ''${out}/lib/x86-mes mkdir -p ''${out}/lib/x86-mes
cp lib/x86-mes/libc.a ''${out}/lib/x86-mes
# libc.c # libc.c
catm ''${TMPDIR}/first.c ${lib.concatStringsSep " " firstLibc} catm ''${TMPDIR}/first.c ${lib.concatStringsSep " " firstLibc}
@ -59,5 +56,5 @@ kaem.runCommand "${pname}-${version}" {
cp lib/posix/getopt.c ''${out}/lib/libgetopt.c cp lib/posix/getopt.c ''${out}/lib/libgetopt.c
# Install headers # Install headers
ln -s ${prefix}/include ''${out}/include ln -s ${mes.srcPrefix}/include ''${out}/include
'' ''

View file

@ -12,7 +12,9 @@ lib.makeScope newScope (self: with self; {
kaem = callPackage ./kaem { }; kaem = callPackage ./kaem { };
kaem-minimal = callPackage ./kaem/minimal.nix { }; kaem-minimal = callPackage ./kaem/minimal.nix { };
inherit (callPackage ./stage0-posix-x86.nix { }) blood-elf-0 hex2 kaem-unwrapped M1 M2; stage0-posix-x86 = callPackage ./stage0-posix-x86.nix { };
inherit (self.stage0-posix-x86) blood-elf-0 hex2 kaem-unwrapped M1 M2;
mescc-tools = callPackage ./mescc-tools { }; mescc-tools = callPackage ./mescc-tools { };

View file

@ -15,7 +15,7 @@
, mes-libc , mes-libc
}: }:
let let
inherit (callPackage ./common.nix { }) buildTinyccMes; inherit (callPackage ./common.nix { }) buildTinyccMes recompileLibc;
version = "unstable-2023-04-20"; version = "unstable-2023-04-20";
rev = "80114c4da6b17fbaabb399cc29f427e368309bc8"; rev = "80114c4da6b17fbaabb399cc29f427e368309bc8";
@ -39,50 +39,54 @@ let
platforms = [ "i686-linux" ]; platforms = [ "i686-linux" ];
}; };
tinycc-boot-mes = kaem.runCommand "tinycc-boot-mes-${version}" {} '' pname = "tinycc-boot-mes";
catm config.h
${mes}/bin/mes --no-auto-compile -e main ${mes}/bin/mescc.scm -- \
-S \
-o tcc.s \
-I . \
-D BOOTSTRAP=1 \
-I ${src} \
-D TCC_TARGET_I386=1 \
-D inline= \
-D CONFIG_TCCDIR=\"''${out}/lib\" \
-D CONFIG_SYSROOT=\"\" \
-D CONFIG_TCC_CRTPREFIX=\"''${out}/lib\" \
-D CONFIG_TCC_ELFINTERP=\"/mes/loader\" \
-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 \
-D CONFIG_USE_LIBGCC=1 \
-D TCC_MES_LIBC=1 \
-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
''${out}/bin/tcc -version tinycc-boot-mes = rec {
compiler = kaem.runCommand "${pname}-${version}" {
passthru.tests.get-version = result: kaem.runCommand "${pname}-get-version-${version}" {} ''
${result}/bin/tcc -version
mkdir ''${out}
'';
} ''
catm config.h
${mes.compiler}/bin/mes --no-auto-compile -e main ${mes.srcPost.bin}/bin/mescc.scm -- \
-S \
-o tcc.s \
-I . \
-D BOOTSTRAP=1 \
-I ${src} \
-D TCC_TARGET_I386=1 \
-D inline= \
-D CONFIG_TCCDIR=\"\" \
-D CONFIG_SYSROOT=\"\" \
-D CONFIG_TCC_CRTPREFIX=\"{B}\" \
-D CONFIG_TCC_ELFINTERP=\"/mes/loader\" \
-D CONFIG_TCC_LIBPATHS=\"{B}\" \
-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 \
-D CONFIG_USE_LIBGCC=1 \
-D TCC_MES_LIBC=1 \
-D TCC_VERSION=\"${version}\" \
-D ONE_SOURCE=1 \
${src}/tcc.c
mkdir -p ''${out}/bin
${mes.compiler}/bin/mes --no-auto-compile -e main ${mes.srcPost.bin}/bin/mescc.scm -- \
-L ${mes.libs}/lib \
-l c+tcc \
-o ''${out}/bin/tcc \
tcc.s
'';
# Recompile libc: crt{1,n,i}, libtcc.a, libc.a, libgetopt.a libs = recompileLibc {
mkdir -p ''${out}/lib inherit pname version;
''${out}/bin/tcc ${mes-libc.CFLAGS} -c -o ''${out}/lib/crt1.o ${mes-libc}/lib/crt1.c tcc = compiler;
''${out}/bin/tcc ${mes-libc.CFLAGS} -c -o ''${out}/lib/crtn.o ${mes-libc}/lib/crtn.c src = mes-libc;
''${out}/bin/tcc ${mes-libc.CFLAGS} -c -o ''${out}/lib/crti.o ${mes-libc}/lib/crti.c libtccOptions = mes-libc.CFLAGS;
''${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 libgetopt.o ${mes-libc}/lib/libgetopt.c
''${out}/bin/tcc -ar cr ''${out}/lib/libgetopt.a libgetopt.o
'';
# Bootstrap stage build flags obtained from # Bootstrap stage build flags obtained from
# https://gitlab.com/janneke/tinycc/-/blob/80114c4da6b17fbaabb399cc29f427e368309bc8/boot.sh # https://gitlab.com/janneke/tinycc/-/blob/80114c4da6b17fbaabb399cc29f427e368309bc8/boot.sh

View file

@ -3,67 +3,105 @@
, mes-libc , mes-libc
, ln-boot , ln-boot
}: }:
{
buildTinyccMes = { rec {
pname,
version, # Recompile libc: crt{1,n,i}, libtcc.a, libc.a, libgetopt.a
src, recompileLibc =
prev, { tcc
buildOptions, , pname
libtccBuildOptions, , version
meta , src
}: , libtccOptions
}:
let
crt = kaem.runCommand "crt" {} ''
mkdir -p ''${out}/lib
${tcc}/bin/tcc ${mes-libc.CFLAGS} -c -o ''${out}/lib/crt1.o ${mes-libc}/lib/crt1.c
${tcc}/bin/tcc ${mes-libc.CFLAGS} -c -o ''${out}/lib/crtn.o ${mes-libc}/lib/crtn.c
${tcc}/bin/tcc ${mes-libc.CFLAGS} -c -o ''${out}/lib/crti.o ${mes-libc}/lib/crti.c
'';
library = lib: options: source: kaem.runCommand "${lib}.a" {} ''
${tcc}/bin/tcc ${options} -c -o ${lib}.o ${source}
${tcc}/bin/tcc -ar cr ''${out} ${lib}.o
'';
libtcc1 = library "libtcc1" libtccOptions "${src}/lib/libtcc1.c";
libc = library "libc" mes-libc.CFLAGS "${mes-libc}/lib/libc.c";
libgetopt = library "libgetopt" mes-libc.CFLAGS "${mes-libc}/lib/libgetopt.c";
in
kaem.runCommand "${pname}-libs-${version}" {} ''
mkdir -p ''${out}/lib
cp ${crt}/lib/crt1.o ''${out}/lib
cp ${crt}/lib/crtn.o ''${out}/lib
cp ${crt}/lib/crti.o ''${out}/lib
cp ${libtcc1} ''${out}/lib/libtcc1.a
cp ${libc} ''${out}/lib/libc.a
cp ${libgetopt} ''${out}/lib/libgetopt.a
'';
buildTinyccMes =
{ pname
, version
, src
, prev
, buildOptions
, libtccBuildOptions
, meta
}:
let let
options = lib.strings.concatStringsSep " " buildOptions; options = lib.strings.concatStringsSep " " buildOptions;
libtccOptions = lib.strings.concatStringsSep " " libtccBuildOptions; libtccOptions = lib.strings.concatStringsSep " "
in (["-c" "-D" "TCC_TARGET_I386=1" ] ++ libtccBuildOptions);
kaem.runCommand "${pname}-${version}" { compiler = kaem.runCommand "${pname}-${version}" {
inherit pname version meta; inherit pname version meta;
nativeBuildInputs = [ ln-boot ]; nativeBuildInputs = [ ln-boot ];
} '' passthru.tests = rec {
catm config.h get-version = result: kaem.runCommand "${pname}-get-version-${version}" {} ''
mkdir -p ''${out}/bin ${result}/bin/tcc -version
${prev}/bin/tcc \ mkdir ''${out}
-g \ '';
-v \ chain = result: kaem.runCommand "${pname}-chain-${version}" {} ''
-static \ echo ${prev.compiler.tests.chain or prev.compiler.tests.get-version};
-o ''${out}/bin/tcc \ ${result}/bin/tcc -version
-D BOOTSTRAP=1 \ mkdir ''${out}
${options} \ '';
-I . \ };
-I ${src} \ } ''
-D TCC_TARGET_I386=1 \ catm config.h
-D CONFIG_TCCDIR=\"''${out}/lib\" \ mkdir -p ''${out}/bin
-D CONFIG_TCC_CRTPREFIX=\"''${out}/lib\" \ ${prev.compiler}/bin/tcc \
-D CONFIG_TCC_ELFINTERP=\"\" \ -B ${prev.libs}/lib \
-D CONFIG_TCC_LIBPATHS=\"''${out}/lib\" \ -g \
-D CONFIG_TCC_SYSINCLUDEPATHS=\"${mes-libc}/include:${src}/include\" \ -v \
-D TCC_LIBGCC=\"libc.a\" \ -static \
-D TCC_LIBTCC1=\"libtcc1.a\" \ -o ''${out}/bin/tcc \
-D CONFIG_TCCBOOT=1 \ -D BOOTSTRAP=1 \
-D CONFIG_TCC_STATIC=1 \ ${options} \
-D CONFIG_USE_LIBGCC=1 \ -I . \
-D TCC_MES_LIBC=1 \ -I ${src} \
-D TCC_VERSION=\"${version}\" \ -D TCC_TARGET_I386=1 \
-D ONE_SOURCE=1 \ -D CONFIG_TCCDIR=\"\" \
-L ${prev}/lib \ -D CONFIG_SYSROOT=\"\" \
${src}/tcc.c -D CONFIG_TCC_CRTPREFIX=\"{B}\" \
-D CONFIG_TCC_ELFINTERP=\"\" \
''${out}/bin/tcc -v -D CONFIG_TCC_LIBPATHS=\"{B}\" \
-D CONFIG_TCC_SYSINCLUDEPATHS=\"${mes-libc}/include\" \
# Recompile libc: crt{1,n,i}, libtcc.a, libc.a, libgetopt.a -D TCC_LIBGCC=\"libc.a\" \
mkdir -p ''${out}/lib -D TCC_LIBTCC1=\"libtcc1.a\" \
''${out}/bin/tcc ${mes-libc.CFLAGS} -c -o ''${out}/lib/crt1.o ${mes-libc}/lib/crt1.c -D CONFIG_TCCBOOT=1 \
''${out}/bin/tcc ${mes-libc.CFLAGS} -c -o ''${out}/lib/crtn.o ${mes-libc}/lib/crtn.c -D CONFIG_TCC_STATIC=1 \
''${out}/bin/tcc ${mes-libc.CFLAGS} -c -o ''${out}/lib/crti.o ${mes-libc}/lib/crti.c -D CONFIG_USE_LIBGCC=1 \
''${out}/bin/tcc -c -D TCC_TARGET_I386=1 ${libtccOptions} -o libtcc1.o ${src}/lib/libtcc1.c -D TCC_MES_LIBC=1 \
''${out}/bin/tcc -ar cr ''${out}/lib/libtcc1.a libtcc1.o -D TCC_VERSION=\"${version}\" \
''${out}/bin/tcc ${mes-libc.CFLAGS} -c -o libc.o ${mes-libc}/lib/libc.c -D ONE_SOURCE=1 \
''${out}/bin/tcc -ar cr ''${out}/lib/libc.a libc.o ${src}/tcc.c
''${out}/bin/tcc ${mes-libc.CFLAGS} -c -o libgetopt.o ${mes-libc}/lib/libgetopt.c '';
''${out}/bin/tcc -ar cr ''${out}/lib/libgetopt.a libgetopt.o libs = recompileLibc {
inherit pname version src libtccOptions;
# Install headers tcc = compiler;
ln -s ${mes-libc}/include ''${out}/include };
''; in { inherit prev compiler libs; };
} }

View file

@ -37,7 +37,12 @@ let
tccdefs = kaem.runCommand "tccdefs-${version}" {} '' tccdefs = kaem.runCommand "tccdefs-${version}" {} ''
mkdir ''${out} mkdir ''${out}
${tinycc-bootstrappable}/bin/tcc -static -DC2STR -o c2str ${src}/conftest.c ${tinycc-bootstrappable.compiler}/bin/tcc \
-B ${tinycc-bootstrappable.libs}/lib \
-static \
-DC2STR \
-o c2str \
${src}/conftest.c
./c2str ${src}/include/tccdefs.h ''${out}/tccdefs_.h ./c2str ${src}/include/tccdefs.h ''${out}/tccdefs_.h
''; '';

View file

@ -18,14 +18,18 @@ rec {
passthru = attrs.passthru or {}; passthru = attrs.passthru or {};
validity = checkMeta.assertValidity { inherit meta attrs; }; validity = checkMeta.assertValidity { inherit meta attrs; };
meta = checkMeta.commonMeta { inherit validity attrs; }; meta = checkMeta.commonMeta { inherit validity attrs; };
baseDrv = derivation ({
inherit (buildPlatform) system;
inherit (meta) name;
} // (builtins.removeAttrs attrs [ "meta" "passthru" ]));
passthru' = passthru // lib.optionalAttrs (passthru ? tests) {
tests = lib.mapAttrs (_: f: f baseDrv) passthru.tests;
};
in in
lib.extendDerivation lib.extendDerivation
validity.handled validity.handled
({ inherit meta passthru; } // passthru) ({ inherit meta; passthru = passthru'; } // passthru')
(derivation ({ baseDrv;
inherit (buildPlatform) system;
inherit (meta) name;
} // (builtins.removeAttrs attrs [ "meta" "passthru" ])));
writeTextFile = writeTextFile =
{ name # the name of the derivation { name # the name of the derivation
@ -36,7 +40,7 @@ rec {
, preferLocalBuild ? true , preferLocalBuild ? true
}: }:
derivationWithMeta { derivationWithMeta {
inherit name text executable allowSubstitutes preferLocalBuild; inherit name text allowSubstitutes preferLocalBuild;
passAsFile = [ "text" ]; passAsFile = [ "text" ];
builder = "${kaem}/bin/kaem"; builder = "${kaem}/bin/kaem";
@ -44,20 +48,18 @@ rec {
"--verbose" "--verbose"
"--strict" "--strict"
"--file" "--file"
(builtins.toFile "write-text-file.kaem" '' (builtins.toFile "write-text-file.kaem" (''
target=''${out}''${destination} target=''${out}''${destination}
if match x''${mkdirDestination} x1; then '' + lib.optionalString (builtins.dirOf destination == ".") ''
mkdir -p ''${out}''${destinationDir} mkdir -p ''${out}''${destinationDir}
fi '' + ''
cp ''${textPath} ''${target} cp ''${textPath} ''${target}
if match x''${executable} x1; then '' + lib.optionalString executable ''
chmod 555 ''${target} chmod 555 ''${target}
fi ''))
'')
]; ];
PATH = lib.makeBinPath [ mescc-tools-extra ]; PATH = lib.makeBinPath [ mescc-tools-extra ];
mkdirDestination = if builtins.dirOf destination == "." then "0" else "1";
destinationDir = builtins.dirOf destination; destinationDir = builtins.dirOf destination;
inherit destination; inherit destination;
}; };