From 3c637c1eec9fe05c48bcd40186c7831933c2d6f2 Mon Sep 17 00:00:00 2001 From: Stephanos Ioannidis Date: Sat, 30 Nov 2019 01:42:18 +0900 Subject: gcc: Assume '-O2' by default for building gcc target libraries The gcc target libraries (e.g. libstdc++) are currently built without any optimisation flag when `CT_CC_GCC_ENABLE_TARGET_OPTSPACE` is not enabled and default to `-O0` unless user explicitly specifies an optimisation flag. This commit updates the gcc build script to assume `-O2` for building target libraries unless user provides a different optimisation flag. Note also that this is the default behaviour for gcc when C[XX]FLAGS_FOR_TARGET is not overridden. Signed-off-by: Stephanos Ioannidis diff --git a/scripts/build/cc/gcc.sh b/scripts/build/cc/gcc.sh index cccd654..39c70bb 100644 --- a/scripts/build/cc/gcc.sh +++ b/scripts/build/cc/gcc.sh @@ -608,6 +608,9 @@ do_gcc_core_backend() { cflags_for_target="${cflags_for_target} -idirafter ${CT_HEADERS_DIR}" fi + # Assume '-O2' by default for building target libraries. + cflags_for_target="-g -O2 ${cflags_for_target}" + # Use --with-local-prefix so older gccs don't look in /usr/local (http://gcc.gnu.org/PR10532). # Pass only user-specified CFLAGS/LDFLAGS in CFLAGS_FOR_TARGET/LDFLAGS_FOR_TARGET: during # the build of, for example, libatomic, GCC tried to compile multiple variants for runtime @@ -936,6 +939,7 @@ do_gcc_backend() { local lang_list local cflags local cflags_for_build + local cflags_for_target local ldflags local build_manuals local exec_prefix @@ -1200,8 +1204,9 @@ do_gcc_backend() { CT_DoLog DEBUG "Extra config passed: '${extra_config[*]}'" - # We may need to modify host/build CFLAGS separately below + # We may need to modify host/build/target CFLAGS separately below cflags_for_build="${cflags}" + cflags_for_target="${CT_TARGET_CFLAGS}" # Clang's default bracket-depth is 256, and building GCC # requires somewhere between 257 and 512. @@ -1217,6 +1222,9 @@ do_gcc_backend() { fi fi + # Assume '-O2' by default for building target libraries. + cflags_for_target="-g -O2 ${cflags_for_target}" + # NB: not using CT_ALL_TARGET_CFLAGS/CT_ALL_TARGET_LDFLAGS here! # See do_gcc_core_backend for explanation. CT_DoExecLog CFG \ @@ -1226,8 +1234,8 @@ do_gcc_backend() { CXXFLAGS="${cflags}" \ CXXFLAGS_FOR_BUILD="${cflags_for_build}" \ LDFLAGS="${final_LDFLAGS[*]}" \ - CFLAGS_FOR_TARGET="${CT_TARGET_CFLAGS}" \ - CXXFLAGS_FOR_TARGET="${CT_TARGET_CFLAGS}" \ + CFLAGS_FOR_TARGET="${cflags_for_target}" \ + CXXFLAGS_FOR_TARGET="${cflags_for_target}" \ LDFLAGS_FOR_TARGET="${CT_TARGET_LDFLAGS}" \ ${CONFIG_SHELL} \ "${CT_SRC_DIR}/gcc/configure" \ -- cgit v0.10.2-6-g49f6 From fffa4c5aa5b5995ce88680170fb2288b10b05fbe Mon Sep 17 00:00:00 2001 From: Stephanos Ioannidis Date: Mon, 7 Jun 2021 14:42:06 +0900 Subject: gcc: Allow setting custom target CXXFLAGS This commit adds two additional arguments (`cxxflags_for_target` and `extra_cxxflags_for_target`) for the gcc backend build function that can be used to specify custom target CXXFLAGS. By default, the target CXXFLAGS is set to the target CFLAGS. When `cxxflags_for_target` is specified however, it overrides that behaviour and allows setting different target CXXFLAGS from the target CFLAGS. The `extra_cxxflags_for_target` argument can be used to specify the extra target CXXFLAGS to be appended to the target CXXFLAGS. This is useful when it is necessary to append CXX-specific flags to the existing CFLAGS to be used as the target CXXFLAGS. A useful application of this is building full and nano versions of libstdc++ with different target CXXFLAGS as necessitated by `nano.specs`. Signed-off-by: Stephanos Ioannidis diff --git a/scripts/build/cc/gcc.sh b/scripts/build/cc/gcc.sh index 39c70bb..dc391e4 100644 --- a/scripts/build/cc/gcc.sh +++ b/scripts/build/cc/gcc.sh @@ -295,7 +295,8 @@ do_gcc_core_backend() { local enable_optspace local complibs local lang_list - local cflags cflags_for_build cflags_for_target + local cflags cflags_for_build cflags_for_target cxxflags_for_target + local extra_cxxflags_for_target local ldflags local build_step local log_txt @@ -611,6 +612,16 @@ do_gcc_core_backend() { # Assume '-O2' by default for building target libraries. cflags_for_target="-g -O2 ${cflags_for_target}" + # Set target CXXFLAGS to CFLAGS if none is provided. + if [ -z "${cxxflags_for_target}" ]; then + cxxflags_for_target="${cflags_for_target}" + fi + + # Append extra CXXFLAGS if provided. + if [ -n "${extra_cxxflags_for_target}" ]; then + cxxflags_for_target="${cxxflags_for_target} ${extra_cxxflags_for_target}" + fi + # Use --with-local-prefix so older gccs don't look in /usr/local (http://gcc.gnu.org/PR10532). # Pass only user-specified CFLAGS/LDFLAGS in CFLAGS_FOR_TARGET/LDFLAGS_FOR_TARGET: during # the build of, for example, libatomic, GCC tried to compile multiple variants for runtime @@ -624,7 +635,7 @@ do_gcc_core_backend() { CXXFLAGS_FOR_BUILD="${cflags_for_build}" \ LDFLAGS="${core_LDFLAGS[*]}" \ CFLAGS_FOR_TARGET="${cflags_for_target}" \ - CXXFLAGS_FOR_TARGET="${cflags_for_target}" \ + CXXFLAGS_FOR_TARGET="${cxxflags_for_target}" \ LDFLAGS_FOR_TARGET="${CT_TARGET_LDFLAGS}" \ ${CONFIG_SHELL} \ "${CT_SRC_DIR}/gcc/configure" \ @@ -940,6 +951,8 @@ do_gcc_backend() { local cflags local cflags_for_build local cflags_for_target + local cxxflags_for_target + local extra_cxxflags_for_target local ldflags local build_manuals local exec_prefix @@ -1225,6 +1238,16 @@ do_gcc_backend() { # Assume '-O2' by default for building target libraries. cflags_for_target="-g -O2 ${cflags_for_target}" + # Set target CXXFLAGS to CFLAGS if none is provided. + if [ -z "${cxxflags_for_target}" ]; then + cxxflags_for_target="${cflags_for_target}" + fi + + # Append extra CXXFLAGS if provided. + if [ -n "${extra_cxxflags_for_target}" ]; then + cxxflags_for_target="${cxxflags_for_target} ${extra_cxxflags_for_target}" + fi + # NB: not using CT_ALL_TARGET_CFLAGS/CT_ALL_TARGET_LDFLAGS here! # See do_gcc_core_backend for explanation. CT_DoExecLog CFG \ @@ -1235,7 +1258,7 @@ do_gcc_backend() { CXXFLAGS_FOR_BUILD="${cflags_for_build}" \ LDFLAGS="${final_LDFLAGS[*]}" \ CFLAGS_FOR_TARGET="${cflags_for_target}" \ - CXXFLAGS_FOR_TARGET="${cflags_for_target}" \ + CXXFLAGS_FOR_TARGET="${cxxflags_for_target}" \ LDFLAGS_FOR_TARGET="${CT_TARGET_LDFLAGS}" \ ${CONFIG_SHELL} \ "${CT_SRC_DIR}/gcc/configure" \ -- cgit v0.10.2-6-g49f6 From 7144b5f275f380941bffca9509ecb97542c8e5ea Mon Sep 17 00:00:00 2001 From: Stephanos Ioannidis Date: Mon, 7 Jun 2021 15:13:19 +0900 Subject: newlib: Add libstdc++ nano-specific CXXFLAGS This commit adds a new config that can be used to specify the target CXXFLAGS specific to the libstdc++ newlib-nano variant. By default, this config is set to specify the `-fno-exceptions` option, which disables C++ exception handling support and greatly reduces the compiled binary size. Signed-off-by: Stephanos Ioannidis diff --git a/config/comp_libs/newlib-nano.in b/config/comp_libs/newlib-nano.in index 0d80c89..5a81fde 100644 --- a/config/comp_libs/newlib-nano.in +++ b/config/comp_libs/newlib-nano.in @@ -18,6 +18,14 @@ config NEWLIB_NANO_GCC_LIBSTDCXX This option compiles an additional target libstdc++ for use with newlib-nano. +config NEWLIB_NANO_GCC_LIBSTDCXX_TARGET_CXXFLAGS + string + prompt "Target CXXFLAGS for libstdc++ newlib-nano variant" + default "-fno-exceptions" + help + Used to add extra CXXFLAGS when compiling the target libstdc++ + newlib-nano library (e.g. -fno-exceptions). + config NEWLIB_NANO_INSTALL_IN_TARGET bool prompt "Additionally install newlib-nano libs into TARGET dir" diff --git a/scripts/build/companion_libs/350-newlib_nano.sh b/scripts/build/companion_libs/350-newlib_nano.sh index d109070..2d9de1b 100644 --- a/scripts/build/companion_libs/350-newlib_nano.sh +++ b/scripts/build/companion_libs/350-newlib_nano.sh @@ -67,6 +67,9 @@ do_cc_libstdcxx_newlib_nano() if [ "${CT_LIBC_NEWLIB_NANO_ENABLE_TARGET_OPTSPACE}" = "y" ]; then final_opts+=( "enable_optspace=yes" ) fi + if [ -n "${CT_NEWLIB_NANO_GCC_LIBSTDCXX_TARGET_CXXFLAGS}" ]; then + final_opts+=( "extra_cxxflags_for_target=${CT_NEWLIB_NANO_GCC_LIBSTDCXX_TARGET_CXXFLAGS}" ) + fi if [ "${CT_BARE_METAL}" = "y" ]; then final_opts+=( "mode=baremetal" ) -- cgit v0.10.2-6-g49f6