blob: 863e3dcc79bccc379948f5517f6cd91511d01d82 [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"
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080023)
24
25func init() {
Colin Cross635c3b02016-05-18 15:37:25 -070026 android.RegisterMakeVarsProvider(pctx, makeVarsProvider)
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080027}
28
Colin Cross635c3b02016-05-18 15:37:25 -070029func makeVarsProvider(ctx android.MakeVarsContext) {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080030 ctx.Strict("LLVM_PREBUILTS_VERSION", "${clangVersion}")
31 ctx.Strict("LLVM_PREBUILTS_BASE", "${clangBase}")
Dan Willemsen73d21f22016-05-16 14:22:56 -070032 ctx.Strict("LLVM_PREBUILTS_PATH", "${clangBin}")
33 ctx.Strict("CLANG", "${clangBin}/clang")
34 ctx.Strict("CLANG_CXX", "${clangBin}/clang++")
35 ctx.Strict("LLVM_AS", "${clangBin}/llvm-as")
36 ctx.Strict("LLVM_LINK", "${clangBin}/llvm-link")
Dan Willemsene8c52372016-05-19 16:57:11 -070037 ctx.StrictSorted("CLANG_CONFIG_UNKNOWN_CFLAGS", strings.Join(clangUnknownCflags, " "))
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080038
Dan Willemsen5efc7062016-05-27 15:23:38 -070039 ctx.Strict("GLOBAL_CFLAGS_NO_OVERRIDE", "${noOverrideGlobalCflags}")
40 ctx.Strict("GLOBAL_CLANG_CFLAGS_NO_OVERRIDE", "${clangExtraNoOverrideCflags}")
41 ctx.Strict("GLOBAL_CPPFLAGS_NO_OVERRIDE", "")
42 ctx.Strict("GLOBAL_CLANG_CPPFLAGS_NO_OVERRIDE", "")
43
Colin Crossb9db4802016-06-03 01:50:47 +000044 hostType := android.CurrentHostType()
45 arches := ctx.Config().HostArches[hostType]
46 makeVarsToolchain(ctx, "", android.Host, hostType, arches[0])
47 if len(arches) > 1 {
48 makeVarsToolchain(ctx, "2ND_", android.Host, hostType, arches[1])
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080049 }
50
Colin Crossb9db4802016-06-03 01:50:47 +000051 if winArches, ok := ctx.Config().HostArches[android.Windows]; ok {
52 makeVarsToolchain(ctx, "", android.Host, android.Windows, winArches[0])
53 if len(winArches) > 1 {
54 makeVarsToolchain(ctx, "2ND_", android.Host, android.Windows, winArches[1])
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080055 }
56 }
57
Colin Crossb9db4802016-06-03 01:50:47 +000058 arches = ctx.Config().DeviceArches
59 makeVarsToolchain(ctx, "", android.Device, android.NoHostType, arches[0])
60 if len(arches) > 1 {
61 makeVarsToolchain(ctx, "2ND_", android.Device, android.NoHostType, arches[1])
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080062 }
63}
64
Colin Cross635c3b02016-05-18 15:37:25 -070065func makeVarsToolchain(ctx android.MakeVarsContext, secondPrefix string,
Colin Crossb9db4802016-06-03 01:50:47 +000066 hod android.HostOrDevice, ht android.HostType, arch android.Arch) {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080067 var typePrefix string
Colin Crossb9db4802016-06-03 01:50:47 +000068 if hod.Host() {
69 if ht == android.Windows {
70 typePrefix = "HOST_CROSS_"
71 } else {
72 typePrefix = "HOST_"
73 }
74 } else {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080075 typePrefix = "TARGET_"
76 }
77 makePrefix := secondPrefix + typePrefix
78
Colin Crossb9db4802016-06-03 01:50:47 +000079 toolchain := toolchainFactories[hod][ht][arch.ArchType](arch)
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080080
Dan Willemsen36cff8b2016-05-17 16:35:02 -070081 var productExtraCflags string
82 var productExtraLdflags string
Colin Crossb9db4802016-06-03 01:50:47 +000083 if hod.Device() && Bool(ctx.Config().ProductVariables.Brillo) {
Dan Willemsen36cff8b2016-05-17 16:35:02 -070084 productExtraCflags += "-D__BRILLO__"
85 }
Colin Crossb9db4802016-06-03 01:50:47 +000086 if hod.Host() && Bool(ctx.Config().ProductVariables.HostStaticBinaries) {
Dan Willemsen36cff8b2016-05-17 16:35:02 -070087 productExtraLdflags += "-static"
Dan Willemsen06f45332016-05-16 19:32:33 -070088 }
89
Dan Willemsen9598e102016-05-19 16:59:04 -070090 ctx.Strict(makePrefix+"GLOBAL_CFLAGS", strings.Join([]string{
Dan Willemsend26a7132016-05-18 23:00:57 -070091 toolchain.Cflags(),
Dan Willemsen36cff8b2016-05-17 16:35:02 -070092 "${commonGlobalCflags}",
93 fmt.Sprintf("${%sGlobalCflags}", hod),
Dan Willemsend26a7132016-05-18 23:00:57 -070094 toolchain.ToolchainCflags(),
Dan Willemsen36cff8b2016-05-17 16:35:02 -070095 productExtraCflags,
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080096 }, " "))
Dan Willemsen9598e102016-05-19 16:59:04 -070097 ctx.Strict(makePrefix+"GLOBAL_CONLYFLAGS", "")
98 ctx.Strict(makePrefix+"GLOBAL_CPPFLAGS", strings.Join([]string{
Dan Willemsen216a4592016-05-16 23:56:53 -070099 "${commonGlobalCppflags}",
100 toolchain.Cppflags(),
101 }, " "))
Dan Willemsen9598e102016-05-19 16:59:04 -0700102 ctx.Strict(makePrefix+"GLOBAL_LDFLAGS", strings.Join([]string{
Dan Willemsend26a7132016-05-18 23:00:57 -0700103 toolchain.Ldflags(),
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800104 toolchain.ToolchainLdflags(),
Dan Willemsen36cff8b2016-05-17 16:35:02 -0700105 productExtraLdflags,
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800106 }, " "))
Dan Willemsen558e5172016-05-19 16:58:46 -0700107 ctx.Strict(makePrefix+"SYSTEMCPP_CPPFLAGS", toolchain.SystemCppCppflags())
108 ctx.Strict(makePrefix+"SYSTEMCPP_LDFLAGS", toolchain.SystemCppLdflags())
109
110 includeFlags, err := ctx.Eval(toolchain.IncludeFlags())
Colin Crossb9db4802016-06-03 01:50:47 +0000111 if err != nil { panic(err) }
Dan Willemsen558e5172016-05-19 16:58:46 -0700112 ctx.StrictRaw(makePrefix+"C_INCLUDES", strings.Replace(includeFlags, "-isystem ", "", -1))
113
Colin Crossb9db4802016-06-03 01:50:47 +0000114 if arch.ArchType == android.Arm {
Dan Willemsen558e5172016-05-19 16:58:46 -0700115 flags, err := toolchain.InstructionSetFlags("arm")
Colin Crossb9db4802016-06-03 01:50:47 +0000116 if err != nil { panic(err) }
Dan Willemsen558e5172016-05-19 16:58:46 -0700117 ctx.Strict(makePrefix+"arm_CFLAGS", flags)
118
119 flags, err = toolchain.InstructionSetFlags("thumb")
Colin Crossb9db4802016-06-03 01:50:47 +0000120 if err != nil { panic(err) }
Dan Willemsen558e5172016-05-19 16:58:46 -0700121 ctx.Strict(makePrefix+"thumb_CFLAGS", flags)
122 }
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800123
124 if toolchain.ClangSupported() {
125 clangPrefix := secondPrefix + "CLANG_" + typePrefix
Dan Willemsen3772da12016-05-16 18:01:46 -0700126 clangExtras := "-target " + toolchain.ClangTriple()
Colin Crossb9db4802016-06-03 01:50:47 +0000127 if ht != android.Darwin {
Dan Willemsen3772da12016-05-16 18:01:46 -0700128 clangExtras += " -B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
129 }
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800130
Dan Willemsen9598e102016-05-19 16:59:04 -0700131 ctx.Strict(clangPrefix+"GLOBAL_CFLAGS", strings.Join([]string{
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800132 toolchain.ClangCflags(),
Dan Willemsend26a7132016-05-18 23:00:57 -0700133 "${commonClangGlobalCflags}",
134 fmt.Sprintf("${%sClangGlobalCflags}", hod),
135 toolchain.ToolchainClangCflags(),
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800136 clangExtras,
Dan Willemsend26a7132016-05-18 23:00:57 -0700137 productExtraCflags,
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800138 }, " "))
Dan Willemsen9598e102016-05-19 16:59:04 -0700139 ctx.Strict(clangPrefix+"GLOBAL_CONLYFLAGS", "${clangExtraConlyflags}")
140 ctx.Strict(clangPrefix+"GLOBAL_CPPFLAGS", strings.Join([]string{
Dan Willemsen216a4592016-05-16 23:56:53 -0700141 "${commonClangGlobalCppflags}",
142 toolchain.ClangCppflags(),
143 }, " "))
Dan Willemsen9598e102016-05-19 16:59:04 -0700144 ctx.Strict(clangPrefix+"GLOBAL_LDFLAGS", strings.Join([]string{
Dan Willemsend26a7132016-05-18 23:00:57 -0700145 toolchain.ClangLdflags(),
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800146 toolchain.ToolchainClangLdflags(),
Dan Willemsen36cff8b2016-05-17 16:35:02 -0700147 productExtraLdflags,
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800148 clangExtras,
149 }, " "))
Dan Willemsen558e5172016-05-19 16:58:46 -0700150
Colin Crossb9db4802016-06-03 01:50:47 +0000151 if hod.Device() {
152 ctx.Strict(secondPrefix + "ADDRESS_SANITIZER_RUNTIME_LIBRARY", strings.TrimSuffix(toolchain.AddressSanitizerRuntimeLibrary(), ".so"))
Dan Willemsen558e5172016-05-19 16:58:46 -0700153 }
154
155 // This is used by external/gentoo/...
Colin Crossb9db4802016-06-03 01:50:47 +0000156 ctx.Strict("CLANG_CONFIG_" + arch.ArchType.Name + "_" + typePrefix + "TRIPLE",
Dan Willemsen558e5172016-05-19 16:58:46 -0700157 toolchain.ClangTriple())
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800158 }
159
160 ctx.Strict(makePrefix+"CC", gccCmd(toolchain, "gcc"))
161 ctx.Strict(makePrefix+"CXX", gccCmd(toolchain, "g++"))
162
Colin Crossb9db4802016-06-03 01:50:47 +0000163 if ht == android.Darwin {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800164 ctx.Strict(makePrefix+"AR", "${macArPath}")
165 } else {
166 ctx.Strict(makePrefix+"AR", gccCmd(toolchain, "ar"))
167 ctx.Strict(makePrefix+"READELF", gccCmd(toolchain, "readelf"))
168 ctx.Strict(makePrefix+"NM", gccCmd(toolchain, "nm"))
169 }
Dan Willemsen73d21f22016-05-16 14:22:56 -0700170
Colin Crossb9db4802016-06-03 01:50:47 +0000171 if ht == android.Windows {
Dan Willemsen73d21f22016-05-16 14:22:56 -0700172 ctx.Strict(makePrefix+"OBJDUMP", gccCmd(toolchain, "objdump"))
173 }
174
Colin Crossb9db4802016-06-03 01:50:47 +0000175 if hod.Device() {
Dan Willemsen73d21f22016-05-16 14:22:56 -0700176 ctx.Strict(makePrefix+"OBJCOPY", gccCmd(toolchain, "objcopy"))
177 ctx.Strict(makePrefix+"LD", gccCmd(toolchain, "ld"))
178 ctx.Strict(makePrefix+"STRIP", gccCmd(toolchain, "strip"))
Dan Willemsen558e5172016-05-19 16:58:46 -0700179 ctx.Strict(makePrefix+"GCC_VERSION", toolchain.GccVersion())
180 ctx.Strict(makePrefix+"NDK_GCC_VERSION", toolchain.GccVersion())
Dan Willemsen73d21f22016-05-16 14:22:56 -0700181 }
182
Dan Willemsen0bd58872016-05-26 15:23:07 -0700183 ctx.Strict(makePrefix+"TOOLCHAIN_ROOT", toolchain.GccRoot())
Dan Willemsen73d21f22016-05-16 14:22:56 -0700184 ctx.Strict(makePrefix+"TOOLS_PREFIX", gccCmd(toolchain, ""))
Dan Willemsen558e5172016-05-19 16:58:46 -0700185 ctx.Strict(makePrefix+"SHLIB_SUFFIX", toolchain.ShlibSuffix())
186 ctx.Strict(makePrefix+"EXECUTABLE_SUFFIX", toolchain.ExecutableSuffix())
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800187}