blob: 4931f1995325eef0cf6fdcca6d57d9e6d91dae93 [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
Ivan Lozano2b081132020-09-08 12:46:52 -040024var (
25 DylibStdlibSuffix = ".dylib-std"
26 RlibStdlibSuffix = ".rlib-std"
27)
28
Ivan Lozanoffee3342019-08-27 12:03:00 -070029func init() {
30 android.RegisterModuleType("rust_library", RustLibraryFactory)
31 android.RegisterModuleType("rust_library_dylib", RustLibraryDylibFactory)
32 android.RegisterModuleType("rust_library_rlib", RustLibraryRlibFactory)
33 android.RegisterModuleType("rust_library_host", RustLibraryHostFactory)
34 android.RegisterModuleType("rust_library_host_dylib", RustLibraryDylibHostFactory)
35 android.RegisterModuleType("rust_library_host_rlib", RustLibraryRlibHostFactory)
Matthew Maurer2ae05132020-06-23 14:28:53 -070036 android.RegisterModuleType("rust_ffi", RustFFIFactory)
37 android.RegisterModuleType("rust_ffi_shared", RustFFISharedFactory)
38 android.RegisterModuleType("rust_ffi_static", RustFFIStaticFactory)
39 android.RegisterModuleType("rust_ffi_host", RustFFIHostFactory)
40 android.RegisterModuleType("rust_ffi_host_shared", RustFFISharedHostFactory)
41 android.RegisterModuleType("rust_ffi_host_static", RustFFIStaticHostFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070042}
43
44type VariantLibraryProperties struct {
Matthew Maurerc761eec2020-06-25 00:47:46 -070045 Enabled *bool `android:"arch_variant"`
46 Srcs []string `android:"path,arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070047}
48
49type LibraryCompilerProperties struct {
Ivan Lozano52767be2019-10-18 14:49:46 -070050 Rlib VariantLibraryProperties `android:"arch_variant"`
51 Dylib VariantLibraryProperties `android:"arch_variant"`
52 Shared VariantLibraryProperties `android:"arch_variant"`
53 Static VariantLibraryProperties `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070054
Ivan Lozano52767be2019-10-18 14:49:46 -070055 // path to include directories to pass to cc_* modules, only relevant for static/shared variants.
56 Include_dirs []string `android:"path,arch_variant"`
Ivan Lozano2b081132020-09-08 12:46:52 -040057
58 // Whether this library is part of the Rust toolchain sysroot.
59 Sysroot *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070060}
61
62type LibraryMutatedProperties struct {
Ivan Lozanoffee3342019-08-27 12:03:00 -070063 // Build a dylib variant
64 BuildDylib bool `blueprint:"mutated"`
65 // Build an rlib variant
66 BuildRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070067 // Build a shared library variant
68 BuildShared bool `blueprint:"mutated"`
69 // Build a static library variant
70 BuildStatic bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070071
72 // This variant is a dylib
73 VariantIsDylib bool `blueprint:"mutated"`
74 // This variant is an rlib
75 VariantIsRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070076 // This variant is a shared library
77 VariantIsShared bool `blueprint:"mutated"`
78 // This variant is a static library
79 VariantIsStatic bool `blueprint:"mutated"`
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040080
81 // This variant is disabled and should not be compiled
82 // (used for SourceProvider variants that produce only source)
83 VariantIsDisabled bool `blueprint:"mutated"`
Ivan Lozano2b081132020-09-08 12:46:52 -040084
85 // Whether this library variant should be link libstd via rlibs
86 VariantIsStaticStd bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070087}
88
89type libraryDecorator struct {
90 *baseCompiler
Matthew Maurerbb3add12020-06-25 09:34:12 -070091 *flagExporter
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +020092 stripper Stripper
Ivan Lozanoffee3342019-08-27 12:03:00 -070093
Ivan Lozano8a23fa42020-06-16 10:26:57 -040094 Properties LibraryCompilerProperties
95 MutatedProperties LibraryMutatedProperties
96 includeDirs android.Paths
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040097 sourceProvider SourceProvider
Ivan Lozanoffee3342019-08-27 12:03:00 -070098}
99
100type libraryInterface interface {
101 rlib() bool
102 dylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700103 static() bool
104 shared() bool
Ivan Lozano2b081132020-09-08 12:46:52 -0400105 sysroot() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700106
107 // Returns true if the build options for the module have selected a particular build type
108 buildRlib() bool
109 buildDylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700110 buildShared() bool
111 buildStatic() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700112
113 // Sets a particular variant type
114 setRlib()
115 setDylib()
Ivan Lozano52767be2019-10-18 14:49:46 -0700116 setShared()
117 setStatic()
118
Ivan Lozano2b081132020-09-08 12:46:52 -0400119 // Set libstd linkage
120 setRlibStd()
121 setDylibStd()
122
Ivan Lozano52767be2019-10-18 14:49:46 -0700123 // Build a specific library variant
Matthew Maurer2ae05132020-06-23 14:28:53 -0700124 BuildOnlyFFI()
125 BuildOnlyRust()
Ivan Lozano52767be2019-10-18 14:49:46 -0700126 BuildOnlyRlib()
127 BuildOnlyDylib()
128 BuildOnlyStatic()
129 BuildOnlyShared()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700130}
131
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400132func (library *libraryDecorator) nativeCoverage() bool {
133 return true
134}
135
Ivan Lozanoffee3342019-08-27 12:03:00 -0700136func (library *libraryDecorator) rlib() bool {
137 return library.MutatedProperties.VariantIsRlib
138}
139
Ivan Lozano2b081132020-09-08 12:46:52 -0400140func (library *libraryDecorator) sysroot() bool {
141 return Bool(library.Properties.Sysroot)
142}
143
Ivan Lozanoffee3342019-08-27 12:03:00 -0700144func (library *libraryDecorator) dylib() bool {
145 return library.MutatedProperties.VariantIsDylib
146}
147
Ivan Lozano52767be2019-10-18 14:49:46 -0700148func (library *libraryDecorator) shared() bool {
149 return library.MutatedProperties.VariantIsShared
150}
151
152func (library *libraryDecorator) static() bool {
153 return library.MutatedProperties.VariantIsStatic
154}
155
Ivan Lozano2b081132020-09-08 12:46:52 -0400156func (library *libraryDecorator) staticStd(ctx *depsContext) bool {
157 // libraries should only request the staticStd when building a static FFI or when variant is staticStd
158 return library.static() || library.MutatedProperties.VariantIsStaticStd
159}
160
Ivan Lozanoffee3342019-08-27 12:03:00 -0700161func (library *libraryDecorator) buildRlib() bool {
162 return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true)
163}
164
165func (library *libraryDecorator) buildDylib() bool {
166 return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true)
167}
168
Ivan Lozano52767be2019-10-18 14:49:46 -0700169func (library *libraryDecorator) buildShared() bool {
170 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
171}
172
173func (library *libraryDecorator) buildStatic() bool {
174 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
175}
176
Ivan Lozanoffee3342019-08-27 12:03:00 -0700177func (library *libraryDecorator) setRlib() {
178 library.MutatedProperties.VariantIsRlib = true
179 library.MutatedProperties.VariantIsDylib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700180 library.MutatedProperties.VariantIsStatic = false
181 library.MutatedProperties.VariantIsShared = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700182}
183
184func (library *libraryDecorator) setDylib() {
185 library.MutatedProperties.VariantIsRlib = false
186 library.MutatedProperties.VariantIsDylib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700187 library.MutatedProperties.VariantIsStatic = false
188 library.MutatedProperties.VariantIsShared = false
189}
190
Ivan Lozano2b081132020-09-08 12:46:52 -0400191func (library *libraryDecorator) setRlibStd() {
192 library.MutatedProperties.VariantIsStaticStd = true
193}
194
195func (library *libraryDecorator) setDylibStd() {
196 library.MutatedProperties.VariantIsStaticStd = false
197}
198
Ivan Lozano52767be2019-10-18 14:49:46 -0700199func (library *libraryDecorator) setShared() {
200 library.MutatedProperties.VariantIsStatic = false
201 library.MutatedProperties.VariantIsShared = true
202 library.MutatedProperties.VariantIsRlib = false
203 library.MutatedProperties.VariantIsDylib = false
204}
205
206func (library *libraryDecorator) setStatic() {
207 library.MutatedProperties.VariantIsStatic = true
208 library.MutatedProperties.VariantIsShared = false
209 library.MutatedProperties.VariantIsRlib = false
210 library.MutatedProperties.VariantIsDylib = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700211}
212
Ivan Lozano042504f2020-08-18 14:31:23 -0400213func (library *libraryDecorator) autoDep(ctx BaseModuleContext) autoDep {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700214 if library.rlib() || library.static() {
215 return rlibAutoDep
216 } else if library.dylib() || library.shared() {
217 return dylibAutoDep
218 } else {
Ivan Lozano042504f2020-08-18 14:31:23 -0400219 panic("autoDep called on library" + ctx.ModuleName() + "that has no enabled variants.")
Matthew Maurer0f003b12020-06-29 14:34:06 -0700220 }
221}
222
Ivan Lozanoffee3342019-08-27 12:03:00 -0700223var _ compiler = (*libraryDecorator)(nil)
Ivan Lozano52767be2019-10-18 14:49:46 -0700224var _ libraryInterface = (*libraryDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700225var _ exportedFlagsProducer = (*libraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700226
Matthew Maurer2ae05132020-06-23 14:28:53 -0700227// rust_library produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700228func RustLibraryFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700229 module, library := NewRustLibrary(android.HostAndDeviceSupported)
230 library.BuildOnlyRust()
231 return module.Init()
232}
233
234// rust_ffi produces all ffi variants.
235func RustFFIFactory() android.Module {
236 module, library := NewRustLibrary(android.HostAndDeviceSupported)
237 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700238 return module.Init()
239}
240
241// rust_library_dylib produces a dylib.
242func RustLibraryDylibFactory() android.Module {
243 module, library := NewRustLibrary(android.HostAndDeviceSupported)
244 library.BuildOnlyDylib()
245 return module.Init()
246}
247
248// rust_library_rlib produces an rlib.
249func RustLibraryRlibFactory() android.Module {
250 module, library := NewRustLibrary(android.HostAndDeviceSupported)
251 library.BuildOnlyRlib()
252 return module.Init()
253}
254
Matthew Maurer2ae05132020-06-23 14:28:53 -0700255// rust_ffi_shared produces a shared library.
256func RustFFISharedFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700257 module, library := NewRustLibrary(android.HostAndDeviceSupported)
258 library.BuildOnlyShared()
259 return module.Init()
260}
261
Matthew Maurer2ae05132020-06-23 14:28:53 -0700262// rust_ffi_static produces a static library.
263func RustFFIStaticFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700264 module, library := NewRustLibrary(android.HostAndDeviceSupported)
265 library.BuildOnlyStatic()
266 return module.Init()
267}
268
Matthew Maurer2ae05132020-06-23 14:28:53 -0700269// rust_library_host produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700270func RustLibraryHostFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700271 module, library := NewRustLibrary(android.HostSupported)
272 library.BuildOnlyRust()
273 return module.Init()
274}
275
276// rust_ffi_host produces all FFI variants.
277func RustFFIHostFactory() android.Module {
278 module, library := NewRustLibrary(android.HostSupported)
279 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700280 return module.Init()
281}
282
283// rust_library_dylib_host produces a dylib.
284func RustLibraryDylibHostFactory() android.Module {
285 module, library := NewRustLibrary(android.HostSupported)
286 library.BuildOnlyDylib()
287 return module.Init()
288}
289
290// rust_library_rlib_host produces an rlib.
291func RustLibraryRlibHostFactory() android.Module {
292 module, library := NewRustLibrary(android.HostSupported)
293 library.BuildOnlyRlib()
294 return module.Init()
295}
296
Matthew Maurer2ae05132020-06-23 14:28:53 -0700297// rust_ffi_static_host produces a static library.
298func RustFFIStaticHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700299 module, library := NewRustLibrary(android.HostSupported)
300 library.BuildOnlyStatic()
301 return module.Init()
302}
303
Matthew Maurer2ae05132020-06-23 14:28:53 -0700304// rust_ffi_shared_host produces an shared library.
305func RustFFISharedHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700306 module, library := NewRustLibrary(android.HostSupported)
307 library.BuildOnlyShared()
308 return module.Init()
309}
310
Matthew Maurer2ae05132020-06-23 14:28:53 -0700311func (library *libraryDecorator) BuildOnlyFFI() {
312 library.MutatedProperties.BuildDylib = false
313 library.MutatedProperties.BuildRlib = false
314 library.MutatedProperties.BuildShared = true
315 library.MutatedProperties.BuildStatic = true
316}
317
318func (library *libraryDecorator) BuildOnlyRust() {
319 library.MutatedProperties.BuildDylib = true
320 library.MutatedProperties.BuildRlib = true
321 library.MutatedProperties.BuildShared = false
322 library.MutatedProperties.BuildStatic = false
323}
324
Ivan Lozanoffee3342019-08-27 12:03:00 -0700325func (library *libraryDecorator) BuildOnlyDylib() {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700326 library.MutatedProperties.BuildDylib = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700327 library.MutatedProperties.BuildRlib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700328 library.MutatedProperties.BuildShared = false
329 library.MutatedProperties.BuildStatic = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700330}
331
332func (library *libraryDecorator) BuildOnlyRlib() {
333 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700334 library.MutatedProperties.BuildRlib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700335 library.MutatedProperties.BuildShared = false
336 library.MutatedProperties.BuildStatic = false
337}
338
339func (library *libraryDecorator) BuildOnlyStatic() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700340 library.MutatedProperties.BuildRlib = false
341 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700342 library.MutatedProperties.BuildShared = false
343 library.MutatedProperties.BuildStatic = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700344}
345
346func (library *libraryDecorator) BuildOnlyShared() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700347 library.MutatedProperties.BuildRlib = false
348 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700349 library.MutatedProperties.BuildStatic = false
350 library.MutatedProperties.BuildShared = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700351}
352
353func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400354 module := newModule(hod, android.MultilibBoth)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700355
356 library := &libraryDecorator{
357 MutatedProperties: LibraryMutatedProperties{
Matthew Maurer2ae05132020-06-23 14:28:53 -0700358 BuildDylib: false,
359 BuildRlib: false,
360 BuildShared: false,
361 BuildStatic: false,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700362 },
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800363 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Matthew Maurerbb3add12020-06-25 09:34:12 -0700364 flagExporter: NewFlagExporter(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700365 }
366
367 module.compiler = library
368
369 return module, library
370}
371
372func (library *libraryDecorator) compilerProps() []interface{} {
373 return append(library.baseCompiler.compilerProps(),
374 &library.Properties,
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200375 &library.MutatedProperties,
376 &library.stripper.StripProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700377}
378
Ivan Lozanof1c84332019-09-20 11:00:37 -0700379func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
380 deps = library.baseCompiler.compilerDeps(ctx, deps)
381
Ivan Lozano52767be2019-10-18 14:49:46 -0700382 if ctx.toolchain().Bionic() && (library.dylib() || library.shared()) {
Ivan Lozano45901ed2020-07-24 16:05:01 -0400383 deps = bionicDeps(deps)
Ivan Lozano12ee9ca2020-04-07 13:19:44 -0400384 deps.CrtBegin = "crtbegin_so"
385 deps.CrtEnd = "crtend_so"
Ivan Lozanof1c84332019-09-20 11:00:37 -0700386 }
387
388 return deps
389}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400390
391func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string {
392 return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
393}
394
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800395func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200396 flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800397 flags = library.baseCompiler.compilerFlags(ctx, flags)
398 if library.shared() || library.static() {
399 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
400 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400401 if library.shared() {
402 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
403 }
404
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800405 return flags
406}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700407
Ivan Lozanoffee3342019-08-27 12:03:00 -0700408func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200409 var outputFile android.ModuleOutPath
410 var fileName string
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400411 var srcPath android.Path
Ivan Lozanoffee3342019-08-27 12:03:00 -0700412
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400413 if library.sourceProvider != nil {
414 srcPath = library.sourceProvider.Srcs()[0]
415 } else {
416 srcPath, _ = srcPathFromModuleSrcs(ctx, library.baseCompiler.Properties.Srcs)
417 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700418
419 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano2093af22020-08-25 12:48:19 -0400420 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700421
Matthew Maurer46c46cc2020-01-13 16:34:34 -0800422 if library.dylib() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700423 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
424 // https://github.com/rust-lang/rust/issues/19680
425 // https://github.com/rust-lang/rust/issues/34909
426 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
427 }
428
Ivan Lozanoffee3342019-08-27 12:03:00 -0700429 if library.rlib() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200430 fileName = library.getStem(ctx) + ctx.toolchain().RlibSuffix()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700431 outputFile = android.PathForModuleOut(ctx, fileName)
432
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400433 outputs := TransformSrctoRlib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
434 library.coverageFile = outputs.coverageFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700435 } else if library.dylib() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200436 fileName = library.getStem(ctx) + ctx.toolchain().DylibSuffix()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700437 outputFile = android.PathForModuleOut(ctx, fileName)
438
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400439 outputs := TransformSrctoDylib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
440 library.coverageFile = outputs.coverageFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700441 } else if library.static() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200442 fileName = library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
Ivan Lozano52767be2019-10-18 14:49:46 -0700443 outputFile = android.PathForModuleOut(ctx, fileName)
444
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400445 outputs := TransformSrctoStatic(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
446 library.coverageFile = outputs.coverageFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700447 } else if library.shared() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200448 fileName = library.sharedLibFilename(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700449 outputFile = android.PathForModuleOut(ctx, fileName)
450
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400451 outputs := TransformSrctoShared(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
452 library.coverageFile = outputs.coverageFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700453 }
454
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200455 if !library.rlib() && library.stripper.NeedsStrip(ctx) {
456 strippedOutputFile := android.PathForModuleOut(ctx, "stripped", fileName)
457 library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
458 library.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
459 }
460
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400461 var coverageFiles android.Paths
462 if library.coverageFile != nil {
463 coverageFiles = append(coverageFiles, library.coverageFile)
464 }
465 if len(deps.coverageFiles) > 0 {
466 coverageFiles = append(coverageFiles, deps.coverageFiles...)
467 }
468 library.coverageOutputZipFile = TransformCoverageFilesToZip(ctx, coverageFiles, library.getStem(ctx))
469
Ivan Lozano52767be2019-10-18 14:49:46 -0700470 if library.rlib() || library.dylib() {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700471 library.exportLinkDirs(deps.linkDirs...)
472 library.exportDepFlags(deps.depFlags...)
Ivan Lozano2093af22020-08-25 12:48:19 -0400473 library.exportLinkObjects(deps.linkObjects...)
Ivan Lozano52767be2019-10-18 14:49:46 -0700474 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700475
476 return outputFile
477}
478
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700479func (library *libraryDecorator) getStem(ctx ModuleContext) string {
480 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
481 validateLibraryStem(ctx, stem, library.crateName())
482
483 return stem + String(library.baseCompiler.Properties.Suffix)
484}
485
Ivan Lozano2b081132020-09-08 12:46:52 -0400486func (library *libraryDecorator) install(ctx ModuleContext) {
487 // Only shared and dylib variants make sense to install.
488 if library.shared() || library.dylib() {
489 library.baseCompiler.install(ctx)
490 }
491}
492
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400493func (library *libraryDecorator) Disabled() bool {
494 return library.MutatedProperties.VariantIsDisabled
495}
496
497func (library *libraryDecorator) SetDisabled() {
498 library.MutatedProperties.VariantIsDisabled = true
499}
500
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700501var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
502
503func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
504 if crate_name == "" {
505 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
506 }
507
508 // crate_names are used for the library output file, and rustc expects these
509 // to be alphanumeric with underscores allowed.
510 if validCrateName.MatchString(crate_name) {
511 ctx.PropertyErrorf("crate_name",
512 "library crate_names must be alphanumeric with underscores allowed")
513 }
514
515 // Libraries are expected to begin with "lib" followed by the crate_name
516 if !strings.HasPrefix(filename, "lib"+crate_name) {
517 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
518 }
519}
520
Ivan Lozanoffee3342019-08-27 12:03:00 -0700521func LibraryMutator(mctx android.BottomUpMutatorContext) {
522 if m, ok := mctx.Module().(*Module); ok && m.compiler != nil {
523 switch library := m.compiler.(type) {
524 case libraryInterface:
Ivan Lozano89435d12020-07-31 11:01:18 -0400525 if library.buildRlib() && library.buildDylib() {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400526 variants := []string{"rlib", "dylib"}
527 if m.sourceProvider != nil {
528 variants = append(variants, "")
529 }
530 modules := mctx.CreateLocalVariations(variants...)
531
Ivan Lozano89435d12020-07-31 11:01:18 -0400532 rlib := modules[0].(*Module)
533 dylib := modules[1].(*Module)
Ivan Lozano89435d12020-07-31 11:01:18 -0400534 rlib.compiler.(libraryInterface).setRlib()
535 dylib.compiler.(libraryInterface).setDylib()
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400536 if m.sourceProvider != nil {
537 // This library is SourceProvider generated, so the non-library-producing
538 // variant needs to disable it's compiler and skip installation.
539 sourceProvider := modules[2].(*Module)
540 sourceProvider.compiler.SetDisabled()
541 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400542 } else if library.buildRlib() {
543 modules := mctx.CreateLocalVariations("rlib")
544 modules[0].(*Module).compiler.(libraryInterface).setRlib()
545 } else if library.buildDylib() {
546 modules := mctx.CreateLocalVariations("dylib")
547 modules[0].(*Module).compiler.(libraryInterface).setDylib()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700548 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400549
550 if m.sourceProvider != nil {
551 // Alias the non-library variant to the empty-string variant.
552 mctx.AliasVariation("")
553 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700554 }
555 }
556}
Ivan Lozano2b081132020-09-08 12:46:52 -0400557
558func LibstdMutator(mctx android.BottomUpMutatorContext) {
559 if m, ok := mctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() {
560 switch library := m.compiler.(type) {
561 case libraryInterface:
562 // Only create a variant if a library is actually being built.
563 if library.rlib() && !library.sysroot() {
564 variants := []string{"rlib-std", "dylib-std"}
565 modules := mctx.CreateLocalVariations(variants...)
566
567 rlib := modules[0].(*Module)
568 dylib := modules[1].(*Module)
569 rlib.compiler.(libraryInterface).setRlibStd()
570 dylib.compiler.(libraryInterface).setDylibStd()
571 rlib.Properties.SubName += RlibStdlibSuffix
572 dylib.Properties.SubName += DylibStdlibSuffix
573 }
574 }
575 }
576}