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 rust |
| 16 | |
| 17 | import ( |
Matthew Maurer | a28404a | 2023-11-20 23:33:28 +0000 | [diff] [blame] | 18 | "errors" |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 19 | "fmt" |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 20 | "regexp" |
| 21 | "strings" |
| 22 | |
Colin Cross | 8a49a3d | 2024-05-20 12:22:27 -0700 | [diff] [blame] | 23 | "github.com/google/blueprint" |
Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 24 | "github.com/google/blueprint/depset" |
Colin Cross | 8a49a3d | 2024-05-20 12:22:27 -0700 | [diff] [blame] | 25 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 26 | "android/soong/android" |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 27 | "android/soong/cc" |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 28 | cc_config "android/soong/cc/config" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 29 | ) |
| 30 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 31 | var ( |
Ivan Lozano | 4df0257 | 2023-06-15 14:21:09 -0400 | [diff] [blame] | 32 | RlibStdlibSuffix = ".rlib-std" |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 33 | ) |
| 34 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 35 | func init() { |
| 36 | android.RegisterModuleType("rust_library", RustLibraryFactory) |
| 37 | android.RegisterModuleType("rust_library_dylib", RustLibraryDylibFactory) |
| 38 | android.RegisterModuleType("rust_library_rlib", RustLibraryRlibFactory) |
| 39 | android.RegisterModuleType("rust_library_host", RustLibraryHostFactory) |
| 40 | android.RegisterModuleType("rust_library_host_dylib", RustLibraryDylibHostFactory) |
| 41 | android.RegisterModuleType("rust_library_host_rlib", RustLibraryRlibHostFactory) |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 42 | android.RegisterModuleType("rust_ffi", RustFFIFactory) |
| 43 | android.RegisterModuleType("rust_ffi_shared", RustFFISharedFactory) |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 44 | android.RegisterModuleType("rust_ffi_host", RustFFIHostFactory) |
| 45 | android.RegisterModuleType("rust_ffi_host_shared", RustFFISharedHostFactory) |
Ivan Lozano | 806efd3 | 2024-12-11 21:38:53 +0000 | [diff] [blame] | 46 | android.RegisterModuleType("rust_ffi_static", RustLibraryRlibFactory) |
| 47 | android.RegisterModuleType("rust_ffi_host_static", RustLibraryRlibHostFactory) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | type VariantLibraryProperties struct { |
Matthew Maurer | c761eec | 2020-06-25 00:47:46 -0700 | [diff] [blame] | 51 | Enabled *bool `android:"arch_variant"` |
| 52 | Srcs []string `android:"path,arch_variant"` |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | type LibraryCompilerProperties struct { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 56 | Rlib VariantLibraryProperties `android:"arch_variant"` |
| 57 | Dylib VariantLibraryProperties `android:"arch_variant"` |
| 58 | Shared VariantLibraryProperties `android:"arch_variant"` |
| 59 | Static VariantLibraryProperties `android:"arch_variant"` |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 60 | |
Ivan Lozano | f033ca6 | 2024-03-21 13:43:14 -0400 | [diff] [blame] | 61 | // TODO: Remove this when all instances of Include_dirs have been removed from rust_ffi modules. |
| 62 | // path to include directories to pass to cc_* modules, only relevant for static/shared variants (deprecated, use export_include_dirs instead). |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 63 | Include_dirs []string `android:"path,arch_variant"` |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 64 | |
Ivan Lozano | f033ca6 | 2024-03-21 13:43:14 -0400 | [diff] [blame] | 65 | // path to include directories to export to cc_* modules, only relevant for static/shared variants. |
| 66 | Export_include_dirs []string `android:"path,arch_variant"` |
| 67 | |
Ivan Lozano | f458901 | 2024-11-20 22:18:11 +0000 | [diff] [blame] | 68 | // Version script to pass to the linker. By default this will replace the |
| 69 | // implicit rustc emitted version script to mirror expected behavior in CC. |
| 70 | // This is only relevant for rust_ffi_shared modules which are exposing a |
| 71 | // versioned C API. |
| 72 | Version_script *string `android:"path,arch_variant"` |
| 73 | |
| 74 | // A version_script formatted text file with additional symbols to export |
| 75 | // for rust shared or dylibs which the rustc compiler does not automatically |
| 76 | // export, e.g. additional symbols from whole_static_libs. Unlike |
| 77 | // Version_script, this is not meant to imply a stable API. |
| 78 | Extra_exported_symbols *string `android:"path,arch_variant"` |
| 79 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 80 | // Whether this library is part of the Rust toolchain sysroot. |
| 81 | Sysroot *bool |
Ashutosh Agarwal | 46e4fad | 2024-08-27 17:13:12 +0000 | [diff] [blame] | 82 | |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 83 | // Deprecated - exclude this rust_ffi target from being included in APEXes. |
| 84 | // TODO(b/362509506): remove this once all apex_exclude uses are switched to stubs. |
Ashutosh Agarwal | 46e4fad | 2024-08-27 17:13:12 +0000 | [diff] [blame] | 85 | Apex_exclude *bool |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 86 | |
| 87 | // Generate stubs to make this library accessible to APEXes. |
| 88 | // Can only be set for modules producing shared libraries. |
| 89 | Stubs cc.StubsProperties `android:"arch_variant"` |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | type LibraryMutatedProperties struct { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 93 | // Build a dylib variant |
| 94 | BuildDylib bool `blueprint:"mutated"` |
| 95 | // Build an rlib variant |
| 96 | BuildRlib bool `blueprint:"mutated"` |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 97 | // Build a shared library variant |
| 98 | BuildShared bool `blueprint:"mutated"` |
| 99 | // Build a static library variant |
| 100 | BuildStatic bool `blueprint:"mutated"` |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 101 | |
| 102 | // This variant is a dylib |
| 103 | VariantIsDylib bool `blueprint:"mutated"` |
| 104 | // This variant is an rlib |
| 105 | VariantIsRlib bool `blueprint:"mutated"` |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 106 | // This variant is a shared library |
| 107 | VariantIsShared bool `blueprint:"mutated"` |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 108 | // This variant is a source provider |
| 109 | VariantIsSource bool `blueprint:"mutated"` |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 110 | |
| 111 | // This variant is disabled and should not be compiled |
| 112 | // (used for SourceProvider variants that produce only source) |
| 113 | VariantIsDisabled bool `blueprint:"mutated"` |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 114 | |
| 115 | // Whether this library variant should be link libstd via rlibs |
| 116 | VariantIsStaticStd bool `blueprint:"mutated"` |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 117 | |
| 118 | // This variant is a stubs lib |
| 119 | BuildStubs bool `blueprint:"mutated"` |
| 120 | // This variant is the latest version |
| 121 | IsLatestVersion bool `blueprint:"mutated"` |
| 122 | // Version of the stubs lib |
| 123 | StubsVersion string `blueprint:"mutated"` |
| 124 | // List of all stubs versions associated with an implementation lib |
| 125 | AllStubsVersions []string `blueprint:"mutated"` |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | type libraryDecorator struct { |
| 129 | *baseCompiler |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 130 | *flagExporter |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 131 | stripper Stripper |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 132 | |
Ivan Lozano | 8a23fa4 | 2020-06-16 10:26:57 -0400 | [diff] [blame] | 133 | Properties LibraryCompilerProperties |
| 134 | MutatedProperties LibraryMutatedProperties |
| 135 | includeDirs android.Paths |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 136 | sourceProvider SourceProvider |
Ivan Lozano | 1921e80 | 2021-05-20 13:39:16 -0400 | [diff] [blame] | 137 | |
Ivan Lozano | 7b0781d | 2021-11-03 15:30:18 -0400 | [diff] [blame] | 138 | // table-of-contents file for cdylib crates to optimize out relinking when possible |
| 139 | tocFile android.OptionalPath |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 140 | |
| 141 | // Path to the file containing the APIs exported by this library |
| 142 | stubsSymbolFilePath android.Path |
| 143 | apiListCoverageXmlPath android.ModuleOutPath |
| 144 | versionScriptPath android.OptionalPath |
| 145 | } |
| 146 | |
| 147 | func (library *libraryDecorator) stubs() bool { |
| 148 | return library.MutatedProperties.BuildStubs |
| 149 | } |
| 150 | |
| 151 | func (library *libraryDecorator) setAPIListCoverageXMLPath(xml android.ModuleOutPath) { |
| 152 | library.apiListCoverageXmlPath = xml |
| 153 | } |
| 154 | |
| 155 | func (library *libraryDecorator) libraryProperties() LibraryCompilerProperties { |
| 156 | return library.Properties |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | type libraryInterface interface { |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 160 | cc.VersionedInterface |
| 161 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 162 | rlib() bool |
| 163 | dylib() bool |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 164 | static() bool |
| 165 | shared() bool |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 166 | sysroot() bool |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 167 | source() bool |
Ashutosh Agarwal | 46e4fad | 2024-08-27 17:13:12 +0000 | [diff] [blame] | 168 | apexExclude() bool |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 169 | |
| 170 | // Returns true if the build options for the module have selected a particular build type |
| 171 | buildRlib() bool |
| 172 | buildDylib() bool |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 173 | buildShared() bool |
| 174 | buildStatic() bool |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 175 | |
| 176 | // Sets a particular variant type |
| 177 | setRlib() |
| 178 | setDylib() |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 179 | setShared() |
| 180 | setStatic() |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 181 | setSource() |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 182 | |
Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 183 | // libstd linkage functions |
| 184 | rlibStd() bool |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 185 | setRlibStd() |
| 186 | setDylibStd() |
| 187 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 188 | // Build a specific library variant |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 189 | BuildOnlyFFI() |
| 190 | BuildOnlyRust() |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 191 | BuildOnlyRlib() |
| 192 | BuildOnlyDylib() |
| 193 | BuildOnlyStatic() |
| 194 | BuildOnlyShared() |
Ivan Lozano | 7b0781d | 2021-11-03 15:30:18 -0400 | [diff] [blame] | 195 | |
| 196 | toc() android.OptionalPath |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 197 | |
| 198 | IsStubsImplementationRequired() bool |
| 199 | setAPIListCoverageXMLPath(out android.ModuleOutPath) |
| 200 | |
| 201 | libraryProperties() LibraryCompilerProperties |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 204 | func (library *libraryDecorator) nativeCoverage() bool { |
| 205 | return true |
| 206 | } |
| 207 | |
Ivan Lozano | 7b0781d | 2021-11-03 15:30:18 -0400 | [diff] [blame] | 208 | func (library *libraryDecorator) toc() android.OptionalPath { |
| 209 | return library.tocFile |
| 210 | } |
| 211 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 212 | func (library *libraryDecorator) rlib() bool { |
| 213 | return library.MutatedProperties.VariantIsRlib |
| 214 | } |
| 215 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 216 | func (library *libraryDecorator) sysroot() bool { |
| 217 | return Bool(library.Properties.Sysroot) |
| 218 | } |
| 219 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 220 | func (library *libraryDecorator) dylib() bool { |
| 221 | return library.MutatedProperties.VariantIsDylib |
| 222 | } |
| 223 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 224 | func (library *libraryDecorator) shared() bool { |
| 225 | return library.MutatedProperties.VariantIsShared |
| 226 | } |
| 227 | |
| 228 | func (library *libraryDecorator) static() bool { |
Colin Cross | 17f9dc5 | 2024-07-01 20:05:54 -0700 | [diff] [blame] | 229 | return false |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 232 | func (library *libraryDecorator) source() bool { |
| 233 | return library.MutatedProperties.VariantIsSource |
| 234 | } |
| 235 | |
Ashutosh Agarwal | 46e4fad | 2024-08-27 17:13:12 +0000 | [diff] [blame] | 236 | func (library *libraryDecorator) apexExclude() bool { |
| 237 | return Bool(library.Properties.Apex_exclude) |
| 238 | } |
| 239 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 240 | func (library *libraryDecorator) buildRlib() bool { |
| 241 | return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true) |
| 242 | } |
| 243 | |
| 244 | func (library *libraryDecorator) buildDylib() bool { |
| 245 | return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true) |
| 246 | } |
| 247 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 248 | func (library *libraryDecorator) buildShared() bool { |
| 249 | return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true) |
| 250 | } |
| 251 | |
| 252 | func (library *libraryDecorator) buildStatic() bool { |
| 253 | return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true) |
| 254 | } |
| 255 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 256 | func (library *libraryDecorator) setRlib() { |
| 257 | library.MutatedProperties.VariantIsRlib = true |
| 258 | library.MutatedProperties.VariantIsDylib = false |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 259 | library.MutatedProperties.VariantIsShared = false |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | func (library *libraryDecorator) setDylib() { |
| 263 | library.MutatedProperties.VariantIsRlib = false |
| 264 | library.MutatedProperties.VariantIsDylib = true |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 265 | library.MutatedProperties.VariantIsShared = false |
| 266 | } |
| 267 | |
Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 268 | func (library *libraryDecorator) rlibStd() bool { |
| 269 | return library.MutatedProperties.VariantIsStaticStd |
| 270 | } |
| 271 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 272 | func (library *libraryDecorator) setRlibStd() { |
| 273 | library.MutatedProperties.VariantIsStaticStd = true |
| 274 | } |
| 275 | |
| 276 | func (library *libraryDecorator) setDylibStd() { |
| 277 | library.MutatedProperties.VariantIsStaticStd = false |
| 278 | } |
| 279 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 280 | func (library *libraryDecorator) setShared() { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 281 | library.MutatedProperties.VariantIsShared = true |
| 282 | library.MutatedProperties.VariantIsRlib = false |
| 283 | library.MutatedProperties.VariantIsDylib = false |
| 284 | } |
| 285 | |
| 286 | func (library *libraryDecorator) setStatic() { |
Colin Cross | 17f9dc5 | 2024-07-01 20:05:54 -0700 | [diff] [blame] | 287 | panic(fmt.Errorf("static variant is not supported for rust modules, use the rlib variant instead")) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 290 | func (library *libraryDecorator) setSource() { |
| 291 | library.MutatedProperties.VariantIsSource = true |
| 292 | } |
| 293 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 294 | func (library *libraryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep { |
Ivan Lozano | add122a | 2023-07-13 11:01:41 -0400 | [diff] [blame] | 295 | if library.preferRlib() { |
Ivan Lozano | ea08613 | 2020-12-08 14:43:00 -0500 | [diff] [blame] | 296 | return rlibAutoDep |
| 297 | } else if library.rlib() || library.static() { |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 298 | return rlibAutoDep |
| 299 | } else if library.dylib() || library.shared() { |
| 300 | return dylibAutoDep |
| 301 | } else { |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 302 | panic(fmt.Errorf("autoDep called on library %q that has no enabled variants.", ctx.ModuleName())) |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
Ivan Lozano | 806efd3 | 2024-12-11 21:38:53 +0000 | [diff] [blame] | 306 | func (library *libraryDecorator) stdLinkage(device bool) RustLinkage { |
| 307 | if library.static() || library.MutatedProperties.VariantIsStaticStd { |
Ivan Lozano | ea08613 | 2020-12-08 14:43:00 -0500 | [diff] [blame] | 308 | return RlibLinkage |
| 309 | } else if library.baseCompiler.preferRlib() { |
| 310 | return RlibLinkage |
| 311 | } |
Ivan Lozano | 806efd3 | 2024-12-11 21:38:53 +0000 | [diff] [blame] | 312 | return DylibLinkage |
Ivan Lozano | ea08613 | 2020-12-08 14:43:00 -0500 | [diff] [blame] | 313 | } |
| 314 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 315 | var _ compiler = (*libraryDecorator)(nil) |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 316 | var _ libraryInterface = (*libraryDecorator)(nil) |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 317 | var _ cc.VersionedInterface = (*libraryDecorator)(nil) |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 318 | var _ exportedFlagsProducer = (*libraryDecorator)(nil) |
Ivan Lozano | 9eaacc8 | 2024-10-30 14:28:17 +0000 | [diff] [blame] | 319 | var _ cc.VersionedInterface = (*libraryDecorator)(nil) |
| 320 | |
| 321 | func (library *libraryDecorator) HasLLNDKStubs() bool { |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 322 | // Rust LLNDK is currently unsupported |
Ivan Lozano | 9eaacc8 | 2024-10-30 14:28:17 +0000 | [diff] [blame] | 323 | return false |
| 324 | } |
| 325 | |
| 326 | func (library *libraryDecorator) HasVendorPublicLibrary() bool { |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 327 | // Rust does not support vendor_public_library yet. |
Ivan Lozano | 9eaacc8 | 2024-10-30 14:28:17 +0000 | [diff] [blame] | 328 | return false |
| 329 | } |
| 330 | |
| 331 | func (library *libraryDecorator) HasLLNDKHeaders() bool { |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 332 | // Rust LLNDK is currently unsupported |
Ivan Lozano | 9eaacc8 | 2024-10-30 14:28:17 +0000 | [diff] [blame] | 333 | return false |
| 334 | } |
| 335 | |
| 336 | func (library *libraryDecorator) HasStubsVariants() bool { |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 337 | // Just having stubs.symbol_file is enough to create a stub variant. In that case |
| 338 | // the stub for the future API level is created. |
| 339 | return library.Properties.Stubs.Symbol_file != nil || |
| 340 | len(library.Properties.Stubs.Versions) > 0 |
Ivan Lozano | 9eaacc8 | 2024-10-30 14:28:17 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | func (library *libraryDecorator) IsStubsImplementationRequired() bool { |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 344 | return BoolDefault(library.Properties.Stubs.Implementation_installable, true) |
Ivan Lozano | 9eaacc8 | 2024-10-30 14:28:17 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | func (library *libraryDecorator) GetAPIListCoverageXMLPath() android.ModuleOutPath { |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 348 | return library.apiListCoverageXmlPath |
Ivan Lozano | 9eaacc8 | 2024-10-30 14:28:17 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | func (library *libraryDecorator) AllStubsVersions() []string { |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 352 | return library.MutatedProperties.AllStubsVersions |
Ivan Lozano | 9eaacc8 | 2024-10-30 14:28:17 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | func (library *libraryDecorator) SetAllStubsVersions(versions []string) { |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 356 | library.MutatedProperties.AllStubsVersions = versions |
Ivan Lozano | 9eaacc8 | 2024-10-30 14:28:17 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | func (library *libraryDecorator) SetStubsVersion(version string) { |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 360 | library.MutatedProperties.StubsVersion = version |
Ivan Lozano | 9eaacc8 | 2024-10-30 14:28:17 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | func (library *libraryDecorator) SetBuildStubs(isLatest bool) { |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 364 | library.MutatedProperties.BuildStubs = true |
| 365 | library.MutatedProperties.IsLatestVersion = isLatest |
Ivan Lozano | 9eaacc8 | 2024-10-30 14:28:17 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | func (library *libraryDecorator) BuildStubs() bool { |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 369 | return library.MutatedProperties.BuildStubs |
Ivan Lozano | 9eaacc8 | 2024-10-30 14:28:17 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | func (library *libraryDecorator) ImplementationModuleName(name string) string { |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 373 | return name |
Ivan Lozano | 9eaacc8 | 2024-10-30 14:28:17 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | func (library *libraryDecorator) IsLLNDKMovedToApex() bool { |
| 377 | // Rust does not support LLNDK. |
| 378 | return false |
| 379 | } |
| 380 | |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 381 | func (library *libraryDecorator) StubsVersion() string { |
| 382 | return library.MutatedProperties.StubsVersion |
Ivan Lozano | 9eaacc8 | 2024-10-30 14:28:17 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 385 | // stubsVersions implements cc.VersionedInterface. |
| 386 | func (library *libraryDecorator) StubsVersions(ctx android.BaseModuleContext) []string { |
| 387 | if !library.HasStubsVariants() { |
| 388 | return nil |
| 389 | } |
| 390 | |
| 391 | // Future API level is implicitly added if there isn't |
| 392 | versions := cc.AddCurrentVersionIfNotPresent(library.Properties.Stubs.Versions) |
| 393 | cc.NormalizeVersions(ctx, versions) |
| 394 | return versions |
Ivan Lozano | 9eaacc8 | 2024-10-30 14:28:17 +0000 | [diff] [blame] | 395 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 396 | |
Martin Geisler | 67ec054 | 2022-11-18 12:08:55 +0100 | [diff] [blame] | 397 | // rust_library produces all Rust variants (rust_library_dylib and |
| 398 | // rust_library_rlib). |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 399 | func RustLibraryFactory() android.Module { |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 400 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 401 | library.BuildOnlyRust() |
| 402 | return module.Init() |
| 403 | } |
| 404 | |
Ivan Lozano | 6184842 | 2024-12-13 19:45:00 +0000 | [diff] [blame] | 405 | // rust_ffi produces all FFI variants (rust_ffi_shared, rust_ffi_static). |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 406 | func RustFFIFactory() android.Module { |
| 407 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 408 | library.BuildOnlyFFI() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 409 | return module.Init() |
| 410 | } |
| 411 | |
Martin Geisler | 67ec054 | 2022-11-18 12:08:55 +0100 | [diff] [blame] | 412 | // rust_library_dylib produces a Rust dylib (Rust crate type "dylib"). |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 413 | func RustLibraryDylibFactory() android.Module { |
| 414 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 415 | library.BuildOnlyDylib() |
| 416 | return module.Init() |
| 417 | } |
| 418 | |
Ivan Lozano | 806efd3 | 2024-12-11 21:38:53 +0000 | [diff] [blame] | 419 | // rust_library_rlib and rust_ffi_static produces an rlib (Rust crate type "rlib"). |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 420 | func RustLibraryRlibFactory() android.Module { |
| 421 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 422 | library.BuildOnlyRlib() |
| 423 | return module.Init() |
| 424 | } |
| 425 | |
Martin Geisler | 67ec054 | 2022-11-18 12:08:55 +0100 | [diff] [blame] | 426 | // rust_ffi_shared produces a shared library (Rust crate type |
| 427 | // "cdylib"). |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 428 | func RustFFISharedFactory() android.Module { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 429 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 430 | library.BuildOnlyShared() |
| 431 | return module.Init() |
| 432 | } |
| 433 | |
Martin Geisler | 67ec054 | 2022-11-18 12:08:55 +0100 | [diff] [blame] | 434 | // rust_library_host produces all Rust variants for the host |
| 435 | // (rust_library_dylib_host and rust_library_rlib_host). |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 436 | func RustLibraryHostFactory() android.Module { |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 437 | module, library := NewRustLibrary(android.HostSupported) |
| 438 | library.BuildOnlyRust() |
| 439 | return module.Init() |
| 440 | } |
| 441 | |
Martin Geisler | 67ec054 | 2022-11-18 12:08:55 +0100 | [diff] [blame] | 442 | // rust_ffi_host produces all FFI variants for the host |
Ivan Lozano | 6184842 | 2024-12-13 19:45:00 +0000 | [diff] [blame] | 443 | // (rust_ffi_static_host and rust_ffi_shared_host). |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 444 | func RustFFIHostFactory() android.Module { |
| 445 | module, library := NewRustLibrary(android.HostSupported) |
| 446 | library.BuildOnlyFFI() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 447 | return module.Init() |
| 448 | } |
| 449 | |
Martin Geisler | 67ec054 | 2022-11-18 12:08:55 +0100 | [diff] [blame] | 450 | // rust_library_dylib_host produces a dylib for the host (Rust crate |
| 451 | // type "dylib"). |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 452 | func RustLibraryDylibHostFactory() android.Module { |
| 453 | module, library := NewRustLibrary(android.HostSupported) |
| 454 | library.BuildOnlyDylib() |
| 455 | return module.Init() |
| 456 | } |
| 457 | |
Ivan Lozano | 806efd3 | 2024-12-11 21:38:53 +0000 | [diff] [blame] | 458 | // rust_library_rlib_host and rust_ffi_static_host produces an rlib for the host |
| 459 | // (Rust crate type "rlib"). |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 460 | func RustLibraryRlibHostFactory() android.Module { |
| 461 | module, library := NewRustLibrary(android.HostSupported) |
| 462 | library.BuildOnlyRlib() |
| 463 | return module.Init() |
| 464 | } |
| 465 | |
Martin Geisler | 67ec054 | 2022-11-18 12:08:55 +0100 | [diff] [blame] | 466 | // rust_ffi_shared_host produces an shared library for the host (Rust |
| 467 | // crate type "cdylib"). |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 468 | func RustFFISharedHostFactory() android.Module { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 469 | module, library := NewRustLibrary(android.HostSupported) |
| 470 | library.BuildOnlyShared() |
| 471 | return module.Init() |
| 472 | } |
| 473 | |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 474 | func CheckRustLibraryProperties(mctx android.DefaultableHookContext) { |
| 475 | lib := mctx.Module().(*Module).compiler.(libraryInterface) |
| 476 | if !lib.buildShared() { |
| 477 | if lib.libraryProperties().Stubs.Symbol_file != nil || |
| 478 | lib.libraryProperties().Stubs.Implementation_installable != nil || |
| 479 | len(lib.libraryProperties().Stubs.Versions) > 0 { |
| 480 | |
| 481 | mctx.PropertyErrorf("stubs", "stubs properties can only be set for rust_ffi or rust_ffi_shared modules") |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 486 | func (library *libraryDecorator) BuildOnlyFFI() { |
| 487 | library.MutatedProperties.BuildDylib = false |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 488 | // we build rlibs for later static ffi linkage. |
| 489 | library.MutatedProperties.BuildRlib = true |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 490 | library.MutatedProperties.BuildShared = true |
Ivan Lozano | fd47b1a | 2024-05-17 14:13:41 -0400 | [diff] [blame] | 491 | library.MutatedProperties.BuildStatic = false |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | func (library *libraryDecorator) BuildOnlyRust() { |
| 495 | library.MutatedProperties.BuildDylib = true |
| 496 | library.MutatedProperties.BuildRlib = true |
| 497 | library.MutatedProperties.BuildShared = false |
| 498 | library.MutatedProperties.BuildStatic = false |
| 499 | } |
| 500 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 501 | func (library *libraryDecorator) BuildOnlyDylib() { |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 502 | library.MutatedProperties.BuildDylib = true |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 503 | library.MutatedProperties.BuildRlib = false |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 504 | library.MutatedProperties.BuildShared = false |
| 505 | library.MutatedProperties.BuildStatic = false |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | func (library *libraryDecorator) BuildOnlyRlib() { |
| 509 | library.MutatedProperties.BuildDylib = false |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 510 | library.MutatedProperties.BuildRlib = true |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 511 | library.MutatedProperties.BuildShared = false |
| 512 | library.MutatedProperties.BuildStatic = false |
| 513 | } |
| 514 | |
| 515 | func (library *libraryDecorator) BuildOnlyStatic() { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 516 | library.MutatedProperties.BuildRlib = false |
| 517 | library.MutatedProperties.BuildDylib = false |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 518 | library.MutatedProperties.BuildShared = false |
| 519 | library.MutatedProperties.BuildStatic = true |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | func (library *libraryDecorator) BuildOnlyShared() { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 523 | library.MutatedProperties.BuildRlib = false |
| 524 | library.MutatedProperties.BuildDylib = false |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 525 | library.MutatedProperties.BuildStatic = false |
| 526 | library.MutatedProperties.BuildShared = true |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) { |
Ivan Lozano | 9d1df10 | 2020-04-28 10:10:23 -0400 | [diff] [blame] | 530 | module := newModule(hod, android.MultilibBoth) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 531 | |
| 532 | library := &libraryDecorator{ |
| 533 | MutatedProperties: LibraryMutatedProperties{ |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 534 | BuildDylib: false, |
| 535 | BuildRlib: false, |
| 536 | BuildShared: false, |
| 537 | BuildStatic: false, |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 538 | }, |
Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 539 | baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem), |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 540 | flagExporter: NewFlagExporter(), |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | module.compiler = library |
| 544 | |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 545 | module.SetDefaultableHook(CheckRustLibraryProperties) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 546 | return module, library |
| 547 | } |
| 548 | |
| 549 | func (library *libraryDecorator) compilerProps() []interface{} { |
| 550 | return append(library.baseCompiler.compilerProps(), |
| 551 | &library.Properties, |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 552 | &library.MutatedProperties, |
| 553 | &library.stripper.StripProperties) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 556 | func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps { |
| 557 | deps = library.baseCompiler.compilerDeps(ctx, deps) |
| 558 | |
Colin Cross | e32f093 | 2022-01-23 20:48:36 -0800 | [diff] [blame] | 559 | if library.dylib() || library.shared() { |
| 560 | if ctx.toolchain().Bionic() { |
| 561 | deps = bionicDeps(ctx, deps, false) |
| 562 | deps.CrtBegin = []string{"crtbegin_so"} |
| 563 | deps.CrtEnd = []string{"crtend_so"} |
| 564 | } else if ctx.Os() == android.LinuxMusl { |
| 565 | deps = muslDeps(ctx, deps, false) |
| 566 | deps.CrtBegin = []string{"libc_musl_crtbegin_so"} |
| 567 | deps.CrtEnd = []string{"libc_musl_crtend_so"} |
| 568 | } |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | return deps |
| 572 | } |
Ivan Lozano | bec05ea | 2020-06-09 08:27:49 -0400 | [diff] [blame] | 573 | |
| 574 | func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string { |
| 575 | return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix() |
| 576 | } |
| 577 | |
Ivan Lozano | 28ed8f4 | 2024-05-06 21:46:39 -0400 | [diff] [blame] | 578 | // Library cfg flags common to all variants |
| 579 | func CommonLibraryCfgFlags(ctx android.ModuleContext, flags Flags) Flags { |
| 580 | return flags |
| 581 | } |
| 582 | |
Ivan Lozano | 67eada3 | 2021-09-23 11:50:33 -0400 | [diff] [blame] | 583 | func (library *libraryDecorator) cfgFlags(ctx ModuleContext, flags Flags) Flags { |
| 584 | flags = library.baseCompiler.cfgFlags(ctx, flags) |
Ivan Lozano | 28ed8f4 | 2024-05-06 21:46:39 -0400 | [diff] [blame] | 585 | flags = CommonLibraryCfgFlags(ctx, flags) |
| 586 | |
Cole Faust | fdec872 | 2024-05-22 11:38:29 -0700 | [diff] [blame] | 587 | cfgs := library.baseCompiler.Properties.Cfgs.GetOrDefault(ctx, nil) |
| 588 | |
Cole Faust | fdec872 | 2024-05-22 11:38:29 -0700 | [diff] [blame] | 589 | cfgFlags := cfgsToFlags(cfgs) |
| 590 | |
| 591 | flags.RustFlags = append(flags.RustFlags, cfgFlags...) |
| 592 | flags.RustdocFlags = append(flags.RustdocFlags, cfgFlags...) |
Ivan Lozano | 28ed8f4 | 2024-05-06 21:46:39 -0400 | [diff] [blame] | 593 | |
| 594 | return flags |
| 595 | } |
| 596 | |
| 597 | // Common flags applied to all libraries irrespective of properties or variant should be included here |
| 598 | func CommonLibraryCompilerFlags(ctx android.ModuleContext, flags Flags) Flags { |
| 599 | flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName()) |
Ivan Lozano | 67eada3 | 2021-09-23 11:50:33 -0400 | [diff] [blame] | 600 | |
| 601 | return flags |
| 602 | } |
| 603 | |
| 604 | func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags { |
Ivan Lozano | e0833b1 | 2019-11-06 19:15:49 -0800 | [diff] [blame] | 605 | flags = library.baseCompiler.compilerFlags(ctx, flags) |
Ivan Lozano | 67eada3 | 2021-09-23 11:50:33 -0400 | [diff] [blame] | 606 | |
Ivan Lozano | 28ed8f4 | 2024-05-06 21:46:39 -0400 | [diff] [blame] | 607 | flags = CommonLibraryCompilerFlags(ctx, flags) |
| 608 | |
Ivan Lozano | 806efd3 | 2024-12-11 21:38:53 +0000 | [diff] [blame] | 609 | if library.rlib() || library.shared() { |
| 610 | // rlibs collect include dirs as well since they are used to |
| 611 | // produce staticlibs in the final C linkages |
Ivan Lozano | e0833b1 | 2019-11-06 19:15:49 -0800 | [diff] [blame] | 612 | library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...) |
Ivan Lozano | f033ca6 | 2024-03-21 13:43:14 -0400 | [diff] [blame] | 613 | library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Export_include_dirs)...) |
Ivan Lozano | e0833b1 | 2019-11-06 19:15:49 -0800 | [diff] [blame] | 614 | } |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 615 | |
Ivan Lozano | bec05ea | 2020-06-09 08:27:49 -0400 | [diff] [blame] | 616 | if library.shared() { |
A. Cody Schuffelen | c183e3a | 2023-08-14 21:09:47 -0700 | [diff] [blame] | 617 | if ctx.Darwin() { |
| 618 | flags.LinkFlags = append( |
| 619 | flags.LinkFlags, |
| 620 | "-dynamic_lib", |
| 621 | "-install_name @rpath/"+library.sharedLibFilename(ctx), |
| 622 | ) |
| 623 | } else { |
| 624 | flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx)) |
| 625 | } |
Ivan Lozano | bec05ea | 2020-06-09 08:27:49 -0400 | [diff] [blame] | 626 | } |
| 627 | |
Ivan Lozano | e0833b1 | 2019-11-06 19:15:49 -0800 | [diff] [blame] | 628 | return flags |
| 629 | } |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 630 | |
Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 631 | func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput { |
| 632 | var outputFile android.ModuleOutPath |
| 633 | var ret buildOutput |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 634 | var fileName string |
Matthew Maurer | a28404a | 2023-11-20 23:33:28 +0000 | [diff] [blame] | 635 | crateRootPath := crateRootPath(ctx, library) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 636 | |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 637 | if library.sourceProvider != nil { |
Ivan Lozano | 9d74a52 | 2020-12-01 09:25:22 -0500 | [diff] [blame] | 638 | deps.srcProviderFiles = append(deps.srcProviderFiles, library.sourceProvider.Srcs()...) |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 639 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 640 | |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 641 | // Ensure link dirs are not duplicated |
| 642 | deps.linkDirs = android.FirstUniqueStrings(deps.linkDirs) |
| 643 | |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 644 | // Calculate output filename |
| 645 | if library.rlib() { |
| 646 | fileName = library.getStem(ctx) + ctx.toolchain().RlibSuffix() |
| 647 | outputFile = android.PathForModuleOut(ctx, fileName) |
Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 648 | ret.outputFile = outputFile |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 649 | } else if library.dylib() { |
| 650 | fileName = library.getStem(ctx) + ctx.toolchain().DylibSuffix() |
| 651 | outputFile = android.PathForModuleOut(ctx, fileName) |
Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 652 | ret.outputFile = outputFile |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 653 | } else if library.static() { |
| 654 | fileName = library.getStem(ctx) + ctx.toolchain().StaticLibSuffix() |
| 655 | outputFile = android.PathForModuleOut(ctx, fileName) |
Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 656 | ret.outputFile = outputFile |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 657 | } else if library.shared() { |
| 658 | fileName = library.sharedLibFilename(ctx) |
| 659 | outputFile = android.PathForModuleOut(ctx, fileName) |
Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 660 | ret.outputFile = outputFile |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | if !library.rlib() && !library.static() && library.stripper.NeedsStrip(ctx) { |
| 664 | strippedOutputFile := outputFile |
| 665 | outputFile = android.PathForModuleOut(ctx, "unstripped", fileName) |
| 666 | library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile) |
| 667 | |
| 668 | library.baseCompiler.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile) |
| 669 | } |
| 670 | library.baseCompiler.unstrippedOutputFile = outputFile |
| 671 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 672 | flags.RustFlags = append(flags.RustFlags, deps.depFlags...) |
Ivan Lozano | 3dfa12d | 2021-02-04 11:29:41 -0500 | [diff] [blame] | 673 | flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...) |
Ivan Lozano | 1f10f68 | 2024-11-08 16:16:50 +0000 | [diff] [blame] | 674 | flags.LinkFlags = append(flags.LinkFlags, deps.rustLibObjects...) |
| 675 | flags.LinkFlags = append(flags.LinkFlags, deps.sharedLibObjects...) |
| 676 | flags.LinkFlags = append(flags.LinkFlags, deps.staticLibObjects...) |
| 677 | flags.LinkFlags = append(flags.LinkFlags, deps.wholeStaticLibObjects...) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 678 | |
Ivan Lozano | f458901 | 2024-11-20 22:18:11 +0000 | [diff] [blame] | 679 | if String(library.Properties.Version_script) != "" { |
| 680 | if String(library.Properties.Extra_exported_symbols) != "" { |
| 681 | ctx.ModuleErrorf("version_script and extra_exported_symbols cannot both be set.") |
| 682 | } |
| 683 | |
| 684 | if library.shared() { |
| 685 | // "-Wl,--android-version-script" signals to the rustcLinker script |
| 686 | // that the default version script should be removed. |
| 687 | flags.LinkFlags = append(flags.LinkFlags, "-Wl,--android-version-script="+android.PathForModuleSrc(ctx, String(library.Properties.Version_script)).String()) |
| 688 | deps.LinkerDeps = append(deps.LinkerDeps, android.PathForModuleSrc(ctx, String(library.Properties.Version_script))) |
| 689 | } else if !library.static() && !library.rlib() { |
| 690 | // We include rlibs here because rust_ffi produces rlib variants |
| 691 | ctx.PropertyErrorf("version_script", "can only be set for rust_ffi modules") |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | if String(library.Properties.Extra_exported_symbols) != "" { |
| 696 | // Passing a second version script (rustc calculates and emits a |
| 697 | // default version script) will concatenate the first version script. |
| 698 | flags.LinkFlags = append(flags.LinkFlags, "-Wl,--version-script="+android.PathForModuleSrc(ctx, String(library.Properties.Extra_exported_symbols)).String()) |
| 699 | deps.LinkerDeps = append(deps.LinkerDeps, android.PathForModuleSrc(ctx, String(library.Properties.Extra_exported_symbols))) |
| 700 | } |
| 701 | |
Matthew Maurer | 46c46cc | 2020-01-13 16:34:34 -0800 | [diff] [blame] | 702 | if library.dylib() { |
Ivan Lozano | f458901 | 2024-11-20 22:18:11 +0000 | [diff] [blame] | 703 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 704 | // We need prefer-dynamic for now to avoid linking in the static stdlib. See: |
| 705 | // https://github.com/rust-lang/rust/issues/19680 |
| 706 | // https://github.com/rust-lang/rust/issues/34909 |
| 707 | flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic") |
| 708 | } |
| 709 | |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 710 | // Call the appropriate builder for this library type |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 711 | if library.stubs() { |
| 712 | ccFlags := library.getApiStubsCcFlags(ctx) |
| 713 | stubObjs := library.compileModuleLibApiStubs(ctx, ccFlags) |
Ivan Lozano | 47596d6 | 2025-01-28 13:50:55 +0000 | [diff] [blame] | 714 | cc.BuildRustStubs(ctx, outputFile, stubObjs, ccFlags) |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 715 | } else if library.rlib() { |
Sam Delmerico | 63ca14e | 2023-09-25 12:13:17 +0000 | [diff] [blame] | 716 | ret.kytheFile = TransformSrctoRlib(ctx, crateRootPath, deps, flags, outputFile).kytheFile |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 717 | } else if library.dylib() { |
Sam Delmerico | 63ca14e | 2023-09-25 12:13:17 +0000 | [diff] [blame] | 718 | ret.kytheFile = TransformSrctoDylib(ctx, crateRootPath, deps, flags, outputFile).kytheFile |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 719 | } else if library.static() { |
Sam Delmerico | 63ca14e | 2023-09-25 12:13:17 +0000 | [diff] [blame] | 720 | ret.kytheFile = TransformSrctoStatic(ctx, crateRootPath, deps, flags, outputFile).kytheFile |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 721 | } else if library.shared() { |
Sam Delmerico | 63ca14e | 2023-09-25 12:13:17 +0000 | [diff] [blame] | 722 | ret.kytheFile = TransformSrctoShared(ctx, crateRootPath, deps, flags, outputFile).kytheFile |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 723 | } |
| 724 | |
Ivan Lozano | 1f10f68 | 2024-11-08 16:16:50 +0000 | [diff] [blame] | 725 | // rlibs and dylibs propagate their shared, whole static, and rustlib dependencies |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 726 | if library.rlib() || library.dylib() { |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 727 | library.flagExporter.exportLinkDirs(deps.linkDirs...) |
Ivan Lozano | 1f10f68 | 2024-11-08 16:16:50 +0000 | [diff] [blame] | 728 | library.flagExporter.exportRustLibs(deps.rustLibObjects...) |
| 729 | library.flagExporter.exportSharedLibs(deps.sharedLibObjects...) |
| 730 | library.flagExporter.exportWholeStaticLibs(deps.wholeStaticLibObjects...) |
| 731 | } |
| 732 | |
| 733 | // rlibs also propagate their staticlibs dependencies |
| 734 | if library.rlib() { |
| 735 | library.flagExporter.exportStaticLibs(deps.staticLibObjects...) |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 736 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 737 | |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 738 | // Since we have FFI rlibs, we need to collect their includes as well |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 739 | if library.static() || library.shared() || library.rlib() || library.stubs() { |
Colin Cross | 4021302 | 2023-12-13 15:19:49 -0800 | [diff] [blame] | 740 | android.SetProvider(ctx, cc.FlagExporterInfoProvider, cc.FlagExporterInfo{ |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 741 | IncludeDirs: android.FirstUniquePaths(library.includeDirs), |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 742 | }) |
| 743 | } |
| 744 | |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 745 | if library.shared() || library.stubs() { |
Ivan Lozano | 7b0781d | 2021-11-03 15:30:18 -0400 | [diff] [blame] | 746 | // Optimize out relinking against shared libraries whose interface hasn't changed by |
| 747 | // depending on a table of contents file instead of the library itself. |
| 748 | tocFile := outputFile.ReplaceExtension(ctx, flags.Toolchain.SharedLibSuffix()[1:]+".toc") |
| 749 | library.tocFile = android.OptionalPathForPath(tocFile) |
| 750 | cc.TransformSharedObjectToToc(ctx, outputFile, tocFile) |
| 751 | |
Colin Cross | 4021302 | 2023-12-13 15:19:49 -0800 | [diff] [blame] | 752 | android.SetProvider(ctx, cc.SharedLibraryInfoProvider, cc.SharedLibraryInfo{ |
Ivan Lozano | 7b0781d | 2021-11-03 15:30:18 -0400 | [diff] [blame] | 753 | TableOfContents: android.OptionalPathForPath(tocFile), |
| 754 | SharedLibrary: outputFile, |
| 755 | Target: ctx.Target(), |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 756 | IsStubs: library.BuildStubs(), |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 757 | }) |
| 758 | } |
| 759 | |
| 760 | if library.static() { |
Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 761 | depSet := depset.NewBuilder[android.Path](depset.TOPOLOGICAL).Direct(outputFile).Build() |
Colin Cross | 4021302 | 2023-12-13 15:19:49 -0800 | [diff] [blame] | 762 | android.SetProvider(ctx, cc.StaticLibraryInfoProvider, cc.StaticLibraryInfo{ |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 763 | StaticLibrary: outputFile, |
| 764 | |
| 765 | TransitiveStaticLibrariesForOrdering: depSet, |
| 766 | }) |
| 767 | } |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 768 | cc.AddStubDependencyProviders(ctx) |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 769 | |
Ivan Lozano | 1f10f68 | 2024-11-08 16:16:50 +0000 | [diff] [blame] | 770 | // Set our flagexporter provider to export relevant Rust flags |
Ivan Lozano | 7fe65ca | 2025-02-05 02:22:33 +0000 | [diff] [blame^] | 771 | library.flagExporter.setRustProvider(ctx) |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 772 | |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 773 | return ret |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Matthew Maurer | a28404a | 2023-11-20 23:33:28 +0000 | [diff] [blame] | 776 | func (library *libraryDecorator) checkedCrateRootPath() (android.Path, error) { |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 777 | if library.sourceProvider != nil { |
Matthew Maurer | a28404a | 2023-11-20 23:33:28 +0000 | [diff] [blame] | 778 | srcs := library.sourceProvider.Srcs() |
| 779 | if len(srcs) == 0 { |
| 780 | return nil, errors.New("Source provider generated 0 sources") |
| 781 | } |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 782 | // Assume the first source from the source provider is the library entry point. |
Matthew Maurer | a28404a | 2023-11-20 23:33:28 +0000 | [diff] [blame] | 783 | return srcs[0], nil |
Sam Delmerico | 63ca14e | 2023-09-25 12:13:17 +0000 | [diff] [blame] | 784 | } else { |
Matthew Maurer | a28404a | 2023-11-20 23:33:28 +0000 | [diff] [blame] | 785 | return library.baseCompiler.checkedCrateRootPath() |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 786 | } |
| 787 | } |
| 788 | |
Ivan Lozano | a8a1fa1 | 2024-10-30 18:15:59 +0000 | [diff] [blame] | 789 | func (library *libraryDecorator) getApiStubsCcFlags(ctx ModuleContext) cc.Flags { |
| 790 | ccFlags := cc.Flags{} |
| 791 | toolchain := cc_config.FindToolchain(ctx.Os(), ctx.Arch()) |
| 792 | |
| 793 | platformSdkVersion := "" |
| 794 | if ctx.Device() { |
| 795 | platformSdkVersion = ctx.Config().PlatformSdkVersion().String() |
| 796 | } |
| 797 | minSdkVersion := cc.MinSdkVersion(ctx.RustModule(), cc.CtxIsForPlatform(ctx), ctx.Device(), platformSdkVersion) |
| 798 | |
| 799 | // Collect common CC compilation flags |
| 800 | ccFlags = cc.CommonLinkerFlags(ctx, ccFlags, true, toolchain, false) |
| 801 | ccFlags = cc.CommonLibraryLinkerFlags(ctx, ccFlags, toolchain, library.getStem(ctx)) |
| 802 | ccFlags = cc.AddStubLibraryCompilerFlags(ccFlags) |
| 803 | ccFlags = cc.AddTargetFlags(ctx, ccFlags, toolchain, minSdkVersion, false) |
| 804 | |
| 805 | return ccFlags |
| 806 | } |
| 807 | |
| 808 | func (library *libraryDecorator) compileModuleLibApiStubs(ctx ModuleContext, ccFlags cc.Flags) cc.Objects { |
| 809 | mod := ctx.RustModule() |
| 810 | |
| 811 | symbolFile := String(library.Properties.Stubs.Symbol_file) |
| 812 | library.stubsSymbolFilePath = android.PathForModuleSrc(ctx, symbolFile) |
| 813 | |
| 814 | apiParams := cc.ApiStubsParams{ |
| 815 | NotInPlatform: mod.NotInPlatform(), |
| 816 | IsNdk: mod.IsNdk(ctx.Config()), |
| 817 | BaseModuleName: mod.BaseModuleName(), |
| 818 | ModuleName: ctx.ModuleName(), |
| 819 | } |
| 820 | flag := cc.GetApiStubsFlags(apiParams) |
| 821 | |
| 822 | nativeAbiResult := cc.ParseNativeAbiDefinition(ctx, symbolFile, |
| 823 | android.ApiLevelOrPanic(ctx, library.MutatedProperties.StubsVersion), flag) |
| 824 | objs := cc.CompileStubLibrary(ctx, ccFlags, nativeAbiResult.StubSrc, mod.getSharedFlags()) |
| 825 | |
| 826 | library.versionScriptPath = android.OptionalPathForPath(nativeAbiResult.VersionScript) |
| 827 | |
| 828 | // Parse symbol file to get API list for coverage |
| 829 | if library.StubsVersion() == "current" && ctx.PrimaryArch() && !mod.InRecovery() && !mod.InProduct() && !mod.InVendor() { |
| 830 | library.apiListCoverageXmlPath = cc.ParseSymbolFileForAPICoverage(ctx, symbolFile) |
| 831 | } |
| 832 | |
| 833 | return objs |
| 834 | } |
| 835 | |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 836 | func (library *libraryDecorator) rustdoc(ctx ModuleContext, flags Flags, |
| 837 | deps PathDeps) android.OptionalPath { |
| 838 | // rustdoc has builtin support for documenting config specific information |
| 839 | // regardless of the actual config it was given |
| 840 | // (https://doc.rust-lang.org/rustdoc/advanced-features.html#cfgdoc-documenting-platform-specific-or-feature-specific-information), |
| 841 | // so we generate the rustdoc for only the primary module so that we have a |
| 842 | // single set of docs to refer to. |
| 843 | if ctx.Module() != ctx.PrimaryModule() { |
| 844 | return android.OptionalPath{} |
| 845 | } |
| 846 | |
Matthew Maurer | a28404a | 2023-11-20 23:33:28 +0000 | [diff] [blame] | 847 | return android.OptionalPathForPath(Rustdoc(ctx, crateRootPath(ctx, library), |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 848 | deps, flags)) |
| 849 | } |
| 850 | |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 851 | func (library *libraryDecorator) getStem(ctx ModuleContext) string { |
| 852 | stem := library.baseCompiler.getStemWithoutSuffix(ctx) |
| 853 | validateLibraryStem(ctx, stem, library.crateName()) |
| 854 | |
| 855 | return stem + String(library.baseCompiler.Properties.Suffix) |
| 856 | } |
| 857 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 858 | func (library *libraryDecorator) install(ctx ModuleContext) { |
| 859 | // Only shared and dylib variants make sense to install. |
| 860 | if library.shared() || library.dylib() { |
| 861 | library.baseCompiler.install(ctx) |
| 862 | } |
| 863 | } |
| 864 | |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 865 | func (library *libraryDecorator) Disabled() bool { |
| 866 | return library.MutatedProperties.VariantIsDisabled |
| 867 | } |
| 868 | |
| 869 | func (library *libraryDecorator) SetDisabled() { |
| 870 | library.MutatedProperties.VariantIsDisabled = true |
| 871 | } |
| 872 | |
Cole Faust | 58eef4f | 2025-01-29 15:14:17 -0800 | [diff] [blame] | 873 | func (library *libraryDecorator) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) { |
| 874 | library.baseCompiler.moduleInfoJSON(ctx, moduleInfoJSON) |
| 875 | |
| 876 | if library.rlib() { |
| 877 | moduleInfoJSON.Class = []string{"RLIB_LIBRARIES"} |
| 878 | } else if library.dylib() { |
| 879 | moduleInfoJSON.Class = []string{"DYLIB_LIBRARIES"} |
| 880 | } else if library.static() { |
| 881 | moduleInfoJSON.Class = []string{"STATIC_LIBRARIES"} |
| 882 | } else if library.shared() { |
| 883 | moduleInfoJSON.Class = []string{"SHARED_LIBRARIES"} |
| 884 | } |
| 885 | } |
| 886 | |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 887 | var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+") |
| 888 | |
| 889 | func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) { |
| 890 | if crate_name == "" { |
| 891 | ctx.PropertyErrorf("crate_name", "crate_name must be defined.") |
| 892 | } |
| 893 | |
| 894 | // crate_names are used for the library output file, and rustc expects these |
| 895 | // to be alphanumeric with underscores allowed. |
| 896 | if validCrateName.MatchString(crate_name) { |
| 897 | ctx.PropertyErrorf("crate_name", |
| 898 | "library crate_names must be alphanumeric with underscores allowed") |
| 899 | } |
| 900 | |
| 901 | // Libraries are expected to begin with "lib" followed by the crate_name |
| 902 | if !strings.HasPrefix(filename, "lib"+crate_name) { |
| 903 | ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>") |
| 904 | } |
| 905 | } |
| 906 | |
Colin Cross | 8a49a3d | 2024-05-20 12:22:27 -0700 | [diff] [blame] | 907 | type libraryTransitionMutator struct{} |
| 908 | |
| 909 | func (libraryTransitionMutator) Split(ctx android.BaseModuleContext) []string { |
| 910 | m, ok := ctx.Module().(*Module) |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 911 | if !ok || m.compiler == nil { |
Colin Cross | 8a49a3d | 2024-05-20 12:22:27 -0700 | [diff] [blame] | 912 | return []string{""} |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 913 | } |
| 914 | library, ok := m.compiler.(libraryInterface) |
| 915 | if !ok { |
Colin Cross | 8a49a3d | 2024-05-20 12:22:27 -0700 | [diff] [blame] | 916 | return []string{""} |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 917 | } |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 918 | |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 919 | // Don't produce rlib/dylib/source variants for shared or static variants |
| 920 | if library.shared() || library.static() { |
Colin Cross | 8a49a3d | 2024-05-20 12:22:27 -0700 | [diff] [blame] | 921 | return []string{""} |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 922 | } |
| 923 | |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 924 | var variants []string |
| 925 | // The source variant is used for SourceProvider modules. The other variants (i.e. rlib and dylib) |
| 926 | // depend on this variant. It must be the first variant to be declared. |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 927 | if m.sourceProvider != nil { |
Colin Cross | 8a49a3d | 2024-05-20 12:22:27 -0700 | [diff] [blame] | 928 | variants = append(variants, sourceVariation) |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 929 | } |
| 930 | if library.buildRlib() { |
| 931 | variants = append(variants, rlibVariation) |
| 932 | } |
| 933 | if library.buildDylib() { |
| 934 | variants = append(variants, dylibVariation) |
| 935 | } |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 936 | |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 937 | if len(variants) == 0 { |
Colin Cross | 8a49a3d | 2024-05-20 12:22:27 -0700 | [diff] [blame] | 938 | return []string{""} |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 939 | } |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 940 | |
Colin Cross | 8a49a3d | 2024-05-20 12:22:27 -0700 | [diff] [blame] | 941 | return variants |
| 942 | } |
Ivan Lozano | 1921e80 | 2021-05-20 13:39:16 -0400 | [diff] [blame] | 943 | |
Colin Cross | 8a49a3d | 2024-05-20 12:22:27 -0700 | [diff] [blame] | 944 | func (libraryTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string { |
| 945 | return "" |
| 946 | } |
| 947 | |
| 948 | func (libraryTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string { |
| 949 | m, ok := ctx.Module().(*Module) |
| 950 | if !ok || m.compiler == nil { |
| 951 | return "" |
| 952 | } |
| 953 | library, ok := m.compiler.(libraryInterface) |
| 954 | if !ok { |
| 955 | return "" |
| 956 | } |
| 957 | |
| 958 | if incomingVariation == "" { |
| 959 | if m.sourceProvider != nil { |
| 960 | return sourceVariation |
| 961 | } |
| 962 | if library.shared() { |
| 963 | return "" |
| 964 | } |
| 965 | if library.buildRlib() { |
| 966 | return rlibVariation |
| 967 | } |
| 968 | if library.buildDylib() { |
| 969 | return dylibVariation |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 970 | } |
| 971 | } |
Colin Cross | 8a49a3d | 2024-05-20 12:22:27 -0700 | [diff] [blame] | 972 | return incomingVariation |
| 973 | } |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 974 | |
Colin Cross | 8a49a3d | 2024-05-20 12:22:27 -0700 | [diff] [blame] | 975 | func (libraryTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) { |
| 976 | m, ok := ctx.Module().(*Module) |
| 977 | if !ok || m.compiler == nil { |
| 978 | return |
| 979 | } |
| 980 | library, ok := m.compiler.(libraryInterface) |
| 981 | if !ok { |
| 982 | return |
| 983 | } |
| 984 | |
| 985 | switch variation { |
| 986 | case rlibVariation: |
| 987 | library.setRlib() |
| 988 | case dylibVariation: |
| 989 | library.setDylib() |
| 990 | if m.ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation { |
| 991 | // TODO(b/165791368) |
| 992 | // Disable dylib Vendor Ramdisk variations until we support these. |
| 993 | m.Disable() |
| 994 | } |
| 995 | |
| 996 | case sourceVariation: |
| 997 | library.setSource() |
| 998 | // The source variant does not produce any library. |
| 999 | // Disable the compilation steps. |
| 1000 | m.compiler.SetDisabled() |
Ivan Lozano | fd47b1a | 2024-05-17 14:13:41 -0400 | [diff] [blame] | 1001 | } |
| 1002 | |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 1003 | // If a source variant is created, add an inter-variant dependency |
| 1004 | // between the other variants and the source variant. |
Colin Cross | 8a49a3d | 2024-05-20 12:22:27 -0700 | [diff] [blame] | 1005 | if m.sourceProvider != nil && variation != sourceVariation { |
| 1006 | ctx.AddVariationDependencies( |
| 1007 | []blueprint.Variation{ |
| 1008 | {"rust_libraries", sourceVariation}, |
| 1009 | }, |
| 1010 | sourceDepTag, ctx.ModuleName()) |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 1011 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 1012 | } |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 1013 | |
Colin Cross | 8a49a3d | 2024-05-20 12:22:27 -0700 | [diff] [blame] | 1014 | type libstdTransitionMutator struct{} |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 1015 | |
Colin Cross | 8a49a3d | 2024-05-20 12:22:27 -0700 | [diff] [blame] | 1016 | func (libstdTransitionMutator) Split(ctx android.BaseModuleContext) []string { |
| 1017 | if m, ok := ctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() { |
| 1018 | // Only create a variant if a library is actually being built. |
| 1019 | if library, ok := m.compiler.(libraryInterface); ok { |
| 1020 | if library.rlib() && !library.sysroot() { |
Ivan Lozano | 806efd3 | 2024-12-11 21:38:53 +0000 | [diff] [blame] | 1021 | return []string{"rlib-std", "dylib-std"} |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 1022 | } |
| 1023 | } |
| 1024 | } |
Colin Cross | 8a49a3d | 2024-05-20 12:22:27 -0700 | [diff] [blame] | 1025 | return []string{""} |
| 1026 | } |
| 1027 | |
| 1028 | func (libstdTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string { |
| 1029 | return "" |
| 1030 | } |
| 1031 | |
| 1032 | func (libstdTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string { |
| 1033 | if m, ok := ctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() { |
| 1034 | if library, ok := m.compiler.(libraryInterface); ok { |
| 1035 | if library.shared() { |
| 1036 | return "" |
| 1037 | } |
| 1038 | if library.rlib() && !library.sysroot() { |
| 1039 | if incomingVariation != "" { |
| 1040 | return incomingVariation |
| 1041 | } |
| 1042 | return "rlib-std" |
| 1043 | } |
| 1044 | } |
| 1045 | } |
| 1046 | return "" |
| 1047 | } |
| 1048 | |
| 1049 | func (libstdTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) { |
| 1050 | if variation == "rlib-std" { |
| 1051 | rlib := ctx.Module().(*Module) |
| 1052 | rlib.compiler.(libraryInterface).setRlibStd() |
| 1053 | rlib.Properties.RustSubName += RlibStdlibSuffix |
| 1054 | } else if variation == "dylib-std" { |
| 1055 | dylib := ctx.Module().(*Module) |
| 1056 | dylib.compiler.(libraryInterface).setDylibStd() |
| 1057 | if dylib.ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation { |
| 1058 | // TODO(b/165791368) |
| 1059 | // Disable rlibs that link against dylib-std on vendor ramdisk variations until those dylib |
| 1060 | // variants are properly supported. |
| 1061 | dylib.Disable() |
| 1062 | } |
| 1063 | } |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 1064 | } |