| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 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 |  | 
|  | 15 | package config | 
|  | 16 |  | 
|  | 17 | import ( | 
|  | 18 | "android/soong/android" | 
|  | 19 | ) | 
|  | 20 |  | 
|  | 21 | type 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 Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 35 |  | 
|  | 36 | Bionic() bool | 
| Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 37 |  | 
|  | 38 | LibclangRuntimeLibraryArch() string | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 39 | } | 
|  | 40 |  | 
|  | 41 | type toolchainBase struct { | 
|  | 42 | } | 
|  | 43 |  | 
|  | 44 | func (toolchainBase) RustTriple() string { | 
|  | 45 | panic("toolchainBase does not define a triple.") | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | func (toolchainBase) ToolchainRustFlags() string { | 
|  | 49 | panic("toolchainBase does not provide rust flags.") | 
|  | 50 | } | 
|  | 51 |  | 
|  | 52 | func (toolchainBase) ToolchainLinkFlags() string { | 
|  | 53 | panic("toolchainBase does not provide link flags.") | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | func (toolchainBase) Is64Bit() bool { | 
|  | 57 | panic("toolchainBase cannot determine datapath width.") | 
|  | 58 | } | 
|  | 59 |  | 
| Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 60 | func (toolchainBase) Bionic() bool { | 
|  | 61 | return true | 
|  | 62 | } | 
|  | 63 |  | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 64 | type toolchain64Bit struct { | 
|  | 65 | toolchainBase | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | func (toolchain64Bit) Is64Bit() bool { | 
|  | 69 | return true | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | type toolchain32Bit struct { | 
|  | 73 | toolchainBase | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | func (toolchain32Bit) Is64Bit() bool { | 
|  | 77 | return false | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | func (toolchain32Bit) Bionic() bool { | 
|  | 81 | return true | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | func (toolchainBase) ExecutableSuffix() string { | 
|  | 85 | return "" | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | func (toolchainBase) SharedLibSuffix() string { | 
|  | 89 | return ".so" | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | func (toolchainBase) StaticLibSuffix() string { | 
|  | 93 | return ".a" | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | func (toolchainBase) RlibSuffix() string { | 
|  | 97 | return ".rlib" | 
|  | 98 | } | 
|  | 99 | func (toolchainBase) DylibSuffix() string { | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 100 | return ".dylib.so" | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 101 | } | 
|  | 102 |  | 
|  | 103 | func (toolchainBase) ProcMacroSuffix() string { | 
|  | 104 | return ".so" | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | func (toolchainBase) Supported() bool { | 
|  | 108 | return false | 
|  | 109 | } | 
|  | 110 |  | 
| Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 111 | func (toolchainBase) LibclangRuntimeLibraryArch() string { | 
|  | 112 | return "" | 
|  | 113 | } | 
|  | 114 |  | 
| ThiƩbaud Weksteen | f1ff54a | 2021-03-22 14:24:54 +0100 | [diff] [blame] | 115 | func BuiltinsRuntimeLibrary(t Toolchain) string { | 
|  | 116 | return LibclangRuntimeLibrary(t, "builtins") | 
|  | 117 | } | 
|  | 118 |  | 
| Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 119 | func LibFuzzerRuntimeLibrary(t Toolchain) string { | 
|  | 120 | return LibclangRuntimeLibrary(t, "fuzzer") | 
|  | 121 | } | 
|  | 122 |  | 
|  | 123 | func LibclangRuntimeLibrary(t Toolchain, library string) string { | 
| Colin Cross | fc0df95 | 2022-02-10 11:41:18 -0800 | [diff] [blame] | 124 | return "libclang_rt." + library | 
| Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 125 | } | 
|  | 126 |  | 
|  | 127 | func 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 Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 138 | func toolchainBaseFactory() Toolchain { | 
|  | 139 | return &toolchainBase{} | 
|  | 140 | } | 
|  | 141 |  | 
|  | 142 | type toolchainFactory func(arch android.Arch) Toolchain | 
|  | 143 |  | 
|  | 144 | var toolchainFactories = make(map[android.OsType]map[android.ArchType]toolchainFactory) | 
|  | 145 |  | 
|  | 146 | func 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 |  | 
|  | 153 | func 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 | } |