1// 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
35
36	Bionic() bool
37}
38
39type toolchainBase struct {
40}
41
42func (toolchainBase) RustTriple() string {
43	panic("toolchainBase does not define a triple.")
44}
45
46func (toolchainBase) ToolchainRustFlags() string {
47	panic("toolchainBase does not provide rust flags.")
48}
49
50func (toolchainBase) ToolchainLinkFlags() string {
51	panic("toolchainBase does not provide link flags.")
52}
53
54func (toolchainBase) Is64Bit() bool {
55	panic("toolchainBase cannot determine datapath width.")
56}
57
58func (toolchainBase) Bionic() bool {
59	return true
60}
61
62type toolchain64Bit struct {
63	toolchainBase
64}
65
66func (toolchain64Bit) Is64Bit() bool {
67	return true
68}
69
70type toolchain32Bit struct {
71	toolchainBase
72}
73
74func (toolchain32Bit) Is64Bit() bool {
75	return false
76}
77
78func (toolchain32Bit) Bionic() bool {
79	return true
80}
81
82func (toolchainBase) ExecutableSuffix() string {
83	return ""
84}
85
86func (toolchainBase) SharedLibSuffix() string {
87	return ".so"
88}
89
90func (toolchainBase) StaticLibSuffix() string {
91	return ".a"
92}
93
94func (toolchainBase) RlibSuffix() string {
95	return ".rlib"
96}
97func (toolchainBase) DylibSuffix() string {
98	return ".dylib.so"
99}
100
101func (toolchainBase) ProcMacroSuffix() string {
102	return ".so"
103}
104
105func (toolchainBase) Supported() bool {
106	return false
107}
108
109func toolchainBaseFactory() Toolchain {
110	return &toolchainBase{}
111}
112
113type toolchainFactory func(arch android.Arch) Toolchain
114
115var toolchainFactories = make(map[android.OsType]map[android.ArchType]toolchainFactory)
116
117func registerToolchainFactory(os android.OsType, arch android.ArchType, factory toolchainFactory) {
118	if toolchainFactories[os] == nil {
119		toolchainFactories[os] = make(map[android.ArchType]toolchainFactory)
120	}
121	toolchainFactories[os][arch] = factory
122}
123
124func FindToolchain(os android.OsType, arch android.Arch) Toolchain {
125	factory := toolchainFactories[os][arch.ArchType]
126	if factory == nil {
127		return toolchainBaseFactory()
128	}
129	return factory(arch)
130}
131