helpers.mk 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. LIBPATHS=`find $${dir} -maxdepth 1 -name "$${LIB}" 2>/dev/null` ; \
  58. if test -n "$${LIBPATHS}" ; then \
  59. break ; \
  60. fi \
  61. done ; \
  62. mkdir -p $(TARGET_DIR)/$${DESTDIR}; \
  63. for LIBPATH in $${LIBPATHS} ; 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. # 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, we need to support two types of multilib toolchains:
  99. #
  100. # - The toolchains that have nested sysroots: a main sysroot, and
  101. # then additional sysroots available as subdirectories of the main
  102. # one. This is for example used by Sourcery CodeBench toolchains.
  103. #
  104. # - The toolchains that have side-by-side sysroots. Each sysroot is a
  105. # complete one, they simply leave one next to each other. This is
  106. # for example used by MIPS Codescape toolchains.
  107. #
  108. # So, we first detect if the selected architecture variant is not the
  109. # default one (i.e, if SYSROOT_DIR != ARCH_SYSROOT_DIR).
  110. #
  111. # If we are in the situation of a nested sysroot, we:
  112. #
  113. # * If needed, import the header files from the default architecture
  114. # variant. Header files are typically shared between the sysroots
  115. # for the different architecture variants. If we use the
  116. # non-default one, header files were not copied by the previous
  117. # step, so we copy them here from the sysroot of the default
  118. # architecture variant.
  119. #
  120. # * Create a symbolic link that matches the name of the subdirectory
  121. # for the architecture variant in the original sysroot. This is
  122. # required as the compiler will by default look in
  123. # sysroot_dir/arch_variant/ for libraries and headers, when the
  124. # non-default architecture variant is used. Without this, the
  125. # compiler fails to find libraries and headers.
  126. #
  127. # If we are in the situation of a side-by-side sysroot, we:
  128. #
  129. # * Create a symbolic link
  130. #
  131. # Finally, some toolchains (i.e Linaro binary toolchains) store
  132. # support libraries (libstdc++, libgcc_s) outside of the sysroot, so
  133. # we simply copy all the libraries from the "support lib directory"
  134. # into our sysroot.
  135. #
  136. # Note that the 'locale' directories are not copied. They are huge
  137. # (400+MB) in CodeSourcery toolchains, and they are not really useful.
  138. #
  139. # $1: main sysroot directory of the toolchain
  140. # $2: arch specific sysroot directory of the toolchain
  141. # $3: arch specific subdirectory in the sysroot
  142. # $4: directory of libraries ('lib', 'lib32' or 'lib64')
  143. # $5: support lib directories (for toolchains storing libgcc_s,
  144. # libstdc++ and other gcc support libraries outside of the
  145. # sysroot)
  146. copy_toolchain_sysroot = \
  147. SYSROOT_DIR="$(strip $1)"; \
  148. ARCH_SYSROOT_DIR="$(strip $2)"; \
  149. ARCH_SUBDIR="$(strip $3)"; \
  150. ARCH_LIB_DIR="$(strip $4)" ; \
  151. SUPPORT_LIB_DIR="$(strip $5)" ; \
  152. for i in etc $${ARCH_LIB_DIR} sbin usr usr/$${ARCH_LIB_DIR}; do \
  153. if [ -d $${ARCH_SYSROOT_DIR}/$$i ] ; then \
  154. rsync -au --chmod=u=rwX,go=rX --exclude 'usr/lib/locale' \
  155. --include '/libexec*/' --exclude '/lib*/' \
  156. $${ARCH_SYSROOT_DIR}/$$i/ $(STAGING_DIR)/$$i/ ; \
  157. fi ; \
  158. done ; \
  159. SYSROOT_DIR_CANON=`readlink -f $${SYSROOT_DIR}` ; \
  160. ARCH_SYSROOT_DIR_CANON=`readlink -f $${ARCH_SYSROOT_DIR}` ; \
  161. if [ $${SYSROOT_DIR_CANON} != $${ARCH_SYSROOT_DIR_CANON} ] ; then \
  162. relpath="./" ; \
  163. if [ $${ARCH_SYSROOT_DIR_CANON:0:$${\#SYSROOT_DIR_CANON}} == $${SYSROOT_DIR_CANON} ] ; then \
  164. if [ ! -d $${ARCH_SYSROOT_DIR}/usr/include ] ; then \
  165. cp -a $${SYSROOT_DIR}/usr/include $(STAGING_DIR)/usr ; \
  166. fi ; \
  167. mkdir -p `dirname $(STAGING_DIR)/$${ARCH_SUBDIR}` ; \
  168. nbslashs=`printf $${ARCH_SUBDIR} | sed 's%[^/]%%g' | wc -c` ; \
  169. for slash in `seq 1 $${nbslashs}` ; do \
  170. relpath=$${relpath}"../" ; \
  171. done ; \
  172. ln -s $${relpath} $(STAGING_DIR)/$${ARCH_SUBDIR} ; \
  173. echo "Symlinking $(STAGING_DIR)/$${ARCH_SUBDIR} -> $${relpath}" ; \
  174. elif [ `dirname $${ARCH_SYSROOT_DIR_CANON}` == `dirname $${SYSROOT_DIR_CANON}` ] ; then \
  175. ln -snf $${relpath} $(STAGING_DIR)/`basename $${ARCH_SYSROOT_DIR_CANON}` ; \
  176. echo "Symlinking $(STAGING_DIR)/`basename $${ARCH_SYSROOT_DIR_CANON}` -> $${relpath}" ; \
  177. fi ; \
  178. fi ; \
  179. if test -n "$${SUPPORT_LIB_DIR}" ; then \
  180. cp -a $${SUPPORT_LIB_DIR}/* $(STAGING_DIR)/lib/ ; \
  181. fi ; \
  182. find $(STAGING_DIR) -type d | xargs chmod 755
  183. #
  184. # Check the specified kernel headers version actually matches the
  185. # version in the toolchain.
  186. #
  187. # $1: sysroot directory
  188. # $2: kernel version string, in the form: X.Y
  189. #
  190. check_kernel_headers_version = \
  191. if ! support/scripts/check-kernel-headers.sh $(1) $(2); then \
  192. exit 1; \
  193. fi
  194. #
  195. # Check the specific gcc version actually matches the version in the
  196. # toolchain
  197. #
  198. # $1: path to gcc
  199. # $2: expected gcc version
  200. #
  201. # Some details about the sed expression:
  202. # - 1!d
  203. # - delete if not line 1
  204. #
  205. # - s/^[^)]+\) ([^[:space:]]+).*/\1/
  206. # - eat all until the first ')' character followed by a space
  207. # - match as many non-space chars as possible
  208. # - eat all the remaining chars on the line
  209. # - replace by the matched expression
  210. #
  211. check_gcc_version = \
  212. expected_version="$(strip $2)" ; \
  213. if [ -z "$${expected_version}" ]; then \
  214. printf "Internal error, gcc version unknown (no GCC_AT_LEAST_X_Y selected)\n"; \
  215. exit 1 ; \
  216. fi; \
  217. real_version=`$(1) --version | sed -r -e '1!d; s/^[^)]+\) ([^[:space:]]+).*/\1/;'` ; \
  218. if [[ ! "$${real_version}" =~ ^$${expected_version}\. ]] ; then \
  219. printf "Incorrect selection of gcc version: expected %s.x, got %s\n" \
  220. "$${expected_version}" "$${real_version}" ; \
  221. exit 1 ; \
  222. fi
  223. #
  224. # Check the availability of a particular glibc feature. This function
  225. # is used to check toolchain options that are always supported by
  226. # glibc, so we simply check that the corresponding option is properly
  227. # enabled.
  228. #
  229. # $1: Buildroot option name
  230. # $2: feature description
  231. #
  232. check_glibc_feature = \
  233. if [ "$($(1))" != "y" ] ; then \
  234. echo "$(2) available in C library, please enable $(1)" ; \
  235. exit 1 ; \
  236. fi
  237. #
  238. # Check the availability of RPC support in a glibc toolchain
  239. #
  240. # $1: sysroot directory
  241. #
  242. check_glibc_rpc_feature = \
  243. IS_IN_LIBC=`test -f $(1)/usr/include/rpc/rpc.h && echo y` ; \
  244. if [ "$(BR2_TOOLCHAIN_HAS_NATIVE_RPC)" != "y" -a "$${IS_IN_LIBC}" = "y" ] ; then \
  245. echo "RPC support available in C library, please enable BR2_TOOLCHAIN_EXTERNAL_INET_RPC" ; \
  246. exit 1 ; \
  247. fi ; \
  248. if [ "$(BR2_TOOLCHAIN_HAS_NATIVE_RPC)" = "y" -a "$${IS_IN_LIBC}" != "y" ] ; then \
  249. echo "RPC support not available in C library, please disable BR2_TOOLCHAIN_EXTERNAL_INET_RPC" ; \
  250. exit 1 ; \
  251. fi
  252. #
  253. # Check the correctness of a glibc external toolchain configuration.
  254. # 1. Check that the C library selected in Buildroot matches the one
  255. # of the external toolchain
  256. # 2. Check that all the C library-related features are enabled in the
  257. # config, since glibc always supports all of them
  258. #
  259. # $1: sysroot directory
  260. #
  261. check_glibc = \
  262. SYSROOT_DIR="$(strip $1)"; \
  263. if test `find $${SYSROOT_DIR}/ -maxdepth 2 -name 'ld-linux*.so.*' -o -name 'ld.so.*' -o -name 'ld64.so.*' | wc -l` -eq 0 ; then \
  264. echo "Incorrect selection of the C library"; \
  265. exit -1; \
  266. fi; \
  267. $(call check_glibc_feature,BR2_USE_MMU,MMU support) ;\
  268. $(call check_glibc_rpc_feature,$${SYSROOT_DIR})
  269. #
  270. # Check that the selected C library really is musl
  271. #
  272. # $1: sysroot directory
  273. check_musl = \
  274. SYSROOT_DIR="$(strip $1)"; \
  275. if test ! -f $${SYSROOT_DIR}/lib/libc.so -o -e $${SYSROOT_DIR}/lib/libm.so ; then \
  276. echo "Incorrect selection of the C library" ; \
  277. exit -1; \
  278. fi
  279. #
  280. # Check the conformity of Buildroot configuration with regard to the
  281. # uClibc configuration of the external toolchain, for a particular
  282. # feature.
  283. #
  284. # If 'Buildroot option name' ($2) is empty it means the uClibc option
  285. # is mandatory.
  286. #
  287. # $1: uClibc macro name
  288. # $2: Buildroot option name
  289. # $3: uClibc config file
  290. # $4: feature description
  291. #
  292. check_uclibc_feature = \
  293. IS_IN_LIBC=`grep -q "\#define $(1) 1" $(3) && echo y` ; \
  294. if [ -z "$(2)" ] ; then \
  295. if [ "$${IS_IN_LIBC}" != "y" ] ; then \
  296. echo "$(4) not available in C library, toolchain unsuitable for Buildroot" ; \
  297. exit 1 ; \
  298. fi ; \
  299. else \
  300. if [ "$($(2))" != "y" -a "$${IS_IN_LIBC}" = "y" ] ; then \
  301. echo "$(4) available in C library, please enable $(2)" ; \
  302. exit 1 ; \
  303. fi ; \
  304. if [ "$($(2))" = "y" -a "$${IS_IN_LIBC}" != "y" ] ; then \
  305. echo "$(4) not available in C library, please disable $(2)" ; \
  306. exit 1 ; \
  307. fi ; \
  308. fi
  309. #
  310. # Check the correctness of a uclibc external toolchain configuration
  311. # 1. Check that the C library selected in Buildroot matches the one
  312. # of the external toolchain
  313. # 2. Check that the features enabled in the Buildroot configuration
  314. # match the features available in the uClibc of the external
  315. # toolchain
  316. #
  317. # $1: sysroot directory
  318. #
  319. check_uclibc = \
  320. SYSROOT_DIR="$(strip $1)"; \
  321. if ! test -f $${SYSROOT_DIR}/usr/include/bits/uClibc_config.h ; then \
  322. echo "Incorrect selection of the C library"; \
  323. exit -1; \
  324. fi; \
  325. UCLIBC_CONFIG_FILE=$${SYSROOT_DIR}/usr/include/bits/uClibc_config.h ; \
  326. $(call check_uclibc_feature,__ARCH_USE_MMU__,BR2_USE_MMU,$${UCLIBC_CONFIG_FILE},MMU support) ;\
  327. $(call check_uclibc_feature,__UCLIBC_HAS_LFS__,,$${UCLIBC_CONFIG_FILE},Large file support) ;\
  328. $(call check_uclibc_feature,__UCLIBC_HAS_IPV6__,,$${UCLIBC_CONFIG_FILE},IPv6 support) ;\
  329. $(call check_uclibc_feature,__UCLIBC_HAS_RPC__,BR2_TOOLCHAIN_HAS_NATIVE_RPC,$${UCLIBC_CONFIG_FILE},RPC support) ;\
  330. $(call check_uclibc_feature,__UCLIBC_HAS_LOCALE__,BR2_ENABLE_LOCALE,$${UCLIBC_CONFIG_FILE},Locale support) ;\
  331. $(call check_uclibc_feature,__UCLIBC_HAS_WCHAR__,BR2_USE_WCHAR,$${UCLIBC_CONFIG_FILE},Wide char support) ;\
  332. $(call check_uclibc_feature,__UCLIBC_HAS_THREADS__,BR2_TOOLCHAIN_HAS_THREADS,$${UCLIBC_CONFIG_FILE},Thread support) ;\
  333. $(call check_uclibc_feature,__PTHREADS_DEBUG_SUPPORT__,BR2_TOOLCHAIN_HAS_THREADS_DEBUG,$${UCLIBC_CONFIG_FILE},Thread debugging support) ;\
  334. $(call check_uclibc_feature,__UCLIBC_HAS_THREADS_NATIVE__,BR2_TOOLCHAIN_HAS_THREADS_NPTL,$${UCLIBC_CONFIG_FILE},NPTL thread support) ;\
  335. $(call check_uclibc_feature,__UCLIBC_HAS_SSP__,BR2_TOOLCHAIN_HAS_SSP,$${UCLIBC_CONFIG_FILE},Stack Smashing Protection support)
  336. #
  337. # Check that the Buildroot configuration of the ABI matches the
  338. # configuration of the external toolchain.
  339. #
  340. # $1: cross-gcc path
  341. # $2: cross-readelf path
  342. #
  343. check_arm_abi = \
  344. __CROSS_CC=$(strip $1) ; \
  345. __CROSS_READELF=$(strip $2) ; \
  346. EXT_TOOLCHAIN_TARGET=`LANG=C $${__CROSS_CC} -v 2>&1 | grep ^Target | cut -f2 -d ' '` ; \
  347. if ! echo $${EXT_TOOLCHAIN_TARGET} | grep -qE 'eabi(hf)?$$' ; then \
  348. echo "External toolchain uses the unsuported OABI" ; \
  349. exit 1 ; \
  350. fi ; \
  351. if ! echo 'int main(void) {}' | $${__CROSS_CC} -x c -o $(BUILD_DIR)/.br-toolchain-test.tmp - ; then \
  352. rm -f $(BUILD_DIR)/.br-toolchain-test.tmp*; \
  353. abistr_$(BR2_ARM_EABI)='EABI'; \
  354. abistr_$(BR2_ARM_EABIHF)='EABIhf'; \
  355. echo "Incorrect ABI setting: $${abistr_y} selected, but toolchain is incompatible"; \
  356. exit 1 ; \
  357. fi ; \
  358. rm -f $(BUILD_DIR)/.br-toolchain-test.tmp*
  359. #
  360. # Check that the external toolchain supports C++
  361. #
  362. # $1: cross-g++ path
  363. #
  364. check_cplusplus = \
  365. __CROSS_CXX=$(strip $1) ; \
  366. $${__CROSS_CXX} -v > /dev/null 2>&1 ; \
  367. if test $$? -ne 0 ; then \
  368. echo "C++ support is selected but is not available in external toolchain" ; \
  369. exit 1 ; \
  370. fi
  371. #
  372. # Check that the cross-compiler given in the configuration exists
  373. #
  374. # $1: cross-gcc path
  375. #
  376. check_cross_compiler_exists = \
  377. __CROSS_CC=$(strip $1) ; \
  378. $${__CROSS_CC} -v > /dev/null 2>&1 ; \
  379. if test $$? -ne 0 ; then \
  380. echo "Cannot execute cross-compiler '$${__CROSS_CC}'" ; \
  381. exit 1 ; \
  382. fi
  383. #
  384. # Check for toolchains known not to work with Buildroot. For now, we
  385. # only check for Angstrom toolchains, by looking at the vendor part of
  386. # the host tuple.
  387. #
  388. # $1: cross-gcc path
  389. #
  390. check_unusable_toolchain = \
  391. __CROSS_CC=$(strip $1) ; \
  392. vendor=`$${__CROSS_CC} -dumpmachine | cut -f2 -d'-'` ; \
  393. if test "$${vendor}" = "angstrom" ; then \
  394. echo "Angstrom toolchains are not pure toolchains: they contain" ; \
  395. echo "many other libraries than just the C library, which makes" ; \
  396. echo "them unsuitable as external toolchains for build systems" ; \
  397. echo "such as Buildroot." ; \
  398. exit 1 ; \
  399. fi; \
  400. with_sysroot=`$${__CROSS_CC} -v 2>&1 |sed -r -e '/.* --with-sysroot=([^[:space:]]+)[[:space:]].*/!d; s//\1/'`; \
  401. if test "$${with_sysroot}" = "/" ; then \
  402. echo "Distribution toolchains are unsuitable for use by Buildroot," ; \
  403. echo "as they were configured in a way that makes them non-relocatable,"; \
  404. echo "and contain a lot of pre-built libraries that would conflict with"; \
  405. echo "the ones Buildroot wants to build."; \
  406. exit 1; \
  407. fi
  408. #
  409. # Generate gdbinit file for use with Buildroot
  410. #
  411. gen_gdbinit_file = \
  412. mkdir -p $(STAGING_DIR)/usr/share/buildroot/ ; \
  413. echo "set sysroot $(STAGING_DIR)" > $(STAGING_DIR)/usr/share/buildroot/gdbinit