This option adds a per-multilib variant that specifies -Os
instead of the default.

Signed-off-by: Keith Packard <[email protected]>
---
 config-ml.in         |  2 +-
 gcc/Makefile.in      | 32 +++++++++++++++++++++++++++-----
 gcc/configure.ac     |  7 +++++++
 gcc/doc/install.texi | 12 ++++++++++++
 4 files changed, 47 insertions(+), 6 deletions(-)

diff --git a/config-ml.in b/config-ml.in
index 7934a1ddf4b..c01fe4741c2 100644
--- a/config-ml.in
+++ b/config-ml.in
@@ -175,7 +175,7 @@ eval scan_arguments "${ac_configure_args}"
 unset scan_arguments
 
 # Only do this if --enable-multilib.
-if [ "${enable_multilib}" = yes ]; then
+if [ "${enable_multilib}" = yes -o "${enable_multilib_space}" = yes ]; then
 
 # Compute whether this is the library's top level directory
 # (ie: not a multilib subdirectory, and not a subdirectory like newlib/src).
diff --git a/gcc/Makefile.in b/gcc/Makefile.in
index cf1408d56e2..c6d65085f11 100644
--- a/gcc/Makefile.in
+++ b/gcc/Makefile.in
@@ -2511,25 +2511,47 @@ libgcc.mvars: config.status Makefile specs xgcc$(exeext)
 
        mv tmp-libgcc.mvars libgcc.mvars
 
+ifeq (@enable_multilib_space@,yes)
+MULTILIB_OPTIONS       += Os
+MULTILIB_DIRNAMES      += space
+MULTILIB_MATCHES       += Os=Oz
+
+MULTILIB_OSDIRNAMES_SPACE = $(MULTILIB_OSDIRNAMES)\
+       $(if $(findstring =,$(MULTILIB_OSDIRNAMES)),\
+         $(foreach OSD,$(MULTILIB_OSDIRNAMES),$(subst =,/Os=,$(OSD))/space),\
+         $(if $(MULTILIB_OSDIRNAMES),space,))
+MULTILIB_REQUIRED_SPACE = $(if $(MULTILIB_REQUIRED),Os $(foreach REQ, 
$(MULTILIB_REQUIRED), $(REQ) $(REQ)/Os),)
+MULTILIB_EXCEPTIONS_SPACE = $(foreach EXC, $(MULTILIB_EXCEPTIONS), $(EXC) 
$(EXC)/Os)
+MULTILIB_REUSE_SPACE = $(foreach REU, $(MULTILIB_REUSE), $(REU) $(subst 
=,/Os=,$(REU))/Os)
+MULTILIB_ENABLE = yes
+else
+MULTILIB_OSDIRNAMES_SPACE = $(MULTILIB_OSDIRNAMES)
+MULTILIB_REQUIRED_SPACE = $(MULTILIB_REQUIRED)
+MULTILIB_EXCEPTIONS_SPACE = $(MULTILIB_EXCEPTIONS)
+MULTILIB_REUSE_SPACE = $(MULTILIB_REUSE)
+MULTILIB_ENABLE = @enable_multilib@
+endif
+
 # Use the genmultilib shell script to generate the information the gcc
 # driver program needs to select the library directory based on the
 # switches.
 multilib.h: s-mlib; @true
 s-mlib: $(srcdir)/genmultilib Makefile
        if test @enable_multilib@ = yes \
+           || test @enable_multilib_space@ = yes \
           || test -n "$(MULTILIB_OSDIRNAMES)"; then \
          $(SHELL) $(srcdir)/genmultilib \
            "$(MULTILIB_OPTIONS)" \
            "$(MULTILIB_DIRNAMES)" \
            "$(MULTILIB_MATCHES)" \
-           "$(MULTILIB_EXCEPTIONS)" \
+           "$(MULTILIB_EXCEPTIONS_SPACE)" \
            "$(MULTILIB_EXTRA_OPTS)" \
            "$(MULTILIB_EXCLUSIONS)" \
-           "$(MULTILIB_OSDIRNAMES)" \
-           "$(MULTILIB_REQUIRED)" \
+           "$(MULTILIB_OSDIRNAMES_SPACE)" \
+           "$(MULTILIB_REQUIRED_SPACE)" \
            "$(if $(MULTILIB_OSDIRNAMES),,$(MULTIARCH_DIRNAME))" \
-           "$(MULTILIB_REUSE)" \
-           "@enable_multilib@" \
+           "$(MULTILIB_REUSE_SPACE)" \
+           "$(MULTILIB_ENABLE)" \
            > tmp-mlib.h; \
        else \
          $(SHELL) $(srcdir)/genmultilib '' '' '' '' '' '' '' '' \
diff --git a/gcc/configure.ac b/gcc/configure.ac
index 04f86b52ef8..3c3b36f1136 100644
--- a/gcc/configure.ac
+++ b/gcc/configure.ac
@@ -835,6 +835,13 @@ AC_ARG_ENABLE(multilib,
 [], [enable_multilib=yes])
 AC_SUBST(enable_multilib)
 
+# Determine whether or not -Os multilibs are enabled.
+AC_ARG_ENABLE(multilib-space,
+[AS_HELP_STRING([--enable-multilib-space],
+               [enable extra -Os variant for every multilib ABI])],
+[], [enable_multilib_space=no])
+AC_SUBST(enable_multilib_space)
+
 # Determine whether or not multiarch is enabled.
 AC_ARG_ENABLE(multiarch,
 [AS_HELP_STRING([--enable-multiarch],
diff --git a/gcc/doc/install.texi b/gcc/doc/install.texi
index 576b7eead5e..e6366dea181 100644
--- a/gcc/doc/install.texi
+++ b/gcc/doc/install.texi
@@ -1336,6 +1336,18 @@ for aarch64*-*-*, amdgcn*-*-*, arm*-*-*, loongarch*-*-*, 
nvptx-*, riscv*-*-*,
 sh*-*-* and x86-64-*-linux*.
 The accepted values and meaning for each target is given below.
 
+@item --enable-multilib-space
+Double the set of target libraries built by building two versions of
+each specified by the options above, one using @option{-O2 }and
+another using @option{-Os}. During linking, the @option{-O2} variant
+will be selected by default. Select the @option{-Os} variant by
+including @option{-Os} or @option{-Oz} in the linker command
+line. Note: because multilib selection only looks for the presence of
+compiler flags (unlike options controlling the compiler optimization
+level), a subsequent optimization flag other than @option{-Os} or or
+@option{-Oz} will not switch back to the @option{-O2} library
+versions.
+
 @table @code
 @item aarch64*-*-*
 @var{list} is a comma separated list of @code{ilp32}, and @code{lp64}
-- 
2.51.0

Reply via email to