minimal-bootstrap: only create one derivation for mes sources (#519568)

This commit is contained in:
dish
2026-06-14 17:18:14 +00:00
committed by GitHub
5 changed files with 112 additions and 2360 deletions
@@ -1,3 +1,8 @@
let
sourcesJson = (builtins.fromJSON (builtins.readFile ./sources.json));
inherit (builtins) split filter isString;
in
{
lib,
fetchurl,
@@ -57,7 +62,7 @@ let
}
.${buildPlatform.system};
sources = (lib.importJSON ./sources.json).${arch}.linux.mescc;
sources = sourcesJson."${arch}.linux.mescc";
inherit (sources)
libc_mini_SOURCES
@@ -67,7 +72,7 @@ let
;
# 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";
setjmp_x86_64 = ./setjmp_x86_64.c;
meta = {
@@ -254,39 +259,31 @@ let
CC = toString ([ cc ] ++ ccArgs);
stripExt = source: lib.replaceStrings [ ".c" ] [ "" ] (baseNameOf source);
compile =
source:
kaem.runCommand (stripExt source) { } ''
mkdir ''${out}
cd ''${out}
${CC} -c ${srcPrefix}/${source}
'';
crt1 = compile "/lib/linux/${arch}-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}";
crt1 =
kaem.runCommand "${pname}-crt1-${version}"
{
inherit meta;
}
''
mkdir ''${out}
cd ''${out}
${CC} -c ${srcPrefix}/lib/linux/${arch}-mes-mescc/crt1.c
'';
mkLib =
libname: sources:
let
os = map compile sources;
in
kaem.runCommand "${pname}-${libname}-${version}"
{
inherit meta;
}
''
cd ${srcPost}/mes-${version}
LIBDIR=''${out}/lib
mkdir -p ''${LIBDIR}
cd ''${LIBDIR}
catm ''${LIBDIR}/${libname}.c ${sources}
${archive "${libname}.a" os}
${sourceArchive "${libname}.s" os}
${CC} -S -o ''${LIBDIR}/${libname}.s ''${out}/lib/${libname}.c
${CC} -c -o ''${LIBDIR}/${libname}.a ''${LIBDIR}/${libname}.s
'';
libc-mini = mkLib "libc-mini" libc_mini_SOURCES;
@@ -338,6 +335,9 @@ let
# Build mes itself
compiler =
let
objs = filter isString (split " " mes_SOURCES);
in
kaem.runCommand "${pname}-${version}"
{
inherit pname version;
@@ -353,8 +353,19 @@ let
}
''
mkdir -p ''${out}/bin
${lib.concatMapStringsSep "\n" (obj: ''
${srcPost.bin}/bin/mes-m2 -e main ${srcPost.bin}/bin/mescc.scm -- \
-D HAVE_CONFIG_H=1 \
-I ${srcPrefix}/include \
-I ${srcPrefix}/include/linux/${arch} \
-I ${srcPost}/mes-${version}/src \
-c \
-o ${lib.removePrefix "src/" obj}.o \
${srcPost}/mes-${version}/${obj}
'') objs}
${srcPost.bin}/bin/mes-m2 -e main ${srcPost.bin}/bin/mescc.scm -- \
-I ${srcPost}/mes-${version}/src \
-L ''${srcPrefix}/lib \
-L ${libs}/lib \
-lc \
@@ -362,7 +373,7 @@ let
-nostdlib \
-o ''${out}/bin/mes \
${libs}/lib/${arch}-mes/crt1.o \
${lib.concatMapStringsSep " " (getRes ".o") (map compile mes_SOURCES)}
${lib.concatMapStringsSep " " (obj: "${lib.removePrefix "src/" obj}.o") objs}
'';
in
{
@@ -17,13 +17,15 @@ ARCHS="x86 x86_64"
KERNELS="linux"
COMPILERS="mescc gcc"
to_json_joined_string() {
printf '%s\n' "$@" | jq --raw-input --null-input '[inputs]|join(" ")'
}
to_json_array() {
if [ $# -eq 0 ]; then
echo '[]'
else
printf '%s\n' "$@" | jq --raw-input --null-input '[inputs]'
fi
to_json_joined_string_1() {
printf '%s\n' "$@" | jq --raw-input --null-input '[inputs]|.[0:100]|join(" ")'
}
to_json_joined_string_2() {
printf '%s\n' "$@" | jq --raw-input --null-input '[inputs]|.[100:]|join(" ")'
}
gen_sources() {
@@ -38,20 +40,22 @@ gen_sources() {
jq --null-input \
--arg key "$mes_cpu.$mes_kernel.$compiler" \
--argjson libc_mini_SOURCES "$(to_json_array $libc_mini_SOURCES)" \
--argjson libmescc_SOURCES "$(to_json_array $libmescc_SOURCES)" \
--argjson libtcc1_SOURCES "$(to_json_array $libtcc1_SOURCES)" \
--argjson libc_SOURCES "$(to_json_array $libc_SOURCES)" \
--argjson libc_tcc_SOURCES "$(to_json_array $libc_tcc_SOURCES)" \
--argjson libc_gnu_SOURCES "$(to_json_array $libc_gnu_SOURCES)" \
--argjson mes_SOURCES "$(to_json_array $mes_SOURCES)" \
--argjson libc_mini_SOURCES "$(to_json_joined_string $libc_mini_SOURCES)" \
--argjson libmescc_SOURCES "$(to_json_joined_string $libmescc_SOURCES)" \
--argjson libtcc1_SOURCES "$(to_json_joined_string $libtcc1_SOURCES)" \
--argjson libc_SOURCES "$(to_json_joined_string $libc_SOURCES)" \
--argjson libc_tcc_SOURCES "$(to_json_joined_string $libc_tcc_SOURCES)" \
--argjson libc_gnu1_SOURCES "$(to_json_joined_string_1 $libc_gnu_SOURCES)" \
--argjson libc_gnu2_SOURCES "$(to_json_joined_string_2 $libc_gnu_SOURCES)" \
--argjson mes_SOURCES "$(to_json_joined_string $mes_SOURCES)" \
'{($key): {
libc_mini_SOURCES: $libc_mini_SOURCES,
libmescc_SOURCES: $libmescc_SOURCES,
libtcc1_SOURCES: $libtcc1_SOURCES,
libc_SOURCES: $libc_SOURCES,
libc_tcc_SOURCES: $libc_tcc_SOURCES,
libc_gnu_SOURCES: $libc_gnu_SOURCES,
libc_gnu1_SOURCES: $libc_gnu1_SOURCES,
libc_gnu2_SOURCES: $libc_gnu2_SOURCES,
mes_SOURCES: $mes_SOURCES
}}'
}
@@ -1,9 +1,12 @@
let
sourcesJson = (builtins.fromJSON (builtins.readFile ./sources.json));
in
{
lib,
kaem,
ln-boot,
mes,
mes-libc,
buildPlatform,
fetchurl,
}:
@@ -18,8 +21,13 @@ let
}
.${buildPlatform.system};
sources = (lib.importJSON ./sources.json).${arch}.linux.gcc;
inherit (sources) libtcc1_SOURCES libc_gnu_SOURCES;
sources = sourcesJson."${arch}.linux.gcc";
inherit (sources) libtcc1_SOURCES libc_gnu1_SOURCES libc_gnu2_SOURCES;
ldexpl = fetchurl {
url = "https://gitlab.com/janneke/mes/-/raw/c837abed8edb341d4e56913729fbe9803b4de47c/lib/math/ldexpl.c";
hash = "sha256-3QoFZZIqVmlMUosEqOdYIMEHzYgQ7GJ7Hz0Bf/1iIig=";
};
# Concatenate all source files into a convenient bundle
# "gcc" variants of source files (eg. "lib/linux/x86-mes-gcc") can also be
@@ -27,14 +35,14 @@ let
#
# Passing this many arguments is too much for kaem so we need to split
# the operation in two
firstLibc = (lib.take 100 libc_gnu_SOURCES) ++ [ ./ldexpl.c ];
lastLibc = lib.drop 100 libc_gnu_SOURCES;
firstLibc = libc_gnu1_SOURCES + " ${ldexpl}";
lastLibc = libc_gnu2_SOURCES;
in
kaem.runCommand "${pname}-${version}"
{
inherit pname version;
nativeBuildInputs = [ ln-boot ];
extraPath = "${ln-boot}/bin";
passthru.CFLAGS = "-std=c11";
@@ -56,8 +64,8 @@ kaem.runCommand "${pname}-${version}"
mkdir -p ''${out}/lib/${arch}-mes
# libc.c
catm ''${TMPDIR}/first.c ${lib.concatStringsSep " " firstLibc}
catm ''${out}/lib/libc.c ''${TMPDIR}/first.c ${lib.concatStringsSep " " lastLibc}
catm ''${TMPDIR}/first.c ${firstLibc}
catm ''${out}/lib/libc.c ''${TMPDIR}/first.c ${lastLibc}
# crt{1,n,i}.c
cp lib/linux/${arch}-mes-gcc/crt1.c ''${out}/lib
@@ -65,11 +73,11 @@ kaem.runCommand "${pname}-${version}"
cp lib/linux/${arch}-mes-gcc/crti.c ''${out}/lib
# libtcc1.c
catm ''${out}/lib/libtcc1.c ${lib.concatStringsSep " " libtcc1_SOURCES}
catm ''${out}/lib/libtcc1.c ${libtcc1_SOURCES}
# getopt.c
cp lib/posix/getopt.c ''${out}/lib/libgetopt.c
# Install headers
ln -s ${mes.srcPrefix}/include ''${out}/include
${ln-boot}/bin/ln -s ${mes.srcPrefix}/include ''${out}/include
''
File diff suppressed because it is too large Load Diff
@@ -28,7 +28,8 @@ rec {
baseDrv = derivation (
{
inherit (buildPlatform) system;
inherit (meta) name;
# redefining from meta to avoid forcing the thunk until it's used
name = attrs.name or "${attrs.pname}-${attrs.version}";
}
// maybeContentAddressed
// (removeAttrs attrs [