blob: 22b90135d9e04423f85ac2e362813f52a251389e [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}")
45 ctx.Strict("RS_CLANG", "${config.RSLLVMPrebuiltsPath}/clang")
46 ctx.Strict("RS_LLVM_AS", "${config.RSLLVMPrebuiltsPath}/llvm-as")
47 ctx.Strict("RS_LLVM_LINK", "${config.RSLLVMPrebuiltsPath}/llvm-link")
48
Colin Crossb98c8b02016-07-29 13:44:28 -070049 ctx.Strict("GLOBAL_CFLAGS_NO_OVERRIDE", "${config.NoOverrideGlobalCflags}")
50 ctx.Strict("GLOBAL_CLANG_CFLAGS_NO_OVERRIDE", "${config.ClangExtraNoOverrideCflags}")
Dan Willemsen5efc7062016-05-27 15:23:38 -070051 ctx.Strict("GLOBAL_CPPFLAGS_NO_OVERRIDE", "")
52 ctx.Strict("GLOBAL_CLANG_CPPFLAGS_NO_OVERRIDE", "")
Dan Willemsen97704ed2016-07-07 21:40:39 -070053 ctx.Strict("NDK_PREBUILT_SHARED_LIBRARIES", strings.Join(ndkPrebuiltSharedLibs, " "))
Dan Willemsen5efc7062016-05-27 15:23:38 -070054
Dan Willemsend2ede872016-11-18 14:54:24 -080055 if ctx.Config().ProductVariables.DeviceVndkVersion != nil {
56 ctx.Strict("BOARD_VNDK_VERSION", *ctx.Config().ProductVariables.DeviceVndkVersion)
57 } else {
58 ctx.Strict("BOARD_VNDK_VERSION", "")
59 }
Dan Willemsend2ede872016-11-18 14:54:24 -080060
Dan Willemsencbceaab2016-10-13 16:44:07 -070061 ctx.Strict("ADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS", asanCflags)
62 ctx.Strict("ADDRESS_SANITIZER_CONFIG_EXTRA_LDFLAGS", asanLdflags)
63 ctx.Strict("ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES", asanLibs)
64
Vishwath Mohanf3918d32017-02-14 07:59:33 -080065 ctx.Strict("CFI_EXTRA_CFLAGS", cfiCflags)
66 ctx.Strict("CFI_EXTRA_LDFLAGS", cfiLdflags)
67
Colin Cross6f6a4282016-10-17 14:19:06 -070068 ctx.Strict("DEFAULT_C_STD_VERSION", config.CStdVersion)
69 ctx.Strict("DEFAULT_CPP_STD_VERSION", config.CppStdVersion)
70 ctx.Strict("DEFAULT_GCC_CPP_STD_VERSION", config.GccCppStdVersion)
Dan Albert043833c2017-02-03 16:13:38 -080071 ctx.Strict("EXPERIMENTAL_C_STD_VERSION", config.ExperimentalCStdVersion)
72 ctx.Strict("EXPERIMENTAL_CPP_STD_VERSION", config.ExperimentalCppStdVersion)
Colin Cross6f6a4282016-10-17 14:19:06 -070073
Dan Willemsena03cf6d2016-09-26 15:45:04 -070074 ctx.Strict("DEFAULT_GLOBAL_TIDY_CHECKS", "${config.TidyDefaultGlobalChecks}")
75 ctx.Strict("DEFAULT_LOCAL_TIDY_CHECKS", joinLocalTidyChecks(config.DefaultLocalTidyChecks))
76 ctx.Strict("DEFAULT_TIDY_HEADER_DIRS", "${config.TidyDefaultHeaderDirs}")
77
Dan Willemsene1240db2016-11-03 14:28:51 -070078 ctx.Strict("AIDL_CPP", "${aidlCmd}")
79
Colin Cross307d1452017-04-30 17:45:07 -070080 includeFlags, err := ctx.Eval("${config.CommonGlobalIncludes}")
Colin Cross10c78c62016-07-20 12:14:19 -070081 if err != nil {
82 panic(err)
83 }
84 includes, systemIncludes := splitSystemIncludes(ctx, includeFlags)
85 ctx.StrictRaw("SRC_HEADERS", strings.Join(includes, " "))
86 ctx.StrictRaw("SRC_SYSTEM_HEADERS", strings.Join(systemIncludes, " "))
87
Dan Willemsen65905f82016-09-14 20:29:05 -070088 sort.Strings(ndkMigratedLibs)
Dan Albert914449f2016-06-17 16:45:24 -070089 ctx.Strict("NDK_MIGRATED_LIBS", strings.Join(ndkMigratedLibs, " "))
90
Colin Crossa1ad8d12016-06-01 17:09:44 -070091 hostTargets := ctx.Config().Targets[android.Host]
92 makeVarsToolchain(ctx, "", hostTargets[0])
93 if len(hostTargets) > 1 {
94 makeVarsToolchain(ctx, "2ND_", hostTargets[1])
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080095 }
96
Colin Crossa1ad8d12016-06-01 17:09:44 -070097 crossTargets := ctx.Config().Targets[android.HostCross]
98 if len(crossTargets) > 0 {
99 makeVarsToolchain(ctx, "", crossTargets[0])
100 if len(crossTargets) > 1 {
101 makeVarsToolchain(ctx, "2ND_", crossTargets[1])
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800102 }
103 }
104
Colin Crossa1ad8d12016-06-01 17:09:44 -0700105 deviceTargets := ctx.Config().Targets[android.Device]
106 makeVarsToolchain(ctx, "", deviceTargets[0])
107 if len(deviceTargets) > 1 {
108 makeVarsToolchain(ctx, "2ND_", deviceTargets[1])
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800109 }
110}
111
Colin Cross635c3b02016-05-18 15:37:25 -0700112func makeVarsToolchain(ctx android.MakeVarsContext, secondPrefix string,
Colin Crossa1ad8d12016-06-01 17:09:44 -0700113 target android.Target) {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800114 var typePrefix string
Colin Crossa1ad8d12016-06-01 17:09:44 -0700115 switch target.Os.Class {
116 case android.Host:
117 typePrefix = "HOST_"
118 case android.HostCross:
119 typePrefix = "HOST_CROSS_"
120 case android.Device:
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800121 typePrefix = "TARGET_"
122 }
123 makePrefix := secondPrefix + typePrefix
124
Colin Crossb98c8b02016-07-29 13:44:28 -0700125 toolchain := config.FindToolchain(target.Os, target.Arch)
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800126
Dan Willemsen36cff8b2016-05-17 16:35:02 -0700127 var productExtraCflags string
128 var productExtraLdflags string
Colin Crossa1ad8d12016-06-01 17:09:44 -0700129
Colin Crossb98c8b02016-07-29 13:44:28 -0700130 hod := "Host"
Colin Crossa1ad8d12016-06-01 17:09:44 -0700131 if target.Os.Class == android.Device {
Colin Crossb98c8b02016-07-29 13:44:28 -0700132 hod = "Device"
Colin Crossa1ad8d12016-06-01 17:09:44 -0700133 }
134
135 if target.Os.Class == android.Device && Bool(ctx.Config().ProductVariables.Brillo) {
Dan Willemsen36cff8b2016-05-17 16:35:02 -0700136 productExtraCflags += "-D__BRILLO__"
137 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700138 if target.Os.Class == android.Host && Bool(ctx.Config().ProductVariables.HostStaticBinaries) {
Dan Willemsen36cff8b2016-05-17 16:35:02 -0700139 productExtraLdflags += "-static"
Dan Willemsen06f45332016-05-16 19:32:33 -0700140 }
141
Dan Willemsen9598e102016-05-19 16:59:04 -0700142 ctx.Strict(makePrefix+"GLOBAL_CFLAGS", strings.Join([]string{
Dan Willemsend26a7132016-05-18 23:00:57 -0700143 toolchain.Cflags(),
Colin Crossb98c8b02016-07-29 13:44:28 -0700144 "${config.CommonGlobalCflags}",
145 fmt.Sprintf("${config.%sGlobalCflags}", hod),
Dan Willemsend26a7132016-05-18 23:00:57 -0700146 toolchain.ToolchainCflags(),
Dan Willemsen36cff8b2016-05-17 16:35:02 -0700147 productExtraCflags,
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800148 }, " "))
Elliott Hughes5a0401a2016-10-07 13:12:58 -0700149 ctx.Strict(makePrefix+"GLOBAL_CONLYFLAGS", strings.Join([]string{
150 "${config.CommonGlobalConlyflags}",
151 }, " "))
Dan Willemsen9598e102016-05-19 16:59:04 -0700152 ctx.Strict(makePrefix+"GLOBAL_CPPFLAGS", strings.Join([]string{
Colin Crossb98c8b02016-07-29 13:44:28 -0700153 "${config.CommonGlobalCppflags}",
Dan Willemsen216a4592016-05-16 23:56:53 -0700154 toolchain.Cppflags(),
155 }, " "))
Dan Willemsen9598e102016-05-19 16:59:04 -0700156 ctx.Strict(makePrefix+"GLOBAL_LDFLAGS", strings.Join([]string{
Dan Willemsend26a7132016-05-18 23:00:57 -0700157 toolchain.Ldflags(),
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800158 toolchain.ToolchainLdflags(),
Dan Willemsen36cff8b2016-05-17 16:35:02 -0700159 productExtraLdflags,
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800160 }, " "))
Dan Willemsen558e5172016-05-19 16:58:46 -0700161
162 includeFlags, err := ctx.Eval(toolchain.IncludeFlags())
Colin Crossa1ad8d12016-06-01 17:09:44 -0700163 if err != nil {
164 panic(err)
165 }
Colin Cross10c78c62016-07-20 12:14:19 -0700166 includes, systemIncludes := splitSystemIncludes(ctx, includeFlags)
167 ctx.StrictRaw(makePrefix+"C_INCLUDES", strings.Join(includes, " "))
168 ctx.StrictRaw(makePrefix+"C_SYSTEM_INCLUDES", strings.Join(systemIncludes, " "))
Dan Willemsen558e5172016-05-19 16:58:46 -0700169
Colin Crossa1ad8d12016-06-01 17:09:44 -0700170 if target.Arch.ArchType == android.Arm {
Dan Willemsen558e5172016-05-19 16:58:46 -0700171 flags, err := toolchain.InstructionSetFlags("arm")
Colin Crossa1ad8d12016-06-01 17:09:44 -0700172 if err != nil {
173 panic(err)
174 }
Dan Willemsen558e5172016-05-19 16:58:46 -0700175 ctx.Strict(makePrefix+"arm_CFLAGS", flags)
176
177 flags, err = toolchain.InstructionSetFlags("thumb")
Colin Crossa1ad8d12016-06-01 17:09:44 -0700178 if err != nil {
179 panic(err)
180 }
Dan Willemsen558e5172016-05-19 16:58:46 -0700181 ctx.Strict(makePrefix+"thumb_CFLAGS", flags)
182 }
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800183
184 if toolchain.ClangSupported() {
185 clangPrefix := secondPrefix + "CLANG_" + typePrefix
Dan Willemsen3772da12016-05-16 18:01:46 -0700186 clangExtras := "-target " + toolchain.ClangTriple()
Dan Willemsen300151b2017-03-13 12:40:30 -0700187 clangExtras += " -B" + config.ToolPath(toolchain)
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800188
Dan Willemsen9598e102016-05-19 16:59:04 -0700189 ctx.Strict(clangPrefix+"GLOBAL_CFLAGS", strings.Join([]string{
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800190 toolchain.ClangCflags(),
Colin Crossb98c8b02016-07-29 13:44:28 -0700191 "${config.CommonClangGlobalCflags}",
192 fmt.Sprintf("${config.%sClangGlobalCflags}", hod),
Dan Willemsend26a7132016-05-18 23:00:57 -0700193 toolchain.ToolchainClangCflags(),
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800194 clangExtras,
Dan Willemsend26a7132016-05-18 23:00:57 -0700195 productExtraCflags,
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800196 }, " "))
Dan Willemsen9598e102016-05-19 16:59:04 -0700197 ctx.Strict(clangPrefix+"GLOBAL_CPPFLAGS", strings.Join([]string{
Colin Crossb98c8b02016-07-29 13:44:28 -0700198 "${config.CommonClangGlobalCppflags}",
Dan Willemsen216a4592016-05-16 23:56:53 -0700199 toolchain.ClangCppflags(),
200 }, " "))
Dan Willemsen9598e102016-05-19 16:59:04 -0700201 ctx.Strict(clangPrefix+"GLOBAL_LDFLAGS", strings.Join([]string{
Dan Willemsend26a7132016-05-18 23:00:57 -0700202 toolchain.ClangLdflags(),
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800203 toolchain.ToolchainClangLdflags(),
Dan Willemsen36cff8b2016-05-17 16:35:02 -0700204 productExtraLdflags,
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800205 clangExtras,
206 }, " "))
Dan Willemsen558e5172016-05-19 16:58:46 -0700207
Colin Crossa1ad8d12016-06-01 17:09:44 -0700208 if target.Os.Class == android.Device {
Evgenii Stepanovaf36db12016-08-15 14:18:24 -0700209 ctx.Strict(secondPrefix+"ADDRESS_SANITIZER_RUNTIME_LIBRARY", strings.TrimSuffix(config.AddressSanitizerRuntimeLibrary(toolchain), ".so"))
210 ctx.Strict(secondPrefix+"UBSAN_RUNTIME_LIBRARY", strings.TrimSuffix(config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain), ".so"))
Pirama Arumuga Nainar16b626b2017-03-27 11:00:38 -0700211 ctx.Strict(secondPrefix+"TSAN_RUNTIME_LIBRARY", strings.TrimSuffix(config.ThreadSanitizerRuntimeLibrary(toolchain), ".so"))
Dan Willemsen558e5172016-05-19 16:58:46 -0700212 }
213
214 // This is used by external/gentoo/...
Colin Crossa1ad8d12016-06-01 17:09:44 -0700215 ctx.Strict("CLANG_CONFIG_"+target.Arch.ArchType.Name+"_"+typePrefix+"TRIPLE",
Dan Willemsen558e5172016-05-19 16:58:46 -0700216 toolchain.ClangTriple())
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800217 }
218
219 ctx.Strict(makePrefix+"CC", gccCmd(toolchain, "gcc"))
220 ctx.Strict(makePrefix+"CXX", gccCmd(toolchain, "g++"))
221
Colin Crossa1ad8d12016-06-01 17:09:44 -0700222 if target.Os == android.Darwin {
Colin Crossb98c8b02016-07-29 13:44:28 -0700223 ctx.Strict(makePrefix+"AR", "${config.MacArPath}")
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800224 } else {
225 ctx.Strict(makePrefix+"AR", gccCmd(toolchain, "ar"))
226 ctx.Strict(makePrefix+"READELF", gccCmd(toolchain, "readelf"))
227 ctx.Strict(makePrefix+"NM", gccCmd(toolchain, "nm"))
228 }
Dan Willemsen73d21f22016-05-16 14:22:56 -0700229
Colin Crossa1ad8d12016-06-01 17:09:44 -0700230 if target.Os == android.Windows {
Dan Willemsen73d21f22016-05-16 14:22:56 -0700231 ctx.Strict(makePrefix+"OBJDUMP", gccCmd(toolchain, "objdump"))
232 }
233
Colin Crossa1ad8d12016-06-01 17:09:44 -0700234 if target.Os.Class == android.Device {
Dan Willemsen73d21f22016-05-16 14:22:56 -0700235 ctx.Strict(makePrefix+"OBJCOPY", gccCmd(toolchain, "objcopy"))
236 ctx.Strict(makePrefix+"LD", gccCmd(toolchain, "ld"))
237 ctx.Strict(makePrefix+"STRIP", gccCmd(toolchain, "strip"))
Dan Willemsen558e5172016-05-19 16:58:46 -0700238 ctx.Strict(makePrefix+"GCC_VERSION", toolchain.GccVersion())
239 ctx.Strict(makePrefix+"NDK_GCC_VERSION", toolchain.GccVersion())
Dan Albert914449f2016-06-17 16:45:24 -0700240 ctx.Strict(makePrefix+"NDK_TRIPLE", toolchain.ClangTriple())
Dan Willemsen73d21f22016-05-16 14:22:56 -0700241 }
242
Dan Willemsen0bd58872016-05-26 15:23:07 -0700243 ctx.Strict(makePrefix+"TOOLCHAIN_ROOT", toolchain.GccRoot())
Dan Willemsen73d21f22016-05-16 14:22:56 -0700244 ctx.Strict(makePrefix+"TOOLS_PREFIX", gccCmd(toolchain, ""))
Dan Willemsen558e5172016-05-19 16:58:46 -0700245 ctx.Strict(makePrefix+"SHLIB_SUFFIX", toolchain.ShlibSuffix())
246 ctx.Strict(makePrefix+"EXECUTABLE_SUFFIX", toolchain.ExecutableSuffix())
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800247}
Colin Cross10c78c62016-07-20 12:14:19 -0700248
249func splitSystemIncludes(ctx android.MakeVarsContext, val string) (includes, systemIncludes []string) {
250 flags, err := ctx.Eval(val)
251 if err != nil {
252 panic(err)
253 }
254
255 extract := func(flags string, dirs []string, prefix string) (string, []string, bool) {
256 if strings.HasPrefix(flags, prefix) {
257 flags = strings.TrimPrefix(flags, prefix)
258 flags = strings.TrimLeft(flags, " ")
259 s := strings.SplitN(flags, " ", 2)
260 dirs = append(dirs, s[0])
261 if len(s) > 1 {
262 return strings.TrimLeft(s[1], " "), dirs, true
263 }
264 return "", dirs, true
265 } else {
266 return flags, dirs, false
267 }
268 }
269
270 flags = strings.TrimLeft(flags, " ")
271 for flags != "" {
272 found := false
273 flags, includes, found = extract(flags, includes, "-I")
274 if !found {
275 flags, systemIncludes, found = extract(flags, systemIncludes, "-isystem ")
276 }
277 if !found {
278 panic(fmt.Errorf("Unexpected flag in %q", flags))
279 }
280 }
281
282 return includes, systemIncludes
283}
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700284
285func joinLocalTidyChecks(checks []config.PathBasedTidyCheck) string {
286 rets := make([]string, len(checks))
287 for i, check := range config.DefaultLocalTidyChecks {
288 rets[i] = check.PathPrefix + ":" + check.Checks
289 }
290 return strings.Join(rets, " ")
291}