blob: 098914eecacb9defef4a30764005db510465706d [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
Dan Albertc715eda2018-01-16 16:21:06 -0800135 NdkMaxPrebuiltVersionInt = 27
Leo Li8756b372017-05-22 16:11:34 -0700136
137 // prebuilts/clang default settings.
138 ClangDefaultBase = "prebuilts/clang/host"
Stephen Hines2210e722020-07-15 11:11:57 -0700139 ClangDefaultVersion = "clang-r399163"
140 ClangDefaultShortVersion = "11.0.4"
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800141
Chih-Hung Hsieh775edde2017-12-24 22:24:47 -0800142 // Directories with warnings from Android.bp files.
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800143 WarningAllowedProjects = []string{
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800144 "device/",
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800145 "vendor/",
146 }
147
Chih-Hung Hsieh775edde2017-12-24 22:24:47 -0800148 // Directories with warnings from Android.mk files.
149 WarningAllowedOldProjects = []string{}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700150)
151
Colin Crossb98c8b02016-07-29 13:44:28 -0700152var pctx = android.NewPackageContext("android/soong/cc/config")
153
Colin Cross4d9c2d12016-07-29 12:48:20 -0700154func init() {
155 if android.BuildOs == android.Linux {
156 commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=")
157 }
158
Elliott Hughes5a0401a2016-10-07 13:12:58 -0700159 pctx.StaticVariable("CommonGlobalConlyflags", strings.Join(commonGlobalConlyflags, " "))
Colin Cross26f14502017-11-06 13:59:48 -0800160 pctx.StaticVariable("DeviceGlobalCppflags", strings.Join(deviceGlobalCppflags, " "))
Colin Cross324a4572017-11-02 23:09:41 -0700161 pctx.StaticVariable("DeviceGlobalLdflags", strings.Join(deviceGlobalLdflags, " "))
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700162 pctx.StaticVariable("DeviceGlobalLldflags", strings.Join(deviceGlobalLldflags, " "))
Colin Cross26f14502017-11-06 13:59:48 -0800163 pctx.StaticVariable("HostGlobalCppflags", strings.Join(hostGlobalCppflags, " "))
Colin Cross324a4572017-11-02 23:09:41 -0700164 pctx.StaticVariable("HostGlobalLdflags", strings.Join(hostGlobalLdflags, " "))
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700165 pctx.StaticVariable("HostGlobalLldflags", strings.Join(hostGlobalLldflags, " "))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700166
Stephen Hines66c8b442019-06-10 17:40:12 -0700167 pctx.VariableFunc("CommonClangGlobalCflags", func(ctx android.PackageVarContext) string {
168 flags := ClangFilterUnknownCflags(commonGlobalCflags)
169 flags = append(flags, "${ClangExtraCflags}")
170
171 // http://b/131390872
172 // Automatically initialize any uninitialized stack variables.
Stephen Hines5c873ac2020-05-14 00:59:09 +0000173 // Prefer zero-init if multiple options are set.
Stephen Hines66c8b442019-06-10 17:40:12 -0700174 if ctx.Config().IsEnvTrue("AUTO_ZERO_INITIALIZE") {
175 flags = append(flags, "-ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang")
176 } else if ctx.Config().IsEnvTrue("AUTO_PATTERN_INITIALIZE") {
177 flags = append(flags, "-ftrivial-auto-var-init=pattern")
Stephen Hines797e1952020-01-28 14:43:11 -0800178 } else if ctx.Config().IsEnvTrue("AUTO_UNINITIALIZE") {
179 flags = append(flags, "-ftrivial-auto-var-init=uninitialized")
Stephen Hines0e1d5d82020-01-30 15:06:00 -0800180 } else {
Stephen Hines5c873ac2020-05-14 00:59:09 +0000181 // Default to zero initialization.
182 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 -0700183 }
184
185 return strings.Join(flags, " ")
186 })
187
Doug Hornc32c6b02019-01-17 14:44:05 -0800188 pctx.VariableFunc("DeviceClangGlobalCflags", func(ctx android.PackageVarContext) string {
189 if ctx.Config().Fuchsia() {
190 return strings.Join(ClangFilterUnknownCflags(deviceGlobalCflags), " ")
191 } else {
192 return strings.Join(append(ClangFilterUnknownCflags(deviceGlobalCflags), "${ClangExtraTargetCflags}"), " ")
193 }
194 })
Colin Crossb98c8b02016-07-29 13:44:28 -0700195 pctx.StaticVariable("HostClangGlobalCflags",
196 strings.Join(ClangFilterUnknownCflags(hostGlobalCflags), " "))
197 pctx.StaticVariable("NoOverrideClangGlobalCflags",
198 strings.Join(append(ClangFilterUnknownCflags(noOverrideGlobalCflags), "${ClangExtraNoOverrideCflags}"), " "))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700199
Colin Crossb98c8b02016-07-29 13:44:28 -0700200 pctx.StaticVariable("CommonClangGlobalCppflags",
201 strings.Join(append(ClangFilterUnknownCflags(commonGlobalCppflags), "${ClangExtraCppflags}"), " "))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700202
Yi Kongcc80f8d2018-06-06 14:42:44 -0700203 pctx.StaticVariable("ClangExternalCflags", "${ClangExtraExternalCflags}")
204
Colin Cross1cfd89a2016-09-15 09:30:46 -0700205 // Everything in these lists is a crime against abstraction and dependency tracking.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700206 // Do not add anything to this list.
Jeff Gaston734e3802017-04-10 15:47:24 -0700207 pctx.PrefixedExistentPathsForSourcesVariable("CommonGlobalIncludes", "-I",
Colin Crosse4bba1e2016-09-22 15:29:50 -0700208 []string{
Colin Cross763a26c2016-09-23 15:48:51 +0000209 "system/core/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}