buildvars.mk 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. ########################################################################### ###
  2. #@Title Define global variables
  3. #@Copyright Copyright (c) Imagination Technologies Ltd. All Rights Reserved
  4. #@Description This file is read once at the start of the build, after reading
  5. # in config.mk. It should define the non-MODULE_* variables used
  6. # in commands, like ALL_CFLAGS
  7. #@License Dual MIT/GPLv2
  8. #
  9. # The contents of this file are subject to the MIT license as set out below.
  10. #
  11. # Permission is hereby granted, free of charge, to any person obtaining a copy
  12. # of this software and associated documentation files (the "Software"), to deal
  13. # in the Software without restriction, including without limitation the rights
  14. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. # copies of the Software, and to permit persons to whom the Software is
  16. # furnished to do so, subject to the following conditions:
  17. #
  18. # The above copyright notice and this permission notice shall be included in
  19. # all copies or substantial portions of the Software.
  20. #
  21. # Alternatively, the contents of this file may be used under the terms of
  22. # the GNU General Public License Version 2 ("GPL") in which case the provisions
  23. # of GPL are applicable instead of those above.
  24. #
  25. # If you wish to allow use of your version of this file only under the terms of
  26. # GPL, and not to allow others to use your version of this file under the terms
  27. # of the MIT license, indicate your decision by deleting the provisions above
  28. # and replace them with the notice and other provisions required by GPL as set
  29. # out in the file called "GPL-COPYING" included in this distribution. If you do
  30. # not delete the provisions above, a recipient may use your version of this file
  31. # under the terms of either the MIT license or GPL.
  32. #
  33. # This License is also included in this distribution in the file called
  34. # "MIT-COPYING".
  35. #
  36. # EXCEPT AS OTHERWISE STATED IN A NEGOTIATED AGREEMENT: (A) THE SOFTWARE IS
  37. # PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  38. # BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  39. # PURPOSE AND NONINFRINGEMENT; AND (B) IN NO EVENT SHALL THE AUTHORS OR
  40. # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  41. # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  42. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  43. ### ###########################################################################
  44. # NOTE: You must *not* use the cc-option et al macros in COMMON_FLAGS,
  45. # COMMON_CFLAGS or COMMON_USER_FLAGS. These flags are shared between
  46. # host and target, which might use compilers with different capabilities.
  47. # ANOTHER NOTE: All flags here must be architecture-independent (i.e. no
  48. # -march or toolchain include paths)
  49. # These flags are used for kernel, User C and User C++
  50. #
  51. COMMON_FLAGS := -W -Wall
  52. # Enable 64-bit file & memory handling on 32-bit systems.
  53. #
  54. # This only affects glibc and possibly other Linux libc implementations; it
  55. # does not apply to Android where _FILE_OFFSET_BITS is not completely
  56. # implemented in bionic, and _LARGEFILE{,64}_SOURCE do not apply.
  57. #
  58. # This makes no difference on 64-bit systems, and allows for file and
  59. # memory addresses >2GB to be handled on 32-bit systems.
  60. #
  61. # _LARGEFILE_SOURCE adds a couple functions (fseeko & ftello)
  62. # _LARGEFILE64_SOURCE adds *64 variants of 32-bit file operations
  63. # _FILE_OFFSET_BITS=64 makes the 64-bit variants the default
  64. #
  65. ifneq ($(SUPPORT_ANDROID_PLATFORM),1)
  66. COMMON_FLAGS += \
  67. -D_LARGEFILE_SOURCE \
  68. -D_LARGEFILE64_SOURCE \
  69. -D_FILE_OFFSET_BITS=64
  70. endif
  71. ifeq (, $(shell which indent))
  72. INDENT_TOOL_NOT_FOUND := 1
  73. else
  74. INDENT_GENERATED_HEADERS := 1
  75. endif
  76. # Some GCC warnings are C only, so we must mask them from C++
  77. #
  78. COMMON_CFLAGS := $(COMMON_FLAGS) \
  79. -Wno-format-zero-length \
  80. -Wmissing-prototypes -Wstrict-prototypes
  81. # User C and User C++ optimization control. Does not affect kernel.
  82. #
  83. ifeq ($(BUILD),debug)
  84. COMMON_USER_FLAGS := -O0
  85. else
  86. OPTIM ?= -O2
  87. ifneq ($(PVRSRV_NEED_PVR_ASSERT),1)
  88. COMMON_USER_FLAGS := -DNDEBUG
  89. endif
  90. ifeq ($(USE_LTO),1)
  91. COMMON_USER_FLAGS += $(OPTIM) -flto
  92. else
  93. COMMON_USER_FLAGS += $(OPTIM)
  94. endif
  95. endif
  96. # GCOV support for user-mode coverage statistics
  97. #
  98. ifeq ($(GCOV_BUILD),on)
  99. COMMON_USER_FLAGS += -fprofile-arcs -ftest-coverage
  100. endif
  101. # Driver has not yet been audited for aliasing issues
  102. #
  103. COMMON_USER_FLAGS += -fno-strict-aliasing
  104. # We always enable debugging. Either the release binaries are stripped
  105. # and the symbols put in the symbolpackage, or we're building debug.
  106. #
  107. COMMON_USER_FLAGS += -g
  108. # User C and User C++ warning flags
  109. #
  110. COMMON_USER_FLAGS += \
  111. -Wpointer-arith -Wunused-parameter \
  112. -Wmissing-format-attribute
  113. # Additional warnings, and optional warnings.
  114. #
  115. TESTED_TARGET_USER_FLAGS := \
  116. $(call cc-option,-Wno-error=implicit-fallthrough) \
  117. $(call cc-option,-Wno-missing-field-initializers) \
  118. $(call cc-option,-Wno-error=assume) \
  119. $(call cc-option,-fdiagnostics-show-option) \
  120. $(call cc-option,-Wno-self-assign) \
  121. $(call cc-option,-Wno-parentheses-equality)
  122. TESTED_HOST_USER_FLAGS := \
  123. $(call host-cc-option,-Wno-error=implicit-fallthrough) \
  124. $(call host-cc-option,-Wno-missing-field-initializers) \
  125. $(call host-cc-option,-fdiagnostics-show-option) \
  126. $(call host-cc-option,-Wno-self-assign) \
  127. $(call host-cc-option,-Wno-parentheses-equality)
  128. # These flags are clang-specific.
  129. # -Wno-unused-command-line-argument works around a buggy interaction
  130. # with ccache, see https://bugzilla.samba.org/show_bug.cgi?id=8118
  131. # -fcolor-diagnostics force-enables colored error messages which
  132. # get disabled when ccache is piped through ccache.
  133. #
  134. TESTED_TARGET_USER_FLAGS += \
  135. $(call cc-option,-Qunused-arguments) \
  136. $(call cc-option,-Wlogical-op) \
  137. $(if $(shell test -t 2 && echo true),$(call cc-option,-fcolor-diagnostics))
  138. TESTED_HOST_USER_FLAGS += \
  139. $(call host-cc-option,-Qunused-arguments) \
  140. $(call host-cc-option,-Wlogical-op) \
  141. $(if $(shell test -t 2 && echo true),$(call host-cc-option,-fcolor-diagnostics))
  142. ifeq ($(W),1)
  143. TESTED_TARGET_USER_FLAGS += \
  144. $(call cc-option,-Wbad-function-cast) \
  145. $(call cc-option,-Wcast-qual) \
  146. $(call cc-option,-Wcast-align) \
  147. $(call cc-option,-Wconversion) \
  148. $(call cc-option,-Wdisabled-optimization) \
  149. $(call cc-option,-Wmissing-declarations) \
  150. $(call cc-option,-Wmissing-include-dirs) \
  151. $(call cc-option,-Wnested-externs) \
  152. $(call cc-option,-Wold-style-definition) \
  153. $(call cc-option,-Woverlength-strings) \
  154. $(call cc-option,-Wpacked) \
  155. $(call cc-option,-Wpacked-bitfield-compat) \
  156. $(call cc-option,-Wpadded) \
  157. $(call cc-option,-Wredundant-decls) \
  158. $(call cc-option,-Wshadow) \
  159. $(call cc-option,-Wswitch-default) \
  160. $(call cc-option,-Wvla) \
  161. $(call cc-option,-Wwrite-strings)
  162. TESTED_HOST_USER_FLAGS += \
  163. $(call host-cc-option,-Wbad-function-cast) \
  164. $(call host-cc-option,-Wcast-qual) \
  165. $(call host-cc-option,-Wcast-align) \
  166. $(call host-cc-option,-Wconversion) \
  167. $(call host-cc-option,-Wdisabled-optimization) \
  168. $(call host-cc-option,-Wmissing-declarations) \
  169. $(call host-cc-option,-Wmissing-include-dirs) \
  170. $(call host-cc-option,-Wnested-externs) \
  171. $(call host-cc-option,-Wold-style-definition) \
  172. $(call host-cc-option,-Woverlength-strings) \
  173. $(call host-cc-option,-Wpacked) \
  174. $(call host-cc-option,-Wpacked-bitfield-compat) \
  175. $(call host-cc-option,-Wpadded) \
  176. $(call host-cc-option,-Wredundant-decls) \
  177. $(call host-cc-option,-Wshadow) \
  178. $(call host-cc-option,-Wswitch-default) \
  179. $(call host-cc-option,-Wvla) \
  180. $(call host-cc-option,-Wwrite-strings)
  181. endif
  182. TESTED_TARGET_USER_FLAGS += \
  183. $(call cc-optional-warning,-Wunused-but-set-variable) \
  184. $(call cc-optional-warning,-Wtypedef-redefinition)
  185. TESTED_HOST_USER_FLAGS += \
  186. $(call host-cc-optional-warning,-Wunused-but-set-variable) \
  187. $(call host-cc-optional-warning,-Wtypedef-redefinition)
  188. KBUILD_FLAGS := \
  189. -Wno-unused-parameter -Wno-sign-compare
  190. # Add '-g' for the case where CONFIG_DEBUG_INFO isn't enabled in the kernel config.
  191. # Although this means the kernel won't contain symbols, the driver module will.
  192. # This is useful for matching up stack traces to source lines in the driver code.
  193. # To get full symbols (kernel and driver), the kernel will need to be configured
  194. # and built with CONFIG_DEBUG_INFO enabled.
  195. ifeq ($(BUILD),debug)
  196. KBUILD_FLAGS += -g
  197. endif
  198. TESTED_KBUILD_FLAGS := \
  199. $(call kernel-cc-option,-Wmissing-include-dirs) \
  200. $(call kernel-cc-option,-Wno-type-limits) \
  201. $(call kernel-cc-option,-Wno-pointer-arith) \
  202. $(call kernel-cc-option,-Wno-pointer-sign) \
  203. $(call kernel-cc-option,-Wno-aggregate-return) \
  204. $(call kernel-cc-option,-Wno-unused-but-set-variable) \
  205. $(call kernel-cc-option,-Wno-ignored-qualifiers) \
  206. $(call kernel-cc-option,-Wno-error=implicit-fallthrough) \
  207. $(call kernel-cc-optional-warning,-Wbad-function-cast) \
  208. $(call kernel-cc-optional-warning,-Wcast-qual) \
  209. $(call kernel-cc-optional-warning,-Wcast-align) \
  210. $(call kernel-cc-optional-warning,-Wconversion) \
  211. $(call kernel-cc-optional-warning,-Wdisabled-optimization) \
  212. $(call kernel-cc-optional-warning,-Wlogical-op) \
  213. $(call kernel-cc-optional-warning,-Wmissing-declarations) \
  214. $(call kernel-cc-optional-warning,-Wmissing-include-dirs) \
  215. $(call kernel-cc-optional-warning,-Wnested-externs) \
  216. $(call kernel-cc-optional-warning,-Wno-missing-field-initializers) \
  217. $(call kernel-cc-optional-warning,-Wold-style-definition) \
  218. $(call kernel-cc-optional-warning,-Woverlength-strings) \
  219. $(call kernel-cc-optional-warning,-Wpacked) \
  220. $(call kernel-cc-optional-warning,-Wpacked-bitfield-compat) \
  221. $(call kernel-cc-optional-warning,-Wpadded) \
  222. $(call kernel-cc-optional-warning,-Wredundant-decls) \
  223. $(call kernel-cc-optional-warning,-Wshadow) \
  224. $(call kernel-cc-optional-warning,-Wswitch-default) \
  225. $(call kernel-cc-optional-warning,-Wwrite-strings)
  226. # Force no-pie, for compilers that enable pie by default
  227. TESTED_KBUILD_FLAGS := \
  228. $(call kernel-cc-option,-fno-pie) \
  229. $(call kernel-cc-option,-no-pie) \
  230. $(TESTED_KBUILD_FLAGS)
  231. # When building against experimentally patched kernels with LLVM support,
  232. # we need to suppress warnings about bugs we haven't fixed yet. This is
  233. # temporary and will go away in the future.
  234. ifeq ($(kernel-cc-is-clang),true)
  235. TESTED_KBUILD_FLAGS := \
  236. $(call kernel-cc-option,-Wno-address-of-packed-member) \
  237. $(call kernel-cc-option,-Wno-unneeded-internal-declaration) \
  238. $(call kernel-cc-option,-Wno-unused-function) \
  239. $(call kernel-cc-option,-Wno-frame-address) \
  240. $(call kernel-cc-optional-warning,-Wno-typedef-redefinition) \
  241. $(call kernel-cc-optional-warning,-Wno-sometimes-uninitialized) \
  242. $(TESTED_KBUILD_FLAGS)
  243. endif
  244. # User C only
  245. #
  246. ALL_CFLAGS := \
  247. -std=gnu99 \
  248. $(COMMON_USER_FLAGS) $(COMMON_CFLAGS) $(TESTED_TARGET_USER_FLAGS) \
  249. $(SYS_CFLAGS)
  250. ALL_HOST_CFLAGS := \
  251. -std=gnu99 \
  252. $(COMMON_USER_FLAGS) $(COMMON_CFLAGS) $(TESTED_HOST_USER_FLAGS)
  253. # User C++ only
  254. #
  255. ALL_CXXFLAGS := \
  256. -std=gnu++1y \
  257. -fno-rtti -fno-exceptions \
  258. $(COMMON_USER_FLAGS) $(COMMON_FLAGS) $(TESTED_TARGET_USER_FLAGS) \
  259. $(SYS_CXXFLAGS)
  260. ALL_HOST_CXXFLAGS := \
  261. -std=gnu++1y \
  262. -fno-rtti -fno-exceptions \
  263. $(COMMON_USER_FLAGS) $(COMMON_FLAGS) $(TESTED_HOST_USER_FLAGS)
  264. ifeq ($(PERFDATA),1)
  265. ALL_CFLAGS += -funwind-tables
  266. endif
  267. # Workaround for clang is producing wrong code when -O0 is used.
  268. # Applies only for clang < 3.8
  269. #
  270. ifeq ($(cc-is-clang),true)
  271. __clang_bindir := $(dir $(shell which clang))
  272. __clang_version := $(shell clang --version | grep -P -o '(?<=clang version )([0-9][^ ]+)')
  273. __clang_major := $(shell echo $(__clang_version) | cut -f1 -d'.')
  274. __clang_minor := $(shell echo $(__clang_version) | cut -f2 -d'.')
  275. ifneq ($(filter -O0,$(ALL_CFLAGS)),)
  276. __clang_lt_3.8 := \
  277. $(shell ((test $(__clang_major) -lt 3) || \
  278. ((test $(__clang_major) -eq 3) && (test $(__clang_minor) -lt 8))) && echo 1 || echo 0)
  279. ifeq ($(__clang_lt_3.8),1)
  280. ALL_CFLAGS := $(patsubst -O0,-O1,$(ALL_CFLAGS))
  281. ALL_CXXFLAGS := $(patsubst -O0,-O1,$(ALL_CXXFLAGS))
  282. endif
  283. endif
  284. endif
  285. # Add GCOV_DIR just for target
  286. #
  287. ifeq ($(GCOV_BUILD),on)
  288. ifneq ($(GCOV_DIR),)
  289. ALL_CFLAGS += -fprofile-dir=$(GCOV_DIR)
  290. ALL_CXXFLAGS += -fprofile-dir=$(GCOV_DIR)
  291. endif
  292. endif
  293. # Kernel C only
  294. #
  295. ALL_KBUILD_CFLAGS := $(COMMON_CFLAGS) $(KBUILD_FLAGS) $(TESTED_KBUILD_FLAGS)
  296. # User C and C++
  297. #
  298. # NOTE: ALL_HOST_LDFLAGS should probably be using -rpath-link too, and if we
  299. # ever need to support building host shared libraries, it's required.
  300. #
  301. # We can't use it right now because we want to support non-GNU-compatible
  302. # linkers like the Darwin 'ld' which doesn't support -rpath-link.
  303. #
  304. # For the same reason (Darwin 'ld') don't bother checking for text
  305. # relocations in host binaries.
  306. #
  307. ALL_HOST_LDFLAGS :=
  308. ALL_LDFLAGS := -Wl,--warn-shared-textrel
  309. ifneq ($(USE_GOLD_LINKER),)
  310. ALL_HOST_LDFLAGS += -fuse-ld=gold
  311. ALL_LDFLAGS +=-fuse-ld=gold
  312. endif
  313. ifeq ($(GCOV_BUILD),on)
  314. ALL_LDFLAGS += -fprofile-arcs
  315. ALL_HOST_LDFLAGS += -fprofile-arcs
  316. endif
  317. ALL_LDFLAGS += $(SYS_LDFLAGS)
  318. # Optional security hardening features.
  319. # Roughly matches Android's default security build options.
  320. ifneq ($(FORTIFY),)
  321. ALL_CFLAGS += -fstack-protector -Wa,--noexecstack
  322. ALL_CXXFLAGS += -fstack-protector -Wa,--noexecstack
  323. ALL_LDFLAGS += -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now
  324. # Vanilla versions of glibc >= 2.16 print a warning if _FORTIFY_SOURCE is
  325. # defined but compiler optimisations are disabled.
  326. ifneq ($(BUILD),debug)
  327. ifneq ($(filter-out -O -O0,$(OPTIM)),)
  328. ALL_CFLAGS += -D_FORTIFY_SOURCE=2
  329. ALL_CXXFLAGS += -D_FORTIFY_SOURCE=2
  330. endif
  331. endif
  332. endif
  333. # Sanitiser support
  334. ifneq ($(USE_SANITISER),)
  335. ifeq ($(USE_SANITISER),1)
  336. # Default sanitisers
  337. override USE_SANITISER := address,undefined
  338. endif
  339. $(info Including the following sanitisers: $(USE_SANITISER))
  340. ALL_CFLAGS += -fsanitize=$(USE_SANITISER)
  341. ALL_CXXFLAGS += -fsanitize=$(USE_SANITISER)
  342. ALL_LDFLAGS += -fsanitize=$(USE_SANITISER)
  343. ALL_HOST_CFLAGS += -fsanitize=$(USE_SANITISER)
  344. ALL_HOST_CXXFLAGS += -fsanitize=$(USE_SANITISER)
  345. ALL_HOST_LDFLAGS += -fsanitize=$(USE_SANITISER)
  346. ifeq ($(cc-is-clang),false)
  347. ALL_HOST_LDFLAGS += -static-libasan
  348. endif
  349. endif
  350. # This variable contains a list of all modules built by kbuild
  351. ALL_KBUILD_MODULES :=
  352. # This variable contains a list of all modules which contain C++ source files
  353. ALL_CXX_MODULES :=
  354. ifneq ($(TOOLCHAIN),)
  355. $(warning **********************************************)
  356. $(warning The TOOLCHAIN option has been removed, but)
  357. $(warning you have it set (via $(origin TOOLCHAIN)))
  358. $(warning **********************************************)
  359. endif
  360. # We need the glibc version to generate the cache names for LLVM and XOrg components.
  361. ifeq ($(CROSS_COMPILE),)
  362. LIBC_VERSION_PROBE := $(shell ldd $(shell which true) | awk '/libc.so/{print $$3'} )
  363. LIBC_VERSION := $(shell $(LIBC_VERSION_PROBE)| tr -d '(),' | head -1)
  364. endif