blob: 9c9d5724a84773e7cab1d2066dd3bb25116b4e8b [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
ThiƩbaud Weksteenf1ff54a2021-03-22 14:24:54 +0100115func BuiltinsRuntimeLibrary(t Toolchain) string {
116 return LibclangRuntimeLibrary(t, "builtins")
117}
118
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500119func LibFuzzerRuntimeLibrary(t Toolchain) string {
120 return LibclangRuntimeLibrary(t, "fuzzer")
121}
122
123func LibclangRuntimeLibrary(t Toolchain, library string) string {
Colin Cross386ee592022-02-10 11:41:18 -0800124 return "libclang_rt." + library
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500125}
126
127func LibRustRuntimeLibrary(t Toolchain, library string) string {
128 arch := t.LibclangRuntimeLibraryArch()
129 if arch == "" {
130 return ""
131 }
132 if !t.Bionic() {
133 return "librustc_rt." + library + "-" + arch
134 }
135 return "librustc_rt." + library + "-" + arch + "-android"
136}
137
Ivan Lozanoffee3342019-08-27 12:03:00 -0700138func toolchainBaseFactory() Toolchain {
139 return &toolchainBase{}
140}
141
142type toolchainFactory func(arch android.Arch) Toolchain
143
144var toolchainFactories = make(map[android.OsType]map[android.ArchType]toolchainFactory)
145
146func registerToolchainFactory(os android.OsType, arch android.ArchType, factory toolchainFactory) {
147 if toolchainFactories[os] == nil {
148 toolchainFactories[os] = make(map[android.ArchType]toolchainFactory)
149 }
150 toolchainFactories[os][arch] = factory
151}
152
153func FindToolchain(os android.OsType, arch android.Arch) Toolchain {
154 factory := toolchainFactories[os][arch.ArchType]
155 if factory == nil {
156 return toolchainBaseFactory()
157 }
158 return factory(arch)
159}