toolchain/external: newer Linaro toolchains do not provide source code
[buildroot-gz.git] / toolchain / helpers.mk
blobee878e89a16ed8fb65a29345373446aef7078f8b
1 # This Makefile fragment declares toolchain related helper functions.
3 # The copy_toolchain_lib_root function copies a toolchain library and
4 # its symbolic links from the sysroot directory to the target
5 # directory. Note that this function is used both by the external
6 # toolchain logic, and the glibc package, so care must be taken when
7 # changing this function.
9 # Most toolchains (CodeSourcery ones) have their libraries either in
10 # /lib or /usr/lib relative to their ARCH_SYSROOT_DIR, so we search
11 # libraries in:
13 # $${ARCH_LIB_DIR}
14 # usr/$${ARCH_LIB_DIR}
16 # Buildroot toolchains, however, have basic libraries in /lib, and
17 # libstdc++/libgcc_s in /usr/<target-name>/lib(64), so we also need to
18 # search libraries in:
20 # usr/$(TOOLCHAIN_EXTERNAL_PREFIX)/$${ARCH_LIB_DIR}
22 # Linaro toolchains have most libraries in lib/<target-name>/, so we
23 # need to search libraries in:
25 # $${ARCH_LIB_DIR}/$(TOOLCHAIN_EXTERNAL_PREFIX)
27 # And recent Linaro toolchains have the GCC support libraries
28 # (libstdc++, libgcc_s, etc.) into a separate directory, outside of
29 # the sysroot, that we called the "SUPPORT_LIB_DIR", into which we
30 # need to search as well.
32 # Thanks to ARCH_LIB_DIR we also take into account toolchains that
33 # have the libraries in lib64 and usr/lib64.
35 # Please be very careful to check the major toolchain sources:
36 # Buildroot, Crosstool-NG, CodeSourcery and Linaro before doing any
37 # modification on the below logic.
39 # $1: arch specific sysroot directory
40 # $2: support libraries directory (can be empty)
41 # $3: library directory ('lib' or 'lib64') from which libraries must be copied
42 # $4: library name
43 # $5: destination directory of the libary, relative to $(TARGET_DIR)
45 copy_toolchain_lib_root = \
46 ARCH_SYSROOT_DIR="$(strip $1)"; \
47 SUPPORT_LIB_DIR="$(strip $2)" ; \
48 ARCH_LIB_DIR="$(strip $3)" ; \
49 LIB="$(strip $4)"; \
50 DESTDIR="$(strip $5)" ; \
52 for dir in \
53 $${ARCH_SYSROOT_DIR}/$${ARCH_LIB_DIR}/$(TOOLCHAIN_EXTERNAL_PREFIX) \
54 $${ARCH_SYSROOT_DIR}/usr/$(TOOLCHAIN_EXTERNAL_PREFIX)/$${ARCH_LIB_DIR} \
55 $${ARCH_SYSROOT_DIR}/$${ARCH_LIB_DIR} \
56 $${ARCH_SYSROOT_DIR}/usr/$${ARCH_LIB_DIR} \
57 $${SUPPORT_LIB_DIR} ; do \
58 LIBPATHS=`find $${dir} -maxdepth 1 -name "$${LIB}" 2>/dev/null` ; \
59 if test -n "$${LIBPATHS}" ; then \
60 break ; \
61 fi \
62 done ; \
63 mkdir -p $(TARGET_DIR)/$${DESTDIR}; \
64 for LIBPATH in $${LIBPATHS} ; do \
65 while true ; do \
66 LIBNAME=`basename $${LIBPATH}`; \
67 LIBDIR=`dirname $${LIBPATH}` ; \
68 LINKTARGET=`readlink $${LIBPATH}` ; \
69 rm -fr $(TARGET_DIR)/$${DESTDIR}/$${LIBNAME}; \
70 if test -h $${LIBPATH} ; then \
71 ln -sf `basename $${LINKTARGET}` $(TARGET_DIR)/$${DESTDIR}/$${LIBNAME} ; \
72 elif test -f $${LIBPATH}; then \
73 $(INSTALL) -D -m0755 $${LIBPATH} $(TARGET_DIR)/$${DESTDIR}/$${LIBNAME}; \
74 else \
75 exit -1; \
76 fi; \
77 if test -z "$${LINKTARGET}" ; then \
78 break ; \
79 fi ; \
80 LIBPATH="`readlink -f $${LIBPATH}`"; \
81 done; \
82 done
85 # Copy the full external toolchain sysroot directory to the staging
86 # dir. The operation of this function is rendered a little bit
87 # complicated by the support for multilib toolchains.
89 # We start by copying etc, lib, sbin and usr from the sysroot of the
90 # selected architecture variant (as pointed by ARCH_SYSROOT_DIR). This
91 # allows to import into the staging directory the C library and
92 # companion libraries for the correct architecture variant. We
93 # explictly only copy etc, lib, sbin and usr since other directories
94 # might exist for other architecture variants (on Codesourcery
95 # toolchain, the sysroot for the default architecture variant contains
96 # the armv4t and thumb2 subdirectories, which are the sysroot for the
97 # corresponding architecture variants), and we don't want to import
98 # them.
100 # Then, we need to support two types of multilib toolchains:
102 # - The toolchains that have nested sysroots: a main sysroot, and
103 # then additional sysroots available as subdirectories of the main
104 # one. This is for example used by Sourcery CodeBench toolchains.
106 # - The toolchains that have side-by-side sysroots. Each sysroot is a
107 # complete one, they simply leave one next to each other. This is
108 # for example used by MIPS Codescape toolchains.
110 # So, we first detect if the selected architecture variant is not the
111 # default one (i.e, if SYSROOT_DIR != ARCH_SYSROOT_DIR).
113 # If we are in the situation of a nested sysroot, we:
115 # * If needed, import the header files from the default architecture
116 # variant. Header files are typically shared between the sysroots
117 # for the different architecture variants. If we use the
118 # non-default one, header files were not copied by the previous
119 # step, so we copy them here from the sysroot of the default
120 # architecture variant.
122 # * Create a symbolic link that matches the name of the subdirectory
123 # for the architecture variant in the original sysroot. This is
124 # required as the compiler will by default look in
125 # sysroot_dir/arch_variant/ for libraries and headers, when the
126 # non-default architecture variant is used. Without this, the
127 # compiler fails to find libraries and headers.
129 # If we are in the situation of a side-by-side sysroot, we:
131 # * Create a symbolic link
133 # Finally, some toolchains (i.e Linaro binary toolchains) store
134 # support libraries (libstdc++, libgcc_s) outside of the sysroot, so
135 # we simply copy all the libraries from the "support lib directory"
136 # into our sysroot.
138 # Note that the 'locale' directories are not copied. They are huge
139 # (400+MB) in CodeSourcery toolchains, and they are not really useful.
141 # $1: main sysroot directory of the toolchain
142 # $2: arch specific sysroot directory of the toolchain
143 # $3: arch specific subdirectory in the sysroot
144 # $4: directory of libraries ('lib', 'lib32' or 'lib64')
145 # $5: support lib directories (for toolchains storing libgcc_s,
146 # libstdc++ and other gcc support libraries outside of the
147 # sysroot)
148 copy_toolchain_sysroot = \
149 SYSROOT_DIR="$(strip $1)"; \
150 ARCH_SYSROOT_DIR="$(strip $2)"; \
151 ARCH_SUBDIR="$(strip $3)"; \
152 ARCH_LIB_DIR="$(strip $4)" ; \
153 SUPPORT_LIB_DIR="$(strip $5)" ; \
154 for i in etc $${ARCH_LIB_DIR} sbin usr usr/$${ARCH_LIB_DIR}; do \
155 if [ -d $${ARCH_SYSROOT_DIR}/$$i ] ; then \
156 rsync -au --chmod=u=rwX,go=rX --exclude 'usr/lib/locale' \
157 --include '/libexec*/' --exclude '/lib*/' \
158 $${ARCH_SYSROOT_DIR}/$$i/ $(STAGING_DIR)/$$i/ ; \
159 fi ; \
160 done ; \
161 SYSROOT_DIR_CANON=`readlink -f $${SYSROOT_DIR}` ; \
162 ARCH_SYSROOT_DIR_CANON=`readlink -f $${ARCH_SYSROOT_DIR}` ; \
163 if [ $${SYSROOT_DIR_CANON} != $${ARCH_SYSROOT_DIR_CANON} ] ; then \
164 relpath="./" ; \
165 if [ $${ARCH_SYSROOT_DIR_CANON:0:$${\#SYSROOT_DIR_CANON}} == $${SYSROOT_DIR_CANON} ] ; then \
166 if [ ! -d $${ARCH_SYSROOT_DIR}/usr/include ] ; then \
167 cp -a $${SYSROOT_DIR}/usr/include $(STAGING_DIR)/usr ; \
168 fi ; \
169 mkdir -p `dirname $(STAGING_DIR)/$${ARCH_SUBDIR}` ; \
170 nbslashs=`printf $${ARCH_SUBDIR} | sed 's%[^/]%%g' | wc -c` ; \
171 for slash in `seq 1 $${nbslashs}` ; do \
172 relpath=$${relpath}"../" ; \
173 done ; \
174 ln -s $${relpath} $(STAGING_DIR)/$${ARCH_SUBDIR} ; \
175 echo "Symlinking $(STAGING_DIR)/$${ARCH_SUBDIR} -> $${relpath}" ; \
176 elif [ `dirname $${ARCH_SYSROOT_DIR_CANON}` == `dirname $${SYSROOT_DIR_CANON}` ] ; then \
177 ln -snf $${relpath} $(STAGING_DIR)/`basename $${ARCH_SYSROOT_DIR_CANON}` ; \
178 echo "Symlinking $(STAGING_DIR)/`basename $${ARCH_SYSROOT_DIR_CANON}` -> $${relpath}" ; \
179 fi ; \
180 fi ; \
181 if test -n "$${SUPPORT_LIB_DIR}" ; then \
182 cp -a $${SUPPORT_LIB_DIR}/* $(STAGING_DIR)/lib/ ; \
183 fi ; \
184 find $(STAGING_DIR) -type d | xargs chmod 755
187 # Check the specified kernel headers version actually matches the
188 # version in the toolchain.
190 # $1: sysroot directory
191 # $2: kernel version string, in the form: X.Y
193 check_kernel_headers_version = \
194 if ! support/scripts/check-kernel-headers.sh $(1) $(2); then \
195 exit 1; \
199 # Check the specific gcc version actually matches the version in the
200 # toolchain
202 # $1: path to gcc
203 # $2: expected gcc version
205 # Some details about the sed expression:
206 # - 1!d
207 # - delete if not line 1
209 # - s/^[^)]+\) ([^[:space:]]+).*/\1/
210 # - eat all until the first ')' character followed by a space
211 # - match as many non-space chars as possible
212 # - eat all the remaining chars on the line
213 # - replace by the matched expression
215 check_gcc_version = \
216 expected_version="$(strip $2)" ; \
217 if [ -z "$${expected_version}" ]; then \
218 printf "Internal error, gcc version unknown (no GCC_AT_LEAST_X_Y selected)\n"; \
219 exit 1 ; \
220 fi; \
221 real_version=`$(1) --version | sed -r -e '1!d; s/^[^)]+\) ([^[:space:]]+).*/\1/;'` ; \
222 if [[ ! "$${real_version}" =~ ^$${expected_version}\. ]] ; then \
223 printf "Incorrect selection of gcc version: expected %s.x, got %s\n" \
224 "$${expected_version}" "$${real_version}" ; \
225 exit 1 ; \
229 # Check the availability of a particular glibc feature. This function
230 # is used to check toolchain options that are always supported by
231 # glibc, so we simply check that the corresponding option is properly
232 # enabled.
234 # $1: Buildroot option name
235 # $2: feature description
237 check_glibc_feature = \
238 if [ "$($(1))" != "y" ] ; then \
239 echo "$(2) available in C library, please enable $(1)" ; \
240 exit 1 ; \
244 # Check the availability of RPC support in a glibc toolchain
246 # $1: sysroot directory
248 check_glibc_rpc_feature = \
249 IS_IN_LIBC=`test -f $(1)/usr/include/rpc/rpc.h && echo y` ; \
250 if [ "$(BR2_TOOLCHAIN_HAS_NATIVE_RPC)" != "y" -a "$${IS_IN_LIBC}" = "y" ] ; then \
251 echo "RPC support available in C library, please enable BR2_TOOLCHAIN_EXTERNAL_INET_RPC" ; \
252 exit 1 ; \
253 fi ; \
254 if [ "$(BR2_TOOLCHAIN_HAS_NATIVE_RPC)" = "y" -a "$${IS_IN_LIBC}" != "y" ] ; then \
255 echo "RPC support not available in C library, please disable BR2_TOOLCHAIN_EXTERNAL_INET_RPC" ; \
256 exit 1 ; \
260 # Check the correctness of a glibc external toolchain configuration.
261 # 1. Check that the C library selected in Buildroot matches the one
262 # of the external toolchain
263 # 2. Check that all the C library-related features are enabled in the
264 # config, since glibc always supports all of them
266 # $1: sysroot directory
268 check_glibc = \
269 SYSROOT_DIR="$(strip $1)"; \
270 if test `find $${SYSROOT_DIR}/ -maxdepth 2 -name 'ld-linux*.so.*' -o -name 'ld.so.*' -o -name 'ld64.so.*' | wc -l` -eq 0 ; then \
271 echo "Incorrect selection of the C library"; \
272 exit -1; \
273 fi; \
274 $(call check_glibc_feature,BR2_USE_MMU,MMU support) ;\
275 $(call check_glibc_rpc_feature,$${SYSROOT_DIR})
278 # Check that the selected C library really is musl
280 # $1: sysroot directory
281 check_musl = \
282 SYSROOT_DIR="$(strip $1)"; \
283 if test ! -f $${SYSROOT_DIR}/lib/libc.so -o -e $${SYSROOT_DIR}/lib/libm.so ; then \
284 echo "Incorrect selection of the C library" ; \
285 exit -1; \
289 # Check the conformity of Buildroot configuration with regard to the
290 # uClibc configuration of the external toolchain, for a particular
291 # feature.
293 # If 'Buildroot option name' ($2) is empty it means the uClibc option
294 # is mandatory.
296 # $1: uClibc macro name
297 # $2: Buildroot option name
298 # $3: uClibc config file
299 # $4: feature description
301 check_uclibc_feature = \
302 IS_IN_LIBC=`grep -q "\#define $(1) 1" $(3) && echo y` ; \
303 if [ -z "$(2)" ] ; then \
304 if [ "$${IS_IN_LIBC}" != "y" ] ; then \
305 echo "$(4) not available in C library, toolchain unsuitable for Buildroot" ; \
306 exit 1 ; \
307 fi ; \
308 else \
309 if [ "$($(2))" != "y" -a "$${IS_IN_LIBC}" = "y" ] ; then \
310 echo "$(4) available in C library, please enable $(2)" ; \
311 exit 1 ; \
312 fi ; \
313 if [ "$($(2))" = "y" -a "$${IS_IN_LIBC}" != "y" ] ; then \
314 echo "$(4) not available in C library, please disable $(2)" ; \
315 exit 1 ; \
316 fi ; \
320 # Check the correctness of a uclibc external toolchain configuration
321 # 1. Check that the C library selected in Buildroot matches the one
322 # of the external toolchain
323 # 2. Check that the features enabled in the Buildroot configuration
324 # match the features available in the uClibc of the external
325 # toolchain
327 # $1: sysroot directory
329 check_uclibc = \
330 SYSROOT_DIR="$(strip $1)"; \
331 if ! test -f $${SYSROOT_DIR}/usr/include/bits/uClibc_config.h ; then \
332 echo "Incorrect selection of the C library"; \
333 exit -1; \
334 fi; \
335 UCLIBC_CONFIG_FILE=$${SYSROOT_DIR}/usr/include/bits/uClibc_config.h ; \
336 $(call check_uclibc_feature,__ARCH_USE_MMU__,BR2_USE_MMU,$${UCLIBC_CONFIG_FILE},MMU support) ;\
337 $(call check_uclibc_feature,__UCLIBC_HAS_LFS__,,$${UCLIBC_CONFIG_FILE},Large file support) ;\
338 $(call check_uclibc_feature,__UCLIBC_HAS_IPV6__,,$${UCLIBC_CONFIG_FILE},IPv6 support) ;\
339 $(call check_uclibc_feature,__UCLIBC_HAS_RPC__,BR2_TOOLCHAIN_HAS_NATIVE_RPC,$${UCLIBC_CONFIG_FILE},RPC support) ;\
340 $(call check_uclibc_feature,__UCLIBC_HAS_LOCALE__,BR2_ENABLE_LOCALE,$${UCLIBC_CONFIG_FILE},Locale support) ;\
341 $(call check_uclibc_feature,__UCLIBC_HAS_WCHAR__,BR2_USE_WCHAR,$${UCLIBC_CONFIG_FILE},Wide char support) ;\
342 $(call check_uclibc_feature,__UCLIBC_HAS_THREADS__,BR2_TOOLCHAIN_HAS_THREADS,$${UCLIBC_CONFIG_FILE},Thread support) ;\
343 $(call check_uclibc_feature,__PTHREADS_DEBUG_SUPPORT__,BR2_TOOLCHAIN_HAS_THREADS_DEBUG,$${UCLIBC_CONFIG_FILE},Thread debugging support) ;\
344 $(call check_uclibc_feature,__UCLIBC_HAS_THREADS_NATIVE__,BR2_TOOLCHAIN_HAS_THREADS_NPTL,$${UCLIBC_CONFIG_FILE},NPTL thread support) ;\
345 $(call check_uclibc_feature,__UCLIBC_HAS_SSP__,BR2_TOOLCHAIN_HAS_SSP,$${UCLIBC_CONFIG_FILE},Stack Smashing Protection support)
348 # Check that the Buildroot configuration of the ABI matches the
349 # configuration of the external toolchain.
351 # $1: cross-gcc path
352 # $2: cross-readelf path
354 check_arm_abi = \
355 __CROSS_CC=$(strip $1) ; \
356 __CROSS_READELF=$(strip $2) ; \
357 EXT_TOOLCHAIN_TARGET=`LANG=C $${__CROSS_CC} -v 2>&1 | grep ^Target | cut -f2 -d ' '` ; \
358 if ! echo $${EXT_TOOLCHAIN_TARGET} | grep -qE 'eabi(hf)?$$' ; then \
359 echo "External toolchain uses the unsuported OABI" ; \
360 exit 1 ; \
361 fi ; \
362 if ! echo 'int main(void) {}' | $${__CROSS_CC} -x c -o $(BUILD_DIR)/.br-toolchain-test.tmp - ; then \
363 rm -f $(BUILD_DIR)/.br-toolchain-test.tmp*; \
364 abistr_$(BR2_ARM_EABI)='EABI'; \
365 abistr_$(BR2_ARM_EABIHF)='EABIhf'; \
366 echo "Incorrect ABI setting: $${abistr_y} selected, but toolchain is incompatible"; \
367 exit 1 ; \
368 fi ; \
369 rm -f $(BUILD_DIR)/.br-toolchain-test.tmp*
372 # Check that the external toolchain supports C++
374 # $1: cross-g++ path
376 check_cplusplus = \
377 __CROSS_CXX=$(strip $1) ; \
378 $${__CROSS_CXX} -v > /dev/null 2>&1 ; \
379 if test $$? -ne 0 ; then \
380 echo "C++ support is selected but is not available in external toolchain" ; \
381 exit 1 ; \
385 # Check that the cross-compiler given in the configuration exists
387 # $1: cross-gcc path
389 check_cross_compiler_exists = \
390 __CROSS_CC=$(strip $1) ; \
391 $${__CROSS_CC} -v > /dev/null 2>&1 ; \
392 if test $$? -ne 0 ; then \
393 echo "Cannot execute cross-compiler '$${__CROSS_CC}'" ; \
394 exit 1 ; \
398 # Check for toolchains known not to work with Buildroot. For now, we
399 # only check for Angstrom toolchains, by looking at the vendor part of
400 # the host tuple.
402 # $1: cross-gcc path
404 check_unusable_toolchain = \
405 __CROSS_CC=$(strip $1) ; \
406 vendor=`$${__CROSS_CC} -dumpmachine | cut -f2 -d'-'` ; \
407 if test "$${vendor}" = "angstrom" ; then \
408 echo "Angstrom toolchains are not pure toolchains: they contain" ; \
409 echo "many other libraries than just the C library, which makes" ; \
410 echo "them unsuitable as external toolchains for build systems" ; \
411 echo "such as Buildroot." ; \
412 exit 1 ; \
413 fi; \
414 with_sysroot=`$${__CROSS_CC} -v 2>&1 |sed -r -e '/.* --with-sysroot=([^[:space:]]+)[[:space:]].*/!d; s//\1/'`; \
415 if test "$${with_sysroot}" = "/" ; then \
416 echo "Distribution toolchains are unsuitable for use by Buildroot," ; \
417 echo "as they were configured in a way that makes them non-relocatable,"; \
418 echo "and contain a lot of pre-built libraries that would conflict with"; \
419 echo "the ones Buildroot wants to build."; \
420 exit 1; \
424 # Generate gdbinit file for use with Buildroot
426 gen_gdbinit_file = \
427 mkdir -p $(STAGING_DIR)/usr/share/buildroot/ ; \
428 echo "set sysroot $(STAGING_DIR)" > $(STAGING_DIR)/usr/share/buildroot/gdbinit