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