1 ################################################################################
2 # External toolchain package infrastructure
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
11 # * Toolchains generated by Crosstool-NG
12 # * Toolchains generated by Buildroot
13 # * Toolchains provided by Linaro for the ARM and AArch64
15 # * Sourcery CodeBench toolchains (from Mentor Graphics) for the ARM,
16 # MIPS, PowerPC, x86, 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 # * Xilinx toolchains for the Microblaze architecture
20 # * Synopsys DesignWare toolchains for ARC cores
22 # The basic principle is the following
24 # 1. If the toolchain is not pre-installed, download and extract it
25 # in $(TOOLCHAIN_EXTERNAL_INSTALL_DIR). Otherwise,
26 # $(TOOLCHAIN_EXTERNAL_INSTALL_DIR) points to were the toolchain has
27 # already been installed by the user.
29 # 2. For all external toolchains, perform some checks on the
30 # conformity between the toolchain configuration described in the
31 # Buildroot menuconfig system, and the real configuration of the
32 # external toolchain. This is for example important to make sure that
33 # the Buildroot configuration system knows whether the toolchain
34 # supports RPC, IPv6, locales, large files, etc. Unfortunately, these
35 # things cannot be detected automatically, since the value of these
36 # options (such as BR2_TOOLCHAIN_HAS_NATIVE_RPC) are needed at
37 # configuration time because these options are used as dependencies
38 # for other options. And at configuration time, we are not able to
39 # retrieve the external toolchain configuration.
41 # 3. Copy the libraries needed at runtime to the target directory,
42 # $(TARGET_DIR). Obviously, things such as the C library, the dynamic
43 # loader and a few other utility libraries are needed if dynamic
44 # applications are to be executed on the target system.
46 # 4. Copy the libraries and headers to the staging directory. This
47 # will allow all further calls to gcc to be made using --sysroot
48 # $(STAGING_DIR), which greatly simplifies the compilation of the
49 # packages when using external toolchains. So in the end, only the
50 # cross-compiler binaries remains external, all libraries and headers
51 # are imported into the Buildroot tree.
53 # 5. Build a toolchain wrapper which executes the external toolchain
54 # with a number of arguments (sysroot/march/mtune/..) hardcoded,
55 # so we're sure the correct configuration is always used and the
56 # toolchain behaves similar to an internal toolchain.
57 # This toolchain wrapper and symlinks are installed into
58 # $(HOST_DIR)/usr/bin like for the internal toolchains, and the rest
59 # of Buildroot is handled identical for the 2 toolchain types.
60 ################################################################################
63 # Definitions of where the toolchain can be found
66 TOOLCHAIN_EXTERNAL_PREFIX
= $(call qstrip
,$(BR2_TOOLCHAIN_EXTERNAL_PREFIX
))
67 TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR
= $(HOST_DIR
)/opt
/ext-toolchain
69 ifeq ($(BR2_TOOLCHAIN_EXTERNAL_DOWNLOAD
),y
)
70 TOOLCHAIN_EXTERNAL_INSTALL_DIR
= $(TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR
)
72 TOOLCHAIN_EXTERNAL_INSTALL_DIR
= $(call qstrip
,$(BR2_TOOLCHAIN_EXTERNAL_PATH
))
75 ifeq ($(TOOLCHAIN_EXTERNAL_INSTALL_DIR
),)
76 ifneq ($(TOOLCHAIN_EXTERNAL_PREFIX
),)
77 # if no path set, figure it out from path
78 TOOLCHAIN_EXTERNAL_BIN
:= $(shell dirname
$(shell which
$(TOOLCHAIN_EXTERNAL_PREFIX
)-gcc
))
81 TOOLCHAIN_EXTERNAL_BIN
:= $(TOOLCHAIN_EXTERNAL_INSTALL_DIR
)/bin
84 # If this is a buildroot toolchain, it already has a wrapper which we want to
85 # bypass. Since this is only evaluated after it has been extracted, we can use
86 # $(wildcard ...) here.
87 TOOLCHAIN_EXTERNAL_SUFFIX
= \
88 $(if
$(wildcard $(TOOLCHAIN_EXTERNAL_BIN
)/*.br_real
),.br_real
)
90 TOOLCHAIN_EXTERNAL_CROSS
= $(TOOLCHAIN_EXTERNAL_BIN
)/$(TOOLCHAIN_EXTERNAL_PREFIX
)-
91 TOOLCHAIN_EXTERNAL_CC
= $(TOOLCHAIN_EXTERNAL_CROSS
)gcc
$(TOOLCHAIN_EXTERNAL_SUFFIX
)
92 TOOLCHAIN_EXTERNAL_CXX
= $(TOOLCHAIN_EXTERNAL_CROSS
)g
++$(TOOLCHAIN_EXTERNAL_SUFFIX
)
93 TOOLCHAIN_EXTERNAL_FC
= $(TOOLCHAIN_EXTERNAL_CROSS
)gfortran
$(TOOLCHAIN_EXTERNAL_SUFFIX
)
94 TOOLCHAIN_EXTERNAL_READELF
= $(TOOLCHAIN_EXTERNAL_CROSS
)readelf
96 # Normal handling of downloaded toolchain tarball extraction.
97 ifeq ($(BR2_TOOLCHAIN_EXTERNAL_DOWNLOAD
),y
)
98 # As a regular package, the toolchain gets extracted in $(@D), but
99 # since it's actually a fairly special package, we need it to be moved
100 # into TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR.
101 define TOOLCHAIN_EXTERNAL_MOVE
102 rm -rf
$(TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR
)
103 mkdir
-p
$(TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR
)
104 mv
$(@D
)/* $(TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR
)/
109 # Definitions of the list of libraries that should be copied to the target.
111 ifeq ($(BR2_TOOLCHAIN_EXTERNAL_GLIBC
)$(BR2_TOOLCHAIN_EXTERNAL_UCLIBC
),y
)
112 TOOLCHAIN_EXTERNAL_LIBS
+= libatomic.so.
* libc.so.
* libcrypt.so.
* libdl.so.
* libgcc_s.so.
* libm.so.
* libnsl.so.
* libresolv.so.
* librt.so.
* libutil.so.
*
113 ifeq ($(BR2_TOOLCHAIN_EXTERNAL_GLIBC
)$(BR2_ARM_EABIHF
),yy
)
114 TOOLCHAIN_EXTERNAL_LIBS
+= ld-linux-armhf.so.
*
116 TOOLCHAIN_EXTERNAL_LIBS
+= ld*.so.
*
118 ifeq ($(BR2_TOOLCHAIN_HAS_THREADS
),y
)
119 TOOLCHAIN_EXTERNAL_LIBS
+= libpthread.so.
*
120 ifneq ($(BR2_PACKAGE_GDB
)$(BR2_TOOLCHAIN_EXTERNAL_GDB_SERVER_COPY
),)
121 TOOLCHAIN_EXTERNAL_LIBS
+= libthread_db.so.
*
126 ifeq ($(BR2_TOOLCHAIN_EXTERNAL_GLIBC
),y
)
127 TOOLCHAIN_EXTERNAL_LIBS
+= libnss_files.so.
* libnss_dns.so.
* libmvec.so.
* libanl.so.
*
130 ifeq ($(BR2_TOOLCHAIN_EXTERNAL_MUSL
),y
)
131 TOOLCHAIN_EXTERNAL_LIBS
+= libc.so libgcc_s.so.
*
134 ifeq ($(BR2_INSTALL_LIBSTDCPP
),y
)
135 TOOLCHAIN_EXTERNAL_LIBS
+= libstdc
++.so.
*
138 ifeq ($(BR2_TOOLCHAIN_HAS_FORTRAN
),y
)
139 TOOLCHAIN_EXTERNAL_LIBS
+= libgfortran.so.
*
140 # fortran needs quadmath on x86 and x86_64
141 ifeq ($(BR2_TOOLCHAIN_HAS_LIBQUADMATH
),y
)
142 TOOLCHAIN_EXTERNAL_LIBS
+= libquadmath.so
*
148 # Definition of the CFLAGS to use with the external toolchain, as well as the
149 # common toolchain wrapper build arguments
151 ifeq ($(call qstrip
,$(BR2_GCC_TARGET_CPU_REVISION
)),)
152 CC_TARGET_CPU_
:= $(call qstrip
,$(BR2_GCC_TARGET_CPU
))
154 CC_TARGET_CPU_
:= $(call qstrip
,$(BR2_GCC_TARGET_CPU
)-$(BR2_GCC_TARGET_CPU_REVISION
))
156 CC_TARGET_ARCH_
:= $(call qstrip
,$(BR2_GCC_TARGET_ARCH
))
157 CC_TARGET_ABI_
:= $(call qstrip
,$(BR2_GCC_TARGET_ABI
))
158 CC_TARGET_FPU_
:= $(call qstrip
,$(BR2_GCC_TARGET_FPU
))
159 CC_TARGET_FLOAT_ABI_
:= $(call qstrip
,$(BR2_GCC_TARGET_FLOAT_ABI
))
160 CC_TARGET_MODE_
:= $(call qstrip
,$(BR2_GCC_TARGET_MODE
))
162 # march/mtune/floating point mode needs to be passed to the external toolchain
163 # to select the right multilib variant
164 ifeq ($(BR2_x86_64
),y
)
165 TOOLCHAIN_EXTERNAL_CFLAGS
+= -m64
166 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS
+= -DBR_64
168 ifneq ($(CC_TARGET_ARCH_
),)
169 TOOLCHAIN_EXTERNAL_CFLAGS
+= -march
=$(CC_TARGET_ARCH_
)
170 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS
+= -DBR_ARCH
='"$(CC_TARGET_ARCH_)"'
172 ifneq ($(CC_TARGET_CPU_
),)
173 TOOLCHAIN_EXTERNAL_CFLAGS
+= -mcpu
=$(CC_TARGET_CPU_
)
174 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS
+= -DBR_CPU
='"$(CC_TARGET_CPU_)"'
176 ifneq ($(CC_TARGET_ABI_
),)
177 TOOLCHAIN_EXTERNAL_CFLAGS
+= -mabi
=$(CC_TARGET_ABI_
)
178 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS
+= -DBR_ABI
='"$(CC_TARGET_ABI_)"'
180 ifneq ($(CC_TARGET_FPU_
),)
181 TOOLCHAIN_EXTERNAL_CFLAGS
+= -mfpu
=$(CC_TARGET_FPU_
)
182 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS
+= -DBR_FPU
='"$(CC_TARGET_FPU_)"'
184 ifneq ($(CC_TARGET_FLOAT_ABI_
),)
185 TOOLCHAIN_EXTERNAL_CFLAGS
+= -mfloat-abi
=$(CC_TARGET_FLOAT_ABI_
)
186 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS
+= -DBR_FLOAT_ABI
='"$(CC_TARGET_FLOAT_ABI_)"'
188 ifneq ($(CC_TARGET_MODE_
),)
189 TOOLCHAIN_EXTERNAL_CFLAGS
+= -m
$(CC_TARGET_MODE_
)
190 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS
+= -DBR_MODE
='"$(CC_TARGET_MODE_)"'
192 ifeq ($(BR2_BINFMT_FLAT
),y
)
193 TOOLCHAIN_EXTERNAL_CFLAGS
+= -Wl
,-elf2flt
194 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS
+= -DBR_BINFMT_FLAT
196 ifeq ($(BR2_mipsel
)$(BR2_mips64el
),y
)
197 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS
+= -DBR_MIPS_TARGET_LITTLE_ENDIAN
198 TOOLCHAIN_EXTERNAL_CFLAGS
+= -EL
200 ifeq ($(BR2_mips
)$(BR2_mips64
),y
)
201 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS
+= -DBR_MIPS_TARGET_BIG_ENDIAN
202 TOOLCHAIN_EXTERNAL_CFLAGS
+= -EB
204 ifeq ($(BR2_arceb
),y
)
205 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS
+= -DBR_ARC_TARGET_BIG_ENDIAN
206 TOOLCHAIN_EXTERNAL_CFLAGS
+= -EB
209 TOOLCHAIN_EXTERNAL_CFLAGS
+= $(call qstrip
,$(BR2_TARGET_OPTIMIZATION
))
211 ifeq ($(BR2_SOFT_FLOAT
),y
)
212 TOOLCHAIN_EXTERNAL_CFLAGS
+= -msoft-float
213 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS
+= -DBR_SOFTFLOAT
=1
216 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS
+= \
217 -DBR_CROSS_PATH_SUFFIX
='"$(TOOLCHAIN_EXTERNAL_SUFFIX)"'
219 ifeq ($(filter $(HOST_DIR
)/%,$(TOOLCHAIN_EXTERNAL_BIN
)),)
220 # TOOLCHAIN_EXTERNAL_BIN points outside HOST_DIR => absolute path
221 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS
+= \
222 -DBR_CROSS_PATH_ABS
='"$(TOOLCHAIN_EXTERNAL_BIN)"'
224 # TOOLCHAIN_EXTERNAL_BIN points inside HOST_DIR => relative path
225 TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS
+= \
226 -DBR_CROSS_PATH_REL
='"$(TOOLCHAIN_EXTERNAL_BIN:$(HOST_DIR)/%=%)"'
231 # The following functions creates the symbolic links needed to get the
232 # cross-compilation tools visible in $(HOST_DIR)/usr/bin. Some of
233 # links are done directly to the corresponding tool in the external
234 # toolchain installation directory, while some other links are done to
235 # the toolchain wrapper (preprocessor, C, C++ and Fortran compiler)
237 # We skip gdb symlink when we are building our own gdb to prevent two
238 # gdb's in $(HOST_DIR)/usr/bin.
240 # The LTO support in gcc creates wrappers for ar, ranlib and nm which load
241 # the lto plugin. These wrappers are called *-gcc-ar, *-gcc-ranlib, and
242 # *-gcc-nm and should be used instead of the real programs when -flto is
243 # used. However, we should not add the toolchain wrapper for them, and they
244 # match the *cc-* pattern. Therefore, an additional case is added for *-ar,
246 define TOOLCHAIN_EXTERNAL_INSTALL_WRAPPER
247 $(Q
)cd
$(HOST_DIR
)/usr
/bin
; \
248 for i in
$(TOOLCHAIN_EXTERNAL_CROSS
)*; do \
251 *-ar|
*-ranlib|
*-nm
) \
252 ln
-sf
$$(echo
$$i | sed
's%^$(HOST_DIR)%../..%') .
; \
254 *cc|
*cc-
*|
*++|
*++-*|
*cpp|
*-gfortran
) \
255 ln
-sf toolchain-wrapper
$$base; \
258 if
test "$(BR2_PACKAGE_HOST_GDB)" != "y"; then \
259 ln
-sf
$$(echo
$$i | sed
's%^$(HOST_DIR)%../..%') .
; \
263 ln
-sf
$$(echo
$$i | sed
's%^$(HOST_DIR)%../..%') .
; \
270 # Various utility functions used by the external toolchain package
271 # infrastructure. Those functions are mainly responsible for:
273 # - installation the toolchain libraries to $(TARGET_DIR)
274 # - copying the toolchain sysroot to $(STAGING_DIR)
275 # - installing a gdbinit file
277 # Details about sysroot directory selection.
279 # To find the sysroot directory, we use the trick of looking for the
280 # 'libc.a' file with the -print-file-name gcc option, and then
281 # mangling the path to find the base directory of the sysroot.
283 # Note that we do not use the -print-sysroot option, because it is
284 # only available since gcc 4.4.x, and we only recently dropped support
285 # for 4.2.x and 4.3.x.
287 # When doing this, we don't pass any option to gcc that could select a
288 # multilib variant (such as -march) as we want the "main" sysroot,
289 # which contains all variants of the C library in the case of multilib
290 # toolchains. We use the TARGET_CC_NO_SYSROOT variable, which is the
291 # path of the cross-compiler, without the --sysroot=$(STAGING_DIR),
292 # since what we want to find is the location of the original toolchain
293 # sysroot. This "main" sysroot directory is stored in SYSROOT_DIR.
295 # Then, multilib toolchains are a little bit more complicated, since
296 # they in fact have multiple sysroots, one for each variant supported
297 # by the toolchain. So we need to find the particular sysroot we're
300 # To do so, we ask the compiler where its sysroot is by passing all
301 # flags (including -march and al.), except the --sysroot flag since we
302 # want to the compiler to tell us where its original sysroot
303 # is. ARCH_SUBDIR will contain the subdirectory, in the main
304 # SYSROOT_DIR, that corresponds to the selected architecture
305 # variant. ARCH_SYSROOT_DIR will contain the full path to this
308 # One might wonder why we don't just bother with ARCH_SYSROOT_DIR. The
309 # fact is that in multilib toolchains, the header files are often only
310 # present in the main sysroot, and only the libraries are available in
311 # each variant-specific sysroot directory.
314 # toolchain_find_sysroot returns the sysroot location for the given
315 # compiler + flags. We need to handle cases where libc.a is in:
321 # - lib32-fp/ (Cavium toolchain)
322 # - lib64-fp/ (Cavium toolchain)
323 # - usr/lib/<tuple>/ (Linaro toolchain)
325 # And variations on these.
326 define toolchain_find_sysroot
327 $$(printf
$(call toolchain_find_libc_a
,$(1)) | sed
-r
-e
's:(usr/)?lib(32|64)?([^/]*)?/([^/]*/)?libc\.a::')
330 # Returns the lib subdirectory for the given compiler + flags (i.e
331 # typically lib32 or lib64 for some toolchains)
332 define toolchain_find_libdir
333 $$(printf
$(call toolchain_find_libc_a
,$(1)) | sed
-r
-e
's:.*/(usr/)?(lib(32|64)?([^/]*)?(/[^/]*)?)/libc.a:\2:')
336 # Returns the location of the libc.a file for the given compiler + flags
337 define toolchain_find_libc_a
338 $$(readlink
-f
$$(LANG
=C
$(1) -print-file-name
=libc.a
))
341 # Integration of the toolchain into Buildroot: find the main sysroot
342 # and the variant-specific sysroot, then copy the needed libraries to
343 # the $(TARGET_DIR) and copy the whole sysroot (libraries and headers)
346 # Variables are defined as follows:
348 # SYSROOT_DIR: the main sysroot directory, deduced from the location of
349 # the libc.a file in the default multilib variant, by
350 # removing the usr/lib[32|64]/libc.a part of the path.
351 # Ex: /x-tools/mips-2011.03/mips-linux-gnu/libc/
353 # ARCH_SYSROOT_DIR: the sysroot of the selected multilib variant,
354 # deduced from the location of the libc.a file in the
355 # selected multilib variant (taking into account the
356 # CFLAGS), by removing usr/lib[32|64]/libc.a at the end
358 # Ex: /x-tools/mips-2011.03/mips-linux-gnu/libc/mips16/soft-float/el/
360 # ARCH_LIB_DIR: 'lib', 'lib32' or 'lib64' depending on where libraries
361 # are stored. Deduced from the location of the libc.a file
362 # in the selected multilib variant, by looking at
366 # ARCH_SUBDIR: the relative location of the sysroot of the selected
367 # multilib variant compared to the main sysroot.
368 # Ex: mips16/soft-float/el
370 # SUPPORT_LIB_DIR: some toolchains, such as recent Linaro toolchains,
371 # store GCC support libraries (libstdc++,
372 # libgcc_s, etc.) outside of the sysroot. In
373 # this case, SUPPORT_LIB_DIR is set to a
374 # non-empty value, and points to the directory
375 # where these support libraries are
376 # available. Those libraries will be copied to
377 # our sysroot, and the directory will also be
378 # considered when searching libraries for copy
379 # to the target filesystem.
381 # Please be very careful to check the major toolchain sources:
382 # Buildroot, Crosstool-NG, CodeSourcery and Linaro
383 # before doing any modification on the below logic.
385 ################################################################################
386 # inner-toolchain-external-package -- defines the generic installation rules
387 # for external toolchain packages
389 # argument 1 is the lowercase package name
390 # argument 2 is the uppercase package name, including a HOST_ prefix
392 # argument 3 is the uppercase package name, without the HOST_ prefix
394 # argument 4 is the type (target or host)
395 ################################################################################
396 define inner-toolchain-external-package
398 $(2)_INSTALL_STAGING
= NO
399 $(2)_ADD_TOOLCHAIN_DEPENDENCY
= NO
401 # In fact, we don't need to download the toolchain, since it is already
402 # available on the system, so force the site and source to be empty so
403 # that nothing will be downloaded/extracted.
404 ifeq ($$(BR2_TOOLCHAIN_EXTERNAL_PREINSTALLED
),y
)
410 $(2)_TOOLCHAIN_WRAPPER_ARGS
+= $$(TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS
)
412 $(2)_BUILD_CMDS
= $$(TOOLCHAIN_WRAPPER_BUILD
)
414 define $(2)_INSTALL_STAGING_CMDS
415 $$(TOOLCHAIN_WRAPPER_INSTALL
)
416 $$(TOOLCHAIN_EXTERNAL_INSTALL_WRAPPER
)
419 ifeq ($$(BR2_TOOLCHAIN_EXTERNAL_MUSL
),y
)
420 $(2)_POST_INSTALL_STAGING_HOOKS
+= TOOLCHAIN_EXTERNAL_MUSL_LD_LINK
423 # Even though we're installing things in both the staging, the host
424 # and the target directory, we do everything within the
425 # install-staging step, arbitrarily.
426 define $(2)_INSTALL_TARGET_CMDS
427 $$(TOOLCHAIN_EXTERNAL_CREATE_TARGET_LIB_SYMLINK
)
428 $$(TOOLCHAIN_EXTERNAL_INSTALL_TARGET_LIBS
)
429 $$(TOOLCHAIN_EXTERNAL_INSTALL_TARGET_GDBSERVER
)
430 $$(TOOLCHAIN_EXTERNAL_FIXUP_UCLIBCNG_LDSO
)
433 # Call the generic package infrastructure to generate the necessary
435 $(call inner-generic-package
,$(1),$(2),$(3),$(4))
439 toolchain-external-package
= $(call inner-toolchain-external-package
,$(pkgname
),$(call UPPERCASE
,$(pkgname
)),$(call UPPERCASE
,$(pkgname
)),target
)