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