blob: 11c316225b4f6db71bea811264654eca9423e5d8 [file] [log] [blame]
Dan Willemsen4b7d5de2016-01-12 23:20:28 -08001// 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 cc
16
17import (
18 "fmt"
Dan Willemsen65905f82016-09-14 20:29:05 -070019 "sort"
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080020 "strings"
21
Colin Cross635c3b02016-05-18 15:37:25 -070022 "android/soong/android"
Colin Crossb98c8b02016-07-29 13:44:28 -070023 "android/soong/cc/config"
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080024)
25
26func init() {
Colin Cross635c3b02016-05-18 15:37:25 -070027 android.RegisterMakeVarsProvider(pctx, makeVarsProvider)
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080028}
29
Colin Cross635c3b02016-05-18 15:37:25 -070030func makeVarsProvider(ctx android.MakeVarsContext) {
Dan Willemsen10091192016-12-14 15:50:54 -080031 ctx.Strict("LLVM_RELEASE_VERSION", "${config.ClangShortVersion}")
Colin Crossb98c8b02016-07-29 13:44:28 -070032 ctx.Strict("LLVM_PREBUILTS_VERSION", "${config.ClangVersion}")
33 ctx.Strict("LLVM_PREBUILTS_BASE", "${config.ClangBase}")
34 ctx.Strict("LLVM_PREBUILTS_PATH", "${config.ClangBin}")
35 ctx.Strict("CLANG", "${config.ClangBin}/clang")
36 ctx.Strict("CLANG_CXX", "${config.ClangBin}/clang++")
37 ctx.Strict("LLVM_AS", "${config.ClangBin}/llvm-as")
38 ctx.Strict("LLVM_LINK", "${config.ClangBin}/llvm-link")
Dan Willemsena03cf6d2016-09-26 15:45:04 -070039 ctx.Strict("PATH_TO_CLANG_TIDY", "${config.ClangBin}/clang-tidy")
Colin Crossb98c8b02016-07-29 13:44:28 -070040 ctx.StrictSorted("CLANG_CONFIG_UNKNOWN_CFLAGS", strings.Join(config.ClangUnknownCflags, " "))
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080041
Jayant Chowdharye622d202017-02-01 19:19:52 -080042 ctx.Strict("RS_LLVM_PREBUILTS_VERSION", "${config.RSClangVersion}")
43 ctx.Strict("RS_LLVM_PREBUILTS_BASE", "${config.RSClangBase}")
44 ctx.Strict("RS_LLVM_PREBUILTS_PATH", "${config.RSLLVMPrebuiltsPath}")
Jayant Chowdhary715cac32017-04-20 06:53:59 -070045 ctx.Strict("RS_LLVM_INCLUDES", "${config.RSIncludePath}")
Jayant Chowdharye622d202017-02-01 19:19:52 -080046 ctx.Strict("RS_CLANG", "${config.RSLLVMPrebuiltsPath}/clang")
47 ctx.Strict("RS_LLVM_AS", "${config.RSLLVMPrebuiltsPath}/llvm-as")
48 ctx.Strict("RS_LLVM_LINK", "${config.RSLLVMPrebuiltsPath}/llvm-link")
49
Colin Crossb98c8b02016-07-29 13:44:28 -070050 ctx.Strict("GLOBAL_CFLAGS_NO_OVERRIDE", "${config.NoOverrideGlobalCflags}")
51 ctx.Strict("GLOBAL_CLANG_CFLAGS_NO_OVERRIDE", "${config.ClangExtraNoOverrideCflags}")
Dan Willemsen5efc7062016-05-27 15:23:38 -070052 ctx.Strict("GLOBAL_CPPFLAGS_NO_OVERRIDE", "")
53 ctx.Strict("GLOBAL_CLANG_CPPFLAGS_NO_OVERRIDE", "")
Dan Willemsen97704ed2016-07-07 21:40:39 -070054 ctx.Strict("NDK_PREBUILT_SHARED_LIBRARIES", strings.Join(ndkPrebuiltSharedLibs, " "))
Dan Willemsen5efc7062016-05-27 15:23:38 -070055
Dan Willemsend2ede872016-11-18 14:54:24 -080056 if ctx.Config().ProductVariables.DeviceVndkVersion != nil {
57 ctx.Strict("BOARD_VNDK_VERSION", *ctx.Config().ProductVariables.DeviceVndkVersion)
58 } else {
59 ctx.Strict("BOARD_VNDK_VERSION", "")
60 }
Dan Willemsend2ede872016-11-18 14:54:24 -080061
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070062 ctx.Strict("ADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS", strings.Join(asanCflags, " "))
63 ctx.Strict("ADDRESS_SANITIZER_CONFIG_EXTRA_LDFLAGS", strings.Join(asanLdflags, " "))
64 ctx.Strict("ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES", strings.Join(asanLibs, " "))
Dan Willemsencbceaab2016-10-13 16:44:07 -070065
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070066 ctx.Strict("CFI_EXTRA_CFLAGS", strings.Join(cfiCflags, " "))
67 ctx.Strict("CFI_EXTRA_LDFLAGS", strings.Join(cfiLdflags, " "))
Vishwath Mohanf3918d32017-02-14 07:59:33 -080068
Ivan Lozano0c3a1ef2017-06-28 09:10:48 -070069 ctx.Strict("INTEGER_OVERFLOW_EXTRA_CFLAGS", strings.Join(intOverflowCflags, " "))
70
Colin Cross6f6a4282016-10-17 14:19:06 -070071 ctx.Strict("DEFAULT_C_STD_VERSION", config.CStdVersion)
72 ctx.Strict("DEFAULT_CPP_STD_VERSION", config.CppStdVersion)
73 ctx.Strict("DEFAULT_GCC_CPP_STD_VERSION", config.GccCppStdVersion)
Dan Albert043833c2017-02-03 16:13:38 -080074 ctx.Strict("EXPERIMENTAL_C_STD_VERSION", config.ExperimentalCStdVersion)
75 ctx.Strict("EXPERIMENTAL_CPP_STD_VERSION", config.ExperimentalCppStdVersion)
Colin Cross6f6a4282016-10-17 14:19:06 -070076
Dan Willemsena03cf6d2016-09-26 15:45:04 -070077 ctx.Strict("DEFAULT_GLOBAL_TIDY_CHECKS", "${config.TidyDefaultGlobalChecks}")
78 ctx.Strict("DEFAULT_LOCAL_TIDY_CHECKS", joinLocalTidyChecks(config.DefaultLocalTidyChecks))
79 ctx.Strict("DEFAULT_TIDY_HEADER_DIRS", "${config.TidyDefaultHeaderDirs}")
80
Dan Willemsene1240db2016-11-03 14:28:51 -070081 ctx.Strict("AIDL_CPP", "${aidlCmd}")
82
Colin Cross2a252be2017-05-01 17:37:24 -070083 ctx.Strict("RS_GLOBAL_INCLUDES", "${config.RsGlobalIncludes}")
84
Colin Cross307d1452017-04-30 17:45:07 -070085 includeFlags, err := ctx.Eval("${config.CommonGlobalIncludes}")
Colin Cross10c78c62016-07-20 12:14:19 -070086 if err != nil {
87 panic(err)
88 }
89 includes, systemIncludes := splitSystemIncludes(ctx, includeFlags)
90 ctx.StrictRaw("SRC_HEADERS", strings.Join(includes, " "))
91 ctx.StrictRaw("SRC_SYSTEM_HEADERS", strings.Join(systemIncludes, " "))
92
Dan Willemsen65905f82016-09-14 20:29:05 -070093 sort.Strings(ndkMigratedLibs)
Dan Albert914449f2016-06-17 16:45:24 -070094 ctx.Strict("NDK_MIGRATED_LIBS", strings.Join(ndkMigratedLibs, " "))
95
Colin Crossa1ad8d12016-06-01 17:09:44 -070096 hostTargets := ctx.Config().Targets[android.Host]
97 makeVarsToolchain(ctx, "", hostTargets[0])
98 if len(hostTargets) > 1 {
99 makeVarsToolchain(ctx, "2ND_", hostTargets[1])
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800100 }
101
Colin Crossa1ad8d12016-06-01 17:09:44 -0700102 crossTargets := ctx.Config().Targets[android.HostCross]
103 if len(crossTargets) > 0 {
104 makeVarsToolchain(ctx, "", crossTargets[0])
105 if len(crossTargets) > 1 {
106 makeVarsToolchain(ctx, "2ND_", crossTargets[1])
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800107 }
108 }
109
Colin Crossa1ad8d12016-06-01 17:09:44 -0700110 deviceTargets := ctx.Config().Targets[android.Device]
111 makeVarsToolchain(ctx, "", deviceTargets[0])
112 if len(deviceTargets) > 1 {
113 makeVarsToolchain(ctx, "2ND_", deviceTargets[1])
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800114 }
115}
116
Colin Cross635c3b02016-05-18 15:37:25 -0700117func makeVarsToolchain(ctx android.MakeVarsContext, secondPrefix string,
Colin Crossa1ad8d12016-06-01 17:09:44 -0700118 target android.Target) {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800119 var typePrefix string
Colin Crossa1ad8d12016-06-01 17:09:44 -0700120 switch target.Os.Class {
121 case android.Host:
122 typePrefix = "HOST_"
123 case android.HostCross:
124 typePrefix = "HOST_CROSS_"
125 case android.Device:
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800126 typePrefix = "TARGET_"
127 }
128 makePrefix := secondPrefix + typePrefix
129
Colin Crossb98c8b02016-07-29 13:44:28 -0700130 toolchain := config.FindToolchain(target.Os, target.Arch)
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800131
Dan Willemsen36cff8b2016-05-17 16:35:02 -0700132 var productExtraCflags string
133 var productExtraLdflags string
Colin Crossa1ad8d12016-06-01 17:09:44 -0700134
Colin Crossb98c8b02016-07-29 13:44:28 -0700135 hod := "Host"
Colin Crossa1ad8d12016-06-01 17:09:44 -0700136 if target.Os.Class == android.Device {
Colin Crossb98c8b02016-07-29 13:44:28 -0700137 hod = "Device"
Colin Crossa1ad8d12016-06-01 17:09:44 -0700138 }
139
140 if target.Os.Class == android.Device && Bool(ctx.Config().ProductVariables.Brillo) {
Dan Willemsen36cff8b2016-05-17 16:35:02 -0700141 productExtraCflags += "-D__BRILLO__"
142 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700143 if target.Os.Class == android.Host && Bool(ctx.Config().ProductVariables.HostStaticBinaries) {
Dan Willemsen36cff8b2016-05-17 16:35:02 -0700144 productExtraLdflags += "-static"
Dan Willemsen06f45332016-05-16 19:32:33 -0700145 }
146
Dan Willemsen9598e102016-05-19 16:59:04 -0700147 ctx.Strict(makePrefix+"GLOBAL_CFLAGS", strings.Join([]string{
Dan Willemsend26a7132016-05-18 23:00:57 -0700148 toolchain.Cflags(),
Colin Crossb98c8b02016-07-29 13:44:28 -0700149 "${config.CommonGlobalCflags}",
150 fmt.Sprintf("${config.%sGlobalCflags}", hod),
Dan Willemsend26a7132016-05-18 23:00:57 -0700151 toolchain.ToolchainCflags(),
Dan Willemsen36cff8b2016-05-17 16:35:02 -0700152 productExtraCflags,
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800153 }, " "))
Elliott Hughes5a0401a2016-10-07 13:12:58 -0700154 ctx.Strict(makePrefix+"GLOBAL_CONLYFLAGS", strings.Join([]string{
155 "${config.CommonGlobalConlyflags}",
156 }, " "))
Dan Willemsen9598e102016-05-19 16:59:04 -0700157 ctx.Strict(makePrefix+"GLOBAL_CPPFLAGS", strings.Join([]string{
Colin Crossb98c8b02016-07-29 13:44:28 -0700158 "${config.CommonGlobalCppflags}",
Dan Willemsen216a4592016-05-16 23:56:53 -0700159 toolchain.Cppflags(),
160 }, " "))
Dan Willemsen9598e102016-05-19 16:59:04 -0700161 ctx.Strict(makePrefix+"GLOBAL_LDFLAGS", strings.Join([]string{
Dan Willemsend26a7132016-05-18 23:00:57 -0700162 toolchain.Ldflags(),
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800163 toolchain.ToolchainLdflags(),
Dan Willemsen36cff8b2016-05-17 16:35:02 -0700164 productExtraLdflags,
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800165 }, " "))
Dan Willemsen558e5172016-05-19 16:58:46 -0700166
167 includeFlags, err := ctx.Eval(toolchain.IncludeFlags())
Colin Crossa1ad8d12016-06-01 17:09:44 -0700168 if err != nil {
169 panic(err)
170 }
Colin Cross10c78c62016-07-20 12:14:19 -0700171 includes, systemIncludes := splitSystemIncludes(ctx, includeFlags)
172 ctx.StrictRaw(makePrefix+"C_INCLUDES", strings.Join(includes, " "))
173 ctx.StrictRaw(makePrefix+"C_SYSTEM_INCLUDES", strings.Join(systemIncludes, " "))
Dan Willemsen558e5172016-05-19 16:58:46 -0700174
Colin Crossa1ad8d12016-06-01 17:09:44 -0700175 if target.Arch.ArchType == android.Arm {
Dan Willemsen558e5172016-05-19 16:58:46 -0700176 flags, err := toolchain.InstructionSetFlags("arm")
Colin Crossa1ad8d12016-06-01 17:09:44 -0700177 if err != nil {
178 panic(err)
179 }
Dan Willemsen558e5172016-05-19 16:58:46 -0700180 ctx.Strict(makePrefix+"arm_CFLAGS", flags)
181
182 flags, err = toolchain.InstructionSetFlags("thumb")
Colin Crossa1ad8d12016-06-01 17:09:44 -0700183 if err != nil {
184 panic(err)
185 }
Dan Willemsen558e5172016-05-19 16:58:46 -0700186 ctx.Strict(makePrefix+"thumb_CFLAGS", flags)
187 }
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800188
189 if toolchain.ClangSupported() {
190 clangPrefix := secondPrefix + "CLANG_" + typePrefix
Dan Willemsen3772da12016-05-16 18:01:46 -0700191 clangExtras := "-target " + toolchain.ClangTriple()
Dan Willemsen300151b2017-03-13 12:40:30 -0700192 clangExtras += " -B" + config.ToolPath(toolchain)
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800193
Dan Willemsen9598e102016-05-19 16:59:04 -0700194 ctx.Strict(clangPrefix+"GLOBAL_CFLAGS", strings.Join([]string{
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800195 toolchain.ClangCflags(),
Colin Crossb98c8b02016-07-29 13:44:28 -0700196 "${config.CommonClangGlobalCflags}",
197 fmt.Sprintf("${config.%sClangGlobalCflags}", hod),
Dan Willemsend26a7132016-05-18 23:00:57 -0700198 toolchain.ToolchainClangCflags(),
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800199 clangExtras,
Dan Willemsend26a7132016-05-18 23:00:57 -0700200 productExtraCflags,
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800201 }, " "))
Dan Willemsen9598e102016-05-19 16:59:04 -0700202 ctx.Strict(clangPrefix+"GLOBAL_CPPFLAGS", strings.Join([]string{
Colin Crossb98c8b02016-07-29 13:44:28 -0700203 "${config.CommonClangGlobalCppflags}",
Dan Willemsen216a4592016-05-16 23:56:53 -0700204 toolchain.ClangCppflags(),
205 }, " "))
Dan Willemsen9598e102016-05-19 16:59:04 -0700206 ctx.Strict(clangPrefix+"GLOBAL_LDFLAGS", strings.Join([]string{
Dan Willemsend26a7132016-05-18 23:00:57 -0700207 toolchain.ClangLdflags(),
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800208 toolchain.ToolchainClangLdflags(),
Dan Willemsen36cff8b2016-05-17 16:35:02 -0700209 productExtraLdflags,
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800210 clangExtras,
211 }, " "))
Dan Willemsen558e5172016-05-19 16:58:46 -0700212
Colin Crossa1ad8d12016-06-01 17:09:44 -0700213 if target.Os.Class == android.Device {
Evgenii Stepanovaf36db12016-08-15 14:18:24 -0700214 ctx.Strict(secondPrefix+"ADDRESS_SANITIZER_RUNTIME_LIBRARY", strings.TrimSuffix(config.AddressSanitizerRuntimeLibrary(toolchain), ".so"))
215 ctx.Strict(secondPrefix+"UBSAN_RUNTIME_LIBRARY", strings.TrimSuffix(config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain), ".so"))
Pirama Arumuga Nainar16b626b2017-03-27 11:00:38 -0700216 ctx.Strict(secondPrefix+"TSAN_RUNTIME_LIBRARY", strings.TrimSuffix(config.ThreadSanitizerRuntimeLibrary(toolchain), ".so"))
Dan Willemsen558e5172016-05-19 16:58:46 -0700217 }
218
219 // This is used by external/gentoo/...
Colin Crossa1ad8d12016-06-01 17:09:44 -0700220 ctx.Strict("CLANG_CONFIG_"+target.Arch.ArchType.Name+"_"+typePrefix+"TRIPLE",
Dan Willemsen558e5172016-05-19 16:58:46 -0700221 toolchain.ClangTriple())
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800222 }
223
224 ctx.Strict(makePrefix+"CC", gccCmd(toolchain, "gcc"))
225 ctx.Strict(makePrefix+"CXX", gccCmd(toolchain, "g++"))
226
Colin Crossa1ad8d12016-06-01 17:09:44 -0700227 if target.Os == android.Darwin {
Colin Crossb98c8b02016-07-29 13:44:28 -0700228 ctx.Strict(makePrefix+"AR", "${config.MacArPath}")
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800229 } else {
230 ctx.Strict(makePrefix+"AR", gccCmd(toolchain, "ar"))
231 ctx.Strict(makePrefix+"READELF", gccCmd(toolchain, "readelf"))
232 ctx.Strict(makePrefix+"NM", gccCmd(toolchain, "nm"))
233 }
Dan Willemsen73d21f22016-05-16 14:22:56 -0700234
Colin Crossa1ad8d12016-06-01 17:09:44 -0700235 if target.Os == android.Windows {
Dan Willemsen73d21f22016-05-16 14:22:56 -0700236 ctx.Strict(makePrefix+"OBJDUMP", gccCmd(toolchain, "objdump"))
237 }
238
Colin Crossa1ad8d12016-06-01 17:09:44 -0700239 if target.Os.Class == android.Device {
Dan Willemsen73d21f22016-05-16 14:22:56 -0700240 ctx.Strict(makePrefix+"OBJCOPY", gccCmd(toolchain, "objcopy"))
241 ctx.Strict(makePrefix+"LD", gccCmd(toolchain, "ld"))
242 ctx.Strict(makePrefix+"STRIP", gccCmd(toolchain, "strip"))
Dan Willemsen558e5172016-05-19 16:58:46 -0700243 ctx.Strict(makePrefix+"GCC_VERSION", toolchain.GccVersion())
244 ctx.Strict(makePrefix+"NDK_GCC_VERSION", toolchain.GccVersion())
Dan Albert914449f2016-06-17 16:45:24 -0700245 ctx.Strict(makePrefix+"NDK_TRIPLE", toolchain.ClangTriple())
Dan Willemsen73d21f22016-05-16 14:22:56 -0700246 }
247
Dan Willemsen0bd58872016-05-26 15:23:07 -0700248 ctx.Strict(makePrefix+"TOOLCHAIN_ROOT", toolchain.GccRoot())
Dan Willemsen73d21f22016-05-16 14:22:56 -0700249 ctx.Strict(makePrefix+"TOOLS_PREFIX", gccCmd(toolchain, ""))
Dan Willemsen558e5172016-05-19 16:58:46 -0700250 ctx.Strict(makePrefix+"SHLIB_SUFFIX", toolchain.ShlibSuffix())
251 ctx.Strict(makePrefix+"EXECUTABLE_SUFFIX", toolchain.ExecutableSuffix())
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800252}
Colin Cross10c78c62016-07-20 12:14:19 -0700253
254func splitSystemIncludes(ctx android.MakeVarsContext, val string) (includes, systemIncludes []string) {
255 flags, err := ctx.Eval(val)
256 if err != nil {
257 panic(err)
258 }
259
260 extract := func(flags string, dirs []string, prefix string) (string, []string, bool) {
261 if strings.HasPrefix(flags, prefix) {
262 flags = strings.TrimPrefix(flags, prefix)
263 flags = strings.TrimLeft(flags, " ")
264 s := strings.SplitN(flags, " ", 2)
265 dirs = append(dirs, s[0])
266 if len(s) > 1 {
267 return strings.TrimLeft(s[1], " "), dirs, true
268 }
269 return "", dirs, true
270 } else {
271 return flags, dirs, false
272 }
273 }
274
275 flags = strings.TrimLeft(flags, " ")
276 for flags != "" {
277 found := false
278 flags, includes, found = extract(flags, includes, "-I")
279 if !found {
280 flags, systemIncludes, found = extract(flags, systemIncludes, "-isystem ")
281 }
282 if !found {
283 panic(fmt.Errorf("Unexpected flag in %q", flags))
284 }
285 }
286
287 return includes, systemIncludes
288}
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700289
290func joinLocalTidyChecks(checks []config.PathBasedTidyCheck) string {
291 rets := make([]string, len(checks))
292 for i, check := range config.DefaultLocalTidyChecks {
293 rets[i] = check.PathPrefix + ":" + check.Checks
294 }
295 return strings.Join(rets, " ")
296}