blob: e5cb7eefda74cbe5789645b562b5ff8a0866b1b9 [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",
George Burgess IVfb81db22020-02-22 19:28:56 -080056 "-Werror=unreachable-code-loop-increment",
Colin Cross4d9c2d12016-07-29 12:48:20 -070057 }
58
Colin Cross6f6a4282016-10-17 14:19:06 -070059 commonGlobalConlyflags = []string{}
Elliott Hughes5a0401a2016-10-07 13:12:58 -070060
Colin Cross4d9c2d12016-07-29 12:48:20 -070061 deviceGlobalCflags = []string{
62 "-fdiagnostics-color",
63
Colin Cross133dbe72017-11-02 22:55:19 -070064 "-ffunction-sections",
Colin Crossea3141d2017-11-06 14:02:02 -080065 "-fdata-sections",
66 "-fno-short-enums",
Colin Cross133dbe72017-11-02 22:55:19 -070067 "-funwind-tables",
68 "-fstack-protector-strong",
69 "-Wa,--noexecstack",
70 "-D_FORTIFY_SOURCE=2",
71
72 "-Wstrict-aliasing=2",
73
Colin Cross4d9c2d12016-07-29 12:48:20 -070074 "-Werror=return-type",
75 "-Werror=non-virtual-dtor",
76 "-Werror=address",
77 "-Werror=sequence-point",
Colin Cross133dbe72017-11-02 22:55:19 -070078 "-Werror=format-security",
Colin Cross4d9c2d12016-07-29 12:48:20 -070079 }
80
Colin Cross26f14502017-11-06 13:59:48 -080081 deviceGlobalCppflags = []string{
82 "-fvisibility-inlines-hidden",
83 }
84
Colin Cross324a4572017-11-02 23:09:41 -070085 deviceGlobalLdflags = []string{
86 "-Wl,-z,noexecstack",
87 "-Wl,-z,relro",
88 "-Wl,-z,now",
89 "-Wl,--build-id=md5",
90 "-Wl,--warn-shared-textrel",
91 "-Wl,--fatal-warnings",
92 "-Wl,--no-undefined-version",
Christopher Ferrisc3a1e222019-04-10 17:57:50 -070093 "-Wl,--exclude-libs,libgcc.a",
Yi Kong3d8792f2019-05-06 16:18:33 -070094 "-Wl,--exclude-libs,libgcc_stripped.a",
Peter Collingbournee5ba2862019-12-10 18:37:45 -080095 "-Wl,--exclude-libs,libunwind_llvm.a",
Colin Cross4d9c2d12016-07-29 12:48:20 -070096 }
97
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070098 deviceGlobalLldflags = append(ClangFilterUnknownLldflags(deviceGlobalLdflags),
99 []string{
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700100 "-fuse-ld=lld",
101 }...)
102
Colin Cross4d9c2d12016-07-29 12:48:20 -0700103 hostGlobalCflags = []string{}
104
Colin Cross26f14502017-11-06 13:59:48 -0800105 hostGlobalCppflags = []string{}
106
Colin Cross324a4572017-11-02 23:09:41 -0700107 hostGlobalLdflags = []string{}
108
Chih-Hung Hsieh3101a962018-04-17 14:16:05 -0700109 hostGlobalLldflags = []string{"-fuse-ld=lld"}
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700110
Colin Cross4d9c2d12016-07-29 12:48:20 -0700111 commonGlobalCppflags = []string{
112 "-Wsign-promo",
113 }
114
115 noOverrideGlobalCflags = []string{
116 "-Werror=int-to-pointer-cast",
117 "-Werror=pointer-to-int-cast",
Stephen Hines2210e722020-07-15 11:11:57 -0700118 // http://b/161386391 for -Wno-void-pointer-to-enum-cast
119 "-Wno-void-pointer-to-enum-cast",
120 // http://b/161386391 for -Wno-void-pointer-to-int-cast
121 "-Wno-void-pointer-to-int-cast",
122 // http://b/161386391 for -Wno-pointer-to-int-cast
123 "-Wno-pointer-to-int-cast",
George Burgess IV6c691642019-09-18 17:52:05 -0700124 "-Werror=fortify-source",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700125 }
126
Colin Crossb98c8b02016-07-29 13:44:28 -0700127 IllegalFlags = []string{
Colin Cross4d9c2d12016-07-29 12:48:20 -0700128 "-w",
129 }
Colin Cross6f6a4282016-10-17 14:19:06 -0700130
Dan Albert043833c2017-02-03 16:13:38 -0800131 CStdVersion = "gnu99"
Elliott Hughes34e4e412018-11-30 16:03:06 +0000132 CppStdVersion = "gnu++17"
Dan Albert043833c2017-02-03 16:13:38 -0800133 ExperimentalCStdVersion = "gnu11"
Elliott Hughes37976122018-11-28 14:16:39 -0800134 ExperimentalCppStdVersion = "gnu++2a"
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700135
Leo Li8756b372017-05-22 16:11:34 -0700136 // prebuilts/clang default settings.
137 ClangDefaultBase = "prebuilts/clang/host"
Stephen Hines5060c9e2020-09-30 19:07:42 -0700138 ClangDefaultVersion = "clang-r399163b"
139 ClangDefaultShortVersion = "11.0.5"
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800140
Chih-Hung Hsieh775edde2017-12-24 22:24:47 -0800141 // Directories with warnings from Android.bp files.
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800142 WarningAllowedProjects = []string{
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800143 "device/",
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800144 "vendor/",
145 }
146
Chih-Hung Hsieh775edde2017-12-24 22:24:47 -0800147 // Directories with warnings from Android.mk files.
148 WarningAllowedOldProjects = []string{}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700149)
150
Colin Crossb98c8b02016-07-29 13:44:28 -0700151var pctx = android.NewPackageContext("android/soong/cc/config")
152
Colin Cross4d9c2d12016-07-29 12:48:20 -0700153func init() {
Kousik Kumard207cbe2020-10-20 05:52:49 +0000154 if android.BuildOs == android.Linux {
155 commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=")
156 }
157
Elliott Hughes5a0401a2016-10-07 13:12:58 -0700158 pctx.StaticVariable("CommonGlobalConlyflags", strings.Join(commonGlobalConlyflags, " "))
Colin Cross26f14502017-11-06 13:59:48 -0800159 pctx.StaticVariable("DeviceGlobalCppflags", strings.Join(deviceGlobalCppflags, " "))
Colin Cross324a4572017-11-02 23:09:41 -0700160 pctx.StaticVariable("DeviceGlobalLdflags", strings.Join(deviceGlobalLdflags, " "))
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700161 pctx.StaticVariable("DeviceGlobalLldflags", strings.Join(deviceGlobalLldflags, " "))
Colin Cross26f14502017-11-06 13:59:48 -0800162 pctx.StaticVariable("HostGlobalCppflags", strings.Join(hostGlobalCppflags, " "))
Colin Cross324a4572017-11-02 23:09:41 -0700163 pctx.StaticVariable("HostGlobalLdflags", strings.Join(hostGlobalLdflags, " "))
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700164 pctx.StaticVariable("HostGlobalLldflags", strings.Join(hostGlobalLldflags, " "))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700165
Stephen Hines66c8b442019-06-10 17:40:12 -0700166 pctx.VariableFunc("CommonClangGlobalCflags", func(ctx android.PackageVarContext) string {
167 flags := ClangFilterUnknownCflags(commonGlobalCflags)
168 flags = append(flags, "${ClangExtraCflags}")
169
170 // http://b/131390872
171 // Automatically initialize any uninitialized stack variables.
Stephen Hines5c873ac2020-05-14 00:59:09 +0000172 // Prefer zero-init if multiple options are set.
Stephen Hines66c8b442019-06-10 17:40:12 -0700173 if ctx.Config().IsEnvTrue("AUTO_ZERO_INITIALIZE") {
174 flags = append(flags, "-ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang")
175 } else if ctx.Config().IsEnvTrue("AUTO_PATTERN_INITIALIZE") {
176 flags = append(flags, "-ftrivial-auto-var-init=pattern")
Stephen Hines797e1952020-01-28 14:43:11 -0800177 } else if ctx.Config().IsEnvTrue("AUTO_UNINITIALIZE") {
178 flags = append(flags, "-ftrivial-auto-var-init=uninitialized")
Stephen Hines0e1d5d82020-01-30 15:06:00 -0800179 } else {
Stephen Hines5c873ac2020-05-14 00:59:09 +0000180 // Default to zero initialization.
181 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 -0700182 }
183
184 return strings.Join(flags, " ")
185 })
186
Doug Hornc32c6b02019-01-17 14:44:05 -0800187 pctx.VariableFunc("DeviceClangGlobalCflags", func(ctx android.PackageVarContext) string {
188 if ctx.Config().Fuchsia() {
189 return strings.Join(ClangFilterUnknownCflags(deviceGlobalCflags), " ")
190 } else {
191 return strings.Join(append(ClangFilterUnknownCflags(deviceGlobalCflags), "${ClangExtraTargetCflags}"), " ")
192 }
193 })
Colin Crossb98c8b02016-07-29 13:44:28 -0700194 pctx.StaticVariable("HostClangGlobalCflags",
195 strings.Join(ClangFilterUnknownCflags(hostGlobalCflags), " "))
196 pctx.StaticVariable("NoOverrideClangGlobalCflags",
197 strings.Join(append(ClangFilterUnknownCflags(noOverrideGlobalCflags), "${ClangExtraNoOverrideCflags}"), " "))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700198
Colin Crossb98c8b02016-07-29 13:44:28 -0700199 pctx.StaticVariable("CommonClangGlobalCppflags",
200 strings.Join(append(ClangFilterUnknownCflags(commonGlobalCppflags), "${ClangExtraCppflags}"), " "))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700201
Yi Kongcc80f8d2018-06-06 14:42:44 -0700202 pctx.StaticVariable("ClangExternalCflags", "${ClangExtraExternalCflags}")
203
Colin Cross1cfd89a2016-09-15 09:30:46 -0700204 // Everything in these lists is a crime against abstraction and dependency tracking.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700205 // Do not add anything to this list.
Jeff Gaston734e3802017-04-10 15:47:24 -0700206 pctx.PrefixedExistentPathsForSourcesVariable("CommonGlobalIncludes", "-I",
Colin Crosse4bba1e2016-09-22 15:29:50 -0700207 []string{
Colin Cross763a26c2016-09-23 15:48:51 +0000208 "system/core/include",
Tom Cherry3d7611e2020-10-09 14:49:58 -0700209 "system/logging/liblog/include",
Colin Cross19280932016-10-05 12:36:42 -0700210 "system/media/audio/include",
Colin Cross2d44c2c2016-10-05 12:36:42 -0700211 "hardware/libhardware/include",
Colin Cross328f04e2016-11-03 15:45:34 -0700212 "hardware/libhardware_legacy/include",
213 "hardware/ril/include",
Colin Cross315a6ff2016-10-05 12:36:42 -0700214 "frameworks/native/include",
Colin Cross14e8dd72016-12-14 11:13:16 -0800215 "frameworks/native/opengl/include",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700216 "frameworks/av/include",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700217 })
218 // This is used by non-NDK modules to get jni.h. export_include_dirs doesn't help
219 // with this, since there is no associated library.
Jeff Gaston734e3802017-04-10 15:47:24 -0700220 pctx.PrefixedExistentPathsForSourcesVariable("CommonNativehelperInclude", "-I",
Steven Moreland3afa3cd2017-09-25 11:22:02 -0700221 []string{"libnativehelper/include_jni"})
Colin Cross4d9c2d12016-07-29 12:48:20 -0700222
Leo Li8756b372017-05-22 16:11:34 -0700223 pctx.SourcePathVariable("ClangDefaultBase", ClangDefaultBase)
Dan Willemsen54daaf02018-03-12 13:24:09 -0700224 pctx.VariableFunc("ClangBase", func(ctx android.PackageVarContext) string {
225 if override := ctx.Config().Getenv("LLVM_PREBUILTS_BASE"); override != "" {
226 return override
Colin Cross4d9c2d12016-07-29 12:48:20 -0700227 }
Dan Willemsen54daaf02018-03-12 13:24:09 -0700228 return "${ClangDefaultBase}"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700229 })
Dan Willemsen54daaf02018-03-12 13:24:09 -0700230 pctx.VariableFunc("ClangVersion", func(ctx android.PackageVarContext) string {
231 if override := ctx.Config().Getenv("LLVM_PREBUILTS_VERSION"); override != "" {
232 return override
Colin Cross4d9c2d12016-07-29 12:48:20 -0700233 }
Dan Willemsen54daaf02018-03-12 13:24:09 -0700234 return ClangDefaultVersion
Colin Cross4d9c2d12016-07-29 12:48:20 -0700235 })
Colin Crossb98c8b02016-07-29 13:44:28 -0700236 pctx.StaticVariable("ClangPath", "${ClangBase}/${HostPrebuiltTag}/${ClangVersion}")
237 pctx.StaticVariable("ClangBin", "${ClangPath}/bin")
238
Dan Willemsen54daaf02018-03-12 13:24:09 -0700239 pctx.VariableFunc("ClangShortVersion", func(ctx android.PackageVarContext) string {
240 if override := ctx.Config().Getenv("LLVM_RELEASE_VERSION"); override != "" {
241 return override
Stephen Hinese55a4cc2016-11-18 17:12:38 -0800242 }
Dan Willemsen54daaf02018-03-12 13:24:09 -0700243 return ClangDefaultShortVersion
Stephen Hinese55a4cc2016-11-18 17:12:38 -0800244 })
Stephen Hines755fe072018-01-24 19:58:36 -0800245 pctx.StaticVariable("ClangAsanLibDir", "${ClangBase}/linux-x86/${ClangVersion}/lib64/clang/${ClangShortVersion}/lib/linux")
Alistair Strachan777475c2016-08-26 12:55:49 -0700246
Jayant Chowdharye622d202017-02-01 19:19:52 -0800247 // These are tied to the version of LLVM directly in external/llvm, so they might trail the host prebuilts
248 // being used for the rest of the build process.
249 pctx.SourcePathVariable("RSClangBase", "prebuilts/clang/host")
250 pctx.SourcePathVariable("RSClangVersion", "clang-3289846")
251 pctx.SourcePathVariable("RSReleaseVersion", "3.8")
252 pctx.StaticVariable("RSLLVMPrebuiltsPath", "${RSClangBase}/${HostPrebuiltTag}/${RSClangVersion}/bin")
253 pctx.StaticVariable("RSIncludePath", "${RSLLVMPrebuiltsPath}/../lib64/clang/${RSReleaseVersion}/include")
254
Jeff Gaston734e3802017-04-10 15:47:24 -0700255 pctx.PrefixedExistentPathsForSourcesVariable("RsGlobalIncludes", "-I",
Colin Cross2a252be2017-05-01 17:37:24 -0700256 []string{
257 "external/clang/lib/Headers",
258 "frameworks/rs/script_api/include",
259 })
260
Dan Willemsen54daaf02018-03-12 13:24:09 -0700261 pctx.VariableFunc("CcWrapper", func(ctx android.PackageVarContext) string {
262 if override := ctx.Config().Getenv("CC_WRAPPER"); override != "" {
263 return override + " "
Alistair Strachan777475c2016-08-26 12:55:49 -0700264 }
Dan Willemsen54daaf02018-03-12 13:24:09 -0700265 return ""
Alistair Strachan777475c2016-08-26 12:55:49 -0700266 })
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400267
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400268 pctx.VariableFunc("RECXXPool", remoteexec.EnvOverrideFunc("RBE_CXX_POOL", remoteexec.DefaultPool))
269 pctx.VariableFunc("RECXXLinksPool", remoteexec.EnvOverrideFunc("RBE_CXX_LINKS_POOL", remoteexec.DefaultPool))
Kousik Kumar4e30bba2020-06-18 09:17:51 -0700270 pctx.VariableFunc("REClangTidyPool", remoteexec.EnvOverrideFunc("RBE_CLANG_TIDY_POOL", remoteexec.DefaultPool))
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400271 pctx.VariableFunc("RECXXLinksExecStrategy", remoteexec.EnvOverrideFunc("RBE_CXX_LINKS_EXEC_STRATEGY", remoteexec.LocalExecStrategy))
Kousik Kumar4e30bba2020-06-18 09:17:51 -0700272 pctx.VariableFunc("REClangTidyExecStrategy", remoteexec.EnvOverrideFunc("RBE_CLANG_TIDY_EXEC_STRATEGY", remoteexec.LocalExecStrategy))
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400273 pctx.VariableFunc("REAbiDumperExecStrategy", remoteexec.EnvOverrideFunc("RBE_ABI_DUMPER_EXEC_STRATEGY", remoteexec.LocalExecStrategy))
Ramy Medhat808594c2020-05-07 06:56:47 -0400274 pctx.VariableFunc("REAbiLinkerExecStrategy", remoteexec.EnvOverrideFunc("RBE_ABI_LINKER_EXEC_STRATEGY", remoteexec.LocalExecStrategy))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700275}
276
277var HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", android.Config.PrebuiltOS)
278
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400279func envOverrideFunc(envVar, defaultVal string) func(ctx android.PackageVarContext) string {
280 return func(ctx android.PackageVarContext) string {
281 if override := ctx.Config().Getenv(envVar); override != "" {
282 return override
283 }
284 return defaultVal
285 }
286}