blob: a44293307a8d8d6fe89ca41b5c10ae8760f32425 [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 Lozano26ecd6c2020-07-31 13:40:31 -040072
73 // This variant is disabled and should not be compiled
74 // (used for SourceProvider variants that produce only source)
75 VariantIsDisabled bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070076}
77
78type libraryDecorator struct {
79 *baseCompiler
Matthew Maurerbb3add12020-06-25 09:34:12 -070080 *flagExporter
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +020081 stripper Stripper
Ivan Lozanoffee3342019-08-27 12:03:00 -070082
Ivan Lozano8a23fa42020-06-16 10:26:57 -040083 Properties LibraryCompilerProperties
84 MutatedProperties LibraryMutatedProperties
85 includeDirs android.Paths
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040086 sourceProvider SourceProvider
Ivan Lozanoffee3342019-08-27 12:03:00 -070087}
88
89type libraryInterface interface {
90 rlib() bool
91 dylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -070092 static() bool
93 shared() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070094
95 // Returns true if the build options for the module have selected a particular build type
96 buildRlib() bool
97 buildDylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -070098 buildShared() bool
99 buildStatic() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700100
101 // Sets a particular variant type
102 setRlib()
103 setDylib()
Ivan Lozano52767be2019-10-18 14:49:46 -0700104 setShared()
105 setStatic()
106
107 // Build a specific library variant
Matthew Maurer2ae05132020-06-23 14:28:53 -0700108 BuildOnlyFFI()
109 BuildOnlyRust()
Ivan Lozano52767be2019-10-18 14:49:46 -0700110 BuildOnlyRlib()
111 BuildOnlyDylib()
112 BuildOnlyStatic()
113 BuildOnlyShared()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700114}
115
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400116func (library *libraryDecorator) nativeCoverage() bool {
117 return true
118}
119
Ivan Lozanoffee3342019-08-27 12:03:00 -0700120func (library *libraryDecorator) rlib() bool {
121 return library.MutatedProperties.VariantIsRlib
122}
123
124func (library *libraryDecorator) dylib() bool {
125 return library.MutatedProperties.VariantIsDylib
126}
127
Ivan Lozano52767be2019-10-18 14:49:46 -0700128func (library *libraryDecorator) shared() bool {
129 return library.MutatedProperties.VariantIsShared
130}
131
132func (library *libraryDecorator) static() bool {
133 return library.MutatedProperties.VariantIsStatic
134}
135
Ivan Lozanoffee3342019-08-27 12:03:00 -0700136func (library *libraryDecorator) buildRlib() bool {
137 return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true)
138}
139
140func (library *libraryDecorator) buildDylib() bool {
141 return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true)
142}
143
Ivan Lozano52767be2019-10-18 14:49:46 -0700144func (library *libraryDecorator) buildShared() bool {
145 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
146}
147
148func (library *libraryDecorator) buildStatic() bool {
149 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
150}
151
Ivan Lozanoffee3342019-08-27 12:03:00 -0700152func (library *libraryDecorator) setRlib() {
153 library.MutatedProperties.VariantIsRlib = true
154 library.MutatedProperties.VariantIsDylib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700155 library.MutatedProperties.VariantIsStatic = false
156 library.MutatedProperties.VariantIsShared = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700157}
158
159func (library *libraryDecorator) setDylib() {
160 library.MutatedProperties.VariantIsRlib = false
161 library.MutatedProperties.VariantIsDylib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700162 library.MutatedProperties.VariantIsStatic = false
163 library.MutatedProperties.VariantIsShared = false
164}
165
166func (library *libraryDecorator) setShared() {
167 library.MutatedProperties.VariantIsStatic = false
168 library.MutatedProperties.VariantIsShared = true
169 library.MutatedProperties.VariantIsRlib = false
170 library.MutatedProperties.VariantIsDylib = false
171}
172
173func (library *libraryDecorator) setStatic() {
174 library.MutatedProperties.VariantIsStatic = true
175 library.MutatedProperties.VariantIsShared = false
176 library.MutatedProperties.VariantIsRlib = false
177 library.MutatedProperties.VariantIsDylib = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700178}
179
Ivan Lozano042504f2020-08-18 14:31:23 -0400180func (library *libraryDecorator) autoDep(ctx BaseModuleContext) autoDep {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700181 if library.rlib() || library.static() {
182 return rlibAutoDep
183 } else if library.dylib() || library.shared() {
184 return dylibAutoDep
185 } else {
Ivan Lozano042504f2020-08-18 14:31:23 -0400186 panic("autoDep called on library" + ctx.ModuleName() + "that has no enabled variants.")
Matthew Maurer0f003b12020-06-29 14:34:06 -0700187 }
188}
189
Ivan Lozanoffee3342019-08-27 12:03:00 -0700190var _ compiler = (*libraryDecorator)(nil)
Ivan Lozano52767be2019-10-18 14:49:46 -0700191var _ libraryInterface = (*libraryDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700192var _ exportedFlagsProducer = (*libraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700193
Matthew Maurer2ae05132020-06-23 14:28:53 -0700194// rust_library produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700195func RustLibraryFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700196 module, library := NewRustLibrary(android.HostAndDeviceSupported)
197 library.BuildOnlyRust()
198 return module.Init()
199}
200
201// rust_ffi produces all ffi variants.
202func RustFFIFactory() android.Module {
203 module, library := NewRustLibrary(android.HostAndDeviceSupported)
204 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700205 return module.Init()
206}
207
208// rust_library_dylib produces a dylib.
209func RustLibraryDylibFactory() android.Module {
210 module, library := NewRustLibrary(android.HostAndDeviceSupported)
211 library.BuildOnlyDylib()
212 return module.Init()
213}
214
215// rust_library_rlib produces an rlib.
216func RustLibraryRlibFactory() android.Module {
217 module, library := NewRustLibrary(android.HostAndDeviceSupported)
218 library.BuildOnlyRlib()
219 return module.Init()
220}
221
Matthew Maurer2ae05132020-06-23 14:28:53 -0700222// rust_ffi_shared produces a shared library.
223func RustFFISharedFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700224 module, library := NewRustLibrary(android.HostAndDeviceSupported)
225 library.BuildOnlyShared()
226 return module.Init()
227}
228
Matthew Maurer2ae05132020-06-23 14:28:53 -0700229// rust_ffi_static produces a static library.
230func RustFFIStaticFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700231 module, library := NewRustLibrary(android.HostAndDeviceSupported)
232 library.BuildOnlyStatic()
233 return module.Init()
234}
235
Matthew Maurer2ae05132020-06-23 14:28:53 -0700236// rust_library_host produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700237func RustLibraryHostFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700238 module, library := NewRustLibrary(android.HostSupported)
239 library.BuildOnlyRust()
240 return module.Init()
241}
242
243// rust_ffi_host produces all FFI variants.
244func RustFFIHostFactory() android.Module {
245 module, library := NewRustLibrary(android.HostSupported)
246 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700247 return module.Init()
248}
249
250// rust_library_dylib_host produces a dylib.
251func RustLibraryDylibHostFactory() android.Module {
252 module, library := NewRustLibrary(android.HostSupported)
253 library.BuildOnlyDylib()
254 return module.Init()
255}
256
257// rust_library_rlib_host produces an rlib.
258func RustLibraryRlibHostFactory() android.Module {
259 module, library := NewRustLibrary(android.HostSupported)
260 library.BuildOnlyRlib()
261 return module.Init()
262}
263
Matthew Maurer2ae05132020-06-23 14:28:53 -0700264// rust_ffi_static_host produces a static library.
265func RustFFIStaticHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700266 module, library := NewRustLibrary(android.HostSupported)
267 library.BuildOnlyStatic()
268 return module.Init()
269}
270
Matthew Maurer2ae05132020-06-23 14:28:53 -0700271// rust_ffi_shared_host produces an shared library.
272func RustFFISharedHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700273 module, library := NewRustLibrary(android.HostSupported)
274 library.BuildOnlyShared()
275 return module.Init()
276}
277
Matthew Maurer2ae05132020-06-23 14:28:53 -0700278func (library *libraryDecorator) BuildOnlyFFI() {
279 library.MutatedProperties.BuildDylib = false
280 library.MutatedProperties.BuildRlib = false
281 library.MutatedProperties.BuildShared = true
282 library.MutatedProperties.BuildStatic = true
283}
284
285func (library *libraryDecorator) BuildOnlyRust() {
286 library.MutatedProperties.BuildDylib = true
287 library.MutatedProperties.BuildRlib = true
288 library.MutatedProperties.BuildShared = false
289 library.MutatedProperties.BuildStatic = false
290}
291
Ivan Lozanoffee3342019-08-27 12:03:00 -0700292func (library *libraryDecorator) BuildOnlyDylib() {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700293 library.MutatedProperties.BuildDylib = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700294 library.MutatedProperties.BuildRlib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700295 library.MutatedProperties.BuildShared = false
296 library.MutatedProperties.BuildStatic = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700297}
298
299func (library *libraryDecorator) BuildOnlyRlib() {
300 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700301 library.MutatedProperties.BuildRlib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700302 library.MutatedProperties.BuildShared = false
303 library.MutatedProperties.BuildStatic = false
304}
305
306func (library *libraryDecorator) BuildOnlyStatic() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700307 library.MutatedProperties.BuildRlib = false
308 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700309 library.MutatedProperties.BuildShared = false
310 library.MutatedProperties.BuildStatic = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700311}
312
313func (library *libraryDecorator) BuildOnlyShared() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700314 library.MutatedProperties.BuildRlib = false
315 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700316 library.MutatedProperties.BuildStatic = false
317 library.MutatedProperties.BuildShared = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700318}
319
320func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400321 module := newModule(hod, android.MultilibBoth)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700322
323 library := &libraryDecorator{
324 MutatedProperties: LibraryMutatedProperties{
Matthew Maurer2ae05132020-06-23 14:28:53 -0700325 BuildDylib: false,
326 BuildRlib: false,
327 BuildShared: false,
328 BuildStatic: false,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700329 },
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800330 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Matthew Maurerbb3add12020-06-25 09:34:12 -0700331 flagExporter: NewFlagExporter(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700332 }
333
334 module.compiler = library
335
336 return module, library
337}
338
339func (library *libraryDecorator) compilerProps() []interface{} {
340 return append(library.baseCompiler.compilerProps(),
341 &library.Properties,
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200342 &library.MutatedProperties,
343 &library.stripper.StripProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700344}
345
Ivan Lozanof1c84332019-09-20 11:00:37 -0700346func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
347 deps = library.baseCompiler.compilerDeps(ctx, deps)
348
Ivan Lozano52767be2019-10-18 14:49:46 -0700349 if ctx.toolchain().Bionic() && (library.dylib() || library.shared()) {
Ivan Lozano45901ed2020-07-24 16:05:01 -0400350 deps = bionicDeps(deps)
Ivan Lozano12ee9ca2020-04-07 13:19:44 -0400351 deps.CrtBegin = "crtbegin_so"
352 deps.CrtEnd = "crtend_so"
Ivan Lozanof1c84332019-09-20 11:00:37 -0700353 }
354
355 return deps
356}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400357
358func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string {
359 return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
360}
361
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800362func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200363 flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800364 flags = library.baseCompiler.compilerFlags(ctx, flags)
365 if library.shared() || library.static() {
366 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
367 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400368 if library.shared() {
369 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
370 }
371
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800372 return flags
373}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700374
Ivan Lozanoffee3342019-08-27 12:03:00 -0700375func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200376 var outputFile android.ModuleOutPath
377 var fileName string
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400378 var srcPath android.Path
Ivan Lozanoffee3342019-08-27 12:03:00 -0700379
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400380 if library.sourceProvider != nil {
381 srcPath = library.sourceProvider.Srcs()[0]
382 } else {
383 srcPath, _ = srcPathFromModuleSrcs(ctx, library.baseCompiler.Properties.Srcs)
384 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700385
386 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano2093af22020-08-25 12:48:19 -0400387 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700388
Matthew Maurer46c46cc2020-01-13 16:34:34 -0800389 if library.dylib() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700390 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
391 // https://github.com/rust-lang/rust/issues/19680
392 // https://github.com/rust-lang/rust/issues/34909
393 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
394 }
395
Ivan Lozanoffee3342019-08-27 12:03:00 -0700396 if library.rlib() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200397 fileName = library.getStem(ctx) + ctx.toolchain().RlibSuffix()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700398 outputFile = android.PathForModuleOut(ctx, fileName)
399
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400400 outputs := TransformSrctoRlib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
401 library.coverageFile = outputs.coverageFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700402 } else if library.dylib() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200403 fileName = library.getStem(ctx) + ctx.toolchain().DylibSuffix()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700404 outputFile = android.PathForModuleOut(ctx, fileName)
405
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400406 outputs := TransformSrctoDylib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
407 library.coverageFile = outputs.coverageFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700408 } else if library.static() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200409 fileName = library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
Ivan Lozano52767be2019-10-18 14:49:46 -0700410 outputFile = android.PathForModuleOut(ctx, fileName)
411
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400412 outputs := TransformSrctoStatic(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
413 library.coverageFile = outputs.coverageFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700414 } else if library.shared() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200415 fileName = library.sharedLibFilename(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700416 outputFile = android.PathForModuleOut(ctx, fileName)
417
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400418 outputs := TransformSrctoShared(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
419 library.coverageFile = outputs.coverageFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700420 }
421
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200422 if !library.rlib() && library.stripper.NeedsStrip(ctx) {
423 strippedOutputFile := android.PathForModuleOut(ctx, "stripped", fileName)
424 library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
425 library.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
426 }
427
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400428 var coverageFiles android.Paths
429 if library.coverageFile != nil {
430 coverageFiles = append(coverageFiles, library.coverageFile)
431 }
432 if len(deps.coverageFiles) > 0 {
433 coverageFiles = append(coverageFiles, deps.coverageFiles...)
434 }
435 library.coverageOutputZipFile = TransformCoverageFilesToZip(ctx, coverageFiles, library.getStem(ctx))
436
Ivan Lozano52767be2019-10-18 14:49:46 -0700437 if library.rlib() || library.dylib() {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700438 library.exportLinkDirs(deps.linkDirs...)
439 library.exportDepFlags(deps.depFlags...)
Ivan Lozano2093af22020-08-25 12:48:19 -0400440 library.exportLinkObjects(deps.linkObjects...)
Ivan Lozano52767be2019-10-18 14:49:46 -0700441 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700442
443 return outputFile
444}
445
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700446func (library *libraryDecorator) getStem(ctx ModuleContext) string {
447 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
448 validateLibraryStem(ctx, stem, library.crateName())
449
450 return stem + String(library.baseCompiler.Properties.Suffix)
451}
452
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400453func (library *libraryDecorator) Disabled() bool {
454 return library.MutatedProperties.VariantIsDisabled
455}
456
457func (library *libraryDecorator) SetDisabled() {
458 library.MutatedProperties.VariantIsDisabled = true
459}
460
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700461var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
462
463func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
464 if crate_name == "" {
465 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
466 }
467
468 // crate_names are used for the library output file, and rustc expects these
469 // to be alphanumeric with underscores allowed.
470 if validCrateName.MatchString(crate_name) {
471 ctx.PropertyErrorf("crate_name",
472 "library crate_names must be alphanumeric with underscores allowed")
473 }
474
475 // Libraries are expected to begin with "lib" followed by the crate_name
476 if !strings.HasPrefix(filename, "lib"+crate_name) {
477 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
478 }
479}
480
Ivan Lozanoffee3342019-08-27 12:03:00 -0700481func LibraryMutator(mctx android.BottomUpMutatorContext) {
482 if m, ok := mctx.Module().(*Module); ok && m.compiler != nil {
483 switch library := m.compiler.(type) {
484 case libraryInterface:
Ivan Lozano89435d12020-07-31 11:01:18 -0400485 if library.buildRlib() && library.buildDylib() {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400486 variants := []string{"rlib", "dylib"}
487 if m.sourceProvider != nil {
488 variants = append(variants, "")
489 }
490 modules := mctx.CreateLocalVariations(variants...)
491
Ivan Lozano89435d12020-07-31 11:01:18 -0400492 rlib := modules[0].(*Module)
493 dylib := modules[1].(*Module)
Ivan Lozano89435d12020-07-31 11:01:18 -0400494 rlib.compiler.(libraryInterface).setRlib()
495 dylib.compiler.(libraryInterface).setDylib()
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400496
497 if m.sourceProvider != nil {
498 // This library is SourceProvider generated, so the non-library-producing
499 // variant needs to disable it's compiler and skip installation.
500 sourceProvider := modules[2].(*Module)
501 sourceProvider.compiler.SetDisabled()
502 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400503 } else if library.buildRlib() {
504 modules := mctx.CreateLocalVariations("rlib")
505 modules[0].(*Module).compiler.(libraryInterface).setRlib()
506 } else if library.buildDylib() {
507 modules := mctx.CreateLocalVariations("dylib")
508 modules[0].(*Module).compiler.(libraryInterface).setDylib()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700509 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400510
511 if m.sourceProvider != nil {
512 // Alias the non-library variant to the empty-string variant.
513 mctx.AliasVariation("")
514 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700515 }
516 }
517}