linux: provide symlink dtc->linux-dtc is there is no dtc yet
[buildroot-gz.git] / toolchain / helpers.mk
blobd28a2caed3ea848edb2e3fc6dbbf75077e2fabf4
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
11 copy_toolchain_lib_root = \
12 LIB="$(strip $1)"; \
14 LIBPATHS=`find -L $(STAGING_DIR) -name "$${LIB}" 2>/dev/null` ; \
15 for LIBPATH in $${LIBPATHS} ; do \
16 DESTDIR=`echo $${LIBPATH} | sed "s,^$(STAGING_DIR)/,," | xargs dirname` ; \
17 mkdir -p $(TARGET_DIR)/$${DESTDIR}; \
18 while true ; do \
19 LIBNAME=`basename $${LIBPATH}`; \
20 LIBDIR=`dirname $${LIBPATH}` ; \
21 LINKTARGET=`readlink $${LIBPATH}` ; \
22 rm -fr $(TARGET_DIR)/$${DESTDIR}/$${LIBNAME}; \
23 if test -h $${LIBPATH} ; then \
24 ln -sf `basename $${LINKTARGET}` $(TARGET_DIR)/$${DESTDIR}/$${LIBNAME} ; \
25 elif test -f $${LIBPATH}; then \
26 $(INSTALL) -D -m0755 $${LIBPATH} $(TARGET_DIR)/$${DESTDIR}/$${LIBNAME}; \
27 else \
28 exit -1; \
29 fi; \
30 if test -z "$${LINKTARGET}" ; then \
31 break ; \
32 fi ; \
33 LIBPATH="`readlink -f $${LIBPATH}`"; \
34 done; \
35 done
38 # Copy the full external toolchain sysroot directory to the staging
39 # dir. The operation of this function is rendered a little bit
40 # complicated by the support for multilib toolchains.
42 # We start by copying etc, lib, sbin and usr from the sysroot of the
43 # selected architecture variant (as pointed by ARCH_SYSROOT_DIR). This
44 # allows to import into the staging directory the C library and
45 # companion libraries for the correct architecture variant. We
46 # explictly only copy etc, lib, sbin and usr 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
51 # them.
53 # Then, we need to support two types of multilib toolchains:
55 # - The toolchains that have nested sysroots: a main sysroot, and
56 # then additional sysroots available as subdirectories of the main
57 # one. This is for example used by Sourcery CodeBench toolchains.
59 # - The toolchains that have side-by-side sysroots. Each sysroot is a
60 # complete one, they simply leave one next to each other. This is
61 # for example used by MIPS Codescape toolchains.
63 # So, we first detect if the selected architecture variant is not the
64 # default one (i.e, if SYSROOT_DIR != ARCH_SYSROOT_DIR).
66 # If we are in the situation of a nested sysroot, we:
68 # * If needed, import the header files from the default architecture
69 # variant. Header files are typically shared between the sysroots
70 # for the different architecture variants. If we use the
71 # non-default one, header files were not copied by the previous
72 # step, so we copy them here from the sysroot of the default
73 # architecture variant.
75 # * Create a symbolic link that matches the name of the subdirectory
76 # for the architecture variant in the original sysroot. This is
77 # required as the compiler will by default look in
78 # sysroot_dir/arch_variant/ for libraries and headers, when the
79 # non-default architecture variant is used. Without this, the
80 # compiler fails to find libraries and headers.
82 # If we are in the situation of a side-by-side sysroot, we:
84 # * Create a symbolic link
86 # Finally, some toolchains (i.e Linaro binary toolchains) store
87 # support libraries (libstdc++, libgcc_s) outside of the sysroot, so
88 # we simply copy all the libraries from the "support lib directory"
89 # into our sysroot.
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
100 # sysroot)
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 \
109 rsync -au --chmod=u=rwX,go=rX --exclude 'usr/lib/locale' \
110 --include '/libexec*/' --exclude '/lib*/' \
111 $${ARCH_SYSROOT_DIR}/$$i/ $(STAGING_DIR)/$$i/ ; \
112 fi ; \
113 done ; \
114 SYSROOT_DIR_CANON=`readlink -f $${SYSROOT_DIR}` ; \
115 ARCH_SYSROOT_DIR_CANON=`readlink -f $${ARCH_SYSROOT_DIR}` ; \
116 if [ $${SYSROOT_DIR_CANON} != $${ARCH_SYSROOT_DIR_CANON} ] ; then \
117 relpath="./" ; \
118 if [ $${ARCH_SYSROOT_DIR_CANON:0:$${\#SYSROOT_DIR_CANON}} == $${SYSROOT_DIR_CANON} ] ; then \
119 if [ ! -d $${ARCH_SYSROOT_DIR}/usr/include ] ; then \
120 cp -a $${SYSROOT_DIR}/usr/include $(STAGING_DIR)/usr ; \
121 fi ; \
122 mkdir -p `dirname $(STAGING_DIR)/$${ARCH_SUBDIR}` ; \
123 nbslashs=`printf $${ARCH_SUBDIR} | sed 's%[^/]%%g' | wc -c` ; \
124 for slash in `seq 1 $${nbslashs}` ; do \
125 relpath=$${relpath}"../" ; \
126 done ; \
127 ln -s $${relpath} $(STAGING_DIR)/$${ARCH_SUBDIR} ; \
128 echo "Symlinking $(STAGING_DIR)/$${ARCH_SUBDIR} -> $${relpath}" ; \
129 elif [ `dirname $${ARCH_SYSROOT_DIR_CANON}` == `dirname $${SYSROOT_DIR_CANON}` ] ; then \
130 ln -snf $${relpath} $(STAGING_DIR)/`basename $${ARCH_SYSROOT_DIR_CANON}` ; \
131 echo "Symlinking $(STAGING_DIR)/`basename $${ARCH_SYSROOT_DIR_CANON}` -> $${relpath}" ; \
132 fi ; \
133 fi ; \
134 if test -n "$${SUPPORT_LIB_DIR}" ; then \
135 cp -a $${SUPPORT_LIB_DIR}/* $(STAGING_DIR)/lib/ ; \
136 fi ; \
137 find $(STAGING_DIR) -type d | xargs chmod 755
140 # Check the specified kernel headers version actually matches the
141 # version in the toolchain.
143 # $1: sysroot directory
144 # $2: kernel version string, in the form: X.Y
146 check_kernel_headers_version = \
147 if ! support/scripts/check-kernel-headers.sh $(1) $(2); then \
148 exit 1; \
152 # Check the specific gcc version actually matches the version in the
153 # toolchain
155 # $1: path to gcc
156 # $2: expected gcc version
158 # Some details about the sed expression:
159 # - 1!d
160 # - delete if not line 1
162 # - s/^[^)]+\) ([^[:space:]]+).*/\1/
163 # - eat all until the first ')' character followed by a space
164 # - match as many non-space chars as possible
165 # - eat all the remaining chars on the line
166 # - replace by the matched expression
168 check_gcc_version = \
169 expected_version="$(strip $2)" ; \
170 if [ -z "$${expected_version}" ]; then \
171 printf "Internal error, gcc version unknown (no GCC_AT_LEAST_X_Y selected)\n"; \
172 exit 1 ; \
173 fi; \
174 real_version=`$(1) --version | sed -r -e '1!d; s/^[^)]+\) ([^[:space:]]+).*/\1/;'` ; \
175 if [[ ! "$${real_version}" =~ ^$${expected_version}\. ]] ; then \
176 printf "Incorrect selection of gcc version: expected %s.x, got %s\n" \
177 "$${expected_version}" "$${real_version}" ; \
178 exit 1 ; \
182 # Check the availability of a particular glibc feature. This function
183 # is used to check toolchain options that are always supported by
184 # glibc, so we simply check that the corresponding option is properly
185 # enabled.
187 # $1: Buildroot option name
188 # $2: feature description
190 check_glibc_feature = \
191 if [ "$($(1))" != "y" ] ; then \
192 echo "$(2) available in C library, please enable $(1)" ; \
193 exit 1 ; \
197 # Check the availability of RPC support in a glibc toolchain
199 # $1: sysroot directory
201 check_glibc_rpc_feature = \
202 IS_IN_LIBC=`test -f $(1)/usr/include/rpc/rpc.h && echo y` ; \
203 if [ "$(BR2_TOOLCHAIN_HAS_NATIVE_RPC)" != "y" -a "$${IS_IN_LIBC}" = "y" ] ; then \
204 echo "RPC support available in C library, please enable BR2_TOOLCHAIN_EXTERNAL_INET_RPC" ; \
205 exit 1 ; \
206 fi ; \
207 if [ "$(BR2_TOOLCHAIN_HAS_NATIVE_RPC)" = "y" -a "$${IS_IN_LIBC}" != "y" ] ; then \
208 echo "RPC support not available in C library, please disable BR2_TOOLCHAIN_EXTERNAL_INET_RPC" ; \
209 exit 1 ; \
213 # Check the correctness of a glibc external toolchain configuration.
214 # 1. Check that the C library selected in Buildroot matches the one
215 # of the external toolchain
216 # 2. Check that all the C library-related features are enabled in the
217 # config, since glibc always supports all of them
219 # $1: sysroot directory
221 check_glibc = \
222 SYSROOT_DIR="$(strip $1)"; \
223 if test `find $${SYSROOT_DIR}/ -maxdepth 2 -name 'ld-linux*.so.*' -o -name 'ld.so.*' -o -name 'ld64.so.*' | wc -l` -eq 0 ; then \
224 echo "Incorrect selection of the C library"; \
225 exit -1; \
226 fi; \
227 $(call check_glibc_feature,BR2_USE_MMU,MMU support) ;\
228 $(call check_glibc_rpc_feature,$${SYSROOT_DIR})
231 # Check that the selected C library really is musl
233 # $1: sysroot directory
234 check_musl = \
235 SYSROOT_DIR="$(strip $1)"; \
236 if test ! -f $${SYSROOT_DIR}/lib/libc.so -o -e $${SYSROOT_DIR}/lib/libm.so ; then \
237 echo "Incorrect selection of the C library" ; \
238 exit -1; \
242 # Check the conformity of Buildroot configuration with regard to the
243 # uClibc configuration of the external toolchain, for a particular
244 # feature.
246 # If 'Buildroot option name' ($2) is empty it means the uClibc option
247 # is mandatory.
249 # $1: uClibc macro name
250 # $2: Buildroot option name
251 # $3: uClibc config file
252 # $4: feature description
254 check_uclibc_feature = \
255 IS_IN_LIBC=`grep -q "\#define $(1) 1" $(3) && echo y` ; \
256 if [ -z "$(2)" ] ; then \
257 if [ "$${IS_IN_LIBC}" != "y" ] ; then \
258 echo "$(4) not available in C library, toolchain unsuitable for Buildroot" ; \
259 exit 1 ; \
260 fi ; \
261 else \
262 if [ "$($(2))" != "y" -a "$${IS_IN_LIBC}" = "y" ] ; then \
263 echo "$(4) available in C library, please enable $(2)" ; \
264 exit 1 ; \
265 fi ; \
266 if [ "$($(2))" = "y" -a "$${IS_IN_LIBC}" != "y" ] ; then \
267 echo "$(4) not available in C library, please disable $(2)" ; \
268 exit 1 ; \
269 fi ; \
273 # Check the correctness of a uclibc external toolchain configuration
274 # 1. Check that the C library selected in Buildroot matches the one
275 # of the external toolchain
276 # 2. Check that the features enabled in the Buildroot configuration
277 # match the features available in the uClibc of the external
278 # toolchain
280 # $1: sysroot directory
282 check_uclibc = \
283 SYSROOT_DIR="$(strip $1)"; \
284 if ! test -f $${SYSROOT_DIR}/usr/include/bits/uClibc_config.h ; then \
285 echo "Incorrect selection of the C library"; \
286 exit -1; \
287 fi; \
288 UCLIBC_CONFIG_FILE=$${SYSROOT_DIR}/usr/include/bits/uClibc_config.h ; \
289 $(call check_uclibc_feature,__ARCH_USE_MMU__,BR2_USE_MMU,$${UCLIBC_CONFIG_FILE},MMU support) ;\
290 $(call check_uclibc_feature,__UCLIBC_HAS_LFS__,,$${UCLIBC_CONFIG_FILE},Large file support) ;\
291 $(call check_uclibc_feature,__UCLIBC_HAS_IPV6__,,$${UCLIBC_CONFIG_FILE},IPv6 support) ;\
292 $(call check_uclibc_feature,__UCLIBC_HAS_RPC__,BR2_TOOLCHAIN_HAS_NATIVE_RPC,$${UCLIBC_CONFIG_FILE},RPC support) ;\
293 $(call check_uclibc_feature,__UCLIBC_HAS_LOCALE__,BR2_ENABLE_LOCALE,$${UCLIBC_CONFIG_FILE},Locale support) ;\
294 $(call check_uclibc_feature,__UCLIBC_HAS_WCHAR__,BR2_USE_WCHAR,$${UCLIBC_CONFIG_FILE},Wide char support) ;\
295 $(call check_uclibc_feature,__UCLIBC_HAS_THREADS__,BR2_TOOLCHAIN_HAS_THREADS,$${UCLIBC_CONFIG_FILE},Thread support) ;\
296 $(call check_uclibc_feature,__PTHREADS_DEBUG_SUPPORT__,BR2_TOOLCHAIN_HAS_THREADS_DEBUG,$${UCLIBC_CONFIG_FILE},Thread debugging support) ;\
297 $(call check_uclibc_feature,__UCLIBC_HAS_THREADS_NATIVE__,BR2_TOOLCHAIN_HAS_THREADS_NPTL,$${UCLIBC_CONFIG_FILE},NPTL thread support) ;\
298 $(call check_uclibc_feature,__UCLIBC_HAS_SSP__,BR2_TOOLCHAIN_HAS_SSP,$${UCLIBC_CONFIG_FILE},Stack Smashing Protection support)
301 # Check that the Buildroot configuration of the ABI matches the
302 # configuration of the external toolchain.
304 # $1: cross-gcc path
305 # $2: cross-readelf path
307 check_arm_abi = \
308 __CROSS_CC=$(strip $1) ; \
309 __CROSS_READELF=$(strip $2) ; \
310 EXT_TOOLCHAIN_TARGET=`LANG=C $${__CROSS_CC} -v 2>&1 | grep ^Target | cut -f2 -d ' '` ; \
311 if ! echo $${EXT_TOOLCHAIN_TARGET} | grep -qE 'eabi(hf)?$$' ; then \
312 echo "External toolchain uses the unsuported OABI" ; \
313 exit 1 ; \
314 fi ; \
315 if ! echo 'int main(void) {}' | $${__CROSS_CC} -x c -o $(BUILD_DIR)/.br-toolchain-test.tmp - ; then \
316 rm -f $(BUILD_DIR)/.br-toolchain-test.tmp*; \
317 abistr_$(BR2_ARM_EABI)='EABI'; \
318 abistr_$(BR2_ARM_EABIHF)='EABIhf'; \
319 echo "Incorrect ABI setting: $${abistr_y} selected, but toolchain is incompatible"; \
320 exit 1 ; \
321 fi ; \
322 rm -f $(BUILD_DIR)/.br-toolchain-test.tmp*
325 # Check that the external toolchain supports C++
327 # $1: cross-g++ path
329 check_cplusplus = \
330 __CROSS_CXX=$(strip $1) ; \
331 $${__CROSS_CXX} -v > /dev/null 2>&1 ; \
332 if test $$? -ne 0 ; then \
333 echo "C++ support is selected but is not available in external toolchain" ; \
334 exit 1 ; \
338 # Check that the cross-compiler given in the configuration exists
340 # $1: cross-gcc path
342 check_cross_compiler_exists = \
343 __CROSS_CC=$(strip $1) ; \
344 $${__CROSS_CC} -v > /dev/null 2>&1 ; \
345 if test $$? -ne 0 ; then \
346 echo "Cannot execute cross-compiler '$${__CROSS_CC}'" ; \
347 exit 1 ; \
351 # Check for toolchains known not to work with Buildroot.
352 # - For the Angstrom toolchains, we check by looking at the vendor part of
353 # the host tuple.
354 # - Exclude distro-class toolchains which are not relocatable.
355 # - Exclude broken toolchains which return "libc.a" with -print-file-name.
356 # - Exclude toolchains which doesn't support --sysroot option.
358 # $1: cross-gcc path
360 check_unusable_toolchain = \
361 __CROSS_CC=$(strip $1) ; \
362 vendor=`$${__CROSS_CC} -dumpmachine | cut -f2 -d'-'` ; \
363 if test "$${vendor}" = "angstrom" ; then \
364 echo "Angstrom toolchains are not pure toolchains: they contain" ; \
365 echo "many other libraries than just the C library, which makes" ; \
366 echo "them unsuitable as external toolchains for build systems" ; \
367 echo "such as Buildroot." ; \
368 exit 1 ; \
369 fi; \
370 with_sysroot=`$${__CROSS_CC} -v 2>&1 |sed -r -e '/.* --with-sysroot=([^[:space:]]+)[[:space:]].*/!d; s//\1/'`; \
371 if test "$${with_sysroot}" = "/" ; then \
372 echo "Distribution toolchains are unsuitable for use by Buildroot," ; \
373 echo "as they were configured in a way that makes them non-relocatable,"; \
374 echo "and contain a lot of pre-built libraries that would conflict with"; \
375 echo "the ones Buildroot wants to build."; \
376 exit 1; \
377 fi; \
378 libc_a_path=`$${__CROSS_CC} -print-file-name=libc.a` ; \
379 if test "$${libc_a_path}" = "libc.a" ; then \
380 echo "Unable to detect the toolchain sysroot, Buildroot cannot use this toolchain." ; \
381 exit 1 ; \
382 fi ; \
383 sysroot_dir="$(call toolchain_find_sysroot,$${__CROSS_CC})" ; \
384 if test -z "$${sysroot_dir}" ; then \
385 echo "External toolchain doesn't support --sysroot. Cannot use." ; \
386 exit 1 ; \
390 # Generate gdbinit file for use with Buildroot
392 gen_gdbinit_file = \
393 mkdir -p $(STAGING_DIR)/usr/share/buildroot/ ; \
394 echo "set sysroot $(STAGING_DIR)" > $(STAGING_DIR)/usr/share/buildroot/gdbinit