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
50		"-fno-strict-aliasing",
51
52		"-Werror=date-time",
53		"-Werror=pragma-pack",
54		"-Werror=pragma-pack-suspicious-include",
55		"-Werror=unreachable-code-loop-increment",
56	}
57
58	commonGlobalConlyflags = []string{}
59
60	deviceGlobalCflags = []string{
61		"-fdiagnostics-color",
62
63		"-ffunction-sections",
64		"-fdata-sections",
65		"-fno-short-enums",
66		"-funwind-tables",
67		"-fstack-protector-strong",
68		"-Wa,--noexecstack",
69		"-D_FORTIFY_SOURCE=2",
70
71		"-Wstrict-aliasing=2",
72
73		"-Werror=return-type",
74		"-Werror=non-virtual-dtor",
75		"-Werror=address",
76		"-Werror=sequence-point",
77		"-Werror=format-security",
78	}
79
80	deviceGlobalCppflags = []string{
81		"-fvisibility-inlines-hidden",
82	}
83
84	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",
92		"-Wl,--exclude-libs,libgcc.a",
93		"-Wl,--exclude-libs,libgcc_stripped.a",
94		"-Wl,--exclude-libs,libunwind_llvm.a",
95	}
96
97	deviceGlobalLldflags = append(ClangFilterUnknownLldflags(deviceGlobalLdflags),
98		[]string{
99			"-fuse-ld=lld",
100		}...)
101
102	hostGlobalCflags = []string{}
103
104	hostGlobalCppflags = []string{}
105
106	hostGlobalLdflags = []string{}
107
108	hostGlobalLldflags = []string{"-fuse-ld=lld"}
109
110	commonGlobalCppflags = []string{
111		"-Wsign-promo",
112	}
113
114	noOverrideGlobalCflags = []string{
115		"-Werror=int-to-pointer-cast",
116		"-Werror=pointer-to-int-cast",
117		"-Werror=fortify-source",
118	}
119
120	IllegalFlags = []string{
121		"-w",
122	}
123
124	CStdVersion               = "gnu99"
125	CppStdVersion             = "gnu++17"
126	ExperimentalCStdVersion   = "gnu11"
127	ExperimentalCppStdVersion = "gnu++2a"
128
129	NdkMaxPrebuiltVersionInt = 27
130
131	// prebuilts/clang default settings.
132	ClangDefaultBase         = "prebuilts/clang/host"
133	ClangDefaultVersion      = "clang-r383902b"
134	ClangDefaultShortVersion = "11.0.2"
135
136	// Directories with warnings from Android.bp files.
137	WarningAllowedProjects = []string{
138		"device/",
139		"vendor/",
140	}
141
142	// Directories with warnings from Android.mk files.
143	WarningAllowedOldProjects = []string{}
144)
145
146var pctx = android.NewPackageContext("android/soong/cc/config")
147
148func init() {
149	if android.BuildOs == android.Linux {
150		commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=")
151	}
152
153	pctx.StaticVariable("CommonGlobalConlyflags", strings.Join(commonGlobalConlyflags, " "))
154	pctx.StaticVariable("DeviceGlobalCppflags", strings.Join(deviceGlobalCppflags, " "))
155	pctx.StaticVariable("DeviceGlobalLdflags", strings.Join(deviceGlobalLdflags, " "))
156	pctx.StaticVariable("DeviceGlobalLldflags", strings.Join(deviceGlobalLldflags, " "))
157	pctx.StaticVariable("HostGlobalCppflags", strings.Join(hostGlobalCppflags, " "))
158	pctx.StaticVariable("HostGlobalLdflags", strings.Join(hostGlobalLdflags, " "))
159	pctx.StaticVariable("HostGlobalLldflags", strings.Join(hostGlobalLldflags, " "))
160
161	pctx.VariableFunc("CommonClangGlobalCflags", func(ctx android.PackageVarContext) string {
162		flags := ClangFilterUnknownCflags(commonGlobalCflags)
163		flags = append(flags, "${ClangExtraCflags}")
164
165		// http://b/131390872
166		// Automatically initialize any uninitialized stack variables.
167		// Prefer zero-init if multiple options are set.
168		if ctx.Config().IsEnvTrue("AUTO_ZERO_INITIALIZE") {
169			flags = append(flags, "-ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang")
170		} else if ctx.Config().IsEnvTrue("AUTO_PATTERN_INITIALIZE") {
171			flags = append(flags, "-ftrivial-auto-var-init=pattern")
172		} else if ctx.Config().IsEnvTrue("AUTO_UNINITIALIZE") {
173			flags = append(flags, "-ftrivial-auto-var-init=uninitialized")
174		} else {
175			// Default to zero initialization.
176			flags = append(flags, "-ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang")
177		}
178
179		return strings.Join(flags, " ")
180	})
181
182	pctx.VariableFunc("DeviceClangGlobalCflags", func(ctx android.PackageVarContext) string {
183		if ctx.Config().Fuchsia() {
184			return strings.Join(ClangFilterUnknownCflags(deviceGlobalCflags), " ")
185		} else {
186			return strings.Join(append(ClangFilterUnknownCflags(deviceGlobalCflags), "${ClangExtraTargetCflags}"), " ")
187		}
188	})
189	pctx.StaticVariable("HostClangGlobalCflags",
190		strings.Join(ClangFilterUnknownCflags(hostGlobalCflags), " "))
191	pctx.StaticVariable("NoOverrideClangGlobalCflags",
192		strings.Join(append(ClangFilterUnknownCflags(noOverrideGlobalCflags), "${ClangExtraNoOverrideCflags}"), " "))
193
194	pctx.StaticVariable("CommonClangGlobalCppflags",
195		strings.Join(append(ClangFilterUnknownCflags(commonGlobalCppflags), "${ClangExtraCppflags}"), " "))
196
197	pctx.StaticVariable("ClangExternalCflags", "${ClangExtraExternalCflags}")
198
199	// Everything in these lists is a crime against abstraction and dependency tracking.
200	// Do not add anything to this list.
201	pctx.PrefixedExistentPathsForSourcesVariable("CommonGlobalIncludes", "-I",
202		[]string{
203			"system/core/include",
204			"system/media/audio/include",
205			"hardware/libhardware/include",
206			"hardware/libhardware_legacy/include",
207			"hardware/ril/include",
208			"frameworks/native/include",
209			"frameworks/native/opengl/include",
210			"frameworks/av/include",
211		})
212	// This is used by non-NDK modules to get jni.h. export_include_dirs doesn't help
213	// with this, since there is no associated library.
214	pctx.PrefixedExistentPathsForSourcesVariable("CommonNativehelperInclude", "-I",
215		[]string{"libnativehelper/include_jni"})
216
217	pctx.SourcePathVariable("ClangDefaultBase", ClangDefaultBase)
218	pctx.VariableFunc("ClangBase", func(ctx android.PackageVarContext) string {
219		if override := ctx.Config().Getenv("LLVM_PREBUILTS_BASE"); override != "" {
220			return override
221		}
222		return "${ClangDefaultBase}"
223	})
224	pctx.VariableFunc("ClangVersion", func(ctx android.PackageVarContext) string {
225		if override := ctx.Config().Getenv("LLVM_PREBUILTS_VERSION"); override != "" {
226			return override
227		}
228		return ClangDefaultVersion
229	})
230	pctx.StaticVariable("ClangPath", "${ClangBase}/${HostPrebuiltTag}/${ClangVersion}")
231	pctx.StaticVariable("ClangBin", "${ClangPath}/bin")
232
233	pctx.VariableFunc("ClangShortVersion", func(ctx android.PackageVarContext) string {
234		if override := ctx.Config().Getenv("LLVM_RELEASE_VERSION"); override != "" {
235			return override
236		}
237		return ClangDefaultShortVersion
238	})
239	pctx.StaticVariable("ClangAsanLibDir", "${ClangBase}/linux-x86/${ClangVersion}/lib64/clang/${ClangShortVersion}/lib/linux")
240
241	// These are tied to the version of LLVM directly in external/llvm, so they might trail the host prebuilts
242	// being used for the rest of the build process.
243	pctx.SourcePathVariable("RSClangBase", "prebuilts/clang/host")
244	pctx.SourcePathVariable("RSClangVersion", "clang-3289846")
245	pctx.SourcePathVariable("RSReleaseVersion", "3.8")
246	pctx.StaticVariable("RSLLVMPrebuiltsPath", "${RSClangBase}/${HostPrebuiltTag}/${RSClangVersion}/bin")
247	pctx.StaticVariable("RSIncludePath", "${RSLLVMPrebuiltsPath}/../lib64/clang/${RSReleaseVersion}/include")
248
249	pctx.PrefixedExistentPathsForSourcesVariable("RsGlobalIncludes", "-I",
250		[]string{
251			"external/clang/lib/Headers",
252			"frameworks/rs/script_api/include",
253		})
254
255	pctx.VariableFunc("CcWrapper", func(ctx android.PackageVarContext) string {
256		if override := ctx.Config().Getenv("CC_WRAPPER"); override != "" {
257			return override + " "
258		}
259		return ""
260	})
261
262	pctx.VariableFunc("RECXXPool", remoteexec.EnvOverrideFunc("RBE_CXX_POOL", remoteexec.DefaultPool))
263	pctx.VariableFunc("RECXXLinksPool", remoteexec.EnvOverrideFunc("RBE_CXX_LINKS_POOL", remoteexec.DefaultPool))
264	pctx.VariableFunc("RECXXLinksExecStrategy", remoteexec.EnvOverrideFunc("RBE_CXX_LINKS_EXEC_STRATEGY", remoteexec.LocalExecStrategy))
265	pctx.VariableFunc("REAbiDumperExecStrategy", remoteexec.EnvOverrideFunc("RBE_ABI_DUMPER_EXEC_STRATEGY", remoteexec.LocalExecStrategy))
266	pctx.VariableFunc("REAbiLinkerExecStrategy", remoteexec.EnvOverrideFunc("RBE_ABI_LINKER_EXEC_STRATEGY", remoteexec.LocalExecStrategy))
267}
268
269var HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", android.Config.PrebuiltOS)
270
271func envOverrideFunc(envVar, defaultVal string) func(ctx android.PackageVarContext) string {
272	return func(ctx android.PackageVarContext) string {
273		if override := ctx.Config().Getenv(envVar); override != "" {
274			return override
275		}
276		return defaultVal
277	}
278}
279