diff mbox series

LoongArch: Reimplement multilib build option handling.

Message ID 20230913095214.125470-1-yangyujie@loongson.cn
State New
Headers show
Series LoongArch: Reimplement multilib build option handling. | expand

Commit Message

Yang Yujie Sept. 13, 2023, 9:52 a.m. UTC
Library build options from --with-multilib-list used to be processed with
*self_spec, which missed the driver's initial canonicalization.  This
caused limitations on CFLAGS override and the use of driver-only options
like -m[no]-lsx.

The problem is solved by promoting the injection rules of --with-multilib-list
options to the first element of DRIVER_SELF_SPECS, to make them execute before
the canonialization.  The library-build options are also hard-coded in
the driver and can be used conveniently by the builders of other non-gcc
libraries via the use of -fmultiflags.

Bootstrapped and tested on loongarch64-linux-gnu.

ChangeLog:

	* config-ml.in: Remove unneeded loongarch clause.
	* configure.ac: Register custom makefile fragments mt-loongarch-*
        for loongarch targets.
	* configure: Regenerate.

config/ChangeLog:

	* mt-loongarch-mlib: New file.  Pass -fmultiflags when building
        target libraries (FLAGS_FOR_TARGET).
	* mt-loongarch-elf: New file.
	* mt-loongarch-gnu: New file.

gcc/ChangeLog:

	* config.gcc: Pass the default ABI via TM_MULTILIB_CONFIG.
	* config/loongarch/loongarch-driver.h: Invoke MLIB_SELF_SPECS
        before the driver canonicalization routines.
	* config/loongarch/loongarch.h: Move definitions of CC1_SPEC etc.
        to loongarch-driver.h
	* config/loongarch/t-linux: Move multilib-related definitions to
        t-multilib.
	* config/loongarch/t-multilib: New file.  Inject library build
        options obtained from --with-multilib-list.
	* config/loongarch/t-loongarch: Same.
---
 config-ml.in                            | 10 ----
 config/mt-loongarch-elf                 |  1 +
 config/mt-loongarch-gnu                 |  2 +
 config/mt-loongarch-mlib                |  1 +
 configure                               |  6 +++
 configure.ac                            |  6 +++
 gcc/config.gcc                          |  6 +--
 gcc/config/loongarch/loongarch-driver.h | 42 +++++++++++++++
 gcc/config/loongarch/loongarch.h        | 50 ------------------
 gcc/config/loongarch/t-linux            | 66 +++---------------------
 gcc/config/loongarch/t-loongarch        |  2 +-
 gcc/config/loongarch/t-multilib         | 68 +++++++++++++++++++++++++
 12 files changed, 137 insertions(+), 123 deletions(-)
 create mode 100644 config/mt-loongarch-elf
 create mode 100644 config/mt-loongarch-gnu
 create mode 100644 config/mt-loongarch-mlib
 create mode 100644 gcc/config/loongarch/t-multilib

Comments

Lulu Cheng Sept. 15, 2023, 2:44 a.m. UTC | #1
Pushed to r14-4009.

