blob: d458311eeaea12af6b7799748dceb0ed96fecfd8 [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",
Yi Kong110cd5f2020-11-04 01:44:15 +080049 "-fdebug-info-for-profiling",
Colin Cross7278afc2017-11-02 22:38:32 -070050
51 "-fno-strict-aliasing",
Dan Willemsen5d980c82019-08-27 19:37:10 -070052
53 "-Werror=date-time",
Elliott Hughes2cdbdf12019-12-03 18:13:00 -080054 "-Werror=pragma-pack",
55 "-Werror=pragma-pack-suspicious-include",
Christopher Di Bella23a991c2020-11-11 22:41:32 +000056 "-Werror=string-plus-int",
George Burgess IVfb81db22020-02-22 19:28:56 -080057 "-Werror=unreachable-code-loop-increment",
Colin Cross4d9c2d12016-07-29 12:48:20 -070058 }
59
Colin Cross6f6a4282016-10-17 14:19:06 -070060 commonGlobalConlyflags = []string{}
Elliott Hughes5a0401a2016-10-07 13:12:58 -070061
Colin Cross4d9c2d12016-07-29 12:48:20 -070062 deviceGlobalCflags = []string{
63 "-fdiagnostics-color",
64
Colin Cross133dbe72017-11-02 22:55:19 -070065 "-ffunction-sections",
Colin Crossea3141d2017-11-06 14:02:02 -080066 "-fdata-sections",
67 "-fno-short-enums",
Colin Cross133dbe72017-11-02 22:55:19 -070068 "-funwind-tables",
69 "-fstack-protector-strong",
70 "-Wa,--noexecstack",
71 "-D_FORTIFY_SOURCE=2",
72
73 "-Wstrict-aliasing=2",
74
Colin Cross4d9c2d12016-07-29 12:48:20 -070075 "-Werror=return-type",
76 "-Werror=non-virtual-dtor",
77 "-Werror=address",
78 "-Werror=sequence-point",
Colin Cross133dbe72017-11-02 22:55:19 -070079 "-Werror=format-security",
Colin Cross4d9c2d12016-07-29 12:48:20 -070080 }
81
Colin Cross26f14502017-11-06 13:59:48 -080082 deviceGlobalCppflags = []string{
83 "-fvisibility-inlines-hidden",
84 }
85
Colin Cross324a4572017-11-02 23:09:41 -070086 deviceGlobalLdflags = []string{
87 "-Wl,-z,noexecstack",
88 "-Wl,-z,relro",
89 "-Wl,-z,now",
90 "-Wl,--build-id=md5",
91 "-Wl,--warn-shared-textrel",
92 "-Wl,--fatal-warnings",
93 "-Wl,--no-undefined-version",
Ryan Prichardb35a85e2021-01-13 19:18:53 -080094 // TODO: Eventually we should link against a libunwind.a with hidden symbols, and then these
95 // --exclude-libs arguments can be removed.
Christopher Ferrisc3a1e222019-04-10 17:57:50 -070096 "-Wl,--exclude-libs,libgcc.a",
Yi Kong3d8792f2019-05-06 16:18:33 -070097 "-Wl,--exclude-libs,libgcc_stripped.a",
Peter Collingbournee5ba2862019-12-10 18:37:45 -080098 "-Wl,--exclude-libs,libunwind_llvm.a",
Ryan Prichardb35a85e2021-01-13 19:18:53 -080099 "-Wl,--exclude-libs,libunwind.a",
Ryo Hashimoto5818b932021-03-23 15:05:22 +0900100 "-Wl,--icf=safe",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700101 }
102
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700103 deviceGlobalLldflags = append(ClangFilterUnknownLldflags(deviceGlobalLdflags),
104 []string{
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700105 "-fuse-ld=lld",
106 }...)
107
Colin Cross4d9c2d12016-07-29 12:48:20 -0700108 hostGlobalCflags = []string{}
109
Colin Cross26f14502017-11-06 13:59:48 -0800110 hostGlobalCppflags = []string{}
111
Colin Cross324a4572017-11-02 23:09:41 -0700112 hostGlobalLdflags = []string{}
113
Chih-Hung Hsieh3101a962018-04-17 14:16:05 -0700114 hostGlobalLldflags = []string{"-fuse-ld=lld"}
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700115
Colin Cross4d9c2d12016-07-29 12:48:20 -0700116 commonGlobalCppflags = []string{
117 "-Wsign-promo",
118 }
119
120 noOverrideGlobalCflags = []string{
Christopher Di Bella23a991c2020-11-11 22:41:32 +0000121 "-Werror=bool-operation",
122 "-Werror=implicit-int-float-conversion",
123 "-Werror=int-in-bool-context",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700124 "-Werror=int-to-pointer-cast",
125 "-Werror=pointer-to-int-cast",
Christopher Di Bella23a991c2020-11-11 22:41:32 +0000126 "-Werror=string-compare",
127 "-Werror=xor-used-as-pow",
Stephen Hines2210e722020-07-15 11:11:57 -0700128 // http://b/161386391 for -Wno-void-pointer-to-enum-cast
129 "-Wno-void-pointer-to-enum-cast",
130 // http://b/161386391 for -Wno-void-pointer-to-int-cast
131 "-Wno-void-pointer-to-int-cast",
132 // http://b/161386391 for -Wno-pointer-to-int-cast
133 "-Wno-pointer-to-int-cast",
George Burgess IV6c691642019-09-18 17:52:05 -0700134 "-Werror=fortify-source",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700135 }
136
Colin Crossb98c8b02016-07-29 13:44:28 -0700137 IllegalFlags = []string{
Colin Cross4d9c2d12016-07-29 12:48:20 -0700138 "-w",
139 }
Colin Cross6f6a4282016-10-17 14:19:06 -0700140
Dan Albert043833c2017-02-03 16:13:38 -0800141 CStdVersion = "gnu99"
Elliott Hughes34e4e412018-11-30 16:03:06 +0000142 CppStdVersion = "gnu++17"
Dan Albert043833c2017-02-03 16:13:38 -0800143 ExperimentalCStdVersion = "gnu11"
Elliott Hughes37976122018-11-28 14:16:39 -0800144 ExperimentalCppStdVersion = "gnu++2a"
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700145
Leo Li8756b372017-05-22 16:11:34 -0700146 // prebuilts/clang default settings.
147 ClangDefaultBase = "prebuilts/clang/host"
Pirama Arumuga Nainar8a717862021-04-15 10:01:22 -0700148 ClangDefaultVersion = "clang-r416183b"
149 ClangDefaultShortVersion = "12.0.5"
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800150
Chih-Hung Hsieh775edde2017-12-24 22:24:47 -0800151 // Directories with warnings from Android.bp files.
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800152 WarningAllowedProjects = []string{
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800153 "device/",
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800154 "vendor/",
155 }
156
Chih-Hung Hsieh775edde2017-12-24 22:24:47 -0800157 // Directories with warnings from Android.mk files.
158 WarningAllowedOldProjects = []string{}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700159)
160
Colin Crossb98c8b02016-07-29 13:44:28 -0700161var pctx = android.NewPackageContext("android/soong/cc/config")
162
Colin Cross4d9c2d12016-07-29 12:48:20 -0700163func init() {
Kousik Kumard207cbe2020-10-20 05:52:49 +0000164 if android.BuildOs == android.Linux {
165 commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=")
166 }
167
Jingwen Chen51a1e1c2021-05-20 13:40:14 +0000168 exportStringListStaticVariable("CommonGlobalConlyflags", commonGlobalConlyflags)
169 exportStringListStaticVariable("DeviceGlobalCppflags", deviceGlobalCppflags)
170 exportStringListStaticVariable("DeviceGlobalLdflags", deviceGlobalLdflags)
171 exportStringListStaticVariable("DeviceGlobalLldflags", deviceGlobalLldflags)
172 exportStringListStaticVariable("HostGlobalCppflags", hostGlobalCppflags)
173 exportStringListStaticVariable("HostGlobalLdflags", hostGlobalLdflags)
174 exportStringListStaticVariable("HostGlobalLldflags", hostGlobalLldflags)
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000175
176 // Export the static default CommonClangGlobalCflags to Bazel.
177 // TODO(187086342): handle cflags that are set in VariableFuncs.
178 commonClangGlobalCFlags := append(
179 ClangFilterUnknownCflags(commonGlobalCflags),
180 []string{
181 "${ClangExtraCflags}",
182 // Default to zero initialization.
183 "-ftrivial-auto-var-init=zero",
184 "-enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang",
185 }...)
Jingwen Chen51a1e1c2021-05-20 13:40:14 +0000186 exportedStringListVars.Set("CommonClangGlobalCflags", commonClangGlobalCFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700187
Stephen Hines66c8b442019-06-10 17:40:12 -0700188 pctx.VariableFunc("CommonClangGlobalCflags", func(ctx android.PackageVarContext) string {
189 flags := ClangFilterUnknownCflags(commonGlobalCflags)
190 flags = append(flags, "${ClangExtraCflags}")
191
192 // http://b/131390872
193 // Automatically initialize any uninitialized stack variables.
Stephen Hines5c873ac2020-05-14 00:59:09 +0000194 // Prefer zero-init if multiple options are set.
Stephen Hines66c8b442019-06-10 17:40:12 -0700195 if ctx.Config().IsEnvTrue("AUTO_ZERO_INITIALIZE") {
196 flags = append(flags, "-ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang")
197 } else if ctx.Config().IsEnvTrue("AUTO_PATTERN_INITIALIZE") {
198 flags = append(flags, "-ftrivial-auto-var-init=pattern")
Stephen Hines797e1952020-01-28 14:43:11 -0800199 } else if ctx.Config().IsEnvTrue("AUTO_UNINITIALIZE") {
200 flags = append(flags, "-ftrivial-auto-var-init=uninitialized")
Stephen Hines0e1d5d82020-01-30 15:06:00 -0800201 } else {
Stephen Hines5c873ac2020-05-14 00:59:09 +0000202 // Default to zero initialization.
203 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 -0700204 }
Stephen Hines66c8b442019-06-10 17:40:12 -0700205 return strings.Join(flags, " ")
206 })
207
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000208 // Export the static default DeviceClangGlobalCflags to Bazel.
209 // TODO(187086342): handle cflags that are set in VariableFuncs.
210 deviceClangGlobalCflags := append(ClangFilterUnknownCflags(deviceGlobalCflags), "${ClangExtraTargetCflags}")
Jingwen Chen51a1e1c2021-05-20 13:40:14 +0000211 exportedStringListVars.Set("DeviceClangGlobalCflags", deviceClangGlobalCflags)
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000212
Doug Hornc32c6b02019-01-17 14:44:05 -0800213 pctx.VariableFunc("DeviceClangGlobalCflags", func(ctx android.PackageVarContext) string {
214 if ctx.Config().Fuchsia() {
215 return strings.Join(ClangFilterUnknownCflags(deviceGlobalCflags), " ")
216 } else {
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000217 return strings.Join(deviceClangGlobalCflags, " ")
Doug Hornc32c6b02019-01-17 14:44:05 -0800218 }
219 })
Colin Cross4d9c2d12016-07-29 12:48:20 -0700220
Jingwen Chen51a1e1c2021-05-20 13:40:14 +0000221 exportStringListStaticVariable("HostClangGlobalCflags", ClangFilterUnknownCflags(hostGlobalCflags))
222 exportStringListStaticVariable("NoOverrideClangGlobalCflags", append(ClangFilterUnknownCflags(noOverrideGlobalCflags), "${ClangExtraNoOverrideCflags}"))
223 exportStringListStaticVariable("CommonClangGlobalCppflags", append(ClangFilterUnknownCflags(commonGlobalCppflags), "${ClangExtraCppflags}"))
224 exportStringListStaticVariable("ClangExternalCflags", []string{"${ClangExtraExternalCflags}"})
Yi Kongcc80f8d2018-06-06 14:42:44 -0700225
Colin Cross1cfd89a2016-09-15 09:30:46 -0700226 // Everything in these lists is a crime against abstraction and dependency tracking.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700227 // Do not add anything to this list.
Jingwen Chen21999952021-05-19 05:53:51 +0000228 commonGlobalIncludes := []string{
229 "system/core/include",
230 "system/logging/liblog/include",
231 "system/media/audio/include",
232 "hardware/libhardware/include",
233 "hardware/libhardware_legacy/include",
234 "hardware/ril/include",
235 "frameworks/native/include",
236 "frameworks/native/opengl/include",
237 "frameworks/av/include",
238 }
Jingwen Chen51a1e1c2021-05-20 13:40:14 +0000239 exportedStringListVars.Set("CommonGlobalIncludes", commonGlobalIncludes)
Jingwen Chen21999952021-05-19 05:53:51 +0000240 pctx.PrefixedExistentPathsForSourcesVariable("CommonGlobalIncludes", "-I", commonGlobalIncludes)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700241
Leo Li8756b372017-05-22 16:11:34 -0700242 pctx.SourcePathVariable("ClangDefaultBase", ClangDefaultBase)
Dan Willemsen54daaf02018-03-12 13:24:09 -0700243 pctx.VariableFunc("ClangBase", func(ctx android.PackageVarContext) string {
244 if override := ctx.Config().Getenv("LLVM_PREBUILTS_BASE"); override != "" {
245 return override
Colin Cross4d9c2d12016-07-29 12:48:20 -0700246 }
Dan Willemsen54daaf02018-03-12 13:24:09 -0700247 return "${ClangDefaultBase}"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700248 })
Dan Willemsen54daaf02018-03-12 13:24:09 -0700249 pctx.VariableFunc("ClangVersion", func(ctx android.PackageVarContext) string {
250 if override := ctx.Config().Getenv("LLVM_PREBUILTS_VERSION"); override != "" {
251 return override
Colin Cross4d9c2d12016-07-29 12:48:20 -0700252 }
Dan Willemsen54daaf02018-03-12 13:24:09 -0700253 return ClangDefaultVersion
Colin Cross4d9c2d12016-07-29 12:48:20 -0700254 })
Colin Crossb98c8b02016-07-29 13:44:28 -0700255 pctx.StaticVariable("ClangPath", "${ClangBase}/${HostPrebuiltTag}/${ClangVersion}")
256 pctx.StaticVariable("ClangBin", "${ClangPath}/bin")
257
Dan Willemsen54daaf02018-03-12 13:24:09 -0700258 pctx.VariableFunc("ClangShortVersion", func(ctx android.PackageVarContext) string {
259 if override := ctx.Config().Getenv("LLVM_RELEASE_VERSION"); override != "" {
260 return override
Stephen Hinese55a4cc2016-11-18 17:12:38 -0800261 }
Dan Willemsen54daaf02018-03-12 13:24:09 -0700262 return ClangDefaultShortVersion
Stephen Hinese55a4cc2016-11-18 17:12:38 -0800263 })
Stephen Hines755fe072018-01-24 19:58:36 -0800264 pctx.StaticVariable("ClangAsanLibDir", "${ClangBase}/linux-x86/${ClangVersion}/lib64/clang/${ClangShortVersion}/lib/linux")
Alistair Strachan777475c2016-08-26 12:55:49 -0700265
Jayant Chowdharye622d202017-02-01 19:19:52 -0800266 // These are tied to the version of LLVM directly in external/llvm, so they might trail the host prebuilts
267 // being used for the rest of the build process.
268 pctx.SourcePathVariable("RSClangBase", "prebuilts/clang/host")
269 pctx.SourcePathVariable("RSClangVersion", "clang-3289846")
270 pctx.SourcePathVariable("RSReleaseVersion", "3.8")
271 pctx.StaticVariable("RSLLVMPrebuiltsPath", "${RSClangBase}/${HostPrebuiltTag}/${RSClangVersion}/bin")
272 pctx.StaticVariable("RSIncludePath", "${RSLLVMPrebuiltsPath}/../lib64/clang/${RSReleaseVersion}/include")
273
Jeff Gaston734e3802017-04-10 15:47:24 -0700274 pctx.PrefixedExistentPathsForSourcesVariable("RsGlobalIncludes", "-I",
Colin Cross2a252be2017-05-01 17:37:24 -0700275 []string{
276 "external/clang/lib/Headers",
277 "frameworks/rs/script_api/include",
278 })
279
Dan Willemsen54daaf02018-03-12 13:24:09 -0700280 pctx.VariableFunc("CcWrapper", func(ctx android.PackageVarContext) string {
281 if override := ctx.Config().Getenv("CC_WRAPPER"); override != "" {
282 return override + " "
Alistair Strachan777475c2016-08-26 12:55:49 -0700283 }
Dan Willemsen54daaf02018-03-12 13:24:09 -0700284 return ""
Alistair Strachan777475c2016-08-26 12:55:49 -0700285 })
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400286
Colin Cross77cdcfd2021-03-12 11:28:25 -0800287 pctx.StaticVariableWithEnvOverride("RECXXPool", "RBE_CXX_POOL", remoteexec.DefaultPool)
288 pctx.StaticVariableWithEnvOverride("RECXXLinksPool", "RBE_CXX_LINKS_POOL", remoteexec.DefaultPool)
289 pctx.StaticVariableWithEnvOverride("REClangTidyPool", "RBE_CLANG_TIDY_POOL", remoteexec.DefaultPool)
290 pctx.StaticVariableWithEnvOverride("RECXXLinksExecStrategy", "RBE_CXX_LINKS_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
291 pctx.StaticVariableWithEnvOverride("REClangTidyExecStrategy", "RBE_CLANG_TIDY_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
292 pctx.StaticVariableWithEnvOverride("REAbiDumperExecStrategy", "RBE_ABI_DUMPER_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
293 pctx.StaticVariableWithEnvOverride("REAbiLinkerExecStrategy", "RBE_ABI_LINKER_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700294}
295
296var HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", android.Config.PrebuiltOS)
297
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400298func envOverrideFunc(envVar, defaultVal string) func(ctx android.PackageVarContext) string {
299 return func(ctx android.PackageVarContext) string {
300 if override := ctx.Config().Getenv(envVar); override != "" {
301 return override
302 }
303 return defaultVal
304 }
305}