blob: 495776795b44b5770ad7b7e8287507013858c8d3 [file] [log] [blame]
Colin Cross4d9c2d12016-07-29 12:48:20 -07001// Copyright 2016 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Crossb98c8b02016-07-29 13:44:28 -070015package config
Colin Cross4d9c2d12016-07-29 12:48:20 -070016
17import (
18 "strings"
19
20 "android/soong/android"
Ramy Medhat9a90fe52020-04-13 13:21:23 -040021 "android/soong/remoteexec"
Colin Cross4d9c2d12016-07-29 12:48:20 -070022)
23
Colin Cross4d9c2d12016-07-29 12:48:20 -070024var (
Leo Li8756b372017-05-22 16:11:34 -070025 // Flags used by lots of devices. Putting them in package static variables
26 // will save bytes in build.ninja so they aren't repeated for every file
Colin Cross4d9c2d12016-07-29 12:48:20 -070027 commonGlobalCflags = []string{
28 "-DANDROID",
29 "-fmessage-length=0",
30 "-W",
31 "-Wall",
32 "-Wno-unused",
33 "-Winit-self",
34 "-Wpointer-arith",
George Burgess IVfb81db22020-02-22 19:28:56 -080035 "-Wunreachable-code-loop-increment",
Colin Cross4d9c2d12016-07-29 12:48:20 -070036
Colin Cross7278afc2017-11-02 22:38:32 -070037 // Make paths in deps files relative
38 "-no-canonical-prefixes",
Colin Cross39d450b2017-11-06 12:53:30 -080039 "-fno-canonical-system-headers",
Colin Cross7278afc2017-11-02 22:38:32 -070040
Colin Cross4d9c2d12016-07-29 12:48:20 -070041 "-DNDEBUG",
42 "-UDEBUG",
Colin Cross7278afc2017-11-02 22:38:32 -070043
44 "-fno-exceptions",
45 "-Wno-multichar",
46
47 "-O2",
48 "-g",
cmtice5e13f862021-04-22 03:17:16 +000049 "-fdebug-default-version=5",
Yi Kong110cd5f2020-11-04 01:44:15 +080050 "-fdebug-info-for-profiling",
Colin Cross7278afc2017-11-02 22:38:32 -070051
52 "-fno-strict-aliasing",
Dan Willemsen5d980c82019-08-27 19:37:10 -070053
54 "-Werror=date-time",
Elliott Hughes2cdbdf12019-12-03 18:13:00 -080055 "-Werror=pragma-pack",
56 "-Werror=pragma-pack-suspicious-include",
Christopher Di Bella23a991c2020-11-11 22:41:32 +000057 "-Werror=string-plus-int",
George Burgess IVfb81db22020-02-22 19:28:56 -080058 "-Werror=unreachable-code-loop-increment",
Colin Cross4d9c2d12016-07-29 12:48:20 -070059 }
60
Colin Cross6f6a4282016-10-17 14:19:06 -070061 commonGlobalConlyflags = []string{}
Elliott Hughes5a0401a2016-10-07 13:12:58 -070062
Colin Cross4d9c2d12016-07-29 12:48:20 -070063 deviceGlobalCflags = []string{
64 "-fdiagnostics-color",
65
Colin Cross133dbe72017-11-02 22:55:19 -070066 "-ffunction-sections",
Colin Crossea3141d2017-11-06 14:02:02 -080067 "-fdata-sections",
68 "-fno-short-enums",
Colin Cross133dbe72017-11-02 22:55:19 -070069 "-funwind-tables",
70 "-fstack-protector-strong",
71 "-Wa,--noexecstack",
72 "-D_FORTIFY_SOURCE=2",
73
74 "-Wstrict-aliasing=2",
75
Colin Cross4d9c2d12016-07-29 12:48:20 -070076 "-Werror=return-type",
77 "-Werror=non-virtual-dtor",
78 "-Werror=address",
79 "-Werror=sequence-point",
Colin Cross133dbe72017-11-02 22:55:19 -070080 "-Werror=format-security",
Colin Cross4d9c2d12016-07-29 12:48:20 -070081 }
82
Colin Cross26f14502017-11-06 13:59:48 -080083 deviceGlobalCppflags = []string{
84 "-fvisibility-inlines-hidden",
85 }
86
Colin Cross324a4572017-11-02 23:09:41 -070087 deviceGlobalLdflags = []string{
88 "-Wl,-z,noexecstack",
89 "-Wl,-z,relro",
90 "-Wl,-z,now",
91 "-Wl,--build-id=md5",
92 "-Wl,--warn-shared-textrel",
93 "-Wl,--fatal-warnings",
94 "-Wl,--no-undefined-version",
Ryan Prichardb35a85e2021-01-13 19:18:53 -080095 // TODO: Eventually we should link against a libunwind.a with hidden symbols, and then these
96 // --exclude-libs arguments can be removed.
Christopher Ferrisc3a1e222019-04-10 17:57:50 -070097 "-Wl,--exclude-libs,libgcc.a",
Yi Kong3d8792f2019-05-06 16:18:33 -070098 "-Wl,--exclude-libs,libgcc_stripped.a",
Peter Collingbournee5ba2862019-12-10 18:37:45 -080099 "-Wl,--exclude-libs,libunwind_llvm.a",
Ryan Prichardb35a85e2021-01-13 19:18:53 -0800100 "-Wl,--exclude-libs,libunwind.a",
Ryo Hashimoto5818b932021-03-23 15:05:22 +0900101 "-Wl,--icf=safe",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700102 }
103
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700104 deviceGlobalLldflags = append(ClangFilterUnknownLldflags(deviceGlobalLdflags),
105 []string{
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700106 "-fuse-ld=lld",
107 }...)
108
Colin Cross4d9c2d12016-07-29 12:48:20 -0700109 hostGlobalCflags = []string{}
110
Colin Cross26f14502017-11-06 13:59:48 -0800111 hostGlobalCppflags = []string{}
112
Colin Cross324a4572017-11-02 23:09:41 -0700113 hostGlobalLdflags = []string{}
114
Chih-Hung Hsieh3101a962018-04-17 14:16:05 -0700115 hostGlobalLldflags = []string{"-fuse-ld=lld"}
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700116
Colin Cross4d9c2d12016-07-29 12:48:20 -0700117 commonGlobalCppflags = []string{
118 "-Wsign-promo",
119 }
120
121 noOverrideGlobalCflags = []string{
Christopher Di Bella23a991c2020-11-11 22:41:32 +0000122 "-Werror=bool-operation",
123 "-Werror=implicit-int-float-conversion",
124 "-Werror=int-in-bool-context",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700125 "-Werror=int-to-pointer-cast",
126 "-Werror=pointer-to-int-cast",
Christopher Di Bella23a991c2020-11-11 22:41:32 +0000127 "-Werror=string-compare",
128 "-Werror=xor-used-as-pow",
Stephen Hines2210e722020-07-15 11:11:57 -0700129 // http://b/161386391 for -Wno-void-pointer-to-enum-cast
130 "-Wno-void-pointer-to-enum-cast",
131 // http://b/161386391 for -Wno-void-pointer-to-int-cast
132 "-Wno-void-pointer-to-int-cast",
133 // http://b/161386391 for -Wno-pointer-to-int-cast
134 "-Wno-pointer-to-int-cast",
George Burgess IV6c691642019-09-18 17:52:05 -0700135 "-Werror=fortify-source",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700136 }
137
Colin Crossb98c8b02016-07-29 13:44:28 -0700138 IllegalFlags = []string{
Colin Cross4d9c2d12016-07-29 12:48:20 -0700139 "-w",
140 }
Colin Cross6f6a4282016-10-17 14:19:06 -0700141
Dan Albert043833c2017-02-03 16:13:38 -0800142 CStdVersion = "gnu99"
Elliott Hughes34e4e412018-11-30 16:03:06 +0000143 CppStdVersion = "gnu++17"
Dan Albert043833c2017-02-03 16:13:38 -0800144 ExperimentalCStdVersion = "gnu11"
Elliott Hughes37976122018-11-28 14:16:39 -0800145 ExperimentalCppStdVersion = "gnu++2a"
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700146
Leo Li8756b372017-05-22 16:11:34 -0700147 // prebuilts/clang default settings.
148 ClangDefaultBase = "prebuilts/clang/host"
Pirama Arumuga Nainar3e545082021-06-23 22:39:37 -0700149 ClangDefaultVersion = "clang-r416183b1"
150 ClangDefaultShortVersion = "12.0.7"
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800151
Chih-Hung Hsieh775edde2017-12-24 22:24:47 -0800152 // Directories with warnings from Android.bp files.
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800153 WarningAllowedProjects = []string{
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800154 "device/",
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800155 "vendor/",
156 }
157
Chih-Hung Hsieh775edde2017-12-24 22:24:47 -0800158 // Directories with warnings from Android.mk files.
159 WarningAllowedOldProjects = []string{}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700160)
161
Colin Crossb98c8b02016-07-29 13:44:28 -0700162var pctx = android.NewPackageContext("android/soong/cc/config")
163
Colin Cross4d9c2d12016-07-29 12:48:20 -0700164func init() {
Kousik Kumard207cbe2020-10-20 05:52:49 +0000165 if android.BuildOs == android.Linux {
166 commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=")
167 }
168
Jingwen Chen51a1e1c2021-05-20 13:40:14 +0000169 exportStringListStaticVariable("CommonGlobalConlyflags", commonGlobalConlyflags)
170 exportStringListStaticVariable("DeviceGlobalCppflags", deviceGlobalCppflags)
171 exportStringListStaticVariable("DeviceGlobalLdflags", deviceGlobalLdflags)
172 exportStringListStaticVariable("DeviceGlobalLldflags", deviceGlobalLldflags)
173 exportStringListStaticVariable("HostGlobalCppflags", hostGlobalCppflags)
174 exportStringListStaticVariable("HostGlobalLdflags", hostGlobalLdflags)
175 exportStringListStaticVariable("HostGlobalLldflags", hostGlobalLldflags)
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000176
177 // Export the static default CommonClangGlobalCflags to Bazel.
178 // TODO(187086342): handle cflags that are set in VariableFuncs.
179 commonClangGlobalCFlags := append(
180 ClangFilterUnknownCflags(commonGlobalCflags),
181 []string{
182 "${ClangExtraCflags}",
183 // Default to zero initialization.
184 "-ftrivial-auto-var-init=zero",
185 "-enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang",
186 }...)
Jingwen Chen51a1e1c2021-05-20 13:40:14 +0000187 exportedStringListVars.Set("CommonClangGlobalCflags", commonClangGlobalCFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700188
Stephen Hines66c8b442019-06-10 17:40:12 -0700189 pctx.VariableFunc("CommonClangGlobalCflags", func(ctx android.PackageVarContext) string {
190 flags := ClangFilterUnknownCflags(commonGlobalCflags)
191 flags = append(flags, "${ClangExtraCflags}")
192
193 // http://b/131390872
194 // Automatically initialize any uninitialized stack variables.
Stephen Hines5c873ac2020-05-14 00:59:09 +0000195 // Prefer zero-init if multiple options are set.
Stephen Hines66c8b442019-06-10 17:40:12 -0700196 if ctx.Config().IsEnvTrue("AUTO_ZERO_INITIALIZE") {
197 flags = append(flags, "-ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang")
198 } else if ctx.Config().IsEnvTrue("AUTO_PATTERN_INITIALIZE") {
199 flags = append(flags, "-ftrivial-auto-var-init=pattern")
Stephen Hines797e1952020-01-28 14:43:11 -0800200 } else if ctx.Config().IsEnvTrue("AUTO_UNINITIALIZE") {
201 flags = append(flags, "-ftrivial-auto-var-init=uninitialized")
Stephen Hines0e1d5d82020-01-30 15:06:00 -0800202 } else {
Stephen Hines5c873ac2020-05-14 00:59:09 +0000203 // Default to zero initialization.
204 flags = append(flags, "-ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang")
Stephen Hines66c8b442019-06-10 17:40:12 -0700205 }
Stephen Hines66c8b442019-06-10 17:40:12 -0700206 return strings.Join(flags, " ")
207 })
208
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000209 // Export the static default DeviceClangGlobalCflags to Bazel.
210 // TODO(187086342): handle cflags that are set in VariableFuncs.
211 deviceClangGlobalCflags := append(ClangFilterUnknownCflags(deviceGlobalCflags), "${ClangExtraTargetCflags}")
Jingwen Chen51a1e1c2021-05-20 13:40:14 +0000212 exportedStringListVars.Set("DeviceClangGlobalCflags", deviceClangGlobalCflags)
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000213
Doug Hornc32c6b02019-01-17 14:44:05 -0800214 pctx.VariableFunc("DeviceClangGlobalCflags", func(ctx android.PackageVarContext) string {
215 if ctx.Config().Fuchsia() {
216 return strings.Join(ClangFilterUnknownCflags(deviceGlobalCflags), " ")
217 } else {
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000218 return strings.Join(deviceClangGlobalCflags, " ")
Doug Hornc32c6b02019-01-17 14:44:05 -0800219 }
220 })
Colin Cross4d9c2d12016-07-29 12:48:20 -0700221
Jingwen Chen51a1e1c2021-05-20 13:40:14 +0000222 exportStringListStaticVariable("HostClangGlobalCflags", ClangFilterUnknownCflags(hostGlobalCflags))
223 exportStringListStaticVariable("NoOverrideClangGlobalCflags", append(ClangFilterUnknownCflags(noOverrideGlobalCflags), "${ClangExtraNoOverrideCflags}"))
224 exportStringListStaticVariable("CommonClangGlobalCppflags", append(ClangFilterUnknownCflags(commonGlobalCppflags), "${ClangExtraCppflags}"))
225 exportStringListStaticVariable("ClangExternalCflags", []string{"${ClangExtraExternalCflags}"})
Yi Kongcc80f8d2018-06-06 14:42:44 -0700226
Colin Cross1cfd89a2016-09-15 09:30:46 -0700227 // Everything in these lists is a crime against abstraction and dependency tracking.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700228 // Do not add anything to this list.
Jingwen Chen21999952021-05-19 05:53:51 +0000229 commonGlobalIncludes := []string{
230 "system/core/include",
231 "system/logging/liblog/include",
232 "system/media/audio/include",
233 "hardware/libhardware/include",
234 "hardware/libhardware_legacy/include",
235 "hardware/ril/include",
236 "frameworks/native/include",
237 "frameworks/native/opengl/include",
238 "frameworks/av/include",
239 }
Jingwen Chen51a1e1c2021-05-20 13:40:14 +0000240 exportedStringListVars.Set("CommonGlobalIncludes", commonGlobalIncludes)
Jingwen Chen21999952021-05-19 05:53:51 +0000241 pctx.PrefixedExistentPathsForSourcesVariable("CommonGlobalIncludes", "-I", commonGlobalIncludes)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700242
Leo Li8756b372017-05-22 16:11:34 -0700243 pctx.SourcePathVariable("ClangDefaultBase", ClangDefaultBase)
Dan Willemsen54daaf02018-03-12 13:24:09 -0700244 pctx.VariableFunc("ClangBase", func(ctx android.PackageVarContext) string {
245 if override := ctx.Config().Getenv("LLVM_PREBUILTS_BASE"); override != "" {
246 return override
Colin Cross4d9c2d12016-07-29 12:48:20 -0700247 }
Dan Willemsen54daaf02018-03-12 13:24:09 -0700248 return "${ClangDefaultBase}"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700249 })
Dan Willemsen54daaf02018-03-12 13:24:09 -0700250 pctx.VariableFunc("ClangVersion", func(ctx android.PackageVarContext) string {
251 if override := ctx.Config().Getenv("LLVM_PREBUILTS_VERSION"); override != "" {
252 return override
Colin Cross4d9c2d12016-07-29 12:48:20 -0700253 }
Dan Willemsen54daaf02018-03-12 13:24:09 -0700254 return ClangDefaultVersion
Colin Cross4d9c2d12016-07-29 12:48:20 -0700255 })
Colin Crossb98c8b02016-07-29 13:44:28 -0700256 pctx.StaticVariable("ClangPath", "${ClangBase}/${HostPrebuiltTag}/${ClangVersion}")
257 pctx.StaticVariable("ClangBin", "${ClangPath}/bin")
258
Dan Willemsen54daaf02018-03-12 13:24:09 -0700259 pctx.VariableFunc("ClangShortVersion", func(ctx android.PackageVarContext) string {
260 if override := ctx.Config().Getenv("LLVM_RELEASE_VERSION"); override != "" {
261 return override
Stephen Hinese55a4cc2016-11-18 17:12:38 -0800262 }
Dan Willemsen54daaf02018-03-12 13:24:09 -0700263 return ClangDefaultShortVersion
Stephen Hinese55a4cc2016-11-18 17:12:38 -0800264 })
Stephen Hines755fe072018-01-24 19:58:36 -0800265 pctx.StaticVariable("ClangAsanLibDir", "${ClangBase}/linux-x86/${ClangVersion}/lib64/clang/${ClangShortVersion}/lib/linux")
Alistair Strachan777475c2016-08-26 12:55:49 -0700266
Jayant Chowdharye622d202017-02-01 19:19:52 -0800267 // These are tied to the version of LLVM directly in external/llvm, so they might trail the host prebuilts
268 // being used for the rest of the build process.
269 pctx.SourcePathVariable("RSClangBase", "prebuilts/clang/host")
270 pctx.SourcePathVariable("RSClangVersion", "clang-3289846")
271 pctx.SourcePathVariable("RSReleaseVersion", "3.8")
272 pctx.StaticVariable("RSLLVMPrebuiltsPath", "${RSClangBase}/${HostPrebuiltTag}/${RSClangVersion}/bin")
273 pctx.StaticVariable("RSIncludePath", "${RSLLVMPrebuiltsPath}/../lib64/clang/${RSReleaseVersion}/include")
274
Jeff Gaston734e3802017-04-10 15:47:24 -0700275 pctx.PrefixedExistentPathsForSourcesVariable("RsGlobalIncludes", "-I",
Colin Cross2a252be2017-05-01 17:37:24 -0700276 []string{
277 "external/clang/lib/Headers",
278 "frameworks/rs/script_api/include",
279 })
280
Dan Willemsen54daaf02018-03-12 13:24:09 -0700281 pctx.VariableFunc("CcWrapper", func(ctx android.PackageVarContext) string {
282 if override := ctx.Config().Getenv("CC_WRAPPER"); override != "" {
283 return override + " "
Alistair Strachan777475c2016-08-26 12:55:49 -0700284 }
Dan Willemsen54daaf02018-03-12 13:24:09 -0700285 return ""
Alistair Strachan777475c2016-08-26 12:55:49 -0700286 })
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400287
Colin Cross77cdcfd2021-03-12 11:28:25 -0800288 pctx.StaticVariableWithEnvOverride("RECXXPool", "RBE_CXX_POOL", remoteexec.DefaultPool)
289 pctx.StaticVariableWithEnvOverride("RECXXLinksPool", "RBE_CXX_LINKS_POOL", remoteexec.DefaultPool)
290 pctx.StaticVariableWithEnvOverride("REClangTidyPool", "RBE_CLANG_TIDY_POOL", remoteexec.DefaultPool)
291 pctx.StaticVariableWithEnvOverride("RECXXLinksExecStrategy", "RBE_CXX_LINKS_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
292 pctx.StaticVariableWithEnvOverride("REClangTidyExecStrategy", "RBE_CLANG_TIDY_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
293 pctx.StaticVariableWithEnvOverride("REAbiDumperExecStrategy", "RBE_ABI_DUMPER_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
294 pctx.StaticVariableWithEnvOverride("REAbiLinkerExecStrategy", "RBE_ABI_LINKER_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700295}
296
297var HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", android.Config.PrebuiltOS)