helpers.mk 15 KB

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