helpers.mk 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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$(SHARP_SIGN)/}" ] ; then \
  121. continue ; \
  122. fi ; \
  123. relpath="$(call relpath_prefix,$${target$(SHARP_SIGN)/})" ; \
  124. echo "Fixing symlink $${link} from $${target} to $${relpath}$${target$(SHARP_SIGN)/}" ; \
  125. ln -sf $${relpath}$${target$(SHARP_SIGN)/} $${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: build directory
  159. # $2: sysroot directory
  160. # $3: kernel version string, in the form: X.Y
  161. #
  162. check_kernel_headers_version = \
  163. if ! support/scripts/check-kernel-headers.sh $(1) $(2) $(3); then \
  164. exit 1; \
  165. fi
  166. #
  167. # Check the specific gcc version actually matches the version in the
  168. # toolchain
  169. #
  170. # $1: path to gcc
  171. # $2: expected gcc version
  172. #
  173. check_gcc_version = \
  174. expected_version="$(strip $2)" ; \
  175. if [ -z "$${expected_version}" ]; then \
  176. exit 0 ; \
  177. fi; \
  178. real_version=`$(1) -dumpversion` ; \
  179. if [[ ! "$${real_version}" =~ ^$${expected_version}\. ]] ; then \
  180. printf "Incorrect selection of gcc version: expected %s.x, got %s\n" \
  181. "$${expected_version}" "$${real_version}" ; \
  182. exit 1 ; \
  183. fi
  184. #
  185. # Check the availability of a particular glibc feature. This function
  186. # is used to check toolchain options that are always supported by
  187. # glibc, so we simply check that the corresponding option is properly
  188. # enabled.
  189. #
  190. # $1: Buildroot option name
  191. # $2: feature description
  192. #
  193. check_glibc_feature = \
  194. if [ "$($(1))" != "y" ] ; then \
  195. echo "$(2) available in C library, please enable $(1)" ; \
  196. exit 1 ; \
  197. fi
  198. #
  199. # Check the availability of RPC support in a glibc toolchain
  200. #
  201. # $1: sysroot directory
  202. #
  203. check_glibc_rpc_feature = \
  204. IS_IN_LIBC=`test -f $(1)/usr/include/rpc/rpc.h && echo y` ; \
  205. if [ "$(BR2_TOOLCHAIN_HAS_NATIVE_RPC)" != "y" -a "$${IS_IN_LIBC}" = "y" ] ; then \
  206. echo "RPC support available in C library, please enable BR2_TOOLCHAIN_EXTERNAL_INET_RPC" ; \
  207. exit 1 ; \
  208. fi ; \
  209. if [ "$(BR2_TOOLCHAIN_HAS_NATIVE_RPC)" = "y" -a "$${IS_IN_LIBC}" != "y" ] ; then \
  210. echo "RPC support not available in C library, please disable BR2_TOOLCHAIN_EXTERNAL_INET_RPC" ; \
  211. exit 1 ; \
  212. fi
  213. #
  214. # Check the correctness of a glibc external toolchain configuration.
  215. # 1. Check that the C library selected in Buildroot matches the one
  216. # of the external toolchain
  217. # 2. Check that all the C library-related features are enabled in the
  218. # config, since glibc always supports all of them
  219. #
  220. # $1: sysroot directory
  221. #
  222. check_glibc = \
  223. SYSROOT_DIR="$(strip $1)"; \
  224. 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 \
  225. echo "Incorrect selection of the C library"; \
  226. exit -1; \
  227. fi; \
  228. $(call check_glibc_feature,BR2_USE_MMU,MMU support) ;\
  229. $(call check_glibc_rpc_feature,$${SYSROOT_DIR})
  230. #
  231. # Check that the selected C library really is musl
  232. #
  233. # $1: cross-gcc path
  234. # $2: cross-readelf path
  235. check_musl = \
  236. __CROSS_CC=$(strip $1) ; \
  237. libc_a_path=`$${__CROSS_CC} -print-file-name=libc.a` ; \
  238. if ! strings $${libc_a_path} | grep -q MUSL_LOCPATH ; then \
  239. echo "Incorrect selection of the C library" ; \
  240. exit -1; \
  241. fi
  242. #
  243. # Check the conformity of Buildroot configuration with regard to the
  244. # uClibc configuration of the external toolchain, for a particular
  245. # feature.
  246. #
  247. # If 'Buildroot option name' ($2) is empty it means the uClibc option
  248. # is mandatory.
  249. #
  250. # $1: uClibc macro name
  251. # $2: Buildroot option name
  252. # $3: uClibc config file
  253. # $4: feature description
  254. #
  255. check_uclibc_feature = \
  256. IS_IN_LIBC=`grep -q "\#define $(1) 1" $(3) && echo y` ; \
  257. if [ -z "$(2)" ] ; then \
  258. if [ "$${IS_IN_LIBC}" != "y" ] ; then \
  259. echo "$(4) not available in C library, toolchain unsuitable for Buildroot" ; \
  260. exit 1 ; \
  261. fi ; \
  262. else \
  263. if [ "$($(2))" != "y" -a "$${IS_IN_LIBC}" = "y" ] ; then \
  264. echo "$(4) available in C library, please enable $(2)" ; \
  265. exit 1 ; \
  266. fi ; \
  267. if [ "$($(2))" = "y" -a "$${IS_IN_LIBC}" != "y" ] ; then \
  268. echo "$(4) not available in C library, please disable $(2)" ; \
  269. exit 1 ; \
  270. fi ; \
  271. fi
  272. #
  273. # Check the correctness of a uclibc external toolchain configuration
  274. # 1. Check that the C library selected in Buildroot matches the one
  275. # of the external toolchain
  276. # 2. Check that the features enabled in the Buildroot configuration
  277. # match the features available in the uClibc of the external
  278. # toolchain
  279. #
  280. # $1: sysroot directory
  281. #
  282. check_uclibc = \
  283. SYSROOT_DIR="$(strip $1)"; \
  284. if ! test -f $${SYSROOT_DIR}/usr/include/bits/uClibc_config.h ; then \
  285. echo "Incorrect selection of the C library"; \
  286. exit -1; \
  287. fi; \
  288. UCLIBC_CONFIG_FILE=$${SYSROOT_DIR}/usr/include/bits/uClibc_config.h ; \
  289. $(call check_uclibc_feature,__ARCH_USE_MMU__,BR2_USE_MMU,$${UCLIBC_CONFIG_FILE},MMU support) ;\
  290. $(call check_uclibc_feature,__UCLIBC_HAS_LFS__,,$${UCLIBC_CONFIG_FILE},Large file support) ;\
  291. $(call check_uclibc_feature,__UCLIBC_HAS_IPV6__,,$${UCLIBC_CONFIG_FILE},IPv6 support) ;\
  292. $(call check_uclibc_feature,__UCLIBC_HAS_RPC__,BR2_TOOLCHAIN_HAS_NATIVE_RPC,$${UCLIBC_CONFIG_FILE},RPC support) ;\
  293. $(call check_uclibc_feature,__UCLIBC_HAS_LOCALE__,BR2_ENABLE_LOCALE,$${UCLIBC_CONFIG_FILE},Locale support) ;\
  294. $(call check_uclibc_feature,__UCLIBC_HAS_WCHAR__,BR2_USE_WCHAR,$${UCLIBC_CONFIG_FILE},Wide char support) ;\
  295. $(call check_uclibc_feature,__UCLIBC_HAS_THREADS__,BR2_TOOLCHAIN_HAS_THREADS,$${UCLIBC_CONFIG_FILE},Thread support) ;\
  296. $(call check_uclibc_feature,__PTHREADS_DEBUG_SUPPORT__,BR2_TOOLCHAIN_HAS_THREADS_DEBUG,$${UCLIBC_CONFIG_FILE},Thread debugging support) ;\
  297. $(call check_uclibc_feature,__UCLIBC_HAS_THREADS_NATIVE__,BR2_TOOLCHAIN_HAS_THREADS_NPTL,$${UCLIBC_CONFIG_FILE},NPTL thread support)
  298. #
  299. # Check that the Buildroot configuration of the ABI matches the
  300. # configuration of the external toolchain.
  301. #
  302. # $1: cross-gcc path
  303. # $2: cross-readelf path
  304. #
  305. check_arm_abi = \
  306. __CROSS_CC=$(strip $1) ; \
  307. EXT_TOOLCHAIN_TARGET=`LANG=C $${__CROSS_CC} -v 2>&1 | grep ^Target | cut -f2 -d ' '` ; \
  308. if ! echo $${EXT_TOOLCHAIN_TARGET} | grep -qE 'eabi(hf)?$$' ; then \
  309. echo "External toolchain uses the unsuported OABI" ; \
  310. exit 1 ; \
  311. fi ; \
  312. if ! echo 'int main(void) {}' | $${__CROSS_CC} -x c -o $(BUILD_DIR)/.br-toolchain-test.tmp - ; then \
  313. rm -f $(BUILD_DIR)/.br-toolchain-test.tmp*; \
  314. abistr_$(BR2_ARM_EABI)='EABI'; \
  315. abistr_$(BR2_ARM_EABIHF)='EABIhf'; \
  316. echo "Incorrect ABI setting: $${abistr_y} selected, but toolchain is incompatible"; \
  317. exit 1 ; \
  318. fi ; \
  319. rm -f $(BUILD_DIR)/.br-toolchain-test.tmp*
  320. #
  321. # Check that the external toolchain supports C++
  322. #
  323. # $1: cross-g++ path
  324. #
  325. check_cplusplus = \
  326. __CROSS_CXX=$(strip $1) ; \
  327. $${__CROSS_CXX} -v > /dev/null 2>&1 ; \
  328. if test $$? -ne 0 ; then \
  329. echo "C++ support is selected but is not available in external toolchain" ; \
  330. exit 1 ; \
  331. fi
  332. #
  333. #
  334. # Check that the external toolchain supports Fortran
  335. #
  336. # $1: cross-gfortran path
  337. #
  338. check_fortran = \
  339. __CROSS_FC=$(strip $1) ; \
  340. __o=$(BUILD_DIR)/.br-toolchain-test-fortran.tmp ; \
  341. printf 'program hello\n\tprint *, "Hello Fortran!\\n"\nend program hello\n' | \
  342. $${__CROSS_FC} -x f95 -o $${__o} - ; \
  343. if test $$? -ne 0 ; then \
  344. rm -f $${__o}* ; \
  345. echo "Fortran support is selected but is not available in external toolchain" ; \
  346. exit 1 ; \
  347. fi ; \
  348. rm -f $${__o}* \
  349. #
  350. # Check that the cross-compiler given in the configuration exists
  351. #
  352. # $1: cross-gcc path
  353. #
  354. check_cross_compiler_exists = \
  355. __CROSS_CC=$(strip $1) ; \
  356. $${__CROSS_CC} -v > /dev/null 2>&1 ; \
  357. if test $$? -ne 0 ; then \
  358. echo "Cannot execute cross-compiler '$${__CROSS_CC}'" ; \
  359. exit 1 ; \
  360. fi
  361. #
  362. # Check for toolchains known not to work with Buildroot.
  363. # - For the Angstrom toolchains, we check by looking at the vendor part of
  364. # the host tuple.
  365. # - Exclude distro-class toolchains which are not relocatable.
  366. # - Exclude broken toolchains which return "libc.a" with -print-file-name.
  367. # - Exclude toolchains which doesn't support --sysroot option.
  368. #
  369. # $1: cross-gcc path
  370. #
  371. check_unusable_toolchain = \
  372. __CROSS_CC=$(strip $1) ; \
  373. vendor=`$${__CROSS_CC} -dumpmachine | cut -f2 -d'-'` ; \
  374. if test "$${vendor}" = "angstrom" ; then \
  375. echo "Angstrom toolchains are not pure toolchains: they contain" ; \
  376. echo "many other libraries than just the C library, which makes" ; \
  377. echo "them unsuitable as external toolchains for build systems" ; \
  378. echo "such as Buildroot." ; \
  379. exit 1 ; \
  380. fi; \
  381. with_sysroot=`$${__CROSS_CC} -v 2>&1 |sed -r -e '/.* --with-sysroot=([^[:space:]]+)[[:space:]].*/!d; s//\1/'`; \
  382. if test "$${with_sysroot}" = "/" ; then \
  383. echo "Distribution toolchains are unsuitable for use by Buildroot," ; \
  384. echo "as they were configured in a way that makes them non-relocatable,"; \
  385. echo "and contain a lot of pre-built libraries that would conflict with"; \
  386. echo "the ones Buildroot wants to build."; \
  387. exit 1; \
  388. fi; \
  389. libc_a_path=`$${__CROSS_CC} -print-file-name=libc.a` ; \
  390. if test "$${libc_a_path}" = "libc.a" ; then \
  391. echo "Unable to detect the toolchain sysroot, Buildroot cannot use this toolchain." ; \
  392. exit 1 ; \
  393. fi ; \
  394. sysroot_dir="$(call toolchain_find_sysroot,$${__CROSS_CC})" ; \
  395. if test -z "$${sysroot_dir}" ; then \
  396. echo "External toolchain doesn't support --sysroot. Cannot use." ; \
  397. exit 1 ; \
  398. fi
  399. #
  400. # Check if the toolchain has SSP (stack smashing protector) support
  401. #
  402. # $1: cross-gcc path
  403. #
  404. check_toolchain_ssp = \
  405. __CROSS_CC=$(strip $1) ; \
  406. __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` ; \
  407. if [ "$(BR2_TOOLCHAIN_HAS_SSP)" != "y" -a "$${__HAS_SSP}" = "y" ] ; then \
  408. echo "SSP support available in this toolchain, please enable BR2_TOOLCHAIN_EXTERNAL_HAS_SSP" ; \
  409. exit 1 ; \
  410. fi ; \
  411. if [ "$(BR2_TOOLCHAIN_HAS_SSP)" = "y" -a "$${__HAS_SSP}" != "y" ] ; then \
  412. echo "SSP support not available in this toolchain, please disable BR2_TOOLCHAIN_EXTERNAL_HAS_SSP" ; \
  413. exit 1 ; \
  414. fi ; \
  415. rm -f $(BUILD_DIR)/.br-toolchain-test.tmp*
  416. #
  417. # Generate gdbinit file for use with Buildroot
  418. #
  419. gen_gdbinit_file = \
  420. mkdir -p $(STAGING_DIR)/usr/share/buildroot/ ; \
  421. echo "set sysroot $(STAGING_DIR)" > $(STAGING_DIR)/usr/share/buildroot/gdbinit
  422. # Given a path, determine the relative prefix (../) needed to return to the
  423. # root level. Note that the last component is treated as a file component; use a
  424. # trailing slash to force treating it as a directory. Examples:
  425. # relpath_prefix(lib32) = ""
  426. # relpath_prefix(lib32/octeon2) = "../"
  427. # relpath_prefix(lib32/octeon2/) = "../../"
  428. #
  429. # $1: input path
  430. define relpath_prefix
  431. $$( \
  432. prefix="" ; \
  433. nbslashs=`printf $1 | sed 's%[^/]%%g' | wc -c` ; \
  434. for slash in `seq 1 $${nbslashs}` ; do \
  435. prefix=$${prefix}"../" ; \
  436. done ; \
  437. printf "$$prefix" ; \
  438. )
  439. endef
  440. # Replace the destination of a symbolic link with a simpler version
  441. # For example,
  442. # usr/lib/libfoo.so -> ../../lib32/libfoo.so.1
  443. # becomes
  444. # usr/lib/libfoo.so -> ../../lib/libfoo.so.1
  445. # since 'lib32' is a symlink to 'lib'.
  446. #
  447. # Likewise,
  448. # usr/lib/octeon2/libbar.so -> ../../../lib32/octeon2/libbar.so.1
  449. # becomes
  450. # usr/lib/octeon2/libbar.so -> ../../lib/libbar.so.1
  451. # assuming lib32->lib and lib/octeon2->lib.
  452. #
  453. # $1: symlink
  454. # $2: base path
  455. define simplify_symlink
  456. ( \
  457. FULL_SRC="$$(readlink -f $$(dirname $1))/$$(basename $1)" ; \
  458. FULL_DEST="$$(readlink -f $1)" ; \
  459. FULL_BASE="$$(readlink -f $2)" ; \
  460. REL_SRC="$${FULL_SRC#$${FULL_BASE}/}" ; \
  461. REL_DEST="$${FULL_DEST#$${FULL_BASE}/}" ; \
  462. DOTS="$(call relpath_prefix,$${REL_SRC})" ; \
  463. ln -sf "$${DOTS}$${REL_DEST}" "$${FULL_SRC}" ; \
  464. )
  465. endef