blob: 8b8e797a29b7ad7933d858217d45ecd9ab2befc1 [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"
22)
23
24func init() {
25 android.RegisterModuleType("rust_library", RustLibraryFactory)
26 android.RegisterModuleType("rust_library_dylib", RustLibraryDylibFactory)
27 android.RegisterModuleType("rust_library_rlib", RustLibraryRlibFactory)
28 android.RegisterModuleType("rust_library_host", RustLibraryHostFactory)
29 android.RegisterModuleType("rust_library_host_dylib", RustLibraryDylibHostFactory)
30 android.RegisterModuleType("rust_library_host_rlib", RustLibraryRlibHostFactory)
Matthew Maurer2ae05132020-06-23 14:28:53 -070031 android.RegisterModuleType("rust_ffi", RustFFIFactory)
32 android.RegisterModuleType("rust_ffi_shared", RustFFISharedFactory)
33 android.RegisterModuleType("rust_ffi_static", RustFFIStaticFactory)
34 android.RegisterModuleType("rust_ffi_host", RustFFIHostFactory)
35 android.RegisterModuleType("rust_ffi_host_shared", RustFFISharedHostFactory)
36 android.RegisterModuleType("rust_ffi_host_static", RustFFIStaticHostFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070037}
38
39type VariantLibraryProperties struct {
Matthew Maurerc761eec2020-06-25 00:47:46 -070040 Enabled *bool `android:"arch_variant"`
41 Srcs []string `android:"path,arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070042}
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
Matthew Maurerbb3add12020-06-25 09:34:12 -070076 *flagExporter
Ivan Lozanoffee3342019-08-27 12:03:00 -070077
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) rlib() bool {
115 return library.MutatedProperties.VariantIsRlib
116}
117
118func (library *libraryDecorator) dylib() bool {
119 return library.MutatedProperties.VariantIsDylib
120}
121
Ivan Lozano52767be2019-10-18 14:49:46 -0700122func (library *libraryDecorator) shared() bool {
123 return library.MutatedProperties.VariantIsShared
124}
125
126func (library *libraryDecorator) static() bool {
127 return library.MutatedProperties.VariantIsStatic
128}
129
Ivan Lozanoffee3342019-08-27 12:03:00 -0700130func (library *libraryDecorator) buildRlib() bool {
131 return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true)
132}
133
134func (library *libraryDecorator) buildDylib() bool {
135 return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true)
136}
137
Ivan Lozano52767be2019-10-18 14:49:46 -0700138func (library *libraryDecorator) buildShared() bool {
139 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
140}
141
142func (library *libraryDecorator) buildStatic() bool {
143 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
144}
145
Ivan Lozanoffee3342019-08-27 12:03:00 -0700146func (library *libraryDecorator) setRlib() {
147 library.MutatedProperties.VariantIsRlib = true
148 library.MutatedProperties.VariantIsDylib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700149 library.MutatedProperties.VariantIsStatic = false
150 library.MutatedProperties.VariantIsShared = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700151}
152
153func (library *libraryDecorator) setDylib() {
154 library.MutatedProperties.VariantIsRlib = false
155 library.MutatedProperties.VariantIsDylib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700156 library.MutatedProperties.VariantIsStatic = false
157 library.MutatedProperties.VariantIsShared = false
158}
159
160func (library *libraryDecorator) setShared() {
161 library.MutatedProperties.VariantIsStatic = false
162 library.MutatedProperties.VariantIsShared = true
163 library.MutatedProperties.VariantIsRlib = false
164 library.MutatedProperties.VariantIsDylib = false
165}
166
167func (library *libraryDecorator) setStatic() {
168 library.MutatedProperties.VariantIsStatic = true
169 library.MutatedProperties.VariantIsShared = false
170 library.MutatedProperties.VariantIsRlib = false
171 library.MutatedProperties.VariantIsDylib = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700172}
173
Matthew Maurer0f003b12020-06-29 14:34:06 -0700174func (library *libraryDecorator) autoDep() autoDep {
175 if library.rlib() || library.static() {
176 return rlibAutoDep
177 } else if library.dylib() || library.shared() {
178 return dylibAutoDep
179 } else {
180 return rlibAutoDep
181 }
182}
183
Ivan Lozanoffee3342019-08-27 12:03:00 -0700184var _ compiler = (*libraryDecorator)(nil)
Ivan Lozano52767be2019-10-18 14:49:46 -0700185var _ libraryInterface = (*libraryDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700186var _ exportedFlagsProducer = (*libraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700187
Matthew Maurer2ae05132020-06-23 14:28:53 -0700188// rust_library produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700189func RustLibraryFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700190 module, library := NewRustLibrary(android.HostAndDeviceSupported)
191 library.BuildOnlyRust()
192 return module.Init()
193}
194
195// rust_ffi produces all ffi variants.
196func RustFFIFactory() android.Module {
197 module, library := NewRustLibrary(android.HostAndDeviceSupported)
198 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700199 return module.Init()
200}
201
202// rust_library_dylib produces a dylib.
203func RustLibraryDylibFactory() android.Module {
204 module, library := NewRustLibrary(android.HostAndDeviceSupported)
205 library.BuildOnlyDylib()
206 return module.Init()
207}
208
209// rust_library_rlib produces an rlib.
210func RustLibraryRlibFactory() android.Module {
211 module, library := NewRustLibrary(android.HostAndDeviceSupported)
212 library.BuildOnlyRlib()
213 return module.Init()
214}
215
Matthew Maurer2ae05132020-06-23 14:28:53 -0700216// rust_ffi_shared produces a shared library.
217func RustFFISharedFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700218 module, library := NewRustLibrary(android.HostAndDeviceSupported)
219 library.BuildOnlyShared()
220 return module.Init()
221}
222
Matthew Maurer2ae05132020-06-23 14:28:53 -0700223// rust_ffi_static produces a static library.
224func RustFFIStaticFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700225 module, library := NewRustLibrary(android.HostAndDeviceSupported)
226 library.BuildOnlyStatic()
227 return module.Init()
228}
229
Matthew Maurer2ae05132020-06-23 14:28:53 -0700230// rust_library_host produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700231func RustLibraryHostFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700232 module, library := NewRustLibrary(android.HostSupported)
233 library.BuildOnlyRust()
234 return module.Init()
235}
236
237// rust_ffi_host produces all FFI variants.
238func RustFFIHostFactory() android.Module {
239 module, library := NewRustLibrary(android.HostSupported)
240 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700241 return module.Init()
242}
243
244// rust_library_dylib_host produces a dylib.
245func RustLibraryDylibHostFactory() android.Module {
246 module, library := NewRustLibrary(android.HostSupported)
247 library.BuildOnlyDylib()
248 return module.Init()
249}
250
251// rust_library_rlib_host produces an rlib.
252func RustLibraryRlibHostFactory() android.Module {
253 module, library := NewRustLibrary(android.HostSupported)
254 library.BuildOnlyRlib()
255 return module.Init()
256}
257
Matthew Maurer2ae05132020-06-23 14:28:53 -0700258// rust_ffi_static_host produces a static library.
259func RustFFIStaticHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700260 module, library := NewRustLibrary(android.HostSupported)
261 library.BuildOnlyStatic()
262 return module.Init()
263}
264
Matthew Maurer2ae05132020-06-23 14:28:53 -0700265// rust_ffi_shared_host produces an shared library.
266func RustFFISharedHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700267 module, library := NewRustLibrary(android.HostSupported)
268 library.BuildOnlyShared()
269 return module.Init()
270}
271
Matthew Maurer2ae05132020-06-23 14:28:53 -0700272func (library *libraryDecorator) BuildOnlyFFI() {
273 library.MutatedProperties.BuildDylib = false
274 library.MutatedProperties.BuildRlib = false
275 library.MutatedProperties.BuildShared = true
276 library.MutatedProperties.BuildStatic = true
277}
278
279func (library *libraryDecorator) BuildOnlyRust() {
280 library.MutatedProperties.BuildDylib = true
281 library.MutatedProperties.BuildRlib = true
282 library.MutatedProperties.BuildShared = false
283 library.MutatedProperties.BuildStatic = false
284}
285
Ivan Lozanoffee3342019-08-27 12:03:00 -0700286func (library *libraryDecorator) BuildOnlyDylib() {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700287 library.MutatedProperties.BuildDylib = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700288 library.MutatedProperties.BuildRlib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700289 library.MutatedProperties.BuildShared = false
290 library.MutatedProperties.BuildStatic = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700291}
292
293func (library *libraryDecorator) BuildOnlyRlib() {
294 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700295 library.MutatedProperties.BuildRlib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700296 library.MutatedProperties.BuildShared = false
297 library.MutatedProperties.BuildStatic = false
298}
299
300func (library *libraryDecorator) BuildOnlyStatic() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700301 library.MutatedProperties.BuildRlib = false
302 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700303 library.MutatedProperties.BuildShared = false
304 library.MutatedProperties.BuildStatic = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700305}
306
307func (library *libraryDecorator) BuildOnlyShared() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700308 library.MutatedProperties.BuildRlib = false
309 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700310 library.MutatedProperties.BuildStatic = false
311 library.MutatedProperties.BuildShared = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700312}
313
314func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400315 module := newModule(hod, android.MultilibBoth)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700316
317 library := &libraryDecorator{
318 MutatedProperties: LibraryMutatedProperties{
Matthew Maurer2ae05132020-06-23 14:28:53 -0700319 BuildDylib: false,
320 BuildRlib: false,
321 BuildShared: false,
322 BuildStatic: false,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700323 },
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800324 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Matthew Maurerbb3add12020-06-25 09:34:12 -0700325 flagExporter: NewFlagExporter(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700326 }
327
328 module.compiler = library
329
330 return module, library
331}
332
333func (library *libraryDecorator) compilerProps() []interface{} {
334 return append(library.baseCompiler.compilerProps(),
335 &library.Properties,
336 &library.MutatedProperties)
337}
338
Ivan Lozanof1c84332019-09-20 11:00:37 -0700339func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
340 deps = library.baseCompiler.compilerDeps(ctx, deps)
341
Ivan Lozano52767be2019-10-18 14:49:46 -0700342 if ctx.toolchain().Bionic() && (library.dylib() || library.shared()) {
Ivan Lozanof1c84332019-09-20 11:00:37 -0700343 deps = library.baseCompiler.bionicDeps(ctx, deps)
Ivan Lozano12ee9ca2020-04-07 13:19:44 -0400344 deps.CrtBegin = "crtbegin_so"
345 deps.CrtEnd = "crtend_so"
Ivan Lozanof1c84332019-09-20 11:00:37 -0700346 }
347
348 return deps
349}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400350
351func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string {
352 return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
353}
354
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800355func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
ThiƩbaud Weksteen1f7f70f2020-06-24 11:32:48 +0200356 flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800357 flags = library.baseCompiler.compilerFlags(ctx, flags)
358 if library.shared() || library.static() {
359 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
360 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400361 if library.shared() {
362 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
363 }
364
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800365 return flags
366}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700367
Ivan Lozanoffee3342019-08-27 12:03:00 -0700368func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
369 var outputFile android.WritablePath
370
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400371 srcPath := srcPathFromModuleSrcs(ctx, library.baseCompiler.Properties.Srcs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700372
373 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
374
Matthew Maurer46c46cc2020-01-13 16:34:34 -0800375 if library.dylib() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700376 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
377 // https://github.com/rust-lang/rust/issues/19680
378 // https://github.com/rust-lang/rust/issues/34909
379 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
380 }
381
Ivan Lozanoffee3342019-08-27 12:03:00 -0700382 if library.rlib() {
383 fileName := library.getStem(ctx) + ctx.toolchain().RlibSuffix()
384 outputFile = android.PathForModuleOut(ctx, fileName)
385
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400386 outputs := TransformSrctoRlib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
387 library.coverageFile = outputs.coverageFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700388 } else if library.dylib() {
389 fileName := library.getStem(ctx) + ctx.toolchain().DylibSuffix()
390 outputFile = android.PathForModuleOut(ctx, fileName)
391
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400392 outputs := TransformSrctoDylib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
393 library.coverageFile = outputs.coverageFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700394 } else if library.static() {
395 fileName := library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
396 outputFile = android.PathForModuleOut(ctx, fileName)
397
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400398 outputs := TransformSrctoStatic(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
399 library.coverageFile = outputs.coverageFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700400 } else if library.shared() {
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400401 fileName := library.sharedLibFilename(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700402 outputFile = android.PathForModuleOut(ctx, fileName)
403
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400404 outputs := TransformSrctoShared(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
405 library.coverageFile = outputs.coverageFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700406 }
407
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400408 var coverageFiles android.Paths
409 if library.coverageFile != nil {
410 coverageFiles = append(coverageFiles, library.coverageFile)
411 }
412 if len(deps.coverageFiles) > 0 {
413 coverageFiles = append(coverageFiles, deps.coverageFiles...)
414 }
415 library.coverageOutputZipFile = TransformCoverageFilesToZip(ctx, coverageFiles, library.getStem(ctx))
416
Ivan Lozano52767be2019-10-18 14:49:46 -0700417 if library.rlib() || library.dylib() {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700418 library.exportLinkDirs(deps.linkDirs...)
419 library.exportDepFlags(deps.depFlags...)
Ivan Lozano52767be2019-10-18 14:49:46 -0700420 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700421 library.unstrippedOutputFile = outputFile
422
423 return outputFile
424}
425
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700426func (library *libraryDecorator) getStem(ctx ModuleContext) string {
427 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
428 validateLibraryStem(ctx, stem, library.crateName())
429
430 return stem + String(library.baseCompiler.Properties.Suffix)
431}
432
433var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
434
435func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
436 if crate_name == "" {
437 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
438 }
439
440 // crate_names are used for the library output file, and rustc expects these
441 // to be alphanumeric with underscores allowed.
442 if validCrateName.MatchString(crate_name) {
443 ctx.PropertyErrorf("crate_name",
444 "library crate_names must be alphanumeric with underscores allowed")
445 }
446
447 // Libraries are expected to begin with "lib" followed by the crate_name
448 if !strings.HasPrefix(filename, "lib"+crate_name) {
449 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
450 }
451}
452
Ivan Lozanoffee3342019-08-27 12:03:00 -0700453func LibraryMutator(mctx android.BottomUpMutatorContext) {
454 if m, ok := mctx.Module().(*Module); ok && m.compiler != nil {
455 switch library := m.compiler.(type) {
456 case libraryInterface:
Ivan Lozanoffee3342019-08-27 12:03:00 -0700457
Ivan Lozano52767be2019-10-18 14:49:46 -0700458 // We only build the rust library variants here. This assumes that
459 // LinkageMutator runs first and there's an empty variant
460 // if rust variants are required.
461 if !library.static() && !library.shared() {
462 if library.buildRlib() && library.buildDylib() {
463 modules := mctx.CreateLocalVariations("rlib", "dylib")
464 rlib := modules[0].(*Module)
465 dylib := modules[1].(*Module)
466
467 rlib.compiler.(libraryInterface).setRlib()
468 dylib.compiler.(libraryInterface).setDylib()
469 } else if library.buildRlib() {
470 modules := mctx.CreateLocalVariations("rlib")
471 modules[0].(*Module).compiler.(libraryInterface).setRlib()
472 } else if library.buildDylib() {
473 modules := mctx.CreateLocalVariations("dylib")
474 modules[0].(*Module).compiler.(libraryInterface).setDylib()
475 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700476 }
477 }
478 }
479}