blob: 56698f2f464c9b30a30518db9e20a052042cdaa2 [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"
19 "path/filepath"
20 "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) {
Colin Crossb98c8b02016-07-29 13:44:28 -070031 ctx.Strict("LLVM_PREBUILTS_VERSION", "${config.ClangVersion}")
32 ctx.Strict("LLVM_PREBUILTS_BASE", "${config.ClangBase}")
33 ctx.Strict("LLVM_PREBUILTS_PATH", "${config.ClangBin}")
34 ctx.Strict("CLANG", "${config.ClangBin}/clang")
35 ctx.Strict("CLANG_CXX", "${config.ClangBin}/clang++")
36 ctx.Strict("LLVM_AS", "${config.ClangBin}/llvm-as")
37 ctx.Strict("LLVM_LINK", "${config.ClangBin}/llvm-link")
38 ctx.StrictSorted("CLANG_CONFIG_UNKNOWN_CFLAGS", strings.Join(config.ClangUnknownCflags, " "))
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080039
Colin Crossb98c8b02016-07-29 13:44:28 -070040 ctx.Strict("GLOBAL_CFLAGS_NO_OVERRIDE", "${config.NoOverrideGlobalCflags}")
41 ctx.Strict("GLOBAL_CLANG_CFLAGS_NO_OVERRIDE", "${config.ClangExtraNoOverrideCflags}")
Dan Willemsen5efc7062016-05-27 15:23:38 -070042 ctx.Strict("GLOBAL_CPPFLAGS_NO_OVERRIDE", "")
43 ctx.Strict("GLOBAL_CLANG_CPPFLAGS_NO_OVERRIDE", "")
Dan Willemsen97704ed2016-07-07 21:40:39 -070044 ctx.Strict("NDK_PREBUILT_SHARED_LIBRARIES", strings.Join(ndkPrebuiltSharedLibs, " "))
Dan Willemsen5efc7062016-05-27 15:23:38 -070045
Colin Crossb98c8b02016-07-29 13:44:28 -070046 includeFlags, err := ctx.Eval("${config.CommonGlobalIncludes}")
Colin Cross10c78c62016-07-20 12:14:19 -070047 if err != nil {
48 panic(err)
49 }
50 includes, systemIncludes := splitSystemIncludes(ctx, includeFlags)
51 ctx.StrictRaw("SRC_HEADERS", strings.Join(includes, " "))
52 ctx.StrictRaw("SRC_SYSTEM_HEADERS", strings.Join(systemIncludes, " "))
53
Dan Albert914449f2016-06-17 16:45:24 -070054 ctx.Strict("NDK_MIGRATED_LIBS", strings.Join(ndkMigratedLibs, " "))
55
Colin Crossa1ad8d12016-06-01 17:09:44 -070056 hostTargets := ctx.Config().Targets[android.Host]
57 makeVarsToolchain(ctx, "", hostTargets[0])
58 if len(hostTargets) > 1 {
59 makeVarsToolchain(ctx, "2ND_", hostTargets[1])
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080060 }
61
Colin Crossa1ad8d12016-06-01 17:09:44 -070062 crossTargets := ctx.Config().Targets[android.HostCross]
63 if len(crossTargets) > 0 {
64 makeVarsToolchain(ctx, "", crossTargets[0])
65 if len(crossTargets) > 1 {
66 makeVarsToolchain(ctx, "2ND_", crossTargets[1])
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080067 }
68 }
69
Colin Crossa1ad8d12016-06-01 17:09:44 -070070 deviceTargets := ctx.Config().Targets[android.Device]
71 makeVarsToolchain(ctx, "", deviceTargets[0])
72 if len(deviceTargets) > 1 {
73 makeVarsToolchain(ctx, "2ND_", deviceTargets[1])
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080074 }
75}
76
Colin Cross635c3b02016-05-18 15:37:25 -070077func makeVarsToolchain(ctx android.MakeVarsContext, secondPrefix string,
Colin Crossa1ad8d12016-06-01 17:09:44 -070078 target android.Target) {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080079 var typePrefix string
Colin Crossa1ad8d12016-06-01 17:09:44 -070080 switch target.Os.Class {
81 case android.Host:
82 typePrefix = "HOST_"
83 case android.HostCross:
84 typePrefix = "HOST_CROSS_"
85 case android.Device:
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080086 typePrefix = "TARGET_"
87 }
88 makePrefix := secondPrefix + typePrefix
89
Colin Crossb98c8b02016-07-29 13:44:28 -070090 toolchain := config.FindToolchain(target.Os, target.Arch)
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080091
Dan Willemsen36cff8b2016-05-17 16:35:02 -070092 var productExtraCflags string
93 var productExtraLdflags string
Colin Crossa1ad8d12016-06-01 17:09:44 -070094
Colin Crossb98c8b02016-07-29 13:44:28 -070095 hod := "Host"
Colin Crossa1ad8d12016-06-01 17:09:44 -070096 if target.Os.Class == android.Device {
Colin Crossb98c8b02016-07-29 13:44:28 -070097 hod = "Device"
Colin Crossa1ad8d12016-06-01 17:09:44 -070098 }
99
100 if target.Os.Class == android.Device && Bool(ctx.Config().ProductVariables.Brillo) {
Dan Willemsen36cff8b2016-05-17 16:35:02 -0700101 productExtraCflags += "-D__BRILLO__"
102 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700103 if target.Os.Class == android.Host && Bool(ctx.Config().ProductVariables.HostStaticBinaries) {
Dan Willemsen36cff8b2016-05-17 16:35:02 -0700104 productExtraLdflags += "-static"
Dan Willemsen06f45332016-05-16 19:32:33 -0700105 }
106
Dan Willemsen9598e102016-05-19 16:59:04 -0700107 ctx.Strict(makePrefix+"GLOBAL_CFLAGS", strings.Join([]string{
Dan Willemsend26a7132016-05-18 23:00:57 -0700108 toolchain.Cflags(),
Colin Crossb98c8b02016-07-29 13:44:28 -0700109 "${config.CommonGlobalCflags}",
110 fmt.Sprintf("${config.%sGlobalCflags}", hod),
Dan Willemsend26a7132016-05-18 23:00:57 -0700111 toolchain.ToolchainCflags(),
Dan Willemsen36cff8b2016-05-17 16:35:02 -0700112 productExtraCflags,
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800113 }, " "))
Dan Willemsen9598e102016-05-19 16:59:04 -0700114 ctx.Strict(makePrefix+"GLOBAL_CONLYFLAGS", "")
115 ctx.Strict(makePrefix+"GLOBAL_CPPFLAGS", strings.Join([]string{
Colin Crossb98c8b02016-07-29 13:44:28 -0700116 "${config.CommonGlobalCppflags}",
Dan Willemsen216a4592016-05-16 23:56:53 -0700117 toolchain.Cppflags(),
118 }, " "))
Dan Willemsen9598e102016-05-19 16:59:04 -0700119 ctx.Strict(makePrefix+"GLOBAL_LDFLAGS", strings.Join([]string{
Dan Willemsend26a7132016-05-18 23:00:57 -0700120 toolchain.Ldflags(),
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800121 toolchain.ToolchainLdflags(),
Dan Willemsen36cff8b2016-05-17 16:35:02 -0700122 productExtraLdflags,
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800123 }, " "))
Dan Willemsen558e5172016-05-19 16:58:46 -0700124
125 includeFlags, err := ctx.Eval(toolchain.IncludeFlags())
Colin Crossa1ad8d12016-06-01 17:09:44 -0700126 if err != nil {
127 panic(err)
128 }
Colin Cross10c78c62016-07-20 12:14:19 -0700129 includes, systemIncludes := splitSystemIncludes(ctx, includeFlags)
130 ctx.StrictRaw(makePrefix+"C_INCLUDES", strings.Join(includes, " "))
131 ctx.StrictRaw(makePrefix+"C_SYSTEM_INCLUDES", strings.Join(systemIncludes, " "))
Dan Willemsen558e5172016-05-19 16:58:46 -0700132
Colin Crossa1ad8d12016-06-01 17:09:44 -0700133 if target.Arch.ArchType == android.Arm {
Dan Willemsen558e5172016-05-19 16:58:46 -0700134 flags, err := toolchain.InstructionSetFlags("arm")
Colin Crossa1ad8d12016-06-01 17:09:44 -0700135 if err != nil {
136 panic(err)
137 }
Dan Willemsen558e5172016-05-19 16:58:46 -0700138 ctx.Strict(makePrefix+"arm_CFLAGS", flags)
139
140 flags, err = toolchain.InstructionSetFlags("thumb")
Colin Crossa1ad8d12016-06-01 17:09:44 -0700141 if err != nil {
142 panic(err)
143 }
Dan Willemsen558e5172016-05-19 16:58:46 -0700144 ctx.Strict(makePrefix+"thumb_CFLAGS", flags)
145 }
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800146
147 if toolchain.ClangSupported() {
148 clangPrefix := secondPrefix + "CLANG_" + typePrefix
Dan Willemsen3772da12016-05-16 18:01:46 -0700149 clangExtras := "-target " + toolchain.ClangTriple()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700150 if target.Os != android.Darwin {
Dan Willemsen3772da12016-05-16 18:01:46 -0700151 clangExtras += " -B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
152 }
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800153
Dan Willemsen9598e102016-05-19 16:59:04 -0700154 ctx.Strict(clangPrefix+"GLOBAL_CFLAGS", strings.Join([]string{
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800155 toolchain.ClangCflags(),
Colin Crossb98c8b02016-07-29 13:44:28 -0700156 "${config.CommonClangGlobalCflags}",
157 fmt.Sprintf("${config.%sClangGlobalCflags}", hod),
Dan Willemsend26a7132016-05-18 23:00:57 -0700158 toolchain.ToolchainClangCflags(),
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800159 clangExtras,
Dan Willemsend26a7132016-05-18 23:00:57 -0700160 productExtraCflags,
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800161 }, " "))
Colin Crossb98c8b02016-07-29 13:44:28 -0700162 ctx.Strict(clangPrefix+"GLOBAL_CONLYFLAGS", "${config.ClangExtraConlyflags}")
Dan Willemsen9598e102016-05-19 16:59:04 -0700163 ctx.Strict(clangPrefix+"GLOBAL_CPPFLAGS", strings.Join([]string{
Colin Crossb98c8b02016-07-29 13:44:28 -0700164 "${config.CommonClangGlobalCppflags}",
Dan Willemsen216a4592016-05-16 23:56:53 -0700165 toolchain.ClangCppflags(),
166 }, " "))
Dan Willemsen9598e102016-05-19 16:59:04 -0700167 ctx.Strict(clangPrefix+"GLOBAL_LDFLAGS", strings.Join([]string{
Dan Willemsend26a7132016-05-18 23:00:57 -0700168 toolchain.ClangLdflags(),
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800169 toolchain.ToolchainClangLdflags(),
Dan Willemsen36cff8b2016-05-17 16:35:02 -0700170 productExtraLdflags,
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800171 clangExtras,
172 }, " "))
Dan Willemsen558e5172016-05-19 16:58:46 -0700173
Colin Crossa1ad8d12016-06-01 17:09:44 -0700174 if target.Os.Class == android.Device {
175 ctx.Strict(secondPrefix+"ADDRESS_SANITIZER_RUNTIME_LIBRARY", strings.TrimSuffix(toolchain.AddressSanitizerRuntimeLibrary(), ".so"))
Dan Willemsen558e5172016-05-19 16:58:46 -0700176 }
177
178 // This is used by external/gentoo/...
Colin Crossa1ad8d12016-06-01 17:09:44 -0700179 ctx.Strict("CLANG_CONFIG_"+target.Arch.ArchType.Name+"_"+typePrefix+"TRIPLE",
Dan Willemsen558e5172016-05-19 16:58:46 -0700180 toolchain.ClangTriple())
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800181 }
182
183 ctx.Strict(makePrefix+"CC", gccCmd(toolchain, "gcc"))
184 ctx.Strict(makePrefix+"CXX", gccCmd(toolchain, "g++"))
185
Colin Crossa1ad8d12016-06-01 17:09:44 -0700186 if target.Os == android.Darwin {
Colin Crossb98c8b02016-07-29 13:44:28 -0700187 ctx.Strict(makePrefix+"AR", "${config.MacArPath}")
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800188 } else {
189 ctx.Strict(makePrefix+"AR", gccCmd(toolchain, "ar"))
190 ctx.Strict(makePrefix+"READELF", gccCmd(toolchain, "readelf"))
191 ctx.Strict(makePrefix+"NM", gccCmd(toolchain, "nm"))
192 }
Dan Willemsen73d21f22016-05-16 14:22:56 -0700193
Colin Crossa1ad8d12016-06-01 17:09:44 -0700194 if target.Os == android.Windows {
Dan Willemsen73d21f22016-05-16 14:22:56 -0700195 ctx.Strict(makePrefix+"OBJDUMP", gccCmd(toolchain, "objdump"))
196 }
197
Colin Crossa1ad8d12016-06-01 17:09:44 -0700198 if target.Os.Class == android.Device {
Dan Willemsen73d21f22016-05-16 14:22:56 -0700199 ctx.Strict(makePrefix+"OBJCOPY", gccCmd(toolchain, "objcopy"))
200 ctx.Strict(makePrefix+"LD", gccCmd(toolchain, "ld"))
201 ctx.Strict(makePrefix+"STRIP", gccCmd(toolchain, "strip"))
Dan Willemsen558e5172016-05-19 16:58:46 -0700202 ctx.Strict(makePrefix+"GCC_VERSION", toolchain.GccVersion())
203 ctx.Strict(makePrefix+"NDK_GCC_VERSION", toolchain.GccVersion())
Dan Albert914449f2016-06-17 16:45:24 -0700204 ctx.Strict(makePrefix+"NDK_TRIPLE", toolchain.ClangTriple())
Dan Willemsen73d21f22016-05-16 14:22:56 -0700205 }
206
Dan Willemsen0bd58872016-05-26 15:23:07 -0700207 ctx.Strict(makePrefix+"TOOLCHAIN_ROOT", toolchain.GccRoot())
Dan Willemsen73d21f22016-05-16 14:22:56 -0700208 ctx.Strict(makePrefix+"TOOLS_PREFIX", gccCmd(toolchain, ""))
Dan Willemsen558e5172016-05-19 16:58:46 -0700209 ctx.Strict(makePrefix+"SHLIB_SUFFIX", toolchain.ShlibSuffix())
210 ctx.Strict(makePrefix+"EXECUTABLE_SUFFIX", toolchain.ExecutableSuffix())
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800211}
Colin Cross10c78c62016-07-20 12:14:19 -0700212
213func splitSystemIncludes(ctx android.MakeVarsContext, val string) (includes, systemIncludes []string) {
214 flags, err := ctx.Eval(val)
215 if err != nil {
216 panic(err)
217 }
218
219 extract := func(flags string, dirs []string, prefix string) (string, []string, bool) {
220 if strings.HasPrefix(flags, prefix) {
221 flags = strings.TrimPrefix(flags, prefix)
222 flags = strings.TrimLeft(flags, " ")
223 s := strings.SplitN(flags, " ", 2)
224 dirs = append(dirs, s[0])
225 if len(s) > 1 {
226 return strings.TrimLeft(s[1], " "), dirs, true
227 }
228 return "", dirs, true
229 } else {
230 return flags, dirs, false
231 }
232 }
233
234 flags = strings.TrimLeft(flags, " ")
235 for flags != "" {
236 found := false
237 flags, includes, found = extract(flags, includes, "-I")
238 if !found {
239 flags, systemIncludes, found = extract(flags, systemIncludes, "-isystem ")
240 }
241 if !found {
242 panic(fmt.Errorf("Unexpected flag in %q", flags))
243 }
244 }
245
246 return includes, systemIncludes
247}