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 ( |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 18 | "fmt" |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 19 | "regexp" |
| 20 | "strings" |
| 21 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 22 | "android/soong/android" |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 23 | "android/soong/cc" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 26 | var ( |
| 27 | DylibStdlibSuffix = ".dylib-std" |
| 28 | RlibStdlibSuffix = ".rlib-std" |
| 29 | ) |
| 30 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 31 | func init() { |
| 32 | android.RegisterModuleType("rust_library", RustLibraryFactory) |
| 33 | android.RegisterModuleType("rust_library_dylib", RustLibraryDylibFactory) |
| 34 | android.RegisterModuleType("rust_library_rlib", RustLibraryRlibFactory) |
| 35 | android.RegisterModuleType("rust_library_host", RustLibraryHostFactory) |
| 36 | android.RegisterModuleType("rust_library_host_dylib", RustLibraryDylibHostFactory) |
| 37 | android.RegisterModuleType("rust_library_host_rlib", RustLibraryRlibHostFactory) |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 38 | android.RegisterModuleType("rust_ffi", RustFFIFactory) |
| 39 | android.RegisterModuleType("rust_ffi_shared", RustFFISharedFactory) |
| 40 | android.RegisterModuleType("rust_ffi_static", RustFFIStaticFactory) |
| 41 | android.RegisterModuleType("rust_ffi_host", RustFFIHostFactory) |
| 42 | android.RegisterModuleType("rust_ffi_host_shared", RustFFISharedHostFactory) |
| 43 | android.RegisterModuleType("rust_ffi_host_static", RustFFIStaticHostFactory) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | type VariantLibraryProperties struct { |
Matthew Maurer | c761eec | 2020-06-25 00:47:46 -0700 | [diff] [blame] | 47 | Enabled *bool `android:"arch_variant"` |
| 48 | Srcs []string `android:"path,arch_variant"` |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | type LibraryCompilerProperties struct { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 52 | Rlib VariantLibraryProperties `android:"arch_variant"` |
| 53 | Dylib VariantLibraryProperties `android:"arch_variant"` |
| 54 | Shared VariantLibraryProperties `android:"arch_variant"` |
| 55 | Static VariantLibraryProperties `android:"arch_variant"` |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 56 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 57 | // path to include directories to pass to cc_* modules, only relevant for static/shared variants. |
| 58 | Include_dirs []string `android:"path,arch_variant"` |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 59 | |
| 60 | // Whether this library is part of the Rust toolchain sysroot. |
| 61 | Sysroot *bool |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | type LibraryMutatedProperties struct { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 65 | // Build a dylib variant |
| 66 | BuildDylib bool `blueprint:"mutated"` |
| 67 | // Build an rlib variant |
| 68 | BuildRlib bool `blueprint:"mutated"` |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 69 | // Build a shared library variant |
| 70 | BuildShared bool `blueprint:"mutated"` |
| 71 | // Build a static library variant |
| 72 | BuildStatic bool `blueprint:"mutated"` |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 73 | |
| 74 | // This variant is a dylib |
| 75 | VariantIsDylib bool `blueprint:"mutated"` |
| 76 | // This variant is an rlib |
| 77 | VariantIsRlib bool `blueprint:"mutated"` |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 78 | // This variant is a shared library |
| 79 | VariantIsShared bool `blueprint:"mutated"` |
| 80 | // This variant is a static library |
| 81 | VariantIsStatic bool `blueprint:"mutated"` |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 82 | // This variant is a source provider |
| 83 | VariantIsSource bool `blueprint:"mutated"` |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 84 | |
| 85 | // This variant is disabled and should not be compiled |
| 86 | // (used for SourceProvider variants that produce only source) |
| 87 | VariantIsDisabled bool `blueprint:"mutated"` |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 88 | |
| 89 | // Whether this library variant should be link libstd via rlibs |
| 90 | VariantIsStaticStd bool `blueprint:"mutated"` |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | type libraryDecorator struct { |
| 94 | *baseCompiler |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 95 | *flagExporter |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 96 | stripper Stripper |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 97 | |
Ivan Lozano | 8a23fa4 | 2020-06-16 10:26:57 -0400 | [diff] [blame] | 98 | Properties LibraryCompilerProperties |
| 99 | MutatedProperties LibraryMutatedProperties |
| 100 | includeDirs android.Paths |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 101 | sourceProvider SourceProvider |
Ivan Lozano | 1921e80 | 2021-05-20 13:39:16 -0400 | [diff] [blame] | 102 | |
| 103 | collectedSnapshotHeaders android.Paths |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | type libraryInterface interface { |
| 107 | rlib() bool |
| 108 | dylib() bool |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 109 | static() bool |
| 110 | shared() bool |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 111 | sysroot() bool |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 112 | source() bool |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 113 | |
| 114 | // Returns true if the build options for the module have selected a particular build type |
| 115 | buildRlib() bool |
| 116 | buildDylib() bool |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 117 | buildShared() bool |
| 118 | buildStatic() bool |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 119 | |
| 120 | // Sets a particular variant type |
| 121 | setRlib() |
| 122 | setDylib() |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 123 | setShared() |
| 124 | setStatic() |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 125 | setSource() |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 126 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 127 | // Set libstd linkage |
| 128 | setRlibStd() |
| 129 | setDylibStd() |
| 130 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 131 | // Build a specific library variant |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 132 | BuildOnlyFFI() |
| 133 | BuildOnlyRust() |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 134 | BuildOnlyRlib() |
| 135 | BuildOnlyDylib() |
| 136 | BuildOnlyStatic() |
| 137 | BuildOnlyShared() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 140 | func (library *libraryDecorator) nativeCoverage() bool { |
| 141 | return true |
| 142 | } |
| 143 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 144 | func (library *libraryDecorator) rlib() bool { |
| 145 | return library.MutatedProperties.VariantIsRlib |
| 146 | } |
| 147 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 148 | func (library *libraryDecorator) sysroot() bool { |
| 149 | return Bool(library.Properties.Sysroot) |
| 150 | } |
| 151 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 152 | func (library *libraryDecorator) dylib() bool { |
| 153 | return library.MutatedProperties.VariantIsDylib |
| 154 | } |
| 155 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 156 | func (library *libraryDecorator) shared() bool { |
| 157 | return library.MutatedProperties.VariantIsShared |
| 158 | } |
| 159 | |
| 160 | func (library *libraryDecorator) static() bool { |
| 161 | return library.MutatedProperties.VariantIsStatic |
| 162 | } |
| 163 | |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 164 | func (library *libraryDecorator) source() bool { |
| 165 | return library.MutatedProperties.VariantIsSource |
| 166 | } |
| 167 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 168 | func (library *libraryDecorator) buildRlib() bool { |
| 169 | return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true) |
| 170 | } |
| 171 | |
| 172 | func (library *libraryDecorator) buildDylib() bool { |
| 173 | return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true) |
| 174 | } |
| 175 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 176 | func (library *libraryDecorator) buildShared() bool { |
| 177 | return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true) |
| 178 | } |
| 179 | |
| 180 | func (library *libraryDecorator) buildStatic() bool { |
| 181 | return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true) |
| 182 | } |
| 183 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 184 | func (library *libraryDecorator) setRlib() { |
| 185 | library.MutatedProperties.VariantIsRlib = true |
| 186 | library.MutatedProperties.VariantIsDylib = false |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 187 | library.MutatedProperties.VariantIsStatic = false |
| 188 | library.MutatedProperties.VariantIsShared = false |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | func (library *libraryDecorator) setDylib() { |
| 192 | library.MutatedProperties.VariantIsRlib = false |
| 193 | library.MutatedProperties.VariantIsDylib = true |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 194 | library.MutatedProperties.VariantIsStatic = false |
| 195 | library.MutatedProperties.VariantIsShared = false |
| 196 | } |
| 197 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 198 | func (library *libraryDecorator) setRlibStd() { |
| 199 | library.MutatedProperties.VariantIsStaticStd = true |
| 200 | } |
| 201 | |
| 202 | func (library *libraryDecorator) setDylibStd() { |
| 203 | library.MutatedProperties.VariantIsStaticStd = false |
| 204 | } |
| 205 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 206 | func (library *libraryDecorator) setShared() { |
| 207 | library.MutatedProperties.VariantIsStatic = false |
| 208 | library.MutatedProperties.VariantIsShared = true |
| 209 | library.MutatedProperties.VariantIsRlib = false |
| 210 | library.MutatedProperties.VariantIsDylib = false |
| 211 | } |
| 212 | |
| 213 | func (library *libraryDecorator) setStatic() { |
| 214 | library.MutatedProperties.VariantIsStatic = true |
| 215 | library.MutatedProperties.VariantIsShared = false |
| 216 | library.MutatedProperties.VariantIsRlib = false |
| 217 | library.MutatedProperties.VariantIsDylib = false |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 220 | func (library *libraryDecorator) setSource() { |
| 221 | library.MutatedProperties.VariantIsSource = true |
| 222 | } |
| 223 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 224 | func (library *libraryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep { |
Ivan Lozano | 1921e80 | 2021-05-20 13:39:16 -0400 | [diff] [blame] | 225 | if ctx.Module().(*Module).InVendor() { |
| 226 | // Vendor modules should statically link libstd. |
| 227 | return rlibAutoDep |
| 228 | } else if library.preferRlib() { |
Ivan Lozano | ea08613 | 2020-12-08 14:43:00 -0500 | [diff] [blame] | 229 | return rlibAutoDep |
| 230 | } else if library.rlib() || library.static() { |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 231 | return rlibAutoDep |
| 232 | } else if library.dylib() || library.shared() { |
| 233 | return dylibAutoDep |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 234 | } else if ctx.BazelConversionMode() { |
| 235 | // In Bazel conversion mode, we are currently ignoring the deptag, so we just need to supply a |
| 236 | // compatible tag in order to add the dependency. |
| 237 | return rlibAutoDep |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 238 | } else { |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 239 | 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] | 240 | } |
| 241 | } |
| 242 | |
Ivan Lozano | ea08613 | 2020-12-08 14:43:00 -0500 | [diff] [blame] | 243 | func (library *libraryDecorator) stdLinkage(ctx *depsContext) RustLinkage { |
Ivan Lozano | 1921e80 | 2021-05-20 13:39:16 -0400 | [diff] [blame] | 244 | if ctx.RustModule().InVendor() { |
| 245 | // Vendor modules should statically link libstd. |
| 246 | return RlibLinkage |
| 247 | } else if library.static() || library.MutatedProperties.VariantIsStaticStd { |
Ivan Lozano | ea08613 | 2020-12-08 14:43:00 -0500 | [diff] [blame] | 248 | return RlibLinkage |
| 249 | } else if library.baseCompiler.preferRlib() { |
| 250 | return RlibLinkage |
| 251 | } |
| 252 | return DefaultLinkage |
| 253 | } |
| 254 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 255 | var _ compiler = (*libraryDecorator)(nil) |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 256 | var _ libraryInterface = (*libraryDecorator)(nil) |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 257 | var _ exportedFlagsProducer = (*libraryDecorator)(nil) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 258 | |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 259 | // rust_library produces all rust variants. |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 260 | func RustLibraryFactory() android.Module { |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 261 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 262 | library.BuildOnlyRust() |
| 263 | return module.Init() |
| 264 | } |
| 265 | |
| 266 | // rust_ffi produces all ffi variants. |
| 267 | func RustFFIFactory() android.Module { |
| 268 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 269 | library.BuildOnlyFFI() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 270 | return module.Init() |
| 271 | } |
| 272 | |
| 273 | // rust_library_dylib produces a dylib. |
| 274 | func RustLibraryDylibFactory() android.Module { |
| 275 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 276 | library.BuildOnlyDylib() |
| 277 | return module.Init() |
| 278 | } |
| 279 | |
| 280 | // rust_library_rlib produces an rlib. |
| 281 | func RustLibraryRlibFactory() android.Module { |
| 282 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 283 | library.BuildOnlyRlib() |
| 284 | return module.Init() |
| 285 | } |
| 286 | |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 287 | // rust_ffi_shared produces a shared library. |
| 288 | func RustFFISharedFactory() android.Module { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 289 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 290 | library.BuildOnlyShared() |
| 291 | return module.Init() |
| 292 | } |
| 293 | |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 294 | // rust_ffi_static produces a static library. |
| 295 | func RustFFIStaticFactory() android.Module { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 296 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 297 | library.BuildOnlyStatic() |
| 298 | return module.Init() |
| 299 | } |
| 300 | |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 301 | // rust_library_host produces all rust variants. |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 302 | func RustLibraryHostFactory() android.Module { |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 303 | module, library := NewRustLibrary(android.HostSupported) |
| 304 | library.BuildOnlyRust() |
| 305 | return module.Init() |
| 306 | } |
| 307 | |
| 308 | // rust_ffi_host produces all FFI variants. |
| 309 | func RustFFIHostFactory() android.Module { |
| 310 | module, library := NewRustLibrary(android.HostSupported) |
| 311 | library.BuildOnlyFFI() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 312 | return module.Init() |
| 313 | } |
| 314 | |
| 315 | // rust_library_dylib_host produces a dylib. |
| 316 | func RustLibraryDylibHostFactory() android.Module { |
| 317 | module, library := NewRustLibrary(android.HostSupported) |
| 318 | library.BuildOnlyDylib() |
| 319 | return module.Init() |
| 320 | } |
| 321 | |
| 322 | // rust_library_rlib_host produces an rlib. |
| 323 | func RustLibraryRlibHostFactory() android.Module { |
| 324 | module, library := NewRustLibrary(android.HostSupported) |
| 325 | library.BuildOnlyRlib() |
| 326 | return module.Init() |
| 327 | } |
| 328 | |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 329 | // rust_ffi_static_host produces a static library. |
| 330 | func RustFFIStaticHostFactory() android.Module { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 331 | module, library := NewRustLibrary(android.HostSupported) |
| 332 | library.BuildOnlyStatic() |
| 333 | return module.Init() |
| 334 | } |
| 335 | |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 336 | // rust_ffi_shared_host produces an shared library. |
| 337 | func RustFFISharedHostFactory() android.Module { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 338 | module, library := NewRustLibrary(android.HostSupported) |
| 339 | library.BuildOnlyShared() |
| 340 | return module.Init() |
| 341 | } |
| 342 | |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 343 | func (library *libraryDecorator) BuildOnlyFFI() { |
| 344 | library.MutatedProperties.BuildDylib = false |
| 345 | library.MutatedProperties.BuildRlib = false |
| 346 | library.MutatedProperties.BuildShared = true |
| 347 | library.MutatedProperties.BuildStatic = true |
| 348 | } |
| 349 | |
| 350 | func (library *libraryDecorator) BuildOnlyRust() { |
| 351 | library.MutatedProperties.BuildDylib = true |
| 352 | library.MutatedProperties.BuildRlib = true |
| 353 | library.MutatedProperties.BuildShared = false |
| 354 | library.MutatedProperties.BuildStatic = false |
| 355 | } |
| 356 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 357 | func (library *libraryDecorator) BuildOnlyDylib() { |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 358 | library.MutatedProperties.BuildDylib = true |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 359 | library.MutatedProperties.BuildRlib = false |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 360 | library.MutatedProperties.BuildShared = false |
| 361 | library.MutatedProperties.BuildStatic = false |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | func (library *libraryDecorator) BuildOnlyRlib() { |
| 365 | library.MutatedProperties.BuildDylib = false |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 366 | library.MutatedProperties.BuildRlib = true |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 367 | library.MutatedProperties.BuildShared = false |
| 368 | library.MutatedProperties.BuildStatic = false |
| 369 | } |
| 370 | |
| 371 | func (library *libraryDecorator) BuildOnlyStatic() { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 372 | library.MutatedProperties.BuildRlib = false |
| 373 | library.MutatedProperties.BuildDylib = false |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 374 | library.MutatedProperties.BuildShared = false |
| 375 | library.MutatedProperties.BuildStatic = true |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | func (library *libraryDecorator) BuildOnlyShared() { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 379 | library.MutatedProperties.BuildRlib = false |
| 380 | library.MutatedProperties.BuildDylib = false |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 381 | library.MutatedProperties.BuildStatic = false |
| 382 | library.MutatedProperties.BuildShared = true |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) { |
Ivan Lozano | 9d1df10 | 2020-04-28 10:10:23 -0400 | [diff] [blame] | 386 | module := newModule(hod, android.MultilibBoth) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 387 | |
| 388 | library := &libraryDecorator{ |
| 389 | MutatedProperties: LibraryMutatedProperties{ |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 390 | BuildDylib: false, |
| 391 | BuildRlib: false, |
| 392 | BuildShared: false, |
| 393 | BuildStatic: false, |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 394 | }, |
Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 395 | baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem), |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 396 | flagExporter: NewFlagExporter(), |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | module.compiler = library |
| 400 | |
| 401 | return module, library |
| 402 | } |
| 403 | |
| 404 | func (library *libraryDecorator) compilerProps() []interface{} { |
| 405 | return append(library.baseCompiler.compilerProps(), |
| 406 | &library.Properties, |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 407 | &library.MutatedProperties, |
| 408 | &library.stripper.StripProperties) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 411 | func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps { |
| 412 | deps = library.baseCompiler.compilerDeps(ctx, deps) |
| 413 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 414 | if ctx.toolchain().Bionic() && (library.dylib() || library.shared()) { |
Thiébaud Weksteen | f1ff54a | 2021-03-22 14:24:54 +0100 | [diff] [blame] | 415 | deps = bionicDeps(ctx, deps, false) |
Ivan Lozano | 12ee9ca | 2020-04-07 13:19:44 -0400 | [diff] [blame] | 416 | deps.CrtBegin = "crtbegin_so" |
| 417 | deps.CrtEnd = "crtend_so" |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | return deps |
| 421 | } |
Ivan Lozano | bec05ea | 2020-06-09 08:27:49 -0400 | [diff] [blame] | 422 | |
| 423 | func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string { |
| 424 | return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix() |
| 425 | } |
| 426 | |
Ivan Lozano | e0833b1 | 2019-11-06 19:15:49 -0800 | [diff] [blame] | 427 | func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags { |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 428 | flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName()) |
Ivan Lozano | e0833b1 | 2019-11-06 19:15:49 -0800 | [diff] [blame] | 429 | flags = library.baseCompiler.compilerFlags(ctx, flags) |
| 430 | if library.shared() || library.static() { |
| 431 | library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...) |
| 432 | } |
Ivan Lozano | bec05ea | 2020-06-09 08:27:49 -0400 | [diff] [blame] | 433 | if library.shared() { |
| 434 | flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx)) |
| 435 | } |
| 436 | |
Ivan Lozano | e0833b1 | 2019-11-06 19:15:49 -0800 | [diff] [blame] | 437 | return flags |
| 438 | } |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 439 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 440 | func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path { |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 441 | var outputFile android.ModuleOutPath |
| 442 | var fileName string |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 443 | srcPath := library.srcPath(ctx, deps) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 444 | |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 445 | if library.sourceProvider != nil { |
Ivan Lozano | 9d74a52 | 2020-12-01 09:25:22 -0500 | [diff] [blame] | 446 | deps.srcProviderFiles = append(deps.srcProviderFiles, library.sourceProvider.Srcs()...) |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 447 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 448 | |
| 449 | flags.RustFlags = append(flags.RustFlags, deps.depFlags...) |
Ivan Lozano | 3dfa12d | 2021-02-04 11:29:41 -0500 | [diff] [blame] | 450 | flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...) |
Ivan Lozano | 2093af2 | 2020-08-25 12:48:19 -0400 | [diff] [blame] | 451 | flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 452 | |
Matthew Maurer | 46c46cc | 2020-01-13 16:34:34 -0800 | [diff] [blame] | 453 | if library.dylib() { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 454 | // We need prefer-dynamic for now to avoid linking in the static stdlib. See: |
| 455 | // https://github.com/rust-lang/rust/issues/19680 |
| 456 | // https://github.com/rust-lang/rust/issues/34909 |
| 457 | flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic") |
| 458 | } |
| 459 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 460 | if library.rlib() { |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 461 | fileName = library.getStem(ctx) + ctx.toolchain().RlibSuffix() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 462 | outputFile = android.PathForModuleOut(ctx, fileName) |
| 463 | |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 464 | TransformSrctoRlib(ctx, srcPath, deps, flags, outputFile) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 465 | } else if library.dylib() { |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 466 | fileName = library.getStem(ctx) + ctx.toolchain().DylibSuffix() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 467 | outputFile = android.PathForModuleOut(ctx, fileName) |
| 468 | |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 469 | TransformSrctoDylib(ctx, srcPath, deps, flags, outputFile) |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 470 | } else if library.static() { |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 471 | fileName = library.getStem(ctx) + ctx.toolchain().StaticLibSuffix() |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 472 | outputFile = android.PathForModuleOut(ctx, fileName) |
| 473 | |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 474 | TransformSrctoStatic(ctx, srcPath, deps, flags, outputFile) |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 475 | } else if library.shared() { |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 476 | fileName = library.sharedLibFilename(ctx) |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 477 | outputFile = android.PathForModuleOut(ctx, fileName) |
| 478 | |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 479 | TransformSrctoShared(ctx, srcPath, deps, flags, outputFile) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Thiébaud Weksteen | 4fab05a | 2021-04-14 19:15:48 +0200 | [diff] [blame] | 482 | if !library.rlib() && !library.static() && library.stripper.NeedsStrip(ctx) { |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 483 | strippedOutputFile := android.PathForModuleOut(ctx, "stripped", fileName) |
| 484 | library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile) |
| 485 | library.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile) |
| 486 | } |
| 487 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 488 | if library.rlib() || library.dylib() { |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 489 | library.flagExporter.exportLinkDirs(deps.linkDirs...) |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 490 | library.flagExporter.exportLinkObjects(deps.linkObjects...) |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 491 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 492 | |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 493 | if library.static() || library.shared() { |
| 494 | ctx.SetProvider(cc.FlagExporterInfoProvider, cc.FlagExporterInfo{ |
| 495 | IncludeDirs: library.includeDirs, |
| 496 | }) |
| 497 | } |
| 498 | |
| 499 | if library.shared() { |
| 500 | ctx.SetProvider(cc.SharedLibraryInfoProvider, cc.SharedLibraryInfo{ |
| 501 | SharedLibrary: outputFile, |
| 502 | UnstrippedSharedLibrary: outputFile, |
Colin Cross | 2df8177 | 2021-01-26 11:00:18 -0800 | [diff] [blame] | 503 | Target: ctx.Target(), |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 504 | }) |
| 505 | } |
| 506 | |
| 507 | if library.static() { |
| 508 | depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(outputFile).Build() |
| 509 | ctx.SetProvider(cc.StaticLibraryInfoProvider, cc.StaticLibraryInfo{ |
| 510 | StaticLibrary: outputFile, |
| 511 | |
| 512 | TransitiveStaticLibrariesForOrdering: depSet, |
| 513 | }) |
| 514 | } |
| 515 | |
| 516 | library.flagExporter.setProvider(ctx) |
| 517 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 518 | return outputFile |
| 519 | } |
| 520 | |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 521 | func (library *libraryDecorator) srcPath(ctx ModuleContext, deps PathDeps) android.Path { |
| 522 | if library.sourceProvider != nil { |
| 523 | // Assume the first source from the source provider is the library entry point. |
| 524 | return library.sourceProvider.Srcs()[0] |
| 525 | } else { |
| 526 | path, _ := srcPathFromModuleSrcs(ctx, library.baseCompiler.Properties.Srcs) |
| 527 | return path |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | func (library *libraryDecorator) rustdoc(ctx ModuleContext, flags Flags, |
| 532 | deps PathDeps) android.OptionalPath { |
| 533 | // rustdoc has builtin support for documenting config specific information |
| 534 | // regardless of the actual config it was given |
| 535 | // (https://doc.rust-lang.org/rustdoc/advanced-features.html#cfgdoc-documenting-platform-specific-or-feature-specific-information), |
| 536 | // so we generate the rustdoc for only the primary module so that we have a |
| 537 | // single set of docs to refer to. |
| 538 | if ctx.Module() != ctx.PrimaryModule() { |
| 539 | return android.OptionalPath{} |
| 540 | } |
| 541 | |
| 542 | return android.OptionalPathForPath(Rustdoc(ctx, library.srcPath(ctx, deps), |
| 543 | deps, flags)) |
| 544 | } |
| 545 | |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 546 | func (library *libraryDecorator) getStem(ctx ModuleContext) string { |
| 547 | stem := library.baseCompiler.getStemWithoutSuffix(ctx) |
| 548 | validateLibraryStem(ctx, stem, library.crateName()) |
| 549 | |
| 550 | return stem + String(library.baseCompiler.Properties.Suffix) |
| 551 | } |
| 552 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 553 | func (library *libraryDecorator) install(ctx ModuleContext) { |
| 554 | // Only shared and dylib variants make sense to install. |
| 555 | if library.shared() || library.dylib() { |
| 556 | library.baseCompiler.install(ctx) |
| 557 | } |
| 558 | } |
| 559 | |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 560 | func (library *libraryDecorator) Disabled() bool { |
| 561 | return library.MutatedProperties.VariantIsDisabled |
| 562 | } |
| 563 | |
| 564 | func (library *libraryDecorator) SetDisabled() { |
| 565 | library.MutatedProperties.VariantIsDisabled = true |
| 566 | } |
| 567 | |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 568 | var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+") |
| 569 | |
| 570 | func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) { |
| 571 | if crate_name == "" { |
| 572 | ctx.PropertyErrorf("crate_name", "crate_name must be defined.") |
| 573 | } |
| 574 | |
| 575 | // crate_names are used for the library output file, and rustc expects these |
| 576 | // to be alphanumeric with underscores allowed. |
| 577 | if validCrateName.MatchString(crate_name) { |
| 578 | ctx.PropertyErrorf("crate_name", |
| 579 | "library crate_names must be alphanumeric with underscores allowed") |
| 580 | } |
| 581 | |
| 582 | // Libraries are expected to begin with "lib" followed by the crate_name |
| 583 | if !strings.HasPrefix(filename, "lib"+crate_name) { |
| 584 | ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>") |
| 585 | } |
| 586 | } |
| 587 | |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 588 | // LibraryMutator mutates the libraries into variants according to the |
| 589 | // build{Rlib,Dylib} attributes. |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 590 | func LibraryMutator(mctx android.BottomUpMutatorContext) { |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 591 | // Only mutate on Rust libraries. |
| 592 | m, ok := mctx.Module().(*Module) |
| 593 | if !ok || m.compiler == nil { |
| 594 | return |
| 595 | } |
| 596 | library, ok := m.compiler.(libraryInterface) |
| 597 | if !ok { |
| 598 | return |
| 599 | } |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 600 | |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 601 | var variants []string |
| 602 | // The source variant is used for SourceProvider modules. The other variants (i.e. rlib and dylib) |
| 603 | // depend on this variant. It must be the first variant to be declared. |
| 604 | sourceVariant := false |
| 605 | if m.sourceProvider != nil { |
| 606 | variants = append(variants, "source") |
| 607 | sourceVariant = true |
| 608 | } |
| 609 | if library.buildRlib() { |
| 610 | variants = append(variants, rlibVariation) |
| 611 | } |
| 612 | if library.buildDylib() { |
| 613 | variants = append(variants, dylibVariation) |
| 614 | } |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 615 | |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 616 | if len(variants) == 0 { |
| 617 | return |
| 618 | } |
| 619 | modules := mctx.CreateLocalVariations(variants...) |
| 620 | |
| 621 | // The order of the variations (modules) matches the variant names provided. Iterate |
| 622 | // through the new variation modules and set their mutated properties. |
| 623 | for i, v := range modules { |
| 624 | switch variants[i] { |
| 625 | case rlibVariation: |
| 626 | v.(*Module).compiler.(libraryInterface).setRlib() |
| 627 | case dylibVariation: |
| 628 | v.(*Module).compiler.(libraryInterface).setDylib() |
Ivan Lozano | c08897c | 2021-04-02 12:41:32 -0400 | [diff] [blame] | 629 | if v.(*Module).ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation { |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 630 | // TODO(b/165791368) |
Ivan Lozano | c08897c | 2021-04-02 12:41:32 -0400 | [diff] [blame] | 631 | // Disable dylib Vendor Ramdisk variations until we support these. |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 632 | v.(*Module).Disable() |
| 633 | } |
Ivan Lozano | 1921e80 | 2021-05-20 13:39:16 -0400 | [diff] [blame] | 634 | |
| 635 | variation := v.(*Module).ModuleBase.ImageVariation().Variation |
| 636 | if strings.HasPrefix(variation, cc.VendorVariationPrefix) && |
| 637 | m.HasVendorVariant() && |
| 638 | !cc.IsVendorProprietaryModule(mctx) && |
| 639 | strings.TrimPrefix(variation, cc.VendorVariationPrefix) == mctx.DeviceConfig().VndkVersion() { |
| 640 | |
| 641 | // cc.MutateImage runs before LibraryMutator, so vendor variations which are meant for rlibs only are |
| 642 | // produced for Dylibs; however, dylibs should not be enabled for boardVndkVersion for |
| 643 | // non-vendor proprietary modules. |
| 644 | v.(*Module).Disable() |
| 645 | } |
| 646 | |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 647 | case "source": |
| 648 | v.(*Module).compiler.(libraryInterface).setSource() |
| 649 | // The source variant does not produce any library. |
| 650 | // Disable the compilation steps. |
| 651 | v.(*Module).compiler.SetDisabled() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 652 | } |
| 653 | } |
Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 654 | |
| 655 | // If a source variant is created, add an inter-variant dependency |
| 656 | // between the other variants and the source variant. |
| 657 | if sourceVariant { |
| 658 | sv := modules[0] |
| 659 | for _, v := range modules[1:] { |
| 660 | if !v.Enabled() { |
| 661 | continue |
| 662 | } |
| 663 | mctx.AddInterVariantDependency(sourceDepTag, v, sv) |
| 664 | } |
| 665 | // Alias the source variation so it can be named directly in "srcs" properties. |
| 666 | mctx.AliasVariation("source") |
| 667 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 668 | } |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 669 | |
| 670 | func LibstdMutator(mctx android.BottomUpMutatorContext) { |
| 671 | if m, ok := mctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() { |
| 672 | switch library := m.compiler.(type) { |
| 673 | case libraryInterface: |
| 674 | // Only create a variant if a library is actually being built. |
| 675 | if library.rlib() && !library.sysroot() { |
| 676 | variants := []string{"rlib-std", "dylib-std"} |
| 677 | modules := mctx.CreateLocalVariations(variants...) |
| 678 | |
| 679 | rlib := modules[0].(*Module) |
| 680 | dylib := modules[1].(*Module) |
| 681 | rlib.compiler.(libraryInterface).setRlibStd() |
| 682 | dylib.compiler.(libraryInterface).setDylibStd() |
Ivan Lozano | c08897c | 2021-04-02 12:41:32 -0400 | [diff] [blame] | 683 | if dylib.ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation { |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 684 | // TODO(b/165791368) |
Ivan Lozano | c08897c | 2021-04-02 12:41:32 -0400 | [diff] [blame] | 685 | // Disable rlibs that link against dylib-std on vendor ramdisk variations until those dylib |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 686 | // variants are properly supported. |
| 687 | dylib.Disable() |
| 688 | } |
Ivan Lozano | c08897c | 2021-04-02 12:41:32 -0400 | [diff] [blame] | 689 | rlib.Properties.RustSubName += RlibStdlibSuffix |
| 690 | dylib.Properties.RustSubName += DylibStdlibSuffix |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 691 | } |
| 692 | } |
| 693 | } |
| 694 | } |
Ivan Lozano | 1921e80 | 2021-05-20 13:39:16 -0400 | [diff] [blame] | 695 | |
| 696 | func (l *libraryDecorator) snapshotHeaders() android.Paths { |
| 697 | if l.collectedSnapshotHeaders == nil { |
| 698 | panic("snapshotHeaders() must be called after collectHeadersForSnapshot()") |
| 699 | } |
| 700 | return l.collectedSnapshotHeaders |
| 701 | } |
| 702 | |
| 703 | // collectHeadersForSnapshot collects all exported headers from library. |
| 704 | // It globs header files in the source tree for exported include directories, |
| 705 | // and tracks generated header files separately. |
| 706 | // |
| 707 | // This is to be called from GenerateAndroidBuildActions, and then collected |
| 708 | // header files can be retrieved by snapshotHeaders(). |
| 709 | func (l *libraryDecorator) collectHeadersForSnapshot(ctx android.ModuleContext, deps PathDeps) { |
| 710 | ret := android.Paths{} |
| 711 | |
| 712 | // Glob together the headers from the modules include_dirs property |
| 713 | for _, path := range android.CopyOfPaths(l.includeDirs) { |
| 714 | dir := path.String() |
| 715 | glob, err := ctx.GlobWithDeps(dir+"/**/*", nil) |
| 716 | if err != nil { |
| 717 | ctx.ModuleErrorf("glob failed: %#v", err) |
| 718 | return |
| 719 | } |
| 720 | |
| 721 | for _, header := range glob { |
| 722 | // Filter out only the files with extensions that are headers. |
| 723 | found := false |
| 724 | for _, ext := range cc.HeaderExts { |
| 725 | if strings.HasSuffix(header, ext) { |
| 726 | found = true |
| 727 | break |
| 728 | } |
| 729 | } |
| 730 | if !found { |
| 731 | continue |
| 732 | } |
| 733 | ret = append(ret, android.PathForSource(ctx, header)) |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | // Glob together the headers from C dependencies as well, starting with non-generated headers. |
| 738 | ret = append(ret, cc.GlobHeadersForSnapshot(ctx, append(android.CopyOfPaths(deps.depIncludePaths), deps.depSystemIncludePaths...))...) |
| 739 | |
| 740 | // Collect generated headers from C dependencies. |
| 741 | ret = append(ret, cc.GlobGeneratedHeadersForSnapshot(ctx, deps.depGeneratedHeaders)...) |
| 742 | |
| 743 | // TODO(185577950): If support for generated headers is added, they need to be collected here as well. |
| 744 | l.collectedSnapshotHeaders = ret |
| 745 | } |