Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 922632 - toolchain.eclass should set CFLAGS_FOR_TARGET/CXXFLAGS_FOR_TARGET
Summary: toolchain.eclass should set CFLAGS_FOR_TARGET/CXXFLAGS_FOR_TARGET
Status: UNCONFIRMED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Eclasses (show other bugs)
Hardware: All Linux
: Normal enhancement (vote)
Assignee: Gentoo Toolchain Maintainers
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-01-21 16:15 UTC by Andrii Batyiev
Modified: 2024-01-30 09:04 UTC (History)
3 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrii Batyiev 2024-01-21 16:15:31 UTC
The gcc package not only contains the compiler itself, but it also contains various system libraries, most importantly libgcc and libstdc++. However, to set proper CFLAGS/CXXFLAGS for that libraries, you need to use CFLAGS_FOR_TARGET/CXXFLAGS_FOR_TARGET environment variables, otherwise these libraries would be built with flags that you do not expect. The toolchain.eclass should set such flags for gcc.

The workaround is to set these variables in make.conf.
Comment 1 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2024-01-21 16:23:06 UTC
From configure.ac:
```
# During gcc bootstrap, if we use some random cc for stage1 then CFLAGS
# might be empty or "-g".  We don't require a C++ compiler, so CXXFLAGS
# might also be empty (or "-g", if a non-GCC C++ compiler is in the path).
# We want to ensure that TARGET libraries (which we know are built with
# gcc) are built with "-O2 -g", so include those options when setting
# CFLAGS_FOR_TARGET and CXXFLAGS_FOR_TARGET.
if test "x$CFLAGS_FOR_TARGET" = x; then
  if test "x${is_cross_compiler}" = xyes; then
    CFLAGS_FOR_TARGET="-g -O2"
  else
    CFLAGS_FOR_TARGET=$CFLAGS
    case " $CFLAGS " in
      *" -O2 "*) ;;
      *) CFLAGS_FOR_TARGET="-O2 $CFLAGS_FOR_TARGET" ;;
    esac
    case " $CFLAGS " in
      *" -g "* | *" -g3 "*) ;;
      *) CFLAGS_FOR_TARGET="-g $CFLAGS_FOR_TARGET" ;;
    esac
  fi
fi
AC_SUBST(CFLAGS_FOR_TARGET)
```

It defaults to CFLAGS (resp. CXXFLAGS).
Comment 2 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2024-01-21 16:23:58 UTC
Pretty sure Arsen and I looked at this and concluded the above when we did the last *FLAGS rework in the eclass in

commit bb2d045c02a6ca647ef3280f4987cbc0d14e5a7e
Author: Sam James <sam@gentoo.org>
Date:   Fri Sep 29 00:27:06 2023 +0100

    toolchain.eclass: rework bootstrapping logic
[...]

Please provide some explanation as to why you believe this is not the case, or why that's not sufficient.
Comment 3 Andrii Batyiev 2024-01-21 17:45:47 UTC
But you filter CFLAGS/CXXFLAGS for gcc in `gcc_do_filter_flags()` function, so libraries would also use filtered flags - I don't think it is expected (I just wanted `-Oz` instead of `-O2` for libstdc++).
Comment 4 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2024-01-29 01:19:18 UTC
I feel like two things are getting mixed here, though.

Setting CFLAGS_FOR_TARGET and CXXFLAGS_FOR_TARGET doesn't fix the filtering problem, does it?

If we do:
```
--- a/eclass/toolchain.eclass
+++ b/eclass/toolchain.eclass
@@ -1722,6 +1722,8 @@ gcc_do_make() {
                STAGE1_LDFLAGS=${STAGE1_LDFLAGS-"${abi_ldflags} ${LDFLAGS}"}
                BOOT_CFLAGS=${BOOT_CFLAGS-"$(get_abi_CFLAGS ${TARGET_DEFAULT_ABI}) ${CFLAGS}"}
                BOOT_LDFLAGS=${BOOT_LDFLAGS-"${abi_ldflags} ${LDFLAGS}"}
+               CFLAGS_FOR_TARGET="${CFLAGS_FOR_TARGET:-${CFLAGS}}"
+               CXXFLAGS_FOR_TARGET="${CXXFLAGS_FOR_TARGET:-${CXXFLAGS}}"
                LDFLAGS_FOR_TARGET="${LDFLAGS_FOR_TARGET:-${LDFLAGS}}"

                emakeargs+=(
@@ -1729,6 +1731,8 @@ gcc_do_make() {
                        STAGE1_LDFLAGS="${STAGE1_LDFLAGS}"
                        BOOT_CFLAGS="${BOOT_CFLAGS}"
                        BOOT_LDFLAGS="${BOOT_LDFLAGS}"
+                       CFLAGS_FOR_TARGET="${CFLAGS_FOR_TARGET}"
+                       CXXFLAGS_FOR_TARGET="${CXXFLAGS_FOR_TARGET}"
                        LDFLAGS_FOR_TARGET="${LDFLAGS_FOR_TARGET}"
                )
        fi
```

which is what the title & comment 0 suggests, how does that avoid the filtering? By that point, the filtering is already done.
Comment 5 Andrii Batyiev 2024-01-30 09:04:16 UTC
(In reply to Sam James from comment #4)
> I feel like two things are getting mixed here, though.
> 
> Setting CFLAGS_FOR_TARGET and CXXFLAGS_FOR_TARGET doesn't fix the filtering
> problem, does it?
> 

The *_FOR_TARGET could be set before filtering (I'm not sure how the crosscompile should be handled here):

--- a/eclass/toolchain.eclass   2024-01-23 06:10:40.000000000 -0000
+++ b/eclass/toolchain.eclass   2024-01-29 18:26:22.351685558 -0000
@@ -755,6 +755,11 @@
        BUILD_CONFIG_TARGETS=()
        is-flagq '-O3' && BUILD_CONFIG_TARGETS+=( bootstrap-O3 )
 
+       if ! is_crosscompile ; then
+               CFLAGS_FOR_TARGET="${CFLAGS_FOR_TARGET:-${CFLAGS}}"
+               CXXFLAGS_FOR_TARGET="${CXXFLAGS_FOR_TARGET:-${CXXFLAGS}}"
+       fi
+
        downgrade_arch_flags
        gcc_do_filter_flags
 
@@ -767,6 +772,8 @@
        einfo "CFLAGS=\"${CFLAGS}\""
        einfo "CXXFLAGS=\"${CXXFLAGS}\""
        einfo "LDFLAGS=\"${LDFLAGS}\""
+       einfo "CFLAGS_FOR_TARGET=\"${CFLAGS_FOR_TARGET}\""
+       einfo "CXXFLAGS_FOR_TARGET=\"${CXXFLAGS_FOR_TARGET}\""
 
        # Force internal zip based jar script to avoid random
        # issues with 3rd party jar implementations. bug #384291
@@ -1729,6 +1736,8 @@
                        STAGE1_LDFLAGS="${STAGE1_LDFLAGS}"
                        BOOT_CFLAGS="${BOOT_CFLAGS}"
                        BOOT_LDFLAGS="${BOOT_LDFLAGS}"
+                       CFLAGS_FOR_TARGET="${CFLAGS_FOR_TARGET}"
+                       CXXFLAGS_FOR_TARGET="${CXXFLAGS_FOR_TARGET}"
                        LDFLAGS_FOR_TARGET="${LDFLAGS_FOR_TARGET}"
                )
        fi