xref: /OK3568_Linux_fs/buildroot/toolchain/toolchain-external/pkg-toolchain-external.mk (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1################################################################################
2# External toolchain package infrastructure
3#
4# This package infrastructure implements the support for external
5# toolchains, i.e toolchains that are available pre-built, ready to
6# use. Such toolchain may either be readily available on the Web
7# (Linaro, Sourcery CodeBench, from processor vendors) or may be built
8# with tools like Crosstool-NG or Buildroot itself. So far, we have
9# tested this with:
10#
11#  * Toolchains generated by Crosstool-NG
12#  * Toolchains generated by Buildroot
13#  * Toolchains provided by Linaro for the ARM and AArch64
14#    architectures
15#  * Sourcery CodeBench toolchains (from Mentor Graphics) for the ARM,
16#    MIPS, PowerPC, x86_64 and NIOS 2 architectures. For the MIPS
17#    toolchain, the -muclibc variant isn't supported yet, only the
18#    default glibc-based variant is.
19#  * Synopsys DesignWare toolchains for ARC cores
20#
21# The basic principle is the following
22#
23#  1. If the toolchain is not pre-installed, download and extract it
24#  in $(TOOLCHAIN_EXTERNAL_INSTALL_DIR). Otherwise,
25#  $(TOOLCHAIN_EXTERNAL_INSTALL_DIR) points to were the toolchain has
26#  already been installed by the user.
27#
28#  2. For all external toolchains, perform some checks on the
29#  conformity between the toolchain configuration described in the
30#  Buildroot menuconfig system, and the real configuration of the
31#  external toolchain. This is for example important to make sure that
32#  the Buildroot configuration system knows whether the toolchain
33#  supports RPC, IPv6, locales, large files, etc. Unfortunately, these
34#  things cannot be detected automatically, since the value of these
35#  options (such as BR2_TOOLCHAIN_HAS_NATIVE_RPC) are needed at
36#  configuration time because these options are used as dependencies
37#  for other options. And at configuration time, we are not able to
38#  retrieve the external toolchain configuration.
39#
40#  3. Copy the libraries needed at runtime to the target directory,
41#  $(TARGET_DIR). Obviously, things such as the C library, the dynamic
42#  loader and a few other utility libraries are needed if dynamic
43#  applications are to be executed on the target system.
44#
45#  4. Copy the libraries and headers to the staging directory. This
46#  will allow all further calls to gcc to be made using --sysroot
47#  $(STAGING_DIR), which greatly simplifies the compilation of the
48#  packages when using external toolchains. So in the end, only the
49#  cross-compiler binaries remains external, all libraries and headers
50#  are imported into the Buildroot tree.
51#
52#  5. Build a toolchain wrapper which executes the external toolchain
53#  with a number of arguments (sysroot/march/mtune/..) hardcoded,
54#  so we're sure the correct configuration is always used and the
55#  toolchain behaves similar to an internal toolchain.
56#  This toolchain wrapper and symlinks are installed into
57#  $(HOST_DIR)/bin like for the internal toolchains, and the rest
58#  of Buildroot is handled identical for the 2 toolchain types.
59################################################################################
60
61#
62# Definitions of where the toolchain can be found
63#
64
65TOOLCHAIN_EXTERNAL_PREFIX = $(call qstrip,$(BR2_TOOLCHAIN_EXTERNAL_PREFIX))
66TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR = $(HOST_DIR)/opt/ext-toolchain
67
68ifeq ($(BR2_TOOLCHAIN_EXTERNAL_DOWNLOAD),y)
69TOOLCHAIN_EXTERNAL_INSTALL_DIR = $(TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR)
70else
71TOOLCHAIN_EXTERNAL_INSTALL_DIR = $(call qstrip,$(BR2_TOOLCHAIN_EXTERNAL_PATH))
72endif
73
74ifeq ($(TOOLCHAIN_EXTERNAL_INSTALL_DIR),)
75ifneq ($(TOOLCHAIN_EXTERNAL_PREFIX),)
76# if no path set, figure it out from path
77TOOLCHAIN_EXTERNAL_BIN := $(dir $(shell which $(TOOLCHAIN_EXTERNAL_PREFIX)-gcc))
78endif
79else
80TOOLCHAIN_EXTERNAL_REL_BIN_PATH = $(call qstrip,$(BR2_TOOLCHAIN_EXTERNAL_REL_BIN_PATH))
81ifeq ($(TOOLCHAIN_EXTERNAL_REL_BIN_PATH),)
82TOOLCHAIN_EXTERNAL_REL_BIN_PATH = bin
83endif
84TOOLCHAIN_EXTERNAL_BIN = $(TOOLCHAIN_EXTERNAL_INSTALL_DIR)/$(TOOLCHAIN_EXTERNAL_REL_BIN_PATH)
85endif
86
87# If this is a buildroot toolchain, it already has a wrapper which we want to
88# bypass. Since this is only evaluated after it has been extracted, we can use
89# $(wildcard ...) here.
90TOOLCHAIN_EXTERNAL_SUFFIX = \
91	$(if $(wildcard $(TOOLCHAIN_EXTERNAL_BIN)/*.br_real),.br_real)
92
93TOOLCHAIN_EXTERNAL_CROSS = $(TOOLCHAIN_EXTERNAL_BIN)/$(TOOLCHAIN_EXTERNAL_PREFIX)-
94TOOLCHAIN_EXTERNAL_CC = $(TOOLCHAIN_EXTERNAL_CROSS)gcc$(TOOLCHAIN_EXTERNAL_SUFFIX)
95TOOLCHAIN_EXTERNAL_CXX = $(TOOLCHAIN_EXTERNAL_CROSS)g++$(TOOLCHAIN_EXTERNAL_SUFFIX)
96TOOLCHAIN_EXTERNAL_GDC = $(TOOLCHAIN_EXTERNAL_CROSS)gdc$(TOOLCHAIN_EXTERNAL_SUFFIX)
97TOOLCHAIN_EXTERNAL_FC = $(TOOLCHAIN_EXTERNAL_CROSS)gfortran$(TOOLCHAIN_EXTERNAL_SUFFIX)
98TOOLCHAIN_EXTERNAL_READELF = $(TOOLCHAIN_EXTERNAL_CROSS)readelf
99
100# Normal handling of downloaded toolchain tarball extraction.
101ifeq ($(BR2_TOOLCHAIN_EXTERNAL_DOWNLOAD),y)
102# As a regular package, the toolchain gets extracted in $(@D), but
103# since it's actually a fairly special package, we need it to be moved
104# into TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR.
105define TOOLCHAIN_EXTERNAL_MOVE
106	rm -rf $(TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR)
107	mkdir -p $(TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR)
108	mv $(@D)/* $(TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR)/
109endef
110endif
111
112#
113# Definitions of the list of libraries that should be copied to the target.
114#
115
116TOOLCHAIN_EXTERNAL_LIBS += ld*.so.* libgcc_s.so.* libatomic.so.*
117
118ifneq ($(BR2_SSP_NONE),y)
119TOOLCHAIN_EXTERNAL_LIBS += libssp.so.*
120endif
121
122ifeq ($(BR2_TOOLCHAIN_EXTERNAL_GLIBC)$(BR2_TOOLCHAIN_EXTERNAL_UCLIBC),y)
123TOOLCHAIN_EXTERNAL_LIBS += libc.so.* libcrypt.so.* libdl.so.* libm.so.* libnsl.so.* libresolv.so.* librt.so.* libutil.so.*
124ifeq ($(BR2_TOOLCHAIN_HAS_THREADS),y)
125TOOLCHAIN_EXTERNAL_LIBS += libpthread.so.*
126ifneq ($(BR2_PACKAGE_GDB)$(BR2_TOOLCHAIN_EXTERNAL_GDB_SERVER_COPY),)
127TOOLCHAIN_EXTERNAL_LIBS += libthread_db.so.*
128endif # gdbserver
129endif # ! no threads
130endif
131
132ifeq ($(BR2_TOOLCHAIN_EXTERNAL_GLIBC),y)
133TOOLCHAIN_EXTERNAL_LIBS += libnss_files.so.* libnss_dns.so.* libmvec.so.* libanl.so.*
134endif
135
136ifeq ($(BR2_TOOLCHAIN_EXTERNAL_MUSL),y)
137TOOLCHAIN_EXTERNAL_LIBS += libc.so
138endif
139
140ifeq ($(BR2_INSTALL_LIBSTDCPP),y)
141TOOLCHAIN_EXTERNAL_LIBS += libstdc++.so.*
142endif
143
144ifeq ($(BR2_TOOLCHAIN_HAS_FORTRAN),y)
145TOOLCHAIN_EXTERNAL_LIBS += libgfortran.so.*
146# fortran needs quadmath on x86 and x86_64
147ifeq ($(BR2_TOOLCHAIN_HAS_LIBQUADMATH),y)
148TOOLCHAIN_EXTERNAL_LIBS += libquadmath.so*
149endif
150endif
151
152ifeq ($(BR2_TOOLCHAIN_HAS_OPENMP),y)
153TOOLCHAIN_EXTERNAL_LIBS += libgomp.so.*
154endif
155
156ifeq ($(BR2_TOOLCHAIN_HAS_DLANG),y)
157TOOLCHAIN_EXTERNAL_LIBS += libgdruntime.so* libgphobos.so*
158endif
159
160TOOLCHAIN_EXTERNAL_LIBS += $(addsuffix .so*,$(call qstrip,$(BR2_TOOLCHAIN_EXTRA_LIBS)))
161
162
163#
164# Definition of the CFLAGS to use with the external toolchain, as well as the
165# common toolchain wrapper build arguments
166#
167
168# march/mtune/floating point mode needs to be passed to the external toolchain
169# to select the right multilib variant
170ifeq ($(BR2_x86_64),y)
171TOOLCHAIN_EXTERNAL_CFLAGS += -m64
172TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_64
173endif
174ifneq ($(GCC_TARGET_ARCH),)
175TOOLCHAIN_EXTERNAL_CFLAGS += -march=$(GCC_TARGET_ARCH)
176TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_ARCH='"$(GCC_TARGET_ARCH)"'
177endif
178ifneq ($(GCC_TARGET_CPU),)
179TOOLCHAIN_EXTERNAL_CFLAGS += -mcpu=$(GCC_TARGET_CPU)
180TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_CPU='"$(GCC_TARGET_CPU)"'
181endif
182ifneq ($(GCC_TARGET_ABI),)
183TOOLCHAIN_EXTERNAL_CFLAGS += -mabi=$(GCC_TARGET_ABI)
184TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_ABI='"$(GCC_TARGET_ABI)"'
185endif
186ifeq ($(BR2_TOOLCHAIN_HAS_MNAN_OPTION),y)
187ifneq ($(GCC_TARGET_NAN),)
188TOOLCHAIN_EXTERNAL_CFLAGS += -mnan=$(GCC_TARGET_NAN)
189TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_NAN='"$(GCC_TARGET_NAN)"'
190endif
191endif
192ifneq ($(GCC_TARGET_FP32_MODE),)
193TOOLCHAIN_EXTERNAL_CFLAGS += -mfp$(GCC_TARGET_FP32_MODE)
194TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_FP32_MODE='"$(GCC_TARGET_FP32_MODE)"'
195endif
196ifneq ($(GCC_TARGET_FPU),)
197TOOLCHAIN_EXTERNAL_CFLAGS += -mfpu=$(GCC_TARGET_FPU)
198TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_FPU='"$(GCC_TARGET_FPU)"'
199endif
200ifneq ($(GCC_TARGET_FLOAT_ABI),)
201TOOLCHAIN_EXTERNAL_CFLAGS += -mfloat-abi=$(GCC_TARGET_FLOAT_ABI)
202TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_FLOAT_ABI='"$(GCC_TARGET_FLOAT_ABI)"'
203endif
204ifneq ($(GCC_TARGET_MODE),)
205TOOLCHAIN_EXTERNAL_CFLAGS += -m$(GCC_TARGET_MODE)
206TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_MODE='"$(GCC_TARGET_MODE)"'
207endif
208ifeq ($(BR2_BINFMT_FLAT),y)
209TOOLCHAIN_EXTERNAL_CFLAGS += -Wl,-elf2flt
210TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_BINFMT_FLAT
211endif
212ifeq ($(BR2_mipsel)$(BR2_mips64el),y)
213TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_MIPS_TARGET_LITTLE_ENDIAN
214TOOLCHAIN_EXTERNAL_CFLAGS += -EL
215endif
216ifeq ($(BR2_mips)$(BR2_mips64),y)
217TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_MIPS_TARGET_BIG_ENDIAN
218TOOLCHAIN_EXTERNAL_CFLAGS += -EB
219endif
220ifeq ($(BR2_arceb),y)
221TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_ARC_TARGET_BIG_ENDIAN
222TOOLCHAIN_EXTERNAL_CFLAGS += -EB
223endif
224
225TOOLCHAIN_EXTERNAL_CFLAGS += $(call qstrip,$(BR2_TARGET_OPTIMIZATION))
226
227ifeq ($(BR2_SOFT_FLOAT),y)
228TOOLCHAIN_EXTERNAL_CFLAGS += -msoft-float
229TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_SOFTFLOAT=1
230endif
231
232TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += \
233	-DBR_CROSS_PATH_SUFFIX='"$(TOOLCHAIN_EXTERNAL_SUFFIX)"'
234
235ifeq ($(filter $(HOST_DIR)/%,$(TOOLCHAIN_EXTERNAL_BIN)),)
236# TOOLCHAIN_EXTERNAL_BIN points outside HOST_DIR => absolute path
237TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += \
238	-DBR_CROSS_PATH_ABS='"$(TOOLCHAIN_EXTERNAL_BIN)"'
239else
240# TOOLCHAIN_EXTERNAL_BIN points inside HOST_DIR => relative path
241TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += \
242	-DBR_CROSS_PATH_REL='"$(TOOLCHAIN_EXTERNAL_BIN:$(HOST_DIR)/%=%)"'
243endif
244
245
246#
247# The following functions creates the symbolic links needed to get the
248# cross-compilation tools visible in $(HOST_DIR)/bin. Some of
249# links are done directly to the corresponding tool in the external
250# toolchain installation directory, while some other links are done to
251# the toolchain wrapper (preprocessor, C, C++ and Fortran compiler)
252#
253# We skip gdb symlink when we are building our own gdb to prevent two
254# gdb's in $(HOST_DIR)/bin.
255#
256# The LTO support in gcc creates wrappers for ar, ranlib and nm which load
257# the lto plugin. These wrappers are called *-gcc-ar, *-gcc-ranlib, and
258# *-gcc-nm and should be used instead of the real programs when -flto is
259# used. However, we should not add the toolchain wrapper for them, and they
260# match the *cc-* pattern. Therefore, an additional case is added for *-ar,
261# *-ranlib and *-nm.
262define TOOLCHAIN_EXTERNAL_INSTALL_WRAPPER
263	$(Q)cd $(HOST_DIR)/bin; \
264	for i in $(TOOLCHAIN_EXTERNAL_CROSS)*; do \
265		base=$${i##*/}; \
266		case "$$base" in \
267		*-ar|*-ranlib|*-nm) \
268			ln -sf $$(echo $$i | sed 's%^$(HOST_DIR)%..%') .; \
269			;; \
270		*cc|*cc-*|*++|*++-*|*cpp|*-gfortran|*-gdc) \
271			ln -sf toolchain-wrapper $$base; \
272			;; \
273		*gdb|*gdbtui) \
274			if test "$(BR2_PACKAGE_HOST_GDB)" != "y"; then \
275				ln -sf $$(echo $$i | sed 's%^$(HOST_DIR)%..%') .; \
276			fi \
277			;; \
278		*) \
279			ln -sf $$(echo $$i | sed 's%^$(HOST_DIR)%..%') .; \
280			;; \
281		esac; \
282	done
283endef
284
285
286# Various utility functions used by the external toolchain package
287# infrastructure. Those functions are mainly responsible for:
288#
289#   - installation the toolchain libraries to $(TARGET_DIR)
290#   - copying the toolchain sysroot to $(STAGING_DIR)
291#   - installing a gdbinit file
292#
293# Details about sysroot directory selection.
294#
295# To find the sysroot directory, we use the trick of looking for the
296# 'libc.a' file with the -print-file-name gcc option, and then
297# mangling the path to find the base directory of the sysroot.
298#
299# Note that we do not use the -print-sysroot option, because it is
300# only available since gcc 4.4.x, and we only recently dropped support
301# for 4.2.x and 4.3.x.
302#
303# When doing this, we don't pass any option to gcc that could select a
304# multilib variant (such as -march) as we want the "main" sysroot,
305# which contains all variants of the C library in the case of multilib
306# toolchains. We use the TARGET_CC_NO_SYSROOT variable, which is the
307# path of the cross-compiler, without the --sysroot=$(STAGING_DIR),
308# since what we want to find is the location of the original toolchain
309# sysroot. This "main" sysroot directory is stored in SYSROOT_DIR.
310#
311# Then, multilib toolchains are a little bit more complicated, since
312# they in fact have multiple sysroots, one for each variant supported
313# by the toolchain. So we need to find the particular sysroot we're
314# interested in.
315#
316# To do so, we ask the compiler where its sysroot is by passing all
317# flags (including -march and al.), except the --sysroot flag since we
318# want to the compiler to tell us where its original sysroot
319# is. ARCH_SUBDIR will contain the subdirectory, in the main
320# SYSROOT_DIR, that corresponds to the selected architecture
321# variant. ARCH_SYSROOT_DIR will contain the full path to this
322# location.
323#
324# One might wonder why we don't just bother with ARCH_SYSROOT_DIR. The
325# fact is that in multilib toolchains, the header files are often only
326# present in the main sysroot, and only the libraries are available in
327# each variant-specific sysroot directory.
328
329
330# toolchain_find_sysroot returns the sysroot location for the given
331# compiler + flags. We need to handle cases where libc.a is in:
332#
333#  - lib/
334#  - usr/lib/
335#  - lib32/
336#  - lib64/
337#  - lib32-fp/ (Cavium toolchain)
338#  - lib64-fp/ (Cavium toolchain)
339#  - usr/lib/<tuple>/ (Linaro toolchain)
340#
341# And variations on these.
342define toolchain_find_sysroot
343$$(printf $(call toolchain_find_libc_a,$(1)) | sed -r -e 's:/(usr/)?lib(32|64)?([^/]*)?/([^/]*/)?libc\.a:/:')
344endef
345
346# Returns the lib subdirectory for the given compiler + flags (i.e
347# typically lib32 or lib64 for some toolchains)
348define toolchain_find_libdir
349$$(printf $(call toolchain_find_libc_a,$(1)) | sed -r -e 's:.*/(usr/)?(lib(32|64)?([^/]*)?(/[^/]*)?)/libc.a:\2:')
350endef
351
352# Returns the location of the libc.a file for the given compiler + flags
353define toolchain_find_libc_a
354$$(readlink -f $$(LANG=C $(1) -print-file-name=libc.a))
355endef
356
357# Integration of the toolchain into Buildroot: find the main sysroot
358# and the variant-specific sysroot, then copy the needed libraries to
359# the $(TARGET_DIR) and copy the whole sysroot (libraries and headers)
360# to $(STAGING_DIR).
361#
362# Variables are defined as follows:
363#
364# SYSROOT_DIR:          the main sysroot directory, deduced from the location of
365#                       the libc.a file in the default multilib variant, by
366#                       removing the usr/lib[32|64]/libc.a part of the path.
367#                       Ex: /x-tools/mips-2011.03/mips-linux-gnu/libc/
368#
369# ARCH_SYSROOT_DIR:     the sysroot of the selected multilib variant,
370#                       deduced from the location of the libc.a file in the
371#                       selected multilib variant (taking into account the
372#                       CFLAGS), by removing usr/lib[32|64]/libc.a at the end
373#                       of the path.
374#                       Ex: /x-tools/mips-2011.03/mips-linux-gnu/libc/mips16/soft-float/el/
375#
376# ARCH_LIB_DIR:         'lib', 'lib32' or 'lib64' depending on where libraries
377#                       are stored. Deduced from the location of the libc.a file
378#                       in the selected multilib variant, by looking at
379#                       usr/lib??/libc.a.
380#                       Ex: lib
381#
382# ARCH_SUBDIR:          the relative location of the sysroot of the selected
383#                       multilib variant compared to the main sysroot.
384#                       Ex: mips16/soft-float/el
385#
386# SUPPORT_LIB_DIR:      some toolchains, such as recent Linaro toolchains,
387#                       store GCC support libraries (libstdc++,
388#                       libgcc_s, etc.) outside of the sysroot. In
389#                       this case, SUPPORT_LIB_DIR is set to a
390#                       non-empty value, and points to the directory
391#                       where these support libraries are
392#                       available. Those libraries will be copied to
393#                       our sysroot, and the directory will also be
394#                       considered when searching libraries for copy
395#                       to the target filesystem.
396#
397# Please be very careful to check the major toolchain sources:
398# Buildroot, Crosstool-NG, CodeSourcery and Linaro
399# before doing any modification on the below logic.
400
401ifeq ($(BR2_STATIC_LIBS),)
402define TOOLCHAIN_EXTERNAL_INSTALL_TARGET_LIBS
403	$(Q)$(call MESSAGE,"Copying external toolchain libraries to target...")
404	$(Q)for libpattern in $(TOOLCHAIN_EXTERNAL_LIBS); do \
405		$(call copy_toolchain_lib_root,$$libpattern); \
406	done
407endef
408endif
409
410ifeq ($(BR2_TOOLCHAIN_EXTERNAL_GDB_SERVER_COPY),y)
411define TOOLCHAIN_EXTERNAL_INSTALL_TARGET_GDBSERVER
412	$(Q)$(call MESSAGE,"Copying gdbserver")
413	$(Q)ARCH_SYSROOT_DIR="$(call toolchain_find_sysroot,$(TOOLCHAIN_EXTERNAL_CC) $(TOOLCHAIN_EXTERNAL_CFLAGS))" ; \
414	ARCH_LIB_DIR="$(call toolchain_find_libdir,$(TOOLCHAIN_EXTERNAL_CC) $(TOOLCHAIN_EXTERNAL_CFLAGS))" ; \
415	gdbserver_found=0 ; \
416	for d in $${ARCH_SYSROOT_DIR}/usr \
417		 $${ARCH_SYSROOT_DIR}/../debug-root/usr \
418		 $${ARCH_SYSROOT_DIR}/usr/$${ARCH_LIB_DIR} \
419		 $(TOOLCHAIN_EXTERNAL_INSTALL_DIR); do \
420		if test -f $${d}/bin/gdbserver ; then \
421			install -m 0755 -D $${d}/bin/gdbserver $(TARGET_DIR)/usr/bin/gdbserver ; \
422			gdbserver_found=1 ; \
423			break ; \
424		fi ; \
425	done ; \
426	if [ $${gdbserver_found} -eq 0 ] ; then \
427		echo "Could not find gdbserver in external toolchain" ; \
428		exit 1 ; \
429	fi
430endef
431endif
432
433define TOOLCHAIN_EXTERNAL_INSTALL_SYSROOT_LIBS
434	$(Q)SYSROOT_DIR="$(call toolchain_find_sysroot,$(TOOLCHAIN_EXTERNAL_CC))" ; \
435	ARCH_SYSROOT_DIR="$(call toolchain_find_sysroot,$(TOOLCHAIN_EXTERNAL_CC) $(TOOLCHAIN_EXTERNAL_CFLAGS))" ; \
436	ARCH_LIB_DIR="$(call toolchain_find_libdir,$(TOOLCHAIN_EXTERNAL_CC) $(TOOLCHAIN_EXTERNAL_CFLAGS))" ; \
437	SUPPORT_LIB_DIR="" ; \
438	if test `find $${ARCH_SYSROOT_DIR} -name 'libstdc++.a' | wc -l` -eq 0 ; then \
439		LIBSTDCPP_A_LOCATION=$$(LANG=C $(TOOLCHAIN_EXTERNAL_CC) $(TOOLCHAIN_EXTERNAL_CFLAGS) -print-file-name=libstdc++.a) ; \
440		if [ -e "$${LIBSTDCPP_A_LOCATION}" ]; then \
441			SUPPORT_LIB_DIR=`readlink -f $${LIBSTDCPP_A_LOCATION} | sed -r -e 's:libstdc\+\+\.a::'` ; \
442		fi ; \
443	fi ; \
444	if [ "$${SYSROOT_DIR}" == "$${ARCH_SYSROOT_DIR}" ] ; then \
445		ARCH_SUBDIR="" ; \
446	elif [ "`dirname $${ARCH_SYSROOT_DIR}`" = "`dirname $${SYSROOT_DIR}`" ] ; then \
447		SYSROOT_DIR_DIRNAME=`dirname $${SYSROOT_DIR}`/ ; \
448		ARCH_SUBDIR=`echo $${ARCH_SYSROOT_DIR} | sed -r -e "s:^$${SYSROOT_DIR_DIRNAME}(.*)/$$:\1:"` ; \
449	else \
450		ARCH_SUBDIR=`echo $${ARCH_SYSROOT_DIR} | sed -r -e "s:^$${SYSROOT_DIR}(.*)/$$:\1:"` ; \
451	fi ; \
452	$(call MESSAGE,"Copying external toolchain sysroot to staging...") ; \
453	$(call copy_toolchain_sysroot,$${SYSROOT_DIR},$${ARCH_SYSROOT_DIR},$${ARCH_SUBDIR},$${ARCH_LIB_DIR},$${SUPPORT_LIB_DIR})
454endef
455
456# Create a symlink from (usr/)$(ARCH_LIB_DIR) to lib.
457# Note: the skeleton package additionally creates lib32->lib or lib64->lib
458# (as appropriate)
459#
460# $1: destination directory (TARGET_DIR / STAGING_DIR)
461create_lib_symlinks = \
462	$(Q)DESTDIR="$(strip $1)" ; \
463	ARCH_LIB_DIR="$(call toolchain_find_libdir,$(TOOLCHAIN_EXTERNAL_CC) $(TOOLCHAIN_EXTERNAL_CFLAGS))" ; \
464	if [ ! -e "$${DESTDIR}/$${ARCH_LIB_DIR}" -a ! -e "$${DESTDIR}/usr/$${ARCH_LIB_DIR}" ]; then \
465		relpath="$(call relpath_prefix,$${ARCH_LIB_DIR})" ; \
466		ln -snf $${relpath}lib "$${DESTDIR}/$${ARCH_LIB_DIR}" ; \
467		ln -snf $${relpath}lib "$${DESTDIR}/usr/$${ARCH_LIB_DIR}" ; \
468	fi
469
470define TOOLCHAIN_EXTERNAL_CREATE_STAGING_LIB_SYMLINK
471	$(call create_lib_symlinks,$(STAGING_DIR))
472endef
473
474define TOOLCHAIN_EXTERNAL_CREATE_TARGET_LIB_SYMLINK
475	$(call create_lib_symlinks,$(TARGET_DIR))
476endef
477
478#
479# Generate gdbinit file for use with Buildroot
480#
481define TOOLCHAIN_EXTERNAL_INSTALL_GDBINIT
482	$(Q)if test -f $(TARGET_CROSS)gdb ; then \
483		$(call MESSAGE,"Installing gdbinit"); \
484		$(gen_gdbinit_file); \
485	fi
486endef
487
488# GCC installs a libstdcxx-...so-gdb.py file that gdb will load automatically,
489# but it contains hardcoded paths referring to the location where the (external)
490# toolchain was built. Fix up these paths so that the pretty printers can be
491# loaded automatically.
492# By default, the pretty printers are installed in
493# $(datadir)/gcc-$(gcc_version)/python but this could have been overwritten with
494# the gcc configure option: --with-python-dir. We thus have to search the
495# correct path first.
496define TOOLCHAIN_EXTERNAL_FIXUP_PRETTY_PRINTER_LOADER
497	$(Q)loadfiles=$$(find $(STAGING_DIR) -name 'libstdc++.so*-gdb.py' 2>/dev/null); \
498	pythondir=$$(find $(TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR) -path '*/libstdcxx/__init__.py' 2>/dev/null | sed 's%/libstdcxx/__init__.py%%' | head -n1); \
499	if [ -n "$$loadfiles" ] && [ -n "$$pythondir" ]; then \
500		echo "Fixing up hardcoded paths in GDB pretty-printer auto-load file(s) for libstdcxx: $$loadfiles"; \
501		sed -ri \
502			-e 's%^libdir\s*=.*%libdir = "$(STAGING_DIR)/lib"%' \
503			-e "s%^pythondir\s*=.*%pythondir = '$$pythondir'%" \
504			$$loadfiles; \
505	fi
506endef
507
508# uClibc-ng dynamic loader is called ld-uClibc.so.1, but gcc is not
509# patched specifically for uClibc-ng, so it continues to generate
510# binaries that expect the dynamic loader to be named ld-uClibc.so.0,
511# like with the original uClibc. Therefore, we create an additional
512# symbolic link to make uClibc-ng systems work properly.
513define TOOLCHAIN_EXTERNAL_FIXUP_UCLIBCNG_LDSO
514	$(Q)if test -e $(TARGET_DIR)/lib/ld-uClibc.so.1; then \
515		ln -sf ld-uClibc.so.1 $(TARGET_DIR)/lib/ld-uClibc.so.0 ; \
516	fi
517	$(Q)if test -e $(TARGET_DIR)/lib/ld64-uClibc.so.1; then \
518		ln -sf ld64-uClibc.so.1 $(TARGET_DIR)/lib/ld64-uClibc.so.0 ; \
519	fi
520endef
521
522define TOOLCHAIN_EXTERNAL_INSTALL_TARGET_LDD
523	$(Q)if test -f $(STAGING_DIR)/usr/bin/ldd ; then \
524		$(INSTALL) -D $(STAGING_DIR)/usr/bin/ldd $(TARGET_DIR)/usr/bin/ldd ; \
525		$(SED) 's:.*/bin/bash:#!/bin/sh:' $(TARGET_DIR)/usr/bin/ldd ; \
526	fi
527endef
528
529################################################################################
530# inner-toolchain-external-package -- defines the generic installation rules
531# for external toolchain packages
532#
533#  argument 1 is the lowercase package name
534#  argument 2 is the uppercase package name, including a HOST_ prefix
535#             for host packages
536#  argument 3 is the uppercase package name, without the HOST_ prefix
537#             for host packages
538#  argument 4 is the type (target or host)
539################################################################################
540define inner-toolchain-external-package
541
542$(2)_INSTALL_STAGING = YES
543$(2)_ADD_TOOLCHAIN_DEPENDENCY = NO
544
545# In fact, we don't need to download the toolchain, since it is already
546# available on the system, so force the site and source to be empty so
547# that nothing will be downloaded/extracted.
548ifeq ($$(BR2_TOOLCHAIN_EXTERNAL_PREINSTALLED),y)
549$(2)_SITE =
550$(2)_SOURCE =
551endif
552
553ifeq ($$(BR2_TOOLCHAIN_EXTERNAL_DOWNLOAD),y)
554$(2)_EXCLUDES = usr/lib/locale/*
555
556$(2)_POST_EXTRACT_HOOKS += \
557	TOOLCHAIN_EXTERNAL_MOVE
558endif
559
560# Checks for an already installed toolchain: check the toolchain
561# location, check that it is usable, and then verify that it
562# matches the configuration provided in Buildroot: ABI, C++ support,
563# kernel headers version, type of C library and all C library features.
564define $(2)_CONFIGURE_CMDS
565	$$(Q)$$(call check_cross_compiler_exists,$$(TOOLCHAIN_EXTERNAL_CC))
566	$$(Q)$$(call check_unusable_toolchain,$$(TOOLCHAIN_EXTERNAL_CC))
567	$$(Q)SYSROOT_DIR="$$(call toolchain_find_sysroot,$$(TOOLCHAIN_EXTERNAL_CC))" ; \
568	$$(call check_kernel_headers_version,\
569		$$(BUILD_DIR),\
570		$$(call toolchain_find_sysroot,$$(TOOLCHAIN_EXTERNAL_CC)),\
571		$$(call qstrip,$$(BR2_TOOLCHAIN_HEADERS_AT_LEAST)),\
572		$$(if $$(BR2_TOOLCHAIN_EXTERNAL_CUSTOM),loose,strict)); \
573	$$(call check_gcc_version,$$(TOOLCHAIN_EXTERNAL_CC),\
574		$$(call qstrip,$$(BR2_TOOLCHAIN_GCC_AT_LEAST))); \
575	if test "$$(BR2_arm)" = "y" ; then \
576		$$(call check_arm_abi,\
577			"$$(TOOLCHAIN_EXTERNAL_CC) $$(TOOLCHAIN_EXTERNAL_CFLAGS)") ; \
578	fi ; \
579	if test "$$(BR2_INSTALL_LIBSTDCPP)" = "y" ; then \
580		$$(call check_cplusplus,$$(TOOLCHAIN_EXTERNAL_CXX)) ; \
581	fi ; \
582	if test "$$(BR2_TOOLCHAIN_HAS_DLANG)" = "y" ; then \
583		$$(call check_dlang,$$(TOOLCHAIN_EXTERNAL_GDC)) ; \
584	fi ; \
585	if test "$$(BR2_TOOLCHAIN_HAS_FORTRAN)" = "y" ; then \
586		$$(call check_fortran,$$(TOOLCHAIN_EXTERNAL_FC)) ; \
587	fi ; \
588	if test "$$(BR2_TOOLCHAIN_HAS_OPENMP)" = "y" ; then \
589		$$(call check_openmp,$$(TOOLCHAIN_EXTERNAL_CC)) ; \
590	fi ; \
591	if test "$$(BR2_TOOLCHAIN_EXTERNAL_UCLIBC)" = "y" ; then \
592		$$(call check_uclibc,$$$${SYSROOT_DIR}) ; \
593	elif test "$$(BR2_TOOLCHAIN_EXTERNAL_MUSL)" = "y" ; then \
594		$$(call check_musl,\
595			"$$(TOOLCHAIN_EXTERNAL_CC) $$(TOOLCHAIN_EXTERNAL_CFLAGS)") ; \
596	else \
597		$$(call check_glibc,$$$${SYSROOT_DIR}) ; \
598	fi
599	$$(Q)$$(call check_toolchain_ssp,$$(TOOLCHAIN_EXTERNAL_CC),$(BR2_SSP_OPTION))
600endef
601
602$(2)_TOOLCHAIN_WRAPPER_ARGS += $$(TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS)
603
604$(2)_BUILD_CMDS = $$(TOOLCHAIN_WRAPPER_BUILD)
605
606define $(2)_INSTALL_STAGING_CMDS
607	$$(TOOLCHAIN_WRAPPER_INSTALL)
608	$$(TOOLCHAIN_EXTERNAL_CREATE_STAGING_LIB_SYMLINK)
609	$$(TOOLCHAIN_EXTERNAL_INSTALL_SYSROOT_LIBS)
610	$$(TOOLCHAIN_EXTERNAL_INSTALL_WRAPPER)
611	$$(TOOLCHAIN_EXTERNAL_INSTALL_GDBINIT)
612	$$(TOOLCHAIN_EXTERNAL_FIXUP_PRETTY_PRINTER_LOADER)
613endef
614
615# Even though we're installing things in both the staging, the host
616# and the target directory, we do everything within the
617# install-staging step, arbitrarily.
618define $(2)_INSTALL_TARGET_CMDS
619	$$(TOOLCHAIN_EXTERNAL_CREATE_TARGET_LIB_SYMLINK)
620	$$(TOOLCHAIN_EXTERNAL_INSTALL_TARGET_LIBS)
621	$$(TOOLCHAIN_EXTERNAL_INSTALL_TARGET_GDBSERVER)
622	$$(TOOLCHAIN_EXTERNAL_FIXUP_UCLIBCNG_LDSO)
623	$$(TOOLCHAIN_EXTERNAL_INSTALL_TARGET_LDD)
624endef
625
626# Call the generic package infrastructure to generate the necessary
627# make targets
628$(call inner-generic-package,$(1),$(2),$(3),$(4))
629
630endef
631
632toolchain-external-package = $(call inner-toolchain-external-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)
633