Files
makronix/mobile/tools/mktargetlibs.sh
T
lotherkandClaude Sonnet 5.5 73c9de51a6 licence: the CNCL line in every file Makronix wrote (tools, docs, www, mobile, top level)
consmirror.c: the BSD notice is replaced (Makronix's own file).  Files with
changes of other sessions pending are left for their commit.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-14 11:50:26 +00:00

148 lines
6.7 KiB
Bash

#!/bin/sh
# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
#
# The second half of the phone's toolchain (docs/NETBSD-USERLAND.md, phase
# T): what build.sh tools leaves out because NetBSD does it differently, done
# in the gcc object directory tools/mktooldir.sh left behind.
#
# Usage: mktargetlibs.sh DEVICEDIR (make targetlibs in mobile/)
#
# 1. gcc's own headers. NetBSD's gcc does not copy them: its
# gcc/Makefile.in:3308 reads "if false; then" around the USER_H copy
# (float.h, stdarg.h, stddef.h ... and the aarch64 intrinsics), because
# NetBSD's libc ships those headers in /usr/include. glibc does not, so
# the copy is done here -- the same files, the same steps as that block:
# USER_H, stdint.h by USE_GCC_STDINT, limits.h from limitx.h + glimits.h
# + limity.h when the sysroot has a limits.h, syslimits.h. The lists come
# from gcc's own Makefile in the object directory, not from this script.
# 2. libgcc and libstdc++. build.sh tools builds only the compiler
# (src/tools/gcc/Makefile: ALL_TARGET= all-gcc); NetBSD builds its target
# libraries later from mknative-generated NetBSD Makefiles
# (src/external/gpl3/gcc/lib), which exist for NetBSD targets only. For
# Linux they are built by gcc's own top-level Makefile, against the sysroot
# (the tooldir's gcc has no built-in sysroot: TARGET_SYSTEM_ROOT=0 in
# src/tools/gcc/Makefile, as NetBSD always passes --sysroot).
# 3. Install both into the tooldir.
set -eu
. "$(dirname "$0")/lib.sh"
[ $# -eq 1 ] || { echo "usage: $0 DEVICEDIR" >&2; exit 2; }
load_device "$1"
dev=$(basename "$DEVDIR")
target=aarch64-linux-gnu
tooldir=$(ls -d "$ROOT"/build/tooldir.*-$target 2>/dev/null | head -n 1)
[ -n "$tooldir" ] && [ -x "$tooldir/bin/$target-gcc" ] || die "no tooldir for $target (make tooldir)"
obj=$ROOT/build/tools-obj/tools/gcc/build
[ -f "$obj/gcc/Makefile" ] || die "no gcc object directory $obj (make tooldir)"
# Makronix/Linux's own sysroot (our glibc, make glibc) once it exists -- the
# order of LFS too: gcc's runtime libraries after the C library; before
# that, the Void-made one it was first built against.
sysroot=$ROOT/build/mxsysroot-$dev
[ -f "$sysroot/usr/lib/libc.so.6" ] || sysroot=$ROOT/build/sysroot-$dev
echo "mktargetlibs: against $sysroot"
gm=$tooldir/bin/nbgmake
# the value of a variable of gcc's own Makefile
gccvar() {
printf 'include Makefile\nmxpv:\n\t@echo $(%s)\n' "$1" > "$obj/gcc/mxpv.mk"
(cd "$obj/gcc" && "$gm" -s -f mxpv.mk mxpv)
rm -f "$obj/gcc/mxpv.mk"
}
# 1. headers, into gcc/include of the object directory, where xgcc reads
# them while it builds libgcc, then into the tooldir
inc=$obj/gcc/include
mkdir -p "$inc"
srcdir=$(gccvar srcdir)
for f in $(gccvar USER_H); do
cp "$f" "$inc/"
done
case $(gccvar USE_GCC_STDINT) in
wrap) cp "$srcdir/ginclude/stdint-gcc.h" "$inc/stdint-gcc.h"
cp "$srcdir/ginclude/stdint-wrap.h" "$inc/stdint.h" ;;
provide)
cp "$srcdir/ginclude/stdint-gcc.h" "$inc/stdint.h" ;;
esac
if [ -f "$sysroot/usr/include/limits.h" ]; then
cat "$srcdir/limitx.h" "$srcdir/glimits.h" "$srcdir/limity.h" > "$inc/limits.h"
else
cp "$srcdir/glimits.h" "$inc/limits.h"
fi
cp "$srcdir/gsyslimits.h" "$inc/syslimits.h"
gccinc=$tooldir/lib/gcc/$target/$(cat "$srcdir/BASE-VER")/include
mkdir -p "$gccinc"
cp "$inc"/* "$gccinc/"
echo "gcc headers: $(ls "$gccinc" | wc -l) in $gccinc"
# 2. and 3. libgcc, libstdc++
# The tooldir's gcc is built with -DNETBSD_TOOLS, which drops the sysroot's
# /lib and /usr/lib from its startfile search (gcc.cc:8605,8615); NetBSD's
# build always names them, src/tools/gcc/Makefile:162:
# CCADDFLAGS= --sysroot=${DESTDIR} -L${DESTDIR}/lib -L${DESTDIR}/usr/lib -B${DESTDIR}/usr/lib/
# The same here (without it: "ld: cannot find crti.o", 2026-11-11).
sr="--sysroot=$sysroot -L$sysroot/lib -L$sysroot/usr/lib -B$sysroot/usr/lib/"
# libstdc++'s locale model: "generic". NetBSD's libstdc++ headers declare
# an explicit specialization __timepunct<wchar_t>::~__timepunct()
# (include/bits/locale_facets_nonio.h:339) that the dragonfly model NetBSD
# itself uses and the generic model define (generic/time_members.cc:209) --
# glibc's own "gnu" model does not, and a libstdc++.so built with it fails at
# run time: "undefined symbol: _ZNSt11__timepunctIwED1Ev" (2026-11-11).
# dragonfly needs <xlocale.h> and the BSD *_l functions (localeconv_l,
# mbrtowc_l, vsnprintf_l ...) that glibc lacks. So: generic -- C++ locales
# know only "C". A finding, not a change of src/.
topvar() {
printf 'include Makefile\nmxpv:\n\t@echo $(%s)\n' "$1" > "$obj/mxpv.mk"
(cd "$obj" && "$gm" -s -f mxpv.mk mxpv)
rm -f "$obj/mxpv.mk"
}
cd "$obj"
tca="$(topvar TARGET_CONFIGARGS) --enable-clocale=generic"
"$gm" -j "$(nproc)" all-target-libgcc configure-target-libstdc++-v3 \
CFLAGS_FOR_TARGET="-O2 -g $sr" CXXFLAGS_FOR_TARGET="-O2 -g $sr" \
LDFLAGS_FOR_TARGET="$sr" TARGET_CONFIGARGS="$tca"
"$gm" install-target-libgcc
grep -q "enable-clocale=generic" "$obj/$target/libstdc++-v3/config.status" ||
die "libstdc++ is configured without --enable-clocale=generic; remove $obj/$target/libstdc++-v3 and run again"
# libstdc++ by its subdirectories. NetBSD's import is pruned for its own
# Makefiles (src/external/gpl3/gcc/lib/libstdc++-v3): its
# libstdc++-v3/Makefile.in:424 has "SUBDIRS = include libsupc++
# $(hosted_source)" without the "src" of Makefile.am:30 -- the library
# itself -- and hosted_source names "po", which the import does not carry.
# So the directories of Makefile.am, in its order, without po; a SUBDIRS=
# on the command line would reach src's own sub-makes too.
lib=$obj/$target/libstdc++-v3
# The include paths NetBSD gives three files of src
# (src/external/gpl3/gcc/lib/libstdc++-v3/Makefile:127-129,
# CPPFLAGS.compatibility.cc and the two cow-string-inst.cc), for the whole of
# src here: GNU make takes no per-file flags from the command line.
lsrc=$srcdir/../libstdc++-v3
srcflags="-I$lsrc/config -I$lsrc/src/c++11 -I$lsrc/src/c++17"
for d in include libsupc++ src python; do
if [ "$d" = src ]; then
"$gm" -C "$lib/$d" -j "$(nproc)" all CPPFLAGS="$srcflags"
else
"$gm" -C "$lib/$d" -j "$(nproc)" all
fi
done
for d in include libsupc++ src python; do
"$gm" -C "$lib/$d" install
done
echo "target libraries installed into $tooldir"
# The runtime halves into Makronix/Linux's sysroot, /usr/lib, where NetBSD
# installs its own libgcc_s and libstdc++ (src/external/gpl3/gcc/lib): a
# program of src/ built by the normal build NEEDs libgcc_s.so.1, and the
# sysroot is that build's DESTDIR. Only into mxsysroot, never the old one.
case $sysroot in
*/mxsysroot-*)
rt=$tooldir/$target/lib64
for f in libgcc_s.so.1 libstdc++.so.6.0.33; do
install -m 444 "$rt/$f" "$sysroot/usr/lib/$f"
done
ln -sf libstdc++.so.6.0.33 "$sysroot/usr/lib/libstdc++.so.6"
echo "runtime libraries installed into $sysroot/usr/lib"
;;
esac