helpers.mk 18 KB

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