blob: 9525c38aae823eae56c7b0b53aee576a7c240736 [file] [log] [blame]
Ivan Lozanoffee3342019-08-27 12:03:00 -07001// Copyright 2019 The Android Open Source Project
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 config
16
17import (
18 "android/soong/android"
19)
20
21type Toolchain interface {
22 RustTriple() string
23 ToolchainRustFlags() string
24 ToolchainLinkFlags() string
25
26 SharedLibSuffix() string
27 StaticLibSuffix() string
28 RlibSuffix() string
29 DylibSuffix() string
30 ProcMacroSuffix() string
31 ExecutableSuffix() string
32
33 Is64Bit() bool
34 Supported() bool
Ivan Lozanof1c84332019-09-20 11:00:37 -070035
36 Bionic() bool
Ivan Lozano6cd99e62020-02-11 08:24:25 -050037
38 LibclangRuntimeLibraryArch() string
Ivan Lozanoffee3342019-08-27 12:03:00 -070039}
40
41type toolchainBase struct {
42}
43
44func (toolchainBase) RustTriple() string {
45 panic("toolchainBase does not define a triple.")
46}
47
48func (toolchainBase) ToolchainRustFlags() string {
49 panic("toolchainBase does not provide rust flags.")
50}
51
52func (toolchainBase) ToolchainLinkFlags() string {
53 panic("toolchainBase does not provide link flags.")
54}
55
56func (toolchainBase) Is64Bit() bool {
57 panic("toolchainBase cannot determine datapath width.")
58}
59
Ivan Lozanof1c84332019-09-20 11:00:37 -070060func (toolchainBase) Bionic() bool {
61 return true
62}
63
Ivan Lozanoffee3342019-08-27 12:03:00 -070064type toolchain64Bit struct {
65 toolchainBase
66}
67
68func (toolchain64Bit) Is64Bit() bool {
69 return true
70}
71
72type toolchain32Bit struct {
73 toolchainBase
74}
75
76func (toolchain32Bit) Is64Bit() bool {
77 return false
78}
79
80func (toolchain32Bit) Bionic() bool {
81 return true
82}
83
84func (toolchainBase) ExecutableSuffix() string {
85 return ""
86}
87
88func (toolchainBase) SharedLibSuffix() string {
89 return ".so"
90}
91
92func (toolchainBase) StaticLibSuffix() string {
93 return ".a"
94}
95
96func (toolchainBase) RlibSuffix() string {
97 return ".rlib"
98}
99func (toolchainBase) DylibSuffix() string {
Ivan Lozano52767be2019-10-18 14:49:46 -0700100 return ".dylib.so"
Ivan Lozanoffee3342019-08-27 12:03:00 -0700101}
102
103func (toolchainBase) ProcMacroSuffix() string {
104 return ".so"
105}
106
107func (toolchainBase) Supported() bool {
108 return false
109}
110
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500111func (toolchainBase) LibclangRuntimeLibraryArch() string {
112 return ""
113}
114
115func LibFuzzerRuntimeLibrary(t Toolchain) string {
116 return LibclangRuntimeLibrary(t, "fuzzer")
117}
118
119func LibclangRuntimeLibrary(t Toolchain, library string) string {
120 arch := t.LibclangRuntimeLibraryArch()
121 if arch == "" {
122 return ""
123 }
124 if !t.Bionic() {
125 return "libclang_rt." + library + "-" + arch
126 }
127 return "libclang_rt." + library + "-" + arch + "-android"
128}
129
130func LibRustRuntimeLibrary(t Toolchain, library string) string {
131 arch := t.LibclangRuntimeLibraryArch()
132 if arch == "" {
133 return ""
134 }
135 if !t.Bionic() {
136 return "librustc_rt." + library + "-" + arch
137 }
138 return "librustc_rt." + library + "-" + arch + "-android"
139}
140
Ivan Lozanoffee3342019-08-27 12:03:00 -0700141func toolchainBaseFactory() Toolchain {
142 return &toolchainBase{}
143}
144
145type toolchainFactory func(arch android.Arch) Toolchain
146
147var toolchainFactories = make(map[android.OsType]map[android.ArchType]toolchainFactory)
148
149func registerToolchainFactory(os android.OsType, arch android.ArchType, factory toolchainFactory) {
150 if toolchainFactories[os] == nil {
151 toolchainFactories[os] = make(map[android.ArchType]toolchainFactory)
152 }
153 toolchainFactories[os][arch] = factory
154}
155
156func FindToolchain(os android.OsType, arch android.Arch) Toolchain {
157 factory := toolchainFactories[os][arch.ArchType]
158 if factory == nil {
159 return toolchainBaseFactory()
160 }
161 return factory(arch)
162}