toolchain.mk 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. #
  2. # Copyright (c) 2023-2024, Arm Limited and Contributors. All rights reserved.
  3. #
  4. # SPDX-License-Identifier: BSD-3-Clause
  5. #
  6. #
  7. # TF-A uses three toolchains:
  8. #
  9. # - The host toolchain (`host`) for building native tools
  10. # - The AArch32 toolchain (`aarch32`) for building Arm AArch32 images
  11. # - The AArch64 toolchain (`aarch64`) for building Arm AArch64 images
  12. #
  13. # In the main Makefile only one of the two Arm toolchains is enabled in any
  14. # given build, but individual tools and libraries may need access to both.
  15. #
  16. ifndef toolchain-mk
  17. toolchain-mk := $(lastword $(MAKEFILE_LIST))
  18. include $(dir $(toolchain-mk))build_env.mk
  19. include $(dir $(toolchain-mk))utilities.mk
  20. #
  21. # Make assigns generic default values to `CC`, `CPP`, `AS`, etc. if they
  22. # are not explicitly assigned values by the user. These are usually okay
  23. # for very simple programs when building for the host system, but we
  24. # need greater control over the toolchain flow.
  25. #
  26. # Therefore, we undefine these built-in variables if they have default
  27. # values, so that we can provide our own default values later instead.
  28. #
  29. ifeq ($(origin CC),default)
  30. undefine CC
  31. endif
  32. ifeq ($(origin CPP),default)
  33. undefine CPP
  34. endif
  35. ifeq ($(origin AS),default)
  36. undefine AS
  37. endif
  38. ifeq ($(origin AR),default)
  39. undefine AR
  40. endif
  41. ifeq ($(origin LD),default)
  42. undefine LD
  43. endif
  44. #
  45. # The full list of toolchains supported by TF-A.
  46. #
  47. # Each of these toolchains defines a file of the same name in the
  48. # `toolchains` directory, which must configure the following variables:
  49. #
  50. # - <toolchain>-name
  51. #
  52. # A human-readable name for the toolchain,
  53. #
  54. # Additionally, for every tool class, it must also define:
  55. #
  56. # - <toolchain>-<tool-class>-parameter
  57. #
  58. # The command line or environment variable used to set the tool for
  59. # for the given tool class.
  60. #
  61. # - <toolchain>-<tool-class>-default-id
  62. #
  63. # The default tool identifier used if the tool for the given tool
  64. # class cannot be identified.
  65. #
  66. # - <toolchain>-<tool-class>-default
  67. #
  68. # The default commands to try, in the order defined, for the given
  69. # tool class if the user does not explicitly provide one, and if the
  70. # command could not be derived from the C compiler.
  71. #
  72. toolchains := host # Used for host targets
  73. toolchains += aarch32 # Used for AArch32 targets
  74. toolchains += aarch64 # Used for AArch64 targets
  75. toolchains += rk3399-m0 # Used for RK3399 Cortex-M0 targets
  76. include $(toolchains:%=$(dir $(toolchain-mk))toolchains/%.mk)
  77. #
  78. # Configure tool classes that we recognize.
  79. #
  80. # In the context of this build system, a tool class identifies a
  81. # specific role or type of tool in the toolchain.
  82. #
  83. toolchain-tool-classes := cc
  84. toolchain-tool-class-name-cc := C compiler
  85. toolchain-tool-classes += cpp
  86. toolchain-tool-class-name-cpp := C preprocessor
  87. toolchain-tool-classes += as
  88. toolchain-tool-class-name-as := assembler
  89. toolchain-tool-classes += ld
  90. toolchain-tool-class-name-ld := linker
  91. toolchain-tool-classes += oc
  92. toolchain-tool-class-name-oc := object copier
  93. toolchain-tool-classes += od
  94. toolchain-tool-class-name-od := object dumper
  95. toolchain-tool-classes += ar
  96. toolchain-tool-class-name-ar := archiver
  97. toolchain-tool-classes += dtc
  98. toolchain-tool-class-name-dtc := device tree compiler
  99. toolchain-tool-classes += poetry
  100. toolchain-tool-class-name-poetry := Python Poetry package manager
  101. #
  102. # Configure tools that we recognize.
  103. #
  104. # Here we declare the list of specific toolchain tools that we know how
  105. # to interact with. We don't organize these into tool classes yet - that
  106. # happens further down.
  107. #
  108. # Arm® Compiler for Embedded
  109. toolchain-tools := arm-clang
  110. toolchain-tool-name-arm-clang := Arm® Compiler for Embedded `armclang`
  111. toolchain-tools += arm-link
  112. toolchain-tool-name-arm-link := Arm® Compiler for Embedded `armlink`
  113. toolchain-tools += arm-ar
  114. toolchain-tool-name-arm-ar := Arm® Compiler for Embedded `armar`
  115. toolchain-tools += arm-fromelf
  116. toolchain-tool-name-arm-fromelf := Arm® Compiler for Embedded `fromelf`
  117. # LLVM Project
  118. toolchain-tools += llvm-clang
  119. toolchain-tool-name-llvm-clang := LLVM Clang (`clang`)
  120. toolchain-tools += llvm-lld
  121. toolchain-tool-name-llvm-lld := LLVM LLD (`lld`)
  122. toolchain-tools += llvm-objcopy
  123. toolchain-tool-name-llvm-objcopy := LLVM `llvm-objcopy`
  124. toolchain-tools += llvm-objdump
  125. toolchain-tool-name-llvm-objdump := LLVM `llvm-objdump`
  126. toolchain-tools += llvm-ar
  127. toolchain-tool-name-llvm-ar := LLVM `llvm-ar`
  128. # GNU Compiler Collection & GNU Binary Utilities
  129. toolchain-tools += gnu-gcc
  130. toolchain-tool-name-gnu-gcc := GNU GCC (`gcc`)
  131. toolchain-tools += gnu-ld
  132. toolchain-tool-name-gnu-ld := GNU LD (`ld.bfd`)
  133. toolchain-tools += gnu-objcopy
  134. toolchain-tool-name-gnu-objcopy := GNU `objcopy`
  135. toolchain-tools += gnu-objdump
  136. toolchain-tool-name-gnu-objdump := GNU `objdump`
  137. toolchain-tools += gnu-ar
  138. toolchain-tool-name-gnu-ar := GNU `ar`
  139. # Other tools
  140. toolchain-tools += generic-dtc
  141. toolchain-tool-name-generic-dtc := Device Tree Compiler (`dtc`)
  142. toolchain-tools += generic-poetry
  143. toolchain-tool-name-generic-poetry := Poetry (`poetry`)
  144. #
  145. # Assign tools to tool classes.
  146. #
  147. # Multifunctional tools, i.e. tools which can perform multiple roles in
  148. # a toolchain, may be specified in multiple tool class lists. For
  149. # example, a C compiler which can also perform the role of a linker may
  150. # be placed in both `toolchain-tools-cc` and `toolchain-tools-ld`.
  151. #
  152. # C-related tools
  153. toolchain-tools-cc := arm-clang llvm-clang gnu-gcc # C compilers
  154. toolchain-tools-cpp := arm-clang llvm-clang gnu-gcc # C preprocessors
  155. # Assembly-related tools
  156. toolchain-tools-as := arm-clang llvm-clang gnu-gcc # Assemblers
  157. # Linking and object-handling tools
  158. toolchain-tools-ld := arm-clang arm-link llvm-clang llvm-lld gnu-gcc gnu-ld # Linkers
  159. toolchain-tools-oc := arm-fromelf llvm-objcopy gnu-objcopy # Object copiers
  160. toolchain-tools-od := arm-fromelf llvm-objdump gnu-objdump # Object dumpers
  161. toolchain-tools-ar := arm-ar llvm-ar gnu-ar # Archivers
  162. # Other tools
  163. toolchain-tools-dtc := generic-dtc # Device tree compilers
  164. toolchain-tools-poetry := generic-poetry # Python Poetry package manager
  165. #
  166. # Helper functions to identify toolchain tools.
  167. #
  168. # The functions defined in this section return a tool identifier when
  169. # given a path to a binary. We generally check a help or version string
  170. # to more reliably identify tools than by looking at the path alone
  171. # (e.g. `gcc` on macOS is actually Apple Clang).
  172. #
  173. # Each tool-guessing function (`toolchain-guess-tool-$(tool)`) takes a
  174. # single argument giving the path to the tool to guess, and returns a
  175. # non-empty value if the tool corresponds to the tool identifier
  176. # `$(tool)`:
  177. #
  178. # $(call toolchain-guess-tool-llvm-clang,aarch64-none-elf-gcc) # <empty>
  179. # $(call toolchain-guess-tool-gnu-gcc,aarch64-none-elf-gcc) # <non-empty>
  180. #
  181. # The `toolchain-guess-tool` function tries to find the corresponding tool
  182. # identifier for a tool given its path. It takes two arguments:
  183. #
  184. # - $(1): a list of candidate tool identifiers to check
  185. # - $(2): the path to the tool to identify
  186. #
  187. # If any of the guess functions corresponding to candidate tool
  188. # identifiers return a non-empty value then the tool identifier of the
  189. # first function to do so is returned:
  190. #
  191. # $(call toolchain-guess-tool,gnu-gcc llvm-clang,armclang) # <empty>
  192. # $(call toolchain-guess-tool,gnu-gcc llvm-clang,clang-14) # llvm-clang
  193. # $(call toolchain-guess-tool,gnu-gcc llvm-clang,aarch64-none-elf-gcc-12) # gnu-gcc
  194. #
  195. # Tools are checked in the order that they are provided, and the first
  196. # match is returned.
  197. #
  198. # Arm Compiler for Embedded
  199. toolchain-guess-tool-arm-clang = $(shell $(1) --version 2>&1 <$(nul) | grep -o "Tool: armclang")
  200. toolchain-guess-tool-arm-link = $(shell $(1) --help 2>&1 <$(nul) | grep -o "Tool: armlink")
  201. toolchain-guess-tool-arm-fromelf = $(shell $(1) --help 2>&1 <$(nul) | grep -o "Tool: fromelf")
  202. toolchain-guess-tool-arm-ar = $(shell $(1) --version 2>&1 <$(nul) | grep -o "Tool: armar")
  203. # LLVM Project
  204. toolchain-guess-tool-llvm-clang = $(shell $(1) -v 2>&1 <$(nul) | grep -o "clang version")
  205. toolchain-guess-tool-llvm-lld = $(shell $(1) --help 2>&1 <$(nul) | grep -o "OVERVIEW: lld")
  206. toolchain-guess-tool-llvm-objcopy = $(shell $(1) --help 2>&1 <$(nul) | grep -o "llvm-objcopy tool")
  207. toolchain-guess-tool-llvm-objdump = $(shell $(1) --help 2>&1 <$(nul) | grep -o "llvm object file dumper")
  208. toolchain-guess-tool-llvm-ar = $(shell $(1) --help 2>&1 <$(nul) | grep -o "LLVM Archiver")
  209. # GNU Compiler Collection & GNU Binary Utilities
  210. toolchain-guess-tool-gnu-gcc = $(shell $(1) -v 2>&1 <$(nul) | grep -o "gcc version")
  211. toolchain-guess-tool-gnu-ld = $(shell $(1) -v 2>&1 <$(nul) | grep -o "GNU ld")
  212. toolchain-guess-tool-gnu-objcopy = $(shell $(1) --version 2>&1 <$(nul) | grep -o "GNU objcopy")
  213. toolchain-guess-tool-gnu-objdump = $(shell $(1) --version 2>&1 <$(nul) | grep -o "GNU objdump")
  214. toolchain-guess-tool-gnu-ar = $(shell $(1) --version 2>&1 <$(nul) | grep -o "GNU ar")
  215. # Other tools
  216. toolchain-guess-tool-generic-dtc = $(shell $(1) --version 2>&1 <$(nul) | grep -o "Version: DTC")
  217. toolchain-guess-tool-generic-poetry = $(shell $(1) --version 2>&1 <$(nul))
  218. toolchain-guess-tool = $(if $(2),$(firstword $(foreach candidate,$(1),$\
  219. $(if $(call toolchain-guess-tool-$(candidate),$(2)),$(candidate)))))
  220. #
  221. # Warn the user that a tool could not be identified.
  222. #
  223. # Parameters:
  224. #
  225. # - $1: The toolchain that the tool belongs to.
  226. # - $2: The tool class that the tool belongs to.
  227. #
  228. define toolchain-warn-unrecognized
  229. $(warning )
  230. $(warning The configured $($(1)-name) $(toolchain-tool-class-name-$(2)) could not be identified:)
  231. $(warning )
  232. $(warning $(space) $($(1)-$(2))$(if $($(1)-$(2)-parameter), (via `$($(1)-$(2)-parameter)`)))
  233. $(warning )
  234. $(warning The following tools were tried, but either did not exist or could not be identified:)
  235. $(warning )
  236. $(foreach default,$($(1)-$(2)-default), \
  237. $(warning $(space) - $(default)))
  238. $(warning )
  239. $(warning The following tools are supported:)
  240. $(warning )
  241. $(foreach tool,$(toolchain-tools-$(2)), \
  242. $(warning $(space) - $(toolchain-tool-name-$(tool))))
  243. $(warning )
  244. $(warning The build system will treat this $(toolchain-tool-class-name-$(2)) as $(toolchain-tool-name-$($(1)-$(2)-default-id)).)
  245. $(warning )
  246. endef
  247. #
  248. # Locate and identify tools belonging to each toolchain.
  249. #
  250. # Each tool class in each toolchain receives a variable of the form
  251. # `$(toolchain)-$(tool)` giving the associated path to the program. For
  252. # example:
  253. #
  254. # - `aarch64-ld` gives the linker for the AArch64 toolchain,
  255. # - `aarch32-oc` gives the object copier for the AArch32 toolchain, and
  256. # - `host-cc` gives the C compiler for the host toolchain.
  257. #
  258. # For each of these variables, if no program path is explicitly provided
  259. # by the parent Makefile then the C compiler is queried (if supported)
  260. # for its location.
  261. #
  262. # If the C compiler cannot provide the location (or the tool class *is*
  263. # the C compiler), then it is assigned a default value specific for that
  264. # toolchain.
  265. #
  266. toolchain-derive-arm-clang-cpp = $(1)
  267. toolchain-derive-arm-clang-as = $(1)
  268. toolchain-derive-arm-clang-ld = # Fall back to `$(toolchain)-ld-default`
  269. toolchain-derive-arm-clang-oc = # Fall back to `$(toolchain)-oc-default`
  270. toolchain-derive-arm-clang-od = # Fall back to `$(toolchain)-od-default`
  271. toolchain-derive-arm-clang-ar = # Fall back to `$(toolchain)-ar-default`
  272. toolchain-derive-llvm-clang-cpp = $(1)
  273. toolchain-derive-llvm-clang-as = $(1)
  274. toolchain-derive-llvm-clang-ld = $(shell $(1) --print-prog-name ld.lld 2>$(nul))
  275. toolchain-derive-llvm-clang-oc = $(shell $(1) --print-prog-name llvm-objcopy 2>$(nul))
  276. toolchain-derive-llvm-clang-od = $(shell $(1) --print-prog-name llvm-objdump 2>$(nul))
  277. toolchain-derive-llvm-clang-ar = $(shell $(1) --print-prog-name llvm-ar 2>$(nul))
  278. toolchain-derive-gnu-gcc-cpp = $(1)
  279. toolchain-derive-gnu-gcc-as = $(1)
  280. toolchain-derive-gnu-gcc-ld = $(1)
  281. toolchain-derive-gnu-gcc-oc = $(shell $(1) --print-prog-name objcopy 2>$(nul))
  282. toolchain-derive-gnu-gcc-od = $(shell $(1) --print-prog-name objdump 2>$(nul))
  283. toolchain-derive-gnu-gcc-ar = $(shell $(1) --print-prog-name ar 2>$(nul))
  284. toolchain-derive = $(if $3,$(call toolchain-derive-$1-$2,$3))
  285. #
  286. # Configure a toolchain.
  287. #
  288. # Parameters:
  289. #
  290. # - $1: The toolchain to configure.
  291. #
  292. # This function iterates over all tool classes and configures them for
  293. # the provided toolchain. Toolchain tools are initialized lazily and
  294. # on-demand based on the first read of the tool path or identifier
  295. # variables.
  296. #
  297. define toolchain-configure
  298. $$(foreach tool-class,$$(toolchain-tool-classes), \
  299. $$(eval $$(call toolchain-configure-tool,$1,$$(tool-class))))
  300. endef
  301. #
  302. # Configure a specific tool within a toolchain.
  303. #
  304. # Parameters:
  305. #
  306. # - $1: The toolchain to configure.
  307. # - $2: The tool class to configure.
  308. #
  309. define toolchain-configure-tool
  310. $1-$2-configure = $\
  311. $$(eval $$(call toolchain-determine-tool,$1,$2))
  312. #
  313. # When either of the following variables are read for the first
  314. # time, the appropriate tool is determined and *both* variables
  315. # are overwritten with their final values.
  316. #
  317. $1-$2 = $$($1-$2-configure)$$($1-$2)
  318. $1-$2-id = $$($1-$2-configure)$$($1-$2-id)
  319. endef
  320. #
  321. # Determines and identifies a tool.
  322. #
  323. # Parameters:
  324. #
  325. # - $1: The toolchain identifier.
  326. # - $2: The tool class.
  327. #
  328. # Tool identification happens by reading the designated tool parameter
  329. # to get the user-specified command for the tool (e.g. `CC` or `LD`). If
  330. # no tool parameter is defined then try to derive the tool from the C
  331. # compiler.
  332. #
  333. # If all else fails, fall back to the default command defined by the
  334. # toolchain makefile.
  335. #
  336. define toolchain-determine-tool
  337. toolchain-$1-$2-derive-from-cc = $$(if $$(filter-out cc,$2),$\
  338. $$(call toolchain-derive,$$($1-cc-id),$2,$$($1-cc)))
  339. toolchain-$1-$2-shell = $\
  340. $$(if $$(call defined,$$($1-$2-parameter)),$\
  341. $$($$($1-$2-parameter)),$\
  342. $$(or $$(toolchain-$1-$2-derive-from-cc),$\
  343. $$(toolchain-$1-$2-default)))
  344. toolchain-$1-$2-default = $$(firstword $\
  345. $$(foreach default,$$($1-$2-default),$\
  346. $$(if $$(call which,$$(default)),$$(default))) $\
  347. $$($1-$2-default))
  348. $1-$2 := $$(if $$(call which,$$(toolchain-$1-$2-shell)),$\
  349. $$(call escape-shell,$$(toolchain-$1-$2-shell)),$\
  350. $$(toolchain-$1-$2-shell))
  351. $1-$2-id := $$(if $$($1-$2),$$(or $\
  352. $$(call toolchain-guess-tool,$$\
  353. $$(toolchain-tools-$2),$$($1-$2)),$\
  354. $$($1-$2-default-id)))
  355. ifeq ($$(or $$($1-$2-id),$$(call bool,$$($1-$2-optional))),)
  356. $$(call toolchain-warn-unrecognized,$1,$2)
  357. endif
  358. endef
  359. $(foreach toolchain,$(toolchains), \
  360. $(eval $(call toolchain-configure,$(toolchain))))
  361. endif