xref: /OK3568_Linux_fs/buildroot/toolchain/helpers.mk (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1# This Makefile fragment declares toolchain related helper functions.
2
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.
8#
9# $1: library name pattern (can include glob wildcards)
10#
11copy_toolchain_lib_root = \
12	LIBPATTERN="$(strip $1)"; \
13	LIBPATHS=`find $(STAGING_DIR)/ -name "$${LIBPATTERN}" 2>/dev/null` ; \
14	for LIBPATH in $${LIBPATHS} ; do \
15		while true ; 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}; \
25				break ; \
26			else \
27				exit -1; \
28			fi; \
29		done; \
30	done
31
32#
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.
36#
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
51# them.
52#
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.
63#
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.
68#
69# Then, if the selected architecture variant is not the default one
70# (i.e, if SYSROOT_DIR != ARCH_SYSROOT_DIR), then we :
71#
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.
78#
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.
85#
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
89# sysroot.
90#
91# Note that the 'locale' directories are not copied. They are huge
92# (400+MB) in CodeSourcery toolchains, and they are not really useful.
93#
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)
101copy_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			continue ; \
110		fi ; \
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/ ; \
115		else \
116			rsync -au --chmod=u=rwX,go=rX --exclude 'locale/' \
117				$${ARCH_SYSROOT_DIR}/$$i/ $(STAGING_DIR)/$$i/ ; \
118		fi ; \
119	done ; \
120	for link in $$(find $(STAGING_DIR) -type l); do \
121		target=$$(readlink $${link}) ; \
122		if [ "$${target}" == "$${target$(SHARP_SIGN)/}" ] ; then \
123			continue ; \
124		fi ; \
125		relpath="$(call relpath_prefix,$${target$(SHARP_SIGN)/})" ; \
126		echo "Fixing symlink $${link} from $${target} to $${relpath}$${target$(SHARP_SIGN)/}" ; \
127		ln -sf $${relpath}$${target$(SHARP_SIGN)/} $${link} ; \
128	done ; \
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)) ; \
136		done ; \
137	fi ; \
138	if [[ ! $$(find $(STAGING_DIR)/lib -name 'ld*.so.*' -print -quit) ]]; then \
139		find $${ARCH_SYSROOT_DIR}/lib -name 'ld*.so.*' -print0 | xargs -0 -I % cp % $(STAGING_DIR)/lib/; \
140	fi ; \
141	if [ `readlink -f $${SYSROOT_DIR}` != `readlink -f $${ARCH_SYSROOT_DIR}` ] ; then \
142		if [ ! -d $${ARCH_SYSROOT_DIR}/usr/include ] ; then \
143			cp -a $${SYSROOT_DIR}/usr/include $(STAGING_DIR)/usr ; \
144		fi ; \
145		mkdir -p `dirname $(STAGING_DIR)/$${ARCH_SUBDIR}` ; \
146		relpath="$(call relpath_prefix,$${ARCH_SUBDIR})./" ; \
147		ln -s $${relpath} $(STAGING_DIR)/$${ARCH_SUBDIR} ; \
148		echo "Symlinking $(STAGING_DIR)/$${ARCH_SUBDIR} -> $${relpath}" ; \
149	fi ; \
150	if test -n "$${SUPPORT_LIB_DIR}" ; then \
151		cp -a $${SUPPORT_LIB_DIR}/* $(STAGING_DIR)/lib/ ; \
152	fi ; \
153	find $(STAGING_DIR) -type d | xargs chmod 755
154
155#
156# Check the specified kernel headers version actually matches the
157# version in the toolchain.
158#
159# $1: build directory
160# $2: sysroot directory
161# $3: kernel version string, in the form: X.Y
162# $4: test to do for the latest kernel version, 'strict' or 'loose'
163#     always 'strict' if this is not the latest version.
164#
165check_kernel_headers_version = \
166	if ! support/scripts/check-kernel-headers.sh $(1) $(2) $(3) \
167		$(if $(BR2_TOOLCHAIN_HEADERS_LATEST),$(4),strict); \
168	then \
169		exit 1; \
170	fi
171
172#
173# Check the specific gcc version actually matches the version in the
174# toolchain
175#
176# $1: path to gcc
177# $2: expected gcc version
178#
179check_gcc_version = \
180	expected_version="$(strip $2)" ; \
181	if [ -z "$${expected_version}" ]; then \
182		exit 0 ; \
183	fi; \
184	real_version=`$(1) -dumpversion` ; \
185	if [[ ! "$${real_version}." =~ ^$${expected_version}\. ]] ; then \
186		printf "Incorrect selection of gcc version: expected %s.x, got %s\n" \
187			"$${expected_version}" "$${real_version}" ; \
188		exit 1 ; \
189	fi
190
191#
192# Check the availability of a particular glibc feature. This function
193# is used to check toolchain options that are always supported by
194# glibc, so we simply check that the corresponding option is properly
195# enabled.
196#
197# $1: Buildroot option name
198# $2: feature description
199#
200check_glibc_feature = \
201	if [ "$($(1))" != "y" ] ; then \
202		echo "$(2) available in C library, please enable $(1)" ; \
203		exit 1 ; \
204	fi
205
206#
207# Check the availability of RPC support in a glibc toolchain
208#
209# $1: sysroot directory
210#
211check_glibc_rpc_feature = \
212	IS_IN_LIBC=`test -f $(1)/usr/include/rpc/rpc.h && echo y` ; \
213	if [ "$(BR2_TOOLCHAIN_HAS_NATIVE_RPC)" != "y" -a "$${IS_IN_LIBC}" = "y" ] ; then \
214		echo "RPC support available in C library, please enable BR2_TOOLCHAIN_EXTERNAL_INET_RPC" ; \
215		exit 1 ; \
216	fi ; \
217	if [ "$(BR2_TOOLCHAIN_HAS_NATIVE_RPC)" = "y" -a "$${IS_IN_LIBC}" != "y" ] ; then \
218		echo "RPC support not available in C library, please disable BR2_TOOLCHAIN_EXTERNAL_INET_RPC" ; \
219		exit 1 ; \
220	fi
221
222#
223# Check the correctness of a glibc external toolchain configuration.
224#  1. Check that the C library selected in Buildroot matches the one
225#     of the external toolchain
226#  2. Check that all the C library-related features are enabled in the
227#     config, since glibc always supports all of them
228#
229# $1: sysroot directory
230#
231check_glibc = \
232	SYSROOT_DIR="$(strip $1)"; \
233	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		echo "Incorrect selection of the C library"; \
235		exit -1; \
236	fi; \
237	$(call check_glibc_feature,BR2_USE_MMU,MMU support) ;\
238	$(call check_glibc_rpc_feature,$${SYSROOT_DIR})
239
240#
241# Check that the selected C library really is musl
242#
243# $1: cross-gcc path
244# $2: cross-readelf path
245check_musl = \
246	__CROSS_CC=$(strip $1) ; \
247	libc_a_path=`$${__CROSS_CC} -print-file-name=libc.a` ; \
248	if ! strings $${libc_a_path} | grep -q MUSL_LOCPATH ; then \
249		echo "Incorrect selection of the C library" ; \
250		exit -1; \
251	fi
252
253#
254# Check the conformity of Buildroot configuration with regard to the
255# uClibc configuration of the external toolchain, for a particular
256# feature.
257#
258# If 'Buildroot option name' ($2) is empty it means the uClibc option
259# is mandatory.
260#
261# $1: uClibc macro name
262# $2: Buildroot option name
263# $3: uClibc config file
264# $4: feature description
265#
266check_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" ; \
271			exit 1 ; \
272		fi ; \
273	else \
274		if [ "$($(2))" != "y" -a "$${IS_IN_LIBC}" = "y" ] ; then \
275			echo "$(4) available in C library, please enable $(2)" ; \
276			exit 1 ; \
277		fi ; \
278		if [ "$($(2))" = "y" -a "$${IS_IN_LIBC}" != "y" ] ; then \
279			echo "$(4) not available in C library, please disable $(2)" ; \
280			exit 1 ; \
281		fi ; \
282	fi
283
284#
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
290#     toolchain
291#
292# $1: sysroot directory
293#
294check_uclibc = \
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"; \
298		exit -1; \
299	fi; \
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)
310
311#
312# Check that the Buildroot configuration of the ABI matches the
313# configuration of the external toolchain.
314#
315# $1: cross-gcc path
316# $2: cross-readelf path
317#
318check_arm_abi = \
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" ; \
323		exit 1 ; \
324	fi ; \
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"; \
330		exit 1 ; \
331	fi ; \
332	rm -f $(BUILD_DIR)/.br-toolchain-test.tmp*
333
334#
335# Check that the external toolchain supports C++
336#
337# $1: cross-g++ path
338#
339check_cplusplus = \
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" ; \
344		exit 1 ; \
345	fi
346
347#
348#
349# Check that the external toolchain supports D language
350#
351# $1: cross-gdc path
352#
353check_dlang = \
354	__CROSS_GDC=$(strip $1) ; \
355	__o=$(BUILD_DIR)/.br-toolchain-test-dlang.tmp ; \
356	printf 'import std.stdio;\nvoid main() { writeln("Hello World!"); }\n' | \
357	$${__CROSS_GDC} -x d -o $${__o} - ; \
358	if test $$? -ne 0 ; then \
359		rm -f $${__o}* ; \
360		echo "D language support is selected but is not available in external toolchain" ; \
361		exit 1 ; \
362	fi ; \
363	rm -f $${__o}* \
364
365#
366#
367# Check that the external toolchain supports Fortran
368#
369# $1: cross-gfortran path
370#
371check_fortran = \
372	__CROSS_FC=$(strip $1) ; \
373	__o=$(BUILD_DIR)/.br-toolchain-test-fortran.tmp ; \
374	printf 'program hello\n\tprint *, "Hello Fortran!\\n"\nend program hello\n' | \
375	$${__CROSS_FC} -x f95 -o $${__o} - ; \
376	if test $$? -ne 0 ; then \
377		rm -f $${__o}* ; \
378		echo "Fortran support is selected but is not available in external toolchain" ; \
379		exit 1 ; \
380	fi ; \
381	rm -f $${__o}* \
382
383#
384#
385# Check that the external toolchain supports OpenMP
386#
387# $1: cross-gcc path
388#
389check_openmp = \
390	__CROSS_CC=$(strip $1) ; \
391	__o=$(BUILD_DIR)/.br-toolchain-test-openmp.tmp ; \
392	printf '\#include <omp.h>\nint main(void) { return omp_get_thread_num(); }' | \
393	$${__CROSS_CC} -fopenmp -x c -o $${__o} - ; \
394	if test $$? -ne 0 ; then \
395		rm -f $${__o}* ; \
396		echo "OpenMP support is selected but is not available in external toolchain"; \
397		exit 1 ; \
398	fi ; \
399	rm -f $${__o}* \
400
401#
402# Check that the cross-compiler given in the configuration exists
403#
404# $1: cross-gcc path
405#
406check_cross_compiler_exists = \
407	__CROSS_CC=$(strip $1) ; \
408	$${__CROSS_CC} -v > /dev/null 2>&1 ; \
409	if test $$? -ne 0 ; then \
410		echo "Cannot execute cross-compiler '$${__CROSS_CC}'" ; \
411		exit 1 ; \
412	fi
413
414#
415# Check for toolchains known not to work with Buildroot.
416# - For the Angstrom toolchains, we check by looking at the vendor part of
417#   the host tuple.
418# - Exclude distro-class toolchains which are not relocatable.
419# - Exclude broken toolchains which return "libc.a" with -print-file-name.
420# - Exclude toolchains which doesn't support --sysroot option.
421#
422# $1: cross-gcc path
423#
424check_unusable_toolchain = \
425	__CROSS_CC=$(strip $1) ; \
426	vendor=`$${__CROSS_CC} -dumpmachine | cut -f2 -d'-'` ; \
427	if test "$${vendor}" = "angstrom" ; then \
428		echo "Angstrom toolchains are not pure toolchains: they contain" ; \
429		echo "many other libraries than just the C library, which makes" ; \
430		echo "them unsuitable as external toolchains for build systems" ; \
431		echo "such as Buildroot." ; \
432		exit 1 ; \
433	fi; \
434	with_sysroot=`$${__CROSS_CC} -v 2>&1 |sed -r -e '/.* --with-sysroot=([^[:space:]]+)[[:space:]].*/!d; s//\1/'`; \
435	if test "$${with_sysroot}"  = "/" ; then \
436		echo "Distribution toolchains are unsuitable for use by Buildroot," ; \
437		echo "as they were configured in a way that makes them non-relocatable,"; \
438		echo "and contain a lot of pre-built libraries that would conflict with"; \
439		echo "the ones Buildroot wants to build."; \
440		exit 1; \
441	fi; \
442	libc_a_path=`$${__CROSS_CC} -print-file-name=libc.a` ; \
443	if test "$${libc_a_path}" = "libc.a" ; then \
444		echo "Unable to detect the toolchain sysroot, Buildroot cannot use this toolchain." ; \
445		exit 1 ; \
446	fi ; \
447	sysroot_dir="$(call toolchain_find_sysroot,$${__CROSS_CC})" ; \
448	if test -z "$${sysroot_dir}" ; then \
449		echo "External toolchain doesn't support --sysroot. Cannot use." ; \
450		exit 1 ; \
451	fi
452
453#
454# Check if the toolchain has SSP (stack smashing protector) support
455#
456# $1: cross-gcc path
457# $2: gcc ssp option
458#
459check_toolchain_ssp = \
460	__CROSS_CC=$(strip $1) ; \
461	__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	if [ "$(BR2_TOOLCHAIN_HAS_SSP)" != "y" -a "$${__HAS_SSP}" = "y" ] ; then \
463		echo "SSP support available in this toolchain, please enable BR2_TOOLCHAIN_EXTERNAL_HAS_SSP" ; \
464		exit 1 ; \
465	fi ; \
466	if [ "$(BR2_TOOLCHAIN_HAS_SSP)" = "y" -a "$${__HAS_SSP}" != "y" ] ; then \
467		echo "SSP support not available in this toolchain, please disable BR2_TOOLCHAIN_EXTERNAL_HAS_SSP" ; \
468		exit 1 ; \
469	fi ; \
470	__SSP_OPTION=$(2); \
471	if [ -n "$${__SSP_OPTION}" ] ; then \
472		if ! echo 'void main(){}' | $${__CROSS_CC} -Werror $${__SSP_OPTION} -x c - -o $(BUILD_DIR)/.br-toolchain-test.tmp >/dev/null 2>&1 ; then \
473			echo "SSP option $${__SSP_OPTION} not available in this toolchain, please select another SSP level" ; \
474			exit 1 ; \
475		fi; \
476	fi; \
477	rm -f $(BUILD_DIR)/.br-toolchain-test.tmp*
478
479#
480# Generate gdbinit file for use with Buildroot
481#
482gen_gdbinit_file = \
483	mkdir -p $(STAGING_DIR)/usr/share/buildroot/ ; \
484	echo "add-auto-load-safe-path $(STAGING_DIR)" > $(STAGING_DIR)/usr/share/buildroot/gdbinit ; \
485	echo "set sysroot $(STAGING_DIR)" >> $(STAGING_DIR)/usr/share/buildroot/gdbinit
486
487# Given a path, determine the relative prefix (../) needed to return to the
488# root level. Note that the last component is treated as a file component; use a
489# trailing slash to force treating it as a directory. Examples:
490#     relpath_prefix(lib32) = ""
491#     relpath_prefix(lib32/octeon2) = "../"
492#     relpath_prefix(lib32/octeon2/) = "../../"
493#
494# $1: input path
495define relpath_prefix
496$$( \
497	prefix="" ; \
498	nbslashs=`printf $1 | sed 's%[^/]%%g' | wc -c` ; \
499	for slash in `seq 1 $${nbslashs}` ; do \
500		prefix=$${prefix}"../" ; \
501	done ; \
502	printf "$$prefix" ; \
503)
504endef
505
506# Replace the destination of a symbolic link with a simpler version
507# For example,
508#     usr/lib/libfoo.so -> ../../lib32/libfoo.so.1
509# becomes
510#     usr/lib/libfoo.so -> ../../lib/libfoo.so.1
511# since 'lib32' is a symlink to 'lib'.
512#
513# Likewise,
514#     usr/lib/octeon2/libbar.so -> ../../../lib32/octeon2/libbar.so.1
515# becomes
516#     usr/lib/octeon2/libbar.so -> ../../lib/libbar.so.1
517# assuming lib32->lib and lib/octeon2->lib.
518#
519# $1: symlink
520# $2: base path
521define simplify_symlink
522( \
523	FULL_SRC="$$(readlink -f $$(dirname $1))/$$(basename $1)" ; \
524	FULL_DEST="$$(readlink -f $1)" ; \
525	FULL_BASE="$$(readlink -f $2)" ; \
526	REL_SRC="$${FULL_SRC#$${FULL_BASE}/}" ; \
527	REL_DEST="$${FULL_DEST#$${FULL_BASE}/}" ; \
528	DOTS="$(call relpath_prefix,$${REL_SRC})" ; \
529	ln -sf "$${DOTS}$${REL_DEST}" "$${FULL_SRC}" ; \
530)
531endef
532