在 2023/9/13 下午5:52, Yang Yujie 写道:
> Library build options from --with-multilib-list used to be processed with
> *self_spec, which missed the driver's initial canonicalization.  This
> caused limitations on CFLAGS override and the use of driver-only options
> like -m[no]-lsx.
>
> The problem is solved by promoting the injection rules of --with-multilib-list
> options to the first element of DRIVER_SELF_SPECS, to make them execute before
> the canonialization.  The library-build options are also hard-coded in
> the driver and can be used conveniently by the builders of other non-gcc
> libraries via the use of -fmultiflags.
>
> Bootstrapped and tested on loongarch64-linux-gnu.
>
> ChangeLog:
>
> 	* config-ml.in: Remove unneeded loongarch clause.
> 	* configure.ac: Register custom makefile fragments mt-loongarch-*
>          for loongarch targets.
> 	* configure: Regenerate.
>
> config/ChangeLog:
>
> 	* mt-loongarch-mlib: New file.  Pass -fmultiflags when building
>          target libraries (FLAGS_FOR_TARGET).
> 	* mt-loongarch-elf: New file.
> 	* mt-loongarch-gnu: New file.
>
> gcc/ChangeLog:
>
> 	* config.gcc: Pass the default ABI via TM_MULTILIB_CONFIG.
> 	* config/loongarch/loongarch-driver.h: Invoke MLIB_SELF_SPECS
>          before the driver canonicalization routines.
> 	* config/loongarch/loongarch.h: Move definitions of CC1_SPEC etc.
>          to loongarch-driver.h
> 	* config/loongarch/t-linux: Move multilib-related definitions to
>          t-multilib.
> 	* config/loongarch/t-multilib: New file.  Inject library build
>          options obtained from --with-multilib-list.
> 	* config/loongarch/t-loongarch: Same.
> ---
>   config-ml.in                            | 10 ----
>   config/mt-loongarch-elf                 |  1 +
>   config/mt-loongarch-gnu                 |  2 +
>   config/mt-loongarch-mlib                |  1 +
>   configure                               |  6 +++
>   configure.ac                            |  6 +++
>   gcc/config.gcc                          |  6 +--
>   gcc/config/loongarch/loongarch-driver.h | 42 +++++++++++++++
>   gcc/config/loongarch/loongarch.h        | 50 ------------------
>   gcc/config/loongarch/t-linux            | 66 +++---------------------
>   gcc/config/loongarch/t-loongarch        |  2 +-
>   gcc/config/loongarch/t-multilib         | 68 +++++++++++++++++++++++++
>   12 files changed, 137 insertions(+), 123 deletions(-)
>   create mode 100644 config/mt-loongarch-elf
>   create mode 100644 config/mt-loongarch-gnu
>   create mode 100644 config/mt-loongarch-mlib
>   create mode 100644 gcc/config/loongarch/t-multilib
>
> diff --git a/config-ml.in b/config-ml.in
> index ad0db078171..68854a4f16c 100644
> --- a/config-ml.in
> +++ b/config-ml.in
> @@ -301,16 +301,6 @@ arm-*-*)
>   	  done
>   	fi
>   	;;
> -loongarch*-*)
> -	old_multidirs="${multidirs}"
> -	multidirs=""
> -	for x in ${old_multidirs}; do
> -	case "$x" in
> -	`${CC-gcc} --print-multi-directory`) : ;;
> -	*) multidirs="${multidirs} ${x}" ;;
> -	esac
> -	done
> -	;;
>   m68*-*-*)
>   	if [ x$enable_softfloat = xno ]
>   	then
> diff --git a/config/mt-loongarch-elf b/config/mt-loongarch-elf
> new file mode 100644
> index 00000000000..bbf29bb578a
> --- /dev/null
> +++ b/config/mt-loongarch-elf
> @@ -0,0 +1 @@
> +include $(srcdir)/config/mt-loongarch-mlib
> diff --git a/config/mt-loongarch-gnu b/config/mt-loongarch-gnu
> new file mode 100644
> index 00000000000..dfefb44ede3
> --- /dev/null
> +++ b/config/mt-loongarch-gnu
> @@ -0,0 +1,2 @@
> +include $(srcdir)/config/mt-gnu
> +include $(srcdir)/config/mt-loongarch-mlib
> diff --git a/config/mt-loongarch-mlib b/config/mt-loongarch-mlib
> new file mode 100644
> index 00000000000..4cfe568f1fc
> --- /dev/null
> +++ b/config/mt-loongarch-mlib
> @@ -0,0 +1 @@
> +FLAGS_FOR_TARGET += -fmultiflags
> diff --git a/configure b/configure
> index 28f0913bdd4..8fc163d36bd 100755
> --- a/configure
> +++ b/configure
> @@ -9683,6 +9683,12 @@ case "${target}" in
>     spu-*-*)
>       target_makefile_frag="config/mt-spu"
>       ;;
> +  loongarch*-*linux* | loongarch*-*gnu*)
> +    target_makefile_frag="config/mt-loongarch-gnu"
> +    ;;
> +  loongarch*-*elf*)
> +    target_makefile_frag="config/mt-loongarch-elf"
> +    ;;
>     mips*-sde-elf* | mips*-mti-elf* | mips*-img-elf*)
>       target_makefile_frag="config/mt-sde"
>       ;;
> diff --git a/configure.ac b/configure.ac
> index 5d25dc864c3..1d16530140a 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -2810,6 +2810,12 @@ case "${target}" in
>     spu-*-*)
>       target_makefile_frag="config/mt-spu"
>       ;;
> +  loongarch*-*linux* | loongarch*-*gnu*)
> +    target_makefile_frag="config/mt-loongarch-gnu"
> +    ;;
> +  loongarch*-*elf*)
> +    target_makefile_frag="config/mt-loongarch-elf"
> +    ;;
>     mips*-sde-elf* | mips*-mti-elf* | mips*-img-elf*)
>       target_makefile_frag="config/mt-sde"
>       ;;
> diff --git a/gcc/config.gcc b/gcc/config.gcc
> index b2fe7c7ceef..3a70e64ccd2 100644
> --- a/gcc/config.gcc
> +++ b/gcc/config.gcc
> @@ -2482,7 +2482,7 @@ loongarch*-*-linux*)
>   	tm_file="elfos.h gnu-user.h linux.h linux-android.h glibc-stdint.h ${tm_file}"
>   	tm_file="${tm_file} loongarch/gnu-user.h loongarch/linux.h"
>   	extra_options="${extra_options} linux-android.opt"
> -	tmake_file="${tmake_file} loongarch/t-linux"
> +	tmake_file="${tmake_file} loongarch/t-multilib loongarch/t-linux"
>   	gnu_ld=yes
>   	gas=yes
>   
> @@ -2494,7 +2494,7 @@ loongarch*-*-linux*)
>   loongarch*-*-elf*)
>   	tm_file="elfos.h newlib-stdint.h ${tm_file}"
>   	tm_file="${tm_file} loongarch/elf.h loongarch/linux.h"
> -	tmake_file="${tmake_file} loongarch/t-linux"
> +	tmake_file="${tmake_file} loongarch/t-multilib loongarch/t-linux"
>   	gnu_ld=yes
>   	gas=yes
>   
> @@ -5167,7 +5167,7 @@ case "${target}" in
>   		loongarch_multilib_list_sane=no
>   
>   		# This one goes to TM_MULTILIB_CONFIG, for use in t-linux.
> -		loongarch_multilib_list_make=""
> +		loongarch_multilib_list_make="${abi_base},"
>   
>   		# This one goes to tm_defines, for use in loongarch-driver.c.
>   		loongarch_multilib_list_c=""
> diff --git a/gcc/config/loongarch/loongarch-driver.h b/gcc/config/loongarch/loongarch-driver.h
> index b39b96dec36..d917fcabd37 100644
> --- a/gcc/config/loongarch/loongarch-driver.h
> +++ b/gcc/config/loongarch/loongarch-driver.h
> @@ -23,6 +23,39 @@ along with GCC; see the file COPYING3.  If not see
>   
>   #include "loongarch-str.h"
>   
> +#ifndef SUBTARGET_CPP_SPEC
> +#define SUBTARGET_CPP_SPEC ""
> +#endif
> +
> +#ifndef SUBTARGET_CC1_SPEC
> +#define SUBTARGET_CC1_SPEC ""
> +#endif
> +
> +#ifndef SUBTARGET_ASM_SPEC
> +#define SUBTARGET_ASM_SPEC ""
> +#endif
> +
> +#define EXTRA_SPECS \
> +  {"early_self_spec", ""}, \
> +  {"subtarget_cc1_spec", SUBTARGET_CC1_SPEC}, \
> +  {"subtarget_cpp_spec", SUBTARGET_CPP_SPEC}, \
> +  {"subtarget_asm_spec", SUBTARGET_ASM_SPEC},
> +
> +
> +#undef CPP_SPEC
> +#define CPP_SPEC \
> +  "%(subtarget_cpp_spec)"
> +
> +#undef CC1_SPEC
> +#define CC1_SPEC \
> +  "%{G*} %{,ada:-gnatea %{mabi=*} -gnatez} " \
> +  "%(subtarget_cc1_spec)"
> +
> +#undef ASM_SPEC
> +#define ASM_SPEC \
> +  "%{mabi=*} %(subtarget_asm_spec)"
> +
> +
>   extern const char*
>   la_driver_init (int argc, const char **argv);
>   
> @@ -45,7 +78,16 @@ driver_get_normalized_m_opts (int argc, const char **argv);
>   #define LA_SET_PARM_SPEC(NAME) \
>     " %{m" OPTSTR_##NAME  "=*: %:set_m_parm(" OPTSTR_##NAME " %*)}" \
>   
> +/* For MLIB_SELF_SPECS.  */
> +#include "loongarch-multilib.h"
> +
> +#ifndef MLIB_SELF_SPECS
> +#define MLIB_SELF_SPECS ""
> +#endif
> +
>   #define DRIVER_HANDLE_MACHINE_OPTIONS \
> +  " %(early_self_spec)", \
> +  MLIB_SELF_SPECS \
>     " %:driver_init()" \
>     " %{c|S|E|nostdlib: %:set_no_link()}" \
>     " %{nostartfiles: %{nodefaultlibs: %:set_no_link()}}" \
> diff --git a/gcc/config/loongarch/loongarch.h b/gcc/config/loongarch/loongarch.h
> index 3fc9dc43ab1..ffb20e33bf7 100644
> --- a/gcc/config/loongarch/loongarch.h
> +++ b/gcc/config/loongarch/loongarch.h
> @@ -64,56 +64,6 @@ along with GCC; see the file COPYING3.  If not see
>   #define NM_FLAGS "-Bn"
>   #endif
>   
> -/* SUBTARGET_ASM_SPEC is always passed to the assembler.  It may be
> -   overridden by subtargets.  */
> -
> -#ifndef SUBTARGET_ASM_SPEC
> -#define SUBTARGET_ASM_SPEC ""
> -#endif
> -
> -#undef ASM_SPEC
> -#define ASM_SPEC "%{mabi=*} %{subtarget_asm_spec}"
> -
> -/* Extra switches sometimes passed to the linker.  */
> -
> -#ifndef LINK_SPEC
> -#define LINK_SPEC ""
> -#endif /* LINK_SPEC defined  */
> -
> -/* Specs for the compiler proper.  */
> -
> -/* CC1_SPEC is the set of arguments to pass to the compiler proper.  */
> -
> -#undef CC1_SPEC
> -#define CC1_SPEC "%{,ada:-gnatea} %{m*} \
> -%{G*} \
> -%(subtarget_cc1_spec) %{,ada:-gnatez}"
> -
> -/* Preprocessor specs.  */
> -
> -/* SUBTARGET_CPP_SPEC is passed to the preprocessor.  It may be
> -   overridden by subtargets.  */
> -#ifndef SUBTARGET_CPP_SPEC
> -#define SUBTARGET_CPP_SPEC ""
> -#endif
> -
> -#define CPP_SPEC "%(subtarget_cpp_spec)"
> -
> -/* This macro defines names of additional specifications to put in the specs
> -   that can be used in various specifications like CC1_SPEC.  Its definition
> -   is an initializer with a subgrouping for each command option.
> -
> -   Each subgrouping contains a string constant, that defines the
> -   specification name, and a string constant that used by the GCC driver
> -   program.
> -
> -   Do not define this macro if it does not need to do anything.  */
> -
> -#define EXTRA_SPECS \
> -  {"subtarget_cc1_spec", SUBTARGET_CC1_SPEC}, \
> -  {"subtarget_cpp_spec", SUBTARGET_CPP_SPEC}, \
> -  {"subtarget_asm_spec", SUBTARGET_ASM_SPEC},
> -
>   /* Registers may have a prefix which can be ignored when matching
>      user asm and register definitions.  */
>   #ifndef REGISTER_PREFIX
> diff --git a/gcc/config/loongarch/t-linux b/gcc/config/loongarch/t-linux
> index 9997596d722..5e849c36435 100644
> --- a/gcc/config/loongarch/t-linux
> +++ b/gcc/config/loongarch/t-linux
> @@ -16,68 +16,16 @@
>   # along with GCC; see the file COPYING3.  If not see
>   # <http://www.gnu.org/licenses/>.
>   
> -# Multilib
> -MULTILIB_OPTIONS = mabi=lp64d/mabi=lp64f/mabi=lp64s
> -MULTILIB_DIRNAMES = base/lp64d base/lp64f base/lp64s
> -
> -# The GCC driver always gets all abi-related options on the command line.
> -# (see loongarch-driver.c:driver_get_normalized_m_opts)
> -comma=,
> -MULTILIB_REQUIRED = $(foreach mlib,$(subst $(comma), ,$(TM_MULTILIB_CONFIG)),\
> -	$(firstword $(subst /, ,$(mlib))))
> -
> -SPECS = specs.install
> -
> -# temporary self_spec when building libraries (e.g. libgcc)
> -gen_mlib_spec = $(if $(word 2,$1),\
> -	%{$(firstword $1):$(patsubst %,-%,$(wordlist 2,$(words $1),$1))})
> -
> -# clean up the result of DRIVER_SELF_SPEC to avoid conflict
> -lib_build_self_spec  = %<march=* %<mtune=* %<mcmodel=* %<mfpu=* %<msimd=*
> -
> -# append user-specified build options from --with-multilib-list
> -lib_build_self_spec += $(foreach mlib,\
> -	$(subst $(comma), ,$(TM_MULTILIB_CONFIG)),\
> -	$(call gen_mlib_spec,$(subst /, ,$(mlib))))
> -
> -specs: specs.install
> -	sed '/^*self_spec:$$/{ n;s/^$$/$(lib_build_self_spec)/g; }' $< > $@
> -
> -# Do some preparation before regression tests:
> -# remove lib-build-specs / make symlinks for the toplevel multilib variant
> -
> -LA_DEFAULT_MULTISUBDIR = $(shell $(GCC_FOR_TARGET) --print-multi-dir)
> -.PHONY: remove-lib-specs
> -check check-host check-target $(CHECK_TARGETS) $(lang_checks): remove-lib-specs
> -remove-lib-specs:
> -	-mv -f specs.install specs 2>/dev/null
> -	-mv $(LA_DEFAULT_MULTISUBDIR)/* ./
> -	-mkdir -p ../$(target_noncanonical)/`dirname $(LA_DEFAULT_MULTISUBDIR)`
> -	-$(LN_S) .. ../$(target_noncanonical)/$(LA_DEFAULT_MULTISUBDIR)
> -
> -# Multiarch
> -ifneq ($(call if_multiarch,yes),yes)
> -    # Define LA_DISABLE_MULTIARCH if multiarch is disabled.
> -    tm_defines += LA_DISABLE_MULTIARCH
> -else
> -    # Only define MULTIARCH_DIRNAME when multiarch is enabled,
> -    # or it would always introduce ${target} into the search path.
> -    MULTIARCH_DIRNAME = $(LA_MULTIARCH_TRIPLET)
> -endif
> +MULTIOSDIR_lp64d := ../lib64$(call if_multiarch,:loongarch64-linux-gnu)
> +MULTIOSDIR_lp64f := ../lib64/f32$(call if_multiarch,:loongarch64-linux-gnuf32)
> +MULTIOSDIR_lp64s := ../lib64/sf$(call if_multiarch,:loongarch64-linux-gnusf)
>   
>   # Don't define MULTILIB_OSDIRNAMES if multilib is disabled.
>   ifeq ($(filter LA_DISABLE_MULTILIB,$(tm_defines)),)
>   
> -    MULTILIB_OSDIRNAMES = \
> -      mabi.lp64d=../lib64$\
> -      $(call if_multiarch,:loongarch64-linux-gnu)
> -
> -    MULTILIB_OSDIRNAMES += \
> -      mabi.lp64f=../lib64/f32$\
> -      $(call if_multiarch,:loongarch64-linux-gnuf32)
> -
> -    MULTILIB_OSDIRNAMES += \
> -      mabi.lp64s=../lib64/sf$\
> -      $(call if_multiarch,:loongarch64-linux-gnusf)
> +    MULTILIB_OSDIRNAMES = .=$(MULTIOSDIR_$(mlib_default))
> +    MULTILIB_OSDIRNAMES += mabi.lp64d=$(MULTIOSDIR_lp64d)
> +    MULTILIB_OSDIRNAMES += mabi.lp64f=$(MULTIOSDIR_lp64f)
> +    MULTILIB_OSDIRNAMES += mabi.lp64s=$(MULTIOSDIR_lp64s)
>   
>   endif
> diff --git a/gcc/config/loongarch/t-loongarch b/gcc/config/loongarch/t-loongarch
> index 43088a05643..9b06fa84bcc 100644
> --- a/gcc/config/loongarch/t-loongarch
> +++ b/gcc/config/loongarch/t-loongarch
> @@ -16,7 +16,7 @@
>   # along with GCC; see the file COPYING3.  If not see
>   # <http://www.gnu.org/licenses/>.
>   
> -TM_H += $(srcdir)/config/loongarch/loongarch-driver.h
> +TM_H += loongarch-multilib.h $(srcdir)/config/loongarch/loongarch-driver.h
>   OPTIONS_H_EXTRA += $(srcdir)/config/loongarch/loongarch-def.h \
>   		   $(srcdir)/config/loongarch/loongarch-tune.h
>   
> diff --git a/gcc/config/loongarch/t-multilib b/gcc/config/loongarch/t-multilib
> new file mode 100644
> index 00000000000..bf6c182987b
> --- /dev/null
> +++ b/gcc/config/loongarch/t-multilib
> @@ -0,0 +1,68 @@
> +# Copyright (C) 2023 Free Software Foundation, Inc.
> +#
> +# This file is part of GCC.
> +#
> +# GCC is free software; you can redistribute it and/or modify
> +# it under the terms of the GNU General Public License as published by
> +# the Free Software Foundation; either version 3, or (at your option)
> +# any later version.
> +#
> +# GCC is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +# GNU General Public License for more details.
> +#
> +# You should have received a copy of the GNU General Public License
> +# along with GCC; see the file COPYING3.  If not see
> +# <http://www.gnu.org/licenses/>.
> +
> +# Helper definitions
> +comma=,
> +null  :=
> +space := $(null) #
> +exclude_1st = $(wordlist 2,$(words $1),$1)
> +
> +# Common definitions
> +mlib_all := lp64d lp64f lp64s
> +$(foreach i,$(mlib_all),$(eval MULTISUBDIR_$i := base/$i))
> +
> +mlib_default := $(firstword $(subst $(comma), ,$(TM_MULTILIB_CONFIG)))
> +mlib_all := $(filter-out $(mlib_default),$(mlib_all))
> +
> +MULTILIB_OPTIONS := $(subst $(space),/,$(foreach i,$(mlib_all),mabi=$(i)))
> +MULTILIB_DIRNAMES := $(foreach i,$(mlib_all),$(MULTISUBDIR_$(i)))
> +
> +# Customize builds with --with-multilib-list
> +MULTILIB_REQUIRED := $(foreach i,$(call exclude_1st,\
> +	$(subst $(comma), ,$(TM_MULTILIB_CONFIG))),\
> +	$(firstword $(subst /, ,$(i))))
> +
> +## spec rules for building libraries, triggered by -fmultiflags
> +gen_mlib_spec = $(if $(word 2,$1),\
> +	%{$(firstword $1):$(patsubst %,-%,$(call exclude_1st,$1)}))
> +
> +lib_build_spec = $(foreach mlib,\
> +	$(call exclude_1st,$(subst $(comma), ,$(TM_MULTILIB_CONFIG))),\
> +	$(call gen_mlib_spec,$(subst /, ,$(mlib))))
> +
> +default_mlib_spec := %{fmultiflags:%{!mabi=*:-mabi=$(mlib_default)}}
> +lib_build_spec    := %{fmultiflags:$(lib_build_spec)}
> +
> +ifneq ($(TM_MULTILIB_CONFIG),)
> +loongarch-multilib.h:
> +	@echo "#define MLIB_SELF_SPECS" \
> +	      "\"$(default_mlib_spec)\"," \
> +	      "\"$(lib_build_spec)\"," > $@
> +else
> +loongarch-multilib.h: ; @touch $@
> +endif
> +
> +# Multiarch
> +ifneq ($(call if_multiarch,yes),yes)
> +    # Define LA_DISABLE_MULTIARCH if multiarch is disabled.
> +    tm_defines += LA_DISABLE_MULTIARCH
> +else
> +    # Only define MULTIARCH_DIRNAME when multiarch is enabled,
> +    # or it would always introduce ${target} into the search path.
> +    MULTIARCH_DIRNAME = $(LA_MULTIARCH_TRIPLET)
> +endif
Jan-Benedict Glaw Oct. 4, 2023, 12:13 p.m. UTC | #2
Hi!

On Wed, 2023-09-13 17:52:14 +0800, Yang Yujie <yangyujie@loongson.cn> wrote:
> Library build options from --with-multilib-list used to be processed with
> *self_spec, which missed the driver's initial canonicalization.  This
> caused limitations on CFLAGS override and the use of driver-only options
> like -m[no]-lsx.
> 
> The problem is solved by promoting the injection rules of --with-multilib-list
> options to the first element of DRIVER_SELF_SPECS, to make them execute before
> the canonialization.  The library-build options are also hard-coded in
> the driver and can be used conveniently by the builders of other non-gcc
> libraries via the use of -fmultiflags.
> 
> Bootstrapped and tested on loongarch64-linux-gnu.

Seems this breaks for me with

../gcc/configure [...] --enable-werror-always --enable-languages=all --disable-gcov --disable-shared --disable-threads --target=loongarch64-linux-gnuf32 --without-headers
make V=1 all-gcc


See eg. http://toolchain.lug-owl.de/laminar/jobs/gcc-loongarch64-linux-gnuf32/44 :

/var/lib/laminar/run/gcc-loongarch64-linux-gnuf32/44/local-toolchain-install/bin/g++ -c   -g -O2   -DIN_GCC -DCROSS_DIRECTORY_STRUCTURE   -fno-exceptions -fno-rtti -fasynchronous-unwind-tables -W -Wall -Wno-narrowing -Wwrite-strings -Wcast-qual -Wmissing-format-attribute -Wconditionally-supported -Woverloaded-virtual -pedantic -Wno-long-long -Wno-variadic-macros -Wno-overlength-strings -Werror -fno-common  -DHAVE_CONFIG_H  -DGENERATOR_FILE -I. -Ibuild -I../../gcc/gcc -I../../gcc/gcc/build -I../../gcc/gcc/../include  -I../../gcc/gcc/../libcpp/include  \
 -o build/genpreds.o ../../gcc/gcc/genpreds.cc
In file included from ../../gcc/gcc/config/loongarch/loongarch.h:53,
                 from ./tm.h:50,
                 from ../../gcc/gcc/genpreds.cc:26:
../../gcc/gcc/config/loongarch/loongarch-driver.h:82:10: fatal error: loongarch-multilib.h: No such file or directory
   82 | #include "loongarch-multilib.h"
      |          ^~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make[1]: *** [Makefile:2966: build/genpreds.o] Error 1
make[1]: Leaving directory '/var/lib/laminar/run/gcc-loongarch64-linux-gnuf32/44/toolchain-build/gcc'
make: *** [Makefile:4659: all-gcc] Error 2


So it failed to execute the t-multilib fragment? Happens for all my
loongarch compilation tests:

http://toolchain.lug-owl.de/laminar/jobs/gcc-loongarch64-linux/45
http://toolchain.lug-owl.de/laminar/jobs/gcc-loongarch64-linux-gnuf32/44
http://toolchain.lug-owl.de/laminar/jobs/gcc-loongarch64-linux-gnuf64/44
http://toolchain.lug-owl.de/laminar/jobs/gcc-loongarch64-linux-gnusf/44

And when this is fixed, it might be a nice idea to have a
--with-multilib-list config in ./contrib/config-list.mk .

MfG, JBG

--
Yang Yujie Oct. 7, 2023, 3:41 a.m. UTC | #3
On Wed, Oct 04, 2023 at 02:13:46PM +0200, Jan-Benedict Glaw wrote:
> Seems this breaks for me with
> 
> ../gcc/configure [...] --enable-werror-always --enable-languages=all --disable-gcov --disable-shared --disable-threads --target=loongarch64-linux-gnuf32 --without-headers
> make V=1 all-gcc
> 
> 
> See eg. http://toolchain.lug-owl.de/laminar/jobs/gcc-loongarch64-linux-gnuf32/44 :
> 
> /var/lib/laminar/run/gcc-loongarch64-linux-gnuf32/44/local-toolchain-install/bin/g++ -c   -g -O2   -DIN_GCC -DCROSS_DIRECTORY_STRUCTURE   -fno-exceptions -fno-rtti -fasynchronous-unwind-tables -W -Wall -Wno-narrowing -Wwrite-strings -Wcast-qual -Wmissing-format-attribute -Wconditionally-supported -Woverloaded-virtual -pedantic -Wno-long-long -Wno-variadic-macros -Wno-overlength-strings -Werror -fno-common  -DHAVE_CONFIG_H  -DGENERATOR_FILE -I. -Ibuild -I../../gcc/gcc -I../../gcc/gcc/build -I../../gcc/gcc/../include  -I../../gcc/gcc/../libcpp/include  \
>  -o build/genpreds.o ../../gcc/gcc/genpreds.cc
> In file included from ../../gcc/gcc/config/loongarch/loongarch.h:53,
>                  from ./tm.h:50,
>                  from ../../gcc/gcc/genpreds.cc:26:
> ../../gcc/gcc/config/loongarch/loongarch-driver.h:82:10: fatal error: loongarch-multilib.h: No such file or directory
>    82 | #include "loongarch-multilib.h"
>       |          ^~~~~~~~~~~~~~~~~~~~~~
> compilation terminated.
> make[1]: *** [Makefile:2966: build/genpreds.o] Error 1
> make[1]: Leaving directory '/var/lib/laminar/run/gcc-loongarch64-linux-gnuf32/44/toolchain-build/gcc'
> make: *** [Makefile:4659: all-gcc] Error 2
> 
> 
> So it failed to execute the t-multilib fragment? Happens for all my
> loongarch compilation tests:
> 
> http://toolchain.lug-owl.de/laminar/jobs/gcc-loongarch64-linux/45
> http://toolchain.lug-owl.de/laminar/jobs/gcc-loongarch64-linux-gnuf32/44
> http://toolchain.lug-owl.de/laminar/jobs/gcc-loongarch64-linux-gnuf64/44
> http://toolchain.lug-owl.de/laminar/jobs/gcc-loongarch64-linux-gnusf/44
>

Thanks for the testing!

This error seems to be difficult to reproduce since it is a makefile dependency
problem.  I think appending loongarch-multilib.h to $(GTM_H) instead of $(TM_H)
could help.

> And when this is fixed, it might be a nice idea to have a
> --with-multilib-list config in ./contrib/config-list.mk .

Thanks, will add this later too.

P.S. Currently support for "f32" is not active, and it should probably be
avoided if you want to build a working rootfs.

Yujie
Xi Ruoyao Oct. 7, 2023, 7:08 a.m. UTC | #4
On Sat, 2023-10-07 at 11:41 +0800, Yang Yujie wrote:
> Thanks for the testing!
> 
> This error seems to be difficult to reproduce since it is a makefile dependency
> problem.  I think appending loongarch-multilib.h to $(GTM_H) instead of $(TM_H)
> could help.

FWIW such issues are easier to reproduce with a high -j number.  I can
easily reproduce it with -j32 on a 3C5000-based server.

> > And when this is fixed, it might be a nice idea to have a
> > --with-multilib-list config in ./contrib/config-list.mk .
> 
> Thanks, will add this later too.
> 
> P.S. Currently support for "f32" is not active, and it should probably be
> avoided if you want to build a working rootfs.
Jan-Benedict Glaw Oct. 7, 2023, 8:03 a.m. UTC | #5
Hi!

On Sat, 2023-10-07 15:08:34 +0800, Xi Ruoyao <xry111@xry111.site> wrote:
> On Sat, 2023-10-07 at 11:41 +0800, Yang Yujie wrote:
> > Thanks for the testing!
> > 
> > This error seems to be difficult to reproduce since it is a makefile dependency
> > problem.  I think appending loongarch-multilib.h to $(GTM_H) instead of $(TM_H)
> > could help.
> 
> FWIW such issues are easier to reproduce with a high -j number.  I can
> easily reproduce it with -j32 on a 3C5000-based server.

That's interesting. It showed up on all of my CI builds and I don't do
parallel builds at all (as I'd like to be able to `diff` the build
logs after sanitizing eg. timestamps and the build path.)

MfG, JBG

--
diff mbox series

Patch

diff --git a/config-ml.in b/config-ml.in
index ad0db078171..68854a4f16c 100644
--- a/config-ml.in
+++ b/config-ml.in
@@ -301,16 +301,6 @@  arm-*-*)
 	  done
 	fi
 	;;
-loongarch*-*)
-	old_multidirs="${multidirs}"
-	multidirs=""
-	for x in ${old_multidirs}; do
-	case "$x" in
-	`${CC-gcc} --print-multi-directory`) : ;;
-	*) multidirs="${multidirs} ${x}" ;;
-	esac
-	done
-	;;
 m68*-*-*)
 	if [ x$enable_softfloat = xno ]
 	then
diff --git a/config/mt-loongarch-elf b/config/mt-loongarch-elf
new file mode 100644
index 00000000000..bbf29bb578a
--- /dev/null
+++ b/config/mt-loongarch-elf
@@ -0,0 +1 @@ 
+include $(srcdir)/config/mt-loongarch-mlib
diff --git a/config/mt-loongarch-gnu b/config/mt-loongarch-gnu
new file mode 100644
index 00000000000..dfefb44ede3
--- /dev/null
+++ b/config/mt-loongarch-gnu
@@ -0,0 +1,2 @@ 
+include $(srcdir)/config/mt-gnu
+include $(srcdir)/config/mt-loongarch-mlib
diff --git a/config/mt-loongarch-mlib b/config/mt-loongarch-mlib
new file mode 100644
index 00000000000..4cfe568f1fc
--- /dev/null
+++ b/config/mt-loongarch-mlib
@@ -0,0 +1 @@ 
+FLAGS_FOR_TARGET += -fmultiflags
diff --git a/configure b/configure
index 28f0913bdd4..8fc163d36bd 100755
--- a/configure
+++ b/configure
@@ -9683,6 +9683,12 @@  case "${target}" in
   spu-*-*)
     target_makefile_frag="config/mt-spu"
     ;;
+  loongarch*-*linux* | loongarch*-*gnu*)
+    target_makefile_frag="config/mt-loongarch-gnu"
+    ;;
+  loongarch*-*elf*)
+    target_makefile_frag="config/mt-loongarch-elf"
+    ;;
   mips*-sde-elf* | mips*-mti-elf* | mips*-img-elf*)
     target_makefile_frag="config/mt-sde"
     ;;
diff --git a/configure.ac b/configure.ac
index 5d25dc864c3..1d16530140a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2810,6 +2810,12 @@  case "${target}" in
   spu-*-*)
     target_makefile_frag="config/mt-spu"
     ;;
+  loongarch*-*linux* | loongarch*-*gnu*)
+    target_makefile_frag="config/mt-loongarch-gnu"
+    ;;
+  loongarch*-*elf*)
+    target_makefile_frag="config/mt-loongarch-elf"
+    ;;
   mips*-sde-elf* | mips*-mti-elf* | mips*-img-elf*)
     target_makefile_frag="config/mt-sde"
     ;;
diff --git a/gcc/config.gcc b/gcc/config.gcc
index b2fe7c7ceef..3a70e64ccd2 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -2482,7 +2482,7 @@  loongarch*-*-linux*)
 	tm_file="elfos.h gnu-user.h linux.h linux-android.h glibc-stdint.h ${tm_file}"
 	tm_file="${tm_file} loongarch/gnu-user.h loongarch/linux.h"
 	extra_options="${extra_options} linux-android.opt"
-	tmake_file="${tmake_file} loongarch/t-linux"
+	tmake_file="${tmake_file} loongarch/t-multilib loongarch/t-linux"
 	gnu_ld=yes
 	gas=yes
 
@@ -2494,7 +2494,7 @@  loongarch*-*-linux*)
 loongarch*-*-elf*)
 	tm_file="elfos.h newlib-stdint.h ${tm_file}"
 	tm_file="${tm_file} loongarch/elf.h loongarch/linux.h"
-	tmake_file="${tmake_file} loongarch/t-linux"
+	tmake_file="${tmake_file} loongarch/t-multilib loongarch/t-linux"
 	gnu_ld=yes
 	gas=yes
 
@@ -5167,7 +5167,7 @@  case "${target}" in
 		loongarch_multilib_list_sane=no
 
 		# This one goes to TM_MULTILIB_CONFIG, for use in t-linux.
-		loongarch_multilib_list_make=""
+		loongarch_multilib_list_make="${abi_base},"
 
 		# This one goes to tm_defines, for use in loongarch-driver.c.
 		loongarch_multilib_list_c=""
diff --git a/gcc/config/loongarch/loongarch-driver.h b/gcc/config/loongarch/loongarch-driver.h
index b39b96dec36..d917fcabd37 100644
--- a/gcc/config/loongarch/loongarch-driver.h
+++ b/gcc/config/loongarch/loongarch-driver.h
@@ -23,6 +23,39 @@  along with GCC; see the file COPYING3.  If not see
 
 #include "loongarch-str.h"
 
+#ifndef SUBTARGET_CPP_SPEC
+#define SUBTARGET_CPP_SPEC ""
+#endif
+
+#ifndef SUBTARGET_CC1_SPEC
+#define SUBTARGET_CC1_SPEC ""
+#endif
+
+#ifndef SUBTARGET_ASM_SPEC
+#define SUBTARGET_ASM_SPEC ""
+#endif
+
+#define EXTRA_SPECS \
+  {"early_self_spec", ""}, \
+  {"subtarget_cc1_spec", SUBTARGET_CC1_SPEC}, \
+  {"subtarget_cpp_spec", SUBTARGET_CPP_SPEC}, \
+  {"subtarget_asm_spec", SUBTARGET_ASM_SPEC},
+
+
+#undef CPP_SPEC
+#define CPP_SPEC \
+  "%(subtarget_cpp_spec)"
+
+#undef CC1_SPEC
+#define CC1_SPEC \
+  "%{G*} %{,ada:-gnatea %{mabi=*} -gnatez} " \
+  "%(subtarget_cc1_spec)"
+
+#undef ASM_SPEC
+#define ASM_SPEC \
+  "%{mabi=*} %(subtarget_asm_spec)"
+
+
 extern const char*
 la_driver_init (int argc, const char **argv);
 
@@ -45,7 +78,16 @@  driver_get_normalized_m_opts (int argc, const char **argv);
 #define LA_SET_PARM_SPEC(NAME) \
   " %{m" OPTSTR_##NAME  "=*: %:set_m_parm(" OPTSTR_##NAME " %*)}" \
 
+/* For MLIB_SELF_SPECS.  */
+#include "loongarch-multilib.h"
+
+#ifndef MLIB_SELF_SPECS
+#define MLIB_SELF_SPECS ""
+#endif
+
 #define DRIVER_HANDLE_MACHINE_OPTIONS \
