Modify optimization flags when building toolchain

How should one modify the optimization flag when building the toolchain?

I tried modifying host_cflags in source-builder/defaults.mc to use -Os instead of -O2 but I don’t see any change in the size of the final binaries or libc:

$ size /home/matteo/dev/rtems/7/sparc-rtems7/lib/leon3/libc.a -t -G
395336     742864       9138    1147338 (TOTALS)

$ size /home/matteo/dev/rtems/7s/sparc-rtems7/lib/leon3/libc.a -t -G
395336     742864       9138    1147338 (TOTALS)

Here 7s was built with -Os and 7 with -O2.

When building newlib manually I do see a change in the sizes when building with CFLAGS_FOR_TARGET="-Os" compared to the default build.

You should be able to override OPTIMIZATION_FLAGS in config.ini, but be aware that the default flags are typically -O2 -g -fdata-sections -ffunction-sections (as per spec/build/bsps/opto2.yml) and that you should start with that rather than fully replacing it with just -Os.

But that would only impact BSP sources built when you build the kernel right?
I’m talking about building things like Newlib with -Os from RSB.

Ah, sorry, I misread the intent. You are correct, config.ini does not apply to newlib. I haven’t done the digging necessary to determine what needs to be adjusted for newlib build flag changes.

Adding --targetcflags="-Os" to the sb-set-builder command seems to have done the trick:

../source-builder/sb-set-builder --prefix=/home/matteo/dev/rtems/7s --targetcflags="-Os" 7/rtems-sparc

When looking at the log file, three optimization flags are shown in compilation commands:

/home/matteo/dev/rtems/rtems-source-builder/rtems/build/sparc-rtems7-gcc-15.2.0-newlib-038afec1-x86_64-linux-gnu-1/build/./gcc/xgcc -B/home/matteo/dev/rtems/rtems-source-builder/rtems/build/sparc-rtems7-gcc-15.2.0-newlib-038afec1-x86_64-linux-gnu-1/build/./gcc/ -nostdinc -B/home/matteo/dev/rtems/rtems-source-builder/rtems/build/sparc-rtems7-gcc-15.2.0-newlib-038afec1-x86_64-linux-gnu-1/build/sparc-rtems7/newlib/ -isystem /home/matteo/dev/rtems/rtems-source-builder/rtems/build/sparc-rtems7-gcc-15.2.0-newlib-038afec1-x86_64-linux-gnu-1/build/sparc-rtems7/newlib/targ-include -isystem /home/matteo/dev/rtems/rtems-source-builder/rtems/build/sparc-rtems7-gcc-15.2.0-newlib-038afec1-x86_64-linux-gnu-1/gcc-15.2.0/newlib/libc/include -B/home/matteo/dev/rtems/7-gr716/sparc-rtems7/bin/ -B/home/matteo/dev/rtems/7-gr716/sparc-rtems7/lib/ -isystem /home/matteo/dev/rtems/7-gr716/sparc-rtems7/include -isystem /home/matteo/dev/rtems/7-gr716/sparc-rtems7/sys-include    -Os -O2 -I../../../gcc-15.2.0/libgcc/../newlib/libc/sys/rtems/include -Os -DIN_GCC -DCROSS_DIRECTORY_STRUCTURE  -W -Wall -Wno-error=narrowing -Wwrite-strings -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes -Wold-style-definition  -isystem ./include   -g -DIN_LIBGCC2 -fbuilding-libgcc -fno-stack-protector -Dinhibit_libc  -I. -I. -I../.././gcc -I../../../gcc-15.2.0/libgcc -I../../../gcc-15.2.0/libgcc/. -I../../../gcc-15.2.0/libgcc/../gcc -I../../../gcc-15.2.0/libgcc/../include  -DHAVE_CC_TLS   -o _mulsi3_s.o -MT _mulsi3_s.o -MD -MP -MF _mulsi3_s.dep -DSHARED -DL_mulsi3 -xassembler-with-cpp -c ../../../gcc-15.2.0/libgcc/config/sparc/lb1spc.S

Relevant part:

[...] -Os -O2 -I../../../gcc-15.2.0/libgcc/../newlib/libc/sys/rtems/include -Os -DIN_GCC [...]

So my guess is the first -Os comes from host_cflags and the second one from --targetcflags.