blob: 7362f2e510a7f3a9fd7c4d05f9657730a5ab8862 [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",
39
Colin Cross4d9c2d12016-07-29 12:48:20 -070040 "-DNDEBUG",
41 "-UDEBUG",
Colin Cross7278afc2017-11-02 22:38:32 -070042
43 "-fno-exceptions",
44 "-Wno-multichar",
45
46 "-O2",
47 "-g",
48
49 "-fno-strict-aliasing",
Colin Cross4d9c2d12016-07-29 12:48:20 -070050 }
51
Colin Cross6f6a4282016-10-17 14:19:06 -070052 commonGlobalConlyflags = []string{}
Elliott Hughes5a0401a2016-10-07 13:12:58 -070053
Colin Cross4d9c2d12016-07-29 12:48:20 -070054 deviceGlobalCflags = []string{
55 "-fdiagnostics-color",
56
Colin Cross133dbe72017-11-02 22:55:19 -070057 "-fno-canonical-system-headers",
58 "-ffunction-sections",
59 "-funwind-tables",
60 "-fstack-protector-strong",
61 "-Wa,--noexecstack",
62 "-D_FORTIFY_SOURCE=2",
63
64 "-Wstrict-aliasing=2",
65
Colin Cross4d9c2d12016-07-29 12:48:20 -070066 "-Werror=return-type",
67 "-Werror=non-virtual-dtor",
68 "-Werror=address",
69 "-Werror=sequence-point",
70 "-Werror=date-time",
Colin Cross133dbe72017-11-02 22:55:19 -070071 "-Werror=format-security",
Colin Cross4d9c2d12016-07-29 12:48:20 -070072 }
73
Colin Cross324a4572017-11-02 23:09:41 -070074 deviceGlobalLdflags = []string{
75 "-Wl,-z,noexecstack",
76 "-Wl,-z,relro",
77 "-Wl,-z,now",
78 "-Wl,--build-id=md5",
79 "-Wl,--warn-shared-textrel",
80 "-Wl,--fatal-warnings",
81 "-Wl,--no-undefined-version",
82 }
83
Colin Cross4d9c2d12016-07-29 12:48:20 -070084 hostGlobalCflags = []string{}
85
Colin Cross324a4572017-11-02 23:09:41 -070086 hostGlobalLdflags = []string{}
87
Colin Cross4d9c2d12016-07-29 12:48:20 -070088 commonGlobalCppflags = []string{
89 "-Wsign-promo",
90 }
91
92 noOverrideGlobalCflags = []string{
93 "-Werror=int-to-pointer-cast",
94 "-Werror=pointer-to-int-cast",
95 }
96
Colin Crossb98c8b02016-07-29 13:44:28 -070097 IllegalFlags = []string{
Colin Cross4d9c2d12016-07-29 12:48:20 -070098 "-w",
99 }
Colin Cross6f6a4282016-10-17 14:19:06 -0700100
Dan Albert043833c2017-02-03 16:13:38 -0800101 CStdVersion = "gnu99"
102 CppStdVersion = "gnu++14"
103 GccCppStdVersion = "gnu++11"
104 ExperimentalCStdVersion = "gnu11"
105 ExperimentalCppStdVersion = "gnu++1z"
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700106
107 NdkMaxPrebuiltVersionInt = 24
Leo Lie748f5d2017-05-22 16:11:34 -0700108
109 // prebuilts/clang default settings.
110 ClangDefaultBase = "prebuilts/clang/host"
Stephen Hines0ed7d242017-10-04 16:12:37 -0700111 ClangDefaultVersion = "clang-4393122"
112 ClangDefaultShortVersion = "5.0.1"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700113)
114
Colin Crossb98c8b02016-07-29 13:44:28 -0700115var pctx = android.NewPackageContext("android/soong/cc/config")
116
Colin Cross4d9c2d12016-07-29 12:48:20 -0700117func init() {
118 if android.BuildOs == android.Linux {
119 commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=")
120 }
121
Colin Crossb98c8b02016-07-29 13:44:28 -0700122 pctx.StaticVariable("CommonGlobalCflags", strings.Join(commonGlobalCflags, " "))
Elliott Hughes5a0401a2016-10-07 13:12:58 -0700123 pctx.StaticVariable("CommonGlobalConlyflags", strings.Join(commonGlobalConlyflags, " "))
Colin Crossb98c8b02016-07-29 13:44:28 -0700124 pctx.StaticVariable("DeviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
Colin Cross324a4572017-11-02 23:09:41 -0700125 pctx.StaticVariable("DeviceGlobalLdflags", strings.Join(deviceGlobalLdflags, " "))
Colin Crossb98c8b02016-07-29 13:44:28 -0700126 pctx.StaticVariable("HostGlobalCflags", strings.Join(hostGlobalCflags, " "))
Colin Cross324a4572017-11-02 23:09:41 -0700127 pctx.StaticVariable("HostGlobalLdflags", strings.Join(hostGlobalLdflags, " "))
Colin Crossb98c8b02016-07-29 13:44:28 -0700128 pctx.StaticVariable("NoOverrideGlobalCflags", strings.Join(noOverrideGlobalCflags, " "))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700129
Colin Crossb98c8b02016-07-29 13:44:28 -0700130 pctx.StaticVariable("CommonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700131
Colin Crossb98c8b02016-07-29 13:44:28 -0700132 pctx.StaticVariable("CommonClangGlobalCflags",
133 strings.Join(append(ClangFilterUnknownCflags(commonGlobalCflags), "${ClangExtraCflags}"), " "))
134 pctx.StaticVariable("DeviceClangGlobalCflags",
135 strings.Join(append(ClangFilterUnknownCflags(deviceGlobalCflags), "${ClangExtraTargetCflags}"), " "))
136 pctx.StaticVariable("HostClangGlobalCflags",
137 strings.Join(ClangFilterUnknownCflags(hostGlobalCflags), " "))
138 pctx.StaticVariable("NoOverrideClangGlobalCflags",
139 strings.Join(append(ClangFilterUnknownCflags(noOverrideGlobalCflags), "${ClangExtraNoOverrideCflags}"), " "))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700140
Colin Crossb98c8b02016-07-29 13:44:28 -0700141 pctx.StaticVariable("CommonClangGlobalCppflags",
142 strings.Join(append(ClangFilterUnknownCflags(commonGlobalCppflags), "${ClangExtraCppflags}"), " "))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700143
Colin Cross1cfd89a2016-09-15 09:30:46 -0700144 // Everything in these lists is a crime against abstraction and dependency tracking.
Colin Cross4d9c2d12016-07-29 12:48:20 -0700145 // Do not add anything to this list.
Jeff Gaston734e3802017-04-10 15:47:24 -0700146 pctx.PrefixedExistentPathsForSourcesVariable("CommonGlobalIncludes", "-I",
Colin Crosse4bba1e2016-09-22 15:29:50 -0700147 []string{
Colin Cross763a26c2016-09-23 15:48:51 +0000148 "system/core/include",
Colin Cross19280932016-10-05 12:36:42 -0700149 "system/media/audio/include",
Colin Cross2d44c2c2016-10-05 12:36:42 -0700150 "hardware/libhardware/include",
Colin Cross328f04e2016-11-03 15:45:34 -0700151 "hardware/libhardware_legacy/include",
152 "hardware/ril/include",
153 "libnativehelper/include",
Colin Cross315a6ff2016-10-05 12:36:42 -0700154 "frameworks/native/include",
Colin Cross14e8dd72016-12-14 11:13:16 -0800155 "frameworks/native/opengl/include",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700156 "frameworks/av/include",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700157 })
158 // This is used by non-NDK modules to get jni.h. export_include_dirs doesn't help
159 // with this, since there is no associated library.
Jeff Gaston734e3802017-04-10 15:47:24 -0700160 pctx.PrefixedExistentPathsForSourcesVariable("CommonNativehelperInclude", "-I",
Steven Moreland3cf63062017-07-18 13:29:35 -0700161 []string{"libnativehelper/include_deprecated"})
Colin Cross4d9c2d12016-07-29 12:48:20 -0700162
Leo Lie748f5d2017-05-22 16:11:34 -0700163 pctx.SourcePathVariable("ClangDefaultBase", ClangDefaultBase)
Colin Crossb98c8b02016-07-29 13:44:28 -0700164 pctx.VariableFunc("ClangBase", func(config interface{}) (string, error) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700165 if override := config.(android.Config).Getenv("LLVM_PREBUILTS_BASE"); override != "" {
166 return override, nil
167 }
Colin Crossb98c8b02016-07-29 13:44:28 -0700168 return "${ClangDefaultBase}", nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700169 })
Colin Crossb98c8b02016-07-29 13:44:28 -0700170 pctx.VariableFunc("ClangVersion", func(config interface{}) (string, error) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700171 if override := config.(android.Config).Getenv("LLVM_PREBUILTS_VERSION"); override != "" {
172 return override, nil
173 }
Leo Lie748f5d2017-05-22 16:11:34 -0700174 return ClangDefaultVersion, nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700175 })
Colin Crossb98c8b02016-07-29 13:44:28 -0700176 pctx.StaticVariable("ClangPath", "${ClangBase}/${HostPrebuiltTag}/${ClangVersion}")
177 pctx.StaticVariable("ClangBin", "${ClangPath}/bin")
178
Stephen Hinese55a4cc2016-11-18 17:12:38 -0800179 pctx.VariableFunc("ClangShortVersion", func(config interface{}) (string, error) {
180 if override := config.(android.Config).Getenv("LLVM_RELEASE_VERSION"); override != "" {
181 return override, nil
182 }
Leo Lie748f5d2017-05-22 16:11:34 -0700183 return ClangDefaultShortVersion, nil
Stephen Hinese55a4cc2016-11-18 17:12:38 -0800184 })
185 pctx.StaticVariable("ClangAsanLibDir", "${ClangPath}/lib64/clang/${ClangShortVersion}/lib/linux")
Stephen Craneba090d12017-05-09 15:44:35 -0700186 if runtime.GOOS == "darwin" {
187 pctx.StaticVariable("LLVMGoldPlugin", "${ClangPath}/lib64/LLVMgold.dylib")
188 } else {
189 pctx.StaticVariable("LLVMGoldPlugin", "${ClangPath}/lib64/LLVMgold.so")
190 }
Alistair Strachan777475c2016-08-26 12:55:49 -0700191
Jayant Chowdharye622d202017-02-01 19:19:52 -0800192 // These are tied to the version of LLVM directly in external/llvm, so they might trail the host prebuilts
193 // being used for the rest of the build process.
194 pctx.SourcePathVariable("RSClangBase", "prebuilts/clang/host")
195 pctx.SourcePathVariable("RSClangVersion", "clang-3289846")
196 pctx.SourcePathVariable("RSReleaseVersion", "3.8")
197 pctx.StaticVariable("RSLLVMPrebuiltsPath", "${RSClangBase}/${HostPrebuiltTag}/${RSClangVersion}/bin")
198 pctx.StaticVariable("RSIncludePath", "${RSLLVMPrebuiltsPath}/../lib64/clang/${RSReleaseVersion}/include")
199
Jeff Gaston734e3802017-04-10 15:47:24 -0700200 pctx.PrefixedExistentPathsForSourcesVariable("RsGlobalIncludes", "-I",
Colin Cross2a252be2017-05-01 17:37:24 -0700201 []string{
202 "external/clang/lib/Headers",
203 "frameworks/rs/script_api/include",
204 })
205
Alistair Strachan777475c2016-08-26 12:55:49 -0700206 pctx.VariableFunc("CcWrapper", func(config interface{}) (string, error) {
207 if override := config.(android.Config).Getenv("CC_WRAPPER"); override != "" {
208 return override + " ", nil
209 }
210 return "", nil
211 })
Colin Cross4d9c2d12016-07-29 12:48:20 -0700212}
213
214var HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", android.Config.PrebuiltOS)
215
Elliott Hughesde28deb2017-10-12 09:07:53 -0700216func bionicHeaders(kernelArch string) string {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700217 return strings.Join([]string{
Colin Cross4d9c2d12016-07-29 12:48:20 -0700218 "-isystem bionic/libc/include",
219 "-isystem bionic/libc/kernel/uapi",
220 "-isystem bionic/libc/kernel/uapi/asm-" + kernelArch,
Elliott Hughes98418a02017-05-25 17:16:10 -0700221 "-isystem bionic/libc/kernel/android/scsi",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700222 "-isystem bionic/libc/kernel/android/uapi",
223 }, " ")
224}
Dan Willemsend2ede872016-11-18 14:54:24 -0800225
Alex Naidis5df73d02017-04-05 20:08:41 +0200226func replaceFirst(slice []string, from, to string) {
227 if slice[0] != from {
228 panic(fmt.Errorf("Expected %q, found %q", from, to))
229 }
230 slice[0] = to
231}