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