blob: 23106ec5ff9f454ad640c8c0beca8e4641b2040b [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
Elliott Hughes5a0401a2016-10-07 13:12:58 -0700168 pctx.StaticVariable("CommonGlobalConlyflags", strings.Join(commonGlobalConlyflags, " "))
Colin Cross26f14502017-11-06 13:59:48 -0800169 pctx.StaticVariable("DeviceGlobalCppflags", strings.Join(deviceGlobalCppflags, " "))
Colin Cross324a4572017-11-02 23:09:41 -0700170 pctx.StaticVariable("DeviceGlobalLdflags", strings.Join(deviceGlobalLdflags, " "))
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700171 pctx.StaticVariable("DeviceGlobalLldflags", strings.Join(deviceGlobalLldflags, " "))
Colin Cross26f14502017-11-06 13:59:48 -0800172 pctx.StaticVariable("HostGlobalCppflags", strings.Join(hostGlobalCppflags, " "))
Colin Cross324a4572017-11-02 23:09:41 -0700173 pctx.StaticVariable("HostGlobalLdflags", strings.Join(hostGlobalLdflags, " "))
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700174 pctx.StaticVariable("HostGlobalLldflags", strings.Join(hostGlobalLldflags, " "))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700175
Stephen Hines66c8b442019-06-10 17:40:12 -0700176 pctx.VariableFunc("CommonClangGlobalCflags", func(ctx android.PackageVarContext) string {
177 flags := ClangFilterUnknownCflags(commonGlobalCflags)
178 flags = append(flags, "${ClangExtraCflags}")
179
180 // http://b/131390872
181 // Automatically initialize any uninitialized stack variables.
Stephen Hines5c873ac2020-05-14 00:59:09 +0000182 // Prefer zero-init if multiple options are set.
Stephen Hines66c8b442019-06-10 17:40:12 -0700183 if ctx.Config().IsEnvTrue("AUTO_ZERO_INITIALIZE") {
184 flags = append(flags, "-ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang")
185 } else if ctx.Config().IsEnvTrue("AUTO_PATTERN_INITIALIZE") {
186 flags = append(flags, "-ftrivial-auto-var-init=pattern")
Stephen Hines797e1952020-01-28 14:43:11 -0800187 } else if ctx.Config().IsEnvTrue("AUTO_UNINITIALIZE") {
188 flags = append(flags, "-ftrivial-auto-var-init=uninitialized")
Stephen Hines0e1d5d82020-01-30 15:06:00 -0800189 } else {
Stephen Hines5c873ac2020-05-14 00:59:09 +0000190 // Default to zero initialization.
191 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 -0700192 }
193
194 return strings.Join(flags, " ")
195 })
196
Doug Hornc32c6b02019-01-17 14:44:05 -0800197 pctx.VariableFunc("DeviceClangGlobalCflags", func(ctx android.PackageVarContext) string {
198 if ctx.Config().Fuchsia() {
199 return strings.Join(ClangFilterUnknownCflags(deviceGlobalCflags), " ")
200 } else {
201 return strings.Join(append(ClangFilterUnknownCflags(deviceGlobalCflags), "${ClangExtraTargetCflags}"), " ")
202 }
203 })
Colin Crossb98c8b02016-07-29 13:44:28 -0700204 pctx.StaticVariable("HostClangGlobalCflags",
205 strings.Join(ClangFilterUnknownCflags(hostGlobalCflags), " "))
206 pctx.StaticVariable("NoOverrideClangGlobalCflags",
207 strings.Join(append(ClangFilterUnknownCflags(noOverrideGlobalCflags), "${ClangExtraNoOverrideCflags}"), " "))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700208
Colin Crossb98c8b02016-07-29 13:44:28 -0700209 pctx.StaticVariable("CommonClangGlobalCppflags",
210 strings.Join(append(ClangFilterUnknownCflags(commonGlobalCppflags), "${ClangExtraCppflags}"), " "))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700211
Yi Kongcc80f8d2018-06-06 14:42:44 -0700212 pctx.StaticVariable("ClangExternalCflags", "${ClangExtraExternalCflags}")
213
Colin Cross1cfd89a2016-09-15 09:30:46 -0700214 // Everything in these lists is a crime against abstraction and dependency tracking.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700215 // Do not add anything to this list.
Jeff Gaston734e3802017-04-10 15:47:24 -0700216 pctx.PrefixedExistentPathsForSourcesVariable("CommonGlobalIncludes", "-I",
Colin Crosse4bba1e2016-09-22 15:29:50 -0700217 []string{
Colin Cross763a26c2016-09-23 15:48:51 +0000218 "system/core/include",
Tom Cherry3d7611e2020-10-09 14:49:58 -0700219 "system/logging/liblog/include",
Colin Cross19280932016-10-05 12:36:42 -0700220 "system/media/audio/include",
Colin Cross2d44c2c2016-10-05 12:36:42 -0700221 "hardware/libhardware/include",
Colin Cross328f04e2016-11-03 15:45:34 -0700222 "hardware/libhardware_legacy/include",
223 "hardware/ril/include",
Colin Cross315a6ff2016-10-05 12:36:42 -0700224 "frameworks/native/include",
Colin Cross14e8dd72016-12-14 11:13:16 -0800225 "frameworks/native/opengl/include",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700226 "frameworks/av/include",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700227 })
Colin Cross4d9c2d12016-07-29 12:48:20 -0700228
Leo Li8756b372017-05-22 16:11:34 -0700229 pctx.SourcePathVariable("ClangDefaultBase", ClangDefaultBase)
Dan Willemsen54daaf02018-03-12 13:24:09 -0700230 pctx.VariableFunc("ClangBase", func(ctx android.PackageVarContext) string {
231 if override := ctx.Config().Getenv("LLVM_PREBUILTS_BASE"); override != "" {
232 return override
Colin Cross4d9c2d12016-07-29 12:48:20 -0700233 }
Dan Willemsen54daaf02018-03-12 13:24:09 -0700234 return "${ClangDefaultBase}"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700235 })
Dan Willemsen54daaf02018-03-12 13:24:09 -0700236 pctx.VariableFunc("ClangVersion", func(ctx android.PackageVarContext) string {
237 if override := ctx.Config().Getenv("LLVM_PREBUILTS_VERSION"); override != "" {
238 return override
Colin Cross4d9c2d12016-07-29 12:48:20 -0700239 }
Dan Willemsen54daaf02018-03-12 13:24:09 -0700240 return ClangDefaultVersion
Colin Cross4d9c2d12016-07-29 12:48:20 -0700241 })
Colin Crossb98c8b02016-07-29 13:44:28 -0700242 pctx.StaticVariable("ClangPath", "${ClangBase}/${HostPrebuiltTag}/${ClangVersion}")
243 pctx.StaticVariable("ClangBin", "${ClangPath}/bin")
244
Dan Willemsen54daaf02018-03-12 13:24:09 -0700245 pctx.VariableFunc("ClangShortVersion", func(ctx android.PackageVarContext) string {
246 if override := ctx.Config().Getenv("LLVM_RELEASE_VERSION"); override != "" {
247 return override
Stephen Hinese55a4cc2016-11-18 17:12:38 -0800248 }
Dan Willemsen54daaf02018-03-12 13:24:09 -0700249 return ClangDefaultShortVersion
Stephen Hinese55a4cc2016-11-18 17:12:38 -0800250 })
Stephen Hines755fe072018-01-24 19:58:36 -0800251 pctx.StaticVariable("ClangAsanLibDir", "${ClangBase}/linux-x86/${ClangVersion}/lib64/clang/${ClangShortVersion}/lib/linux")
Alistair Strachan777475c2016-08-26 12:55:49 -0700252
Jayant Chowdharye622d202017-02-01 19:19:52 -0800253 // These are tied to the version of LLVM directly in external/llvm, so they might trail the host prebuilts
254 // being used for the rest of the build process.
255 pctx.SourcePathVariable("RSClangBase", "prebuilts/clang/host")
256 pctx.SourcePathVariable("RSClangVersion", "clang-3289846")
257 pctx.SourcePathVariable("RSReleaseVersion", "3.8")
258 pctx.StaticVariable("RSLLVMPrebuiltsPath", "${RSClangBase}/${HostPrebuiltTag}/${RSClangVersion}/bin")
259 pctx.StaticVariable("RSIncludePath", "${RSLLVMPrebuiltsPath}/../lib64/clang/${RSReleaseVersion}/include")
260
Jeff Gaston734e3802017-04-10 15:47:24 -0700261 pctx.PrefixedExistentPathsForSourcesVariable("RsGlobalIncludes", "-I",
Colin Cross2a252be2017-05-01 17:37:24 -0700262 []string{
263 "external/clang/lib/Headers",
264 "frameworks/rs/script_api/include",
265 })
266
Dan Willemsen54daaf02018-03-12 13:24:09 -0700267 pctx.VariableFunc("CcWrapper", func(ctx android.PackageVarContext) string {
268 if override := ctx.Config().Getenv("CC_WRAPPER"); override != "" {
269 return override + " "
Alistair Strachan777475c2016-08-26 12:55:49 -0700270 }
Dan Willemsen54daaf02018-03-12 13:24:09 -0700271 return ""
Alistair Strachan777475c2016-08-26 12:55:49 -0700272 })
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400273
Colin Cross77cdcfd2021-03-12 11:28:25 -0800274 pctx.StaticVariableWithEnvOverride("RECXXPool", "RBE_CXX_POOL", remoteexec.DefaultPool)
275 pctx.StaticVariableWithEnvOverride("RECXXLinksPool", "RBE_CXX_LINKS_POOL", remoteexec.DefaultPool)
276 pctx.StaticVariableWithEnvOverride("REClangTidyPool", "RBE_CLANG_TIDY_POOL", remoteexec.DefaultPool)
277 pctx.StaticVariableWithEnvOverride("RECXXLinksExecStrategy", "RBE_CXX_LINKS_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
278 pctx.StaticVariableWithEnvOverride("REClangTidyExecStrategy", "RBE_CLANG_TIDY_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
279 pctx.StaticVariableWithEnvOverride("REAbiDumperExecStrategy", "RBE_ABI_DUMPER_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
280 pctx.StaticVariableWithEnvOverride("REAbiLinkerExecStrategy", "RBE_ABI_LINKER_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700281}
282
283var HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", android.Config.PrebuiltOS)
284
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400285func envOverrideFunc(envVar, defaultVal string) func(ctx android.PackageVarContext) string {
286 return func(ctx android.PackageVarContext) string {
287 if override := ctx.Config().Getenv(envVar); override != "" {
288 return override
289 }
290 return defaultVal
291 }
292}