helpers.mk 12 KB

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