2
1

helpers.mk 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. # Most toolchains (CodeSourcery ones) have their libraries either in
  9. # /lib or /usr/lib relative to their ARCH_SYSROOT_DIR, so we search
  10. # libraries in:
  11. #
  12. # $${ARCH_LIB_DIR}
  13. # usr/$${ARCH_LIB_DIR}
  14. #
  15. # Buildroot toolchains, however, have basic libraries in /lib, and
  16. # libstdc++/libgcc_s in /usr/<target-name>/lib(64), so we also need to
  17. # search libraries in:
  18. #
  19. # usr/$(TOOLCHAIN_EXTERNAL_PREFIX)/$${ARCH_LIB_DIR}
  20. #
  21. # Linaro toolchains have most libraries in lib/<target-name>/, so we
  22. # need to search libraries in:
  23. #
  24. # $${ARCH_LIB_DIR}/$(TOOLCHAIN_EXTERNAL_PREFIX)
  25. #
  26. # And recent Linaro toolchains have the GCC support libraries
  27. # (libstdc++, libgcc_s, etc.) into a separate directory, outside of
  28. # the sysroot, that we called the "SUPPORT_LIB_DIR", into which we
  29. # need to search as well.
  30. #
  31. # Thanks to ARCH_LIB_DIR we also take into account toolchains that
  32. # have the libraries in lib64 and usr/lib64.
  33. #
  34. # Please be very careful to check the major toolchain sources:
  35. # Buildroot, Crosstool-NG, CodeSourcery and Linaro before doing any
  36. # modification on the below logic.
  37. #
  38. # $1: arch specific sysroot directory
  39. # $2: support libraries directory (can be empty)
  40. # $3: library directory ('lib' or 'lib64') from which libraries must be copied
  41. # $4: library name
  42. # $5: destination directory of the libary, relative to $(TARGET_DIR)
  43. #
  44. copy_toolchain_lib_root = \
  45. ARCH_SYSROOT_DIR="$(strip $1)"; \
  46. SUPPORT_LIB_DIR="$(strip $2)" ; \
  47. ARCH_LIB_DIR="$(strip $3)" ; \
  48. LIB="$(strip $4)"; \
  49. DESTDIR="$(strip $5)" ; \
  50. \
  51. for dir in \
  52. $${ARCH_SYSROOT_DIR}/$${ARCH_LIB_DIR}/$(TOOLCHAIN_EXTERNAL_PREFIX) \
  53. $${ARCH_SYSROOT_DIR}/usr/$(TOOLCHAIN_EXTERNAL_PREFIX)/$${ARCH_LIB_DIR} \
  54. $${ARCH_SYSROOT_DIR}/$${ARCH_LIB_DIR} \
  55. $${ARCH_SYSROOT_DIR}/usr/$${ARCH_LIB_DIR} \
  56. $${SUPPORT_LIB_DIR} ; do \
  57. LIBSPATH=`find $${dir} -maxdepth 1 -name "$${LIB}" 2>/dev/null` ; \
  58. if test -n "$${LIBSPATH}" ; then \
  59. break ; \
  60. fi \
  61. done ; \
  62. mkdir -p $(TARGET_DIR)/$${DESTDIR}; \
  63. for LIBPATH in $${LIBSPATH} ; do \
  64. while true ; do \
  65. LIBNAME=`basename $${LIBPATH}`; \
  66. LIBDIR=`dirname $${LIBPATH}` ; \
  67. LINKTARGET=`readlink $${LIBPATH}` ; \
  68. rm -fr $(TARGET_DIR)/$${DESTDIR}/$${LIBNAME}; \
  69. if test -h $${LIBPATH} ; then \
  70. ln -sf `basename $${LINKTARGET}` $(TARGET_DIR)/$${DESTDIR}/$${LIBNAME} ; \
  71. elif test -f $${LIBPATH}; then \
  72. $(INSTALL) -D -m0755 $${LIBPATH} $(TARGET_DIR)/$${DESTDIR}/$${LIBNAME}; \
  73. else \
  74. exit -1; \
  75. fi; \
  76. if test -z "$${LINKTARGET}" ; then \
  77. break ; \
  78. fi ; \
  79. LIBPATH="`readlink -f $${LIBPATH}`"; \
  80. done; \
  81. done; \
  82. \
  83. echo -n
  84. #
  85. # Copy the full external toolchain sysroot directory to the staging
  86. # dir. The operation of this function is rendered a little bit
  87. # complicated by the support for multilib toolchains.
  88. #
  89. # We start by copying etc, lib, sbin and usr from the sysroot of the
  90. # selected architecture variant (as pointed by ARCH_SYSROOT_DIR). This
  91. # allows to import into the staging directory the C library and
  92. # companion libraries for the correct architecture variant. We
  93. # explictly only copy etc, lib, sbin and usr since other directories
  94. # might exist for other architecture variants (on Codesourcery
  95. # toolchain, the sysroot for the default architecture variant contains
  96. # the armv4t and thumb2 subdirectories, which are the sysroot for the
  97. # corresponding architecture variants), and we don't want to import
  98. # them.
  99. #
  100. # Then, if the selected architecture variant is not the default one
  101. # (i.e, if SYSROOT_DIR != ARCH_SYSROOT_DIR), then we :
  102. #
  103. # * Import the header files from the default architecture
  104. # variant. Header files are typically shared between the sysroots
  105. # for the different architecture variants. If we use the
  106. # non-default one, header files were not copied by the previous
  107. # step, so we copy them here from the sysroot of the default
  108. # architecture variant.
  109. #
  110. # * Create a symbolic link that matches the name of the subdirectory
  111. # for the architecture variant in the original sysroot. This is
  112. # required as the compiler will by default look in
  113. # sysroot_dir/arch_variant/ for libraries and headers, when the
  114. # non-default architecture variant is used. Without this, the
  115. # compiler fails to find libraries and headers.
  116. #
  117. # Some toolchains (i.e Linaro binary toolchains) store support
  118. # libraries (libstdc++, libgcc_s) outside of the sysroot, so we simply
  119. # copy all the libraries from the "support lib directory" into our
  120. # sysroot.
  121. #
  122. # Note that the 'locale' directories are not copied. They are huge
  123. # (400+MB) in CodeSourcery toolchains, and they are not really useful.
  124. #
  125. # $1: main sysroot directory of the toolchain
  126. # $2: arch specific sysroot directory of the toolchain
  127. # $3: arch specific subdirectory in the sysroot
  128. # $4: directory of libraries ('lib', 'lib32' or 'lib64')
  129. # $5: support lib directories (for toolchains storing libgcc_s,
  130. # libstdc++ and other gcc support libraries outside of the
  131. # sysroot)
  132. copy_toolchain_sysroot = \
  133. SYSROOT_DIR="$(strip $1)"; \
  134. ARCH_SYSROOT_DIR="$(strip $2)"; \
  135. ARCH_SUBDIR="$(strip $3)"; \
  136. ARCH_LIB_DIR="$(strip $4)" ; \
  137. SUPPORT_LIB_DIR="$(strip $5)" ; \
  138. for i in etc $${ARCH_LIB_DIR} sbin usr usr/$${ARCH_LIB_DIR}; do \
  139. if [ -d $${ARCH_SYSROOT_DIR}/$$i ] ; then \
  140. rsync -au --chmod=Du+w --exclude 'usr/lib/locale' \
  141. --exclude lib --exclude lib32 --exclude lib64 \
  142. $${ARCH_SYSROOT_DIR}/$$i/ $(STAGING_DIR)/$$i/ ; \
  143. fi ; \
  144. done ; \
  145. if [ `readlink -f $${SYSROOT_DIR}` != `readlink -f $${ARCH_SYSROOT_DIR}` ] ; then \
  146. if [ ! -d $${ARCH_SYSROOT_DIR}/usr/include ] ; then \
  147. cp -a $${SYSROOT_DIR}/usr/include $(STAGING_DIR)/usr ; \
  148. fi ; \
  149. mkdir -p `dirname $(STAGING_DIR)/$${ARCH_SUBDIR}` ; \
  150. relpath="./" ; \
  151. nbslashs=`echo -n $${ARCH_SUBDIR} | sed 's%[^/]%%g' | wc -c` ; \
  152. for slash in `seq 1 $${nbslashs}` ; do \
  153. relpath=$${relpath}"../" ; \
  154. done ; \
  155. ln -s $${relpath} $(STAGING_DIR)/$${ARCH_SUBDIR} ; \
  156. echo "Symlinking $(STAGING_DIR)/$${ARCH_SUBDIR} -> $${relpath}" ; \
  157. fi ; \
  158. if test -n "$${SUPPORT_LIB_DIR}" ; then \
  159. cp -a $${SUPPORT_LIB_DIR}/* $(STAGING_DIR)/lib/ ; \
  160. fi ; \
  161. find $(STAGING_DIR) -type d | xargs chmod 755
  162. #
  163. # Check the availability of a particular glibc feature. This function
  164. # is used to check toolchain options that are always supported by
  165. # glibc, so we simply check that the corresponding option is properly
  166. # enabled.
  167. #
  168. # $1: Buildroot option name
  169. # $2: feature description
  170. #
  171. check_glibc_feature = \
  172. if [ "$($(1))" != "y" ] ; then \
  173. echo "$(2) available in C library, please enable $(1)" ; \
  174. exit 1 ; \
  175. fi
  176. #
  177. # Check the availability of RPC support in a glibc toolchain
  178. #
  179. # $1: sysroot directory
  180. #
  181. check_glibc_rpc_feature = \
  182. IS_IN_LIBC=`test -f $(1)/usr/include/rpc/rpc.h && echo y` ; \
  183. if [ "$(BR2_TOOLCHAIN_HAS_NATIVE_RPC)" != "y" -a "$${IS_IN_LIBC}" = "y" ] ; then \
  184. echo "RPC support available in C library, please enable BR2_TOOLCHAIN_HAS_NATIVE_RPC" ; \
  185. exit 1 ; \
  186. fi ; \
  187. if [ "$(BR2_TOOLCHAIN_HAS_NATIVE_RPC)" = "y" -a "$${IS_IN_LIBC}" != "y" ] ; then \
  188. echo "RPC support not available in C library, please disable BR2_TOOLCHAIN_HAS_NATIVE_RPC" ; \
  189. exit 1 ; \
  190. fi
  191. #
  192. # Check the correctness of a glibc external toolchain configuration.
  193. # 1. Check that the C library selected in Buildroot matches the one
  194. # of the external toolchain
  195. # 2. Check that all the C library-related features are enabled in the
  196. # config, since glibc always supports all of them
  197. #
  198. # $1: sysroot directory
  199. #
  200. check_glibc = \
  201. SYSROOT_DIR="$(strip $1)"; \
  202. if test `find $${SYSROOT_DIR}/ -maxdepth 2 -name 'ld-linux*.so.*' -o -name 'ld.so.*' | wc -l` -eq 0 ; then \
  203. echo "Incorrect selection of the C library"; \
  204. exit -1; \
  205. fi; \
  206. $(call check_glibc_feature,BR2_USE_MMU,MMU support) ;\
  207. $(call check_glibc_rpc_feature,$${SYSROOT_DIR})
  208. #
  209. # Check that the selected C library really is musl
  210. #
  211. # $1: sysroot directory
  212. check_musl = \
  213. SYSROOT_DIR="$(strip $1)"; \
  214. if test ! -f $${SYSROOT_DIR}/lib/libc.so -o -e $${SYSROOT_DIR}/lib/libm.so ; then \
  215. echo "Incorrect selection of the C library" ; \
  216. exit -1; \
  217. fi
  218. #
  219. # Check the conformity of Buildroot configuration with regard to the
  220. # uClibc configuration of the external toolchain, for a particular
  221. # feature.
  222. #
  223. # $1: uClibc macro name
  224. # $2: Buildroot option name
  225. # $3: uClibc config file
  226. # $4: feature description
  227. #
  228. check_uclibc_feature = \
  229. IS_IN_LIBC=`grep -q "\#define $(1) 1" $(3) && echo y` ; \
  230. if [ "$($(2))" != "y" -a "$${IS_IN_LIBC}" = "y" ] ; then \
  231. echo "$(4) available in C library, please enable $(2)" ; \
  232. exit 1 ; \
  233. fi ; \
  234. if [ "$($(2))" = "y" -a "$${IS_IN_LIBC}" != "y" ] ; then \
  235. echo "$(4) not available in C library, please disable $(2)" ; \
  236. exit 1 ; \
  237. fi
  238. #
  239. # Check the correctness of a uclibc external toolchain configuration
  240. # 1. Check that the C library selected in Buildroot matches the one
  241. # of the external toolchain
  242. # 2. Check that the features enabled in the Buildroot configuration
  243. # match the features available in the uClibc of the external
  244. # toolchain
  245. #
  246. # $1: sysroot directory
  247. #
  248. check_uclibc = \
  249. SYSROOT_DIR="$(strip $1)"; \
  250. if ! test -f $${SYSROOT_DIR}/usr/include/bits/uClibc_config.h ; then \
  251. echo "Incorrect selection of the C library"; \
  252. exit -1; \
  253. fi; \
  254. UCLIBC_CONFIG_FILE=$${SYSROOT_DIR}/usr/include/bits/uClibc_config.h ; \
  255. $(call check_uclibc_feature,__ARCH_USE_MMU__,BR2_USE_MMU,$${UCLIBC_CONFIG_FILE},MMU support) ;\
  256. $(call check_uclibc_feature,__UCLIBC_HAS_LFS__,BR2_LARGEFILE,$${UCLIBC_CONFIG_FILE},Large file support) ;\
  257. $(call check_uclibc_feature,__UCLIBC_HAS_IPV6__,BR2_INET_IPV6,$${UCLIBC_CONFIG_FILE},IPv6 support) ;\
  258. $(call check_uclibc_feature,__UCLIBC_HAS_RPC__,BR2_TOOLCHAIN_HAS_NATIVE_RPC,$${UCLIBC_CONFIG_FILE},RPC support) ;\
  259. $(call check_uclibc_feature,__UCLIBC_HAS_LOCALE__,BR2_ENABLE_LOCALE,$${UCLIBC_CONFIG_FILE},Locale support) ;\
  260. $(call check_uclibc_feature,__UCLIBC_HAS_WCHAR__,BR2_USE_WCHAR,$${UCLIBC_CONFIG_FILE},Wide char support) ;\
  261. $(call check_uclibc_feature,__UCLIBC_HAS_THREADS__,BR2_TOOLCHAIN_HAS_THREADS,$${UCLIBC_CONFIG_FILE},Thread support) ;\
  262. $(call check_uclibc_feature,__PTHREADS_DEBUG_SUPPORT__,BR2_TOOLCHAIN_HAS_THREADS_DEBUG,$${UCLIBC_CONFIG_FILE},Thread debugging support) ;\
  263. $(call check_uclibc_feature,__UCLIBC_HAS_SSP__,BR2_TOOLCHAIN_HAS_SSP,$${UCLIBC_CONFIG_FILE},Stack Smashing Protection support)
  264. #
  265. # Check that the Buildroot configuration of the ABI matches the
  266. # configuration of the external toolchain.
  267. #
  268. # $1: cross-gcc path
  269. #
  270. check_arm_abi = \
  271. __CROSS_CC=$(strip $1) ; \
  272. __CROSS_READELF=$(strip $2) ; \
  273. EXT_TOOLCHAIN_TARGET=`LANG=C $${__CROSS_CC} -v 2>&1 | grep ^Target | cut -f2 -d ' '` ; \
  274. if ! echo $${EXT_TOOLCHAIN_TARGET} | grep -qE 'eabi(hf)?$$' ; then \
  275. echo "External toolchain uses the unsuported OABI" ; \
  276. exit 1 ; \
  277. fi ; \
  278. EXT_TOOLCHAIN_CRT1=`LANG=C $${__CROSS_CC} -print-file-name=crt1.o` ; \
  279. if $${__CROSS_READELF} -A $${EXT_TOOLCHAIN_CRT1} | grep -q "Tag_ABI_VFP_args:" ; then \
  280. EXT_TOOLCHAIN_ABI="eabihf" ; \
  281. else \
  282. EXT_TOOLCHAIN_ABI="eabi" ; \
  283. fi ; \
  284. if [ "$(BR2_ARM_EABI)" = "y" -a "$${EXT_TOOLCHAIN_ABI}" = "eabihf" ] ; then \
  285. echo "Incorrect ABI setting: EABI selected, but toolchain uses EABIhf" ; \
  286. exit 1 ; \
  287. fi ; \
  288. if [ "$(BR2_ARM_EABIHF)" = "y" -a "$${EXT_TOOLCHAIN_ABI}" = "eabi" ] ; then \
  289. echo "Incorrect ABI setting: EABIhf selected, but toolchain uses EABI" ; \
  290. exit 1 ; \
  291. fi
  292. #
  293. # Check that the external toolchain supports C++
  294. #
  295. # $1: cross-g++ path
  296. #
  297. check_cplusplus = \
  298. __CROSS_CXX=$(strip $1) ; \
  299. $${__CROSS_CXX} -v > /dev/null 2>&1 ; \
  300. if test $$? -ne 0 ; then \
  301. echo "C++ support is selected but is not available in external toolchain" ; \
  302. exit 1 ; \
  303. fi
  304. #
  305. # Check that the cross-compiler given in the configuration exists
  306. #
  307. # $1: cross-gcc path
  308. #
  309. check_cross_compiler_exists = \
  310. __CROSS_CC=$(strip $1) ; \
  311. $${__CROSS_CC} -v > /dev/null 2>&1 ; \
  312. if test $$? -ne 0 ; then \
  313. echo "Cannot execute cross-compiler '$${__CROSS_CC}'" ; \
  314. exit 1 ; \
  315. fi
  316. #
  317. # Check for toolchains known not to work with Buildroot. For now, we
  318. # only check for Angstrom toolchains, by looking at the vendor part of
  319. # the host tuple.
  320. #
  321. # $1: cross-gcc path
  322. #
  323. check_unusable_toolchain = \
  324. __CROSS_CC=$(strip $1) ; \
  325. vendor=`$${__CROSS_CC} -dumpmachine | cut -f2 -d'-'` ; \
  326. if test "$${vendor}" = "angstrom" ; then \
  327. echo "Angstrom toolchains are not pure toolchains: they contain" ; \
  328. echo "many other libraries than just the C library, which makes" ; \
  329. echo "them unsuitable as external toolchains for build systems" ; \
  330. echo "such as Buildroot." ; \
  331. exit 1 ; \
  332. fi