blob: 48698bf8230e741073fd74abbcc23a64be4344b4 [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
22 "android/soong/common"
23)
24
25func init() {
26 common.RegisterMakeVarsProvider(pctx, makeVarsProvider)
27}
28
29func makeVarsProvider(ctx common.MakeVarsContext) {
30 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 Willemsen4b7d5de2016-01-12 23:20:28 -080037
38 hostType := common.CurrentHostType()
39 arches := ctx.Config().HostArches[hostType]
40 makeVarsToolchain(ctx, "", common.Host, hostType, arches[0])
41 if len(arches) > 1 {
42 makeVarsToolchain(ctx, "2ND_", common.Host, hostType, arches[1])
43 }
44
45 if winArches, ok := ctx.Config().HostArches[common.Windows]; ok {
46 makeVarsToolchain(ctx, "", common.Host, common.Windows, winArches[0])
47 if len(winArches) > 1 {
48 makeVarsToolchain(ctx, "2ND_", common.Host, common.Windows, winArches[1])
49 }
50 }
51
52 arches = ctx.Config().DeviceArches
53 makeVarsToolchain(ctx, "", common.Device, common.NoHostType, arches[0])
54 if len(arches) > 1 {
55 makeVarsToolchain(ctx, "2ND_", common.Device, common.NoHostType, arches[1])
56 }
57}
58
59func makeVarsToolchain(ctx common.MakeVarsContext, secondPrefix string,
60 hod common.HostOrDevice, ht common.HostType, arch common.Arch) {
61 var typePrefix string
62 if hod.Host() {
63 if ht == common.Windows {
64 typePrefix = "HOST_CROSS_"
65 } else {
66 typePrefix = "HOST_"
67 }
68 } else {
69 typePrefix = "TARGET_"
70 }
71 makePrefix := secondPrefix + typePrefix
72
73 toolchain := toolchainFactories[hod][ht][arch.ArchType](arch)
74
75 globalCflags := fmt.Sprintf("${commonGlobalCflags} ${%sGlobalCflags}", hod)
76
Dan Willemsen06f45332016-05-16 19:32:33 -070077 if hod.Device() && Bool(ctx.Config().ProductVariables.Brillo) {
78 globalCflags += " -D__BRILLO__"
79 }
80
Dan Willemsenb436fde2016-05-16 18:04:45 -070081 ctx.StrictSorted(makePrefix+"GLOBAL_CFLAGS", strings.Join([]string{
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080082 toolchain.ToolchainCflags(),
83 globalCflags,
84 toolchain.Cflags(),
85 }, " "))
Dan Willemsen216a4592016-05-16 23:56:53 -070086 ctx.StrictSorted(makePrefix+"GLOBAL_CONLYFLAGS", "")
87 ctx.StrictSorted(makePrefix+"GLOBAL_CPPFLAGS", strings.Join([]string{
88 "${commonGlobalCppflags}",
89 toolchain.Cppflags(),
90 }, " "))
Dan Willemsenb436fde2016-05-16 18:04:45 -070091 ctx.StrictSorted(makePrefix+"GLOBAL_LDFLAGS", strings.Join([]string{
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080092 toolchain.ToolchainLdflags(),
93 toolchain.Ldflags(),
94 }, " "))
95
96 if toolchain.ClangSupported() {
97 clangPrefix := secondPrefix + "CLANG_" + typePrefix
Dan Willemsen3772da12016-05-16 18:01:46 -070098 clangExtras := "-target " + toolchain.ClangTriple()
99 if ht != common.Darwin {
100 clangExtras += " -B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
101 }
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800102
103 globalClangCflags := fmt.Sprintf("${commonClangGlobalCflags} ${clangExtraCflags} ${%sClangGlobalCflags}", hod)
104
Dan Willemsen06f45332016-05-16 19:32:33 -0700105 if hod.Device() && Bool(ctx.Config().ProductVariables.Brillo) {
106 globalClangCflags += " -D__BRILLO__"
107 }
108
Dan Willemsenb436fde2016-05-16 18:04:45 -0700109 ctx.StrictSorted(clangPrefix+"GLOBAL_CFLAGS", strings.Join([]string{
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800110 toolchain.ToolchainClangCflags(),
111 globalClangCflags,
112 toolchain.ClangCflags(),
113 clangExtras,
114 }, " "))
Dan Willemsen216a4592016-05-16 23:56:53 -0700115 ctx.StrictSorted(clangPrefix+"GLOBAL_CONLYFLAGS", "${clangExtraConlyflags}")
116 ctx.StrictSorted(clangPrefix+"GLOBAL_CPPFLAGS", strings.Join([]string{
117 "${commonClangGlobalCppflags}",
118 toolchain.ClangCppflags(),
119 }, " "))
Dan Willemsenb436fde2016-05-16 18:04:45 -0700120 ctx.StrictSorted(clangPrefix+"GLOBAL_LDFLAGS", strings.Join([]string{
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800121 toolchain.ToolchainClangLdflags(),
122 toolchain.ClangLdflags(),
123 clangExtras,
124 }, " "))
125 }
126
127 ctx.Strict(makePrefix+"CC", gccCmd(toolchain, "gcc"))
128 ctx.Strict(makePrefix+"CXX", gccCmd(toolchain, "g++"))
129
130 if ht == common.Darwin {
131 ctx.Strict(makePrefix+"AR", "${macArPath}")
132 } else {
133 ctx.Strict(makePrefix+"AR", gccCmd(toolchain, "ar"))
134 ctx.Strict(makePrefix+"READELF", gccCmd(toolchain, "readelf"))
135 ctx.Strict(makePrefix+"NM", gccCmd(toolchain, "nm"))
136 }
Dan Willemsen73d21f22016-05-16 14:22:56 -0700137
138 if ht == common.Windows {
139 ctx.Strict(makePrefix+"OBJDUMP", gccCmd(toolchain, "objdump"))
140 }
141
142 if hod.Device() {
143 ctx.Strict(makePrefix+"OBJCOPY", gccCmd(toolchain, "objcopy"))
144 ctx.Strict(makePrefix+"LD", gccCmd(toolchain, "ld"))
145 ctx.Strict(makePrefix+"STRIP", gccCmd(toolchain, "strip"))
146 }
147
148 ctx.Strict(makePrefix+"TOOLS_PREFIX", gccCmd(toolchain, ""))
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800149}