blob: 2792c5b4a372fb26db578ff35bdaac40fd05d623 [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 (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +020018 "fmt"
Ivan Lozanoad8b18b2019-10-31 19:38:29 -070019 "regexp"
20 "strings"
21
Ivan Lozanoffee3342019-08-27 12:03:00 -070022 "android/soong/android"
23)
24
Ivan Lozano2b081132020-09-08 12:46:52 -040025var (
26 DylibStdlibSuffix = ".dylib-std"
27 RlibStdlibSuffix = ".rlib-std"
28)
29
Ivan Lozanoffee3342019-08-27 12:03:00 -070030func init() {
31 android.RegisterModuleType("rust_library", RustLibraryFactory)
32 android.RegisterModuleType("rust_library_dylib", RustLibraryDylibFactory)
33 android.RegisterModuleType("rust_library_rlib", RustLibraryRlibFactory)
34 android.RegisterModuleType("rust_library_host", RustLibraryHostFactory)
35 android.RegisterModuleType("rust_library_host_dylib", RustLibraryDylibHostFactory)
36 android.RegisterModuleType("rust_library_host_rlib", RustLibraryRlibHostFactory)
Matthew Maurer2ae05132020-06-23 14:28:53 -070037 android.RegisterModuleType("rust_ffi", RustFFIFactory)
38 android.RegisterModuleType("rust_ffi_shared", RustFFISharedFactory)
39 android.RegisterModuleType("rust_ffi_static", RustFFIStaticFactory)
40 android.RegisterModuleType("rust_ffi_host", RustFFIHostFactory)
41 android.RegisterModuleType("rust_ffi_host_shared", RustFFISharedHostFactory)
42 android.RegisterModuleType("rust_ffi_host_static", RustFFIStaticHostFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070043}
44
45type VariantLibraryProperties struct {
Matthew Maurerc761eec2020-06-25 00:47:46 -070046 Enabled *bool `android:"arch_variant"`
47 Srcs []string `android:"path,arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070048}
49
50type LibraryCompilerProperties struct {
Ivan Lozano52767be2019-10-18 14:49:46 -070051 Rlib VariantLibraryProperties `android:"arch_variant"`
52 Dylib VariantLibraryProperties `android:"arch_variant"`
53 Shared VariantLibraryProperties `android:"arch_variant"`
54 Static VariantLibraryProperties `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070055
Ivan Lozano52767be2019-10-18 14:49:46 -070056 // path to include directories to pass to cc_* modules, only relevant for static/shared variants.
57 Include_dirs []string `android:"path,arch_variant"`
Ivan Lozano2b081132020-09-08 12:46:52 -040058
59 // Whether this library is part of the Rust toolchain sysroot.
60 Sysroot *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070061}
62
63type LibraryMutatedProperties struct {
Ivan Lozanoffee3342019-08-27 12:03:00 -070064 // Build a dylib variant
65 BuildDylib bool `blueprint:"mutated"`
66 // Build an rlib variant
67 BuildRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070068 // Build a shared library variant
69 BuildShared bool `blueprint:"mutated"`
70 // Build a static library variant
71 BuildStatic bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070072
73 // This variant is a dylib
74 VariantIsDylib bool `blueprint:"mutated"`
75 // This variant is an rlib
76 VariantIsRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070077 // This variant is a shared library
78 VariantIsShared bool `blueprint:"mutated"`
79 // This variant is a static library
80 VariantIsStatic bool `blueprint:"mutated"`
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +020081 // This variant is a source provider
82 VariantIsSource bool `blueprint:"mutated"`
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040083
84 // This variant is disabled and should not be compiled
85 // (used for SourceProvider variants that produce only source)
86 VariantIsDisabled bool `blueprint:"mutated"`
Ivan Lozano2b081132020-09-08 12:46:52 -040087
88 // Whether this library variant should be link libstd via rlibs
89 VariantIsStaticStd bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070090}
91
92type libraryDecorator struct {
93 *baseCompiler
Matthew Maurerbb3add12020-06-25 09:34:12 -070094 *flagExporter
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +020095 stripper Stripper
Ivan Lozanoffee3342019-08-27 12:03:00 -070096
Ivan Lozano8a23fa42020-06-16 10:26:57 -040097 Properties LibraryCompilerProperties
98 MutatedProperties LibraryMutatedProperties
99 includeDirs android.Paths
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400100 sourceProvider SourceProvider
Ivan Lozanoffee3342019-08-27 12:03:00 -0700101}
102
103type libraryInterface interface {
104 rlib() bool
105 dylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700106 static() bool
107 shared() bool
Ivan Lozano2b081132020-09-08 12:46:52 -0400108 sysroot() bool
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200109 source() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700110
111 // Returns true if the build options for the module have selected a particular build type
112 buildRlib() bool
113 buildDylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700114 buildShared() bool
115 buildStatic() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700116
117 // Sets a particular variant type
118 setRlib()
119 setDylib()
Ivan Lozano52767be2019-10-18 14:49:46 -0700120 setShared()
121 setStatic()
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200122 setSource()
Ivan Lozano52767be2019-10-18 14:49:46 -0700123
Ivan Lozano2b081132020-09-08 12:46:52 -0400124 // Set libstd linkage
125 setRlibStd()
126 setDylibStd()
127
Ivan Lozano52767be2019-10-18 14:49:46 -0700128 // Build a specific library variant
Matthew Maurer2ae05132020-06-23 14:28:53 -0700129 BuildOnlyFFI()
130 BuildOnlyRust()
Ivan Lozano52767be2019-10-18 14:49:46 -0700131 BuildOnlyRlib()
132 BuildOnlyDylib()
133 BuildOnlyStatic()
134 BuildOnlyShared()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700135}
136
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400137func (library *libraryDecorator) nativeCoverage() bool {
138 return true
139}
140
Ivan Lozanoffee3342019-08-27 12:03:00 -0700141func (library *libraryDecorator) rlib() bool {
142 return library.MutatedProperties.VariantIsRlib
143}
144
Ivan Lozano2b081132020-09-08 12:46:52 -0400145func (library *libraryDecorator) sysroot() bool {
146 return Bool(library.Properties.Sysroot)
147}
148
Ivan Lozanoffee3342019-08-27 12:03:00 -0700149func (library *libraryDecorator) dylib() bool {
150 return library.MutatedProperties.VariantIsDylib
151}
152
Ivan Lozano52767be2019-10-18 14:49:46 -0700153func (library *libraryDecorator) shared() bool {
154 return library.MutatedProperties.VariantIsShared
155}
156
157func (library *libraryDecorator) static() bool {
158 return library.MutatedProperties.VariantIsStatic
159}
160
Ivan Lozano2b081132020-09-08 12:46:52 -0400161func (library *libraryDecorator) staticStd(ctx *depsContext) bool {
162 // libraries should only request the staticStd when building a static FFI or when variant is staticStd
163 return library.static() || library.MutatedProperties.VariantIsStaticStd
164}
165
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200166func (library *libraryDecorator) source() bool {
167 return library.MutatedProperties.VariantIsSource
168}
169
Ivan Lozanoffee3342019-08-27 12:03:00 -0700170func (library *libraryDecorator) buildRlib() bool {
171 return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true)
172}
173
174func (library *libraryDecorator) buildDylib() bool {
175 return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true)
176}
177
Ivan Lozano52767be2019-10-18 14:49:46 -0700178func (library *libraryDecorator) buildShared() bool {
179 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
180}
181
182func (library *libraryDecorator) buildStatic() bool {
183 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
184}
185
Ivan Lozanoffee3342019-08-27 12:03:00 -0700186func (library *libraryDecorator) setRlib() {
187 library.MutatedProperties.VariantIsRlib = true
188 library.MutatedProperties.VariantIsDylib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700189 library.MutatedProperties.VariantIsStatic = false
190 library.MutatedProperties.VariantIsShared = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700191}
192
193func (library *libraryDecorator) setDylib() {
194 library.MutatedProperties.VariantIsRlib = false
195 library.MutatedProperties.VariantIsDylib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700196 library.MutatedProperties.VariantIsStatic = false
197 library.MutatedProperties.VariantIsShared = false
198}
199
Ivan Lozano2b081132020-09-08 12:46:52 -0400200func (library *libraryDecorator) setRlibStd() {
201 library.MutatedProperties.VariantIsStaticStd = true
202}
203
204func (library *libraryDecorator) setDylibStd() {
205 library.MutatedProperties.VariantIsStaticStd = false
206}
207
Ivan Lozano52767be2019-10-18 14:49:46 -0700208func (library *libraryDecorator) setShared() {
209 library.MutatedProperties.VariantIsStatic = false
210 library.MutatedProperties.VariantIsShared = true
211 library.MutatedProperties.VariantIsRlib = false
212 library.MutatedProperties.VariantIsDylib = false
213}
214
215func (library *libraryDecorator) setStatic() {
216 library.MutatedProperties.VariantIsStatic = true
217 library.MutatedProperties.VariantIsShared = false
218 library.MutatedProperties.VariantIsRlib = false
219 library.MutatedProperties.VariantIsDylib = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700220}
221
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200222func (library *libraryDecorator) setSource() {
223 library.MutatedProperties.VariantIsSource = true
224}
225
Ivan Lozano042504f2020-08-18 14:31:23 -0400226func (library *libraryDecorator) autoDep(ctx BaseModuleContext) autoDep {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700227 if library.rlib() || library.static() {
228 return rlibAutoDep
229 } else if library.dylib() || library.shared() {
230 return dylibAutoDep
231 } else {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200232 panic(fmt.Errorf("autoDep called on library %q that has no enabled variants.", ctx.ModuleName()))
Matthew Maurer0f003b12020-06-29 14:34:06 -0700233 }
234}
235
Ivan Lozanoffee3342019-08-27 12:03:00 -0700236var _ compiler = (*libraryDecorator)(nil)
Ivan Lozano52767be2019-10-18 14:49:46 -0700237var _ libraryInterface = (*libraryDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700238var _ exportedFlagsProducer = (*libraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700239
Matthew Maurer2ae05132020-06-23 14:28:53 -0700240// rust_library produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700241func RustLibraryFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700242 module, library := NewRustLibrary(android.HostAndDeviceSupported)
243 library.BuildOnlyRust()
244 return module.Init()
245}
246
247// rust_ffi produces all ffi variants.
248func RustFFIFactory() android.Module {
249 module, library := NewRustLibrary(android.HostAndDeviceSupported)
250 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700251 return module.Init()
252}
253
254// rust_library_dylib produces a dylib.
255func RustLibraryDylibFactory() android.Module {
256 module, library := NewRustLibrary(android.HostAndDeviceSupported)
257 library.BuildOnlyDylib()
258 return module.Init()
259}
260
261// rust_library_rlib produces an rlib.
262func RustLibraryRlibFactory() android.Module {
263 module, library := NewRustLibrary(android.HostAndDeviceSupported)
264 library.BuildOnlyRlib()
265 return module.Init()
266}
267
Matthew Maurer2ae05132020-06-23 14:28:53 -0700268// rust_ffi_shared produces a shared library.
269func RustFFISharedFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700270 module, library := NewRustLibrary(android.HostAndDeviceSupported)
271 library.BuildOnlyShared()
272 return module.Init()
273}
274
Matthew Maurer2ae05132020-06-23 14:28:53 -0700275// rust_ffi_static produces a static library.
276func RustFFIStaticFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700277 module, library := NewRustLibrary(android.HostAndDeviceSupported)
278 library.BuildOnlyStatic()
279 return module.Init()
280}
281
Matthew Maurer2ae05132020-06-23 14:28:53 -0700282// rust_library_host produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700283func RustLibraryHostFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700284 module, library := NewRustLibrary(android.HostSupported)
285 library.BuildOnlyRust()
286 return module.Init()
287}
288
289// rust_ffi_host produces all FFI variants.
290func RustFFIHostFactory() android.Module {
291 module, library := NewRustLibrary(android.HostSupported)
292 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700293 return module.Init()
294}
295
296// rust_library_dylib_host produces a dylib.
297func RustLibraryDylibHostFactory() android.Module {
298 module, library := NewRustLibrary(android.HostSupported)
299 library.BuildOnlyDylib()
300 return module.Init()
301}
302
303// rust_library_rlib_host produces an rlib.
304func RustLibraryRlibHostFactory() android.Module {
305 module, library := NewRustLibrary(android.HostSupported)
306 library.BuildOnlyRlib()
307 return module.Init()
308}
309
Matthew Maurer2ae05132020-06-23 14:28:53 -0700310// rust_ffi_static_host produces a static library.
311func RustFFIStaticHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700312 module, library := NewRustLibrary(android.HostSupported)
313 library.BuildOnlyStatic()
314 return module.Init()
315}
316
Matthew Maurer2ae05132020-06-23 14:28:53 -0700317// rust_ffi_shared_host produces an shared library.
318func RustFFISharedHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700319 module, library := NewRustLibrary(android.HostSupported)
320 library.BuildOnlyShared()
321 return module.Init()
322}
323
Matthew Maurer2ae05132020-06-23 14:28:53 -0700324func (library *libraryDecorator) BuildOnlyFFI() {
325 library.MutatedProperties.BuildDylib = false
326 library.MutatedProperties.BuildRlib = false
327 library.MutatedProperties.BuildShared = true
328 library.MutatedProperties.BuildStatic = true
329}
330
331func (library *libraryDecorator) BuildOnlyRust() {
332 library.MutatedProperties.BuildDylib = true
333 library.MutatedProperties.BuildRlib = true
334 library.MutatedProperties.BuildShared = false
335 library.MutatedProperties.BuildStatic = false
336}
337
Ivan Lozanoffee3342019-08-27 12:03:00 -0700338func (library *libraryDecorator) BuildOnlyDylib() {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700339 library.MutatedProperties.BuildDylib = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700340 library.MutatedProperties.BuildRlib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700341 library.MutatedProperties.BuildShared = false
342 library.MutatedProperties.BuildStatic = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700343}
344
345func (library *libraryDecorator) BuildOnlyRlib() {
346 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700347 library.MutatedProperties.BuildRlib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700348 library.MutatedProperties.BuildShared = false
349 library.MutatedProperties.BuildStatic = false
350}
351
352func (library *libraryDecorator) BuildOnlyStatic() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700353 library.MutatedProperties.BuildRlib = false
354 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700355 library.MutatedProperties.BuildShared = false
356 library.MutatedProperties.BuildStatic = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700357}
358
359func (library *libraryDecorator) BuildOnlyShared() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700360 library.MutatedProperties.BuildRlib = false
361 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700362 library.MutatedProperties.BuildStatic = false
363 library.MutatedProperties.BuildShared = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700364}
365
366func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400367 module := newModule(hod, android.MultilibBoth)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700368
369 library := &libraryDecorator{
370 MutatedProperties: LibraryMutatedProperties{
Matthew Maurer2ae05132020-06-23 14:28:53 -0700371 BuildDylib: false,
372 BuildRlib: false,
373 BuildShared: false,
374 BuildStatic: false,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700375 },
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800376 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Matthew Maurerbb3add12020-06-25 09:34:12 -0700377 flagExporter: NewFlagExporter(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700378 }
379
380 module.compiler = library
381
382 return module, library
383}
384
385func (library *libraryDecorator) compilerProps() []interface{} {
386 return append(library.baseCompiler.compilerProps(),
387 &library.Properties,
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200388 &library.MutatedProperties,
389 &library.stripper.StripProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700390}
391
Ivan Lozanof1c84332019-09-20 11:00:37 -0700392func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
393 deps = library.baseCompiler.compilerDeps(ctx, deps)
394
Ivan Lozano52767be2019-10-18 14:49:46 -0700395 if ctx.toolchain().Bionic() && (library.dylib() || library.shared()) {
Ivan Lozano45901ed2020-07-24 16:05:01 -0400396 deps = bionicDeps(deps)
Ivan Lozano12ee9ca2020-04-07 13:19:44 -0400397 deps.CrtBegin = "crtbegin_so"
398 deps.CrtEnd = "crtend_so"
Ivan Lozanof1c84332019-09-20 11:00:37 -0700399 }
400
401 return deps
402}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400403
404func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string {
405 return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
406}
407
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800408func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200409 flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800410 flags = library.baseCompiler.compilerFlags(ctx, flags)
411 if library.shared() || library.static() {
412 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
413 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400414 if library.shared() {
415 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
416 }
417
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800418 return flags
419}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700420
Ivan Lozanoffee3342019-08-27 12:03:00 -0700421func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200422 var outputFile android.ModuleOutPath
423 var fileName string
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400424 var srcPath android.Path
Ivan Lozanoffee3342019-08-27 12:03:00 -0700425
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400426 if library.sourceProvider != nil {
427 srcPath = library.sourceProvider.Srcs()[0]
428 } else {
429 srcPath, _ = srcPathFromModuleSrcs(ctx, library.baseCompiler.Properties.Srcs)
430 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700431
432 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano2093af22020-08-25 12:48:19 -0400433 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700434
Matthew Maurer46c46cc2020-01-13 16:34:34 -0800435 if library.dylib() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700436 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
437 // https://github.com/rust-lang/rust/issues/19680
438 // https://github.com/rust-lang/rust/issues/34909
439 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
440 }
441
Ivan Lozanoffee3342019-08-27 12:03:00 -0700442 if library.rlib() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200443 fileName = library.getStem(ctx) + ctx.toolchain().RlibSuffix()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700444 outputFile = android.PathForModuleOut(ctx, fileName)
445
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400446 outputs := TransformSrctoRlib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
447 library.coverageFile = outputs.coverageFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700448 } else if library.dylib() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200449 fileName = library.getStem(ctx) + ctx.toolchain().DylibSuffix()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700450 outputFile = android.PathForModuleOut(ctx, fileName)
451
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400452 outputs := TransformSrctoDylib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
453 library.coverageFile = outputs.coverageFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700454 } else if library.static() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200455 fileName = library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
Ivan Lozano52767be2019-10-18 14:49:46 -0700456 outputFile = android.PathForModuleOut(ctx, fileName)
457
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400458 outputs := TransformSrctoStatic(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
459 library.coverageFile = outputs.coverageFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700460 } else if library.shared() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200461 fileName = library.sharedLibFilename(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700462 outputFile = android.PathForModuleOut(ctx, fileName)
463
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400464 outputs := TransformSrctoShared(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
465 library.coverageFile = outputs.coverageFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700466 }
467
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200468 if !library.rlib() && library.stripper.NeedsStrip(ctx) {
469 strippedOutputFile := android.PathForModuleOut(ctx, "stripped", fileName)
470 library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
471 library.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
472 }
473
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400474 var coverageFiles android.Paths
475 if library.coverageFile != nil {
476 coverageFiles = append(coverageFiles, library.coverageFile)
477 }
478 if len(deps.coverageFiles) > 0 {
479 coverageFiles = append(coverageFiles, deps.coverageFiles...)
480 }
481 library.coverageOutputZipFile = TransformCoverageFilesToZip(ctx, coverageFiles, library.getStem(ctx))
482
Ivan Lozano52767be2019-10-18 14:49:46 -0700483 if library.rlib() || library.dylib() {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700484 library.exportLinkDirs(deps.linkDirs...)
485 library.exportDepFlags(deps.depFlags...)
Ivan Lozano2093af22020-08-25 12:48:19 -0400486 library.exportLinkObjects(deps.linkObjects...)
Ivan Lozano52767be2019-10-18 14:49:46 -0700487 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700488
489 return outputFile
490}
491
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700492func (library *libraryDecorator) getStem(ctx ModuleContext) string {
493 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
494 validateLibraryStem(ctx, stem, library.crateName())
495
496 return stem + String(library.baseCompiler.Properties.Suffix)
497}
498
Ivan Lozano2b081132020-09-08 12:46:52 -0400499func (library *libraryDecorator) install(ctx ModuleContext) {
500 // Only shared and dylib variants make sense to install.
501 if library.shared() || library.dylib() {
502 library.baseCompiler.install(ctx)
503 }
504}
505
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400506func (library *libraryDecorator) Disabled() bool {
507 return library.MutatedProperties.VariantIsDisabled
508}
509
510func (library *libraryDecorator) SetDisabled() {
511 library.MutatedProperties.VariantIsDisabled = true
512}
513
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700514var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
515
516func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
517 if crate_name == "" {
518 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
519 }
520
521 // crate_names are used for the library output file, and rustc expects these
522 // to be alphanumeric with underscores allowed.
523 if validCrateName.MatchString(crate_name) {
524 ctx.PropertyErrorf("crate_name",
525 "library crate_names must be alphanumeric with underscores allowed")
526 }
527
528 // Libraries are expected to begin with "lib" followed by the crate_name
529 if !strings.HasPrefix(filename, "lib"+crate_name) {
530 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
531 }
532}
533
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200534// LibraryMutator mutates the libraries into variants according to the
535// build{Rlib,Dylib} attributes.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700536func LibraryMutator(mctx android.BottomUpMutatorContext) {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200537 // Only mutate on Rust libraries.
538 m, ok := mctx.Module().(*Module)
539 if !ok || m.compiler == nil {
540 return
541 }
542 library, ok := m.compiler.(libraryInterface)
543 if !ok {
544 return
545 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400546
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200547 var variants []string
548 // The source variant is used for SourceProvider modules. The other variants (i.e. rlib and dylib)
549 // depend on this variant. It must be the first variant to be declared.
550 sourceVariant := false
551 if m.sourceProvider != nil {
552 variants = append(variants, "source")
553 sourceVariant = true
554 }
555 if library.buildRlib() {
556 variants = append(variants, rlibVariation)
557 }
558 if library.buildDylib() {
559 variants = append(variants, dylibVariation)
560 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400561
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200562 if len(variants) == 0 {
563 return
564 }
565 modules := mctx.CreateLocalVariations(variants...)
566
567 // The order of the variations (modules) matches the variant names provided. Iterate
568 // through the new variation modules and set their mutated properties.
569 for i, v := range modules {
570 switch variants[i] {
571 case rlibVariation:
572 v.(*Module).compiler.(libraryInterface).setRlib()
573 case dylibVariation:
574 v.(*Module).compiler.(libraryInterface).setDylib()
575 case "source":
576 v.(*Module).compiler.(libraryInterface).setSource()
577 // The source variant does not produce any library.
578 // Disable the compilation steps.
579 v.(*Module).compiler.SetDisabled()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700580 }
581 }
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200582
583 // If a source variant is created, add an inter-variant dependency
584 // between the other variants and the source variant.
585 if sourceVariant {
586 sv := modules[0]
587 for _, v := range modules[1:] {
588 if !v.Enabled() {
589 continue
590 }
591 mctx.AddInterVariantDependency(sourceDepTag, v, sv)
592 }
593 // Alias the source variation so it can be named directly in "srcs" properties.
594 mctx.AliasVariation("source")
595 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700596}
Ivan Lozano2b081132020-09-08 12:46:52 -0400597
598func LibstdMutator(mctx android.BottomUpMutatorContext) {
599 if m, ok := mctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() {
600 switch library := m.compiler.(type) {
601 case libraryInterface:
602 // Only create a variant if a library is actually being built.
603 if library.rlib() && !library.sysroot() {
604 variants := []string{"rlib-std", "dylib-std"}
605 modules := mctx.CreateLocalVariations(variants...)
606
607 rlib := modules[0].(*Module)
608 dylib := modules[1].(*Module)
609 rlib.compiler.(libraryInterface).setRlibStd()
610 dylib.compiler.(libraryInterface).setDylibStd()
611 rlib.Properties.SubName += RlibStdlibSuffix
612 dylib.Properties.SubName += DylibStdlibSuffix
613 }
614 }
615 }
616}