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 # $1: library name pattern (can include glob wildcards)
11 copy_toolchain_lib_root = \
12 LIBPATTERN="$(strip $1)"; \
13 LIBPATHS=`find $(STAGING_DIR)/ -name "$${LIBPATTERN}" 2>/dev/null` ; \
14 for LIBPATH in $${LIBPATHS} ; do \
16 LIBNAME=`basename $${LIBPATH}`; \
17 DESTDIR=`echo $${LIBPATH} | sed "s,^$(STAGING_DIR)/,," | xargs dirname` ; \
18 mkdir -p $(TARGET_DIR)/$${DESTDIR}; \
19 rm -fr $(TARGET_DIR)/$${DESTDIR}/$${LIBNAME}; \
20 if test -h $${LIBPATH} ; then \
21 cp -d $${LIBPATH} $(TARGET_DIR)/$${DESTDIR}/$${LIBNAME}; \
22 LIBPATH="`readlink -f $${LIBPATH}`"; \
23 elif test -f $${LIBPATH}; then \
24 $(INSTALL) -D -m0755 $${LIBPATH} $(TARGET_DIR)/$${DESTDIR}/$${LIBNAME}; \
33 # Copy the full external toolchain sysroot directory to the staging
34 # dir. The operation of this function is rendered a little bit
35 # complicated by the support for multilib toolchains.
37 # We start by copying etc, 'lib', sbin, usr and usr/'lib' from the
38 # sysroot of the selected architecture variant (as pointed to by
39 # ARCH_SYSROOT_DIR). This allows to import into the staging directory
40 # the C library and companion libraries for the correct architecture
41 # variant. 'lib' may not be literally 'lib' but could be something else,
42 # e.g. lib32-fp (as determined by ARCH_LIB_DIR) and we only want to copy
43 # that lib directory and no other. When copying usr, we therefore need
44 # to be extra careful not to include usr/lib* but we _do_ want to
45 # include usr/libexec.
46 # We are selective in the directories we copy since other directories
47 # might exist for other architecture variants (on Codesourcery
48 # toolchain, the sysroot for the default architecture variant contains
49 # the armv4t and thumb2 subdirectories, which are the sysroot for the
50 # corresponding architecture variants), and we don't want to import
53 # If ARCH_LIB_DIR is not a singular directory component, e.g.
54 # 'lib32/octeon2', then symbolic links in ARCH_LIB_DIR and
55 # usr/ARCH_LIB_DIR may be broken because Buildroot will flatten the
56 # directory structure (e.g. lib32/octeon2/foo is actually stored in
57 # lib/foo). This is only relevant for links that contain one or more ../
58 # components, as links to the current directory are always fine.
59 # We need to fix the broken links by removing the right amount of ../
60 # dots from the link destination.
61 # Once the link destination is valid again, it can be simplified to
62 # remove the dependency on intermediate directory symlinks.
64 # It is possible that ARCH_LIB_DIR does not contain the dynamic loader
65 # (ld*.so or similar) because it (or the main symlink to it) normally
66 # resides in /lib while ARCH_LIB_DIR may be something else (e.g. lib64,
67 # lib/<tuple>, ...). Therefore, copy the dynamic loader separately.
69 # Then, if the selected architecture variant is not the default one
70 # (i.e, if SYSROOT_DIR != ARCH_SYSROOT_DIR), then we :
72 # * Import the header files from the default architecture
73 # variant. Header files are typically shared between the sysroots
74 # for the different architecture variants. If we use the
75 # non-default one, header files were not copied by the previous
76 # step, so we copy them here from the sysroot of the default
77 # architecture variant.
79 # * Create a symbolic link that matches the name of the subdirectory
80 # for the architecture variant in the original sysroot. This is
81 # required as the compiler will by default look in
82 # sysroot_dir/arch_variant/ for libraries and headers, when the
83 # non-default architecture variant is used. Without this, the
84 # compiler fails to find libraries and headers.
86 # Some toolchains (i.e Linaro binary toolchains) store support
87 # libraries (libstdc++, libgcc_s) outside of the sysroot, so we simply
88 # copy all the libraries from the "support lib directory" into our
91 # Note that the 'locale' directories are not copied. They are huge
92 # (400+MB) in CodeSourcery toolchains, and they are not really useful.
94 # $1: main sysroot directory of the toolchain
95 # $2: arch specific sysroot directory of the toolchain
96 # $3: arch specific subdirectory in the sysroot
97 # $4: directory of libraries ('lib', 'lib32' or 'lib64')
98 # $5: support lib directories (for toolchains storing libgcc_s,
99 # libstdc++ and other gcc support libraries outside of the
101 copy_toolchain_sysroot = \
102 SYSROOT_DIR="$(strip $1)"; \
103 ARCH_SYSROOT_DIR="$(strip $2)"; \
104 ARCH_SUBDIR="$(strip $3)"; \
105 ARCH_LIB_DIR="$(strip $4)" ; \
106 SUPPORT_LIB_DIR="$(strip $5)" ; \
107 for i in etc $${ARCH_LIB_DIR} sbin usr usr/$${ARCH_LIB_DIR}; do \
108 if [ ! -d $${ARCH_SYSROOT_DIR}/$$i ] ; then \
111 if [ "$$i" = "usr" ]; then \
112 rsync -au --chmod=u=rwX,go=rX --exclude 'locale/' \
113 --include '/libexec*/' --exclude '/lib*/' \
114 $${ARCH_SYSROOT_DIR}/$$i/ $(STAGING_DIR)/$$i/ ; \
116 rsync -au --chmod=u=rwX,go=rX --exclude 'locale/' \
117 $${ARCH_SYSROOT_DIR}/$$i/ $(STAGING_DIR)/$$i/ ; \
120 for link in $$(find $(STAGING_DIR) -type l); do \
121 target=$$(readlink $${link}) ; \
122 if [ "$${target}" == "$${target\#/}" ] ; then \
125 relpath="$(call relpath_prefix,$${target\#/})" ; \
126 echo "Fixing symlink $${link} from $${target} to $${relpath}$${target\#/}" ; \
127 ln -sf $${relpath}$${target\#/} $${link} ; \
129 relpath="$(call relpath_prefix,$${ARCH_LIB_DIR})" ; \
130 if [ "$${relpath}" != "" ]; then \
131 for i in $$(find -H $(STAGING_DIR)/$${ARCH_LIB_DIR} $(STAGING_DIR)/usr/$${ARCH_LIB_DIR} -type l -xtype l); do \
132 LINKTARGET=`readlink $$i` ; \
133 NEWLINKTARGET=$${LINKTARGET\#$$relpath} ; \
134 ln -sf $${NEWLINKTARGET} $$i ; \
135 $(call simplify_symlink,$$i,$(STAGING_DIR)) ; \
138 if [ ! -e $(STAGING_DIR)/lib/ld*.so.* ]; then \
139 if [ -e $${ARCH_SYSROOT_DIR}/lib/ld*.so.* ]; then \
140 cp -a $${ARCH_SYSROOT_DIR}/lib/ld*.so.* $(STAGING_DIR)/lib/ ; \
143 if [ `readlink -f $${SYSROOT_DIR}` != `readlink -f $${ARCH_SYSROOT_DIR}` ] ; then \
144 if [ ! -d $${ARCH_SYSROOT_DIR}/usr/include ] ; then \
145 cp -a $${SYSROOT_DIR}/usr/include $(STAGING_DIR)/usr ; \
147 mkdir -p `dirname $(STAGING_DIR)/$${ARCH_SUBDIR}` ; \
148 relpath="$(call relpath_prefix,$${ARCH_SUBDIR})./" ; \
149 ln -s $${relpath} $(STAGING_DIR)/$${ARCH_SUBDIR} ; \
150 echo "Symlinking $(STAGING_DIR)/$${ARCH_SUBDIR} -> $${relpath}" ; \
152 if test -n "$${SUPPORT_LIB_DIR}" ; then \
153 cp -a $${SUPPORT_LIB_DIR}/* $(STAGING_DIR)/lib/ ; \
155 find $(STAGING_DIR) -type d | xargs chmod 755
158 # Check the specified kernel headers version actually matches the
159 # version in the toolchain.
161 # $1: sysroot directory
162 # $2: kernel version string, in the form: X.Y
164 check_kernel_headers_version = \
165 if ! support/scripts/check-kernel-headers.sh $(1) $(2); then \
170 # Check the specific gcc version actually matches the version in the
174 # $2: expected gcc version
176 check_gcc_version = \
177 expected_version="$(strip $2)" ; \
178 if [ -z "$${expected_version}" ]; then \
181 real_version=`$(1) -dumpversion` ; \
182 if [[ ! "$${real_version}" =~ ^$${expected_version}\. ]] ; then \
183 printf "Incorrect selection of gcc version: expected %s.x, got %s\n" \
184 "$${expected_version}" "$${real_version}" ; \
189 # Check the availability of a particular glibc feature. This function
190 # is used to check toolchain options that are always supported by
191 # glibc, so we simply check that the corresponding option is properly
194 # $1: Buildroot option name
195 # $2: feature description
197 check_glibc_feature = \
198 if [ "$($(1))" != "y" ] ; then \
199 echo "$(2) available in C library, please enable $(1)" ; \
204 # Check the availability of RPC support in a glibc toolchain
206 # $1: sysroot directory
208 check_glibc_rpc_feature = \
209 IS_IN_LIBC=`test -f $(1)/usr/include/rpc/rpc.h && echo y` ; \
210 if [ "$(BR2_TOOLCHAIN_HAS_NATIVE_RPC)" != "y" -a "$${IS_IN_LIBC}" = "y" ] ; then \
211 echo "RPC support available in C library, please enable BR2_TOOLCHAIN_EXTERNAL_INET_RPC" ; \
214 if [ "$(BR2_TOOLCHAIN_HAS_NATIVE_RPC)" = "y" -a "$${IS_IN_LIBC}" != "y" ] ; then \
215 echo "RPC support not available in C library, please disable BR2_TOOLCHAIN_EXTERNAL_INET_RPC" ; \
220 # Check the correctness of a glibc external toolchain configuration.
221 # 1. Check that the C library selected in Buildroot matches the one
222 # of the external toolchain
223 # 2. Check that all the C library-related features are enabled in the
224 # config, since glibc always supports all of them
226 # $1: sysroot directory
229 SYSROOT_DIR="$(strip $1)"; \
230 if test `find -L $${SYSROOT_DIR}/ -maxdepth 2 -name 'ld-linux*.so.*' -o -name 'ld.so.*' -o -name 'ld64.so.*' | wc -l` -eq 0 ; then \
231 echo "Incorrect selection of the C library"; \
234 $(call check_glibc_feature,BR2_USE_MMU,MMU support) ;\
235 $(call check_glibc_rpc_feature,$${SYSROOT_DIR})
238 # Check that the selected C library really is musl
241 # $2: cross-readelf path
243 __CROSS_CC=$(strip $1) ; \
244 __CROSS_READELF=$(strip $2) ; \
245 echo 'void main(void) {}' | $${__CROSS_CC} -x c -o $(BUILD_DIR)/.br-toolchain-test.tmp - >/dev/null 2>&1; \
246 if ! $${__CROSS_READELF} -l $(BUILD_DIR)/.br-toolchain-test.tmp 2> /dev/null | grep 'program interpreter: /lib/ld-musl' -q; then \
247 rm -f $(BUILD_DIR)/.br-toolchain-test.tmp*; \
248 echo "Incorrect selection of the C library" ; \
251 rm -f $(BUILD_DIR)/.br-toolchain-test.tmp*
254 # Check the conformity of Buildroot configuration with regard to the
255 # uClibc configuration of the external toolchain, for a particular
258 # If 'Buildroot option name' ($2) is empty it means the uClibc option
261 # $1: uClibc macro name
262 # $2: Buildroot option name
263 # $3: uClibc config file
264 # $4: feature description
266 check_uclibc_feature = \
267 IS_IN_LIBC=`grep -q "\#define $(1) 1" $(3) && echo y` ; \
268 if [ -z "$(2)" ] ; then \
269 if [ "$${IS_IN_LIBC}" != "y" ] ; then \
270 echo "$(4) not available in C library, toolchain unsuitable for Buildroot" ; \
274 if [ "$($(2))" != "y" -a "$${IS_IN_LIBC}" = "y" ] ; then \
275 echo "$(4) available in C library, please enable $(2)" ; \
278 if [ "$($(2))" = "y" -a "$${IS_IN_LIBC}" != "y" ] ; then \
279 echo "$(4) not available in C library, please disable $(2)" ; \
285 # Check the correctness of a uclibc external toolchain configuration
286 # 1. Check that the C library selected in Buildroot matches the one
287 # of the external toolchain
288 # 2. Check that the features enabled in the Buildroot configuration
289 # match the features available in the uClibc of the external
292 # $1: sysroot directory
295 SYSROOT_DIR="$(strip $1)"; \
296 if ! test -f $${SYSROOT_DIR}/usr/include/bits/uClibc_config.h ; then \
297 echo "Incorrect selection of the C library"; \
300 UCLIBC_CONFIG_FILE=$${SYSROOT_DIR}/usr/include/bits/uClibc_config.h ; \
301 $(call check_uclibc_feature,__ARCH_USE_MMU__,BR2_USE_MMU,$${UCLIBC_CONFIG_FILE},MMU support) ;\
302 $(call check_uclibc_feature,__UCLIBC_HAS_LFS__,,$${UCLIBC_CONFIG_FILE},Large file support) ;\
303 $(call check_uclibc_feature,__UCLIBC_HAS_IPV6__,,$${UCLIBC_CONFIG_FILE},IPv6 support) ;\
304 $(call check_uclibc_feature,__UCLIBC_HAS_RPC__,BR2_TOOLCHAIN_HAS_NATIVE_RPC,$${UCLIBC_CONFIG_FILE},RPC support) ;\
305 $(call check_uclibc_feature,__UCLIBC_HAS_LOCALE__,BR2_ENABLE_LOCALE,$${UCLIBC_CONFIG_FILE},Locale support) ;\
306 $(call check_uclibc_feature,__UCLIBC_HAS_WCHAR__,BR2_USE_WCHAR,$${UCLIBC_CONFIG_FILE},Wide char support) ;\
307 $(call check_uclibc_feature,__UCLIBC_HAS_THREADS__,BR2_TOOLCHAIN_HAS_THREADS,$${UCLIBC_CONFIG_FILE},Thread support) ;\
308 $(call check_uclibc_feature,__PTHREADS_DEBUG_SUPPORT__,BR2_TOOLCHAIN_HAS_THREADS_DEBUG,$${UCLIBC_CONFIG_FILE},Thread debugging support) ;\
309 $(call check_uclibc_feature,__UCLIBC_HAS_THREADS_NATIVE__,BR2_TOOLCHAIN_HAS_THREADS_NPTL,$${UCLIBC_CONFIG_FILE},NPTL thread support)
312 # Check that the Buildroot configuration of the ABI matches the
313 # configuration of the external toolchain.
316 # $2: cross-readelf path
319 __CROSS_CC=$(strip $1) ; \
320 EXT_TOOLCHAIN_TARGET=`LANG=C $${__CROSS_CC} -v 2>&1 | grep ^Target | cut -f2 -d ' '` ; \
321 if ! echo $${EXT_TOOLCHAIN_TARGET} | grep -qE 'eabi(hf)?$$' ; then \
322 echo "External toolchain uses the unsuported OABI" ; \
325 if ! echo 'int main(void) {}' | $${__CROSS_CC} -x c -o $(BUILD_DIR)/.br-toolchain-test.tmp - ; then \
326 rm -f $(BUILD_DIR)/.br-toolchain-test.tmp*; \
327 abistr_$(BR2_ARM_EABI)='EABI'; \
328 abistr_$(BR2_ARM_EABIHF)='EABIhf'; \
329 echo "Incorrect ABI setting: $${abistr_y} selected, but toolchain is incompatible"; \
332 rm -f $(BUILD_DIR)/.br-toolchain-test.tmp*
335 # Check that the external toolchain supports C++
340 __CROSS_CXX=$(strip $1) ; \
341 $${__CROSS_CXX} -v > /dev/null 2>&1 ; \
342 if test $$? -ne 0 ; then \
343 echo "C++ support is selected but is not available in external toolchain" ; \
349 # Check that the external toolchain supports Fortran
351 # $1: cross-gfortran path
354 __CROSS_FC=$(strip $1) ; \
355 __o=$(BUILD_DIR)/.br-toolchain-test-fortran.tmp ; \
356 printf 'program hello\n\tprint *, "Hello Fortran!\\n"\nend program hello\n' | \
357 $${__CROSS_FC} -x f95 -o $${__o} - ; \
358 if test $$? -ne 0 ; then \
360 echo "Fortran support is selected but is not available in external toolchain" ; \
366 # Check that the cross-compiler given in the configuration exists
370 check_cross_compiler_exists = \
371 __CROSS_CC=$(strip $1) ; \
372 $${__CROSS_CC} -v > /dev/null 2>&1 ; \
373 if test $$? -ne 0 ; then \
374 echo "Cannot execute cross-compiler '$${__CROSS_CC}'" ; \
379 # Check for toolchains known not to work with Buildroot.
380 # - For the Angstrom toolchains, we check by looking at the vendor part of
382 # - Exclude distro-class toolchains which are not relocatable.
383 # - Exclude broken toolchains which return "libc.a" with -print-file-name.
384 # - Exclude toolchains which doesn't support --sysroot option.
388 check_unusable_toolchain = \
389 __CROSS_CC=$(strip $1) ; \
390 vendor=`$${__CROSS_CC} -dumpmachine | cut -f2 -d'-'` ; \
391 if test "$${vendor}" = "angstrom" ; then \
392 echo "Angstrom toolchains are not pure toolchains: they contain" ; \
393 echo "many other libraries than just the C library, which makes" ; \
394 echo "them unsuitable as external toolchains for build systems" ; \
395 echo "such as Buildroot." ; \
398 with_sysroot=`$${__CROSS_CC} -v 2>&1 |sed -r -e '/.* --with-sysroot=([^[:space:]]+)[[:space:]].*/!d; s//\1/'`; \
399 if test "$${with_sysroot}" = "/" ; then \
400 echo "Distribution toolchains are unsuitable for use by Buildroot," ; \
401 echo "as they were configured in a way that makes them non-relocatable,"; \
402 echo "and contain a lot of pre-built libraries that would conflict with"; \
403 echo "the ones Buildroot wants to build."; \
406 libc_a_path=`$${__CROSS_CC} -print-file-name=libc.a` ; \
407 if test "$${libc_a_path}" = "libc.a" ; then \
408 echo "Unable to detect the toolchain sysroot, Buildroot cannot use this toolchain." ; \
411 sysroot_dir="$(call toolchain_find_sysroot,$${__CROSS_CC})" ; \
412 if test -z "$${sysroot_dir}" ; then \
413 echo "External toolchain doesn't support --sysroot. Cannot use." ; \
418 # Check if the toolchain has SSP (stack smashing protector) support
422 check_toolchain_ssp = \
423 __CROSS_CC=$(strip $1) ; \
424 __HAS_SSP=`echo 'void main(){}' | $${__CROSS_CC} -Werror -fstack-protector -x c - -o $(BUILD_DIR)/.br-toolchain-test.tmp >/dev/null 2>&1 && echo y` ; \
425 if [ "$(BR2_TOOLCHAIN_HAS_SSP)" != "y" -a "$${__HAS_SSP}" = "y" ] ; then \
426 echo "SSP support available in this toolchain, please enable BR2_TOOLCHAIN_EXTERNAL_HAS_SSP" ; \
429 if [ "$(BR2_TOOLCHAIN_HAS_SSP)" = "y" -a "$${__HAS_SSP}" != "y" ] ; then \
430 echo "SSP support not available in this toolchain, please disable BR2_TOOLCHAIN_EXTERNAL_HAS_SSP" ; \
433 rm -f $(BUILD_DIR)/.br-toolchain-test.tmp*
436 # Generate gdbinit file for use with Buildroot
439 mkdir -p $(STAGING_DIR)/usr/share/buildroot/ ; \
440 echo "set sysroot $(STAGING_DIR)" > $(STAGING_DIR)/usr/share/buildroot/gdbinit
442 # Given a path, determine the relative prefix (../) needed to return to the
443 # root level. Note that the last component is treated as a file component; use a
444 # trailing slash to force treating it as a directory. Examples:
445 # relpath_prefix(lib32) = ""
446 # relpath_prefix(lib32/octeon2) = "../"
447 # relpath_prefix(lib32/octeon2/) = "../../"
450 define relpath_prefix
453 nbslashs=`printf $1 | sed 's%[^/]%%g' | wc -c` ; \
454 for slash in `seq 1 $${nbslashs}` ; do \
455 prefix=$${prefix}"../" ; \
457 printf "$$prefix" ; \
461 # Replace the destination of a symbolic link with a simpler version
463 # usr/lib/libfoo.so -> ../../lib32/libfoo.so.1
465 # usr/lib/libfoo.so -> ../../lib/libfoo.so.1
466 # since 'lib32' is a symlink to 'lib'.
469 # usr/lib/octeon2/libbar.so -> ../../../lib32/octeon2/libbar.so.1
471 # usr/lib/octeon2/libbar.so -> ../../lib/libbar.so.1
472 # assuming lib32->lib and lib/octeon2->lib.
476 define simplify_symlink
478 FULL_SRC="$$(readlink -f $$(dirname $1))/$$(basename $1)" ; \
479 FULL_DEST="$$(readlink -f $1)" ; \
480 FULL_BASE="$$(readlink -f $2)" ; \
481 REL_SRC="$${FULL_SRC#$${FULL_BASE}/}" ; \
482 REL_DEST="$${FULL_DEST#$${FULL_BASE}/}" ; \
483 DOTS="$(call relpath_prefix,$${REL_SRC})" ; \
484 ln -sf "$${DOTS}$${REL_DEST}" "$${FULL_SRC}" ; \