+  " %(early_self_spec)", \
+  MLIB_SELF_SPECS \
   " %:driver_init()" \
   " %{c|S|E|nostdlib: %:set_no_link()}" \
   " %{nostartfiles: %{nodefaultlibs: %:set_no_link()}}" \
diff --git a/gcc/config/loongarch/loongarch.h b/gcc/config/loongarch/loongarch.h
index 3fc9dc43ab1..ffb20e33bf7 100644
--- a/gcc/config/loongarch/loongarch.h
+++ b/gcc/config/loongarch/loongarch.h
@@ -64,56 +64,6 @@  along with GCC; see the file COPYING3.  If not see
 #define NM_FLAGS "-Bn"
 #endif
 
-/* SUBTARGET_ASM_SPEC is always passed to the assembler.  It may be
-   overridden by subtargets.  */
-
-#ifndef SUBTARGET_ASM_SPEC
-#define SUBTARGET_ASM_SPEC ""
-#endif
-
-#undef ASM_SPEC
-#define ASM_SPEC "%{mabi=*} %{subtarget_asm_spec}"
-
-/* Extra switches sometimes passed to the linker.  */
-
-#ifndef LINK_SPEC
-#define LINK_SPEC ""
-#endif /* LINK_SPEC defined  */
-
-/* Specs for the compiler proper.  */
-
-/* CC1_SPEC is the set of arguments to pass to the compiler proper.  */
-
-#undef CC1_SPEC
-#define CC1_SPEC "%{,ada:-gnatea} %{m*} \
-%{G*} \
-%(subtarget_cc1_spec) %{,ada:-gnatez}"
-
-/* Preprocessor specs.  */
-
-/* SUBTARGET_CPP_SPEC is passed to the preprocessor.  It may be
-   overridden by subtargets.  */
-#ifndef SUBTARGET_CPP_SPEC
-#define SUBTARGET_CPP_SPEC ""
-#endif
-
-#define CPP_SPEC "%(subtarget_cpp_spec)"
-
-/* This macro defines names of additional specifications to put in the specs
-   that can be used in various specifications like CC1_SPEC.  Its definition
-   is an initializer with a subgrouping for each command option.
-
-   Each subgrouping contains a string constant, that defines the
-   specification name, and a string constant that used by the GCC driver
-   program.
-
-   Do not define this macro if it does not need to do anything.  */
-
-#define EXTRA_SPECS \
-  {"subtarget_cc1_spec", SUBTARGET_CC1_SPEC}, \
-  {"subtarget_cpp_spec", SUBTARGET_CPP_SPEC}, \
-  {"subtarget_asm_spec", SUBTARGET_ASM_SPEC},
-
 /* Registers may have a prefix which can be ignored when matching
    user asm and register definitions.  */
 #ifndef REGISTER_PREFIX
