#981983 libnewlib-arm-none-eabi: CFLAGS used for host object instead of target objects

#981983#5
Date:
2021-02-05 14:30:29 UTC
From:
To:
Dear Maintainer,

newlib is supposed to be built with `-f{data,function}-sections`, and
newlib-nano is supposed to be built with also `-Os -fshort-wchar`:

=== cut debian/rules ===
CFLAGS := CFLAGS="-g -O2 -ffunction-sections -fdata-sections"
CFLAGS_NANO := CFLAGS="-g -Os -ffunction-sections -fdata-sections -fshort-wchar"
=== cut ===

Unfortunately, this is cross-compilation, and those flags are used to compile only
*build host* objects, and not for *target* objects. So, all target libraries are built
with wrong cflags.

This is especially bad for newlib-nano, as `-fshort-wchar` is alters ABI
(and lack of `-f*-sections` prevents removing unused data/functions on link
time, which is important for ram/flash limited targets).

(actually, I'm not sure if `-fshort-wchar` was that much good idea: you either don't
use `wchar_t` at all, or you want "proper" 32-bit one; and given that `-fshort-wchar`
is not automatically passed by `-specs=nano.specs`, this requires non-trivial and flaky
auto-configuration from library users, e.g. https://github.com/RIOT-OS/RIOT/pull/5627 ).

Fix:
CFLAGS += CFLAGS_FOR_TARGET="-g -O2 -ffunction-sections -fdata-sections"
CFLAGS_NANO += CFLAGS_FOR_TARGET="-g -Os -ffunction-sections -fdata-sections -fshort-wchar"

Disclaimer: noticed while building local backport; verified with buildd's logs
https://buildd.debian.org/status/fetch.php?pkg=newlib&arch=all&ver=3.3.0-1&stamp=1583408649&raw=1
and inspecting lib*.a from "official" packages from sid/bullseye, buster, stretch
(confirmed all was built without -f{funciton,data}-section). Bugreport metadata
altered to match sid version.

#981983#10
Date:
2024-11-05 09:51:41 UTC
From:
To:
Still a problem with 4.4, reported in Ubuntu:
https://bugs.launchpad.net/debian/+source/newlib/+bug/2086618

I found that CFLAGS still have to be provided, even if empty, so my
solution is:

CFLAGS := CFLAGS= CFLAGS_FOR_TARGET="-g -O2 -ffunction-sections
-fdata-sections"
CFLAGS_NANO := CFLAGS= CFLAGS_FOR_TARGET="-g -Os -ffunction-sections
-fdata-sections -fshort-wchar"