xref: /OK3568_Linux_fs/buildroot/toolchain/helpers.mk (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun# This Makefile fragment declares toolchain related helper functions.
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun# The copy_toolchain_lib_root function copies a toolchain library and
4*4882a593Smuzhiyun# its symbolic links from the sysroot directory to the target
5*4882a593Smuzhiyun# directory. Note that this function is used both by the external
6*4882a593Smuzhiyun# toolchain logic, and the glibc package, so care must be taken when
7*4882a593Smuzhiyun# changing this function.
8*4882a593Smuzhiyun#
9*4882a593Smuzhiyun# $1: library name pattern (can include glob wildcards)
10*4882a593Smuzhiyun#
11*4882a593Smuzhiyuncopy_toolchain_lib_root = \
12*4882a593Smuzhiyun	LIBPATTERN="$(strip $1)"; \
13*4882a593Smuzhiyun	LIBPATHS=`find $(STAGING_DIR)/ -name "$${LIBPATTERN}" 2>/dev/null` ; \
14*4882a593Smuzhiyun	for LIBPATH in $${LIBPATHS} ; do \
15*4882a593Smuzhiyun		while true ; do \
16*4882a593Smuzhiyun			LIBNAME=`basename $${LIBPATH}`; \
17*4882a593Smuzhiyun			DESTDIR=`echo $${LIBPATH} | sed "s,^$(STAGING_DIR)/,," | xargs dirname` ; \
18*4882a593Smuzhiyun			mkdir -p $(TARGET_DIR)/$${DESTDIR}; \
19*4882a593Smuzhiyun			rm -fr $(TARGET_DIR)/$${DESTDIR}/$${LIBNAME}; \
20*4882a593Smuzhiyun			if test -h $${LIBPATH} ; then \
21*4882a593Smuzhiyun				cp -d $${LIBPATH} $(TARGET_DIR)/$${DESTDIR}/$${LIBNAME}; \
22*4882a593Smuzhiyun				LIBPATH="`readlink -f $${LIBPATH}`"; \
23*4882a593Smuzhiyun			elif test -f $${LIBPATH}; then \
24*4882a593Smuzhiyun				$(INSTALL) -D -m0755 $${LIBPATH} $(TARGET_DIR)/$${DESTDIR}/$${LIBNAME}; \
25*4882a593Smuzhiyun				break ; \
26*4882a593Smuzhiyun			else \
27*4882a593Smuzhiyun				exit -1; \
28*4882a593Smuzhiyun			fi; \
29*4882a593Smuzhiyun		done; \
30*4882a593Smuzhiyun	done
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun#
33*4882a593Smuzhiyun# Copy the full external toolchain sysroot directory to the staging
34*4882a593Smuzhiyun# dir. The operation of this function is rendered a little bit
35*4882a593Smuzhiyun# complicated by the support for multilib toolchains.
36*4882a593Smuzhiyun#
37*4882a593Smuzhiyun# We start by copying etc, 'lib', sbin, usr and usr/'lib' from the
38*4882a593Smuzhiyun# sysroot of the selected architecture variant (as pointed to by
39*4882a593Smuzhiyun# ARCH_SYSROOT_DIR). This allows to import into the staging directory
40*4882a593Smuzhiyun# the C library and companion libraries for the correct architecture
41*4882a593Smuzhiyun# variant. 'lib' may not be literally 'lib' but could be something else,
42*4882a593Smuzhiyun# e.g. lib32-fp (as determined by ARCH_LIB_DIR) and we only want to copy
43*4882a593Smuzhiyun# that lib directory and no other. When copying usr, we therefore need
44*4882a593Smuzhiyun# to be extra careful not to include usr/lib* but we _do_ want to
45*4882a593Smuzhiyun# include usr/libexec.
46*4882a593Smuzhiyun# We are selective in the directories we copy since other directories
47*4882a593Smuzhiyun# might exist for other architecture variants (on Codesourcery
48*4882a593Smuzhiyun# toolchain, the sysroot for the default architecture variant contains
49*4882a593Smuzhiyun# the armv4t and thumb2 subdirectories, which are the sysroot for the
50*4882a593Smuzhiyun# corresponding architecture variants), and we don't want to import
51*4882a593Smuzhiyun# them.
52*4882a593Smuzhiyun#
53*4882a593Smuzhiyun# If ARCH_LIB_DIR is not a singular directory component, e.g.
54*4882a593Smuzhiyun# 'lib32/octeon2', then symbolic links in ARCH_LIB_DIR and
55*4882a593Smuzhiyun# usr/ARCH_LIB_DIR may be broken because Buildroot will flatten the
56*4882a593Smuzhiyun# directory structure (e.g. lib32/octeon2/foo is actually stored in
57*4882a593Smuzhiyun# lib/foo). This is only relevant for links that contain one or more ../
58*4882a593Smuzhiyun# components, as links to the current directory are always fine.
59*4882a593Smuzhiyun# We need to fix the broken links by removing the right amount of ../
60*4882a593Smuzhiyun# dots from the link destination.
61*4882a593Smuzhiyun# Once the link destination is valid again, it can be simplified to
62*4882a593Smuzhiyun# remove the dependency on intermediate directory symlinks.
63*4882a593Smuzhiyun#
64*4882a593Smuzhiyun# It is possible that ARCH_LIB_DIR does not contain the dynamic loader
65*4882a593Smuzhiyun# (ld*.so or similar) because it (or the main symlink to it) normally
66*4882a593Smuzhiyun# resides in /lib while ARCH_LIB_DIR may be something else (e.g. lib64,
67*4882a593Smuzhiyun# lib/<tuple>, ...). Therefore, copy the dynamic loader separately.
68*4882a593Smuzhiyun#
69*4882a593Smuzhiyun# Then, if the selected architecture variant is not the default one
70*4882a593Smuzhiyun# (i.e, if SYSROOT_DIR != ARCH_SYSROOT_DIR), then we :
71*4882a593Smuzhiyun#
72*4882a593Smuzhiyun#  * Import the header files from the default architecture
73*4882a593Smuzhiyun#    variant. Header files are typically shared between the sysroots
74*4882a593Smuzhiyun#    for the different architecture variants. If we use the
75*4882a593Smuzhiyun#    non-default one, header files were not copied by the previous
76*4882a593Smuzhiyun#    step, so we copy them here from the sysroot of the default
77*4882a593Smuzhiyun#    architecture variant.
78*4882a593Smuzhiyun#
79*4882a593Smuzhiyun#  * Create a symbolic link that matches the name of the subdirectory
80*4882a593Smuzhiyun#    for the architecture variant in the original sysroot. This is
81*4882a593Smuzhiyun#    required as the compiler will by default look in
82*4882a593Smuzhiyun#    sysroot_dir/arch_variant/ for libraries and headers, when the
83*4882a593Smuzhiyun#    non-default architecture variant is used. Without this, the
84*4882a593Smuzhiyun#    compiler fails to find libraries and headers.
85*4882a593Smuzhiyun#
86*4882a593Smuzhiyun# Some toolchains (i.e Linaro binary toolchains) store support
87*4882a593Smuzhiyun# libraries (libstdc++, libgcc_s) outside of the sysroot, so we simply
88*4882a593Smuzhiyun# copy all the libraries from the "support lib directory" into our
89*4882a593Smuzhiyun# sysroot.
90*4882a593Smuzhiyun#
91*4882a593Smuzhiyun# Note that the 'locale' directories are not copied. They are huge
92*4882a593Smuzhiyun# (400+MB) in CodeSourcery toolchains, and they are not really useful.
93*4882a593Smuzhiyun#
94*4882a593Smuzhiyun# $1: main sysroot directory of the toolchain
95*4882a593Smuzhiyun# $2: arch specific sysroot directory of the toolchain
96*4882a593Smuzhiyun# $3: arch specific subdirectory in the sysroot
97*4882a593Smuzhiyun# $4: directory of libraries ('lib', 'lib32' or 'lib64')
98*4882a593Smuzhiyun# $5: support lib directories (for toolchains storing libgcc_s,
99*4882a593Smuzhiyun#     libstdc++ and other gcc support libraries outside of the
100*4882a593Smuzhiyun#     sysroot)
101*4882a593Smuzhiyuncopy_toolchain_sysroot = \
102*4882a593Smuzhiyun	SYSROOT_DIR="$(strip $1)"; \
103*4882a593Smuzhiyun	ARCH_SYSROOT_DIR="$(strip $2)"; \
104*4882a593Smuzhiyun	ARCH_SUBDIR="$(strip $3)"; \
105*4882a593Smuzhiyun	ARCH_LIB_DIR="$(strip $4)" ; \
106*4882a593Smuzhiyun	SUPPORT_LIB_DIR="$(strip $5)" ; \
107*4882a593Smuzhiyun	for i in etc $${ARCH_LIB_DIR} sbin usr usr/$${ARCH_LIB_DIR}; do \
108*4882a593Smuzhiyun		if [ ! -d $${ARCH_SYSROOT_DIR}/$$i ] ; then \
109*4882a593Smuzhiyun			continue ; \
110*4882a593Smuzhiyun		fi ; \
111*4882a593Smuzhiyun		if [ "$$i" = "usr" ]; then \
112*4882a593Smuzhiyun			rsync -au --chmod=u=rwX,go=rX --exclude 'locale/' \
113*4882a593Smuzhiyun				--include '/libexec*/' --exclude '/lib*/' \
114*4882a593Smuzhiyun				$${ARCH_SYSROOT_DIR}/$$i/ $(STAGING_DIR)/$$i/ ; \
115*4882a593Smuzhiyun		else \
116*4882a593Smuzhiyun			rsync -au --chmod=u=rwX,go=rX --exclude 'locale/' \
117*4882a593Smuzhiyun				$${ARCH_SYSROOT_DIR}/$$i/ $(STAGING_DIR)/$$i/ ; \
118*4882a593Smuzhiyun		fi ; \
119*4882a593Smuzhiyun	done ; \
120*4882a593Smuzhiyun	for link in $$(find $(STAGING_DIR) -type l); do \
121*4882a593Smuzhiyun		target=$$(readlink $${link}) ; \
122*4882a593Smuzhiyun		if [ "$${target}" == "$${target$(SHARP_SIGN)/}" ] ; then \
123*4882a593Smuzhiyun			continue ; \
124*4882a593Smuzhiyun		fi ; \
125*4882a593Smuzhiyun		relpath="$(call relpath_prefix,$${target$(SHARP_SIGN)/})" ; \
126*4882a593Smuzhiyun		echo "Fixing symlink $${link} from $${target} to $${relpath}$${target$(SHARP_SIGN)/}" ; \
127*4882a593Smuzhiyun		ln -sf $${relpath}$${target$(SHARP_SIGN)/} $${link} ; \
128*4882a593Smuzhiyun	done ; \
129*4882a593Smuzhiyun	relpath="$(call relpath_prefix,$${ARCH_LIB_DIR})" ; \
130*4882a593Smuzhiyun	if [ "$${relpath}" != "" ]; then \
131*4882a593Smuzhiyun		for i in $$(find -H $(STAGING_DIR)/$${ARCH_LIB_DIR} $(STAGING_DIR)/usr/$${ARCH_LIB_DIR} -type l -xtype l); do \
132*4882a593Smuzhiyun			LINKTARGET=`readlink $$i` ; \
133*4882a593Smuzhiyun			NEWLINKTARGET=$${LINKTARGET\#$$relpath} ; \
134*4882a593Smuzhiyun			ln -sf $${NEWLINKTARGET} $$i ; \
135*4882a593Smuzhiyun			$(call simplify_symlink,$$i,$(STAGING_DIR)) ; \
136*4882a593Smuzhiyun		done ; \
137*4882a593Smuzhiyun	fi ; \
138*4882a593Smuzhiyun	if [[ ! $$(find $(STAGING_DIR)/lib -name 'ld*.so.*' -print -quit) ]]; then \
139*4882a593Smuzhiyun		find $${ARCH_SYSROOT_DIR}/lib -name 'ld*.so.*' -print0 | xargs -0 -I % cp % $(STAGING_DIR)/lib/; \
140*4882a593Smuzhiyun	fi ; \
141*4882a593Smuzhiyun	if [ `readlink -f $${SYSROOT_DIR}` != `readlink -f $${ARCH_SYSROOT_DIR}` ] ; then \
142*4882a593Smuzhiyun		if [ ! -d $${ARCH_SYSROOT_DIR}/usr/include ] ; then \
143*4882a593Smuzhiyun			cp -a $${SYSROOT_DIR}/usr/include $(STAGING_DIR)/usr ; \
144*4882a593Smuzhiyun		fi ; \
145*4882a593Smuzhiyun		mkdir -p `dirname $(STAGING_DIR)/$${ARCH_SUBDIR}` ; \
146*4882a593Smuzhiyun		relpath="$(call relpath_prefix,$${ARCH_SUBDIR})./" ; \
147*4882a593Smuzhiyun		ln -s $${relpath} $(STAGING_DIR)/$${ARCH_SUBDIR} ; \
148*4882a593Smuzhiyun		echo "Symlinking $(STAGING_DIR)/$${ARCH_SUBDIR} -> $${relpath}" ; \
149*4882a593Smuzhiyun	fi ; \
150*4882a593Smuzhiyun	if test -n "$${SUPPORT_LIB_DIR}" ; then \
151*4882a593Smuzhiyun		cp -a $${SUPPORT_LIB_DIR}/* $(STAGING_DIR)/lib/ ; \
152*4882a593Smuzhiyun	fi ; \
153*4882a593Smuzhiyun	find $(STAGING_DIR) -type d | xargs chmod 755
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun#
156*4882a593Smuzhiyun# Check the specified kernel headers version actually matches the
157*4882a593Smuzhiyun# version in the toolchain.
158*4882a593Smuzhiyun#
159*4882a593Smuzhiyun# $1: build directory
160*4882a593Smuzhiyun# $2: sysroot directory
161*4882a593Smuzhiyun# $3: kernel version string, in the form: X.Y
162*4882a593Smuzhiyun# $4: test to do for the latest kernel version, 'strict' or 'loose'
163*4882a593Smuzhiyun#     always 'strict' if this is not the latest version.
164*4882a593Smuzhiyun#
165*4882a593Smuzhiyuncheck_kernel_headers_version = \
166*4882a593Smuzhiyun	if ! support/scripts/check-kernel-headers.sh $(1) $(2) $(3) \
167*4882a593Smuzhiyun		$(if $(BR2_TOOLCHAIN_HEADERS_LATEST),$(4),strict); \
168*4882a593Smuzhiyun	then \
169*4882a593Smuzhiyun		exit 1; \
170*4882a593Smuzhiyun	fi
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun#
173*4882a593Smuzhiyun# Check the specific gcc version actually matches the version in the
174*4882a593Smuzhiyun# toolchain
175*4882a593Smuzhiyun#
176*4882a593Smuzhiyun# $1: path to gcc
177*4882a593Smuzhiyun# $2: expected gcc version
178*4882a593Smuzhiyun#
179*4882a593Smuzhiyuncheck_gcc_version = \
180*4882a593Smuzhiyun	expected_version="$(strip $2)" ; \
181*4882a593Smuzhiyun	if [ -z "$${expected_version}" ]; then \
182*4882a593Smuzhiyun		exit 0 ; \
183*4882a593Smuzhiyun	fi; \
184*4882a593Smuzhiyun	real_version=`$(1) -dumpversion` ; \
185*4882a593Smuzhiyun	if [[ ! "$${real_version}." =~ ^$${expected_version}\. ]] ; then \
186*4882a593Smuzhiyun		printf "Incorrect selection of gcc version: expected %s.x, got %s\n" \
187*4882a593Smuzhiyun			"$${expected_version}" "$${real_version}" ; \
188*4882a593Smuzhiyun		exit 1 ; \
189*4882a593Smuzhiyun	fi
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun#
192*4882a593Smuzhiyun# Check the availability of a particular glibc feature. This function
193*4882a593Smuzhiyun# is used to check toolchain options that are always supported by
194*4882a593Smuzhiyun# glibc, so we simply check that the corresponding option is properly
195*4882a593Smuzhiyun# enabled.
196*4882a593Smuzhiyun#
197*4882a593Smuzhiyun# $1: Buildroot option name
198*4882a593Smuzhiyun# $2: feature description
199*4882a593Smuzhiyun#
200*4882a593Smuzhiyuncheck_glibc_feature = \
201*4882a593Smuzhiyun	if [ "$($(1))" != "y" ] ; then \
202*4882a593Smuzhiyun		echo "$(2) available in C library, please enable $(1)" ; \
203*4882a593Smuzhiyun		exit 1 ; \
204*4882a593Smuzhiyun	fi
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun#
207*4882a593Smuzhiyun# Check the availability of RPC support in a glibc toolchain
208*4882a593Smuzhiyun#
209*4882a593Smuzhiyun# $1: sysroot directory
210*4882a593Smuzhiyun#
211*4882a593Smuzhiyuncheck_glibc_rpc_feature = \
212*4882a593Smuzhiyun	IS_IN_LIBC=`test -f $(1)/usr/include/rpc/rpc.h && echo y` ; \
213*4882a593Smuzhiyun	if [ "$(BR2_TOOLCHAIN_HAS_NATIVE_RPC)" != "y" -a "$${IS_IN_LIBC}" = "y" ] ; then \
214*4882a593Smuzhiyun		echo "RPC support available in C library, please enable BR2_TOOLCHAIN_EXTERNAL_INET_RPC" ; \
215*4882a593Smuzhiyun		exit 1 ; \
216*4882a593Smuzhiyun	fi ; \
217*4882a593Smuzhiyun	if [ "$(BR2_TOOLCHAIN_HAS_NATIVE_RPC)" = "y" -a "$${IS_IN_LIBC}" != "y" ] ; then \
218*4882a593Smuzhiyun		echo "RPC support not available in C library, please disable BR2_TOOLCHAIN_EXTERNAL_INET_RPC" ; \
219*4882a593Smuzhiyun		exit 1 ; \
220*4882a593Smuzhiyun	fi
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun#
223*4882a593Smuzhiyun# Check the correctness of a glibc external toolchain configuration.
224*4882a593Smuzhiyun#  1. Check that the C library selected in Buildroot matches the one
225*4882a593Smuzhiyun#     of the external toolchain
226*4882a593Smuzhiyun#  2. Check that all the C library-related features are enabled in the
227*4882a593Smuzhiyun#     config, since glibc always supports all of them
228*4882a593Smuzhiyun#
229*4882a593Smuzhiyun# $1: sysroot directory
230*4882a593Smuzhiyun#
231*4882a593Smuzhiyuncheck_glibc = \
232*4882a593Smuzhiyun	SYSROOT_DIR="$(strip $1)"; \
233*4882a593Smuzhiyun	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 \
234*4882a593Smuzhiyun		echo "Incorrect selection of the C library"; \
235*4882a593Smuzhiyun		exit -1; \
236*4882a593Smuzhiyun	fi; \
237*4882a593Smuzhiyun	$(call check_glibc_feature,BR2_USE_MMU,MMU support) ;\
238*4882a593Smuzhiyun	$(call check_glibc_rpc_feature,$${SYSROOT_DIR})
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun#
241*4882a593Smuzhiyun# Check that the selected C library really is musl
242*4882a593Smuzhiyun#
243*4882a593Smuzhiyun# $1: cross-gcc path
244*4882a593Smuzhiyun# $2: cross-readelf path
245*4882a593Smuzhiyuncheck_musl = \
246*4882a593Smuzhiyun	__CROSS_CC=$(strip $1) ; \
247*4882a593Smuzhiyun	libc_a_path=`$${__CROSS_CC} -print-file-name=libc.a` ; \
248*4882a593Smuzhiyun	if ! strings $${libc_a_path} | grep -q MUSL_LOCPATH ; then \
249*4882a593Smuzhiyun		echo "Incorrect selection of the C library" ; \
250*4882a593Smuzhiyun		exit -1; \
251*4882a593Smuzhiyun	fi
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun#
254*4882a593Smuzhiyun# Check the conformity of Buildroot configuration with regard to the
255*4882a593Smuzhiyun# uClibc configuration of the external toolchain, for a particular
256*4882a593Smuzhiyun# feature.
257*4882a593Smuzhiyun#
258*4882a593Smuzhiyun# If 'Buildroot option name' ($2) is empty it means the uClibc option
259*4882a593Smuzhiyun# is mandatory.
260*4882a593Smuzhiyun#
261*4882a593Smuzhiyun# $1: uClibc macro name
262*4882a593Smuzhiyun# $2: Buildroot option name
263*4882a593Smuzhiyun# $3: uClibc config file
264*4882a593Smuzhiyun# $4: feature description
265*4882a593Smuzhiyun#
266*4882a593Smuzhiyuncheck_uclibc_feature = \
267*4882a593Smuzhiyun	IS_IN_LIBC=`grep -q "\#define $(1) 1" $(3) && echo y` ; \
268*4882a593Smuzhiyun	if [ -z "$(2)" ] ; then \
269*4882a593Smuzhiyun		if [ "$${IS_IN_LIBC}" != "y" ] ; then \
270*4882a593Smuzhiyun			echo "$(4) not available in C library, toolchain unsuitable for Buildroot" ; \
271*4882a593Smuzhiyun			exit 1 ; \
272*4882a593Smuzhiyun		fi ; \
273*4882a593Smuzhiyun	else \
274*4882a593Smuzhiyun		if [ "$($(2))" != "y" -a "$${IS_IN_LIBC}" = "y" ] ; then \
275*4882a593Smuzhiyun			echo "$(4) available in C library, please enable $(2)" ; \
276*4882a593Smuzhiyun			exit 1 ; \
277*4882a593Smuzhiyun		fi ; \
278*4882a593Smuzhiyun		if [ "$($(2))" = "y" -a "$${IS_IN_LIBC}" != "y" ] ; then \
279*4882a593Smuzhiyun			echo "$(4) not available in C library, please disable $(2)" ; \
280*4882a593Smuzhiyun			exit 1 ; \
281*4882a593Smuzhiyun		fi ; \
282*4882a593Smuzhiyun	fi
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun#
285*4882a593Smuzhiyun# Check the correctness of a uclibc external toolchain configuration
286*4882a593Smuzhiyun#  1. Check that the C library selected in Buildroot matches the one
287*4882a593Smuzhiyun#     of the external toolchain
288*4882a593Smuzhiyun#  2. Check that the features enabled in the Buildroot configuration
289*4882a593Smuzhiyun#     match the features available in the uClibc of the external
290*4882a593Smuzhiyun#     toolchain
291*4882a593Smuzhiyun#
292*4882a593Smuzhiyun# $1: sysroot directory
293*4882a593Smuzhiyun#
294*4882a593Smuzhiyuncheck_uclibc = \
295*4882a593Smuzhiyun	SYSROOT_DIR="$(strip $1)"; \
296*4882a593Smuzhiyun	if ! test -f $${SYSROOT_DIR}/usr/include/bits/uClibc_config.h ; then \
297*4882a593Smuzhiyun		echo "Incorrect selection of the C library"; \
298*4882a593Smuzhiyun		exit -1; \
299*4882a593Smuzhiyun	fi; \
300*4882a593Smuzhiyun	UCLIBC_CONFIG_FILE=$${SYSROOT_DIR}/usr/include/bits/uClibc_config.h ; \
301*4882a593Smuzhiyun	$(call check_uclibc_feature,__ARCH_USE_MMU__,BR2_USE_MMU,$${UCLIBC_CONFIG_FILE},MMU support) ;\
302*4882a593Smuzhiyun	$(call check_uclibc_feature,__UCLIBC_HAS_LFS__,,$${UCLIBC_CONFIG_FILE},Large file support) ;\
303*4882a593Smuzhiyun	$(call check_uclibc_feature,__UCLIBC_HAS_IPV6__,,$${UCLIBC_CONFIG_FILE},IPv6 support) ;\
304*4882a593Smuzhiyun	$(call check_uclibc_feature,__UCLIBC_HAS_RPC__,BR2_TOOLCHAIN_HAS_NATIVE_RPC,$${UCLIBC_CONFIG_FILE},RPC support) ;\
305*4882a593Smuzhiyun	$(call check_uclibc_feature,__UCLIBC_HAS_LOCALE__,BR2_ENABLE_LOCALE,$${UCLIBC_CONFIG_FILE},Locale support) ;\
306*4882a593Smuzhiyun	$(call check_uclibc_feature,__UCLIBC_HAS_WCHAR__,BR2_USE_WCHAR,$${UCLIBC_CONFIG_FILE},Wide char support) ;\
307*4882a593Smuzhiyun	$(call check_uclibc_feature,__UCLIBC_HAS_THREADS__,BR2_TOOLCHAIN_HAS_THREADS,$${UCLIBC_CONFIG_FILE},Thread support) ;\
308*4882a593Smuzhiyun	$(call check_uclibc_feature,__PTHREADS_DEBUG_SUPPORT__,BR2_TOOLCHAIN_HAS_THREADS_DEBUG,$${UCLIBC_CONFIG_FILE},Thread debugging support) ;\
309*4882a593Smuzhiyun	$(call check_uclibc_feature,__UCLIBC_HAS_THREADS_NATIVE__,BR2_TOOLCHAIN_HAS_THREADS_NPTL,$${UCLIBC_CONFIG_FILE},NPTL thread support)
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun#
312*4882a593Smuzhiyun# Check that the Buildroot configuration of the ABI matches the
313*4882a593Smuzhiyun# configuration of the external toolchain.
314*4882a593Smuzhiyun#
315*4882a593Smuzhiyun# $1: cross-gcc path
316*4882a593Smuzhiyun# $2: cross-readelf path
317*4882a593Smuzhiyun#
318*4882a593Smuzhiyuncheck_arm_abi = \
319*4882a593Smuzhiyun	__CROSS_CC=$(strip $1) ; \
320*4882a593Smuzhiyun	EXT_TOOLCHAIN_TARGET=`LANG=C $${__CROSS_CC} -v 2>&1 | grep ^Target | cut -f2 -d ' '` ; \
321*4882a593Smuzhiyun	if ! echo $${EXT_TOOLCHAIN_TARGET} | grep -qE 'eabi(hf)?$$' ; then \
322*4882a593Smuzhiyun		echo "External toolchain uses the unsuported OABI" ; \
323*4882a593Smuzhiyun		exit 1 ; \
324*4882a593Smuzhiyun	fi ; \
325*4882a593Smuzhiyun	if ! echo 'int main(void) {}' | $${__CROSS_CC} -x c -o $(BUILD_DIR)/.br-toolchain-test.tmp - ; then \
326*4882a593Smuzhiyun		rm -f $(BUILD_DIR)/.br-toolchain-test.tmp*; \
327*4882a593Smuzhiyun		abistr_$(BR2_ARM_EABI)='EABI'; \
328*4882a593Smuzhiyun		abistr_$(BR2_ARM_EABIHF)='EABIhf'; \
329*4882a593Smuzhiyun		echo "Incorrect ABI setting: $${abistr_y} selected, but toolchain is incompatible"; \
330*4882a593Smuzhiyun		exit 1 ; \
331*4882a593Smuzhiyun	fi ; \
332*4882a593Smuzhiyun	rm -f $(BUILD_DIR)/.br-toolchain-test.tmp*
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun#
335*4882a593Smuzhiyun# Check that the external toolchain supports C++
336*4882a593Smuzhiyun#
337*4882a593Smuzhiyun# $1: cross-g++ path
338*4882a593Smuzhiyun#
339*4882a593Smuzhiyuncheck_cplusplus = \
340*4882a593Smuzhiyun	__CROSS_CXX=$(strip $1) ; \
341*4882a593Smuzhiyun	$${__CROSS_CXX} -v > /dev/null 2>&1 ; \
342*4882a593Smuzhiyun	if test $$? -ne 0 ; then \
343*4882a593Smuzhiyun		echo "C++ support is selected but is not available in external toolchain" ; \
344*4882a593Smuzhiyun		exit 1 ; \
345*4882a593Smuzhiyun	fi
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun#
348*4882a593Smuzhiyun#
349*4882a593Smuzhiyun# Check that the external toolchain supports D language
350*4882a593Smuzhiyun#
351*4882a593Smuzhiyun# $1: cross-gdc path
352*4882a593Smuzhiyun#
353*4882a593Smuzhiyuncheck_dlang = \
354*4882a593Smuzhiyun	__CROSS_GDC=$(strip $1) ; \
355*4882a593Smuzhiyun	__o=$(BUILD_DIR)/.br-toolchain-test-dlang.tmp ; \
356*4882a593Smuzhiyun	printf 'import std.stdio;\nvoid main() { writeln("Hello World!"); }\n' | \
357*4882a593Smuzhiyun	$${__CROSS_GDC} -x d -o $${__o} - ; \
358*4882a593Smuzhiyun	if test $$? -ne 0 ; then \
359*4882a593Smuzhiyun		rm -f $${__o}* ; \
360*4882a593Smuzhiyun		echo "D language support is selected but is not available in external toolchain" ; \
361*4882a593Smuzhiyun		exit 1 ; \
362*4882a593Smuzhiyun	fi ; \
363*4882a593Smuzhiyun	rm -f $${__o}* \
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun#
366*4882a593Smuzhiyun#
367*4882a593Smuzhiyun# Check that the external toolchain supports Fortran
368*4882a593Smuzhiyun#
369*4882a593Smuzhiyun# $1: cross-gfortran path
370*4882a593Smuzhiyun#
371*4882a593Smuzhiyuncheck_fortran = \
372*4882a593Smuzhiyun	__CROSS_FC=$(strip $1) ; \
373*4882a593Smuzhiyun	__o=$(BUILD_DIR)/.br-toolchain-test-fortran.tmp ; \
374*4882a593Smuzhiyun	printf 'program hello\n\tprint *, "Hello Fortran!\\n"\nend program hello\n' | \
375*4882a593Smuzhiyun	$${__CROSS_FC} -x f95 -o $${__o} - ; \
376*4882a593Smuzhiyun	if test $$? -ne 0 ; then \
377*4882a593Smuzhiyun		rm -f $${__o}* ; \
378*4882a593Smuzhiyun		echo "Fortran support is selected but is not available in external toolchain" ; \
379*4882a593Smuzhiyun		exit 1 ; \
380*4882a593Smuzhiyun	fi ; \
381*4882a593Smuzhiyun	rm -f $${__o}* \
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun#
384*4882a593Smuzhiyun#
385*4882a593Smuzhiyun# Check that the external toolchain supports OpenMP
386*4882a593Smuzhiyun#
387*4882a593Smuzhiyun# $1: cross-gcc path
388*4882a593Smuzhiyun#
389*4882a593Smuzhiyuncheck_openmp = \
390*4882a593Smuzhiyun	__CROSS_CC=$(strip $1) ; \
391*4882a593Smuzhiyun	__o=$(BUILD_DIR)/.br-toolchain-test-openmp.tmp ; \
392*4882a593Smuzhiyun	printf '\#include <omp.h>\nint main(void) { return omp_get_thread_num(); }' | \
393*4882a593Smuzhiyun	$${__CROSS_CC} -fopenmp -x c -o $${__o} - ; \
394*4882a593Smuzhiyun	if test $$? -ne 0 ; then \
395*4882a593Smuzhiyun		rm -f $${__o}* ; \
396*4882a593Smuzhiyun		echo "OpenMP support is selected but is not available in external toolchain"; \
397*4882a593Smuzhiyun		exit 1 ; \
398*4882a593Smuzhiyun	fi ; \
399*4882a593Smuzhiyun	rm -f $${__o}* \
400*4882a593Smuzhiyun
401*4882a593Smuzhiyun#
402*4882a593Smuzhiyun# Check that the cross-compiler given in the configuration exists
403*4882a593Smuzhiyun#
404*4882a593Smuzhiyun# $1: cross-gcc path
405*4882a593Smuzhiyun#
406*4882a593Smuzhiyuncheck_cross_compiler_exists = \
407*4882a593Smuzhiyun	__CROSS_CC=$(strip $1) ; \
408*4882a593Smuzhiyun	$${__CROSS_CC} -v > /dev/null 2>&1 ; \
409*4882a593Smuzhiyun	if test $$? -ne 0 ; then \
410*4882a593Smuzhiyun		echo "Cannot execute cross-compiler '$${__CROSS_CC}'" ; \
411*4882a593Smuzhiyun		exit 1 ; \
412*4882a593Smuzhiyun	fi
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun#
415*4882a593Smuzhiyun# Check for toolchains known not to work with Buildroot.
416*4882a593Smuzhiyun# - For the Angstrom toolchains, we check by looking at the vendor part of
417*4882a593Smuzhiyun#   the host tuple.
418*4882a593Smuzhiyun# - Exclude distro-class toolchains which are not relocatable.
419*4882a593Smuzhiyun# - Exclude broken toolchains which return "libc.a" with -print-file-name.
420*4882a593Smuzhiyun# - Exclude toolchains which doesn't support --sysroot option.
421*4882a593Smuzhiyun#
422*4882a593Smuzhiyun# $1: cross-gcc path
423*4882a593Smuzhiyun#
424*4882a593Smuzhiyuncheck_unusable_toolchain = \
425*4882a593Smuzhiyun	__CROSS_CC=$(strip $1) ; \
426*4882a593Smuzhiyun	vendor=`$${__CROSS_CC} -dumpmachine | cut -f2 -d'-'` ; \
427*4882a593Smuzhiyun	if test "$${vendor}" = "angstrom" ; then \
428*4882a593Smuzhiyun		echo "Angstrom toolchains are not pure toolchains: they contain" ; \
429*4882a593Smuzhiyun		echo "many other libraries than just the C library, which makes" ; \
430*4882a593Smuzhiyun		echo "them unsuitable as external toolchains for build systems" ; \
431*4882a593Smuzhiyun		echo "such as Buildroot." ; \
432*4882a593Smuzhiyun		exit 1 ; \
433*4882a593Smuzhiyun	fi; \
434*4882a593Smuzhiyun	with_sysroot=`$${__CROSS_CC} -v 2>&1 |sed -r -e '/.* --with-sysroot=([^[:space:]]+)[[:space:]].*/!d; s//\1/'`; \
435*4882a593Smuzhiyun	if test "$${with_sysroot}"  = "/" ; then \
436*4882a593Smuzhiyun		echo "Distribution toolchains are unsuitable for use by Buildroot," ; \
437*4882a593Smuzhiyun		echo "as they were configured in a way that makes them non-relocatable,"; \
438*4882a593Smuzhiyun		echo "and contain a lot of pre-built libraries that would conflict with"; \
439*4882a593Smuzhiyun		echo "the ones Buildroot wants to build."; \
440*4882a593Smuzhiyun		exit 1; \
441*4882a593Smuzhiyun	fi; \
442*4882a593Smuzhiyun	libc_a_path=`$${__CROSS_CC} -print-file-name=libc.a` ; \
443*4882a593Smuzhiyun	if test "$${libc_a_path}" = "libc.a" ; then \
444*4882a593Smuzhiyun		echo "Unable to detect the toolchain sysroot, Buildroot cannot use this toolchain." ; \
445*4882a593Smuzhiyun		exit 1 ; \
446*4882a593Smuzhiyun	fi ; \
447*4882a593Smuzhiyun	sysroot_dir="$(call toolchain_find_sysroot,$${__CROSS_CC})" ; \
448*4882a593Smuzhiyun	if test -z "$${sysroot_dir}" ; then \
449*4882a593Smuzhiyun		echo "External toolchain doesn't support --sysroot. Cannot use." ; \
450*4882a593Smuzhiyun		exit 1 ; \
451*4882a593Smuzhiyun	fi
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun#
454*4882a593Smuzhiyun# Check if the toolchain has SSP (stack smashing protector) support
455*4882a593Smuzhiyun#
456*4882a593Smuzhiyun# $1: cross-gcc path
457*4882a593Smuzhiyun# $2: gcc ssp option
458*4882a593Smuzhiyun#
459*4882a593Smuzhiyuncheck_toolchain_ssp = \
460*4882a593Smuzhiyun	__CROSS_CC=$(strip $1) ; \
461*4882a593Smuzhiyun	__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` ; \
462*4882a593Smuzhiyun	if [ "$(BR2_TOOLCHAIN_HAS_SSP)" != "y" -a "$${__HAS_SSP}" = "y" ] ; then \
463*4882a593Smuzhiyun		echo "SSP support available in this toolchain, please enable BR2_TOOLCHAIN_EXTERNAL_HAS_SSP" ; \
464*4882a593Smuzhiyun		exit 1 ; \
465*4882a593Smuzhiyun	fi ; \
466*4882a593Smuzhiyun	if [ "$(BR2_TOOLCHAIN_HAS_SSP)" = "y" -a "$${__HAS_SSP}" != "y" ] ; then \
467*4882a593Smuzhiyun		echo "SSP support not available in this toolchain, please disable BR2_TOOLCHAIN_EXTERNAL_HAS_SSP" ; \
468*4882a593Smuzhiyun		exit 1 ; \
469*4882a593Smuzhiyun	fi ; \
470*4882a593Smuzhiyun	__SSP_OPTION=$(2); \
471*4882a593Smuzhiyun	if [ -n "$${__SSP_OPTION}" ] ; then \
472*4882a593Smuzhiyun		if ! echo 'void main(){}' | $${__CROSS_CC} -Werror $${__SSP_OPTION} -x c - -o $(BUILD_DIR)/.br-toolchain-test.tmp >/dev/null 2>&1 ; then \
473*4882a593Smuzhiyun			echo "SSP option $${__SSP_OPTION} not available in this toolchain, please select another SSP level" ; \
474*4882a593Smuzhiyun			exit 1 ; \
475*4882a593Smuzhiyun		fi; \
476*4882a593Smuzhiyun	fi; \
477*4882a593Smuzhiyun	rm -f $(BUILD_DIR)/.br-toolchain-test.tmp*
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun#
480*4882a593Smuzhiyun# Generate gdbinit file for use with Buildroot
481*4882a593Smuzhiyun#
482*4882a593Smuzhiyungen_gdbinit_file = \
483*4882a593Smuzhiyun	mkdir -p $(STAGING_DIR)/usr/share/buildroot/ ; \
484*4882a593Smuzhiyun	echo "add-auto-load-safe-path $(STAGING_DIR)" > $(STAGING_DIR)/usr/share/buildroot/gdbinit ; \
485*4882a593Smuzhiyun	echo "set sysroot $(STAGING_DIR)" >> $(STAGING_DIR)/usr/share/buildroot/gdbinit
486*4882a593Smuzhiyun
487*4882a593Smuzhiyun# Given a path, determine the relative prefix (../) needed to return to the
488*4882a593Smuzhiyun# root level. Note that the last component is treated as a file component; use a
489*4882a593Smuzhiyun# trailing slash to force treating it as a directory. Examples:
490*4882a593Smuzhiyun#     relpath_prefix(lib32) = ""
491*4882a593Smuzhiyun#     relpath_prefix(lib32/octeon2) = "../"
492*4882a593Smuzhiyun#     relpath_prefix(lib32/octeon2/) = "../../"
493*4882a593Smuzhiyun#
494*4882a593Smuzhiyun# $1: input path
495*4882a593Smuzhiyundefine relpath_prefix
496*4882a593Smuzhiyun$$( \
497*4882a593Smuzhiyun	prefix="" ; \
498*4882a593Smuzhiyun	nbslashs=`printf $1 | sed 's%[^/]%%g' | wc -c` ; \
499*4882a593Smuzhiyun	for slash in `seq 1 $${nbslashs}` ; do \
500*4882a593Smuzhiyun		prefix=$${prefix}"../" ; \
501*4882a593Smuzhiyun	done ; \
502*4882a593Smuzhiyun	printf "$$prefix" ; \
503*4882a593Smuzhiyun)
504*4882a593Smuzhiyunendef
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun# Replace the destination of a symbolic link with a simpler version
507*4882a593Smuzhiyun# For example,
508*4882a593Smuzhiyun#     usr/lib/libfoo.so -> ../../lib32/libfoo.so.1
509*4882a593Smuzhiyun# becomes
510*4882a593Smuzhiyun#     usr/lib/libfoo.so -> ../../lib/libfoo.so.1
511*4882a593Smuzhiyun# since 'lib32' is a symlink to 'lib'.
512*4882a593Smuzhiyun#
513*4882a593Smuzhiyun# Likewise,
514*4882a593Smuzhiyun#     usr/lib/octeon2/libbar.so -> ../../../lib32/octeon2/libbar.so.1
515*4882a593Smuzhiyun# becomes
516*4882a593Smuzhiyun#     usr/lib/octeon2/libbar.so -> ../../lib/libbar.so.1
517*4882a593Smuzhiyun# assuming lib32->lib and lib/octeon2->lib.
518*4882a593Smuzhiyun#
519*4882a593Smuzhiyun# $1: symlink
520*4882a593Smuzhiyun# $2: base path
521*4882a593Smuzhiyundefine simplify_symlink
522*4882a593Smuzhiyun( \
523*4882a593Smuzhiyun	FULL_SRC="$$(readlink -f $$(dirname $1))/$$(basename $1)" ; \
524*4882a593Smuzhiyun	FULL_DEST="$$(readlink -f $1)" ; \
525*4882a593Smuzhiyun	FULL_BASE="$$(readlink -f $2)" ; \
526*4882a593Smuzhiyun	REL_SRC="$${FULL_SRC#$${FULL_BASE}/}" ; \
527*4882a593Smuzhiyun	REL_DEST="$${FULL_DEST#$${FULL_BASE}/}" ; \
528*4882a593Smuzhiyun	DOTS="$(call relpath_prefix,$${REL_SRC})" ; \
529*4882a593Smuzhiyun	ln -sf "$${DOTS}$${REL_DEST}" "$${FULL_SRC}" ; \
530*4882a593Smuzhiyun)
531*4882a593Smuzhiyunendef
532