pkg-toolchain-external.mk 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. ################################################################################
  2. # External toolchain package infrastructure
  3. #
  4. # This package infrastructure implements the support for external
  5. # toolchains, i.e toolchains that are available pre-built, ready to
  6. # use. Such toolchain may either be readily available on the Web
  7. # (Linaro, Sourcery CodeBench, from processor vendors) or may be built
  8. # with tools like Crosstool-NG or Buildroot itself. So far, we have
  9. # tested this with:
  10. #
  11. # * Toolchains generated by Crosstool-NG
  12. # * Toolchains generated by Buildroot
  13. # * Toolchains provided by Linaro for the ARM and AArch64
  14. # architectures
  15. # * Sourcery CodeBench toolchains (from Mentor Graphics) for the ARM,
  16. # MIPS, PowerPC, and x86_64 architectures. For the MIPS
  17. # toolchain, the -muclibc variant isn't supported yet, only the
  18. # default glibc-based variant is.
  19. # * Synopsys DesignWare toolchains for ARC cores
  20. #
  21. # The basic principle is the following
  22. #
  23. # 1. If the toolchain is not pre-installed, download and extract it
  24. # in $(TOOLCHAIN_EXTERNAL_INSTALL_DIR). Otherwise,
  25. # $(TOOLCHAIN_EXTERNAL_INSTALL_DIR) points to were the toolchain has
  26. # already been installed by the user.
  27. #
  28. # 2. For all external toolchains, perform some checks on the
  29. # conformity between the toolchain configuration described in the
  30. # Buildroot menuconfig system, and the real configuration of the
  31. # external toolchain. This is for example important to make sure that
  32. # the Buildroot configuration system knows whether the toolchain
  33. # supports RPC, IPv6, locales, large files, etc. Unfortunately, these
  34. # things cannot be detected automatically, since the value of these
  35. # options (such as BR2_TOOLCHAIN_HAS_NATIVE_RPC) are needed at
  36. # configuration time because these options are used as dependencies
  37. # for other options. And at configuration time, we are not able to
  38. # retrieve the external toolchain configuration.
  39. #
  40. # 3. Copy the libraries needed at runtime to the target directory,
  41. # $(TARGET_DIR). Obviously, things such as the C library, the dynamic
  42. # loader and a few other utility libraries are needed if dynamic
  43. # applications are to be executed on the target system.
  44. #
  45. # 4. Copy the libraries and headers to the staging directory. This
  46. # will allow all further calls to gcc to be made using --sysroot
  47. # $(STAGING_DIR), which greatly simplifies the compilation of the
  48. # packages when using external toolchains. So in the end, only the
  49. # cross-compiler binaries remains external, all libraries and headers
  50. # are imported into the Buildroot tree.
  51. #
  52. # 5. Build a toolchain wrapper which executes the external toolchain
  53. # with a number of arguments (sysroot/march/mtune/..) hardcoded,
  54. # so we're sure the correct configuration is always used and the
  55. # toolchain behaves similar to an internal toolchain.
  56. # This toolchain wrapper and symlinks are installed into
  57. # $(HOST_DIR)/bin like for the internal toolchains, and the rest
  58. # of Buildroot is handled identical for the 2 toolchain types.
  59. ################################################################################
  60. #
  61. # Definitions of where the toolchain can be found
  62. #
  63. TOOLCHAIN_EXTERNAL_PREFIX = $(call qstrip,$(BR2_TOOLCHAIN_EXTERNAL_PREFIX))
  64. TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR = $(HOST_DIR)/opt/ext-toolchain
  65. ifeq ($(BR2_TOOLCHAIN_EXTERNAL_DOWNLOAD),y)
  66. TOOLCHAIN_EXTERNAL_INSTALL_DIR = $(TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR)
  67. else
  68. TOOLCHAIN_EXTERNAL_INSTALL_DIR = $(abspath $(call qstrip,$(BR2_TOOLCHAIN_EXTERNAL_PATH)))
  69. endif
  70. ifeq ($(TOOLCHAIN_EXTERNAL_INSTALL_DIR),)
  71. ifneq ($(TOOLCHAIN_EXTERNAL_PREFIX),)
  72. # if no path set, figure it out from path
  73. TOOLCHAIN_EXTERNAL_BIN := $(dir $(shell which $(TOOLCHAIN_EXTERNAL_PREFIX)-gcc))
  74. endif
  75. else
  76. TOOLCHAIN_EXTERNAL_REL_BIN_PATH = $(call qstrip,$(BR2_TOOLCHAIN_EXTERNAL_REL_BIN_PATH))
  77. ifeq ($(TOOLCHAIN_EXTERNAL_REL_BIN_PATH),)
  78. TOOLCHAIN_EXTERNAL_REL_BIN_PATH = bin
  79. endif
  80. TOOLCHAIN_EXTERNAL_BIN = $(TOOLCHAIN_EXTERNAL_INSTALL_DIR)/$(TOOLCHAIN_EXTERNAL_REL_BIN_PATH)
  81. endif
  82. # If this is a buildroot toolchain, it already has a wrapper which we want to
  83. # bypass. Since this is only evaluated after it has been extracted, we can use
  84. # $(wildcard ...) here.
  85. TOOLCHAIN_EXTERNAL_SUFFIX = \
  86. $(if $(wildcard $(TOOLCHAIN_EXTERNAL_BIN)/*.br_real),.br_real)
  87. TOOLCHAIN_EXTERNAL_CROSS = $(TOOLCHAIN_EXTERNAL_BIN)/$(TOOLCHAIN_EXTERNAL_PREFIX)-
  88. TOOLCHAIN_EXTERNAL_CC = $(TOOLCHAIN_EXTERNAL_CROSS)gcc$(TOOLCHAIN_EXTERNAL_SUFFIX)
  89. TOOLCHAIN_EXTERNAL_CXX = $(TOOLCHAIN_EXTERNAL_CROSS)g++$(TOOLCHAIN_EXTERNAL_SUFFIX)
  90. TOOLCHAIN_EXTERNAL_GDC = $(TOOLCHAIN_EXTERNAL_CROSS)gdc$(TOOLCHAIN_EXTERNAL_SUFFIX)
  91. TOOLCHAIN_EXTERNAL_FC = $(TOOLCHAIN_EXTERNAL_CROSS)gfortran$(TOOLCHAIN_EXTERNAL_SUFFIX)
  92. TOOLCHAIN_EXTERNAL_READELF = $(TOOLCHAIN_EXTERNAL_CROSS)readelf
  93. # Normal handling of downloaded toolchain tarball extraction.
  94. ifeq ($(BR2_TOOLCHAIN_EXTERNAL_DOWNLOAD),y)
  95. # As a regular package, the toolchain gets extracted in $(@D), but
  96. # since it's actually a fairly special package, we need it to be moved
  97. # into TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR.
  98. define TOOLCHAIN_EXTERNAL_MOVE
  99. rm -rf $(TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR)
  100. mkdir -p $(TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR)
  101. mv $(@D)/* $(TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR)/
  102. endef
  103. endif
  104. #
  105. # Definitions of the list of libraries that should be copied to the target.
  106. #
  107. TOOLCHAIN_EXTERNAL_LIBS += ld*.so.* libgcc_s.so.* libatomic.so.*
  108. ifneq ($(BR2_SSP_NONE),y)
  109. TOOLCHAIN_EXTERNAL_LIBS += libssp.so.*
  110. endif
  111. ifeq ($(BR2_TOOLCHAIN_EXTERNAL_GLIBC)$(BR2_TOOLCHAIN_EXTERNAL_UCLIBC),y)
  112. TOOLCHAIN_EXTERNAL_LIBS += libc.so.* libdl.so.* libm.so.* libnsl.so.* libresolv.so.* librt.so.* libutil.so.*
  113. ifeq ($(BR2_TOOLCHAIN_HAS_THREADS),y)
  114. TOOLCHAIN_EXTERNAL_LIBS += libpthread.so.*
  115. ifneq ($(BR2_PACKAGE_GDB)$(BR2_TOOLCHAIN_EXTERNAL_GDB_SERVER_COPY),)
  116. TOOLCHAIN_EXTERNAL_LIBS += libthread_db.so.*
  117. endif # gdbserver
  118. endif # ! no threads
  119. endif
  120. ifeq ($(BR2_TOOLCHAIN_EXTERNAL_UCLIBC),y)
  121. # uClibc, though mono-lib, still has a separate libcrypt as a stub
  122. TOOLCHAIN_EXTERNAL_LIBS += libcrypt.so.*
  123. endif
  124. ifeq ($(BR2_TOOLCHAIN_EXTERNAL_GLIBC),y)
  125. TOOLCHAIN_EXTERNAL_LIBS += libnss_files.so.* libnss_dns.so.* libmvec.so.* libanl.so.*
  126. # Note: explicitly do copy libcrypt.so.1: it is not the same SONAME as the
  127. # one from libxcrypt, so no conflict, but some prebuilt binaries may have
  128. # it in their DT_NEEDED. However, do remove the headers, static lib, and
  129. # symlink to avoid conflict with libxcrypt (the prebuilt binaries do not
  130. # need those either).
  131. TOOLCHAIN_EXTERNAL_LIBS += libcrypt.so.1
  132. define TOOLCHAIN_EXTERNAL_GLIBC_NO_LIBCRYPT
  133. rm -f $(STAGING_DIR)/usr/include/crypt.h \
  134. $(STAGING_DIR)/usr/lib/libcrypt.so \
  135. $(STAGING_DIR)/usr/lib/libcrypt.a
  136. endef
  137. endif
  138. ifeq ($(BR2_TOOLCHAIN_EXTERNAL_MUSL),y)
  139. TOOLCHAIN_EXTERNAL_LIBS += libc.so
  140. endif
  141. ifeq ($(BR2_INSTALL_LIBSTDCPP),y)
  142. TOOLCHAIN_EXTERNAL_LIBS += libstdc++.so.*
  143. endif
  144. ifeq ($(BR2_TOOLCHAIN_HAS_FORTRAN),y)
  145. TOOLCHAIN_EXTERNAL_LIBS += libgfortran.so.*
  146. # fortran needs quadmath on x86 and x86_64
  147. ifeq ($(BR2_TOOLCHAIN_HAS_LIBQUADMATH),y)
  148. TOOLCHAIN_EXTERNAL_LIBS += libquadmath.so*
  149. endif
  150. endif
  151. ifeq ($(BR2_TOOLCHAIN_HAS_OPENMP),y)
  152. TOOLCHAIN_EXTERNAL_LIBS += libgomp.so.*
  153. endif
  154. ifeq ($(BR2_TOOLCHAIN_HAS_DLANG),y)
  155. TOOLCHAIN_EXTERNAL_LIBS += libgdruntime.so* libgphobos.so*
  156. endif
  157. TOOLCHAIN_EXTERNAL_LIBS += $(addsuffix .so*,$(call qstrip,$(BR2_TOOLCHAIN_EXTRA_LIBS)))
  158. #
  159. # Definition of the CFLAGS to use with the external toolchain, as well as the
  160. # common toolchain wrapper build arguments
  161. #
  162. # march/mtune/floating point mode needs to be passed to the external toolchain
  163. # to select the right multilib variant
  164. ifeq ($(BR2_x86_64),y)
  165. TOOLCHAIN_EXTERNAL_CFLAGS += -m64
  166. TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_64
  167. endif
  168. ifneq ($(GCC_TARGET_ARCH),)
  169. TOOLCHAIN_EXTERNAL_CFLAGS += -march=$(GCC_TARGET_ARCH)
  170. TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_ARCH='"$(GCC_TARGET_ARCH)"'
  171. endif
  172. ifneq ($(GCC_TARGET_CPU),)
  173. TOOLCHAIN_EXTERNAL_CFLAGS += -mcpu=$(GCC_TARGET_CPU)
  174. TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_CPU='"$(GCC_TARGET_CPU)"'
  175. endif
  176. ifneq ($(GCC_TARGET_ABI),)
  177. TOOLCHAIN_EXTERNAL_CFLAGS += -mabi=$(GCC_TARGET_ABI)
  178. TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_ABI='"$(GCC_TARGET_ABI)"'
  179. endif
  180. ifeq ($(BR2_TOOLCHAIN_HAS_MNAN_OPTION),y)
  181. ifneq ($(GCC_TARGET_NAN),)
  182. TOOLCHAIN_EXTERNAL_CFLAGS += -mnan=$(GCC_TARGET_NAN)
  183. TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_NAN='"$(GCC_TARGET_NAN)"'
  184. endif
  185. endif
  186. ifneq ($(GCC_TARGET_FP32_MODE),)
  187. TOOLCHAIN_EXTERNAL_CFLAGS += -mfp$(GCC_TARGET_FP32_MODE)
  188. TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_FP32_MODE='"$(GCC_TARGET_FP32_MODE)"'
  189. endif
  190. ifneq ($(GCC_TARGET_FPU),)
  191. TOOLCHAIN_EXTERNAL_CFLAGS += -mfpu=$(GCC_TARGET_FPU)
  192. TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_FPU='"$(GCC_TARGET_FPU)"'
  193. endif
  194. ifneq ($(GCC_TARGET_FLOAT_ABI),)
  195. TOOLCHAIN_EXTERNAL_CFLAGS += -mfloat-abi=$(GCC_TARGET_FLOAT_ABI)
  196. TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_FLOAT_ABI='"$(GCC_TARGET_FLOAT_ABI)"'
  197. endif
  198. ifneq ($(GCC_TARGET_SIMD),)
  199. TOOLCHAIN_EXTERNAL_CFLAGS += -msimd=$(GCC_TARGET_SIMD)
  200. TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_SIMD='"$(GCC_TARGET_SIMD)"'
  201. endif
  202. ifneq ($(GCC_TARGET_MODE),)
  203. TOOLCHAIN_EXTERNAL_CFLAGS += -m$(GCC_TARGET_MODE)
  204. TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_MODE='"$(GCC_TARGET_MODE)"'
  205. endif
  206. ifeq ($(BR2_BINFMT_FLAT),y)
  207. TOOLCHAIN_EXTERNAL_CFLAGS += -Wl,-elf2flt
  208. TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_BINFMT_FLAT
  209. endif
  210. ifeq ($(BR2_mipsel)$(BR2_mips64el),y)
  211. TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_MIPS_TARGET_LITTLE_ENDIAN
  212. TOOLCHAIN_EXTERNAL_CFLAGS += -EL
  213. endif
  214. ifeq ($(BR2_mips)$(BR2_mips64),y)
  215. TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_MIPS_TARGET_BIG_ENDIAN
  216. TOOLCHAIN_EXTERNAL_CFLAGS += -EB
  217. endif
  218. ifeq ($(BR2_arceb),y)
  219. TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_ARC_TARGET_BIG_ENDIAN
  220. TOOLCHAIN_EXTERNAL_CFLAGS += -EB
  221. endif
  222. TOOLCHAIN_EXTERNAL_CFLAGS += $(call qstrip,$(BR2_TARGET_OPTIMIZATION))
  223. ifeq ($(BR2_SOFT_FLOAT),y)
  224. TOOLCHAIN_EXTERNAL_CFLAGS += -msoft-float
  225. TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += -DBR_SOFTFLOAT=1
  226. endif
  227. TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += \
  228. -DBR_CROSS_PATH_SUFFIX='"$(TOOLCHAIN_EXTERNAL_SUFFIX)"'
  229. ifeq ($(filter $(HOST_DIR)/%,$(TOOLCHAIN_EXTERNAL_BIN)),)
  230. # TOOLCHAIN_EXTERNAL_BIN points outside HOST_DIR => absolute path
  231. TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += \
  232. -DBR_CROSS_PATH_ABS='"$(TOOLCHAIN_EXTERNAL_BIN)"'
  233. else
  234. # TOOLCHAIN_EXTERNAL_BIN points inside HOST_DIR => relative path
  235. TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS += \
  236. -DBR_CROSS_PATH_REL='"$(TOOLCHAIN_EXTERNAL_BIN:$(HOST_DIR)/%=%)"'
  237. endif
  238. #
  239. # The following functions creates the symbolic links needed to get the
  240. # cross-compilation tools visible in $(HOST_DIR)/bin. Some of
  241. # links are done directly to the corresponding tool in the external
  242. # toolchain installation directory, while some other links are done to
  243. # the toolchain wrapper (preprocessor, C, C++ and Fortran compiler)
  244. #
  245. # We skip gdb symlink when we are building our own gdb to prevent two
  246. # gdb's in $(HOST_DIR)/bin.
  247. #
  248. # The LTO support in gcc creates wrappers for ar, ranlib and nm which load
  249. # the lto plugin. These wrappers are called *-gcc-ar, *-gcc-ranlib, and
  250. # *-gcc-nm and should be used instead of the real programs when -flto is
  251. # used. However, we should not add the toolchain wrapper for them, and they
  252. # match the *cc-* pattern. Therefore, an additional case is added for *-ar,
  253. # *-ranlib and *-nm.
  254. define TOOLCHAIN_EXTERNAL_INSTALL_WRAPPER
  255. $(Q)cd $(HOST_DIR)/bin; \
  256. for i in $(TOOLCHAIN_EXTERNAL_CROSS)*; do \
  257. base=$${i##*/}; \
  258. case "$$base" in \
  259. *-ar|*-ranlib|*-nm) \
  260. ln -sf $$(echo $$i | sed 's%^$(HOST_DIR)%..%') .; \
  261. ;; \
  262. *cc|*cc-*|*++|*++-*|*cpp|*-gfortran|*-gdc) \
  263. ln -sf toolchain-wrapper $$base; \
  264. ;; \
  265. *gdb|*gdbtui) \
  266. if test "$(BR2_PACKAGE_HOST_GDB)" != "y"; then \
  267. ln -sf $$(echo $$i | sed 's%^$(HOST_DIR)%..%') .; \
  268. fi \
  269. ;; \
  270. *) \
  271. ln -sf $$(echo $$i | sed 's%^$(HOST_DIR)%..%') .; \
  272. ;; \
  273. esac; \
  274. done
  275. endef
  276. # Various utility functions used by the external toolchain package
  277. # infrastructure. Those functions are mainly responsible for:
  278. #
  279. # - installation the toolchain libraries to $(TARGET_DIR)
  280. # - copying the toolchain sysroot to $(STAGING_DIR)
  281. # - installing a gdbinit file
  282. #
  283. # Details about sysroot directory selection.
  284. #
  285. # To find the sysroot directory, we use the trick of looking for the
  286. # 'libc.a' file with the -print-file-name gcc option, and then
  287. # mangling the path to find the base directory of the sysroot.
  288. #
  289. # Note that we do not use the -print-sysroot option, because it is
  290. # only available since gcc 4.4.x, and we only recently dropped support
  291. # for 4.2.x and 4.3.x.
  292. #
  293. # When doing this, we don't pass any option to gcc that could select a
  294. # multilib variant (such as -march) as we want the "main" sysroot,
  295. # which contains all variants of the C library in the case of multilib
  296. # toolchains. We use the TARGET_CC_NO_SYSROOT variable, which is the
  297. # path of the cross-compiler, without the --sysroot=$(STAGING_DIR),
  298. # since what we want to find is the location of the original toolchain
  299. # sysroot. This "main" sysroot directory is stored in SYSROOT_DIR.
  300. #
  301. # Then, multilib toolchains are a little bit more complicated, since
  302. # they in fact have multiple sysroots, one for each variant supported
  303. # by the toolchain. So we need to find the particular sysroot we're
  304. # interested in.
  305. #
  306. # To do so, we ask the compiler where its sysroot is by passing all
  307. # flags (including -march and al.), except the --sysroot flag since we
  308. # want to the compiler to tell us where its original sysroot
  309. # is. ARCH_SUBDIR will contain the subdirectory, in the main
  310. # SYSROOT_DIR, that corresponds to the selected architecture
  311. # variant. ARCH_SYSROOT_DIR will contain the full path to this
  312. # location.
  313. #
  314. # One might wonder why we don't just bother with ARCH_SYSROOT_DIR. The
  315. # fact is that in multilib toolchains, the header files are often only
  316. # present in the main sysroot, and only the libraries are available in
  317. # each variant-specific sysroot directory.
  318. # toolchain_find_sysroot returns the sysroot location for the given
  319. # compiler + flags. We need to handle cases where libc.a is in:
  320. #
  321. # - lib/
  322. # - usr/lib/
  323. # - lib32/
  324. # - lib64/
  325. # - lib32-fp/ (Cavium toolchain)
  326. # - lib64-fp/ (Cavium toolchain)
  327. # - usr/lib/<tuple>/ (Linaro toolchain)
  328. #
  329. # And variations on these.
  330. define toolchain_find_sysroot
  331. $$(printf $(call toolchain_find_libc_a,$(1)) | sed -r -e 's:/(usr/)?lib(32|64)?([^/]*)?/([^/]*/)?libc\.a:/:')
  332. endef
  333. # Returns the lib subdirectory for the given compiler + flags (i.e
  334. # typically lib32 or lib64 for some toolchains)
  335. define toolchain_find_libdir
  336. $$(printf $(call toolchain_find_libc_a,$(1)) | sed -r -e 's:.*/(usr/)?(lib(32|64)?([^/]*)?(/[^/]*)?)/libc.a:\2:')
  337. endef
  338. # Returns the location of the libc.a file for the given compiler + flags
  339. define toolchain_find_libc_a
  340. $$(readlink -f $$(LANG=C $(1) -print-file-name=libc.a))
  341. endef
  342. # Integration of the toolchain into Buildroot: find the main sysroot
  343. # and the variant-specific sysroot, then copy the needed libraries to
  344. # the $(TARGET_DIR) and copy the whole sysroot (libraries and headers)
  345. # to $(STAGING_DIR).
  346. #
  347. # Variables are defined as follows:
  348. #
  349. # SYSROOT_DIR: the main sysroot directory, deduced from the location of
  350. # the libc.a file in the default multilib variant, by
  351. # removing the usr/lib[32|64]/libc.a part of the path.
  352. # Ex: /x-tools/mips-2011.03/mips-linux-gnu/libc/
  353. #
  354. # ARCH_SYSROOT_DIR: the sysroot of the selected multilib variant,
  355. # deduced from the location of the libc.a file in the
  356. # selected multilib variant (taking into account the
  357. # CFLAGS), by removing usr/lib[32|64]/libc.a at the end
  358. # of the path.
  359. # Ex: /x-tools/mips-2011.03/mips-linux-gnu/libc/mips16/soft-float/el/
  360. #
  361. # ARCH_LIB_DIR: 'lib', 'lib32' or 'lib64' depending on where libraries
  362. # are stored. Deduced from the location of the libc.a file
  363. # in the selected multilib variant, by looking at
  364. # usr/lib??/libc.a.
  365. # Ex: lib
  366. #
  367. # ARCH_SUBDIR: the relative location of the sysroot of the selected
  368. # multilib variant compared to the main sysroot.
  369. # Ex: mips16/soft-float/el
  370. #
  371. # SUPPORT_LIB_DIR: some toolchains, such as recent Linaro toolchains,
  372. # store GCC support libraries (libstdc++,
  373. # libgcc_s, etc.) outside of the sysroot. In
  374. # this case, SUPPORT_LIB_DIR is set to a
  375. # non-empty value, and points to the directory
  376. # where these support libraries are
  377. # available. Those libraries will be copied to
  378. # our sysroot, and the directory will also be
  379. # considered when searching libraries for copy
  380. # to the target filesystem.
  381. #
  382. # Please be very careful to check the major toolchain sources:
  383. # Buildroot, Crosstool-NG, CodeSourcery and Linaro
  384. # before doing any modification on the below logic.
  385. ifeq ($(BR2_STATIC_LIBS),)
  386. define TOOLCHAIN_EXTERNAL_INSTALL_TARGET_LIBS
  387. $(Q)$(call MESSAGE,"Copying external toolchain libraries to target...")
  388. $(Q)for libpattern in $(TOOLCHAIN_EXTERNAL_LIBS); do \
  389. $(call copy_toolchain_lib_root,$$libpattern); \
  390. done
  391. endef
  392. endif
  393. ifeq ($(BR2_TOOLCHAIN_EXTERNAL_GDB_SERVER_COPY),y)
  394. define TOOLCHAIN_EXTERNAL_INSTALL_TARGET_GDBSERVER
  395. $(Q)$(call MESSAGE,"Copying gdbserver")
  396. $(Q)ARCH_SYSROOT_DIR="$(call toolchain_find_sysroot,$(TOOLCHAIN_EXTERNAL_CC) $(TOOLCHAIN_EXTERNAL_CFLAGS))" ; \
  397. ARCH_LIB_DIR="$(call toolchain_find_libdir,$(TOOLCHAIN_EXTERNAL_CC) $(TOOLCHAIN_EXTERNAL_CFLAGS))" ; \
  398. gdbserver_found=0 ; \
  399. for d in $${ARCH_SYSROOT_DIR}/usr \
  400. $${ARCH_SYSROOT_DIR}/../debug-root/usr \
  401. $${ARCH_SYSROOT_DIR}/usr/$${ARCH_LIB_DIR} \
  402. $(TOOLCHAIN_EXTERNAL_INSTALL_DIR); do \
  403. if test -f $${d}/bin/gdbserver ; then \
  404. install -m 0755 -D $${d}/bin/gdbserver $(TARGET_DIR)/usr/bin/gdbserver ; \
  405. gdbserver_found=1 ; \
  406. break ; \
  407. fi ; \
  408. done ; \
  409. if [ $${gdbserver_found} -eq 0 ] ; then \
  410. echo "Could not find gdbserver in external toolchain" ; \
  411. exit 1 ; \
  412. fi
  413. endef
  414. endif
  415. define TOOLCHAIN_EXTERNAL_INSTALL_SYSROOT_LIBS
  416. $(Q)SYSROOT_DIR="$(call toolchain_find_sysroot,$(TOOLCHAIN_EXTERNAL_CC))" ; \
  417. ARCH_SYSROOT_DIR="$(call toolchain_find_sysroot,$(TOOLCHAIN_EXTERNAL_CC) $(TOOLCHAIN_EXTERNAL_CFLAGS))" ; \
  418. ARCH_LIB_DIR="$(call toolchain_find_libdir,$(TOOLCHAIN_EXTERNAL_CC) $(TOOLCHAIN_EXTERNAL_CFLAGS))" ; \
  419. SUPPORT_LIB_DIR="" ; \
  420. if test `find $${ARCH_SYSROOT_DIR} -name 'libstdc++.a' | wc -l` -eq 0 ; then \
  421. LIBSTDCPP_A_LOCATION=$$(LANG=C $(TOOLCHAIN_EXTERNAL_CC) $(TOOLCHAIN_EXTERNAL_CFLAGS) -print-file-name=libstdc++.a) ; \
  422. if [ -e "$${LIBSTDCPP_A_LOCATION}" ]; then \
  423. SUPPORT_LIB_DIR=`readlink -f $${LIBSTDCPP_A_LOCATION} | sed -r -e 's:libstdc\+\+\.a::'` ; \
  424. fi ; \
  425. fi ; \
  426. if [ "$${SYSROOT_DIR}" == "$${ARCH_SYSROOT_DIR}" ] ; then \
  427. ARCH_SUBDIR="" ; \
  428. elif [ "`dirname $${ARCH_SYSROOT_DIR}`" = "`dirname $${SYSROOT_DIR}`" ] ; then \
  429. SYSROOT_DIR_DIRNAME=`dirname $${SYSROOT_DIR}`/ ; \
  430. ARCH_SUBDIR=`echo $${ARCH_SYSROOT_DIR} | sed -r -e "s:^$${SYSROOT_DIR_DIRNAME}(.*)/$$:\1:"` ; \
  431. else \
  432. ARCH_SUBDIR=`echo $${ARCH_SYSROOT_DIR} | sed -r -e "s:^$${SYSROOT_DIR}(.*)/$$:\1:"` ; \
  433. fi ; \
  434. $(call MESSAGE,"Copying external toolchain sysroot to staging...") ; \
  435. $(call copy_toolchain_sysroot,$${SYSROOT_DIR},$${ARCH_SYSROOT_DIR},$${ARCH_SUBDIR},$${ARCH_LIB_DIR},$${SUPPORT_LIB_DIR})
  436. endef
  437. # Create a symlink from (usr/)$(ARCH_LIB_DIR) to lib.
  438. # Note: the skeleton package additionally creates lib32->lib or lib64->lib
  439. # (as appropriate)
  440. #
  441. # $1: destination directory (TARGET_DIR / STAGING_DIR)
  442. create_lib_symlinks = \
  443. $(Q)DESTDIR="$(strip $1)" ; \
  444. ARCH_LIB_DIR="$(call toolchain_find_libdir,$(TOOLCHAIN_EXTERNAL_CC) $(TOOLCHAIN_EXTERNAL_CFLAGS))" ; \
  445. if [ ! -e "$${DESTDIR}/$${ARCH_LIB_DIR}" -a ! -e "$${DESTDIR}/usr/$${ARCH_LIB_DIR}" ]; then \
  446. relpath="$(call relpath_prefix,$${ARCH_LIB_DIR})" ; \
  447. ln -snf $${relpath}lib "$${DESTDIR}/$${ARCH_LIB_DIR}" ; \
  448. ln -snf $${relpath}lib "$${DESTDIR}/usr/$${ARCH_LIB_DIR}" ; \
  449. fi
  450. define TOOLCHAIN_EXTERNAL_CREATE_STAGING_LIB_SYMLINK
  451. $(call create_lib_symlinks,$(STAGING_DIR))
  452. endef
  453. define TOOLCHAIN_EXTERNAL_CREATE_TARGET_LIB_SYMLINK
  454. $(call create_lib_symlinks,$(TARGET_DIR))
  455. endef
  456. #
  457. # Generate gdbinit file for use with Buildroot
  458. #
  459. define TOOLCHAIN_EXTERNAL_INSTALL_GDBINIT
  460. $(Q)if test -f $(TARGET_CROSS)gdb ; then \
  461. $(call MESSAGE,"Installing gdbinit"); \
  462. $(gen_gdbinit_file); \
  463. fi
  464. endef
  465. # GCC installs a libstdcxx-...so-gdb.py file that gdb will load automatically,
  466. # but it contains hardcoded paths referring to the location where the (external)
  467. # toolchain was built. Fix up these paths so that the pretty printers can be
  468. # loaded automatically.
  469. # By default, the pretty printers are installed in
  470. # $(datadir)/gcc-$(gcc_version)/python but this could have been overwritten with
  471. # the gcc configure option: --with-python-dir. We thus have to search the
  472. # correct path first.
  473. define TOOLCHAIN_EXTERNAL_FIXUP_PRETTY_PRINTER_LOADER
  474. $(Q)loadfiles=$$(find $(STAGING_DIR) -name 'libstdc++.so*-gdb.py' 2>/dev/null); \
  475. pythondir=$$(find $(TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR) -path '*/libstdcxx/__init__.py' 2>/dev/null | sed 's%/libstdcxx/__init__.py%%' | head -n1); \
  476. if [ -n "$$loadfiles" ] && [ -n "$$pythondir" ]; then \
  477. echo "Fixing up hardcoded paths in GDB pretty-printer auto-load file(s) for libstdcxx: $$loadfiles"; \
  478. sed -ri \
  479. -e 's%^libdir\s*=.*%libdir = "$(STAGING_DIR)/lib"%' \
  480. -e "s%^pythondir\s*=.*%pythondir = '$$pythondir'%" \
  481. $$loadfiles; \
  482. fi
  483. endef
  484. # uClibc-ng dynamic loader is called ld-uClibc.so.1, but gcc is not
  485. # patched specifically for uClibc-ng, so it continues to generate
  486. # binaries that expect the dynamic loader to be named ld-uClibc.so.0,
  487. # like with the original uClibc. Therefore, we create an additional
  488. # symbolic link to make uClibc-ng systems work properly.
  489. define TOOLCHAIN_EXTERNAL_FIXUP_UCLIBCNG_LDSO
  490. $(Q)if test -e $(TARGET_DIR)/lib/ld-uClibc.so.1; then \
  491. ln -sf ld-uClibc.so.1 $(TARGET_DIR)/lib/ld-uClibc.so.0 ; \
  492. fi
  493. $(Q)if test -e $(TARGET_DIR)/lib/ld64-uClibc.so.1; then \
  494. ln -sf ld64-uClibc.so.1 $(TARGET_DIR)/lib/ld64-uClibc.so.0 ; \
  495. fi
  496. endef
  497. define TOOLCHAIN_EXTERNAL_INSTALL_TARGET_LDD
  498. $(Q)if test -f $(STAGING_DIR)/usr/bin/ldd ; then \
  499. $(INSTALL) -D $(STAGING_DIR)/usr/bin/ldd $(TARGET_DIR)/usr/bin/ldd ; \
  500. $(SED) 's:.*/bin/bash:#!/bin/sh:' $(TARGET_DIR)/usr/bin/ldd ; \
  501. fi
  502. endef
  503. ################################################################################
  504. # inner-toolchain-external-package -- defines the generic installation rules
  505. # for external toolchain packages
  506. #
  507. # argument 1 is the lowercase package name
  508. # argument 2 is the uppercase package name, including a HOST_ prefix
  509. # for host packages
  510. # argument 3 is the uppercase package name, without the HOST_ prefix
  511. # for host packages
  512. # argument 4 is the type (target or host)
  513. ################################################################################
  514. define inner-toolchain-external-package
  515. $(2)_INSTALL_STAGING = YES
  516. $(2)_ADD_TOOLCHAIN_DEPENDENCY = NO
  517. # In fact, we don't need to download the toolchain, since it is already
  518. # available on the system, so force the site and source to be empty so
  519. # that nothing will be downloaded/extracted.
  520. ifeq ($$(BR2_TOOLCHAIN_EXTERNAL_PREINSTALLED),y)
  521. $(2)_SITE =
  522. $(2)_SOURCE =
  523. endif
  524. ifeq ($$(BR2_TOOLCHAIN_EXTERNAL_DOWNLOAD),y)
  525. $(2)_EXCLUDES = usr/lib/locale/*
  526. $(2)_POST_EXTRACT_HOOKS += \
  527. TOOLCHAIN_EXTERNAL_MOVE
  528. endif
  529. # Checks for an already installed toolchain: check the toolchain
  530. # location, check that it is usable, and then verify that it
  531. # matches the configuration provided in Buildroot: ABI, C++ support,
  532. # kernel headers version, type of C library and all C library features.
  533. define $(2)_CONFIGURE_CMDS
  534. $$(Q)$$(call check_cross_compiler_exists,$$(TOOLCHAIN_EXTERNAL_CC))
  535. $$(Q)$$(call check_unusable_toolchain,$$(TOOLCHAIN_EXTERNAL_CC),"$$(TOOLCHAIN_EXTERNAL_CFLAGS)")
  536. $$(Q)SYSROOT_DIR="$$(call toolchain_find_sysroot,$$(TOOLCHAIN_EXTERNAL_CC))" ; \
  537. $$(call check_kernel_headers_version,\
  538. $$(BUILD_DIR),\
  539. $$(call toolchain_find_sysroot,$$(TOOLCHAIN_EXTERNAL_CC)),\
  540. $$(call qstrip,$$(BR2_TOOLCHAIN_HEADERS_AT_LEAST)),\
  541. $$(if $$(BR2_TOOLCHAIN_EXTERNAL_CUSTOM),loose,strict)); \
  542. $$(call check_gcc_version,$$(TOOLCHAIN_EXTERNAL_CC),\
  543. $$(call qstrip,$$(BR2_TOOLCHAIN_GCC_AT_LEAST))); \
  544. if test "$$(BR2_arm)" = "y" ; then \
  545. $$(call check_arm_abi,\
  546. "$$(TOOLCHAIN_EXTERNAL_CC) $$(TOOLCHAIN_EXTERNAL_CFLAGS)") ; \
  547. fi ; \
  548. $$(call check_cplusplus,$$(TOOLCHAIN_EXTERNAL_CXX)) ; \
  549. $$(call check_dlang,$$(TOOLCHAIN_EXTERNAL_GDC)) ; \
  550. $$(call check_fortran,$$(TOOLCHAIN_EXTERNAL_FC)) ; \
  551. $$(call check_openmp,$$(TOOLCHAIN_EXTERNAL_CC)) ; \
  552. if test "$$(BR2_TOOLCHAIN_EXTERNAL_UCLIBC)" = "y" ; then \
  553. $$(call check_uclibc,$$$${SYSROOT_DIR}) ; \
  554. elif test "$$(BR2_TOOLCHAIN_EXTERNAL_MUSL)" = "y" ; then \
  555. $$(call check_musl,\
  556. "$$(TOOLCHAIN_EXTERNAL_CC) $$(TOOLCHAIN_EXTERNAL_CFLAGS)") ; \
  557. else \
  558. $$(call check_glibc,$$$${SYSROOT_DIR}) ; \
  559. fi
  560. $$(Q)$$(call check_toolchain_ssp,$$(TOOLCHAIN_EXTERNAL_CC),$(BR2_SSP_OPTION))
  561. endef
  562. $(2)_TOOLCHAIN_WRAPPER_ARGS += $$(TOOLCHAIN_EXTERNAL_TOOLCHAIN_WRAPPER_ARGS)
  563. $(2)_BUILD_CMDS = $$(TOOLCHAIN_WRAPPER_BUILD)
  564. define $(2)_INSTALL_STAGING_CMDS
  565. $$(TOOLCHAIN_WRAPPER_INSTALL)
  566. $$(TOOLCHAIN_EXTERNAL_CREATE_STAGING_LIB_SYMLINK)
  567. $$(TOOLCHAIN_EXTERNAL_INSTALL_SYSROOT_LIBS)
  568. $$(TOOLCHAIN_EXTERNAL_GLIBC_NO_LIBCRYPT)
  569. $$(TOOLCHAIN_EXTERNAL_INSTALL_WRAPPER)
  570. $$(TOOLCHAIN_EXTERNAL_INSTALL_GDBINIT)
  571. $$(TOOLCHAIN_EXTERNAL_FIXUP_PRETTY_PRINTER_LOADER)
  572. endef
  573. # Even though we're installing things in both the staging, the host
  574. # and the target directory, we do everything within the
  575. # install-staging step, arbitrarily.
  576. define $(2)_INSTALL_TARGET_CMDS
  577. $$(TOOLCHAIN_EXTERNAL_CREATE_TARGET_LIB_SYMLINK)
  578. $$(TOOLCHAIN_EXTERNAL_INSTALL_TARGET_LIBS)
  579. $$(TOOLCHAIN_EXTERNAL_INSTALL_TARGET_GDBSERVER)
  580. $$(TOOLCHAIN_EXTERNAL_FIXUP_UCLIBCNG_LDSO)
  581. $$(TOOLCHAIN_EXTERNAL_INSTALL_TARGET_LDD)
  582. endef
  583. # Call the generic package infrastructure to generate the necessary
  584. # make targets
  585. $(call inner-generic-package,$(1),$(2),$(3),$(4))
  586. endef
  587. toolchain-external-package = $(call inner-toolchain-external-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)