helpers.mk 10 KB

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