A few years ago, I set up the toolchain for STM8 development. Back then, simply following the steps in the README was enough to get everything compiled and working out of the box. However, after installing a clean Ubuntu 26.04 along with GCC/G++ 15, I found that compiling stm8-binutils-gdb wasn't as straightforward as it used to be. That is exactly what we will look into today.
The Problem and the Proposed Solution
Since there is no official .deb package available in the repositories, we have to build the toolchain from source. You can find the project homepage at https://stm8-binutils-gdb.sourceforge.io, and the source archives are available for download on https://sourceforge.net/projects/stm8-binutils-gdb/files/.
If you simply follow the instructions, the build fails with a compilation error in the readline library, which is required by stm8-gdb. Apparently, it has not been adapted for building with modern GCC v15. On the other hand, readline is available as a .deb package in the repositories. The idea is to avoid building it from source and instead link against the pre-compiled version provided by the repository.However, there are no out-of-the-box configuration flags to achieve this, so we will need to slightly modify the build configuration scripts.
Implementation Details
Assuming you have already downloaded the stm8-binutils-gdb-sources-2021-07-18.tar.gz archive, extract it, review the README.txt, and run ./patch_binutils.sh:
$ tar -xf stm8-binutils-gdb-sources-2021-07-18.tar.gz
$ cd stm8-binutils-gdb-sources/
$ cat README.txt
$ ./patch_binutils.sh
Next, we need to modify a few configuration files. In configure_binutils.sh, replace the following line:
./configure --host=$_HOST --target=stm8-none-elf32 $_PREFIX --program-prefix=stm8-
with
./configure --host=$_HOST --target=stm8-none-elf32 $_PREFIX \
--program-prefix=stm8- --with-system-zlib --with-system-readline
This explicitly tells the build system to use the pre-installed system versions of zlib and readline.Next, we need to add support for the --with-system-readline option, as the scripts only support --with-system-zlib by default. To do this, we will patch binutils-2.30/configure file. This file is quite large, so instead of fully analyzing its internal logic, I decided to implement support for --with-system-readline by mimicking the existing --with-system-zlib implementation.
First, let's add the new option to the help message block. Below the line
--with-system-zlib use installed libz
insert the line
--with-system-readline use installed readline
After that, we need to add two more blocks that handle the option logic.
Find block
use_included_zlib=
# Check whether --with-system-zlib was given.
if test "${with_system_zlib+set}" = set; then :
withval=$with_system_zlib;
fi
# Make sure we don't let ZLIB be added if we didn't want it.
if test x$with_system_zlib = xyes ; then
use_included_zlib=no
noconfigdirs="$noconfigdirs zlib"
fi
and append the following to it
# custom option for readline
use_included_readline=
# Check whether --with-system-readline was given.
if test "${with_system_readline+set}" = set; then :
withval=$with_system_readline;
fi
# Make sure we don't let readline be added if we didn't want it.
if test x$with_system_readline = xyes ; then
use_included_readline=no
noconfigdirs="$noconfigdirs readline"
fi
Then locate block
if test x${use_included_zlib} = x &&
echo " ${configdirs} " | grep " zlib " > /dev/null 2>&1 ; then
:
else
with_system_zlib=yes
extra_host_args="$extra_host_args --with-system-zlib"
fi
and append
if test x${use_included_readline} = x &&
echo " ${configdirs} " | grep " readline " > /dev/null 2>&1 ; then
:
else
with_system_readline=yes
extra_host_args="$extra_host_args --with-system-readline"
fi
Before starting the build, you need to install the libreadline-dev and texinfo packages:
$ sudo apt install libreadline-dev texinfo
It is possible that a completely clean machine might require additional dependencies, but it appears that my system already had everything necessary installed.
Now, run the modified configuration script and start the build process:
$ ./configure_binutils.sh $ cd binutils-2.30 $ make
You can speed up the build by running it on all available CPU cores simultaneously:
make -j$(nproc)