• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// 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
15package config
16
17import (
18	"strings"
19
20	"android/soong/android"
21	"android/soong/remoteexec"
22)
23
24var (
25	// 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
27	commonGlobalCflags = []string{
28		"-DANDROID",
29		"-fmessage-length=0",
30		"-W",
31		"-Wall",
32		"-Wno-unused",
33		"-Winit-self",
34		"-Wpointer-arith",
35		"-Wunreachable-code-loop-increment",
36
37		// Make paths in deps files relative
38		"-no-canonical-prefixes",
39		"-fno-canonical-system-headers",
40
41		"-DNDEBUG",
42		"-UDEBUG",
43
44		"-fno-exceptions",
45		"-Wno-multichar",
46
47		"-O2",
48		"-g",
49		"-fdebug-info-for-profiling",
50
51		"-fno-strict-aliasing",
52
53		"-Werror=date-time",
54		"-Werror=pragma-pack",
55		"-Werror=pragma-pack-suspicious-include",
56		"-Werror=string-plus-int",
57		"-Werror=unreachable-code-loop-increment",
58	}
59
60	commonGlobalConlyflags = []string{}
61
62	deviceGlobalCflags = []string{
63		"-fdiagnostics-color",
64
65		"-ffunction-sections",
66		"-fdata-sections",
67		"-fno-short-enums",
68		"-funwind-tables",
69		"-fstack-protector-strong",
70		"-Wa,--noexecstack",
71		"-D_FORTIFY_SOURCE=2",
72
73		"-Wstrict-aliasing=2",
74
75		"-Werror=return-type",
76		"-Werror=non-virtual-dtor",
77		"-Werror=address",
78		"-Werror=sequence-point",
79		"-Werror=format-security",
80	}
81
82	deviceGlobalCppflags = []string{
83		"-fvisibility-inlines-hidden",
84	}
85
86	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",
94		// TODO: Eventually we should link against a libunwind.a with hidden symbols, and then these
95		// --exclude-libs arguments can be removed.
96		"-Wl,--exclude-libs,libgcc.a",
97		"-Wl,--exclude-libs,libgcc_stripped.a",
98		"-Wl,--exclude-libs,libunwind_llvm.a",
99		"-Wl,--exclude-libs,libunwind.a",
100		"-Wl,--icf=safe",
101	}
102
103	deviceGlobalLldflags = append(ClangFilterUnknownLldflags(deviceGlobalLdflags),
104		[]string{
105			"-fuse-ld=lld",
106		}...)
107
108	hostGlobalCflags = []string{}
109
110	hostGlobalCppflags = []string{}
111
112	hostGlobalLdflags = []string{}
113
114	hostGlobalLldflags = []string{"-fuse-ld=lld"}
115
116	commonGlobalCppflags = []string{
117		"-Wsign-promo",
118	}
119
120	noOverrideGlobalCflags = []string{
121		"-Werror=bool-operation",
122		"-Werror=implicit-int-float-conversion",
123		"-Werror=int-in-bool-context",
124		"-Werror=int-to-pointer-cast",
125		"-Werror=pointer-to-int-cast",
126		"-Werror=string-compare",
127		"-Werror=xor-used-as-pow",
128		// 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",
134		"-Werror=fortify-source",
135	}
136
137	IllegalFlags = []string{
138		"-w",
139	}
140
141	CStdVersion               = "gnu99"
142	CppStdVersion             = "gnu++17"
143	ExperimentalCStdVersion   = "gnu11"
144	ExperimentalCppStdVersion = "gnu++2a"
145
146	// prebuilts/clang default settings.
147	ClangDefaultBase         = "prebuilts/clang/host"
148	ClangDefaultVersion      = "clang-r416183b1"
149	ClangDefaultShortVersion = "12.0.7"
150
151	// Directories with warnings from Android.bp files.
152	WarningAllowedProjects = []string{
153		"device/",
154		"vendor/",
155	}
156
157	// Directories with warnings from Android.mk files.
158	WarningAllowedOldProjects = []string{}
159)
160
161var pctx = android.NewPackageContext("android/soong/cc/config")
162
163func init() {
164	if android.BuildOs == android.Linux {
165		commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=")
166	}
167
168	staticVariableExportedToBazel("CommonGlobalConlyflags", commonGlobalConlyflags)
169	staticVariableExportedToBazel("DeviceGlobalCppflags", deviceGlobalCppflags)
170	staticVariableExportedToBazel("DeviceGlobalLdflags", deviceGlobalLdflags)
171	staticVariableExportedToBazel("DeviceGlobalLldflags", deviceGlobalLldflags)
172	staticVariableExportedToBazel("HostGlobalCppflags", hostGlobalCppflags)
173	staticVariableExportedToBazel("HostGlobalLdflags", hostGlobalLdflags)
174	staticVariableExportedToBazel("HostGlobalLldflags", hostGlobalLldflags)
175
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		}...)
186	exportedVars.Set("CommonClangGlobalCflags", variableValue(commonClangGlobalCFlags))
187
188	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.
194		// Prefer zero-init if multiple options are set.
195		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")
199		} else if ctx.Config().IsEnvTrue("AUTO_UNINITIALIZE") {
200			flags = append(flags, "-ftrivial-auto-var-init=uninitialized")
201		} else {
202			// 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")
204		}
205		return strings.Join(flags, " ")
206	})
207
208	// Export the static default DeviceClangGlobalCflags to Bazel.
209	// TODO(187086342): handle cflags that are set in VariableFuncs.
210	deviceClangGlobalCflags := append(ClangFilterUnknownCflags(deviceGlobalCflags), "${ClangExtraTargetCflags}")
211	exportedVars.Set("DeviceClangGlobalCflags", variableValue(deviceClangGlobalCflags))
212
213	pctx.VariableFunc("DeviceClangGlobalCflags", func(ctx android.PackageVarContext) string {
214		if ctx.Config().Fuchsia() {
215			return strings.Join(ClangFilterUnknownCflags(deviceGlobalCflags), " ")
216		} else {
217			return strings.Join(deviceClangGlobalCflags, " ")
218		}
219	})
220
221	staticVariableExportedToBazel("HostClangGlobalCflags", ClangFilterUnknownCflags(hostGlobalCflags))
222	staticVariableExportedToBazel("NoOverrideClangGlobalCflags", append(ClangFilterUnknownCflags(noOverrideGlobalCflags), "${ClangExtraNoOverrideCflags}"))
223	staticVariableExportedToBazel("CommonClangGlobalCppflags", append(ClangFilterUnknownCflags(commonGlobalCppflags), "${ClangExtraCppflags}"))
224	staticVariableExportedToBazel("ClangExternalCflags", []string{"${ClangExtraExternalCflags}"})
225
226	// Everything in these lists is a crime against abstraction and dependency tracking.
227	// Do not add anything to this list.
228	pctx.PrefixedExistentPathsForSourcesVariable("CommonGlobalIncludes", "-I",
229		[]string{
230			"system/core/include",
231			"system/logging/liblog/include",
232			"system/media/audio/include",
233			"hardware/libhardware/include",
234			"hardware/libhardware_legacy/include",
235			"hardware/ril/include",
236			"frameworks/native/include",
237			"frameworks/native/opengl/include",
238			"frameworks/av/include",
239		})
240
241	pctx.SourcePathVariable("ClangDefaultBase", ClangDefaultBase)
242	pctx.VariableFunc("ClangBase", func(ctx android.PackageVarContext) string {
243		if override := ctx.Config().Getenv("LLVM_PREBUILTS_BASE"); override != "" {
244			return override
245		}
246		return "${ClangDefaultBase}"
247	})
248	pctx.VariableFunc("ClangVersion", func(ctx android.PackageVarContext) string {
249		if override := ctx.Config().Getenv("LLVM_PREBUILTS_VERSION"); override != "" {
250			return override
251		}
252		return ClangDefaultVersion
253	})
254	pctx.StaticVariable("ClangPath", "${ClangBase}/${HostPrebuiltTag}/${ClangVersion}")
255	pctx.StaticVariable("ClangBin", "${ClangPath}/bin")
256
257	pctx.VariableFunc("ClangShortVersion", func(ctx android.PackageVarContext) string {
258		if override := ctx.Config().Getenv("LLVM_RELEASE_VERSION"); override != "" {
259			return override
260		}
261		return ClangDefaultShortVersion
262	})
263	pctx.StaticVariable("ClangAsanLibDir", "${ClangBase}/linux-x86/${ClangVersion}/lib64/clang/${ClangShortVersion}/lib/linux")
264
265	// These are tied to the version of LLVM directly in external/llvm, so they might trail the host prebuilts
266	// being used for the rest of the build process.
267	pctx.SourcePathVariable("RSClangBase", "prebuilts/clang/host")
268	pctx.SourcePathVariable("RSClangVersion", "clang-3289846")
269	pctx.SourcePathVariable("RSReleaseVersion", "3.8")
270	pctx.StaticVariable("RSLLVMPrebuiltsPath", "${RSClangBase}/${HostPrebuiltTag}/${RSClangVersion}/bin")
271	pctx.StaticVariable("RSIncludePath", "${RSLLVMPrebuiltsPath}/../lib64/clang/${RSReleaseVersion}/include")
272
273	pctx.PrefixedExistentPathsForSourcesVariable("RsGlobalIncludes", "-I",
274		[]string{
275			"external/clang/lib/Headers",
276			"frameworks/rs/script_api/include",
277		})
278
279	pctx.VariableFunc("CcWrapper", func(ctx android.PackageVarContext) string {
280		if override := ctx.Config().Getenv("CC_WRAPPER"); override != "" {
281			return override + " "
282		}
283		return ""
284	})
285
286	pctx.StaticVariableWithEnvOverride("RECXXPool", "RBE_CXX_POOL", remoteexec.DefaultPool)
287	pctx.StaticVariableWithEnvOverride("RECXXLinksPool", "RBE_CXX_LINKS_POOL", remoteexec.DefaultPool)
288	pctx.StaticVariableWithEnvOverride("REClangTidyPool", "RBE_CLANG_TIDY_POOL", remoteexec.DefaultPool)
289	pctx.StaticVariableWithEnvOverride("RECXXLinksExecStrategy", "RBE_CXX_LINKS_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
290	pctx.StaticVariableWithEnvOverride("REClangTidyExecStrategy", "RBE_CLANG_TIDY_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
291	pctx.StaticVariableWithEnvOverride("REAbiDumperExecStrategy", "RBE_ABI_DUMPER_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
292	pctx.StaticVariableWithEnvOverride("REAbiLinkerExecStrategy", "RBE_ABI_LINKER_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
293}
294
295var HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", android.Config.PrebuiltOS)
296
297func envOverrideFunc(envVar, defaultVal string) func(ctx android.PackageVarContext) string {
298	return func(ctx android.PackageVarContext) string {
299		if override := ctx.Config().Getenv(envVar); override != "" {
300			return override
301		}
302		return defaultVal
303	}
304}
305