blob: 3dc2559e9535d5731dd72dfe8b02c6a91d0770ec [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 {
Matthew Maurerc761eec2020-06-25 00:47:46 -070041 Enabled *bool `android:"arch_variant"`
42 Srcs []string `android:"path,arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070043}
44
45type LibraryCompilerProperties struct {
Ivan Lozano52767be2019-10-18 14:49:46 -070046 Rlib VariantLibraryProperties `android:"arch_variant"`
47 Dylib VariantLibraryProperties `android:"arch_variant"`
48 Shared VariantLibraryProperties `android:"arch_variant"`
49 Static VariantLibraryProperties `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070050
Ivan Lozano52767be2019-10-18 14:49:46 -070051 // path to include directories to pass to cc_* modules, only relevant for static/shared variants.
52 Include_dirs []string `android:"path,arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070053}
54
55type LibraryMutatedProperties struct {
Ivan Lozanoffee3342019-08-27 12:03:00 -070056 // Build a dylib variant
57 BuildDylib bool `blueprint:"mutated"`
58 // Build an rlib variant
59 BuildRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070060 // Build a shared library variant
61 BuildShared bool `blueprint:"mutated"`
62 // Build a static library variant
63 BuildStatic bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070064
65 // This variant is a dylib
66 VariantIsDylib bool `blueprint:"mutated"`
67 // This variant is an rlib
68 VariantIsRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070069 // This variant is a shared library
70 VariantIsShared bool `blueprint:"mutated"`
71 // This variant is a static library
72 VariantIsStatic bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070073}
74
75type libraryDecorator struct {
76 *baseCompiler
77
Ivan Lozano8a23fa42020-06-16 10:26:57 -040078 Properties LibraryCompilerProperties
79 MutatedProperties LibraryMutatedProperties
80 includeDirs android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -070081}
82
83type libraryInterface interface {
84 rlib() bool
85 dylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -070086 static() bool
87 shared() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070088
89 // Returns true if the build options for the module have selected a particular build type
90 buildRlib() bool
91 buildDylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -070092 buildShared() bool
93 buildStatic() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070094
95 // Sets a particular variant type
96 setRlib()
97 setDylib()
Ivan Lozano52767be2019-10-18 14:49:46 -070098 setShared()
99 setStatic()
100
101 // Build a specific library variant
Matthew Maurer2ae05132020-06-23 14:28:53 -0700102 BuildOnlyFFI()
103 BuildOnlyRust()
Ivan Lozano52767be2019-10-18 14:49:46 -0700104 BuildOnlyRlib()
105 BuildOnlyDylib()
106 BuildOnlyStatic()
107 BuildOnlyShared()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700108}
109
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400110func (library *libraryDecorator) nativeCoverage() bool {
111 return true
112}
113
Ivan Lozanoffee3342019-08-27 12:03:00 -0700114func (library *libraryDecorator) exportedDirs() []string {
115 return library.linkDirs
116}
117
118func (library *libraryDecorator) exportedDepFlags() []string {
119 return library.depFlags
120}
121
122func (library *libraryDecorator) reexportDirs(dirs ...string) {
123 library.linkDirs = android.FirstUniqueStrings(append(library.linkDirs, dirs...))
124}
125
126func (library *libraryDecorator) reexportDepFlags(flags ...string) {
127 library.depFlags = android.FirstUniqueStrings(append(library.depFlags, flags...))
128}
129
130func (library *libraryDecorator) rlib() bool {
131 return library.MutatedProperties.VariantIsRlib
132}
133
134func (library *libraryDecorator) dylib() bool {
135 return library.MutatedProperties.VariantIsDylib
136}
137
Ivan Lozano52767be2019-10-18 14:49:46 -0700138func (library *libraryDecorator) shared() bool {
139 return library.MutatedProperties.VariantIsShared
140}
141
142func (library *libraryDecorator) static() bool {
143 return library.MutatedProperties.VariantIsStatic
144}
145
Ivan Lozanoffee3342019-08-27 12:03:00 -0700146func (library *libraryDecorator) buildRlib() bool {
147 return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true)
148}
149
150func (library *libraryDecorator) buildDylib() bool {
151 return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true)
152}
153
Ivan Lozano52767be2019-10-18 14:49:46 -0700154func (library *libraryDecorator) buildShared() bool {
155 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
156}
157
158func (library *libraryDecorator) buildStatic() bool {
159 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
160}
161
Ivan Lozanoffee3342019-08-27 12:03:00 -0700162func (library *libraryDecorator) setRlib() {
163 library.MutatedProperties.VariantIsRlib = true
164 library.MutatedProperties.VariantIsDylib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700165 library.MutatedProperties.VariantIsStatic = false
166 library.MutatedProperties.VariantIsShared = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700167}
168
169func (library *libraryDecorator) setDylib() {
170 library.MutatedProperties.VariantIsRlib = false
171 library.MutatedProperties.VariantIsDylib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700172 library.MutatedProperties.VariantIsStatic = false
173 library.MutatedProperties.VariantIsShared = false
174}
175
176func (library *libraryDecorator) setShared() {
177 library.MutatedProperties.VariantIsStatic = false
178 library.MutatedProperties.VariantIsShared = true
179 library.MutatedProperties.VariantIsRlib = false
180 library.MutatedProperties.VariantIsDylib = false
181}
182
183func (library *libraryDecorator) setStatic() {
184 library.MutatedProperties.VariantIsStatic = true
185 library.MutatedProperties.VariantIsShared = false
186 library.MutatedProperties.VariantIsRlib = false
187 library.MutatedProperties.VariantIsDylib = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700188}
189
Matthew Maurer0f003b12020-06-29 14:34:06 -0700190func (library *libraryDecorator) autoDep() autoDep {
191 if library.rlib() || library.static() {
192 return rlibAutoDep
193 } else if library.dylib() || library.shared() {
194 return dylibAutoDep
195 } else {
196 return rlibAutoDep
197 }
198}
199
Ivan Lozanoffee3342019-08-27 12:03:00 -0700200var _ compiler = (*libraryDecorator)(nil)
Ivan Lozano52767be2019-10-18 14:49:46 -0700201var _ libraryInterface = (*libraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700202
Matthew Maurer2ae05132020-06-23 14:28:53 -0700203// rust_library produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700204func RustLibraryFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700205 module, library := NewRustLibrary(android.HostAndDeviceSupported)
206 library.BuildOnlyRust()
207 return module.Init()
208}
209
210// rust_ffi produces all ffi variants.
211func RustFFIFactory() android.Module {
212 module, library := NewRustLibrary(android.HostAndDeviceSupported)
213 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700214 return module.Init()
215}
216
217// rust_library_dylib produces a dylib.
218func RustLibraryDylibFactory() android.Module {
219 module, library := NewRustLibrary(android.HostAndDeviceSupported)
220 library.BuildOnlyDylib()
221 return module.Init()
222}
223
224// rust_library_rlib produces an rlib.
225func RustLibraryRlibFactory() android.Module {
226 module, library := NewRustLibrary(android.HostAndDeviceSupported)
227 library.BuildOnlyRlib()
228 return module.Init()
229}
230
Matthew Maurer2ae05132020-06-23 14:28:53 -0700231// rust_ffi_shared produces a shared library.
232func RustFFISharedFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700233 module, library := NewRustLibrary(android.HostAndDeviceSupported)
234 library.BuildOnlyShared()
235 return module.Init()
236}
237
Matthew Maurer2ae05132020-06-23 14:28:53 -0700238// rust_ffi_static produces a static library.
239func RustFFIStaticFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700240 module, library := NewRustLibrary(android.HostAndDeviceSupported)
241 library.BuildOnlyStatic()
242 return module.Init()
243}
244
Matthew Maurer2ae05132020-06-23 14:28:53 -0700245// rust_library_host produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700246func RustLibraryHostFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700247 module, library := NewRustLibrary(android.HostSupported)
248 library.BuildOnlyRust()
249 return module.Init()
250}
251
252// rust_ffi_host produces all FFI variants.
253func RustFFIHostFactory() android.Module {
254 module, library := NewRustLibrary(android.HostSupported)
255 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700256 return module.Init()
257}
258
259// rust_library_dylib_host produces a dylib.
260func RustLibraryDylibHostFactory() android.Module {
261 module, library := NewRustLibrary(android.HostSupported)
262 library.BuildOnlyDylib()
263 return module.Init()
264}
265
266// rust_library_rlib_host produces an rlib.
267func RustLibraryRlibHostFactory() android.Module {
268 module, library := NewRustLibrary(android.HostSupported)
269 library.BuildOnlyRlib()
270 return module.Init()
271}
272
Matthew Maurer2ae05132020-06-23 14:28:53 -0700273// rust_ffi_static_host produces a static library.
274func RustFFIStaticHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700275 module, library := NewRustLibrary(android.HostSupported)
276 library.BuildOnlyStatic()
277 return module.Init()
278}
279
Matthew Maurer2ae05132020-06-23 14:28:53 -0700280// rust_ffi_shared_host produces an shared library.
281func RustFFISharedHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700282 module, library := NewRustLibrary(android.HostSupported)
283 library.BuildOnlyShared()
284 return module.Init()
285}
286
Matthew Maurer2ae05132020-06-23 14:28:53 -0700287func (library *libraryDecorator) BuildOnlyFFI() {
288 library.MutatedProperties.BuildDylib = false
289 library.MutatedProperties.BuildRlib = false
290 library.MutatedProperties.BuildShared = true
291 library.MutatedProperties.BuildStatic = true
292}
293
294func (library *libraryDecorator) BuildOnlyRust() {
295 library.MutatedProperties.BuildDylib = true
296 library.MutatedProperties.BuildRlib = true
297 library.MutatedProperties.BuildShared = false
298 library.MutatedProperties.BuildStatic = false
299}
300
Ivan Lozanoffee3342019-08-27 12:03:00 -0700301func (library *libraryDecorator) BuildOnlyDylib() {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700302 library.MutatedProperties.BuildDylib = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700303 library.MutatedProperties.BuildRlib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700304 library.MutatedProperties.BuildShared = false
305 library.MutatedProperties.BuildStatic = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700306}
307
308func (library *libraryDecorator) BuildOnlyRlib() {
309 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700310 library.MutatedProperties.BuildRlib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700311 library.MutatedProperties.BuildShared = false
312 library.MutatedProperties.BuildStatic = false
313}
314
315func (library *libraryDecorator) BuildOnlyStatic() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700316 library.MutatedProperties.BuildRlib = false
317 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700318 library.MutatedProperties.BuildShared = false
319 library.MutatedProperties.BuildStatic = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700320}
321
322func (library *libraryDecorator) BuildOnlyShared() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700323 library.MutatedProperties.BuildRlib = false
324 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700325 library.MutatedProperties.BuildStatic = false
326 library.MutatedProperties.BuildShared = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700327}
328
329func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400330 module := newModule(hod, android.MultilibBoth)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700331
332 library := &libraryDecorator{
333 MutatedProperties: LibraryMutatedProperties{
Matthew Maurer2ae05132020-06-23 14:28:53 -0700334 BuildDylib: false,
335 BuildRlib: false,
336 BuildShared: false,
337 BuildStatic: false,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700338 },
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800339 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700340 }
341
342 module.compiler = library
343
344 return module, library
345}
346
347func (library *libraryDecorator) compilerProps() []interface{} {
348 return append(library.baseCompiler.compilerProps(),
349 &library.Properties,
350 &library.MutatedProperties)
351}
352
Ivan Lozanof1c84332019-09-20 11:00:37 -0700353func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
Ivan Lozano88f2b1c2019-11-21 14:35:20 -0800354
Ivan Lozano7e741cc2020-06-19 12:32:30 -0400355 // TODO(b/155498724) Remove if C static libraries no longer require libstd as an rlib dependency.
356 if !ctx.Host() && library.static() {
Ivan Lozano88f2b1c2019-11-21 14:35:20 -0800357 library.setNoStdlibs()
358 for _, stdlib := range config.Stdlibs {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700359 deps.Rlibs = append(deps.Rlibs, stdlib)
Ivan Lozano88f2b1c2019-11-21 14:35:20 -0800360 }
361 }
362
Ivan Lozanof1c84332019-09-20 11:00:37 -0700363 deps = library.baseCompiler.compilerDeps(ctx, deps)
364
Ivan Lozano52767be2019-10-18 14:49:46 -0700365 if ctx.toolchain().Bionic() && (library.dylib() || library.shared()) {
Ivan Lozanof1c84332019-09-20 11:00:37 -0700366 deps = library.baseCompiler.bionicDeps(ctx, deps)
Ivan Lozano12ee9ca2020-04-07 13:19:44 -0400367 deps.CrtBegin = "crtbegin_so"
368 deps.CrtEnd = "crtend_so"
Ivan Lozanof1c84332019-09-20 11:00:37 -0700369 }
370
371 return deps
372}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400373
374func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string {
375 return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
376}
377
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800378func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
ThiƩbaud Weksteen1f7f70f2020-06-24 11:32:48 +0200379 flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800380 flags = library.baseCompiler.compilerFlags(ctx, flags)
381 if library.shared() || library.static() {
382 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
383 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400384 if library.shared() {
385 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
386 }
387
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800388 return flags
389}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700390
Ivan Lozanoffee3342019-08-27 12:03:00 -0700391func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
392 var outputFile android.WritablePath
393
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400394 srcPath := srcPathFromModuleSrcs(ctx, library.baseCompiler.Properties.Srcs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700395
396 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
397
Matthew Maurer46c46cc2020-01-13 16:34:34 -0800398 if library.dylib() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700399 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
400 // https://github.com/rust-lang/rust/issues/19680
401 // https://github.com/rust-lang/rust/issues/34909
402 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
403 }
404
Ivan Lozanoffee3342019-08-27 12:03:00 -0700405 if library.rlib() {
406 fileName := library.getStem(ctx) + ctx.toolchain().RlibSuffix()
407 outputFile = android.PathForModuleOut(ctx, fileName)
408
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400409 outputs := TransformSrctoRlib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
410 library.coverageFile = outputs.coverageFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700411 } else if library.dylib() {
412 fileName := library.getStem(ctx) + ctx.toolchain().DylibSuffix()
413 outputFile = android.PathForModuleOut(ctx, fileName)
414
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400415 outputs := TransformSrctoDylib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
416 library.coverageFile = outputs.coverageFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700417 } else if library.static() {
418 fileName := library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
419 outputFile = android.PathForModuleOut(ctx, fileName)
420
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400421 outputs := TransformSrctoStatic(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
422 library.coverageFile = outputs.coverageFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700423 } else if library.shared() {
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400424 fileName := library.sharedLibFilename(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700425 outputFile = android.PathForModuleOut(ctx, fileName)
426
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400427 outputs := TransformSrctoShared(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
428 library.coverageFile = outputs.coverageFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700429 }
430
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400431 var coverageFiles android.Paths
432 if library.coverageFile != nil {
433 coverageFiles = append(coverageFiles, library.coverageFile)
434 }
435 if len(deps.coverageFiles) > 0 {
436 coverageFiles = append(coverageFiles, deps.coverageFiles...)
437 }
438 library.coverageOutputZipFile = TransformCoverageFilesToZip(ctx, coverageFiles, library.getStem(ctx))
439
Ivan Lozano52767be2019-10-18 14:49:46 -0700440 if library.rlib() || library.dylib() {
441 library.reexportDirs(deps.linkDirs...)
442 library.reexportDepFlags(deps.depFlags...)
443 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700444 library.unstrippedOutputFile = outputFile
445
446 return outputFile
447}
448
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700449func (library *libraryDecorator) getStem(ctx ModuleContext) string {
450 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
451 validateLibraryStem(ctx, stem, library.crateName())
452
453 return stem + String(library.baseCompiler.Properties.Suffix)
454}
455
456var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
457
458func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
459 if crate_name == "" {
460 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
461 }
462
463 // crate_names are used for the library output file, and rustc expects these
464 // to be alphanumeric with underscores allowed.
465 if validCrateName.MatchString(crate_name) {
466 ctx.PropertyErrorf("crate_name",
467 "library crate_names must be alphanumeric with underscores allowed")
468 }
469
470 // Libraries are expected to begin with "lib" followed by the crate_name
471 if !strings.HasPrefix(filename, "lib"+crate_name) {
472 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
473 }
474}
475
Ivan Lozanoffee3342019-08-27 12:03:00 -0700476func LibraryMutator(mctx android.BottomUpMutatorContext) {
477 if m, ok := mctx.Module().(*Module); ok && m.compiler != nil {
478 switch library := m.compiler.(type) {
479 case libraryInterface:
Ivan Lozanoffee3342019-08-27 12:03:00 -0700480
Ivan Lozano52767be2019-10-18 14:49:46 -0700481 // We only build the rust library variants here. This assumes that
482 // LinkageMutator runs first and there's an empty variant
483 // if rust variants are required.
484 if !library.static() && !library.shared() {
485 if library.buildRlib() && library.buildDylib() {
486 modules := mctx.CreateLocalVariations("rlib", "dylib")
487 rlib := modules[0].(*Module)
488 dylib := modules[1].(*Module)
489
490 rlib.compiler.(libraryInterface).setRlib()
491 dylib.compiler.(libraryInterface).setDylib()
492 } else if library.buildRlib() {
493 modules := mctx.CreateLocalVariations("rlib")
494 modules[0].(*Module).compiler.(libraryInterface).setRlib()
495 } else if library.buildDylib() {
496 modules := mctx.CreateLocalVariations("dylib")
497 modules[0].(*Module).compiler.(libraryInterface).setDylib()
498 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700499 }
500 }
501 }
502}