161.15. libstdcxx-riscv64-unknown-elf-16.2.0
The C++ standard library for riscv64-unknown-elf, built against picolibc
| Version | 16.2.0, release 1 |
| Shelf | embedded — Microcontroller and bare-metal development: cross toolchains, C libraries, flashers, debug probes and serial consoles |
| Phase | 41_system (System), 473 of 1047 in build order |
| Licence | GPL-3.0-or-later WITH GCC-exception-3.1 |
| Homepage | https://gcc.gnu.org/ |
| Build time | 30.6 SBU |
| Installed size | 86.7 MiB in 1057 files |
| Recipe | ports/core/embedded/libstdcxx-riscv64-unknown-elf/kpkgbuild |
About
The C++ standard library for bare-metal RISC-V: libstdc++.a, libsupc++.a and libstdc++exp.a for every multilib of gcc-riscv64-unknown-elf, from rv64gc with the lp64d ABI down to the rv32i variants, and the headers under /usr/riscv64-unknown-elf/include/c++, built against picolibc. With it riscv64-unknown-elf-g++ can compile and link C++ for RV32 and RV64 microcontrollers and for the soft cores the FPGA tools synthesise. It is the second build of the cross compiler’s gcc source: libstdc++ compiles against the target C library, and picolibc is built with that compiler, so the compiler, the C library and this library are three recipes. It is the same build as libstdcxx-arm-none-eabi for the other target; how the split works is in A second build of the same source.
Why it is in KDOS
The comment above the 4 ports from binutils-riscv64-unknown-elf to libstdcxx-riscv64-unknown-elf in script/phases/41_system/packages.d/embedded.txt:
RV32 and RV64 — the MCUs and the soft cores the FPGA flow (yosys, nextpnr) synthesises.
Sources
gcc-riscv64-unknown-elf-16.2.0.tar.xz(102.2 MiB)
sha256e6738e29597f733270731aa90600f37ffdc045079dfc27ec7e8192cc81085c3e
upstream: https://mirrors.ocf.berkeley.edu/gnu/gcc/gcc-16.2.0/gcc-16.2.0.tar.xz
archive copy: sources-2/embedded--gcc-arm-none-eabi-16.2.0.tar.xz
Dependencies
Depends on: gcc-riscv64-unknown-elf, binutils-riscv64-unknown-elf, picolibc-riscv64-unknown-elf, gmp, mpfr, mpc, zlib, zstd.
Required by: no port names it.
Patches
build.sh applies these; kpkg applies nothing by itself.
picolibc-long-double-math.patch— In bothlibstdc++-v3/configure.acand the generatedconfigure, adds theHAVE_*Ldefinitions (HAVE_ACOSLthroughHAVE_TANHL) for thelong doublemaths functions to libstdc++’s picolibc branch, which defines only thefloatones although picolibc has both. Without them<cmath>declares nostd::acosland its kin, and the C++23stdmodule does not compile.
Installation
From the repository root, inside the build container, this is the line kdosbuild runs for the port:
script/chroot/exec.sh bash -c 'source script/phases/41_system/phase.env && kpkg install libstdcxx-riscv64-unknown-elf'
What kpkg does
kpkg runs these steps, in this order, as root inside the chroot. The port directory is /ports/core/embedded/libstdcxx-riscv64-unknown-elf.
1. Verify every declared source. Each file a sha256 = line names, found in the port directory or in /var/cache/kpkg/sources, must hash to its line; a mismatch stops the build before anything is unpacked.
cd /ports/core/embedded/libstdcxx-riscv64-unknown-elf
echo 'e6738e29597f733270731aa90600f37ffdc045079dfc27ec7e8192cc81085c3e gcc-riscv64-unknown-elf-16.2.0.tar.xz' | sha256sum -c -
2. Unpack. The work directory is emptied and the first source is unpacked into $SRC with its top directory stripped; any later tarball lands unstripped beside it in $SRC_ROOT, and a file that is not a tarball is copied into $SRC.
SRC_ROOT=/var/cache/kpkg/work/libstdcxx-riscv64-unknown-elf
SRC=$SRC_ROOT/libstdcxx-riscv64-unknown-elf-16.2.0
PKG=$SRC_ROOT/pkg
rm -rf "$SRC_ROOT"
mkdir -p "$SRC" "$PKG"
tar --no-same-permissions --warning=no-unknown-keyword -xf gcc-riscv64-unknown-elf-16.2.0.tar.xz -C "$SRC" --strip-components=1
Every config.guess and config.sub older than automake’s is then replaced with automake’s, so a configure script recognises the musl target.
3. Build. build.sh is sourced in a subshell under set -e, from $SRC, with name, version, release and the recipe’s helpers _up, group and the five paths PKGNAME, PORT_SRC, SRC_ROOT, SRC and PKG set as shell variables. Its body, verbatim:
patch -p1 -i "$PORT_SRC/picolibc-long-double-math.patch"
mkdir -p build && cd build
../configure \
--target=riscv64-unknown-elf \
--prefix=/usr \
--libexecdir=/usr/lib \
--with-sysroot=/usr/riscv64-unknown-elf \
--with-native-system-header-dir=/include \
--enable-languages=c,c++ \
--with-picolibc \
--without-newlib \
--with-headers=no \
--disable-nls \
--disable-shared \
--disable-threads \
--disable-libssp \
--disable-libgomp \
--disable-libquadmath \
--disable-libatomic \
--disable-libstdcxx-pch \
--disable-libstdcxx-verbose \
--enable-hosted-libstdcxx \
--disable-decimal-float \
--with-gnu-as --with-gnu-ld \
--with-zstd=/usr \
--without-isl \
--enable-multilib --with-abi=lp64d --with-arch=rv64gc \
--with-pkgversion="KDOS" \
CFLAGS_FOR_TARGET="-Os -pipe -ffunction-sections -fdata-sections" \
CXXFLAGS_FOR_TARGET="-Os -pipe -ffunction-sections -fdata-sections"
make all-target-libstdc++-v3 2>&1 | tee ../libstdcxx-build.log
test "${PIPESTATUS[0]}" -eq 0
if grep -F 'stamp-modules-bits] Error' ../libstdcxx-build.log; then
exit 1
fi
make -C riscv64-unknown-elf/libstdc++-v3 DESTDIR=$PKG install
find "$PKG" \( -name '*.la' -o -name '*-gdb.py' \) -delete
rm -rf "$PKG/usr/share"
The switches, and why each is set:
--target=riscv64-unknown-elf,--prefix=/usr,--libexecdir=/usr/lib,--with-sysroot=/usr/riscv64-unknown-elf,--enable-multilib,--with-abi=lp64d,--with-arch=rv64gc— The options gcc-riscv64-unknown-elf is configured with: the default architecture and ABI, and gcc’s default RISC-V multilib set beside them. They have to match it: the prefix, the sysroot and the multilib set decide the directories the libraries are installed in, and the installed compiler driver searches only its own.--with-native-system-header-dir=/include— Where the C library’s headers are, relative to the sysroot. picolibc’s headers are in/usr/riscv64-unknown-elf/include, not in the sysroot’s/usr/include, and without the switch the build stops atfixincludes, which looks for a directory that does not exist.--with-picolibc,--without-newlib— Select libstdc++’s picolibc configuration: theos/picolibcsupport files and a fixed list of the functions picolibc provides, taken without link tests, which a bare-metal target cannot run.--with-picolibcalso keeps libtool’sdlopenprobe, a link test, out of configure. configure tests for newlib before picolibc, so--with-newlibwould take the newlib branch and itsos/newlibfiles instead.--with-headers=no— In libstdc++’s configure, takes#include <stdio.h>out of the prologue autoconf puts in front of every compile test, and, with--without-newlibbeside it, makes the library default to a freestanding build, which--enable-hosted-libstdcxxreverses.--enable-hosted-libstdcxx— Builds the hosted library, with streams, strings, containers and the rest, which picolibc can support, rather than the freestanding subset.--disable-libstdcxx-verbose— Leaves the verbose terminate handler out of the library. That handler prints the type and message of an uncaught exception, and pulls the demangler and stdio into every firmware image that can throw.--disable-shared,--disable-threads,--disable-libstdcxx-pch— Static archives only, no thread support and no precompiled headers, as in gcc-riscv64-unknown-elf.--disable-libssp,--disable-libgomp,--disable-libquadmath,--disable-libatomic,--disable-decimal-float— The other target libraries and features a gcc build can make for its target, none of which this build installs.--with-zstd=/usr,--without-isl— The host side of the configured compiler: zstd for compressed LTO streams, and no isl loop optimiser, as in gcc-riscv64-unknown-elf. Nothing of the compiler itself is installed from this build.--with-pkgversion="KDOS"— The string the compiler prints in place of(GCC)in its version line.CFLAGS_FOR_TARGET,CXXFLAGS_FOR_TARGET— The flags for the target libraries:-Os -pipe -ffunction-sections -fdata-sections, nothing from the host’s flags.-Osbecause every byte oflibstdc++a firmware links lands in the part’s flash, and the per-function sections let--gc-sectionsdrop what a firmware image does not call. See A language standard reaching the compiler’s own runtime.
The common switches of each build system are explained in the Flag glossary.
4. Package. kpkg removes libtool archives, the info directory index, mimeinfo.cache and every fonts.dir from $PKG, copies the source’s licence files into /usr/share/licenses/libstdcxx-riscv64-unknown-elf, prunes what the image does not ship, lints the tree and rolls it reproducibly:
tar --sort=name --format=gnu --numeric-owner --owner=0 --group=0 \
--mtime=@$SOURCE_DATE_EPOCH \
--use-compress-program='xz -9 -T0 --block-size=32MiB --no-adjust' \
-cf libstdcxx-riscv64-unknown-elf-16.2.0-1.tar.xz -C "$PKG" .
5. Adopt. kpkgadd installs the package’s files over the root, refusing a path another package owns, records the file list in /var/lib/kpkg/db/libstdcxx-riscv64-unknown-elf, and runs the triggers its paths call for.
Contents
Installed directories: /usr/riscv64-unknown-elf/include/c++, /usr/share/licenses/libstdcxx-riscv64-unknown-elf.
Notes
- The build log is checked for the
stdmodule. When the module interface fails, make printsstamp-modules-bits] Error 1 (ignored)and goes on, and the package would ship alibstdc++.modules.jsonthat advertises a module that does not exist.build.shtees the output ofmake all-target-libstdc++-v3to a log, fails on make’s own exit status, and fails again if that line is in the log. - Only libstdc++ is installed. The install runs
make installinriscv64-unknown-elf/libstdc++-v3, not the top-levelinstall-target-libstdc++-v3, which installslibgccfirst and would give this package files gcc-riscv64-unknown-elf owns. The libtool.lafiles are deleted, and so are the-gdb.pypretty-printer loaders beside each archive and/usr/share, where the printers would land in/usr/share/gcc-<version>/python, the host gcc’s own path; a loader beside a static archive is never auto-loaded anyway. - The version is the compiler’s. The recipe carries the gcc tarball under gcc-riscv64-unknown-elf’s file name and checksum, and both recipes carry
group = gcc-riscv64-unknown-elf, so the two are bumped together.