circus, python-psutil: fix syntax in Config.in
[buildroot-gz.git] / toolchain / helpers.mk
blob3991bc1cf1e36840ca7f34db8bb9948574a7086c
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 $(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, if the selected architecture variant is not the default one
54 # (i.e, if SYSROOT_DIR != ARCH_SYSROOT_DIR), then we :
56 # * Import the header files from the default architecture
57 # variant. Header files are typically shared between the sysroots
58 # for the different architecture variants. If we use the
59 # non-default one, header files were not copied by the previous
60 # step, so we copy them here from the sysroot of the default
61 # architecture variant.
63 # * Create a symbolic link that matches the name of the subdirectory
64 # for the architecture variant in the original sysroot. This is
65 # required as the compiler will by default look in
66 # sysroot_dir/arch_variant/ for libraries and headers, when the
67 # non-default architecture variant is used. Without this, the
68 # compiler fails to find libraries and headers.
70 # Some toolchains (i.e Linaro binary toolchains) store support
71 # libraries (libstdc++, libgcc_s) outside of the sysroot, so we simply
72 # copy all the libraries from the "support lib directory" into our
73 # sysroot.
75 # Note that the 'locale' directories are not copied. They are huge
76 # (400+MB) in CodeSourcery toolchains, and they are not really useful.
78 # $1: main sysroot directory of the toolchain
79 # $2: arch specific sysroot directory of the toolchain
80 # $3: arch specific subdirectory in the sysroot
81 # $4: directory of libraries ('lib', 'lib32' or 'lib64')
82 # $5: support lib directories (for toolchains storing libgcc_s,
83 # libstdc++ and other gcc support libraries outside of the
84 # sysroot)
85 copy_toolchain_sysroot = \
86 SYSROOT_DIR="$(strip $1)"; \
87 ARCH_SYSROOT_DIR="$(strip $2)"; \
88 ARCH_SUBDIR="$(strip $3)"; \
89 ARCH_LIB_DIR="$(strip $4)" ; \
90 SUPPORT_LIB_DIR="$(strip $5)" ; \
91 for i in etc $${ARCH_LIB_DIR} sbin usr usr/$${ARCH_LIB_DIR}; do \
92 if [ -d $${ARCH_SYSROOT_DIR}/$$i ] ; then \
93 rsync -au --chmod=u=rwX,go=rX --exclude 'usr/lib/locale' \
94 --include '/libexec*/' --exclude '/lib*/' \
95 $${ARCH_SYSROOT_DIR}/$$i/ $(STAGING_DIR)/$$i/ ; \
96 fi ; \
97 done ; \
98 if [ `readlink -f $${SYSROOT_DIR}` != `readlink -f $${ARCH_SYSROOT_DIR}` ] ; then \
99 if [ ! -d $${ARCH_SYSROOT_DIR}/usr/include ] ; then \
100 cp -a $${SYSROOT_DIR}/usr/include $(STAGING_DIR)/usr ; \
101 fi ; \
102 mkdir -p `dirname $(STAGING_DIR)/$${ARCH_SUBDIR}` ; \
103 relpath="./" ; \
104 nbslashs=`printf $${ARCH_SUBDIR} | sed 's%[^/]%%g' | wc -c` ; \
105 for slash in `seq 1 $${nbslashs}` ; do \
106 relpath=$${relpath}"../" ; \
107 done ; \
108 ln -s $${relpath} $(STAGING_DIR)/$${ARCH_SUBDIR} ; \
109 echo "Symlinking $(STAGING_DIR)/$${ARCH_SUBDIR} -> $${relpath}" ; \
110 fi ; \
111 if test -n "$${SUPPORT_LIB_DIR}" ; then \
112 cp -a $${SUPPORT_LIB_DIR}/* $(STAGING_DIR)/lib/ ; \
113 fi ; \
114 find $(STAGING_DIR) -type d | xargs chmod 755
117 # Check the specified kernel headers version actually matches the
118 # version in the toolchain.
120 # $1: sysroot directory
121 # $2: kernel version string, in the form: X.Y
123 check_kernel_headers_version = \
124 if ! support/scripts/check-kernel-headers.sh $(1) $(2); then \
125 exit 1; \
129 # Check the specific gcc version actually matches the version in the
130 # toolchain
132 # $1: path to gcc
133 # $2: expected gcc version
135 # Some details about the sed expression:
136 # - 1!d
137 # - delete if not line 1
139 # - s/^[^)]+\) ([^[:space:]]+).*/\1/
140 # - eat all until the first ')' character followed by a space
141 # - match as many non-space chars as possible
142 # - eat all the remaining chars on the line
143 # - replace by the matched expression
145 check_gcc_version = \
146 expected_version="$(strip $2)" ; \
147 if [ -z "$${expected_version}" ]; then \
148 printf "Internal error, gcc version unknown (no GCC_AT_LEAST_X_Y selected)\n"; \
149 exit 1 ; \
150 fi; \
151 real_version=`$(1) --version | sed -r -e '1!d; s/^[^)]+\) ([^[:space:]]+).*/\1/;'` ; \
152 if [[ ! "$${real_version}" =~ ^$${expected_version}\. ]] ; then \
153 printf "Incorrect selection of gcc version: expected %s.x, got %s\n" \
154 "$${expected_version}" "$${real_version}" ; \
155 exit 1 ; \
159 # Check the availability of a particular glibc feature. This function
160 # is used to check toolchain options that are always supported by
161 # glibc, so we simply check that the corresponding option is properly
162 # enabled.
164 # $1: Buildroot option name
165 # $2: feature description
167 check_glibc_feature = \
168 if [ "$($(1))" != "y" ] ; then \
169 echo "$(2) available in C library, please enable $(1)" ; \
170 exit 1 ; \
174 # Check the availability of RPC support in a glibc toolchain
176 # $1: sysroot directory
178 check_glibc_rpc_feature = \
179 IS_IN_LIBC=`test -f $(1)/usr/include/rpc/rpc.h && echo y` ; \
180 if [ "$(BR2_TOOLCHAIN_HAS_NATIVE_RPC)" != "y" -a "$${IS_IN_LIBC}" = "y" ] ; then \
181 echo "RPC support available in C library, please enable BR2_TOOLCHAIN_EXTERNAL_INET_RPC" ; \
182 exit 1 ; \
183 fi ; \
184 if [ "$(BR2_TOOLCHAIN_HAS_NATIVE_RPC)" = "y" -a "$${IS_IN_LIBC}" != "y" ] ; then \
185 echo "RPC support not available in C library, please disable BR2_TOOLCHAIN_EXTERNAL_INET_RPC" ; \
186 exit 1 ; \
190 # Check the correctness of a glibc external toolchain configuration.
191 # 1. Check that the C library selected in Buildroot matches the one
192 # of the external toolchain
193 # 2. Check that all the C library-related features are enabled in the
194 # config, since glibc always supports all of them
196 # $1: sysroot directory
198 check_glibc = \
199 SYSROOT_DIR="$(strip $1)"; \
200 if test `find $${SYSROOT_DIR}/ -maxdepth 2 -name 'ld-linux*.so.*' -o -name 'ld.so.*' -o -name 'ld64.so.*' | wc -l` -eq 0 ; then \
201 echo "Incorrect selection of the C library"; \
202 exit -1; \
203 fi; \
204 $(call check_glibc_feature,BR2_USE_MMU,MMU support) ;\
205 $(call check_glibc_rpc_feature,$${SYSROOT_DIR})
208 # Check that the selected C library really is musl
210 # $1: sysroot directory
211 check_musl = \
212 SYSROOT_DIR="$(strip $1)"; \
213 if test ! -f $${SYSROOT_DIR}/lib/libc.so -o -e $${SYSROOT_DIR}/lib/libm.so ; then \
214 echo "Incorrect selection of the C library" ; \
215 exit -1; \
219 # Check the conformity of Buildroot configuration with regard to the
220 # uClibc configuration of the external toolchain, for a particular
221 # feature.
223 # If 'Buildroot option name' ($2) is empty it means the uClibc option
224 # is mandatory.
226 # $1: uClibc macro name
227 # $2: Buildroot option name
228 # $3: uClibc config file
229 # $4: feature description
231 check_uclibc_feature = \
232 IS_IN_LIBC=`grep -q "\#define $(1) 1" $(3) && echo y` ; \
233 if [ -z "$(2)" ] ; then \
234 if [ "$${IS_IN_LIBC}" != "y" ] ; then \
235 echo "$(4) not available in C library, toolchain unsuitable for Buildroot" ; \
236 exit 1 ; \
237 fi ; \
238 else \
239 if [ "$($(2))" != "y" -a "$${IS_IN_LIBC}" = "y" ] ; then \
240 echo "$(4) available in C library, please enable $(2)" ; \
241 exit 1 ; \
242 fi ; \
243 if [ "$($(2))" = "y" -a "$${IS_IN_LIBC}" != "y" ] ; then \
244 echo "$(4) not available in C library, please disable $(2)" ; \
245 exit 1 ; \
246 fi ; \
250 # Check the correctness of a uclibc external toolchain configuration
251 # 1. Check that the C library selected in Buildroot matches the one
252 # of the external toolchain
253 # 2. Check that the features enabled in the Buildroot configuration
254 # match the features available in the uClibc of the external
255 # toolchain
257 # $1: sysroot directory
259 check_uclibc = \
260 SYSROOT_DIR="$(strip $1)"; \
261 if ! test -f $${SYSROOT_DIR}/usr/include/bits/uClibc_config.h ; then \
262 echo "Incorrect selection of the C library"; \
263 exit -1; \
264 fi; \
265 UCLIBC_CONFIG_FILE=$${SYSROOT_DIR}/usr/include/bits/uClibc_config.h ; \
266 $(call check_uclibc_feature,__ARCH_USE_MMU__,BR2_USE_MMU,$${UCLIBC_CONFIG_FILE},MMU support) ;\
267 $(call check_uclibc_feature,__UCLIBC_HAS_LFS__,,$${UCLIBC_CONFIG_FILE},Large file support) ;\
268 $(call check_uclibc_feature,__UCLIBC_HAS_IPV6__,,$${UCLIBC_CONFIG_FILE},IPv6 support) ;\
269 $(call check_uclibc_feature,__UCLIBC_HAS_RPC__,BR2_TOOLCHAIN_HAS_NATIVE_RPC,$${UCLIBC_CONFIG_FILE},RPC support) ;\
270 $(call check_uclibc_feature,__UCLIBC_HAS_LOCALE__,BR2_ENABLE_LOCALE,$${UCLIBC_CONFIG_FILE},Locale support) ;\
271 $(call check_uclibc_feature,__UCLIBC_HAS_WCHAR__,BR2_USE_WCHAR,$${UCLIBC_CONFIG_FILE},Wide char support) ;\
272 $(call check_uclibc_feature,__UCLIBC_HAS_THREADS__,BR2_TOOLCHAIN_HAS_THREADS,$${UCLIBC_CONFIG_FILE},Thread support) ;\
273 $(call check_uclibc_feature,__PTHREADS_DEBUG_SUPPORT__,BR2_TOOLCHAIN_HAS_THREADS_DEBUG,$${UCLIBC_CONFIG_FILE},Thread debugging support) ;\
274 $(call check_uclibc_feature,__UCLIBC_HAS_THREADS_NATIVE__,BR2_TOOLCHAIN_HAS_THREADS_NPTL,$${UCLIBC_CONFIG_FILE},NPTL thread support)
277 # Check that the Buildroot configuration of the ABI matches the
278 # configuration of the external toolchain.
280 # $1: cross-gcc path
281 # $2: cross-readelf path
283 check_arm_abi = \
284 __CROSS_CC=$(strip $1) ; \
285 __CROSS_READELF=$(strip $2) ; \
286 EXT_TOOLCHAIN_TARGET=`LANG=C $${__CROSS_CC} -v 2>&1 | grep ^Target | cut -f2 -d ' '` ; \
287 if ! echo $${EXT_TOOLCHAIN_TARGET} | grep -qE 'eabi(hf)?$$' ; then \
288 echo "External toolchain uses the unsuported OABI" ; \
289 exit 1 ; \
290 fi ; \
291 if ! echo 'int main(void) {}' | $${__CROSS_CC} -x c -o $(BUILD_DIR)/.br-toolchain-test.tmp - ; then \
292 rm -f $(BUILD_DIR)/.br-toolchain-test.tmp*; \
293 abistr_$(BR2_ARM_EABI)='EABI'; \
294 abistr_$(BR2_ARM_EABIHF)='EABIhf'; \
295 echo "Incorrect ABI setting: $${abistr_y} selected, but toolchain is incompatible"; \
296 exit 1 ; \
297 fi ; \
298 rm -f $(BUILD_DIR)/.br-toolchain-test.tmp*
301 # Check that the external toolchain supports C++
303 # $1: cross-g++ path
305 check_cplusplus = \
306 __CROSS_CXX=$(strip $1) ; \
307 $${__CROSS_CXX} -v > /dev/null 2>&1 ; \
308 if test $$? -ne 0 ; then \
309 echo "C++ support is selected but is not available in external toolchain" ; \
310 exit 1 ; \
315 # Check that the external toolchain supports Fortran
317 # $1: cross-gfortran path
319 check_fortran = \
320 __CROSS_FC=$(strip $1) ; \
321 __o=$(BUILD_DIR)/.br-toolchain-test-fortran.tmp ; \
322 printf 'program hello\n\tprint *, "Hello Fortran!\\n"\nend program hello\n' | \
323 $${__CROSS_FC} -x f95 -o $${__o} - ; \
324 if test $$? -ne 0 ; then \
325 rm -f $${__o}* ; \
326 echo "Fortran support is selected but is not available in external toolchain" ; \
327 exit 1 ; \
328 fi ; \
329 rm -f $${__o}* \
332 # Check that the cross-compiler given in the configuration exists
334 # $1: cross-gcc path
336 check_cross_compiler_exists = \
337 __CROSS_CC=$(strip $1) ; \
338 $${__CROSS_CC} -v > /dev/null 2>&1 ; \
339 if test $$? -ne 0 ; then \
340 echo "Cannot execute cross-compiler '$${__CROSS_CC}'" ; \
341 exit 1 ; \
345 # Check for toolchains known not to work with Buildroot.
346 # - For the Angstrom toolchains, we check by looking at the vendor part of
347 # the host tuple.
348 # - Exclude distro-class toolchains which are not relocatable.
349 # - Exclude broken toolchains which return "libc.a" with -print-file-name.
350 # - Exclude toolchains which doesn't support --sysroot option.
352 # $1: cross-gcc path
354 check_unusable_toolchain = \
355 __CROSS_CC=$(strip $1) ; \
356 vendor=`$${__CROSS_CC} -dumpmachine | cut -f2 -d'-'` ; \
357 if test "$${vendor}" = "angstrom" ; then \
358 echo "Angstrom toolchains are not pure toolchains: they contain" ; \
359 echo "many other libraries than just the C library, which makes" ; \
360 echo "them unsuitable as external toolchains for build systems" ; \
361 echo "such as Buildroot." ; \
362 exit 1 ; \
363 fi; \
364 with_sysroot=`$${__CROSS_CC} -v 2>&1 |sed -r -e '/.* --with-sysroot=([^[:space:]]+)[[:space:]].*/!d; s//\1/'`; \
365 if test "$${with_sysroot}" = "/" ; then \
366 echo "Distribution toolchains are unsuitable for use by Buildroot," ; \
367 echo "as they were configured in a way that makes them non-relocatable,"; \
368 echo "and contain a lot of pre-built libraries that would conflict with"; \
369 echo "the ones Buildroot wants to build."; \
370 exit 1; \
371 fi; \
372 libc_a_path=`$${__CROSS_CC} -print-file-name=libc.a` ; \
373 if test "$${libc_a_path}" = "libc.a" ; then \
374 echo "Unable to detect the toolchain sysroot, Buildroot cannot use this toolchain." ; \
375 exit 1 ; \
376 fi ; \
377 sysroot_dir="$(call toolchain_find_sysroot,$${__CROSS_CC})" ; \
378 if test -z "$${sysroot_dir}" ; then \
379 echo "External toolchain doesn't support --sysroot. Cannot use." ; \
380 exit 1 ; \
384 # Check if the toolchain has SSP (stack smashing protector) support
386 # $1: cross-gcc path
388 check_toolchain_ssp = \
389 __CROSS_CC=$(strip $1) ; \
390 __HAS_SSP=`echo 'void main(){}' | $${__CROSS_CC} -fstack-protector -x c - -o $(BUILD_DIR)/.br-toolchain-test.tmp >/dev/null 2>&1 && echo y` ; \
391 if [ "$(BR2_TOOLCHAIN_HAS_SSP)" != "y" -a "$${__HAS_SSP}" = "y" ] ; then \
392 echo "SSP support available in this toolchain, please enable BR2_TOOLCHAIN_EXTERNAL_HAS_SSP" ; \
393 exit 1 ; \
394 fi ; \
395 if [ "$(BR2_TOOLCHAIN_HAS_SSP)" = "y" -a "$${__HAS_SSP}" != "y" ] ; then \
396 echo "SSP support not available in this toolchain, please disable BR2_TOOLCHAIN_EXTERNAL_HAS_SSP" ; \
397 exit 1 ; \
398 fi ; \
399 rm -f $(BUILD_DIR)/.br-toolchain-test.tmp*
402 # Generate gdbinit file for use with Buildroot
404 gen_gdbinit_file = \
405 mkdir -p $(STAGING_DIR)/usr/share/buildroot/ ; \
406 echo "set sysroot $(STAGING_DIR)" > $(STAGING_DIR)/usr/share/buildroot/gdbinit