ext-tool.mk 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #
  2. # This file implements the support for external toolchains, i.e
  3. # toolchains that have not been produced by Buildroot itself and that
  4. # are already available on the system on which Buildroot runs. So far,
  5. # we have tested this with:
  6. #
  7. # * Toolchains generated by Crosstool-NG
  8. # * Toolchains generated by Buildroot
  9. # * ARM toolchains made available by Codesourcery
  10. #
  11. # The basic principle is the following
  12. #
  13. # 1. Perform some checks on the conformity between the toolchain
  14. # configuration described in the Buildroot menuconfig system, and the
  15. # real configuration of the external toolchain. This is for example
  16. # important to make sure that the Buildroot configuration system
  17. # knows whether the toolchain supports RPC, IPv6, locales, large
  18. # files, etc. Unfortunately, these things cannot be detected
  19. # automatically, since the value of these options (such as
  20. # BR2_INET_RPC) are needed at configuration time because these
  21. # options are used as dependencies for other options. And at
  22. # configuration time, we are not able to retrieve the external
  23. # toolchain configuration.
  24. #
  25. # 2. Copy the libraries needed at runtime to the target directory,
  26. # $(TARGET_DIR). Obviously, things such as the C library, the dynamic
  27. # loader and a few other utility libraries are needed if dynamic
  28. # applications are to be executed on the target system.
  29. #
  30. # 3. Copy the libraries and headers to the staging directory. This
  31. # will allow all further calls to gcc to be made using --sysroot
  32. # $(STAGING_DIR), which greatly simplifies the compilation of the
  33. # packages when using external toolchains. So in the end, only the
  34. # cross-compiler binaries remains external, all libraries and headers
  35. # are imported into the Buildroot tree.
  36. #
  37. # Copy a toolchain library and its symbolic links from the sysroot
  38. # directory to the target directory. Also optionaly strips the
  39. # library.
  40. #
  41. # $1: arch specific sysroot directory
  42. # $2: library name
  43. # $3: destination directory
  44. # $4: strip (y|n), default is to strip
  45. #
  46. copy_toolchain_lib_root = \
  47. ARCH_SYSROOT_DIR="$(strip $1)"; \
  48. LIB="$(strip $2)"; \
  49. STRIP="$(strip $4)"; \
  50. \
  51. LIBS=`(cd $${ARCH_SYSROOT_DIR}; find . -path "./lib/$${LIB}.*" -o -path "./usr/lib/$${LIB}.*")` ; \
  52. for FILE in $${LIBS} ; do \
  53. LIB=`basename $${FILE}`; \
  54. LIBDIR=`dirname $${FILE}` ; \
  55. while test \! -z "$${LIB}"; do \
  56. FULLPATH="$${ARCH_SYSROOT_DIR}/$${LIBDIR}/$${LIB}" ; \
  57. rm -fr $(TARGET_DIR)/$${LIBDIR}/$${LIB}; \
  58. mkdir -p $(TARGET_DIR)/$${LIBDIR}; \
  59. if test -h $${FULLPATH} ; then \
  60. cp -d $${FULLPATH} $(TARGET_DIR)/$${LIBDIR}/; \
  61. elif test -f $${FULLPATH}; then \
  62. $(INSTALL) -D -m0755 $${FULLPATH} $(TARGET_DIR)/$${LIBDIR}/$${LIB}; \
  63. case "$${STRIP}" in \
  64. (0 | n | no) \
  65. ;; \
  66. (*) \
  67. $(TARGET_CROSS)strip "$(TARGET_DIR)/$${LIBDIR}/$${LIB}"; \
  68. ;; \
  69. esac; \
  70. else \
  71. exit -1; \
  72. fi; \
  73. LIB="`readlink $${FULLPATH}`"; \
  74. done; \
  75. done; \
  76. \
  77. echo -n
  78. #
  79. # Copy the full external toolchain sysroot directory to the staging
  80. # dir. The operation of this function is rendered a little bit
  81. # complicated by the support for multilib toolchains.
  82. #
  83. # We start by copying etc, lib, sbin and usr from the sysroot of the
  84. # selected architecture variant (as pointed by ARCH_SYSROOT_DIR). This
  85. # allows to import into the staging directory the C library and
  86. # companion libraries for the correct architecture variant. We
  87. # explictly only copy etc, lib, sbin and usr since other directories
  88. # might exist for other architecture variants (on Codesourcery
  89. # toolchain, the sysroot for the default architecture variant contains
  90. # the armv4t and thumb2 subdirectories, which are the sysroot for the
  91. # corresponding architecture variants), and we don't want to import
  92. # them.
  93. #
  94. # Then, if the selected architecture variant is not the default one
  95. # (i.e, if SYSROOT_DIR != ARCH_SYSROOT_DIR), then we :
  96. #
  97. # * Import the header files from the default architecture
  98. # variant. Header files are typically shared between the sysroots
  99. # for the different architecture variants. If we use the
  100. # non-default one, header files were not copied by the previous
  101. # step, so we copy them here from the sysroot of the default
  102. # architecture variant.
  103. #
  104. # * Create a symbolic link that matches the name of the subdirectory
  105. # for the architecture variant in the original sysroot. This is
  106. # required as the compiler will by default look in
  107. # sysroot_dir/arch_variant/ for libraries and headers, when the
  108. # non-default architecture variant is used. Without this, the
  109. # compiler fails to find libraries and headers.
  110. #
  111. # $1: main sysroot directory of the toolchain
  112. # $2: arch specific sysroot directory of the toolchain
  113. # $3: arch specific subdirectory in the sysroot
  114. #
  115. copy_toolchain_sysroot = \
  116. SYSROOT_DIR="$(strip $1)"; \
  117. ARCH_SYSROOT_DIR="$(strip $2)"; \
  118. ARCH_SUBDIR="$(strip $3)"; \
  119. for i in etc lib sbin usr ; do \
  120. cp -a $${ARCH_SYSROOT_DIR}/$$i $(STAGING_DIR)/ ; \
  121. done ; \
  122. if [ `readlink -f $${SYSROOT_DIR}` != `readlink -f $${ARCH_SYSROOT_DIR}` ] ; then \
  123. if [ ! -d $${ARCH_SYSROOT_DIR}/usr/include ] ; then \
  124. cp -a $${SYSROOT_DIR}/usr/include $(STAGING_DIR)/usr ; \
  125. fi ; \
  126. ln -s . $(STAGING_DIR)/$(ARCH_SUBDIR) ; \
  127. fi ; \
  128. find $(STAGING_DIR) -type d | xargs chmod 755
  129. #
  130. # Check the availability of a particular glibc feature. We assume that
  131. # all Buildroot toolchain options are supported by glibc, so we just
  132. # check that they are enabled.
  133. #
  134. # $1: Buildroot option name
  135. # $2: feature description
  136. #
  137. check_glibc_feature = \
  138. if [ x$($(1)) != x"y" ] ; then \
  139. echo "$(2) available in C library, please enable $(1)" ; \
  140. exit 1 ; \
  141. fi
  142. #
  143. # Check the correctness of a glibc external toolchain configuration.
  144. # 1. Check that the C library selected in Buildroot matches the one
  145. # of the external toolchain
  146. # 2. Check that all the C library-related features are enabled in the
  147. # config, since glibc always supports all of them
  148. #
  149. # $1: sysroot directory
  150. #
  151. check_glibc = \
  152. SYSROOT_DIR="$(strip $1)"; \
  153. if ! test -f $${SYSROOT_DIR}/lib/ld-linux.so.* ; then \
  154. echo "Incorrect selection of the C library"; \
  155. exit -1; \
  156. fi; \
  157. $(call check_glibc_feature,BR2_LARGEFILE,Large file support) ;\
  158. $(call check_glibc_feature,BR2_INET_IPV6,IPv6 support) ;\
  159. $(call check_glibc_feature,BR2_INET_RPC,RPC support) ;\
  160. $(call check_glibc_feature,BR2_ENABLE_LOCALE,Locale support) ;\
  161. $(call check_glibc_feature,BR2_USE_WCHAR,Wide char support) ;\
  162. $(call check_glibc_feature,BR2_PROGRAM_INVOCATION,Program invocation support)
  163. #
  164. # Check the conformity of Buildroot configuration with regard to the
  165. # uClibc configuration of the external toolchain, for a particular
  166. # feature.
  167. #
  168. # $1: uClibc macro name
  169. # $2: Buildroot option name
  170. # $3: uClibc config file
  171. # $4: feature description
  172. #
  173. check_uclibc_feature = \
  174. IS_IN_LIBC=`grep -q "\#define $(1) 1" $(3) && echo y` ; \
  175. if [ x$($(2)) != x"y" -a x$${IS_IN_LIBC} == x"y" ] ; then \
  176. echo "$(4) available in C library, please enable $(2)" ; \
  177. exit 1 ; \
  178. fi ; \
  179. if [ x$($(2)) == x"y" -a x$${IS_IN_LIBC} != x"y" ] ; then \
  180. echo "$(4) not available in C library, please disable $(2)" ; \
  181. exit 1 ; \
  182. fi
  183. #
  184. # Check the correctness of a uclibc external toolchain configuration
  185. # 1. Check that the C library selected in Buildroot matches the one
  186. # of the external toolchain
  187. # 2. Check that the features enabled in the Buildroot configuration
  188. # match the features available in the uClibc of the external
  189. # toolchain
  190. #
  191. # $1: sysroot directory
  192. #
  193. check_uclibc = \
  194. SYSROOT_DIR="$(strip $1)"; \
  195. if ! test -f $${SYSROOT_DIR}/lib/ld-uClibc.so.* ; then \
  196. echo "Incorrect selection of the C library"; \
  197. exit -1; \
  198. fi; \
  199. UCLIBC_CONFIG_FILE=$${SYSROOT_DIR}/usr/include/bits/uClibc_config.h ; \
  200. $(call check_uclibc_feature,__UCLIBC_HAS_LFS__,BR2_LARGEFILE,$${UCLIBC_CONFIG_FILE},Large file support) ;\
  201. $(call check_uclibc_feature,__UCLIBC_HAS_IPV6__,BR2_INET_IPV6,$${UCLIBC_CONFIG_FILE},IPv6 support) ;\
  202. $(call check_uclibc_feature,__UCLIBC_HAS_RPC__,BR2_INET_RPC,$${UCLIBC_CONFIG_FILE},RPC support) ;\
  203. $(call check_uclibc_feature,__UCLIBC_HAS_LOCALE__,BR2_ENABLE_LOCALE,$${UCLIBC_CONFIG_FILE},Locale support) ;\
  204. $(call check_uclibc_feature,__UCLIBC_HAS_WCHAR__,BR2_USE_WCHAR,$${UCLIBC_CONFIG_FILE},Wide char support) ;\
  205. $(call check_uclibc_feature,__UCLIBC_HAS_PROGRAM_INVOCATION_NAME__,BR2_PROGRAM_INVOCATION,$${UCLIBC_CONFIG_FILE},Program invocation support) ;\
  206. #
  207. # Check that the Buildroot configuration of the ABI matches the
  208. # configuration of the external toolchain.
  209. #
  210. check_arm_abi = \
  211. EXT_TOOLCHAIN_TARGET=$(shell LANG=C $(TARGET_CC) -v 2>&1 | grep ^Target | cut -f2 -d ' ') ; \
  212. if echo $${EXT_TOOLCHAIN_TARGET} | grep -q 'eabi$$' ; then \
  213. EXT_TOOLCHAIN_ABI="eabi" ; \
  214. else \
  215. EXT_TOOLCHAIN_ABI="oabi" ; \
  216. fi ; \
  217. if [ x$(BR2_ARM_OABI) == x"y" -a $${EXT_TOOLCHAIN_ABI} == "eabi" ] ; then \
  218. echo "Incorrect ABI setting" ; \
  219. exit 1 ; \
  220. fi ; \
  221. if [ x$(BR2_ARM_EABI) == x"y" -a $${EXT_TOOLCHAIN_ABI} == "oabi" ] ; then \
  222. echo "Incorrect ABI setting" ; \
  223. exit 1 ; \
  224. fi ; \
  225. #
  226. # Check that the cross-compiler given in the configuration exists
  227. #
  228. check_cross_compiler_exists = \
  229. if ! test -x $(TARGET_CC) ; then \
  230. echo "Cannot find cross-compiler $(TARGET_CC)" ; \
  231. exit 1 ; \
  232. fi ; \
  233. uclibc: dependencies $(STAMP_DIR)/ext-toolchain-installed
  234. EXTERNAL_LIBS=libc.so libcrypt.so libdl.so libgcc_s.so libm.so libnsl.so libpthread.so libresolv.so librt.so libutil.so
  235. ifeq ($(BR2_TOOLCHAIN_EXTERNAL_UCLIBC),y)
  236. EXTERNAL_LIBS+=ld-uClibc.so
  237. else
  238. EXTERNAL_LIBS+=ld-linux.so libnss_files.so libnss_dns.so
  239. endif
  240. ifeq ($(BR2_INSTALL_LIBSTDCPP),y)
  241. EXTERNAL_LIBS+=libstdc++.so
  242. endif
  243. # SYSROOT_DIR selection. We first try the -print-sysroot option,
  244. # available in gcc 4.4.x and in some Codesourcery toolchains. If this
  245. # option is not available, we fallback to the value of --with-sysroot
  246. # as visible in CROSS-gcc -v. We don't pass the -march= option to gcc
  247. # as we want the "main" sysroot, which contains all variants of the C
  248. # library in the case of multilib toolchains.
  249. SYSROOT_DIR=$(shell $(TARGET_CC) -print-sysroot 2>/dev/null)
  250. ifeq ($(SYSROOT_DIR),)
  251. SYSROOT_DIR=$(shell readlink -f $$(LANG=C $(TARGET_CC) -print-file-name=libc.a |sed -r -e 's:usr/lib/libc\.a::;'))
  252. endif
  253. # Now, find if the toolchain specifies a sub-directory for the
  254. # specific architecture variant we're interested in. This is the case
  255. # with multilib toolchain, when the selected architecture variant is
  256. # not the default one. To do so, we ask the compiler by passing the
  257. # appropriate -march= flags. ARCH_SUBDIR will contain the
  258. # subdirectory, in the main SYSROOT_DIR, that corresponds to the
  259. # selected architecture variant. ARCH_SYSROOT_DIR will contain the
  260. # full path to this location.
  261. ifneq ($(CC_TARGET_ARCH_),)
  262. TARGET_CC_ARCH_CFLAGS+=-march=$(CC_TARGET_ARCH_)
  263. endif
  264. ARCH_SUBDIR=$(shell $(TARGET_CC) $(TARGET_CC_ARCH_CFLAGS) -print-multi-directory)
  265. ARCH_SYSROOT_DIR=$(SYSROOT_DIR)/$(ARCH_SUBDIR)
  266. $(STAMP_DIR)/ext-toolchain-installed:
  267. @echo "Checking external toolchain settings"
  268. $(Q)$(call check_cross_compiler_exists)
  269. ifeq ($(strip $(SYSROOT_DIR)),)
  270. @echo "External toolchain doesn't support --sysroot. Cannot use."
  271. exit 1
  272. endif
  273. ifeq ($(BR2_arm),y)
  274. $(Q)$(call check_arm_abi)
  275. endif
  276. ifeq ($(BR2_TOOLCHAIN_EXTERNAL_UCLIBC),y)
  277. $(Q)$(call check_uclibc,$(SYSROOT_DIR))
  278. else
  279. $(Q)$(call check_glibc,$(SYSROOT_DIR))
  280. endif
  281. mkdir -p $(TARGET_DIR)/lib
  282. @echo "Copy external toolchain libraries to target..."
  283. $(Q)for libs in $(EXTERNAL_LIBS); do \
  284. $(call copy_toolchain_lib_root,$(ARCH_SYSROOT_DIR),$$libs,$(BR2_TOOLCHAIN_EXTERNAL_STRIP)); \
  285. done
  286. @echo "Copy external toolchain sysroot to staging..."
  287. $(Q)$(call copy_toolchain_sysroot,$(SYSROOT_DIR),$(ARCH_SYSROOT_DIR),$(ARCH_SUBDIR))
  288. @touch $@