blob: a53df85d7c15afc4dbdd5f991d81057bff045524 [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
189var _ compiler = (*libraryDecorator)(nil)
Ivan Lozano52767be2019-10-18 14:49:46 -0700190var _ libraryInterface = (*libraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700191
Matthew Maurer2ae05132020-06-23 14:28:53 -0700192// rust_library produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700193func RustLibraryFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700194 module, library := NewRustLibrary(android.HostAndDeviceSupported)
195 library.BuildOnlyRust()
196 return module.Init()
197}
198
199// rust_ffi produces all ffi variants.
200func RustFFIFactory() android.Module {
201 module, library := NewRustLibrary(android.HostAndDeviceSupported)
202 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700203 return module.Init()
204}
205
206// rust_library_dylib produces a dylib.
207func RustLibraryDylibFactory() android.Module {
208 module, library := NewRustLibrary(android.HostAndDeviceSupported)
209 library.BuildOnlyDylib()
210 return module.Init()
211}
212
213// rust_library_rlib produces an rlib.
214func RustLibraryRlibFactory() android.Module {
215 module, library := NewRustLibrary(android.HostAndDeviceSupported)
216 library.BuildOnlyRlib()
217 return module.Init()
218}
219
Matthew Maurer2ae05132020-06-23 14:28:53 -0700220// rust_ffi_shared produces a shared library.
221func RustFFISharedFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700222 module, library := NewRustLibrary(android.HostAndDeviceSupported)
223 library.BuildOnlyShared()
224 return module.Init()
225}
226
Matthew Maurer2ae05132020-06-23 14:28:53 -0700227// rust_ffi_static produces a static library.
228func RustFFIStaticFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700229 module, library := NewRustLibrary(android.HostAndDeviceSupported)
230 library.BuildOnlyStatic()
231 return module.Init()
232}
233
Matthew Maurer2ae05132020-06-23 14:28:53 -0700234// rust_library_host produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700235func RustLibraryHostFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700236 module, library := NewRustLibrary(android.HostSupported)
237 library.BuildOnlyRust()
238 return module.Init()
239}
240
241// rust_ffi_host produces all FFI variants.
242func RustFFIHostFactory() android.Module {
243 module, library := NewRustLibrary(android.HostSupported)
244 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700245 return module.Init()
246}
247
248// rust_library_dylib_host produces a dylib.
249func RustLibraryDylibHostFactory() android.Module {
250 module, library := NewRustLibrary(android.HostSupported)
251 library.BuildOnlyDylib()
252 return module.Init()
253}
254
255// rust_library_rlib_host produces an rlib.
256func RustLibraryRlibHostFactory() android.Module {
257 module, library := NewRustLibrary(android.HostSupported)
258 library.BuildOnlyRlib()
259 return module.Init()
260}
261
Matthew Maurer2ae05132020-06-23 14:28:53 -0700262// rust_ffi_static_host produces a static library.
263func RustFFIStaticHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700264 module, library := NewRustLibrary(android.HostSupported)
265 library.BuildOnlyStatic()
266 return module.Init()
267}
268
Matthew Maurer2ae05132020-06-23 14:28:53 -0700269// rust_ffi_shared_host produces an shared library.
270func RustFFISharedHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700271 module, library := NewRustLibrary(android.HostSupported)
272 library.BuildOnlyShared()
273 return module.Init()
274}
275
Matthew Maurer2ae05132020-06-23 14:28:53 -0700276func (library *libraryDecorator) BuildOnlyFFI() {
277 library.MutatedProperties.BuildDylib = false
278 library.MutatedProperties.BuildRlib = false
279 library.MutatedProperties.BuildShared = true
280 library.MutatedProperties.BuildStatic = true
281}
282
283func (library *libraryDecorator) BuildOnlyRust() {
284 library.MutatedProperties.BuildDylib = true
285 library.MutatedProperties.BuildRlib = true
286 library.MutatedProperties.BuildShared = false
287 library.MutatedProperties.BuildStatic = false
288}
289
Ivan Lozanoffee3342019-08-27 12:03:00 -0700290func (library *libraryDecorator) BuildOnlyDylib() {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700291 library.MutatedProperties.BuildDylib = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700292 library.MutatedProperties.BuildRlib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700293 library.MutatedProperties.BuildShared = false
294 library.MutatedProperties.BuildStatic = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700295}
296
297func (library *libraryDecorator) BuildOnlyRlib() {
298 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700299 library.MutatedProperties.BuildRlib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700300 library.MutatedProperties.BuildShared = false
301 library.MutatedProperties.BuildStatic = false
302}
303
304func (library *libraryDecorator) BuildOnlyStatic() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700305 library.MutatedProperties.BuildRlib = false
306 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700307 library.MutatedProperties.BuildShared = false
308 library.MutatedProperties.BuildStatic = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700309}
310
311func (library *libraryDecorator) BuildOnlyShared() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700312 library.MutatedProperties.BuildRlib = false
313 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700314 library.MutatedProperties.BuildStatic = false
315 library.MutatedProperties.BuildShared = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700316}
317
318func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400319 module := newModule(hod, android.MultilibBoth)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700320
321 library := &libraryDecorator{
322 MutatedProperties: LibraryMutatedProperties{
Matthew Maurer2ae05132020-06-23 14:28:53 -0700323 BuildDylib: false,
324 BuildRlib: false,
325 BuildShared: false,
326 BuildStatic: false,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700327 },
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800328 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700329 }
330
331 module.compiler = library
332
333 return module, library
334}
335
336func (library *libraryDecorator) compilerProps() []interface{} {
337 return append(library.baseCompiler.compilerProps(),
338 &library.Properties,
339 &library.MutatedProperties)
340}
341
Ivan Lozanof1c84332019-09-20 11:00:37 -0700342func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
Ivan Lozano88f2b1c2019-11-21 14:35:20 -0800343
Ivan Lozano7e741cc2020-06-19 12:32:30 -0400344 // TODO(b/155498724) Remove if C static libraries no longer require libstd as an rlib dependency.
345 if !ctx.Host() && library.static() {
Ivan Lozano88f2b1c2019-11-21 14:35:20 -0800346 library.setNoStdlibs()
347 for _, stdlib := range config.Stdlibs {
348 deps.Rlibs = append(deps.Rlibs, stdlib+".static")
349 }
350 }
351
Ivan Lozanof1c84332019-09-20 11:00:37 -0700352 deps = library.baseCompiler.compilerDeps(ctx, deps)
353
Ivan Lozano52767be2019-10-18 14:49:46 -0700354 if ctx.toolchain().Bionic() && (library.dylib() || library.shared()) {
Ivan Lozanof1c84332019-09-20 11:00:37 -0700355 deps = library.baseCompiler.bionicDeps(ctx, deps)
Ivan Lozano12ee9ca2020-04-07 13:19:44 -0400356 deps.CrtBegin = "crtbegin_so"
357 deps.CrtEnd = "crtend_so"
Ivan Lozanof1c84332019-09-20 11:00:37 -0700358 }
359
360 return deps
361}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400362
363func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string {
364 return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
365}
366
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800367func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
ThiƩbaud Weksteen1f7f70f2020-06-24 11:32:48 +0200368 flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800369 flags = library.baseCompiler.compilerFlags(ctx, flags)
370 if library.shared() || library.static() {
371 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
372 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400373 if library.shared() {
374 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
375 }
376
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800377 return flags
378}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700379
Ivan Lozanoffee3342019-08-27 12:03:00 -0700380func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
381 var outputFile android.WritablePath
382
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400383 srcPath := srcPathFromModuleSrcs(ctx, library.baseCompiler.Properties.Srcs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700384
385 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
386
Matthew Maurer46c46cc2020-01-13 16:34:34 -0800387 if library.dylib() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700388 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
389 // https://github.com/rust-lang/rust/issues/19680
390 // https://github.com/rust-lang/rust/issues/34909
391 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
392 }
393
Ivan Lozanoffee3342019-08-27 12:03:00 -0700394 if library.rlib() {
395 fileName := library.getStem(ctx) + ctx.toolchain().RlibSuffix()
396 outputFile = android.PathForModuleOut(ctx, fileName)
397
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400398 outputs := TransformSrctoRlib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
399 library.coverageFile = outputs.coverageFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700400 } else if library.dylib() {
401 fileName := library.getStem(ctx) + ctx.toolchain().DylibSuffix()
402 outputFile = android.PathForModuleOut(ctx, fileName)
403
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400404 outputs := TransformSrctoDylib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
405 library.coverageFile = outputs.coverageFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700406 } else if library.static() {
407 fileName := library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
408 outputFile = android.PathForModuleOut(ctx, fileName)
409
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400410 outputs := TransformSrctoStatic(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
411 library.coverageFile = outputs.coverageFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700412 } else if library.shared() {
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400413 fileName := library.sharedLibFilename(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700414 outputFile = android.PathForModuleOut(ctx, fileName)
415
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400416 outputs := TransformSrctoShared(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
417 library.coverageFile = outputs.coverageFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700418 }
419
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400420 var coverageFiles android.Paths
421 if library.coverageFile != nil {
422 coverageFiles = append(coverageFiles, library.coverageFile)
423 }
424 if len(deps.coverageFiles) > 0 {
425 coverageFiles = append(coverageFiles, deps.coverageFiles...)
426 }
427 library.coverageOutputZipFile = TransformCoverageFilesToZip(ctx, coverageFiles, library.getStem(ctx))
428
Ivan Lozano52767be2019-10-18 14:49:46 -0700429 if library.rlib() || library.dylib() {
430 library.reexportDirs(deps.linkDirs...)
431 library.reexportDepFlags(deps.depFlags...)
432 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700433 library.unstrippedOutputFile = outputFile
434
435 return outputFile
436}
437
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700438func (library *libraryDecorator) getStem(ctx ModuleContext) string {
439 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
440 validateLibraryStem(ctx, stem, library.crateName())
441
442 return stem + String(library.baseCompiler.Properties.Suffix)
443}
444
445var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
446
447func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
448 if crate_name == "" {
449 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
450 }
451
452 // crate_names are used for the library output file, and rustc expects these
453 // to be alphanumeric with underscores allowed.
454 if validCrateName.MatchString(crate_name) {
455 ctx.PropertyErrorf("crate_name",
456 "library crate_names must be alphanumeric with underscores allowed")
457 }
458
459 // Libraries are expected to begin with "lib" followed by the crate_name
460 if !strings.HasPrefix(filename, "lib"+crate_name) {
461 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
462 }
463}
464
Ivan Lozanoffee3342019-08-27 12:03:00 -0700465func LibraryMutator(mctx android.BottomUpMutatorContext) {
466 if m, ok := mctx.Module().(*Module); ok && m.compiler != nil {
467 switch library := m.compiler.(type) {
468 case libraryInterface:
Ivan Lozanoffee3342019-08-27 12:03:00 -0700469
Ivan Lozano52767be2019-10-18 14:49:46 -0700470 // We only build the rust library variants here. This assumes that
471 // LinkageMutator runs first and there's an empty variant
472 // if rust variants are required.
473 if !library.static() && !library.shared() {
474 if library.buildRlib() && library.buildDylib() {
475 modules := mctx.CreateLocalVariations("rlib", "dylib")
476 rlib := modules[0].(*Module)
477 dylib := modules[1].(*Module)
478
479 rlib.compiler.(libraryInterface).setRlib()
480 dylib.compiler.(libraryInterface).setDylib()
481 } else if library.buildRlib() {
482 modules := mctx.CreateLocalVariations("rlib")
483 modules[0].(*Module).compiler.(libraryInterface).setRlib()
484 } else if library.buildDylib() {
485 modules := mctx.CreateLocalVariations("dylib")
486 modules[0].(*Module).compiler.(libraryInterface).setDylib()
487 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700488 }
489 }
490 }
491}