blob: 3e72a7a21f4fe254b1ea43cf0f96f82b6e047573 [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 Willemsen4b7d5de2016-01-12 23:20:28 -080037
Colin Cross635c3b02016-05-18 15:37:25 -070038 hostType := android.CurrentHostType()
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080039 arches := ctx.Config().HostArches[hostType]
Colin Cross635c3b02016-05-18 15:37:25 -070040 makeVarsToolchain(ctx, "", android.Host, hostType, arches[0])
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080041 if len(arches) > 1 {
Colin Cross635c3b02016-05-18 15:37:25 -070042 makeVarsToolchain(ctx, "2ND_", android.Host, hostType, arches[1])
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080043 }
44
Colin Cross635c3b02016-05-18 15:37:25 -070045 if winArches, ok := ctx.Config().HostArches[android.Windows]; ok {
46 makeVarsToolchain(ctx, "", android.Host, android.Windows, winArches[0])
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080047 if len(winArches) > 1 {
Colin Cross635c3b02016-05-18 15:37:25 -070048 makeVarsToolchain(ctx, "2ND_", android.Host, android.Windows, winArches[1])
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080049 }
50 }
51
52 arches = ctx.Config().DeviceArches
Colin Cross635c3b02016-05-18 15:37:25 -070053 makeVarsToolchain(ctx, "", android.Device, android.NoHostType, arches[0])
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080054 if len(arches) > 1 {
Colin Cross635c3b02016-05-18 15:37:25 -070055 makeVarsToolchain(ctx, "2ND_", android.Device, android.NoHostType, arches[1])
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080056 }
57}
58
Colin Cross635c3b02016-05-18 15:37:25 -070059func makeVarsToolchain(ctx android.MakeVarsContext, secondPrefix string,
60 hod android.HostOrDevice, ht android.HostType, arch android.Arch) {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080061 var typePrefix string
62 if hod.Host() {
Colin Cross635c3b02016-05-18 15:37:25 -070063 if ht == android.Windows {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080064 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
Dan Willemsen36cff8b2016-05-17 16:35:02 -070075 var productExtraCflags string
76 var productExtraLdflags string
Dan Willemsen06f45332016-05-16 19:32:33 -070077 if hod.Device() && Bool(ctx.Config().ProductVariables.Brillo) {
Dan Willemsen36cff8b2016-05-17 16:35:02 -070078 productExtraCflags += "-D__BRILLO__"
79 }
80 if hod.Host() && Bool(ctx.Config().ProductVariables.HostStaticBinaries) {
81 productExtraLdflags += "-static"
Dan Willemsen06f45332016-05-16 19:32:33 -070082 }
83
Dan Willemsenb436fde2016-05-16 18:04:45 -070084 ctx.StrictSorted(makePrefix+"GLOBAL_CFLAGS", strings.Join([]string{
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080085 toolchain.ToolchainCflags(),
Dan Willemsen36cff8b2016-05-17 16:35:02 -070086 "${commonGlobalCflags}",
87 fmt.Sprintf("${%sGlobalCflags}", hod),
88 productExtraCflags,
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080089 toolchain.Cflags(),
90 }, " "))
Dan Willemsen216a4592016-05-16 23:56:53 -070091 ctx.StrictSorted(makePrefix+"GLOBAL_CONLYFLAGS", "")
92 ctx.StrictSorted(makePrefix+"GLOBAL_CPPFLAGS", strings.Join([]string{
93 "${commonGlobalCppflags}",
94 toolchain.Cppflags(),
95 }, " "))
Dan Willemsenb436fde2016-05-16 18:04:45 -070096 ctx.StrictSorted(makePrefix+"GLOBAL_LDFLAGS", strings.Join([]string{
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080097 toolchain.ToolchainLdflags(),
Dan Willemsen36cff8b2016-05-17 16:35:02 -070098 productExtraLdflags,
Dan Willemsen4b7d5de2016-01-12 23:20:28 -080099 toolchain.Ldflags(),
100 }, " "))
101
102 if toolchain.ClangSupported() {
103 clangPrefix := secondPrefix + "CLANG_" + typePrefix
Dan Willemsen3772da12016-05-16 18:01:46 -0700104 clangExtras := "-target " + toolchain.ClangTriple()
Colin Cross635c3b02016-05-18 15:37:25 -0700105 if ht != android.Darwin {
Dan Willemsen3772da12016-05-16 18:01:46 -0700106 clangExtras += " -B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
107 }
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800108
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(),
Dan Willemsen36cff8b2016-05-17 16:35:02 -0700111 "${commonClangGlobalCflags}",
112 "${clangExtraCflags}",
113 fmt.Sprintf("${%sClangGlobalCflags}", hod),
114 productExtraCflags,
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800115 toolchain.ClangCflags(),
116 clangExtras,
117 }, " "))
Dan Willemsen216a4592016-05-16 23:56:53 -0700118 ctx.StrictSorted(clangPrefix+"GLOBAL_CONLYFLAGS", "${clangExtraConlyflags}")
119 ctx.StrictSorted(clangPrefix+"GLOBAL_CPPFLAGS", strings.Join([]string{
120 "${commonClangGlobalCppflags}",
121 toolchain.ClangCppflags(),
122 }, " "))
Dan Willemsenb436fde2016-05-16 18:04:45 -0700123 ctx.StrictSorted(clangPrefix+"GLOBAL_LDFLAGS", strings.Join([]string{
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800124 toolchain.ToolchainClangLdflags(),
Dan Willemsen36cff8b2016-05-17 16:35:02 -0700125 productExtraLdflags,
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800126 toolchain.ClangLdflags(),
127 clangExtras,
128 }, " "))
129 }
130
131 ctx.Strict(makePrefix+"CC", gccCmd(toolchain, "gcc"))
132 ctx.Strict(makePrefix+"CXX", gccCmd(toolchain, "g++"))
133
Colin Cross635c3b02016-05-18 15:37:25 -0700134 if ht == android.Darwin {
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800135 ctx.Strict(makePrefix+"AR", "${macArPath}")
136 } else {
137 ctx.Strict(makePrefix+"AR", gccCmd(toolchain, "ar"))
138 ctx.Strict(makePrefix+"READELF", gccCmd(toolchain, "readelf"))
139 ctx.Strict(makePrefix+"NM", gccCmd(toolchain, "nm"))
140 }
Dan Willemsen73d21f22016-05-16 14:22:56 -0700141
Colin Cross635c3b02016-05-18 15:37:25 -0700142 if ht == android.Windows {
Dan Willemsen73d21f22016-05-16 14:22:56 -0700143 ctx.Strict(makePrefix+"OBJDUMP", gccCmd(toolchain, "objdump"))
144 }
145
146 if hod.Device() {
147 ctx.Strict(makePrefix+"OBJCOPY", gccCmd(toolchain, "objcopy"))
148 ctx.Strict(makePrefix+"LD", gccCmd(toolchain, "ld"))
149 ctx.Strict(makePrefix+"STRIP", gccCmd(toolchain, "strip"))
150 }
151
152 ctx.Strict(makePrefix+"TOOLS_PREFIX", gccCmd(toolchain, ""))
Dan Willemsen4b7d5de2016-01-12 23:20:28 -0800153}