helpers.mk 18 KB

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