toolchain.mk 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. toolchains ?= host $(ARCH)
  19. include $(dir $(lastword $(MAKEFILE_LIST)))build_env.mk
  20. include $(dir $(lastword $(MAKEFILE_LIST)))utilities.mk
  21. include $(addprefix $(dir $(lastword $(MAKEFILE_LIST)))toolchains/, \
  22. $(addsuffix .mk,$(toolchains)))
  23. #
  24. # Configure tool classes that we recognize.
  25. #
  26. # In the context of this build system, a tool class identifies a
  27. # specific role or type of tool in the toolchain.
  28. #
  29. tool-classes := cc
  30. tool-class-name-cc := C compiler
  31. tool-classes += cpp
  32. tool-class-name-cpp := C preprocessor
  33. tool-classes += as
  34. tool-class-name-as := assembler
  35. tool-classes += ld
  36. tool-class-name-ld := linker
  37. tool-classes += oc
  38. tool-class-name-oc := object copier
  39. tool-classes += od
  40. tool-class-name-od := object dumper
  41. tool-classes += ar
  42. tool-class-name-ar := archiver
  43. tool-classes += dtc
  44. tool-class-name-dtc := device tree compiler
  45. #
  46. # Configure tools that we recognize.
  47. #
  48. # Here we declare the list of specific toolchain tools that we know how
  49. # to interact with. We don't organize these into tool classes yet - that
  50. # happens further down.
  51. #
  52. # Arm® Compiler for Embedded
  53. tools := arm-clang
  54. tool-name-arm-clang := Arm® Compiler for Embedded `armclang`
  55. tools += arm-link
  56. tool-name-arm-link := Arm® Compiler for Embedded `armlink`
  57. tools += arm-ar
  58. tool-name-arm-ar := Arm® Compiler for Embedded `armar`
  59. tools += arm-fromelf
  60. tool-name-arm-fromelf := Arm® Compiler for Embedded `fromelf`
  61. # LLVM Project
  62. tools += llvm-clang
  63. tool-name-llvm-clang := LLVM Clang (`clang`)
  64. tools += llvm-lld
  65. tool-name-llvm-lld := LLVM LLD (`lld`)
  66. tools += llvm-objcopy
  67. tool-name-llvm-objcopy := LLVM `llvm-objcopy`
  68. tools += llvm-objdump
  69. tool-name-llvm-objdump := LLVM `llvm-objdump`
  70. tools += llvm-ar
  71. tool-name-llvm-ar := LLVM `llvm-ar`
  72. # GNU Compiler Collection & GNU Binary Utilities
  73. tools += gnu-gcc
  74. tool-name-gnu-gcc := GNU GCC (`gcc`)
  75. tools += gnu-ld
  76. tool-name-gnu-ld := GNU LD (`ld.bfd`)
  77. tools += gnu-objcopy
  78. tool-name-gnu-objcopy := GNU `objcopy`
  79. tools += gnu-objdump
  80. tool-name-gnu-objdump := GNU `objdump`
  81. tools += gnu-ar
  82. tool-name-gnu-ar := GNU `ar`
  83. # Other tools
  84. tools += generic-dtc
  85. tool-name-generic-dtc := Device Tree Compiler (`dtc`)
  86. #
  87. # Assign tools to tool classes.
  88. #
  89. # Multifunctional tools, i.e. tools which can perform multiple roles in
  90. # a toolchain, may be specified in multiple tool class lists. For
  91. # example, a C compiler which can also perform the role of a linker may
  92. # be placed in both `tools-cc` and `tools-ld`.
  93. #
  94. # C-related tools
  95. tools-cc := arm-clang llvm-clang gnu-gcc # C compilers
  96. tools-cpp := arm-clang llvm-clang gnu-gcc # C preprocessors
  97. # Assembly-related tools
  98. tools-as := arm-clang llvm-clang gnu-gcc # Assemblers
  99. # Linking and object-handling tools
  100. tools-ld := arm-clang arm-link llvm-clang llvm-lld gnu-gcc gnu-ld # Linkers
  101. tools-oc := arm-fromelf llvm-objcopy gnu-objcopy # Object copiers
  102. tools-od := arm-fromelf llvm-objdump gnu-objdump # Object dumpers
  103. tools-ar := arm-ar llvm-ar gnu-ar # Archivers
  104. # Other tools
  105. tools-dtc := generic-dtc # Device tree compilers
  106. define check-tool-class-tools
  107. $(eval tool-class := $(1))
  108. ifndef tools-$(tool-class)
  109. $$(error no tools registered to handle tool class `$(tool-class)`)
  110. endif
  111. endef
  112. $(foreach tool-class,$(tool-classes), \
  113. $(eval $(call check-tool-class-tools,$(tool-class))))
  114. #
  115. # Default tools for each toolchain.
  116. #
  117. # Toolchains can specify a default path to any given tool with a tool
  118. # class. These values are used in the absence of user-specified values,
  119. # and are configured by the makefile for each toolchain using variables
  120. # of the form:
  121. #
  122. # - $(toolchain)-$(tool-class)-default
  123. #
  124. # For example, the default C compiler for the AArch32 and AArch64
  125. # toolchains could be configured with:
  126. #
  127. # - aarch32-cc-default
  128. # - aarch64-cc-default
  129. #
  130. define check-toolchain-tool-class-default
  131. $(eval toolchain := $(1))
  132. $(eval tool-class := $(2))
  133. ifndef $(toolchain)-$(tool-class)-default
  134. $$(error no default value specified for tool class `$(tool-class)` of toolchain `$(toolchain)`)
  135. endif
  136. endef
  137. define check-toolchain-tool-class-defaults
  138. $(eval toolchain := $(1))
  139. $(foreach tool-class,$(tool-classes), \
  140. $(eval $(call check-toolchain-tool-class-default,$(toolchain),$(tool-class))))
  141. endef
  142. $(foreach toolchain,$(toolchains), \
  143. $(eval $(call check-toolchain-tool-class-defaults,$(toolchain))))
  144. #
  145. # Helper functions to identify toolchain tools.
  146. #
  147. # The functions defined in this section return a tool identifier when
  148. # given a path to a binary. We generally check a help or version string
  149. # to more reliably identify tools than by looking at the path alone
  150. # (e.g. `gcc` on macOS is actually Apple Clang).
  151. #
  152. # Each tool-guessing function (`guess-tool-$(tool)`) takes a single
  153. # argument giving the path to the tool to guess, and returns a non-empty
  154. # value if the tool corresponds to the tool identifier `$(tool)`:
  155. #
  156. # $(call guess-tool-llvm-clang,aarch64-none-elf-gcc) # <empty>
  157. # $(call guess-tool-gnu-gcc,aarch64-none-elf-gcc) # <non-empty>
  158. #
  159. # The `guess-tool` function tries to find the corresponding tool
  160. # identifier for a tool given its path. It takes two arguments:
  161. #
  162. # - $(1): a list of candidate tool identifiers to check
  163. # - $(2): the path to the tool to identify
  164. #
  165. # If any of the guess functions corresponding to candidate tool
  166. # identifiers return a non-empty value then the tool identifier of the
  167. # first function to do so is returned:
  168. #
  169. # $(call guess-tool,gnu-gcc llvm-clang,armclang) # <empty>
  170. # $(call guess-tool,gnu-gcc llvm-clang,clang-14) # llvm-clang
  171. # $(call guess-tool,gnu-gcc llvm-clang,aarch64-none-elf-gcc-12) # gnu-gcc
  172. #
  173. # Tools are checked in the order that they appear in
  174. # `tools-$(tool-class)`, and the first match is returned.
  175. #
  176. # Arm Compiler for Embedded
  177. guess-tool-arm-clang = $(shell $(1) --version 2>&1 <$(nul) | grep -o "Tool: armclang")
  178. guess-tool-arm-link = $(shell $(1) --help 2>&1 <$(nul) | grep -o "Tool: armlink")
  179. guess-tool-arm-fromelf = $(shell $(1) --help 2>&1 <$(nul) | grep -o "Tool: fromelf")
  180. guess-tool-arm-ar = $(shell $(1) --version 2>&1 <$(nul) | grep -o "Tool: armar")
  181. # LLVM Project
  182. guess-tool-llvm-clang = $(shell $(1) -v 2>&1 <$(nul) | grep -o "clang version")
  183. guess-tool-llvm-lld = $(shell $(1) --help 2>&1 <$(nul) | grep -o "OVERVIEW: lld")
  184. guess-tool-llvm-objcopy = $(shell $(1) --help 2>&1 <$(nul) | grep -o "llvm-objcopy tool")
  185. guess-tool-llvm-objdump = $(shell $(1) --help 2>&1 <$(nul) | grep -o "llvm object file dumper")
  186. guess-tool-llvm-ar = $(shell $(1) --help 2>&1 <$(nul) | grep -o "LLVM Archiver")
  187. # GNU Compiler Collection & GNU Binary Utilities
  188. guess-tool-gnu-gcc = $(shell $(1) -v 2>&1 <$(nul) | grep -o "gcc version")
  189. guess-tool-gnu-ld = $(shell $(1) -v 2>&1 <$(nul) | grep -o "GNU ld")
  190. guess-tool-gnu-objcopy = $(shell $(1) --version 2>&1 <$(nul) | grep -o "GNU objcopy")
  191. guess-tool-gnu-objdump = $(shell $(1) --version 2>&1 <$(nul) | grep -o "GNU objdump")
  192. guess-tool-gnu-ar = $(shell $(1) --version 2>&1 <$(nul) | grep -o "GNU ar")
  193. # Other tools
  194. guess-tool-generic-dtc = $(shell $(1) --version 2>&1 <$(nul) | grep -o "Version: DTC")
  195. guess-tool = $(firstword $(foreach candidate,$(1), \
  196. $(if $(call guess-tool-$(candidate),$(2)),$(candidate))))
  197. #
  198. # Locate and identify tools belonging to each toolchain.
  199. #
  200. # Each tool class in each toolchain receives a variable of the form
  201. # `$(toolchain)-$(tool)` giving the associated path to the program. For
  202. # example:
  203. #
  204. # - `aarch64-ld` gives the linker for the AArch64 toolchain,
  205. # - `aarch32-oc` gives the object copier for the AArch32 toolchain, and
  206. # - `host-cc` gives the C compiler for the host toolchain.
  207. #
  208. # For each of these variables, if no program path is explicitly provided
  209. # by the parent Makefile then the C compiler is queried (if supported)
  210. # for its location. This is done via the `guess-$(tool)-$(tool-class)`
  211. # set of functions. For example:
  212. #
  213. # - `guess-arm-clang-ld` guesses the linker via Arm Clang,
  214. # - `guess-llvm-clang-as` guesses the assembler via LLVM Clang, and
  215. # - `guess-gnu-gcc-od` guesses the object dumper via GNU GCC.
  216. #
  217. # If the C compiler cannot provide the location (or the tool class is
  218. # the C compiler), then it is assigned the value of the
  219. # `$(toolchain)-$(tool)-default` variable.
  220. #
  221. guess-arm-clang-cpp = $(1)
  222. guess-arm-clang-as = $(1)
  223. guess-arm-clang-ld = # Fall back to `$(toolchain)-ld-default`
  224. guess-arm-clang-oc = # Fall back to `$(toolchain)-oc-default`
  225. guess-arm-clang-od = # Fall back to `$(toolchain)-od-default`
  226. guess-arm-clang-ar = # Fall back to `$(toolchain)-ar-default`
  227. guess-llvm-clang-cpp = $(1)
  228. guess-llvm-clang-as = $(1)
  229. guess-llvm-clang-ld = $(shell $(1) --print-prog-name ld.lld 2>$(nul))
  230. guess-llvm-clang-oc = $(shell $(1) --print-prog-name llvm-objcopy 2>$(nul))
  231. guess-llvm-clang-od = $(shell $(1) --print-prog-name llvm-objdump 2>$(nul))
  232. guess-llvm-clang-ar = $(shell $(1) --print-prog-name llvm-ar 2>$(nul))
  233. guess-gnu-gcc-cpp = $(1)
  234. guess-gnu-gcc-as = $(1)
  235. guess-gnu-gcc-ld = $(1)
  236. guess-gnu-gcc-oc = $(shell $(1) --print-prog-name objcopy 2>$(nul))
  237. guess-gnu-gcc-od = $(shell $(1) --print-prog-name objdump 2>$(nul))
  238. guess-gnu-gcc-ar = $(shell $(1) --print-prog-name ar 2>$(nul))
  239. define toolchain-warn-unrecognized
  240. $$(warning )
  241. $$(warning The configured $$($(1)-name) $$(tool-class-name-$(2)) could not be identified and may not be supported:)
  242. $$(warning )
  243. $$(warning $$(space) $$($(1)-$(2)))
  244. $$(warning )
  245. $$(warning The default $$($(1)-name) $$(tool-class-name-$(2)) is:)
  246. $$(warning )
  247. $$(warning $$(space) $$($(1)-$(2)-default))
  248. $$(warning )
  249. $$(warning The following tools are supported:)
  250. $$(warning )
  251. $$(foreach tool,$$(tools-$(2)), \
  252. $$(warning $$(space) - $$(tool-name-$$(tool))))
  253. $$(warning )
  254. $$(warning The build system will treat this $$(tool-class-name-$(2)) as $$(tool-name-$$($(1)-$(2)-id-default)).)
  255. $$(warning )
  256. endef
  257. define toolchain-determine-tool
  258. $(1)-$(2)-guess = $$(if $$(filter-out cc,$(2)),$\
  259. $$(call guess-$$($(1)-cc-id)-$(2),$$($(1)-cc)))
  260. $(1)-$(2) := $$(or $$($(1)-$(2)),$$($(1)-$(2)-guess))
  261. $(1)-$(2) := $$(or $$($(1)-$(2)),$$($(1)-$(2)-default))
  262. ifneq ($$(call which,$$($(1)-$(2))),)
  263. # If we can resolve this tool to a program on the `PATH`
  264. # then escape it for use in a shell, which allows us to
  265. # preserve spaces.
  266. $(1)-$(2) := $$(call escape-shell,$$($(1)-$(2)))
  267. endif
  268. $(1)-$(2)-id := $$(call guess-tool,$$(tools-$(2)),$$($(1)-$(2)))
  269. ifndef $(1)-$(2)-id
  270. $(1)-$(2)-id := $$($(1)-$(2)-id-default)
  271. $$(eval $$(call toolchain-warn-unrecognized,$(1),$(2)))
  272. endif
  273. endef
  274. define toolchain-determine
  275. $$(foreach tool-class,$$(tool-classes), \
  276. $$(eval $$(call toolchain-determine-tool,$(1),$$(tool-class))))
  277. endef
  278. $(foreach toolchain,$(toolchains), \
  279. $(eval $(call toolchain-determine,$(toolchain))))
  280. endif