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