blob: d4c626699ec5343d410ef99c06db32ff8264918a [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",
49
50 "-fno-strict-aliasing",
Dan Willemsen5d980c82019-08-27 19:37:10 -070051
52 "-Werror=date-time",
Elliott Hughes2cdbdf12019-12-03 18:13:00 -080053 "-Werror=pragma-pack",
54 "-Werror=pragma-pack-suspicious-include",
George Burgess IVfb81db22020-02-22 19:28:56 -080055 "-Werror=unreachable-code-loop-increment",
Colin Cross4d9c2d12016-07-29 12:48:20 -070056 }
57
Colin Cross6f6a4282016-10-17 14:19:06 -070058 commonGlobalConlyflags = []string{}
Elliott Hughes5a0401a2016-10-07 13:12:58 -070059
Colin Cross4d9c2d12016-07-29 12:48:20 -070060 deviceGlobalCflags = []string{
61 "-fdiagnostics-color",
62
Colin Cross133dbe72017-11-02 22:55:19 -070063 "-ffunction-sections",
Colin Crossea3141d2017-11-06 14:02:02 -080064 "-fdata-sections",
65 "-fno-short-enums",
Colin Cross133dbe72017-11-02 22:55:19 -070066 "-funwind-tables",
67 "-fstack-protector-strong",
68 "-Wa,--noexecstack",
69 "-D_FORTIFY_SOURCE=2",
70
71 "-Wstrict-aliasing=2",
72
Colin Cross4d9c2d12016-07-29 12:48:20 -070073 "-Werror=return-type",
74 "-Werror=non-virtual-dtor",
75 "-Werror=address",
76 "-Werror=sequence-point",
Colin Cross133dbe72017-11-02 22:55:19 -070077 "-Werror=format-security",
Colin Cross4d9c2d12016-07-29 12:48:20 -070078 }
79
Colin Cross26f14502017-11-06 13:59:48 -080080 deviceGlobalCppflags = []string{
81 "-fvisibility-inlines-hidden",
82 }
83
Colin Cross324a4572017-11-02 23:09:41 -070084 deviceGlobalLdflags = []string{
85 "-Wl,-z,noexecstack",
86 "-Wl,-z,relro",
87 "-Wl,-z,now",
88 "-Wl,--build-id=md5",
89 "-Wl,--warn-shared-textrel",
90 "-Wl,--fatal-warnings",
91 "-Wl,--no-undefined-version",
Christopher Ferrisc3a1e222019-04-10 17:57:50 -070092 "-Wl,--exclude-libs,libgcc.a",
Yi Kong3d8792f2019-05-06 16:18:33 -070093 "-Wl,--exclude-libs,libgcc_stripped.a",
Peter Collingbournee5ba2862019-12-10 18:37:45 -080094 "-Wl,--exclude-libs,libunwind_llvm.a",
Colin Cross4d9c2d12016-07-29 12:48:20 -070095 }
96
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070097 deviceGlobalLldflags = append(ClangFilterUnknownLldflags(deviceGlobalLdflags),
98 []string{
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070099 "-fuse-ld=lld",
100 }...)
101
Colin Cross4d9c2d12016-07-29 12:48:20 -0700102 hostGlobalCflags = []string{}
103
Colin Cross26f14502017-11-06 13:59:48 -0800104 hostGlobalCppflags = []string{}
105
Colin Cross324a4572017-11-02 23:09:41 -0700106 hostGlobalLdflags = []string{}
107
Chih-Hung Hsieh3101a962018-04-17 14:16:05 -0700108 hostGlobalLldflags = []string{"-fuse-ld=lld"}
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700109
Colin Cross4d9c2d12016-07-29 12:48:20 -0700110 commonGlobalCppflags = []string{
111 "-Wsign-promo",
112 }
113
114 noOverrideGlobalCflags = []string{
115 "-Werror=int-to-pointer-cast",
116 "-Werror=pointer-to-int-cast",
Stephen Hines2210e722020-07-15 11:11:57 -0700117 // http://b/161386391 for -Wno-void-pointer-to-enum-cast
118 "-Wno-void-pointer-to-enum-cast",
119 // http://b/161386391 for -Wno-void-pointer-to-int-cast
120 "-Wno-void-pointer-to-int-cast",
121 // http://b/161386391 for -Wno-pointer-to-int-cast
122 "-Wno-pointer-to-int-cast",
George Burgess IV6c691642019-09-18 17:52:05 -0700123 "-Werror=fortify-source",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700124 }
125
Colin Crossb98c8b02016-07-29 13:44:28 -0700126 IllegalFlags = []string{
Colin Cross4d9c2d12016-07-29 12:48:20 -0700127 "-w",
128 }
Colin Cross6f6a4282016-10-17 14:19:06 -0700129
Dan Albert043833c2017-02-03 16:13:38 -0800130 CStdVersion = "gnu99"
Elliott Hughes34e4e412018-11-30 16:03:06 +0000131 CppStdVersion = "gnu++17"
Dan Albert043833c2017-02-03 16:13:38 -0800132 ExperimentalCStdVersion = "gnu11"
Elliott Hughes37976122018-11-28 14:16:39 -0800133 ExperimentalCppStdVersion = "gnu++2a"
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700134
Leo Li8756b372017-05-22 16:11:34 -0700135 // prebuilts/clang default settings.
136 ClangDefaultBase = "prebuilts/clang/host"
Stephen Hines5060c9e2020-09-30 19:07:42 -0700137 ClangDefaultVersion = "clang-r399163b"
138 ClangDefaultShortVersion = "11.0.5"
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800139
Chih-Hung Hsieh775edde2017-12-24 22:24:47 -0800140 // Directories with warnings from Android.bp files.
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800141 WarningAllowedProjects = []string{
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800142 "device/",
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800143 "vendor/",
144 }
145
Chih-Hung Hsieh775edde2017-12-24 22:24:47 -0800146 // Directories with warnings from Android.mk files.
147 WarningAllowedOldProjects = []string{}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700148)
149
Colin Crossb98c8b02016-07-29 13:44:28 -0700150var pctx = android.NewPackageContext("android/soong/cc/config")
151
Colin Cross4d9c2d12016-07-29 12:48:20 -0700152func init() {
153 if android.BuildOs == android.Linux {
154 commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=")
155 }
156
Elliott Hughes5a0401a2016-10-07 13:12:58 -0700157 pctx.StaticVariable("CommonGlobalConlyflags", strings.Join(commonGlobalConlyflags, " "))
Colin Cross26f14502017-11-06 13:59:48 -0800158 pctx.StaticVariable("DeviceGlobalCppflags", strings.Join(deviceGlobalCppflags, " "))
Colin Cross324a4572017-11-02 23:09:41 -0700159 pctx.StaticVariable("DeviceGlobalLdflags", strings.Join(deviceGlobalLdflags, " "))
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700160 pctx.StaticVariable("DeviceGlobalLldflags", strings.Join(deviceGlobalLldflags, " "))
Colin Cross26f14502017-11-06 13:59:48 -0800161 pctx.StaticVariable("HostGlobalCppflags", strings.Join(hostGlobalCppflags, " "))
Colin Cross324a4572017-11-02 23:09:41 -0700162 pctx.StaticVariable("HostGlobalLdflags", strings.Join(hostGlobalLdflags, " "))
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700163 pctx.StaticVariable("HostGlobalLldflags", strings.Join(hostGlobalLldflags, " "))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700164
Stephen Hines66c8b442019-06-10 17:40:12 -0700165 pctx.VariableFunc("CommonClangGlobalCflags", func(ctx android.PackageVarContext) string {
166 flags := ClangFilterUnknownCflags(commonGlobalCflags)
167 flags = append(flags, "${ClangExtraCflags}")
168
169 // http://b/131390872
170 // Automatically initialize any uninitialized stack variables.
Stephen Hines5c873ac2020-05-14 00:59:09 +0000171 // Prefer zero-init if multiple options are set.
Stephen Hines66c8b442019-06-10 17:40:12 -0700172 if ctx.Config().IsEnvTrue("AUTO_ZERO_INITIALIZE") {
173 flags = append(flags, "-ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang")
174 } else if ctx.Config().IsEnvTrue("AUTO_PATTERN_INITIALIZE") {
175 flags = append(flags, "-ftrivial-auto-var-init=pattern")
Stephen Hines797e1952020-01-28 14:43:11 -0800176 } else if ctx.Config().IsEnvTrue("AUTO_UNINITIALIZE") {
177 flags = append(flags, "-ftrivial-auto-var-init=uninitialized")
Stephen Hines0e1d5d82020-01-30 15:06:00 -0800178 } else {
Stephen Hines5c873ac2020-05-14 00:59:09 +0000179 // Default to zero initialization.
180 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 -0700181 }
182
183 return strings.Join(flags, " ")
184 })
185
Doug Hornc32c6b02019-01-17 14:44:05 -0800186 pctx.VariableFunc("DeviceClangGlobalCflags", func(ctx android.PackageVarContext) string {
187 if ctx.Config().Fuchsia() {
188 return strings.Join(ClangFilterUnknownCflags(deviceGlobalCflags), " ")
189 } else {
190 return strings.Join(append(ClangFilterUnknownCflags(deviceGlobalCflags), "${ClangExtraTargetCflags}"), " ")
191 }
192 })
Colin Crossb98c8b02016-07-29 13:44:28 -0700193 pctx.StaticVariable("HostClangGlobalCflags",
194 strings.Join(ClangFilterUnknownCflags(hostGlobalCflags), " "))
195 pctx.StaticVariable("NoOverrideClangGlobalCflags",
196 strings.Join(append(ClangFilterUnknownCflags(noOverrideGlobalCflags), "${ClangExtraNoOverrideCflags}"), " "))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700197
Colin Crossb98c8b02016-07-29 13:44:28 -0700198 pctx.StaticVariable("CommonClangGlobalCppflags",
199 strings.Join(append(ClangFilterUnknownCflags(commonGlobalCppflags), "${ClangExtraCppflags}"), " "))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700200
Yi Kongcc80f8d2018-06-06 14:42:44 -0700201 pctx.StaticVariable("ClangExternalCflags", "${ClangExtraExternalCflags}")
202
Colin Cross1cfd89a2016-09-15 09:30:46 -0700203 // Everything in these lists is a crime against abstraction and dependency tracking.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700204 // Do not add anything to this list.
Jeff Gaston734e3802017-04-10 15:47:24 -0700205 pctx.PrefixedExistentPathsForSourcesVariable("CommonGlobalIncludes", "-I",
Colin Crosse4bba1e2016-09-22 15:29:50 -0700206 []string{
Colin Cross763a26c2016-09-23 15:48:51 +0000207 "system/core/include",
Colin Cross19280932016-10-05 12:36:42 -0700208 "system/media/audio/include",
Colin Cross2d44c2c2016-10-05 12:36:42 -0700209 "hardware/libhardware/include",
Colin Cross328f04e2016-11-03 15:45:34 -0700210 "hardware/libhardware_legacy/include",
211 "hardware/ril/include",
Colin Cross315a6ff2016-10-05 12:36:42 -0700212 "frameworks/native/include",
Colin Cross14e8dd72016-12-14 11:13:16 -0800213 "frameworks/native/opengl/include",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700214 "frameworks/av/include",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700215 })
216 // This is used by non-NDK modules to get jni.h. export_include_dirs doesn't help
217 // with this, since there is no associated library.
Jeff Gaston734e3802017-04-10 15:47:24 -0700218 pctx.PrefixedExistentPathsForSourcesVariable("CommonNativehelperInclude", "-I",
Steven Moreland3afa3cd2017-09-25 11:22:02 -0700219 []string{"libnativehelper/include_jni"})
Colin Cross4d9c2d12016-07-29 12:48:20 -0700220
Leo Li8756b372017-05-22 16:11:34 -0700221 pctx.SourcePathVariable("ClangDefaultBase", ClangDefaultBase)
Dan Willemsen54daaf02018-03-12 13:24:09 -0700222 pctx.VariableFunc("ClangBase", func(ctx android.PackageVarContext) string {
223 if override := ctx.Config().Getenv("LLVM_PREBUILTS_BASE"); override != "" {
224 return override
Colin Cross4d9c2d12016-07-29 12:48:20 -0700225 }
Dan Willemsen54daaf02018-03-12 13:24:09 -0700226 return "${ClangDefaultBase}"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700227 })
Dan Willemsen54daaf02018-03-12 13:24:09 -0700228 pctx.VariableFunc("ClangVersion", func(ctx android.PackageVarContext) string {
229 if override := ctx.Config().Getenv("LLVM_PREBUILTS_VERSION"); override != "" {
230 return override
Colin Cross4d9c2d12016-07-29 12:48:20 -0700231 }
Dan Willemsen54daaf02018-03-12 13:24:09 -0700232 return ClangDefaultVersion
Colin Cross4d9c2d12016-07-29 12:48:20 -0700233 })
Colin Crossb98c8b02016-07-29 13:44:28 -0700234 pctx.StaticVariable("ClangPath", "${ClangBase}/${HostPrebuiltTag}/${ClangVersion}")
235 pctx.StaticVariable("ClangBin", "${ClangPath}/bin")
236
Dan Willemsen54daaf02018-03-12 13:24:09 -0700237 pctx.VariableFunc("ClangShortVersion", func(ctx android.PackageVarContext) string {
238 if override := ctx.Config().Getenv("LLVM_RELEASE_VERSION"); override != "" {
239 return override
Stephen Hinese55a4cc2016-11-18 17:12:38 -0800240 }
Dan Willemsen54daaf02018-03-12 13:24:09 -0700241 return ClangDefaultShortVersion
Stephen Hinese55a4cc2016-11-18 17:12:38 -0800242 })
Stephen Hines755fe072018-01-24 19:58:36 -0800243 pctx.StaticVariable("ClangAsanLibDir", "${ClangBase}/linux-x86/${ClangVersion}/lib64/clang/${ClangShortVersion}/lib/linux")
Alistair Strachan777475c2016-08-26 12:55:49 -0700244
Jayant Chowdharye622d202017-02-01 19:19:52 -0800245 // These are tied to the version of LLVM directly in external/llvm, so they might trail the host prebuilts
246 // being used for the rest of the build process.
247 pctx.SourcePathVariable("RSClangBase", "prebuilts/clang/host")
248 pctx.SourcePathVariable("RSClangVersion", "clang-3289846")
249 pctx.SourcePathVariable("RSReleaseVersion", "3.8")
250 pctx.StaticVariable("RSLLVMPrebuiltsPath", "${RSClangBase}/${HostPrebuiltTag}/${RSClangVersion}/bin")
251 pctx.StaticVariable("RSIncludePath", "${RSLLVMPrebuiltsPath}/../lib64/clang/${RSReleaseVersion}/include")
252
Jeff Gaston734e3802017-04-10 15:47:24 -0700253 pctx.PrefixedExistentPathsForSourcesVariable("RsGlobalIncludes", "-I",
Colin Cross2a252be2017-05-01 17:37:24 -0700254 []string{
255 "external/clang/lib/Headers",
256 "frameworks/rs/script_api/include",
257 })
258
Dan Willemsen54daaf02018-03-12 13:24:09 -0700259 pctx.VariableFunc("CcWrapper", func(ctx android.PackageVarContext) string {
260 if override := ctx.Config().Getenv("CC_WRAPPER"); override != "" {
261 return override + " "
Alistair Strachan777475c2016-08-26 12:55:49 -0700262 }
Dan Willemsen54daaf02018-03-12 13:24:09 -0700263 return ""
Alistair Strachan777475c2016-08-26 12:55:49 -0700264 })
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400265
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400266 pctx.VariableFunc("RECXXPool", remoteexec.EnvOverrideFunc("RBE_CXX_POOL", remoteexec.DefaultPool))
267 pctx.VariableFunc("RECXXLinksPool", remoteexec.EnvOverrideFunc("RBE_CXX_LINKS_POOL", remoteexec.DefaultPool))
Kousik Kumar4e30bba2020-06-18 09:17:51 -0700268 pctx.VariableFunc("REClangTidyPool", remoteexec.EnvOverrideFunc("RBE_CLANG_TIDY_POOL", remoteexec.DefaultPool))
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400269 pctx.VariableFunc("RECXXLinksExecStrategy", remoteexec.EnvOverrideFunc("RBE_CXX_LINKS_EXEC_STRATEGY", remoteexec.LocalExecStrategy))
Kousik Kumar4e30bba2020-06-18 09:17:51 -0700270 pctx.VariableFunc("REClangTidyExecStrategy", remoteexec.EnvOverrideFunc("RBE_CLANG_TIDY_EXEC_STRATEGY", remoteexec.LocalExecStrategy))
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400271 pctx.VariableFunc("REAbiDumperExecStrategy", remoteexec.EnvOverrideFunc("RBE_ABI_DUMPER_EXEC_STRATEGY", remoteexec.LocalExecStrategy))
Ramy Medhat808594c2020-05-07 06:56:47 -0400272 pctx.VariableFunc("REAbiLinkerExecStrategy", remoteexec.EnvOverrideFunc("RBE_ABI_LINKER_EXEC_STRATEGY", remoteexec.LocalExecStrategy))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700273}
274
275var HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", android.Config.PrebuiltOS)
276
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400277func envOverrideFunc(envVar, defaultVal string) func(ctx android.PackageVarContext) string {
278 return func(ctx android.PackageVarContext) string {
279 if override := ctx.Config().Getenv(envVar); override != "" {
280 return override
281 }
282 return defaultVal
283 }
284}