blob: 5e99cdebd64bc4533c9a1483e467ea8c98057b3c [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 (
Alex Naidis5df73d02017-04-05 20:08:41 +020018 "fmt"
Stephen Craneba090d12017-05-09 15:44:35 -070019 "runtime"
Colin Cross4d9c2d12016-07-29 12:48:20 -070020 "strings"
21
22 "android/soong/android"
23)
24
Colin Cross4d9c2d12016-07-29 12:48:20 -070025var (
Leo Lie748f5d2017-05-22 16:11:34 -070026 // Flags used by lots of devices. Putting them in package static variables
27 // will save bytes in build.ninja so they aren't repeated for every file
Colin Cross4d9c2d12016-07-29 12:48:20 -070028 commonGlobalCflags = []string{
29 "-DANDROID",
30 "-fmessage-length=0",
31 "-W",
32 "-Wall",
33 "-Wno-unused",
34 "-Winit-self",
35 "-Wpointer-arith",
36
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",
Colin Cross4d9c2d12016-07-29 12:48:20 -070051 }
52
Colin Cross6f6a4282016-10-17 14:19:06 -070053 commonGlobalConlyflags = []string{}
Elliott Hughes5a0401a2016-10-07 13:12:58 -070054
Colin Cross4d9c2d12016-07-29 12:48:20 -070055 deviceGlobalCflags = []string{
56 "-fdiagnostics-color",
57
Colin Cross133dbe72017-11-02 22:55:19 -070058 "-ffunction-sections",
Colin Crossea3141d2017-11-06 14:02:02 -080059 "-fdata-sections",
60 "-fno-short-enums",
Colin Cross133dbe72017-11-02 22:55:19 -070061 "-funwind-tables",
62 "-fstack-protector-strong",
63 "-Wa,--noexecstack",
64 "-D_FORTIFY_SOURCE=2",
65
66 "-Wstrict-aliasing=2",
67
Colin Cross4d9c2d12016-07-29 12:48:20 -070068 "-Werror=return-type",
69 "-Werror=non-virtual-dtor",
70 "-Werror=address",
71 "-Werror=sequence-point",
72 "-Werror=date-time",
Colin Cross133dbe72017-11-02 22:55:19 -070073 "-Werror=format-security",
Colin Cross4d9c2d12016-07-29 12:48:20 -070074 }
75
Colin Cross26f14502017-11-06 13:59:48 -080076 deviceGlobalCppflags = []string{
77 "-fvisibility-inlines-hidden",
78 }
79
Colin Cross324a4572017-11-02 23:09:41 -070080 deviceGlobalLdflags = []string{
81 "-Wl,-z,noexecstack",
82 "-Wl,-z,relro",
83 "-Wl,-z,now",
84 "-Wl,--build-id=md5",
85 "-Wl,--warn-shared-textrel",
86 "-Wl,--fatal-warnings",
87 "-Wl,--no-undefined-version",
88 }
89
Colin Cross4d9c2d12016-07-29 12:48:20 -070090 hostGlobalCflags = []string{}
91
Colin Cross26f14502017-11-06 13:59:48 -080092 hostGlobalCppflags = []string{}
93
Colin Cross324a4572017-11-02 23:09:41 -070094 hostGlobalLdflags = []string{}
95
Colin Cross4d9c2d12016-07-29 12:48:20 -070096 commonGlobalCppflags = []string{
97 "-Wsign-promo",
98 }
99
100 noOverrideGlobalCflags = []string{
101 "-Werror=int-to-pointer-cast",
102 "-Werror=pointer-to-int-cast",
103 }
104
Colin Crossb98c8b02016-07-29 13:44:28 -0700105 IllegalFlags = []string{
Colin Cross4d9c2d12016-07-29 12:48:20 -0700106 "-w",
107 }
Colin Cross6f6a4282016-10-17 14:19:06 -0700108
Dan Albert043833c2017-02-03 16:13:38 -0800109 CStdVersion = "gnu99"
110 CppStdVersion = "gnu++14"
111 GccCppStdVersion = "gnu++11"
112 ExperimentalCStdVersion = "gnu11"
113 ExperimentalCppStdVersion = "gnu++1z"
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700114
Dan Albertc715eda2018-01-16 16:21:06 -0800115 NdkMaxPrebuiltVersionInt = 27
Leo Lie748f5d2017-05-22 16:11:34 -0700116
117 // prebuilts/clang default settings.
118 ClangDefaultBase = "prebuilts/clang/host"
Stephen Hines0ed7d242017-10-04 16:12:37 -0700119 ClangDefaultVersion = "clang-4393122"
120 ClangDefaultShortVersion = "5.0.1"
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800121
Chih-Hung Hsieh775edde2017-12-24 22:24:47 -0800122 // Directories with warnings from Android.bp files.
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800123 WarningAllowedProjects = []string{
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800124 "device/",
Chih-Hung Hsieh64a38dc2017-11-14 14:09:14 -0800125 "vendor/",
126 }
127
Chih-Hung Hsieh775edde2017-12-24 22:24:47 -0800128 // Directories with warnings from Android.mk files.
129 WarningAllowedOldProjects = []string{}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700130)
131
Colin Crossb98c8b02016-07-29 13:44:28 -0700132var pctx = android.NewPackageContext("android/soong/cc/config")
133
Colin Cross4d9c2d12016-07-29 12:48:20 -0700134func init() {
135 if android.BuildOs == android.Linux {
136 commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=")
137 }
138
Colin Crossb98c8b02016-07-29 13:44:28 -0700139 pctx.StaticVariable("CommonGlobalCflags", strings.Join(commonGlobalCflags, " "))
Elliott Hughes5a0401a2016-10-07 13:12:58 -0700140 pctx.StaticVariable("CommonGlobalConlyflags", strings.Join(commonGlobalConlyflags, " "))
Colin Crossb98c8b02016-07-29 13:44:28 -0700141 pctx.StaticVariable("DeviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
Colin Cross26f14502017-11-06 13:59:48 -0800142 pctx.StaticVariable("DeviceGlobalCppflags", strings.Join(deviceGlobalCppflags, " "))
Colin Cross324a4572017-11-02 23:09:41 -0700143 pctx.StaticVariable("DeviceGlobalLdflags", strings.Join(deviceGlobalLdflags, " "))
Colin Crossb98c8b02016-07-29 13:44:28 -0700144 pctx.StaticVariable("HostGlobalCflags", strings.Join(hostGlobalCflags, " "))
Colin Cross26f14502017-11-06 13:59:48 -0800145 pctx.StaticVariable("HostGlobalCppflags", strings.Join(hostGlobalCppflags, " "))
Colin Cross324a4572017-11-02 23:09:41 -0700146 pctx.StaticVariable("HostGlobalLdflags", strings.Join(hostGlobalLdflags, " "))
Colin Crossb98c8b02016-07-29 13:44:28 -0700147 pctx.StaticVariable("NoOverrideGlobalCflags", strings.Join(noOverrideGlobalCflags, " "))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700148
Colin Crossb98c8b02016-07-29 13:44:28 -0700149 pctx.StaticVariable("CommonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700150
Colin Crossb98c8b02016-07-29 13:44:28 -0700151 pctx.StaticVariable("CommonClangGlobalCflags",
152 strings.Join(append(ClangFilterUnknownCflags(commonGlobalCflags), "${ClangExtraCflags}"), " "))
153 pctx.StaticVariable("DeviceClangGlobalCflags",
154 strings.Join(append(ClangFilterUnknownCflags(deviceGlobalCflags), "${ClangExtraTargetCflags}"), " "))
155 pctx.StaticVariable("HostClangGlobalCflags",
156 strings.Join(ClangFilterUnknownCflags(hostGlobalCflags), " "))
157 pctx.StaticVariable("NoOverrideClangGlobalCflags",
158 strings.Join(append(ClangFilterUnknownCflags(noOverrideGlobalCflags), "${ClangExtraNoOverrideCflags}"), " "))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700159
Colin Crossb98c8b02016-07-29 13:44:28 -0700160 pctx.StaticVariable("CommonClangGlobalCppflags",
161 strings.Join(append(ClangFilterUnknownCflags(commonGlobalCppflags), "${ClangExtraCppflags}"), " "))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700162
Colin Cross1cfd89a2016-09-15 09:30:46 -0700163 // Everything in these lists is a crime against abstraction and dependency tracking.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700164 // Do not add anything to this list.
Jeff Gaston734e3802017-04-10 15:47:24 -0700165 pctx.PrefixedExistentPathsForSourcesVariable("CommonGlobalIncludes", "-I",
Colin Crosse4bba1e2016-09-22 15:29:50 -0700166 []string{
Colin Cross763a26c2016-09-23 15:48:51 +0000167 "system/core/include",
Colin Cross19280932016-10-05 12:36:42 -0700168 "system/media/audio/include",
Colin Cross2d44c2c2016-10-05 12:36:42 -0700169 "hardware/libhardware/include",
Colin Cross328f04e2016-11-03 15:45:34 -0700170 "hardware/libhardware_legacy/include",
171 "hardware/ril/include",
172 "libnativehelper/include",
Colin Cross315a6ff2016-10-05 12:36:42 -0700173 "frameworks/native/include",
Colin Cross14e8dd72016-12-14 11:13:16 -0800174 "frameworks/native/opengl/include",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700175 "frameworks/av/include",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700176 })
177 // This is used by non-NDK modules to get jni.h. export_include_dirs doesn't help
178 // with this, since there is no associated library.
Jeff Gaston734e3802017-04-10 15:47:24 -0700179 pctx.PrefixedExistentPathsForSourcesVariable("CommonNativehelperInclude", "-I",
Steven Moreland3cf63062017-07-18 13:29:35 -0700180 []string{"libnativehelper/include_deprecated"})
Colin Cross4d9c2d12016-07-29 12:48:20 -0700181
Leo Lie748f5d2017-05-22 16:11:34 -0700182 pctx.SourcePathVariable("ClangDefaultBase", ClangDefaultBase)
Colin Cross0875c522017-11-28 17:34:01 -0800183 pctx.VariableFunc("ClangBase", func(config android.Config) (string, error) {
184 if override := config.Getenv("LLVM_PREBUILTS_BASE"); override != "" {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700185 return override, nil
186 }
Colin Crossb98c8b02016-07-29 13:44:28 -0700187 return "${ClangDefaultBase}", nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700188 })
Colin Cross0875c522017-11-28 17:34:01 -0800189 pctx.VariableFunc("ClangVersion", func(config android.Config) (string, error) {
190 if override := config.Getenv("LLVM_PREBUILTS_VERSION"); override != "" {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700191 return override, nil
192 }
Leo Lie748f5d2017-05-22 16:11:34 -0700193 return ClangDefaultVersion, nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700194 })
Colin Crossb98c8b02016-07-29 13:44:28 -0700195 pctx.StaticVariable("ClangPath", "${ClangBase}/${HostPrebuiltTag}/${ClangVersion}")
196 pctx.StaticVariable("ClangBin", "${ClangPath}/bin")
197
Colin Cross0875c522017-11-28 17:34:01 -0800198 pctx.VariableFunc("ClangShortVersion", func(config android.Config) (string, error) {
199 if override := config.Getenv("LLVM_RELEASE_VERSION"); override != "" {
Stephen Hinese55a4cc2016-11-18 17:12:38 -0800200 return override, nil
201 }
Leo Lie748f5d2017-05-22 16:11:34 -0700202 return ClangDefaultShortVersion, nil
Stephen Hinese55a4cc2016-11-18 17:12:38 -0800203 })
204 pctx.StaticVariable("ClangAsanLibDir", "${ClangPath}/lib64/clang/${ClangShortVersion}/lib/linux")
Stephen Craneba090d12017-05-09 15:44:35 -0700205 if runtime.GOOS == "darwin" {
206 pctx.StaticVariable("LLVMGoldPlugin", "${ClangPath}/lib64/LLVMgold.dylib")
207 } else {
208 pctx.StaticVariable("LLVMGoldPlugin", "${ClangPath}/lib64/LLVMgold.so")
209 }
Alistair Strachan777475c2016-08-26 12:55:49 -0700210
Jayant Chowdharye622d202017-02-01 19:19:52 -0800211 // These are tied to the version of LLVM directly in external/llvm, so they might trail the host prebuilts
212 // being used for the rest of the build process.
213 pctx.SourcePathVariable("RSClangBase", "prebuilts/clang/host")
214 pctx.SourcePathVariable("RSClangVersion", "clang-3289846")
215 pctx.SourcePathVariable("RSReleaseVersion", "3.8")
216 pctx.StaticVariable("RSLLVMPrebuiltsPath", "${RSClangBase}/${HostPrebuiltTag}/${RSClangVersion}/bin")
217 pctx.StaticVariable("RSIncludePath", "${RSLLVMPrebuiltsPath}/../lib64/clang/${RSReleaseVersion}/include")
218
Jeff Gaston734e3802017-04-10 15:47:24 -0700219 pctx.PrefixedExistentPathsForSourcesVariable("RsGlobalIncludes", "-I",
Colin Cross2a252be2017-05-01 17:37:24 -0700220 []string{
221 "external/clang/lib/Headers",
222 "frameworks/rs/script_api/include",
223 })
224
Colin Cross0875c522017-11-28 17:34:01 -0800225 pctx.VariableFunc("CcWrapper", func(config android.Config) (string, error) {
226 if override := config.Getenv("CC_WRAPPER"); override != "" {
Alistair Strachan777475c2016-08-26 12:55:49 -0700227 return override + " ", nil
228 }
229 return "", nil
230 })
Colin Cross4d9c2d12016-07-29 12:48:20 -0700231}
232
233var HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", android.Config.PrebuiltOS)
234
Elliott Hughesde28deb2017-10-12 09:07:53 -0700235func bionicHeaders(kernelArch string) string {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700236 return strings.Join([]string{
Colin Cross4d9c2d12016-07-29 12:48:20 -0700237 "-isystem bionic/libc/include",
238 "-isystem bionic/libc/kernel/uapi",
239 "-isystem bionic/libc/kernel/uapi/asm-" + kernelArch,
Elliott Hughes98418a02017-05-25 17:16:10 -0700240 "-isystem bionic/libc/kernel/android/scsi",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700241 "-isystem bionic/libc/kernel/android/uapi",
242 }, " ")
243}
Dan Willemsend2ede872016-11-18 14:54:24 -0800244
Alex Naidis5df73d02017-04-05 20:08:41 +0200245func replaceFirst(slice []string, from, to string) {
246 if slice[0] != from {
247 panic(fmt.Errorf("Expected %q, found %q", from, to))
248 }
249 slice[0] = to
250}