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 ( |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 18 | "regexp" |
| 19 | "strings" |
| 20 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 21 | "android/soong/android" |
Ivan Lozano | 88f2b1c | 2019-11-21 14:35:20 -0800 | [diff] [blame] | 22 | "android/soong/rust/config" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 23 | ) |
| 24 | |
| 25 | func init() { |
| 26 | android.RegisterModuleType("rust_library", RustLibraryFactory) |
| 27 | android.RegisterModuleType("rust_library_dylib", RustLibraryDylibFactory) |
| 28 | android.RegisterModuleType("rust_library_rlib", RustLibraryRlibFactory) |
| 29 | android.RegisterModuleType("rust_library_host", RustLibraryHostFactory) |
| 30 | android.RegisterModuleType("rust_library_host_dylib", RustLibraryDylibHostFactory) |
| 31 | android.RegisterModuleType("rust_library_host_rlib", RustLibraryRlibHostFactory) |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame^] | 32 | android.RegisterModuleType("rust_ffi", RustFFIFactory) |
| 33 | android.RegisterModuleType("rust_ffi_shared", RustFFISharedFactory) |
| 34 | android.RegisterModuleType("rust_ffi_static", RustFFIStaticFactory) |
| 35 | android.RegisterModuleType("rust_ffi_host", RustFFIHostFactory) |
| 36 | android.RegisterModuleType("rust_ffi_host_shared", RustFFISharedHostFactory) |
| 37 | android.RegisterModuleType("rust_ffi_host_static", RustFFIStaticHostFactory) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | type VariantLibraryProperties struct { |
| 41 | Enabled *bool `android:"arch_variant"` |
| 42 | } |
| 43 | |
| 44 | type LibraryCompilerProperties struct { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 45 | Rlib VariantLibraryProperties `android:"arch_variant"` |
| 46 | Dylib VariantLibraryProperties `android:"arch_variant"` |
| 47 | Shared VariantLibraryProperties `android:"arch_variant"` |
| 48 | Static VariantLibraryProperties `android:"arch_variant"` |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 49 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 50 | // path to include directories to pass to cc_* modules, only relevant for static/shared variants. |
| 51 | Include_dirs []string `android:"path,arch_variant"` |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | type LibraryMutatedProperties struct { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 55 | // Build a dylib variant |
| 56 | BuildDylib bool `blueprint:"mutated"` |
| 57 | // Build an rlib variant |
| 58 | BuildRlib bool `blueprint:"mutated"` |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 59 | // Build a shared library variant |
| 60 | BuildShared bool `blueprint:"mutated"` |
| 61 | // Build a static library variant |
| 62 | BuildStatic bool `blueprint:"mutated"` |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 63 | |
| 64 | // This variant is a dylib |
| 65 | VariantIsDylib bool `blueprint:"mutated"` |
| 66 | // This variant is an rlib |
| 67 | VariantIsRlib bool `blueprint:"mutated"` |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 68 | // This variant is a shared library |
| 69 | VariantIsShared bool `blueprint:"mutated"` |
| 70 | // This variant is a static library |
| 71 | VariantIsStatic bool `blueprint:"mutated"` |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | type libraryDecorator struct { |
| 75 | *baseCompiler |
| 76 | |
Ivan Lozano | 8a23fa4 | 2020-06-16 10:26:57 -0400 | [diff] [blame] | 77 | Properties LibraryCompilerProperties |
| 78 | MutatedProperties LibraryMutatedProperties |
| 79 | includeDirs android.Paths |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | type libraryInterface interface { |
| 83 | rlib() bool |
| 84 | dylib() bool |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 85 | static() bool |
| 86 | shared() bool |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 87 | |
| 88 | // Returns true if the build options for the module have selected a particular build type |
| 89 | buildRlib() bool |
| 90 | buildDylib() bool |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 91 | buildShared() bool |
| 92 | buildStatic() bool |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 93 | |
| 94 | // Sets a particular variant type |
| 95 | setRlib() |
| 96 | setDylib() |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 97 | setShared() |
| 98 | setStatic() |
| 99 | |
| 100 | // Build a specific library variant |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame^] | 101 | BuildOnlyFFI() |
| 102 | BuildOnlyRust() |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 103 | BuildOnlyRlib() |
| 104 | BuildOnlyDylib() |
| 105 | BuildOnlyStatic() |
| 106 | BuildOnlyShared() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 109 | func (library *libraryDecorator) nativeCoverage() bool { |
| 110 | return true |
| 111 | } |
| 112 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 113 | func (library *libraryDecorator) exportedDirs() []string { |
| 114 | return library.linkDirs |
| 115 | } |
| 116 | |
| 117 | func (library *libraryDecorator) exportedDepFlags() []string { |
| 118 | return library.depFlags |
| 119 | } |
| 120 | |
| 121 | func (library *libraryDecorator) reexportDirs(dirs ...string) { |
| 122 | library.linkDirs = android.FirstUniqueStrings(append(library.linkDirs, dirs...)) |
| 123 | } |
| 124 | |
| 125 | func (library *libraryDecorator) reexportDepFlags(flags ...string) { |
| 126 | library.depFlags = android.FirstUniqueStrings(append(library.depFlags, flags...)) |
| 127 | } |
| 128 | |
| 129 | func (library *libraryDecorator) rlib() bool { |
| 130 | return library.MutatedProperties.VariantIsRlib |
| 131 | } |
| 132 | |
| 133 | func (library *libraryDecorator) dylib() bool { |
| 134 | return library.MutatedProperties.VariantIsDylib |
| 135 | } |
| 136 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 137 | func (library *libraryDecorator) shared() bool { |
| 138 | return library.MutatedProperties.VariantIsShared |
| 139 | } |
| 140 | |
| 141 | func (library *libraryDecorator) static() bool { |
| 142 | return library.MutatedProperties.VariantIsStatic |
| 143 | } |
| 144 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 145 | func (library *libraryDecorator) buildRlib() bool { |
| 146 | return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true) |
| 147 | } |
| 148 | |
| 149 | func (library *libraryDecorator) buildDylib() bool { |
| 150 | return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true) |
| 151 | } |
| 152 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 153 | func (library *libraryDecorator) buildShared() bool { |
| 154 | return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true) |
| 155 | } |
| 156 | |
| 157 | func (library *libraryDecorator) buildStatic() bool { |
| 158 | return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true) |
| 159 | } |
| 160 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 161 | func (library *libraryDecorator) setRlib() { |
| 162 | library.MutatedProperties.VariantIsRlib = true |
| 163 | library.MutatedProperties.VariantIsDylib = false |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 164 | library.MutatedProperties.VariantIsStatic = false |
| 165 | library.MutatedProperties.VariantIsShared = false |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | func (library *libraryDecorator) setDylib() { |
| 169 | library.MutatedProperties.VariantIsRlib = false |
| 170 | library.MutatedProperties.VariantIsDylib = true |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 171 | library.MutatedProperties.VariantIsStatic = false |
| 172 | library.MutatedProperties.VariantIsShared = false |
| 173 | } |
| 174 | |
| 175 | func (library *libraryDecorator) setShared() { |
| 176 | library.MutatedProperties.VariantIsStatic = false |
| 177 | library.MutatedProperties.VariantIsShared = true |
| 178 | library.MutatedProperties.VariantIsRlib = false |
| 179 | library.MutatedProperties.VariantIsDylib = false |
| 180 | } |
| 181 | |
| 182 | func (library *libraryDecorator) setStatic() { |
| 183 | library.MutatedProperties.VariantIsStatic = true |
| 184 | library.MutatedProperties.VariantIsShared = false |
| 185 | library.MutatedProperties.VariantIsRlib = false |
| 186 | library.MutatedProperties.VariantIsDylib = false |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | var _ compiler = (*libraryDecorator)(nil) |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 190 | var _ libraryInterface = (*libraryDecorator)(nil) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 191 | |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame^] | 192 | // rust_library produces all rust variants. |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 193 | func RustLibraryFactory() android.Module { |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame^] | 194 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 195 | library.BuildOnlyRust() |
| 196 | return module.Init() |
| 197 | } |
| 198 | |
| 199 | // rust_ffi produces all ffi variants. |
| 200 | func RustFFIFactory() android.Module { |
| 201 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 202 | library.BuildOnlyFFI() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 203 | return module.Init() |
| 204 | } |
| 205 | |
| 206 | // rust_library_dylib produces a dylib. |
| 207 | func RustLibraryDylibFactory() android.Module { |
| 208 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 209 | library.BuildOnlyDylib() |
| 210 | return module.Init() |
| 211 | } |
| 212 | |
| 213 | // rust_library_rlib produces an rlib. |
| 214 | func RustLibraryRlibFactory() android.Module { |
| 215 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 216 | library.BuildOnlyRlib() |
| 217 | return module.Init() |
| 218 | } |
| 219 | |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame^] | 220 | // rust_ffi_shared produces a shared library. |
| 221 | func RustFFISharedFactory() android.Module { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 222 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 223 | library.BuildOnlyShared() |
| 224 | return module.Init() |
| 225 | } |
| 226 | |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame^] | 227 | // rust_ffi_static produces a static library. |
| 228 | func RustFFIStaticFactory() android.Module { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 229 | module, library := NewRustLibrary(android.HostAndDeviceSupported) |
| 230 | library.BuildOnlyStatic() |
| 231 | return module.Init() |
| 232 | } |
| 233 | |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame^] | 234 | // rust_library_host produces all rust variants. |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 235 | func RustLibraryHostFactory() android.Module { |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame^] | 236 | module, library := NewRustLibrary(android.HostSupported) |
| 237 | library.BuildOnlyRust() |
| 238 | return module.Init() |
| 239 | } |
| 240 | |
| 241 | // rust_ffi_host produces all FFI variants. |
| 242 | func RustFFIHostFactory() android.Module { |
| 243 | module, library := NewRustLibrary(android.HostSupported) |
| 244 | library.BuildOnlyFFI() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 245 | return module.Init() |
| 246 | } |
| 247 | |
| 248 | // rust_library_dylib_host produces a dylib. |
| 249 | func RustLibraryDylibHostFactory() android.Module { |
| 250 | module, library := NewRustLibrary(android.HostSupported) |
| 251 | library.BuildOnlyDylib() |
| 252 | return module.Init() |
| 253 | } |
| 254 | |
| 255 | // rust_library_rlib_host produces an rlib. |
| 256 | func RustLibraryRlibHostFactory() android.Module { |
| 257 | module, library := NewRustLibrary(android.HostSupported) |
| 258 | library.BuildOnlyRlib() |
| 259 | return module.Init() |
| 260 | } |
| 261 | |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame^] | 262 | // rust_ffi_static_host produces a static library. |
| 263 | func RustFFIStaticHostFactory() android.Module { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 264 | module, library := NewRustLibrary(android.HostSupported) |
| 265 | library.BuildOnlyStatic() |
| 266 | return module.Init() |
| 267 | } |
| 268 | |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame^] | 269 | // rust_ffi_shared_host produces an shared library. |
| 270 | func RustFFISharedHostFactory() android.Module { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 271 | module, library := NewRustLibrary(android.HostSupported) |
| 272 | library.BuildOnlyShared() |
| 273 | return module.Init() |
| 274 | } |
| 275 | |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame^] | 276 | func (library *libraryDecorator) BuildOnlyFFI() { |
| 277 | library.MutatedProperties.BuildDylib = false |
| 278 | library.MutatedProperties.BuildRlib = false |
| 279 | library.MutatedProperties.BuildShared = true |
| 280 | library.MutatedProperties.BuildStatic = true |
| 281 | } |
| 282 | |
| 283 | func (library *libraryDecorator) BuildOnlyRust() { |
| 284 | library.MutatedProperties.BuildDylib = true |
| 285 | library.MutatedProperties.BuildRlib = true |
| 286 | library.MutatedProperties.BuildShared = false |
| 287 | library.MutatedProperties.BuildStatic = false |
| 288 | } |
| 289 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 290 | func (library *libraryDecorator) BuildOnlyDylib() { |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame^] | 291 | library.MutatedProperties.BuildDylib = true |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 292 | library.MutatedProperties.BuildRlib = false |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 293 | library.MutatedProperties.BuildShared = false |
| 294 | library.MutatedProperties.BuildStatic = false |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | func (library *libraryDecorator) BuildOnlyRlib() { |
| 298 | library.MutatedProperties.BuildDylib = false |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame^] | 299 | library.MutatedProperties.BuildRlib = true |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 300 | library.MutatedProperties.BuildShared = false |
| 301 | library.MutatedProperties.BuildStatic = false |
| 302 | } |
| 303 | |
| 304 | func (library *libraryDecorator) BuildOnlyStatic() { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 305 | library.MutatedProperties.BuildRlib = false |
| 306 | library.MutatedProperties.BuildDylib = false |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame^] | 307 | library.MutatedProperties.BuildShared = false |
| 308 | library.MutatedProperties.BuildStatic = true |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | func (library *libraryDecorator) BuildOnlyShared() { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 312 | library.MutatedProperties.BuildRlib = false |
| 313 | library.MutatedProperties.BuildDylib = false |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame^] | 314 | library.MutatedProperties.BuildStatic = false |
| 315 | library.MutatedProperties.BuildShared = true |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) { |
Ivan Lozano | 9d1df10 | 2020-04-28 10:10:23 -0400 | [diff] [blame] | 319 | module := newModule(hod, android.MultilibBoth) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 320 | |
| 321 | library := &libraryDecorator{ |
| 322 | MutatedProperties: LibraryMutatedProperties{ |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame^] | 323 | BuildDylib: false, |
| 324 | BuildRlib: false, |
| 325 | BuildShared: false, |
| 326 | BuildStatic: false, |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 327 | }, |
Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 328 | baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem), |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | module.compiler = library |
| 332 | |
| 333 | return module, library |
| 334 | } |
| 335 | |
| 336 | func (library *libraryDecorator) compilerProps() []interface{} { |
| 337 | return append(library.baseCompiler.compilerProps(), |
| 338 | &library.Properties, |
| 339 | &library.MutatedProperties) |
| 340 | } |
| 341 | |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 342 | func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps { |
Ivan Lozano | 88f2b1c | 2019-11-21 14:35:20 -0800 | [diff] [blame] | 343 | |
Ivan Lozano | 7e741cc | 2020-06-19 12:32:30 -0400 | [diff] [blame] | 344 | // TODO(b/155498724) Remove if C static libraries no longer require libstd as an rlib dependency. |
| 345 | if !ctx.Host() && library.static() { |
Ivan Lozano | 88f2b1c | 2019-11-21 14:35:20 -0800 | [diff] [blame] | 346 | library.setNoStdlibs() |
| 347 | for _, stdlib := range config.Stdlibs { |
| 348 | deps.Rlibs = append(deps.Rlibs, stdlib+".static") |
| 349 | } |
| 350 | } |
| 351 | |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 352 | deps = library.baseCompiler.compilerDeps(ctx, deps) |
| 353 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 354 | if ctx.toolchain().Bionic() && (library.dylib() || library.shared()) { |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 355 | deps = library.baseCompiler.bionicDeps(ctx, deps) |
Ivan Lozano | 12ee9ca | 2020-04-07 13:19:44 -0400 | [diff] [blame] | 356 | deps.CrtBegin = "crtbegin_so" |
| 357 | deps.CrtEnd = "crtend_so" |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | return deps |
| 361 | } |
Ivan Lozano | bec05ea | 2020-06-09 08:27:49 -0400 | [diff] [blame] | 362 | |
| 363 | func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string { |
| 364 | return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix() |
| 365 | } |
| 366 | |
Ivan Lozano | e0833b1 | 2019-11-06 19:15:49 -0800 | [diff] [blame] | 367 | func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags { |
ThiƩbaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 368 | flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName()) |
Ivan Lozano | e0833b1 | 2019-11-06 19:15:49 -0800 | [diff] [blame] | 369 | flags = library.baseCompiler.compilerFlags(ctx, flags) |
| 370 | if library.shared() || library.static() { |
| 371 | library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...) |
| 372 | } |
Ivan Lozano | bec05ea | 2020-06-09 08:27:49 -0400 | [diff] [blame] | 373 | if library.shared() { |
| 374 | flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx)) |
| 375 | } |
| 376 | |
Ivan Lozano | e0833b1 | 2019-11-06 19:15:49 -0800 | [diff] [blame] | 377 | return flags |
| 378 | } |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 379 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 380 | func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path { |
| 381 | var outputFile android.WritablePath |
| 382 | |
Ivan Lozano | 8a23fa4 | 2020-06-16 10:26:57 -0400 | [diff] [blame] | 383 | srcPath := srcPathFromModuleSrcs(ctx, library.baseCompiler.Properties.Srcs) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 384 | |
| 385 | flags.RustFlags = append(flags.RustFlags, deps.depFlags...) |
| 386 | |
Matthew Maurer | 46c46cc | 2020-01-13 16:34:34 -0800 | [diff] [blame] | 387 | if library.dylib() { |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 388 | // We need prefer-dynamic for now to avoid linking in the static stdlib. See: |
| 389 | // https://github.com/rust-lang/rust/issues/19680 |
| 390 | // https://github.com/rust-lang/rust/issues/34909 |
| 391 | flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic") |
| 392 | } |
| 393 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 394 | if library.rlib() { |
| 395 | fileName := library.getStem(ctx) + ctx.toolchain().RlibSuffix() |
| 396 | outputFile = android.PathForModuleOut(ctx, fileName) |
| 397 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 398 | outputs := TransformSrctoRlib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs) |
| 399 | library.coverageFile = outputs.coverageFile |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 400 | } else if library.dylib() { |
| 401 | fileName := library.getStem(ctx) + ctx.toolchain().DylibSuffix() |
| 402 | outputFile = android.PathForModuleOut(ctx, fileName) |
| 403 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 404 | outputs := TransformSrctoDylib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs) |
| 405 | library.coverageFile = outputs.coverageFile |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 406 | } else if library.static() { |
| 407 | fileName := library.getStem(ctx) + ctx.toolchain().StaticLibSuffix() |
| 408 | outputFile = android.PathForModuleOut(ctx, fileName) |
| 409 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 410 | outputs := TransformSrctoStatic(ctx, srcPath, deps, flags, outputFile, deps.linkDirs) |
| 411 | library.coverageFile = outputs.coverageFile |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 412 | } else if library.shared() { |
Ivan Lozano | bec05ea | 2020-06-09 08:27:49 -0400 | [diff] [blame] | 413 | fileName := library.sharedLibFilename(ctx) |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 414 | outputFile = android.PathForModuleOut(ctx, fileName) |
| 415 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 416 | outputs := TransformSrctoShared(ctx, srcPath, deps, flags, outputFile, deps.linkDirs) |
| 417 | library.coverageFile = outputs.coverageFile |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 420 | var coverageFiles android.Paths |
| 421 | if library.coverageFile != nil { |
| 422 | coverageFiles = append(coverageFiles, library.coverageFile) |
| 423 | } |
| 424 | if len(deps.coverageFiles) > 0 { |
| 425 | coverageFiles = append(coverageFiles, deps.coverageFiles...) |
| 426 | } |
| 427 | library.coverageOutputZipFile = TransformCoverageFilesToZip(ctx, coverageFiles, library.getStem(ctx)) |
| 428 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 429 | if library.rlib() || library.dylib() { |
| 430 | library.reexportDirs(deps.linkDirs...) |
| 431 | library.reexportDepFlags(deps.depFlags...) |
| 432 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 433 | library.unstrippedOutputFile = outputFile |
| 434 | |
| 435 | return outputFile |
| 436 | } |
| 437 | |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 438 | func (library *libraryDecorator) getStem(ctx ModuleContext) string { |
| 439 | stem := library.baseCompiler.getStemWithoutSuffix(ctx) |
| 440 | validateLibraryStem(ctx, stem, library.crateName()) |
| 441 | |
| 442 | return stem + String(library.baseCompiler.Properties.Suffix) |
| 443 | } |
| 444 | |
| 445 | var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+") |
| 446 | |
| 447 | func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) { |
| 448 | if crate_name == "" { |
| 449 | ctx.PropertyErrorf("crate_name", "crate_name must be defined.") |
| 450 | } |
| 451 | |
| 452 | // crate_names are used for the library output file, and rustc expects these |
| 453 | // to be alphanumeric with underscores allowed. |
| 454 | if validCrateName.MatchString(crate_name) { |
| 455 | ctx.PropertyErrorf("crate_name", |
| 456 | "library crate_names must be alphanumeric with underscores allowed") |
| 457 | } |
| 458 | |
| 459 | // Libraries are expected to begin with "lib" followed by the crate_name |
| 460 | if !strings.HasPrefix(filename, "lib"+crate_name) { |
| 461 | ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>") |
| 462 | } |
| 463 | } |
| 464 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 465 | func LibraryMutator(mctx android.BottomUpMutatorContext) { |
| 466 | if m, ok := mctx.Module().(*Module); ok && m.compiler != nil { |
| 467 | switch library := m.compiler.(type) { |
| 468 | case libraryInterface: |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 469 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 470 | // We only build the rust library variants here. This assumes that |
| 471 | // LinkageMutator runs first and there's an empty variant |
| 472 | // if rust variants are required. |
| 473 | if !library.static() && !library.shared() { |
| 474 | if library.buildRlib() && library.buildDylib() { |
| 475 | modules := mctx.CreateLocalVariations("rlib", "dylib") |
| 476 | rlib := modules[0].(*Module) |
| 477 | dylib := modules[1].(*Module) |
| 478 | |
| 479 | rlib.compiler.(libraryInterface).setRlib() |
| 480 | dylib.compiler.(libraryInterface).setDylib() |
| 481 | } else if library.buildRlib() { |
| 482 | modules := mctx.CreateLocalVariations("rlib") |
| 483 | modules[0].(*Module).compiler.(libraryInterface).setRlib() |
| 484 | } else if library.buildDylib() { |
| 485 | modules := mctx.CreateLocalVariations("dylib") |
| 486 | modules[0].(*Module).compiler.(libraryInterface).setDylib() |
| 487 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | } |
| 491 | } |