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