diff --git a/gcc/config/loongarch/t-linux b/gcc/config/loongarch/t-linux
index 9997596d722..5e849c36435 100644
--- a/gcc/config/loongarch/t-linux
+++ b/gcc/config/loongarch/t-linux
@@ -16,68 +16,16 @@ 
 # along with GCC; see the file COPYING3.  If not see
 # <http://www.gnu.org/licenses/>.
 
-# Multilib
-MULTILIB_OPTIONS = mabi=lp64d/mabi=lp64f/mabi=lp64s
-MULTILIB_DIRNAMES = base/lp64d base/lp64f base/lp64s
-
-# The GCC driver always gets all abi-related options on the command line.
-# (see loongarch-driver.c:driver_get_normalized_m_opts)
-comma=,
-MULTILIB_REQUIRED = $(foreach mlib,$(subst $(comma), ,$(TM_MULTILIB_CONFIG)),\
-	$(firstword $(subst /, ,$(mlib))))
-
-SPECS = specs.install
-
-# temporary self_spec when building libraries (e.g. libgcc)
-gen_mlib_spec = $(if $(word 2,$1),\
-	%{$(firstword $1):$(patsubst %,-%,$(wordlist 2,$(words $1),$1))})
-
-# clean up the result of DRIVER_SELF_SPEC to avoid conflict
-lib_build_self_spec  = %<march=* %<mtune=* %<mcmodel=* %<mfpu=* %<msimd=*
-
-# append user-specified build options from --with-multilib-list
-lib_build_self_spec += $(foreach mlib,\
-	$(subst $(comma), ,$(TM_MULTILIB_CONFIG)),\
-	$(call gen_mlib_spec,$(subst /, ,$(mlib))))
-
-specs: specs.install
-	sed '/^*self_spec:$$/{ n;s/^$$/$(lib_build_self_spec)/g; }' $< > $@
-
-# Do some preparation before regression tests:
-# remove lib-build-specs / make symlinks for the toplevel multilib variant
-
-LA_DEFAULT_MULTISUBDIR = $(shell $(GCC_FOR_TARGET) --print-multi-dir)
-.PHONY: remove-lib-specs
-check check-host check-target $(CHECK_TARGETS) $(lang_checks): remove-lib-specs
-remove-lib-specs:
-	-mv -f specs.install specs 2>/dev/null
-	-mv $(LA_DEFAULT_MULTISUBDIR)/* ./
-	-mkdir -p ../$(target_noncanonical)/`dirname $(LA_DEFAULT_MULTISUBDIR)`
-	-$(LN_S) .. ../$(target_noncanonical)/$(LA_DEFAULT_MULTISUBDIR)
-
-# Multiarch
-ifneq ($(call if_multiarch,yes),yes)
-    # Define LA_DISABLE_MULTIARCH if multiarch is disabled.
-    tm_defines += LA_DISABLE_MULTIARCH
-else
-    # Only define MULTIARCH_DIRNAME when multiarch is enabled,
-    # or it would always introduce ${target} into the search path.
-    MULTIARCH_DIRNAME = $(LA_MULTIARCH_TRIPLET)
-endif
+MULTIOSDIR_lp64d := ../lib64$(call if_multiarch,:loongarch64-linux-gnu)
+MULTIOSDIR_lp64f := ../lib64/f32$(call if_multiarch,:loongarch64-linux-gnuf32)
+MULTIOSDIR_lp64s := ../lib64/sf$(call if_multiarch,:loongarch64-linux-gnusf)
 
 # Don't define MULTILIB_OSDIRNAMES if multilib is disabled.
 ifeq ($(filter LA_DISABLE_MULTILIB,$(tm_defines)),)
 
-    MULTILIB_OSDIRNAMES = \
-      mabi.lp64d=../lib64$\
-      $(call if_multiarch,:loongarch64-linux-gnu)
-
-    MULTILIB_OSDIRNAMES += \
-      mabi.lp64f=../lib64/f32$\
-      $(call if_multiarch,:loongarch64-linux-gnuf32)
-
-    MULTILIB_OSDIRNAMES += \
-      mabi.lp64s=../lib64/sf$\
-      $(call if_multiarch,:loongarch64-linux-gnusf)
+    MULTILIB_OSDIRNAMES = .=$(MULTIOSDIR_$(mlib_default))
+    MULTILIB_OSDIRNAMES += mabi.lp64d=$(MULTIOSDIR_lp64d)
+    MULTILIB_OSDIRNAMES += mabi.lp64f=$(MULTIOSDIR_lp64f)
+    MULTILIB_OSDIRNAMES += mabi.lp64s=$(MULTIOSDIR_lp64s)
 
 endif
diff --git a/gcc/config/loongarch/t-loongarch b/gcc/config/loongarch/t-loongarch
index 43088a05643..9b06fa84bcc 100644
--- a/gcc/config/loongarch/t-loongarch
+++ b/gcc/config/loongarch/t-loongarch
@@ -16,7 +16,7 @@ 
 # along with GCC; see the file COPYING3.  If not see
 # <http://www.gnu.org/licenses/>.
 
-TM_H += $(srcdir)/config/loongarch/loongarch-driver.h
+TM_H += loongarch-multilib.h $(srcdir)/config/loongarch/loongarch-driver.h
 OPTIONS_H_EXTRA += $(srcdir)/config/loongarch/loongarch-def.h \
 		   $(srcdir)/config/loongarch/loongarch-tune.h
 
diff --git a/gcc/config/loongarch/t-multilib b/gcc/config/loongarch/t-multilib
new file mode 100644
index 00000000000..bf6c182987b
--- /dev/null
+++ b/gcc/config/loongarch/t-multilib
@@ -0,0 +1,68 @@ 
+# Copyright (C) 2023 Free Software Foundation, Inc.
+#
+# This file is part of GCC.
+#
+# GCC is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+#
+# GCC is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GCC; see the file COPYING3.  If not see
+# <http://www.gnu.org/licenses/>.
+
+# Helper definitions
+comma=,
+null  :=
+space := $(null) #
+exclude_1st = $(wordlist 2,$(words $1),$1)
+
+# Common definitions
+mlib_all := lp64d lp64f lp64s
+$(foreach i,$(mlib_all),$(eval MULTISUBDIR_$i := base/$i))
+
+mlib_default := $(firstword $(subst $(comma), ,$(TM_MULTILIB_CONFIG)))
+mlib_all := $(filter-out $(mlib_default),$(mlib_all))
+
+MULTILIB_OPTIONS := $(subst $(space),/,$(foreach i,$(mlib_all),mabi=$(i)))
+MULTILIB_DIRNAMES := $(foreach i,$(mlib_all),$(MULTISUBDIR_$(i)))
+
+# Customize builds with --with-multilib-list
+MULTILIB_REQUIRED := $(foreach i,$(call exclude_1st,\
+	$(subst $(comma), ,$(TM_MULTILIB_CONFIG))),\
+	$(firstword $(subst /, ,$(i))))
+
+## spec rules for building libraries, triggered by -fmultiflags
+gen_mlib_spec = $(if $(word 2,$1),\
+	%{$(firstword $1):$(patsubst %,-%,$(call exclude_1st,$1)}))
+
+lib_build_spec = $(foreach mlib,\
+	$(call exclude_1st,$(subst $(comma), ,$(TM_MULTILIB_CONFIG))),\
+	$(call gen_mlib_spec,$(subst /, ,$(mlib))))
+
+default_mlib_spec := %{fmultiflags:%{!mabi=*:-mabi=$(mlib_default)}}
+lib_build_spec    := %{fmultiflags:$(lib_build_spec)}
+
+ifneq ($(TM_MULTILIB_CONFIG),)
+loongarch-multilib.h:
+	@echo "#define MLIB_SELF_SPECS" \
+	      "\"$(default_mlib_spec)\"," \
+	      "\"$(lib_build_spec)\"," > $@
+else
+loongarch-multilib.h: ; @touch $@
+endif
+
+# Multiarch
+ifneq ($(call if_multiarch,yes),yes)
+    # Define LA_DISABLE_MULTIARCH if multiarch is disabled.
+    tm_defines += LA_DISABLE_MULTIARCH
+else
+    # Only define MULTIARCH_DIRNAME when multiarch is enabled,
+    # or it would always introduce ${target} into the search path.
+    MULTIARCH_DIRNAME = $(LA_MULTIARCH_TRIPLET)
+endif