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