2
1

helpers.mk 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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 pattern (can include glob wildcards)
  9. #
  10. copy_toolchain_lib_root = \
  11. LIBPATTERN="$(strip $1)"; \
  12. LIBPATHS=`find $(STAGING_DIR)/ -name "$${LIBPATTERN}" 2>/dev/null` ; \
  13. for LIBPATH in $${LIBPATHS} ; do \
  14. while true ; do \
  15. LIBNAME=`basename $${LIBPATH}`; \
  16. DESTDIR=`echo $${LIBPATH} | sed "s,^$(STAGING_DIR)/,," | xargs dirname` ; \
  17. mkdir -p $(TARGET_DIR)/$${DESTDIR}; \
  18. rm -fr $(TARGET_DIR)/$${DESTDIR}/$${LIBNAME}; \
  19. if test -h $${LIBPATH} ; then \
  20. cp -d $${LIBPATH} $(TARGET_DIR)/$${DESTDIR}/$${LIBNAME}; \
  21. OLD_LIBPATH="$${LIBPATH}"; \
  22. LIBPATH="`readlink -f $${LIBPATH}`"; \
  23. if [ "$${LIBPATH}" = "" ]; then \
  24. echo "LIBPATH empty after trying to resolve symlink $${OLD_LIBPATH}" 1>&2; \
  25. exit 1; \
  26. fi; \
  27. elif test -f $${LIBPATH}; then \
  28. $(INSTALL) -D -m0755 $${LIBPATH} $(TARGET_DIR)/$${DESTDIR}/$${LIBNAME}; \
  29. break ; \
  30. else \
  31. exit -1; \
  32. fi; \
  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, usr and usr/'lib' from the
  41. # sysroot of the selected architecture variant (as pointed to by
  42. # ARCH_SYSROOT_DIR). This allows to import into the staging directory
  43. # the C library and companion libraries for the correct architecture
  44. # variant. 'lib' may not be literally 'lib' but could be something else,
  45. # e.g. lib32-fp (as determined by ARCH_LIB_DIR) and we only want to copy
  46. # that lib directory and no other. When copying usr, we therefore need
  47. # to be extra careful not to include usr/lib* but we _do_ want to
  48. # include usr/libexec.
  49. # We are selective in the directories we copy since other directories
  50. # might exist for other architecture variants (on Codesourcery
  51. # toolchain, the sysroot for the default architecture variant contains
  52. # the armv4t and thumb2 subdirectories, which are the sysroot for the
  53. # corresponding architecture variants), and we don't want to import
  54. # them.
  55. #
  56. # If ARCH_LIB_DIR is not a singular directory component, e.g.
  57. # 'lib32/octeon2', then symbolic links in ARCH_LIB_DIR and
  58. # usr/ARCH_LIB_DIR may be broken because Buildroot will flatten the
  59. # directory structure (e.g. lib32/octeon2/foo is actually stored in
  60. # lib/foo). This is only relevant for links that contain one or more ../
  61. # components, as links to the current directory are always fine.
  62. # We need to fix the broken links by removing the right amount of ../
  63. # dots from the link destination.
  64. # Once the link destination is valid again, it can be simplified to
  65. # remove the dependency on intermediate directory symlinks.
  66. #
  67. # It is possible that ARCH_LIB_DIR does not contain the dynamic loader
  68. # (ld*.so or similar) because it (or the main symlink to it) normally
  69. # resides in /lib while ARCH_LIB_DIR may be something else (e.g. lib64,
  70. # lib/<tuple>, ...). Therefore, copy the dynamic loader separately.
  71. #
  72. # Then, if the selected architecture variant is not the default one
  73. # (i.e, if SYSROOT_DIR != ARCH_SYSROOT_DIR), then we :
  74. #
  75. # * Import the header files from the default architecture
  76. # variant. Header files are typically shared between the sysroots
  77. # for the different architecture variants. If we use the
  78. # non-default one, header files were not copied by the previous
  79. # step, so we copy them here from the sysroot of the default
  80. # architecture variant.
  81. #
  82. # * Create a symbolic link that matches the name of the subdirectory
  83. # for the architecture variant in the original sysroot. This is
  84. # required as the compiler will by default look in
  85. # sysroot_dir/arch_variant/ for libraries and headers, when the
  86. # non-default architecture variant is used. Without this, the
  87. # compiler fails to find libraries and headers.
  88. #
  89. # Some toolchains (i.e Linaro binary toolchains) store support
  90. # libraries (libstdc++, libgcc_s) outside of the sysroot, so we simply
  91. # copy all the libraries from the "support lib directory" into our
  92. # sysroot.
  93. #
  94. # Note that the 'locale' directories are not copied. They are huge
  95. # (400+MB) in CodeSourcery toolchains, and they are not really useful.
  96. #
  97. # $1: main sysroot directory of the toolchain
  98. # $2: arch specific sysroot directory of the toolchain
  99. # $3: arch specific subdirectory in the sysroot
  100. # $4: directory of libraries ('lib', 'lib32' or 'lib64')
  101. # $5: support lib directories (for toolchains storing libgcc_s,
  102. # libstdc++ and other gcc support libraries outside of the
  103. # sysroot)
  104. copy_toolchain_sysroot = \
  105. SYSROOT_DIR="$(strip $1)"; \
  106. ARCH_SYSROOT_DIR="$(strip $2)"; \
  107. ARCH_SUBDIR="$(strip $3)"; \
  108. ARCH_LIB_DIR="$(strip $4)" ; \
  109. SUPPORT_LIB_DIR="$(strip $5)" ; \
  110. for i in etc $${ARCH_LIB_DIR} sbin usr usr/$${ARCH_LIB_DIR}; do \
  111. if [ ! -d $${ARCH_SYSROOT_DIR}/$$i ] ; then \
  112. continue ; \
  113. fi ; \
  114. if [ "$$i" = "usr" ]; then \
  115. rsync -au --chmod=u=rwX,go=rX --exclude 'locale/' \
  116. --include '/libexec*/' --exclude '/lib*/' \
  117. $${ARCH_SYSROOT_DIR}/$$i/ $(STAGING_DIR)/$$i/ ; \
  118. else \
  119. rsync -au --chmod=u=rwX,go=rX --exclude 'locale/' \
  120. $${ARCH_SYSROOT_DIR}/$$i/ $(STAGING_DIR)/$$i/ ; \
  121. fi ; \
  122. done ; \
  123. for link in $$(find $(STAGING_DIR) -type l); do \
  124. target=$$(readlink $${link}) ; \
  125. if [ "$${target}" == "$${target$(SHARP_SIGN)/}" ] ; then \
  126. continue ; \
  127. fi ; \
  128. relpath="$(call relpath_prefix,$${target$(SHARP_SIGN)/})" ; \
  129. echo "Fixing symlink $${link} from $${target} to $${relpath}$${target$(SHARP_SIGN)/}" ; \
  130. ln -sf $${relpath}$${target$(SHARP_SIGN)/} $${link} ; \
  131. done ; \
  132. relpath="$(call relpath_prefix,$${ARCH_LIB_DIR})" ; \
  133. if [ "$${relpath}" != "" ]; then \
  134. for i in $$(find -H $(STAGING_DIR)/$${ARCH_LIB_DIR} $(STAGING_DIR)/usr/$${ARCH_LIB_DIR} -type l -xtype l); do \
  135. LINKTARGET=`readlink $$i` ; \
  136. NEWLINKTARGET=$${LINKTARGET\#$$relpath} ; \
  137. ln -sf $${NEWLINKTARGET} $$i ; \
  138. $(call simplify_symlink,$$i,$(STAGING_DIR)) ; \
  139. done ; \
  140. fi ; \
  141. if [[ ! $$(find $(STAGING_DIR)/lib -name 'ld*.so.*' -print -quit) ]]; then \
  142. find $${ARCH_SYSROOT_DIR}/lib -name 'ld*.so.*' -print0 | xargs -0 -I % cp % $(STAGING_DIR)/lib/; \
  143. fi ; \
  144. if [ `readlink -f $${SYSROOT_DIR}` != `readlink -f $${ARCH_SYSROOT_DIR}` ] ; then \
  145. if [ ! -d $${ARCH_SYSROOT_DIR}/usr/include ] ; then \
  146. cp -a $${SYSROOT_DIR}/usr/include $(STAGING_DIR)/usr ; \
  147. fi ; \
  148. mkdir -p `dirname $(STAGING_DIR)/$${ARCH_SUBDIR}` ; \
  149. relpath="$(call relpath_prefix,$${ARCH_SUBDIR})./" ; \
  150. ln -s $${relpath} $(STAGING_DIR)/$${ARCH_SUBDIR} ; \
  151. echo "Symlinking $(STAGING_DIR)/$${ARCH_SUBDIR} -> $${relpath}" ; \
  152. fi ; \
  153. if test -n "$${SUPPORT_LIB_DIR}" ; then \
  154. cp -a $${SUPPORT_LIB_DIR}/* $(STAGING_DIR)/lib/ ; \
  155. fi ; \
  156. find $(STAGING_DIR) -type d -print0 | xargs -0 chmod 755
  157. #
  158. # Check the specified kernel headers version actually matches the
  159. # version in the toolchain.
  160. #
  161. # $1: build directory
  162. # $2: sysroot directory
  163. # $3: kernel version string, in the form: X.Y
  164. # $4: test to do for the latest kernel version, 'strict' or 'loose'
  165. # always 'strict' if this is not the latest version.
  166. #
  167. check_kernel_headers_version = \
  168. if ! support/scripts/check-kernel-headers.sh $(1) $(2) $(3) \
  169. $(if $(BR2_TOOLCHAIN_HEADERS_LATEST),$(4),strict); \
  170. then \
  171. exit 1; \
  172. fi
  173. #
  174. # Check the specific gcc version actually matches the version in the
  175. # toolchain
  176. #
  177. # $1: path to gcc
  178. # $2: expected gcc version
  179. #
  180. check_gcc_version = \
  181. expected_version="$(strip $2)" ; \
  182. if [ -z "$${expected_version}" ]; then \
  183. exit 0 ; \
  184. fi; \
  185. real_version=`$(1) -dumpversion` ; \
  186. if [[ ! "$${real_version}." =~ ^$${expected_version}\. ]] ; then \
  187. printf "Incorrect selection of gcc version: expected %s.x, got %s\n" \
  188. "$${expected_version}" "$${real_version}" ; \
  189. exit 1 ; \
  190. fi
  191. #
  192. # Check the availability of a particular glibc feature. This function
  193. # is used to check toolchain options that are always supported by
  194. # glibc, so we simply check that the corresponding option is properly
  195. # enabled.
  196. #
  197. # $1: Buildroot option name
  198. # $2: feature description
  199. #
  200. check_glibc_feature = \
  201. if [ "$($(1))" != "y" ] ; then \
  202. echo "$(2) available in C library, please enable $(1)" ; \
  203. exit 1 ; \
  204. fi
  205. #
  206. # Check the availability of RPC support in a glibc toolchain
  207. #
  208. # $1: sysroot directory
  209. #
  210. check_glibc_rpc_feature = \
  211. IS_IN_LIBC=`test -f $(1)/usr/include/rpc/rpc.h && echo y` ; \
  212. if [ "$(BR2_TOOLCHAIN_HAS_NATIVE_RPC)" != "y" -a "$${IS_IN_LIBC}" = "y" ] ; then \
  213. echo "RPC support available in C library, please enable BR2_TOOLCHAIN_EXTERNAL_INET_RPC" ; \
  214. exit 1 ; \
  215. fi ; \
  216. if [ "$(BR2_TOOLCHAIN_HAS_NATIVE_RPC)" = "y" -a "$${IS_IN_LIBC}" != "y" ] ; then \
  217. echo "RPC support not available in C library, please disable BR2_TOOLCHAIN_EXTERNAL_INET_RPC" ; \
  218. exit 1 ; \
  219. fi
  220. #
  221. # Check the correctness of a glibc external toolchain configuration.
  222. # 1. Check that the C library selected in Buildroot matches the one
  223. # of the external toolchain
  224. # 2. Check that all the C library-related features are enabled in the
  225. # config, since glibc always supports all of them
  226. #
  227. # $1: sysroot directory
  228. #
  229. check_glibc = \
  230. SYSROOT_DIR="$(strip $1)"; \
  231. if test `find -L $${SYSROOT_DIR}/ -maxdepth 2 -name 'ld-linux*.so.*' -o -name 'ld.so.*' -o -name 'ld64.so.*' | wc -l` -eq 0 ; then \
  232. echo "Incorrect selection of the C library"; \
  233. exit -1; \
  234. fi; \
  235. $(call check_glibc_feature,BR2_USE_MMU,MMU support) ;\
  236. $(call check_glibc_rpc_feature,$${SYSROOT_DIR})
  237. #
  238. # Check that the selected C library really is musl
  239. #
  240. # $1: cross-gcc path
  241. # $2: cross-readelf path
  242. check_musl = \
  243. __CROSS_CC=$(strip $1) ; \
  244. libc_a_path=`$${__CROSS_CC} -print-file-name=libc.a` ; \
  245. if ! strings $${libc_a_path} | grep -q MUSL_LOCPATH ; then \
  246. echo "Incorrect selection of the C library" ; \
  247. exit -1; \
  248. fi
  249. #
  250. # Check the conformity of Buildroot configuration with regard to the
  251. # uClibc configuration of the external toolchain, for a particular
  252. # feature.
  253. #
  254. # If 'Buildroot option name' ($2) is empty it means the uClibc option
  255. # is mandatory.
  256. #
  257. # $1: uClibc macro name
  258. # $2: Buildroot option name
  259. # $3: uClibc config file
  260. # $4: feature description
  261. #
  262. check_uclibc_feature = \
  263. IS_IN_LIBC=`grep -q "\#define $(1) 1" $(3) && echo y` ; \
  264. if [ -z "$(2)" ] ; then \
  265. if [ "$${IS_IN_LIBC}" != "y" ] ; then \
  266. echo "$(4) not available in C library, toolchain unsuitable for Buildroot" ; \
  267. exit 1 ; \
  268. fi ; \
  269. else \
  270. if [ "$($(2))" != "y" -a "$${IS_IN_LIBC}" = "y" ] ; then \
  271. echo "$(4) available in C library, please enable $(2)" ; \
  272. exit 1 ; \
  273. fi ; \
  274. if [ "$($(2))" = "y" -a "$${IS_IN_LIBC}" != "y" ] ; then \
  275. echo "$(4) not available in C library, please disable $(2)" ; \
  276. exit 1 ; \
  277. fi ; \
  278. fi
  279. #
  280. # Check the correctness of a uclibc external toolchain configuration
  281. # 1. Check that the C library selected in Buildroot matches the one
  282. # of the external toolchain
  283. # 2. Check that the features enabled in the Buildroot configuration
  284. # match the features available in the uClibc of the external
  285. # toolchain
  286. #
  287. # $1: sysroot directory
  288. #
  289. check_uclibc = \
  290. SYSROOT_DIR="$(strip $1)"; \
  291. if ! test -f $${SYSROOT_DIR}/usr/include/bits/uClibc_config.h ; then \
  292. echo "Incorrect selection of the C library"; \
  293. exit -1; \
  294. fi; \
  295. UCLIBC_CONFIG_FILE=$${SYSROOT_DIR}/usr/include/bits/uClibc_config.h ; \
  296. $(call check_uclibc_feature,__ARCH_USE_MMU__,BR2_USE_MMU,$${UCLIBC_CONFIG_FILE},MMU support) ;\
  297. $(call check_uclibc_feature,__UCLIBC_HAS_LFS__,,$${UCLIBC_CONFIG_FILE},Large file support) ;\
  298. $(call check_uclibc_feature,__UCLIBC_HAS_IPV6__,,$${UCLIBC_CONFIG_FILE},IPv6 support) ;\
  299. $(call check_uclibc_feature,__UCLIBC_HAS_RPC__,BR2_TOOLCHAIN_HAS_NATIVE_RPC,$${UCLIBC_CONFIG_FILE},RPC support) ;\
  300. $(call check_uclibc_feature,__UCLIBC_HAS_XLOCALE__,BR2_ENABLE_LOCALE,$${UCLIBC_CONFIG_FILE},Locale support) ;\
  301. $(call check_uclibc_feature,__UCLIBC_HAS_WCHAR__,BR2_USE_WCHAR,$${UCLIBC_CONFIG_FILE},Wide char support) ;\
  302. $(call check_uclibc_feature,__UCLIBC_HAS_THREADS__,BR2_TOOLCHAIN_HAS_THREADS,$${UCLIBC_CONFIG_FILE},Thread support) ;\
  303. $(call check_uclibc_feature,__PTHREADS_DEBUG_SUPPORT__,BR2_TOOLCHAIN_HAS_THREADS_DEBUG,$${UCLIBC_CONFIG_FILE},Thread debugging support) ;\
  304. $(call check_uclibc_feature,__UCLIBC_HAS_THREADS_NATIVE__,BR2_TOOLCHAIN_HAS_THREADS_NPTL,$${UCLIBC_CONFIG_FILE},NPTL thread support)
  305. #
  306. # Check that the Buildroot configuration of the ABI matches the
  307. # configuration of the external toolchain.
  308. #
  309. # $1: cross-gcc path
  310. # $2: cross-readelf path
  311. #
  312. check_arm_abi = \
  313. __CROSS_CC=$(strip $1) ; \
  314. EXT_TOOLCHAIN_TARGET=`LANG=C $${__CROSS_CC} -v 2>&1 | grep ^Target | cut -f2 -d ' '` ; \
  315. if ! echo $${EXT_TOOLCHAIN_TARGET} | grep -qE 'eabi(hf)?$$' ; then \
  316. echo "External toolchain uses the unsupported OABI" ; \
  317. exit 1 ; \
  318. fi ; \
  319. if ! echo 'int main(void) {}' | $${__CROSS_CC} -x c -o $(BUILD_DIR)/.br-toolchain-test.tmp - ; then \
  320. rm -f $(BUILD_DIR)/.br-toolchain-test.tmp*; \
  321. abistr_$(BR2_ARM_EABI)='EABI'; \
  322. abistr_$(BR2_ARM_EABIHF)='EABIhf'; \
  323. echo "Incorrect ABI setting: $${abistr_y} selected, but toolchain is incompatible"; \
  324. exit 1 ; \
  325. fi ; \
  326. rm -f $(BUILD_DIR)/.br-toolchain-test.tmp*
  327. #
  328. # Check that the external toolchain supports C++
  329. #
  330. # $1: cross-g++ path
  331. #
  332. check_cplusplus = \
  333. __CROSS_CXX=$(strip $1) ; \
  334. __HAS_CXX=`$${__CROSS_CXX} -v > /dev/null 2>&1 && echo y`; \
  335. if [ "$${__HAS_CXX}" != "y" -a "$(BR2_INSTALL_LIBSTDCPP)" = y ] ; then \
  336. echo "C++ support is selected but is not available in external toolchain" ; \
  337. exit 1 ; \
  338. elif [ "$${__HAS_CXX}" = "y" -a "$(BR2_INSTALL_LIBSTDCPP)" != y ] ; then \
  339. echo "C++ support is not selected but is available in external toolchain" ; \
  340. exit 1 ; \
  341. fi \
  342. #
  343. #
  344. # Check that the external toolchain supports D language
  345. #
  346. # $1: cross-gdc path
  347. #
  348. check_dlang = \
  349. __CROSS_GDC=$(strip $1) ; \
  350. __o=$(BUILD_DIR)/.br-toolchain-test-dlang.tmp ; \
  351. __HAS_DLANG=`printf 'import std.stdio;\nvoid main() { writeln("Hello World!"); }\n' | \
  352. $${__CROSS_GDC} -x d -o $${__o} - >/dev/null 2>&1 && echo y`; \
  353. rm -f $${__o}* ; \
  354. if [ "$${__HAS_DLANG}" != "y" -a "$(BR2_TOOLCHAIN_HAS_DLANG)" = y ] ; then \
  355. echo "D language support is selected but is not available in external toolchain" ; \
  356. exit 1 ; \
  357. elif [ "$${__HAS_DLANG}" = "y" -a "$(BR2_TOOLCHAIN_HAS_DLANG)" != y ] ; then \
  358. echo "D language support is not selected but is available in external toolchain" ; \
  359. exit 1 ; \
  360. fi \
  361. #
  362. #
  363. # Check that the external toolchain supports Fortran
  364. #
  365. # $1: cross-gfortran path
  366. #
  367. check_fortran = \
  368. __CROSS_FC=$(strip $1) ; \
  369. __o=$(BUILD_DIR)/.br-toolchain-test-fortran.tmp ; \
  370. __HAS_FORTRAN=`printf 'program hello\n\tprint *, "Hello Fortran!\\\n"\nend program hello\n' | \
  371. $${__CROSS_FC} -x f95 -ffree-form -o $${__o} - 2>/dev/null && echo y`; \
  372. rm -f $${__o}* ; \
  373. if [ "$${__HAS_FORTRAN}" != "y" -a "$(BR2_TOOLCHAIN_HAS_FORTRAN)" = y ] ; then \
  374. echo "Fortran support is selected but is not available in external toolchain" ; \
  375. exit 1 ; \
  376. elif [ "$${__HAS_FORTRAN}" = "y" -a "$(BR2_TOOLCHAIN_HAS_FORTRAN)" != y ] ; then \
  377. echo "Fortran support is not selected but is available in external toolchain" ; \
  378. exit 1 ; \
  379. fi \
  380. #
  381. #
  382. # Check that the external toolchain supports OpenMP
  383. #
  384. # $1: cross-gcc path
  385. #
  386. check_openmp = \
  387. __CROSS_CC=$(strip $1) ; \
  388. __o=$(BUILD_DIR)/.br-toolchain-test-openmp.tmp ; \
  389. __HAS_OPENMP=`printf '\#include <omp.h>\nint main(void) { return omp_get_thread_num(); }' | \
  390. $${__CROSS_CC} -fopenmp -x c -o $${__o} - >/dev/null 2>&1 && echo y` ; \
  391. rm -f $${__o}* ; \
  392. if [ "$${__HAS_OPENMP}" != "y" -a "$(BR2_TOOLCHAIN_HAS_OPENMP)" = y ] ; then \
  393. echo "OpenMP support is selected but is not available in external toolchain"; \
  394. exit 1 ; \
  395. elif [ "$${__HAS_OPENMP}" = "y" -a "$(BR2_TOOLCHAIN_HAS_OPENMP)" != y ] ; then \
  396. echo "OpenMP support is not selected but is available in external toolchain"; \
  397. exit 1 ; \
  398. fi \
  399. #
  400. # Check that the cross-compiler given in the configuration exists
  401. #
  402. # $1: cross-gcc path
  403. #
  404. check_cross_compiler_exists = \
  405. __CROSS_CC=$(strip $1) ; \
  406. $${__CROSS_CC} -v > /dev/null 2>&1 ; \
  407. if test $$? -ne 0 ; then \
  408. echo "Cannot execute cross-compiler '$${__CROSS_CC}'" ; \
  409. exit 1 ; \
  410. fi
  411. #
  412. # Check for toolchains known not to work with Buildroot.
  413. # - For the Angstrom toolchains, we check by looking at the vendor part of
  414. # the host tuple.
  415. # - Exclude distro-class toolchains which are not relocatable.
  416. # - Exclude broken toolchains which return "libc.a" with -print-file-name.
  417. # - Exclude toolchains used with wrong toolchain cflags or broken toolchains
  418. # which return "libc.a" with -print-file-name and toolchain cflags.
  419. # - Exclude toolchains which doesn't support --sysroot option.
  420. #
  421. # $1: cross-gcc path
  422. # $1: toolchain cflags
  423. #
  424. check_unusable_toolchain = \
  425. __CROSS_CC=$(strip $1) ; \
  426. __TOOLCHAIN_CFLAGS=$(strip $2) ; \
  427. vendor=`$${__CROSS_CC} -dumpmachine | cut -f2 -d'-'` ; \
  428. if test "$${vendor}" = "angstrom" ; then \
  429. echo "Angstrom toolchains are not pure toolchains: they contain" ; \
  430. echo "many other libraries than just the C library, which makes" ; \
  431. echo "them unsuitable as external toolchains for build systems" ; \
  432. echo "such as Buildroot." ; \
  433. exit 1 ; \
  434. fi; \
  435. with_sysroot=`$${__CROSS_CC} -v 2>&1 |sed -r -e '/.* --with-sysroot=([^[:space:]]+)[[:space:]].*/!d; s//\1/'`; \
  436. if test "$${with_sysroot}" = "/" ; then \
  437. echo "Distribution toolchains are unsuitable for use by Buildroot," ; \
  438. echo "as they were configured in a way that makes them non-relocatable,"; \
  439. echo "and contain a lot of pre-built libraries that would conflict with"; \
  440. echo "the ones Buildroot wants to build."; \
  441. exit 1; \
  442. fi; \
  443. libc_a_path=`$${__CROSS_CC} -print-file-name=libc.a` ; \
  444. if test "$${libc_a_path}" = "libc.a" ; then \
  445. echo "Unable to detect the toolchain sysroot, Buildroot cannot use this toolchain." ; \
  446. exit 1 ; \
  447. fi ; \
  448. libc_a_archsysroot_path=`$${__CROSS_CC} $${__TOOLCHAIN_CFLAGS} -print-file-name=libc.a` ; \
  449. if test "$${libc_a_archsysroot_path}" = "libc.a" ; then \
  450. echo "Unable to detect the toolchain architecture sysroot." ; \
  451. echo "Please check the Target Architecture Variant selected, the toolchains may not support it." ; \
  452. echo "Buildroot cannot use this toolchain." ; \
  453. exit 1 ; \
  454. fi; \
  455. sysroot_dir="$(call toolchain_find_sysroot,$${__CROSS_CC})" ; \
  456. if test -z "$${sysroot_dir}" ; then \
  457. echo "External toolchain doesn't support --sysroot. Cannot use." ; \
  458. exit 1 ; \
  459. fi
  460. #
  461. # Check if the toolchain has SSP (stack smashing protector) support
  462. #
  463. # $1: cross-gcc path
  464. # $2: gcc ssp option
  465. #
  466. check_toolchain_ssp = \
  467. __CROSS_CC=$(strip $1) ; \
  468. __HAS_SSP=`echo 'int main(){}' | $${__CROSS_CC} -Werror -fstack-protector -x c - -o $(BUILD_DIR)/.br-toolchain-test.tmp >/dev/null 2>&1 && echo y` ; \
  469. if [ "$(BR2_TOOLCHAIN_HAS_SSP)" != "y" -a "$${__HAS_SSP}" = "y" ] ; then \
  470. echo "SSP support available in this toolchain, please enable BR2_TOOLCHAIN_EXTERNAL_HAS_SSP" ; \
  471. exit 1 ; \
  472. fi ; \
  473. if [ "$(BR2_TOOLCHAIN_HAS_SSP)" = "y" -a "$${__HAS_SSP}" != "y" ] ; then \
  474. echo "SSP support not available in this toolchain, please disable BR2_TOOLCHAIN_EXTERNAL_HAS_SSP" ; \
  475. exit 1 ; \
  476. fi ; \
  477. __SSP_OPTION=$(2); \
  478. if [ -n "$${__SSP_OPTION}" ] ; then \
  479. if ! echo 'int main(){}' | $${__CROSS_CC} -Werror $${__SSP_OPTION} -x c - -o $(BUILD_DIR)/.br-toolchain-test.tmp >/dev/null 2>&1 ; then \
  480. echo "SSP option $${__SSP_OPTION} not available in this toolchain, please select another SSP level" ; \
  481. exit 1 ; \
  482. fi; \
  483. fi; \
  484. rm -f $(BUILD_DIR)/.br-toolchain-test.tmp*
  485. #
  486. # Generate gdbinit file for use with Buildroot
  487. #
  488. gen_gdbinit_file = \
  489. mkdir -p $(STAGING_DIR)/usr/share/buildroot/ ; \
  490. echo "add-auto-load-safe-path $(STAGING_DIR)" > $(STAGING_DIR)/usr/share/buildroot/gdbinit ; \
  491. echo "set sysroot $(STAGING_DIR)" >> $(STAGING_DIR)/usr/share/buildroot/gdbinit
  492. # Given a path, determine the relative prefix (../) needed to return to the
  493. # root level. Note that the last component is treated as a file component; use a
  494. # trailing slash to force treating it as a directory. Examples:
  495. # relpath_prefix(lib32) = ""
  496. # relpath_prefix(lib32/octeon2) = "../"
  497. # relpath_prefix(lib32/octeon2/) = "../../"
  498. #
  499. # $1: input path
  500. define relpath_prefix
  501. $$( \
  502. prefix="" ; \
  503. nbslashs=`printf $1 | sed 's%[^/]%%g' | wc -c` ; \
  504. for slash in `seq 1 $${nbslashs}` ; do \
  505. prefix=$${prefix}"../" ; \
  506. done ; \
  507. printf "$$prefix" ; \
  508. )
  509. endef
  510. # Replace the destination of a symbolic link with a simpler version
  511. # For example,
  512. # usr/lib/libfoo.so -> ../../lib32/libfoo.so.1
  513. # becomes
  514. # usr/lib/libfoo.so -> ../../lib/libfoo.so.1
  515. # since 'lib32' is a symlink to 'lib'.
  516. #
  517. # Likewise,
  518. # usr/lib/octeon2/libbar.so -> ../../../lib32/octeon2/libbar.so.1
  519. # becomes
  520. # usr/lib/octeon2/libbar.so -> ../../lib/libbar.so.1
  521. # assuming lib32->lib and lib/octeon2->lib.
  522. #
  523. # $1: symlink
  524. # $2: base path
  525. define simplify_symlink
  526. ( \
  527. FULL_SRC="$$(readlink -f $$(dirname $1))/$$(basename $1)" ; \
  528. FULL_DEST="$$(readlink -f $1)" ; \
  529. FULL_BASE="$$(readlink -f $2)" ; \
  530. REL_SRC="$${FULL_SRC#$${FULL_BASE}/}" ; \
  531. REL_DEST="$${FULL_DEST#$${FULL_BASE}/}" ; \
  532. DOTS="$(call relpath_prefix,$${REL_SRC})" ; \
  533. ln -sf "$${DOTS}$${REL_DEST}" "$${FULL_SRC}" ; \
  534. )
  535. endef