blob: b266d04ca569145f8fce9c5331f3958cf8c67319 [file] [log] [blame]
Ivan Lozanoffee3342019-08-27 12:03:00 -07001// 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
15package rust
16
17import (
Ivan Lozanoad8b18b2019-10-31 19:38:29 -070018 "regexp"
19 "strings"
20
Ivan Lozanoffee3342019-08-27 12:03:00 -070021 "android/soong/android"
Ivan Lozano88f2b1c2019-11-21 14:35:20 -080022 "android/soong/rust/config"
Ivan Lozanoffee3342019-08-27 12:03:00 -070023)
24
25func 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 Maurer2ae05132020-06-23 14:28:53 -070032 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 Lozanoffee3342019-08-27 12:03:00 -070038}
39
40type VariantLibraryProperties struct {
41 Enabled *bool `android:"arch_variant"`
42}
43
44type LibraryCompilerProperties struct {
Ivan Lozano52767be2019-10-18 14:49:46 -070045 Rlib VariantLibraryProperties `android:"arch_variant"`
46 Dylib VariantLibraryProperties `android:"arch_variant"`
47 Shared VariantLibraryProperties `android:"arch_variant"`
48 Static VariantLibraryProperties `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070049
Ivan Lozano52767be2019-10-18 14:49:46 -070050 // path to include directories to pass to cc_* modules, only relevant for static/shared variants.
51 Include_dirs []string `android:"path,arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070052}
53
54type LibraryMutatedProperties struct {
Ivan Lozanoffee3342019-08-27 12:03:00 -070055 // Build a dylib variant
56 BuildDylib bool `blueprint:"mutated"`
57 // Build an rlib variant
58 BuildRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070059 // Build a shared library variant
60 BuildShared bool `blueprint:"mutated"`
61 // Build a static library variant
62 BuildStatic bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070063
64 // This variant is a dylib
65 VariantIsDylib bool `blueprint:"mutated"`
66 // This variant is an rlib
67 VariantIsRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070068 // This variant is a shared library
69 VariantIsShared bool `blueprint:"mutated"`
70 // This variant is a static library
71 VariantIsStatic bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070072}
73
74type libraryDecorator struct {
75 *baseCompiler
76
Ivan Lozano8a23fa42020-06-16 10:26:57 -040077 Properties LibraryCompilerProperties
78 MutatedProperties LibraryMutatedProperties
79 includeDirs android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -070080}
81
82type libraryInterface interface {
83 rlib() bool
84 dylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -070085 static() bool
86 shared() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070087
88 // Returns true if the build options for the module have selected a particular build type
89 buildRlib() bool
90 buildDylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -070091 buildShared() bool
92 buildStatic() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070093
94 // Sets a particular variant type
95 setRlib()
96 setDylib()
Ivan Lozano52767be2019-10-18 14:49:46 -070097 setShared()
98 setStatic()
99
100 // Build a specific library variant
Matthew Maurer2ae05132020-06-23 14:28:53 -0700101 BuildOnlyFFI()
102 BuildOnlyRust()
Ivan Lozano52767be2019-10-18 14:49:46 -0700103 BuildOnlyRlib()
104 BuildOnlyDylib()
105 BuildOnlyStatic()
106 BuildOnlyShared()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700107}
108
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400109func (library *libraryDecorator) nativeCoverage() bool {
110 return true
111}
112
Ivan Lozanoffee3342019-08-27 12:03:00 -0700113func (library *libraryDecorator) exportedDirs() []string {
114 return library.linkDirs
115}
116
117func (library *libraryDecorator) exportedDepFlags() []string {
118 return library.depFlags
119}
120
121func (library *libraryDecorator) reexportDirs(dirs ...string) {
122 library.linkDirs = android.FirstUniqueStrings(append(library.linkDirs, dirs...))
123}
124
125func (library *libraryDecorator) reexportDepFlags(flags ...string) {
126 library.depFlags = android.FirstUniqueStrings(append(library.depFlags, flags...))
127}
128
129func (library *libraryDecorator) rlib() bool {
130 return library.MutatedProperties.VariantIsRlib
131}
132
133func (library *libraryDecorator) dylib() bool {
134 return library.MutatedProperties.VariantIsDylib
135}
136
Ivan Lozano52767be2019-10-18 14:49:46 -0700137func (library *libraryDecorator) shared() bool {
138 return library.MutatedProperties.VariantIsShared
139}
140
141func (library *libraryDecorator) static() bool {
142 return library.MutatedProperties.VariantIsStatic
143}
144
Ivan Lozanoffee3342019-08-27 12:03:00 -0700145func (library *libraryDecorator) buildRlib() bool {
146 return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true)
147}
148
149func (library *libraryDecorator) buildDylib() bool {
150 return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true)
151}
152
Ivan Lozano52767be2019-10-18 14:49:46 -0700153func (library *libraryDecorator) buildShared() bool {
154 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
155}
156
157func (library *libraryDecorator) buildStatic() bool {
158 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
159}
160
Ivan Lozanoffee3342019-08-27 12:03:00 -0700161func (library *libraryDecorator) setRlib() {
162 library.MutatedProperties.VariantIsRlib = true
163 library.MutatedProperties.VariantIsDylib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700164 library.MutatedProperties.VariantIsStatic = false
165 library.MutatedProperties.VariantIsShared = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700166}
167
168func (library *libraryDecorator) setDylib() {
169 library.MutatedProperties.VariantIsRlib = false
170 library.MutatedProperties.VariantIsDylib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700171 library.MutatedProperties.VariantIsStatic = false
172 library.MutatedProperties.VariantIsShared = false
173}
174
175func (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
182func (library *libraryDecorator) setStatic() {
183 library.MutatedProperties.VariantIsStatic = true
184 library.MutatedProperties.VariantIsShared = false
185 library.MutatedProperties.VariantIsRlib = false
186 library.MutatedProperties.VariantIsDylib = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700187}
188
Matthew Maurer0f003b12020-06-29 14:34:06 -0700189func (library *libraryDecorator) autoDep() autoDep {
190 if library.rlib() || library.static() {
191 return rlibAutoDep
192 } else if library.dylib() || library.shared() {
193 return dylibAutoDep
194 } else {
195 return rlibAutoDep
196 }
197}
198
Ivan Lozanoffee3342019-08-27 12:03:00 -0700199var _ compiler = (*libraryDecorator)(nil)
Ivan Lozano52767be2019-10-18 14:49:46 -0700200var _ libraryInterface = (*libraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700201
Matthew Maurer2ae05132020-06-23 14:28:53 -0700202// rust_library produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700203func RustLibraryFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700204 module, library := NewRustLibrary(android.HostAndDeviceSupported)
205 library.BuildOnlyRust()
206 return module.Init()
207}
208
209// rust_ffi produces all ffi variants.
210func RustFFIFactory() android.Module {
211 module, library := NewRustLibrary(android.HostAndDeviceSupported)
212 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700213 return module.Init()
214}
215
216// rust_library_dylib produces a dylib.
217func RustLibraryDylibFactory() android.Module {
218 module, library := NewRustLibrary(android.HostAndDeviceSupported)
219 library.BuildOnlyDylib()
220 return module.Init()
221}
222
223// rust_library_rlib produces an rlib.
224func RustLibraryRlibFactory() android.Module {
225 module, library := NewRustLibrary(android.HostAndDeviceSupported)
226 library.BuildOnlyRlib()
227 return module.Init()
228}
229
Matthew Maurer2ae05132020-06-23 14:28:53 -0700230// rust_ffi_shared produces a shared library.
231func RustFFISharedFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700232 module, library := NewRustLibrary(android.HostAndDeviceSupported)
233 library.BuildOnlyShared()
234 return module.Init()
235}
236
Matthew Maurer2ae05132020-06-23 14:28:53 -0700237// rust_ffi_static produces a static library.
238func RustFFIStaticFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700239 module, library := NewRustLibrary(android.HostAndDeviceSupported)
240 library.BuildOnlyStatic()
241 return module.Init()
242}
243
Matthew Maurer2ae05132020-06-23 14:28:53 -0700244// rust_library_host produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700245func RustLibraryHostFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700246 module, library := NewRustLibrary(android.HostSupported)
247 library.BuildOnlyRust()
248 return module.Init()
249}
250
251// rust_ffi_host produces all FFI variants.
252func RustFFIHostFactory() android.Module {
253 module, library := NewRustLibrary(android.HostSupported)
254 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700255 return module.Init()
256}
257
258// rust_library_dylib_host produces a dylib.
259func RustLibraryDylibHostFactory() android.Module {
260 module, library := NewRustLibrary(android.HostSupported)
261 library.BuildOnlyDylib()
262 return module.Init()
263}
264
265// rust_library_rlib_host produces an rlib.
266func RustLibraryRlibHostFactory() android.Module {
267 module, library := NewRustLibrary(android.HostSupported)
268 library.BuildOnlyRlib()
269 return module.Init()
270}
271
Matthew Maurer2ae05132020-06-23 14:28:53 -0700272// rust_ffi_static_host produces a static library.
273func RustFFIStaticHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700274 module, library := NewRustLibrary(android.HostSupported)
275 library.BuildOnlyStatic()
276 return module.Init()
277}
278
Matthew Maurer2ae05132020-06-23 14:28:53 -0700279// rust_ffi_shared_host produces an shared library.
280func RustFFISharedHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700281 module, library := NewRustLibrary(android.HostSupported)
282 library.BuildOnlyShared()
283 return module.Init()
284}
285
Matthew Maurer2ae05132020-06-23 14:28:53 -0700286func (library *libraryDecorator) BuildOnlyFFI() {
287 library.MutatedProperties.BuildDylib = false
288 library.MutatedProperties.BuildRlib = false
289 library.MutatedProperties.BuildShared = true
290 library.MutatedProperties.BuildStatic = true
291}
292
293func (library *libraryDecorator) BuildOnlyRust() {
294 library.MutatedProperties.BuildDylib = true
295 library.MutatedProperties.BuildRlib = true
296 library.MutatedProperties.BuildShared = false
297 library.MutatedProperties.BuildStatic = false
298}
299
Ivan Lozanoffee3342019-08-27 12:03:00 -0700300func (library *libraryDecorator) BuildOnlyDylib() {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700301 library.MutatedProperties.BuildDylib = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700302 library.MutatedProperties.BuildRlib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700303 library.MutatedProperties.BuildShared = false
304 library.MutatedProperties.BuildStatic = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700305}
306
307func (library *libraryDecorator) BuildOnlyRlib() {
308 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700309 library.MutatedProperties.BuildRlib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700310 library.MutatedProperties.BuildShared = false
311 library.MutatedProperties.BuildStatic = false
312}
313
314func (library *libraryDecorator) BuildOnlyStatic() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700315 library.MutatedProperties.BuildRlib = false
316 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700317 library.MutatedProperties.BuildShared = false
318 library.MutatedProperties.BuildStatic = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700319}
320
321func (library *libraryDecorator) BuildOnlyShared() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700322 library.MutatedProperties.BuildRlib = false
323 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700324 library.MutatedProperties.BuildStatic = false
325 library.MutatedProperties.BuildShared = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700326}
327
328func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400329 module := newModule(hod, android.MultilibBoth)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700330
331 library := &libraryDecorator{
332 MutatedProperties: LibraryMutatedProperties{
Matthew Maurer2ae05132020-06-23 14:28:53 -0700333 BuildDylib: false,
334 BuildRlib: false,
335 BuildShared: false,
336 BuildStatic: false,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700337 },
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800338 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700339 }
340
341 module.compiler = library
342
343 return module, library
344}
345
346func (library *libraryDecorator) compilerProps() []interface{} {
347 return append(library.baseCompiler.compilerProps(),
348 &library.Properties,
349 &library.MutatedProperties)
350}
351
Ivan Lozanof1c84332019-09-20 11:00:37 -0700352func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
Ivan Lozano88f2b1c2019-11-21 14:35:20 -0800353
Ivan Lozano7e741cc2020-06-19 12:32:30 -0400354 // TODO(b/155498724) Remove if C static libraries no longer require libstd as an rlib dependency.
355 if !ctx.Host() && library.static() {
Ivan Lozano88f2b1c2019-11-21 14:35:20 -0800356 library.setNoStdlibs()
357 for _, stdlib := range config.Stdlibs {
358 deps.Rlibs = append(deps.Rlibs, stdlib+".static")
359 }
360 }
361
Ivan Lozanof1c84332019-09-20 11:00:37 -0700362 deps = library.baseCompiler.compilerDeps(ctx, deps)
363
Ivan Lozano52767be2019-10-18 14:49:46 -0700364 if ctx.toolchain().Bionic() && (library.dylib() || library.shared()) {
Ivan Lozanof1c84332019-09-20 11:00:37 -0700365 deps = library.baseCompiler.bionicDeps(ctx, deps)
Ivan Lozano12ee9ca2020-04-07 13:19:44 -0400366 deps.CrtBegin = "crtbegin_so"
367 deps.CrtEnd = "crtend_so"
Ivan Lozanof1c84332019-09-20 11:00:37 -0700368 }
369
370 return deps
371}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400372
373func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string {
374 return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
375}
376
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800377func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
ThiƩbaud Weksteen1f7f70f2020-06-24 11:32:48 +0200378 flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800379 flags = library.baseCompiler.compilerFlags(ctx, flags)
380 if library.shared() || library.static() {
381 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
382 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400383 if library.shared() {
384 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
385 }
386
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800387 return flags
388}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700389
Ivan Lozanoffee3342019-08-27 12:03:00 -0700390func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
391 var outputFile android.WritablePath
392
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400393 srcPath := srcPathFromModuleSrcs(ctx, library.baseCompiler.Properties.Srcs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700394
395 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
396
Matthew Maurer46c46cc2020-01-13 16:34:34 -0800397 if library.dylib() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700398 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
399 // https://github.com/rust-lang/rust/issues/19680
400 // https://github.com/rust-lang/rust/issues/34909
401 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
402 }
403
Ivan Lozanoffee3342019-08-27 12:03:00 -0700404 if library.rlib() {
405 fileName := library.getStem(ctx) + ctx.toolchain().RlibSuffix()
406 outputFile = android.PathForModuleOut(ctx, fileName)
407
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400408 outputs := TransformSrctoRlib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
409 library.coverageFile = outputs.coverageFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700410 } else if library.dylib() {
411 fileName := library.getStem(ctx) + ctx.toolchain().DylibSuffix()
412 outputFile = android.PathForModuleOut(ctx, fileName)
413
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400414 outputs := TransformSrctoDylib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
415 library.coverageFile = outputs.coverageFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700416 } else if library.static() {
417 fileName := library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
418 outputFile = android.PathForModuleOut(ctx, fileName)
419
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400420 outputs := TransformSrctoStatic(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
421 library.coverageFile = outputs.coverageFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700422 } else if library.shared() {
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400423 fileName := library.sharedLibFilename(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700424 outputFile = android.PathForModuleOut(ctx, fileName)
425
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400426 outputs := TransformSrctoShared(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
427 library.coverageFile = outputs.coverageFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700428 }
429
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400430 var coverageFiles android.Paths
431 if library.coverageFile != nil {
432 coverageFiles = append(coverageFiles, library.coverageFile)
433 }
434 if len(deps.coverageFiles) > 0 {
435 coverageFiles = append(coverageFiles, deps.coverageFiles...)
436 }
437 library.coverageOutputZipFile = TransformCoverageFilesToZip(ctx, coverageFiles, library.getStem(ctx))
438
Ivan Lozano52767be2019-10-18 14:49:46 -0700439 if library.rlib() || library.dylib() {
440 library.reexportDirs(deps.linkDirs...)
441 library.reexportDepFlags(deps.depFlags...)
442 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700443 library.unstrippedOutputFile = outputFile
444
445 return outputFile
446}
447
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700448func (library *libraryDecorator) getStem(ctx ModuleContext) string {
449 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
450 validateLibraryStem(ctx, stem, library.crateName())
451
452 return stem + String(library.baseCompiler.Properties.Suffix)
453}
454
455var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
456
457func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
458 if crate_name == "" {
459 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
460 }
461
462 // crate_names are used for the library output file, and rustc expects these
463 // to be alphanumeric with underscores allowed.
464 if validCrateName.MatchString(crate_name) {
465 ctx.PropertyErrorf("crate_name",
466 "library crate_names must be alphanumeric with underscores allowed")
467 }
468
469 // Libraries are expected to begin with "lib" followed by the crate_name
470 if !strings.HasPrefix(filename, "lib"+crate_name) {
471 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
472 }
473}
474
Ivan Lozanoffee3342019-08-27 12:03:00 -0700475func LibraryMutator(mctx android.BottomUpMutatorContext) {
476 if m, ok := mctx.Module().(*Module); ok && m.compiler != nil {
477 switch library := m.compiler.(type) {
478 case libraryInterface:
Ivan Lozanoffee3342019-08-27 12:03:00 -0700479
Ivan Lozano52767be2019-10-18 14:49:46 -0700480 // We only build the rust library variants here. This assumes that
481 // LinkageMutator runs first and there's an empty variant
482 // if rust variants are required.
483 if !library.static() && !library.shared() {
484 if library.buildRlib() && library.buildDylib() {
485 modules := mctx.CreateLocalVariations("rlib", "dylib")
486 rlib := modules[0].(*Module)
487 dylib := modules[1].(*Module)
488
489 rlib.compiler.(libraryInterface).setRlib()
490 dylib.compiler.(libraryInterface).setDylib()
491 } else if library.buildRlib() {
492 modules := mctx.CreateLocalVariations("rlib")
493 modules[0].(*Module).compiler.(libraryInterface).setRlib()
494 } else if library.buildDylib() {
495 modules := mctx.CreateLocalVariations("dylib")
496 modules[0].(*Module).compiler.(libraryInterface).setDylib()
497 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700498 }
499 }
500 }
501}