]> rtime.felk.cvut.cz Git - coffee/buildroot.git/blob - toolchain/toolchain-external/pkg-toolchain-external.mk
toolchain/toolchain-external: libatomic should also be copied for musl toolchains
[coffee/buildroot.git] / toolchain / toolchain-external / pkg-toolchain-external.mk
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
65 TOOLCHAIN_EXTERNAL_PREFIX = $(call qstrip,$(BR2_TOOLCHAIN_EXTERNAL_PREFIX))
66 TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR = $(HOST_DIR)/opt/ext-toolchain
67
68 ifeq ($(BR2_TOOLCHAIN_EXTERNAL_DOWNLOAD),y)
69 TOOLCHAIN_EXTERNAL_INSTALL_DIR = $(TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR)
70 else
71 TOOLCHAIN_EXTERNAL_INSTALL_DIR = $(call qstrip,$(BR2_TOOLCHAIN_EXTERNAL_PATH))
72 endif
73
74 ifeq ($(TOOLCHAIN_EXTERNAL_INSTALL_DIR),)
75 ifneq ($(TOOLCHAIN_EXTERNAL_PREFIX),)
76 # if no path set, figure it out from path
77 TOOLCHAIN_EXTERNAL_BIN := $(dir $(shell which $(TOOLCHAIN_EXTERNAL_PREFIX)-gcc))
78 endif
79 else
80 TOOLCHAIN_EXTERNAL_BIN = $(TOOLCHAIN_EXTERNAL_INSTALL_DIR)/bin
81 endif
82
83 # If this is a buildroot toolchain, it already has a wrapper which we want to
84 # bypass. Since this is only evaluated after it has been extracted, we can use
85 # $(wildcard ...) here.
86 TOOLCHAIN_EXTERNAL_SUFFIX = \
87         $(if $(wildcard $(TOOLCHAIN_EXTERNAL_BIN)/*.br_real),.br_real)
88
89 TOOLCHAIN_EXTERNAL_CROSS = $(TOOLCHAIN_EXTERNAL_BIN)/$(TOOLCHAIN_EXTERNAL_PREFIX)-
90 TOOLCHAIN_EXTERNAL_CC = $(TOOLCHAIN_EXTERNAL_CROSS)gcc$(TOOLCHAIN_EXTERNAL_SUFFIX)
91 TOOLCHAIN_EXTERNAL_CXX = $(TOOLCHAIN_EXTERNAL_CROSS)g++$(TOOLCHAIN_EXTERNAL_SUFFIX)
92 TOOLCHAIN_EXTERNAL_FC = $(TOOLCHAIN_EXTERNAL_CROSS)gfortran$(TOOLCHAIN_EXTERNAL_SUFFIX)
93 TOOLCHAIN_EXTERNAL_READELF = $(TOOLCHAIN_EXTERNAL_CROSS)readelf
94
95 # Normal handling of downloaded toolchain tarball extraction.
96 ifeq ($(BR2_TOOLCHAIN_EXTERNAL_DOWNLOAD),y)
97 # As a regular package, the toolchain gets extracted in $(@D), but
98 # since it's actually a fairly special package, we need it to be moved
99 # into TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR.
100 define TOOLCHAIN_EXTERNAL_MOVE
101         rm -rf $(TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR)
102         mkdir -p $(TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR)
103         mv $(@D)/* $(TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR)/
104 endef
105 endif
106
107 #
108 # Definitions of the list of libraries that should be copied to the target.
109 #
110
111 TOOLCHAIN_EXTERNAL_LIBS += ld*.so* libgcc_s.so.* libatomic.so.*
112
113 ifeq ($(BR2_TOOLCHAIN_EXTERNAL_GLIBC)$(BR2_TOOLCHAIN_EXTERNAL_UCLIBC),y)
114 TOOLCHAIN_EXTERNAL_LIBS += libc.so.* libcrypt.so.* libdl.so.* libm.so.* libnsl.so.* libresolv.so.* librt.so.* libutil.so.*
115 ifeq ($(BR2_TOOLCHAIN_HAS_THREADS),y)
116 TOOLCHAIN_EXTERNAL_LIBS += libpthread.so.*
117 ifneq ($(BR2_PACKAGE_GDB)$(BR2_TOOLCHAIN_EXTERNAL_GDB_SERVER_COPY),)
118 TOOLCHAIN_EXTERNAL_LIBS += libthread_db.so.*
119 endif # gdbserver
120 endif # ! no threads
121 endif
122
123 ifeq ($(BR2_TOOLCHAIN_EXTERNAL_GLIBC),y)
124 TOOLCHAIN_EXTERNAL_LIBS += libnss_files.so.* libnss_dns.so.* libmvec.so.* libanl.so.*
125 endif
126
127 ifeq ($(BR2_TOOLCHAIN_EXTERNAL_MUSL),y)
128 TOOLCHAIN_EXTERNAL_LIBS += libc.so
129 endif
130
131 ifeq ($(BR2_INSTALL_LIBSTDCPP),y)
132 TOOLCHAIN_EXTERNAL_LIBS += libstdc++.so.*
133 endif
134
135 ifeq ($(BR2_TOOLCHAIN_HAS_FORTRAN),y)
136 TOOLCHAIN_EXTERNAL_LIBS += libgfortran.so.*
137 # fortran needs quadmath on x86 and x86_64
138 ifeq ($(BR2_TOOLCHAIN_HAS_LIBQUADMATH),y)
139 TOOLCHAIN_EXTERNAL_LIBS += libquadmath.so*
140 endif
141 endif
142
143 TOOLCHAIN_EXTERNAL_LIBS += $(call qstrip,$(BR2_TOOLCHAIN_EXTRA_EXTERNAL_LIBS))
144
145
146 #
147 # Definition of the CFLAGS to use with the external toolchain, as well as the
148 # common toolchain wrapper build arguments
149 #
150 ifeq ($(call qstrip,$(BR2_GCC_TARGET_CPU_REVISION)),)
151 CC_TARGET_CPU_ := $(call qstrip,$(BR2_GCC_TARGET_CPU))
152 else
153 CC_TARGET_CPU_ := $(call qstrip,$(BR2_GCC_TARGET_CPU)-$(BR2_GCC_TARGET_CPU_REVISION))
154 endif
155 CC_TARGET_ARCH_ := $(call qstrip,$(BR2_GCC_TARGET_ARCH))
156 CC_TARGET_ABI_ := $(call qstrip,$(BR2_GCC_TARGET_ABI))
157 CC_TARGET_NAN_ := $(call qstrip,$(BR2_GCC_TARGET_NAN))
158 CC_TARGET_FP32_MODE_ := $(call qstrip,$(BR2_GCC_TARGET_FP32_MODE))
159 CC_TARGET_FPU_ := $(call qstrip,$(BR2_GCC_TARGET_FPU))
160 CC_TARGET_FLOAT_ABI_ := $(call qstrip,$(BR2_GCC_TARGET_FLOAT_ABI))
161 CC_TARGET_MODE_ := $(call qstrip,$(BR2_GCC_TARGET_MODE))
162
163 # march/mtune/floating point mode needs to be passed to the external toolchain
164 # to select the right multilib variant
165 ifeq ($(BR2_x86_64),y)
166 TOOLCHAIN_EXTERNAL_CFLAGS += -m64
167 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_64
168 endif
169 ifneq ($(CC_TARGET_ARCH_),)
170 TOOLCHAIN_EXTERNAL_CFLAGS += -march=$(CC_TARGET_ARCH_)
171 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_ARCH='"$(CC_TARGET_ARCH_)"'
172 endif
173 ifneq ($(CC_TARGET_CPU_),)
174 TOOLCHAIN_EXTERNAL_CFLAGS += -mcpu=$(CC_TARGET_CPU_)
175 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_CPU='"$(CC_TARGET_CPU_)"'
176 endif
177 ifneq ($(CC_TARGET_ABI_),)
178 TOOLCHAIN_EXTERNAL_CFLAGS += -mabi=$(CC_TARGET_ABI_)
179 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_ABI='"$(CC_TARGET_ABI_)"'
180 endif
181 ifeq ($(BR2_TOOLCHAIN_HAS_MNAN_OPTION),y)
182 ifneq ($(CC_TARGET_NAN_),)
183 TOOLCHAIN_EXTERNAL_CFLAGS += -mnan=$(CC_TARGET_NAN_)
184 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_NAN='"$(CC_TARGET_NAN_)"'
185 endif
186 endif
187 ifneq ($(CC_TARGET_FP32_MODE_),)
188 TOOLCHAIN_EXTERNAL_CFLAGS += -mfp$(CC_TARGET_FP32_MODE_)
189 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_FP32_MODE='"$(CC_TARGET_FP32_MODE_)"'
190 endif
191 ifneq ($(CC_TARGET_FPU_),)
192 TOOLCHAIN_EXTERNAL_CFLAGS += -mfpu=$(CC_TARGET_FPU_)
193 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_FPU='"$(CC_TARGET_FPU_)"'
194 endif
195 ifneq ($(CC_TARGET_FLOAT_ABI_),)
196 TOOLCHAIN_EXTERNAL_CFLAGS += -mfloat-abi=$(CC_TARGET_FLOAT_ABI_)
197 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_FLOAT_ABI='"$(CC_TARGET_FLOAT_ABI_)"'
198 endif
199 ifneq ($(CC_TARGET_MODE_),)
200 TOOLCHAIN_EXTERNAL_CFLAGS += -m$(CC_TARGET_MODE_)
201 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_MODE='"$(CC_TARGET_MODE_)"'
202 endif
203 ifeq ($(BR2_BINFMT_FLAT),y)
204 TOOLCHAIN_EXTERNAL_CFLAGS += -Wl,-elf2flt
205 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_BINFMT_FLAT
206 endif
207 ifeq ($(BR2_mipsel)$(BR2_mips64el),y)
208 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_MIPS_TARGET_LITTLE_ENDIAN
209 TOOLCHAIN_EXTERNAL_CFLAGS += -EL
210 endif
211 ifeq ($(BR2_mips)$(BR2_mips64),y)
212 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_MIPS_TARGET_BIG_ENDIAN
213 TOOLCHAIN_EXTERNAL_CFLAGS += -EB
214 endif
215 ifeq ($(BR2_arceb),y)
216 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_ARC_TARGET_BIG_ENDIAN
217 TOOLCHAIN_EXTERNAL_CFLAGS += -EB
218 endif
219
220 TOOLCHAIN_EXTERNAL_CFLAGS += $(call qstrip,$(BR2_TARGET_OPTIMIZATION))
221
222 ifeq ($(BR2_SOFT_FLOAT),y)
223 TOOLCHAIN_EXTERNAL_CFLAGS += -msoft-float
224 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_SOFTFLOAT=1
225 endif
226
227 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += \
228         -DBR_CROSS_PATH_SUFFIX='"$(TOOLCHAIN_EXTERNAL_SUFFIX)"'
229
230 ifeq ($(filter $(HOST_DIR)/%,$(TOOLCHAIN_EXTERNAL_BIN)),)
231 # TOOLCHAIN_EXTERNAL_BIN points outside HOST_DIR => absolute path
232 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += \
233         -DBR_CROSS_PATH_ABS='"$(TOOLCHAIN_EXTERNAL_BIN)"'
234 else
235 # TOOLCHAIN_EXTERNAL_BIN points inside HOST_DIR => relative path
236 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += \
237         -DBR_CROSS_PATH_REL='"$(TOOLCHAIN_EXTERNAL_BIN:$(HOST_DIR)/%=%)"'
238 endif
239
240
241 #
242 # The following functions creates the symbolic links needed to get the
243 # cross-compilation tools visible in $(HOST_DIR)/bin. Some of
244 # links are done directly to the corresponding tool in the external
245 # toolchain installation directory, while some other links are done to
246 # the toolchain wrapper (preprocessor, C, C++ and Fortran compiler)
247 #
248 # We skip gdb symlink when we are building our own gdb to prevent two
249 # gdb's in $(HOST_DIR)/bin.
250 #
251 # The LTO support in gcc creates wrappers for ar, ranlib and nm which load
252 # the lto plugin. These wrappers are called *-gcc-ar, *-gcc-ranlib, and
253 # *-gcc-nm and should be used instead of the real programs when -flto is
254 # used. However, we should not add the toolchain wrapper for them, and they
255 # match the *cc-* pattern. Therefore, an additional case is added for *-ar,
256 # *-ranlib and *-nm.
257 define TOOLCHAIN_EXTERNAL_INSTALL_WRAPPER
258         $(Q)cd $(HOST_DIR)/bin; \
259         for i in $(TOOLCHAIN_EXTERNAL_CROSS)*; do \
260                 base=$${i##*/}; \
261                 case "$$base" in \
262                 *-ar|*-ranlib|*-nm) \
263                         ln -sf $$(echo $$i | sed 's%^$(HOST_DIR)%..%') .; \
264                         ;; \
265                 *cc|*cc-*|*++|*++-*|*cpp|*-gfortran) \
266                         ln -sf toolchain-wrapper $$base; \
267                         ;; \
268                 *gdb|*gdbtui) \
269                         if test "$(BR2_PACKAGE_HOST_GDB)" != "y"; then \
270                                 ln -sf $$(echo $$i | sed 's%^$(HOST_DIR)%..%') .; \
271                         fi \
272                         ;; \
273                 *) \
274                         ln -sf $$(echo $$i | sed 's%^$(HOST_DIR)%..%') .; \
275                         ;; \
276                 esac; \
277         done
278 endef
279
280
281 # Various utility functions used by the external toolchain package
282 # infrastructure. Those functions are mainly responsible for:
283 #
284 #   - installation the toolchain libraries to $(TARGET_DIR)
285 #   - copying the toolchain sysroot to $(STAGING_DIR)
286 #   - installing a gdbinit file
287 #
288 # Details about sysroot directory selection.
289 #
290 # To find the sysroot directory, we use the trick of looking for the
291 # 'libc.a' file with the -print-file-name gcc option, and then
292 # mangling the path to find the base directory of the sysroot.
293 #
294 # Note that we do not use the -print-sysroot option, because it is
295 # only available since gcc 4.4.x, and we only recently dropped support
296 # for 4.2.x and 4.3.x.
297 #
298 # When doing this, we don't pass any option to gcc that could select a
299 # multilib variant (such as -march) as we want the "main" sysroot,
300 # which contains all variants of the C library in the case of multilib
301 # toolchains. We use the TARGET_CC_NO_SYSROOT variable, which is the
302 # path of the cross-compiler, without the --sysroot=$(STAGING_DIR),
303 # since what we want to find is the location of the original toolchain
304 # sysroot. This "main" sysroot directory is stored in SYSROOT_DIR.
305 #
306 # Then, multilib toolchains are a little bit more complicated, since
307 # they in fact have multiple sysroots, one for each variant supported
308 # by the toolchain. So we need to find the particular sysroot we're
309 # interested in.
310 #
311 # To do so, we ask the compiler where its sysroot is by passing all
312 # flags (including -march and al.), except the --sysroot flag since we
313 # want to the compiler to tell us where its original sysroot
314 # is. ARCH_SUBDIR will contain the subdirectory, in the main
315 # SYSROOT_DIR, that corresponds to the selected architecture
316 # variant. ARCH_SYSROOT_DIR will contain the full path to this
317 # location.
318 #
319 # One might wonder why we don't just bother with ARCH_SYSROOT_DIR. The
320 # fact is that in multilib toolchains, the header files are often only
321 # present in the main sysroot, and only the libraries are available in
322 # each variant-specific sysroot directory.
323
324
325 # toolchain_find_sysroot returns the sysroot location for the given
326 # compiler + flags. We need to handle cases where libc.a is in:
327 #
328 #  - lib/
329 #  - usr/lib/
330 #  - lib32/
331 #  - lib64/
332 #  - lib32-fp/ (Cavium toolchain)
333 #  - lib64-fp/ (Cavium toolchain)
334 #  - usr/lib/<tuple>/ (Linaro toolchain)
335 #
336 # And variations on these.
337 define toolchain_find_sysroot
338 $$(printf $(call toolchain_find_libc_a,$(1)) | sed -r -e 's:(usr/)?lib(32|64)?([^/]*)?/([^/]*/)?libc\.a::')
339 endef
340
341 # Returns the lib subdirectory for the given compiler + flags (i.e
342 # typically lib32 or lib64 for some toolchains)
343 define toolchain_find_libdir
344 $$(printf $(call toolchain_find_libc_a,$(1)) | sed -r -e 's:.*/(usr/)?(lib(32|64)?([^/]*)?(/[^/]*)?)/libc.a:\2:')
345 endef
346
347 # Returns the location of the libc.a file for the given compiler + flags
348 define toolchain_find_libc_a
349 $$(readlink -f $$(LANG=C $(1) -print-file-name=libc.a))
350 endef
351
352 # Integration of the toolchain into Buildroot: find the main sysroot
353 # and the variant-specific sysroot, then copy the needed libraries to
354 # the $(TARGET_DIR) and copy the whole sysroot (libraries and headers)
355 # to $(STAGING_DIR).
356 #
357 # Variables are defined as follows:
358 #
359 # SYSROOT_DIR:          the main sysroot directory, deduced from the location of
360 #                       the libc.a file in the default multilib variant, by
361 #                       removing the usr/lib[32|64]/libc.a part of the path.
362 #                       Ex: /x-tools/mips-2011.03/mips-linux-gnu/libc/
363 #
364 # ARCH_SYSROOT_DIR:     the sysroot of the selected multilib variant,
365 #                       deduced from the location of the libc.a file in the
366 #                       selected multilib variant (taking into account the
367 #                       CFLAGS), by removing usr/lib[32|64]/libc.a at the end
368 #                       of the path.
369 #                       Ex: /x-tools/mips-2011.03/mips-linux-gnu/libc/mips16/soft-float/el/
370 #
371 # ARCH_LIB_DIR:         'lib', 'lib32' or 'lib64' depending on where libraries
372 #                       are stored. Deduced from the location of the libc.a file
373 #                       in the selected multilib variant, by looking at
374 #                       usr/lib??/libc.a.
375 #                       Ex: lib
376 #
377 # ARCH_SUBDIR:          the relative location of the sysroot of the selected
378 #                       multilib variant compared to the main sysroot.
379 #                       Ex: mips16/soft-float/el
380 #
381 # SUPPORT_LIB_DIR:      some toolchains, such as recent Linaro toolchains,
382 #                       store GCC support libraries (libstdc++,
383 #                       libgcc_s, etc.) outside of the sysroot. In
384 #                       this case, SUPPORT_LIB_DIR is set to a
385 #                       non-empty value, and points to the directory
386 #                       where these support libraries are
387 #                       available. Those libraries will be copied to
388 #                       our sysroot, and the directory will also be
389 #                       considered when searching libraries for copy
390 #                       to the target filesystem.
391 #
392 # Please be very careful to check the major toolchain sources:
393 # Buildroot, Crosstool-NG, CodeSourcery and Linaro
394 # before doing any modification on the below logic.
395
396 ifeq ($(BR2_STATIC_LIBS),)
397 define TOOLCHAIN_EXTERNAL_INSTALL_TARGET_LIBS
398         $(Q)$(call MESSAGE,"Copying external toolchain libraries to target...")
399         $(Q)for libpattern in $(TOOLCHAIN_EXTERNAL_LIBS); do \
400                 $(call copy_toolchain_lib_root,$$libpattern); \
401         done
402 endef
403 endif
404
405 ifeq ($(BR2_TOOLCHAIN_EXTERNAL_GDB_SERVER_COPY),y)
406 define TOOLCHAIN_EXTERNAL_INSTALL_TARGET_GDBSERVER
407         $(Q)$(call MESSAGE,"Copying gdbserver")
408         $(Q)ARCH_SYSROOT_DIR="$(call toolchain_find_sysroot,$(TOOLCHAIN_EXTERNAL_CC) $(TOOLCHAIN_EXTERNAL_CFLAGS))" ; \
409         ARCH_LIB_DIR="$(call toolchain_find_libdir,$(TOOLCHAIN_EXTERNAL_CC) $(TOOLCHAIN_EXTERNAL_CFLAGS))" ; \
410         gdbserver_found=0 ; \
411         for d in $${ARCH_SYSROOT_DIR}/usr \
412                  $${ARCH_SYSROOT_DIR}/../debug-root/usr \
413                  $${ARCH_SYSROOT_DIR}/usr/$${ARCH_LIB_DIR} \
414                  $(TOOLCHAIN_EXTERNAL_INSTALL_DIR); do \
415                 if test -f $${d}/bin/gdbserver ; then \
416                         install -m 0755 -D $${d}/bin/gdbserver $(TARGET_DIR)/usr/bin/gdbserver ; \
417                         gdbserver_found=1 ; \
418                         break ; \
419                 fi ; \
420         done ; \
421         if [ $${gdbserver_found} -eq 0 ] ; then \
422                 echo "Could not find gdbserver in external toolchain" ; \
423                 exit 1 ; \
424         fi
425 endef
426 endif
427
428 define TOOLCHAIN_EXTERNAL_INSTALL_SYSROOT_LIBS
429         $(Q)SYSROOT_DIR="$(call toolchain_find_sysroot,$(TOOLCHAIN_EXTERNAL_CC))" ; \
430         ARCH_SYSROOT_DIR="$(call toolchain_find_sysroot,$(TOOLCHAIN_EXTERNAL_CC) $(TOOLCHAIN_EXTERNAL_CFLAGS))" ; \
431         ARCH_LIB_DIR="$(call toolchain_find_libdir,$(TOOLCHAIN_EXTERNAL_CC) $(TOOLCHAIN_EXTERNAL_CFLAGS))" ; \
432         SUPPORT_LIB_DIR="" ; \
433         if test `find $${ARCH_SYSROOT_DIR} -name 'libstdc++.a' | wc -l` -eq 0 ; then \
434                 LIBSTDCPP_A_LOCATION=$$(LANG=C $(TOOLCHAIN_EXTERNAL_CC) $(TOOLCHAIN_EXTERNAL_CFLAGS) -print-file-name=libstdc++.a) ; \
435                 if [ -e "$${LIBSTDCPP_A_LOCATION}" ]; then \
436                         SUPPORT_LIB_DIR=`readlink -f $${LIBSTDCPP_A_LOCATION} | sed -r -e 's:libstdc\+\+\.a::'` ; \
437                 fi ; \
438         fi ; \
439         if [ "$${SYSROOT_DIR}" == "$${ARCH_SYSROOT_DIR}" ] ; then \
440                 ARCH_SUBDIR="" ; \
441         elif [ "`dirname $${ARCH_SYSROOT_DIR}`" = "`dirname $${SYSROOT_DIR}`" ] ; then \
442                 SYSROOT_DIR_DIRNAME=`dirname $${SYSROOT_DIR}`/ ; \
443                 ARCH_SUBDIR=`echo $${ARCH_SYSROOT_DIR} | sed -r -e "s:^$${SYSROOT_DIR_DIRNAME}(.*)/$$:\1:"` ; \
444         else \
445                 ARCH_SUBDIR=`echo $${ARCH_SYSROOT_DIR} | sed -r -e "s:^$${SYSROOT_DIR}(.*)/$$:\1:"` ; \
446         fi ; \
447         $(call MESSAGE,"Copying external toolchain sysroot to staging...") ; \
448         $(call copy_toolchain_sysroot,$${SYSROOT_DIR},$${ARCH_SYSROOT_DIR},$${ARCH_SUBDIR},$${ARCH_LIB_DIR},$${SUPPORT_LIB_DIR})
449 endef
450
451 # Create a symlink from (usr/)$(ARCH_LIB_DIR) to lib.
452 # Note: the skeleton package additionally creates lib32->lib or lib64->lib
453 # (as appropriate)
454 #
455 # $1: destination directory (TARGET_DIR / STAGING_DIR)
456 create_lib_symlinks = \
457         $(Q)DESTDIR="$(strip $1)" ; \
458         ARCH_LIB_DIR="$(call toolchain_find_libdir,$(TOOLCHAIN_EXTERNAL_CC) $(TOOLCHAIN_EXTERNAL_CFLAGS))" ; \
459         if [ ! -e "$${DESTDIR}/$${ARCH_LIB_DIR}" -a ! -e "$${DESTDIR}/usr/$${ARCH_LIB_DIR}" ]; then \
460                 relpath="$(call relpath_prefix,$${ARCH_LIB_DIR})" ; \
461                 ln -snf $${relpath}lib "$${DESTDIR}/$${ARCH_LIB_DIR}" ; \
462                 ln -snf $${relpath}lib "$${DESTDIR}/usr/$${ARCH_LIB_DIR}" ; \
463         fi
464
465 define TOOLCHAIN_EXTERNAL_CREATE_STAGING_LIB_SYMLINK
466         $(call create_lib_symlinks,$(STAGING_DIR))
467 endef
468
469 define TOOLCHAIN_EXTERNAL_CREATE_TARGET_LIB_SYMLINK
470         $(call create_lib_symlinks,$(TARGET_DIR))
471 endef
472
473 #
474 # Generate gdbinit file for use with Buildroot
475 #
476 define TOOLCHAIN_EXTERNAL_INSTALL_GDBINIT
477         $(Q)if test -f $(TARGET_CROSS)gdb ; then \
478                 $(call MESSAGE,"Installing gdbinit"); \
479                 $(gen_gdbinit_file); \
480         fi
481 endef
482
483 # uClibc-ng dynamic loader is called ld-uClibc.so.1, but gcc is not
484 # patched specifically for uClibc-ng, so it continues to generate
485 # binaries that expect the dynamic loader to be named ld-uClibc.so.0,
486 # like with the original uClibc. Therefore, we create an additional
487 # symbolic link to make uClibc-ng systems work properly.
488 define TOOLCHAIN_EXTERNAL_FIXUP_UCLIBCNG_LDSO
489         $(Q)if test -e $(TARGET_DIR)/lib/ld-uClibc.so.1; then \
490                 ln -sf ld-uClibc.so.1 $(TARGET_DIR)/lib/ld-uClibc.so.0 ; \
491         fi
492         $(Q)if test -e $(TARGET_DIR)/lib/ld64-uClibc.so.1; then \
493                 ln -sf ld64-uClibc.so.1 $(TARGET_DIR)/lib/ld64-uClibc.so.0 ; \
494         fi
495 endef
496
497
498 ################################################################################
499 # inner-toolchain-external-package -- defines the generic installation rules
500 # for external toolchain packages
501 #
502 #  argument 1 is the lowercase package name
503 #  argument 2 is the uppercase package name, including a HOST_ prefix
504 #             for host packages
505 #  argument 3 is the uppercase package name, without the HOST_ prefix
506 #             for host packages
507 #  argument 4 is the type (target or host)
508 ################################################################################
509 define inner-toolchain-external-package
510
511 $(2)_INSTALL_STAGING = YES
512 $(2)_ADD_TOOLCHAIN_DEPENDENCY = NO
513
514 # In fact, we don't need to download the toolchain, since it is already
515 # available on the system, so force the site and source to be empty so
516 # that nothing will be downloaded/extracted.
517 ifeq ($$(BR2_TOOLCHAIN_EXTERNAL_PREINSTALLED),y)
518 $(2)_SITE =
519 $(2)_SOURCE =
520 endif
521
522 ifeq ($$(BR2_TOOLCHAIN_EXTERNAL_DOWNLOAD),y)
523 $(2)_EXCLUDES = usr/lib/locale/*
524
525 $(2)_POST_EXTRACT_HOOKS += \
526         TOOLCHAIN_EXTERNAL_MOVE
527 endif
528
529 # Checks for an already installed toolchain: check the toolchain
530 # location, check that it is usable, and then verify that it
531 # matches the configuration provided in Buildroot: ABI, C++ support,
532 # kernel headers version, type of C library and all C library features.
533 define $(2)_CONFIGURE_CMDS
534         $$(Q)$$(call check_cross_compiler_exists,$$(TOOLCHAIN_EXTERNAL_CC))
535         $$(Q)$$(call check_unusable_toolchain,$$(TOOLCHAIN_EXTERNAL_CC))
536         $$(Q)SYSROOT_DIR="$$(call toolchain_find_sysroot,$$(TOOLCHAIN_EXTERNAL_CC))" ; \
537         $$(call check_kernel_headers_version,\
538                 $$(call toolchain_find_sysroot,$$(TOOLCHAIN_EXTERNAL_CC)),\
539                 $$(call qstrip,$$(BR2_TOOLCHAIN_HEADERS_AT_LEAST))); \
540         $$(call check_gcc_version,$$(TOOLCHAIN_EXTERNAL_CC),\
541                 $$(call qstrip,$$(BR2_TOOLCHAIN_GCC_AT_LEAST))); \
542         if test "$$(BR2_arm)" = "y" ; then \
543                 $$(call check_arm_abi,\
544                         "$$(TOOLCHAIN_EXTERNAL_CC) $$(TOOLCHAIN_EXTERNAL_CFLAGS)") ; \
545         fi ; \
546         if test "$$(BR2_INSTALL_LIBSTDCPP)" = "y" ; then \
547                 $$(call check_cplusplus,$$(TOOLCHAIN_EXTERNAL_CXX)) ; \
548         fi ; \
549         if test "$$(BR2_TOOLCHAIN_HAS_FORTRAN)" = "y" ; then \
550                 $$(call check_fortran,$$(TOOLCHAIN_EXTERNAL_FC)) ; \
551         fi ; \
552         if test "$$(BR2_TOOLCHAIN_EXTERNAL_UCLIBC)" = "y" ; then \
553                 $$(call check_uclibc,$$$${SYSROOT_DIR}) ; \
554         elif test "$$(BR2_TOOLCHAIN_EXTERNAL_MUSL)" = "y" ; then \
555                 $$(call check_musl,\
556                         "$$(TOOLCHAIN_EXTERNAL_CC) $$(TOOLCHAIN_EXTERNAL_CFLAGS)",\
557                         $$(TOOLCHAIN_EXTERNAL_READELF)) ; \
558         else \
559                 $$(call check_glibc,$$$${SYSROOT_DIR}) ; \
560         fi
561         $$(Q)$$(call check_toolchain_ssp,$$(TOOLCHAIN_EXTERNAL_CC))
562 endef
563
564 $(2)_TOOLCHAIN_WRAPPER_ARGS += $$(TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS)
565
566 $(2)_BUILD_CMDS = $$(TOOLCHAIN_WRAPPER_BUILD)
567
568 define $(2)_INSTALL_STAGING_CMDS
569         $$(TOOLCHAIN_WRAPPER_INSTALL)
570         $$(TOOLCHAIN_EXTERNAL_CREATE_STAGING_LIB_SYMLINK)
571         $$(TOOLCHAIN_EXTERNAL_INSTALL_SYSROOT_LIBS)
572         $$(TOOLCHAIN_EXTERNAL_INSTALL_WRAPPER)
573         $$(TOOLCHAIN_EXTERNAL_INSTALL_GDBINIT)
574 endef
575
576 # Even though we're installing things in both the staging, the host
577 # and the target directory, we do everything within the
578 # install-staging step, arbitrarily.
579 define $(2)_INSTALL_TARGET_CMDS
580         $$(TOOLCHAIN_EXTERNAL_CREATE_TARGET_LIB_SYMLINK)
581         $$(TOOLCHAIN_EXTERNAL_INSTALL_TARGET_LIBS)
582         $$(TOOLCHAIN_EXTERNAL_INSTALL_TARGET_GDBSERVER)
583         $$(TOOLCHAIN_EXTERNAL_FIXUP_UCLIBCNG_LDSO)
584 endef
585
586 # Call the generic package infrastructure to generate the necessary
587 # make targets
588 $(call inner-generic-package,$(1),$(2),$(3),$(4))
589
590 endef
591
592 toolchain-external-package = $(call inner-toolchain-external-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)