blob: 9005f0c54a65b84c8620e46a2ac5c9c64b9ef40a [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"
Colin Cross0de8a1e2020-09-18 14:15:30 -070023 "android/soong/cc"
Ivan Lozanoffee3342019-08-27 12:03:00 -070024)
25
Ivan Lozano2b081132020-09-08 12:46:52 -040026var (
27 DylibStdlibSuffix = ".dylib-std"
28 RlibStdlibSuffix = ".rlib-std"
29)
30
Ivan Lozanoffee3342019-08-27 12:03:00 -070031func init() {
32 android.RegisterModuleType("rust_library", RustLibraryFactory)
33 android.RegisterModuleType("rust_library_dylib", RustLibraryDylibFactory)
34 android.RegisterModuleType("rust_library_rlib", RustLibraryRlibFactory)
35 android.RegisterModuleType("rust_library_host", RustLibraryHostFactory)
36 android.RegisterModuleType("rust_library_host_dylib", RustLibraryDylibHostFactory)
37 android.RegisterModuleType("rust_library_host_rlib", RustLibraryRlibHostFactory)
Matthew Maurer2ae05132020-06-23 14:28:53 -070038 android.RegisterModuleType("rust_ffi", RustFFIFactory)
39 android.RegisterModuleType("rust_ffi_shared", RustFFISharedFactory)
40 android.RegisterModuleType("rust_ffi_static", RustFFIStaticFactory)
41 android.RegisterModuleType("rust_ffi_host", RustFFIHostFactory)
42 android.RegisterModuleType("rust_ffi_host_shared", RustFFISharedHostFactory)
43 android.RegisterModuleType("rust_ffi_host_static", RustFFIStaticHostFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070044}
45
46type VariantLibraryProperties struct {
Matthew Maurerc761eec2020-06-25 00:47:46 -070047 Enabled *bool `android:"arch_variant"`
48 Srcs []string `android:"path,arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070049}
50
51type LibraryCompilerProperties struct {
Ivan Lozano52767be2019-10-18 14:49:46 -070052 Rlib VariantLibraryProperties `android:"arch_variant"`
53 Dylib VariantLibraryProperties `android:"arch_variant"`
54 Shared VariantLibraryProperties `android:"arch_variant"`
55 Static VariantLibraryProperties `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070056
Ivan Lozano52767be2019-10-18 14:49:46 -070057 // path to include directories to pass to cc_* modules, only relevant for static/shared variants.
58 Include_dirs []string `android:"path,arch_variant"`
Ivan Lozano2b081132020-09-08 12:46:52 -040059
60 // Whether this library is part of the Rust toolchain sysroot.
61 Sysroot *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070062}
63
64type LibraryMutatedProperties struct {
Ivan Lozanoffee3342019-08-27 12:03:00 -070065 // Build a dylib variant
66 BuildDylib bool `blueprint:"mutated"`
67 // Build an rlib variant
68 BuildRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070069 // Build a shared library variant
70 BuildShared bool `blueprint:"mutated"`
71 // Build a static library variant
72 BuildStatic bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070073
74 // This variant is a dylib
75 VariantIsDylib bool `blueprint:"mutated"`
76 // This variant is an rlib
77 VariantIsRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070078 // This variant is a shared library
79 VariantIsShared bool `blueprint:"mutated"`
80 // This variant is a static library
81 VariantIsStatic bool `blueprint:"mutated"`
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +020082 // This variant is a source provider
83 VariantIsSource bool `blueprint:"mutated"`
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040084
85 // This variant is disabled and should not be compiled
86 // (used for SourceProvider variants that produce only source)
87 VariantIsDisabled bool `blueprint:"mutated"`
Ivan Lozano2b081132020-09-08 12:46:52 -040088
89 // Whether this library variant should be link libstd via rlibs
90 VariantIsStaticStd bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070091}
92
93type libraryDecorator struct {
94 *baseCompiler
Matthew Maurerbb3add12020-06-25 09:34:12 -070095 *flagExporter
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +020096 stripper Stripper
Ivan Lozanoffee3342019-08-27 12:03:00 -070097
Ivan Lozano8a23fa42020-06-16 10:26:57 -040098 Properties LibraryCompilerProperties
99 MutatedProperties LibraryMutatedProperties
100 includeDirs android.Paths
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400101 sourceProvider SourceProvider
Ivan Lozanoffee3342019-08-27 12:03:00 -0700102}
103
104type libraryInterface interface {
105 rlib() bool
106 dylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700107 static() bool
108 shared() bool
Ivan Lozano2b081132020-09-08 12:46:52 -0400109 sysroot() bool
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200110 source() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700111
112 // Returns true if the build options for the module have selected a particular build type
113 buildRlib() bool
114 buildDylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700115 buildShared() bool
116 buildStatic() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700117
118 // Sets a particular variant type
119 setRlib()
120 setDylib()
Ivan Lozano52767be2019-10-18 14:49:46 -0700121 setShared()
122 setStatic()
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200123 setSource()
Ivan Lozano52767be2019-10-18 14:49:46 -0700124
Ivan Lozano2b081132020-09-08 12:46:52 -0400125 // Set libstd linkage
126 setRlibStd()
127 setDylibStd()
128
Ivan Lozano52767be2019-10-18 14:49:46 -0700129 // Build a specific library variant
Matthew Maurer2ae05132020-06-23 14:28:53 -0700130 BuildOnlyFFI()
131 BuildOnlyRust()
Ivan Lozano52767be2019-10-18 14:49:46 -0700132 BuildOnlyRlib()
133 BuildOnlyDylib()
134 BuildOnlyStatic()
135 BuildOnlyShared()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700136}
137
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400138func (library *libraryDecorator) nativeCoverage() bool {
139 return true
140}
141
Ivan Lozanoffee3342019-08-27 12:03:00 -0700142func (library *libraryDecorator) rlib() bool {
143 return library.MutatedProperties.VariantIsRlib
144}
145
Ivan Lozano2b081132020-09-08 12:46:52 -0400146func (library *libraryDecorator) sysroot() bool {
147 return Bool(library.Properties.Sysroot)
148}
149
Ivan Lozanoffee3342019-08-27 12:03:00 -0700150func (library *libraryDecorator) dylib() bool {
151 return library.MutatedProperties.VariantIsDylib
152}
153
Ivan Lozano52767be2019-10-18 14:49:46 -0700154func (library *libraryDecorator) shared() bool {
155 return library.MutatedProperties.VariantIsShared
156}
157
158func (library *libraryDecorator) static() bool {
159 return library.MutatedProperties.VariantIsStatic
160}
161
Ivan Lozanodd055472020-09-28 13:22:45 -0400162func (library *libraryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
163 // libraries should only request the RlibLinkage when building a static FFI or when variant is StaticStd
164 if library.static() || library.MutatedProperties.VariantIsStaticStd {
165 return RlibLinkage
166 }
167 return DefaultLinkage
Ivan Lozano2b081132020-09-08 12:46:52 -0400168}
169
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200170func (library *libraryDecorator) source() bool {
171 return library.MutatedProperties.VariantIsSource
172}
173
Ivan Lozanoffee3342019-08-27 12:03:00 -0700174func (library *libraryDecorator) buildRlib() bool {
175 return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true)
176}
177
178func (library *libraryDecorator) buildDylib() bool {
179 return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true)
180}
181
Ivan Lozano52767be2019-10-18 14:49:46 -0700182func (library *libraryDecorator) buildShared() bool {
183 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
184}
185
186func (library *libraryDecorator) buildStatic() bool {
187 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
188}
189
Ivan Lozanoffee3342019-08-27 12:03:00 -0700190func (library *libraryDecorator) setRlib() {
191 library.MutatedProperties.VariantIsRlib = true
192 library.MutatedProperties.VariantIsDylib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700193 library.MutatedProperties.VariantIsStatic = false
194 library.MutatedProperties.VariantIsShared = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700195}
196
197func (library *libraryDecorator) setDylib() {
198 library.MutatedProperties.VariantIsRlib = false
199 library.MutatedProperties.VariantIsDylib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700200 library.MutatedProperties.VariantIsStatic = false
201 library.MutatedProperties.VariantIsShared = false
202}
203
Ivan Lozano2b081132020-09-08 12:46:52 -0400204func (library *libraryDecorator) setRlibStd() {
205 library.MutatedProperties.VariantIsStaticStd = true
206}
207
208func (library *libraryDecorator) setDylibStd() {
209 library.MutatedProperties.VariantIsStaticStd = false
210}
211
Ivan Lozano52767be2019-10-18 14:49:46 -0700212func (library *libraryDecorator) setShared() {
213 library.MutatedProperties.VariantIsStatic = false
214 library.MutatedProperties.VariantIsShared = true
215 library.MutatedProperties.VariantIsRlib = false
216 library.MutatedProperties.VariantIsDylib = false
217}
218
219func (library *libraryDecorator) setStatic() {
220 library.MutatedProperties.VariantIsStatic = true
221 library.MutatedProperties.VariantIsShared = false
222 library.MutatedProperties.VariantIsRlib = false
223 library.MutatedProperties.VariantIsDylib = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700224}
225
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200226func (library *libraryDecorator) setSource() {
227 library.MutatedProperties.VariantIsSource = true
228}
229
Ivan Lozano042504f2020-08-18 14:31:23 -0400230func (library *libraryDecorator) autoDep(ctx BaseModuleContext) autoDep {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700231 if library.rlib() || library.static() {
232 return rlibAutoDep
233 } else if library.dylib() || library.shared() {
234 return dylibAutoDep
235 } else {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200236 panic(fmt.Errorf("autoDep called on library %q that has no enabled variants.", ctx.ModuleName()))
Matthew Maurer0f003b12020-06-29 14:34:06 -0700237 }
238}
239
Ivan Lozanoffee3342019-08-27 12:03:00 -0700240var _ compiler = (*libraryDecorator)(nil)
Ivan Lozano52767be2019-10-18 14:49:46 -0700241var _ libraryInterface = (*libraryDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700242var _ exportedFlagsProducer = (*libraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700243
Matthew Maurer2ae05132020-06-23 14:28:53 -0700244// rust_library produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700245func RustLibraryFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700246 module, library := NewRustLibrary(android.HostAndDeviceSupported)
247 library.BuildOnlyRust()
248 return module.Init()
249}
250
251// rust_ffi produces all ffi variants.
252func RustFFIFactory() android.Module {
253 module, library := NewRustLibrary(android.HostAndDeviceSupported)
254 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700255 return module.Init()
256}
257
258// rust_library_dylib produces a dylib.
259func RustLibraryDylibFactory() android.Module {
260 module, library := NewRustLibrary(android.HostAndDeviceSupported)
261 library.BuildOnlyDylib()
262 return module.Init()
263}
264
265// rust_library_rlib produces an rlib.
266func RustLibraryRlibFactory() android.Module {
267 module, library := NewRustLibrary(android.HostAndDeviceSupported)
268 library.BuildOnlyRlib()
269 return module.Init()
270}
271
Matthew Maurer2ae05132020-06-23 14:28:53 -0700272// rust_ffi_shared produces a shared library.
273func RustFFISharedFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700274 module, library := NewRustLibrary(android.HostAndDeviceSupported)
275 library.BuildOnlyShared()
276 return module.Init()
277}
278
Matthew Maurer2ae05132020-06-23 14:28:53 -0700279// rust_ffi_static produces a static library.
280func RustFFIStaticFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700281 module, library := NewRustLibrary(android.HostAndDeviceSupported)
282 library.BuildOnlyStatic()
283 return module.Init()
284}
285
Matthew Maurer2ae05132020-06-23 14:28:53 -0700286// rust_library_host produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700287func RustLibraryHostFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700288 module, library := NewRustLibrary(android.HostSupported)
289 library.BuildOnlyRust()
290 return module.Init()
291}
292
293// rust_ffi_host produces all FFI variants.
294func RustFFIHostFactory() android.Module {
295 module, library := NewRustLibrary(android.HostSupported)
296 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700297 return module.Init()
298}
299
300// rust_library_dylib_host produces a dylib.
301func RustLibraryDylibHostFactory() android.Module {
302 module, library := NewRustLibrary(android.HostSupported)
303 library.BuildOnlyDylib()
304 return module.Init()
305}
306
307// rust_library_rlib_host produces an rlib.
308func RustLibraryRlibHostFactory() android.Module {
309 module, library := NewRustLibrary(android.HostSupported)
310 library.BuildOnlyRlib()
311 return module.Init()
312}
313
Matthew Maurer2ae05132020-06-23 14:28:53 -0700314// rust_ffi_static_host produces a static library.
315func RustFFIStaticHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700316 module, library := NewRustLibrary(android.HostSupported)
317 library.BuildOnlyStatic()
318 return module.Init()
319}
320
Matthew Maurer2ae05132020-06-23 14:28:53 -0700321// rust_ffi_shared_host produces an shared library.
322func RustFFISharedHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700323 module, library := NewRustLibrary(android.HostSupported)
324 library.BuildOnlyShared()
325 return module.Init()
326}
327
Matthew Maurer2ae05132020-06-23 14:28:53 -0700328func (library *libraryDecorator) BuildOnlyFFI() {
329 library.MutatedProperties.BuildDylib = false
330 library.MutatedProperties.BuildRlib = false
331 library.MutatedProperties.BuildShared = true
332 library.MutatedProperties.BuildStatic = true
333}
334
335func (library *libraryDecorator) BuildOnlyRust() {
336 library.MutatedProperties.BuildDylib = true
337 library.MutatedProperties.BuildRlib = true
338 library.MutatedProperties.BuildShared = false
339 library.MutatedProperties.BuildStatic = false
340}
341
Ivan Lozanoffee3342019-08-27 12:03:00 -0700342func (library *libraryDecorator) BuildOnlyDylib() {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700343 library.MutatedProperties.BuildDylib = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700344 library.MutatedProperties.BuildRlib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700345 library.MutatedProperties.BuildShared = false
346 library.MutatedProperties.BuildStatic = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700347}
348
349func (library *libraryDecorator) BuildOnlyRlib() {
350 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700351 library.MutatedProperties.BuildRlib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700352 library.MutatedProperties.BuildShared = false
353 library.MutatedProperties.BuildStatic = false
354}
355
356func (library *libraryDecorator) BuildOnlyStatic() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700357 library.MutatedProperties.BuildRlib = false
358 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700359 library.MutatedProperties.BuildShared = false
360 library.MutatedProperties.BuildStatic = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700361}
362
363func (library *libraryDecorator) BuildOnlyShared() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700364 library.MutatedProperties.BuildRlib = false
365 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700366 library.MutatedProperties.BuildStatic = false
367 library.MutatedProperties.BuildShared = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700368}
369
370func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400371 module := newModule(hod, android.MultilibBoth)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700372
373 library := &libraryDecorator{
374 MutatedProperties: LibraryMutatedProperties{
Matthew Maurer2ae05132020-06-23 14:28:53 -0700375 BuildDylib: false,
376 BuildRlib: false,
377 BuildShared: false,
378 BuildStatic: false,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700379 },
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800380 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Matthew Maurerbb3add12020-06-25 09:34:12 -0700381 flagExporter: NewFlagExporter(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700382 }
383
384 module.compiler = library
385
386 return module, library
387}
388
389func (library *libraryDecorator) compilerProps() []interface{} {
390 return append(library.baseCompiler.compilerProps(),
391 &library.Properties,
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200392 &library.MutatedProperties,
393 &library.stripper.StripProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700394}
395
Ivan Lozanof1c84332019-09-20 11:00:37 -0700396func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
397 deps = library.baseCompiler.compilerDeps(ctx, deps)
398
Ivan Lozano52767be2019-10-18 14:49:46 -0700399 if ctx.toolchain().Bionic() && (library.dylib() || library.shared()) {
Ivan Lozanobf63d002020-10-02 10:03:23 -0400400 deps = bionicDeps(deps, false)
Ivan Lozano12ee9ca2020-04-07 13:19:44 -0400401 deps.CrtBegin = "crtbegin_so"
402 deps.CrtEnd = "crtend_so"
Ivan Lozanof1c84332019-09-20 11:00:37 -0700403 }
404
405 return deps
406}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400407
408func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string {
409 return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
410}
411
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800412func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200413 flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800414 flags = library.baseCompiler.compilerFlags(ctx, flags)
415 if library.shared() || library.static() {
416 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
417 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400418 if library.shared() {
419 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
420 }
421
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800422 return flags
423}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700424
Ivan Lozanoffee3342019-08-27 12:03:00 -0700425func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200426 var outputFile android.ModuleOutPath
427 var fileName string
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400428 var srcPath android.Path
Ivan Lozanoffee3342019-08-27 12:03:00 -0700429
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400430 if library.sourceProvider != nil {
Ivan Lozano57f434e2020-10-28 09:32:10 -0400431 // Assume the first source from the source provider is the library entry point.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400432 srcPath = library.sourceProvider.Srcs()[0]
Ivan Lozano9d74a522020-12-01 09:25:22 -0500433 deps.srcProviderFiles = append(deps.srcProviderFiles, library.sourceProvider.Srcs()...)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400434 } else {
435 srcPath, _ = srcPathFromModuleSrcs(ctx, library.baseCompiler.Properties.Srcs)
436 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700437
438 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano2093af22020-08-25 12:48:19 -0400439 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700440
Matthew Maurer46c46cc2020-01-13 16:34:34 -0800441 if library.dylib() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700442 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
443 // https://github.com/rust-lang/rust/issues/19680
444 // https://github.com/rust-lang/rust/issues/34909
445 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
446 }
447
Ivan Lozanoffee3342019-08-27 12:03:00 -0700448 if library.rlib() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200449 fileName = library.getStem(ctx) + ctx.toolchain().RlibSuffix()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700450 outputFile = android.PathForModuleOut(ctx, fileName)
451
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400452 outputs := TransformSrctoRlib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
453 library.coverageFile = outputs.coverageFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700454 } else if library.dylib() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200455 fileName = library.getStem(ctx) + ctx.toolchain().DylibSuffix()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700456 outputFile = android.PathForModuleOut(ctx, fileName)
457
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400458 outputs := TransformSrctoDylib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
459 library.coverageFile = outputs.coverageFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700460 } else if library.static() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200461 fileName = library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
Ivan Lozano52767be2019-10-18 14:49:46 -0700462 outputFile = android.PathForModuleOut(ctx, fileName)
463
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400464 outputs := TransformSrctoStatic(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
465 library.coverageFile = outputs.coverageFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700466 } else if library.shared() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200467 fileName = library.sharedLibFilename(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700468 outputFile = android.PathForModuleOut(ctx, fileName)
469
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400470 outputs := TransformSrctoShared(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
471 library.coverageFile = outputs.coverageFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700472 }
473
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200474 if !library.rlib() && library.stripper.NeedsStrip(ctx) {
475 strippedOutputFile := android.PathForModuleOut(ctx, "stripped", fileName)
476 library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
477 library.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
478 }
479
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400480 var coverageFiles android.Paths
481 if library.coverageFile != nil {
482 coverageFiles = append(coverageFiles, library.coverageFile)
483 }
484 if len(deps.coverageFiles) > 0 {
485 coverageFiles = append(coverageFiles, deps.coverageFiles...)
486 }
487 library.coverageOutputZipFile = TransformCoverageFilesToZip(ctx, coverageFiles, library.getStem(ctx))
488
Ivan Lozano52767be2019-10-18 14:49:46 -0700489 if library.rlib() || library.dylib() {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700490 library.flagExporter.exportLinkDirs(deps.linkDirs...)
491 library.flagExporter.exportDepFlags(deps.depFlags...)
492 library.flagExporter.exportLinkObjects(deps.linkObjects...)
Ivan Lozano52767be2019-10-18 14:49:46 -0700493 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700494
Colin Cross0de8a1e2020-09-18 14:15:30 -0700495 if library.static() || library.shared() {
496 ctx.SetProvider(cc.FlagExporterInfoProvider, cc.FlagExporterInfo{
497 IncludeDirs: library.includeDirs,
498 })
499 }
500
501 if library.shared() {
502 ctx.SetProvider(cc.SharedLibraryInfoProvider, cc.SharedLibraryInfo{
503 SharedLibrary: outputFile,
504 UnstrippedSharedLibrary: outputFile,
505 })
506 }
507
508 if library.static() {
509 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(outputFile).Build()
510 ctx.SetProvider(cc.StaticLibraryInfoProvider, cc.StaticLibraryInfo{
511 StaticLibrary: outputFile,
512
513 TransitiveStaticLibrariesForOrdering: depSet,
514 })
515 }
516
517 library.flagExporter.setProvider(ctx)
518
Ivan Lozanoffee3342019-08-27 12:03:00 -0700519 return outputFile
520}
521
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700522func (library *libraryDecorator) getStem(ctx ModuleContext) string {
523 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
524 validateLibraryStem(ctx, stem, library.crateName())
525
526 return stem + String(library.baseCompiler.Properties.Suffix)
527}
528
Ivan Lozano2b081132020-09-08 12:46:52 -0400529func (library *libraryDecorator) install(ctx ModuleContext) {
530 // Only shared and dylib variants make sense to install.
531 if library.shared() || library.dylib() {
532 library.baseCompiler.install(ctx)
533 }
534}
535
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400536func (library *libraryDecorator) Disabled() bool {
537 return library.MutatedProperties.VariantIsDisabled
538}
539
540func (library *libraryDecorator) SetDisabled() {
541 library.MutatedProperties.VariantIsDisabled = true
542}
543
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700544var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
545
546func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
547 if crate_name == "" {
548 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
549 }
550
551 // crate_names are used for the library output file, and rustc expects these
552 // to be alphanumeric with underscores allowed.
553 if validCrateName.MatchString(crate_name) {
554 ctx.PropertyErrorf("crate_name",
555 "library crate_names must be alphanumeric with underscores allowed")
556 }
557
558 // Libraries are expected to begin with "lib" followed by the crate_name
559 if !strings.HasPrefix(filename, "lib"+crate_name) {
560 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
561 }
562}
563
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200564// LibraryMutator mutates the libraries into variants according to the
565// build{Rlib,Dylib} attributes.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700566func LibraryMutator(mctx android.BottomUpMutatorContext) {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200567 // Only mutate on Rust libraries.
568 m, ok := mctx.Module().(*Module)
569 if !ok || m.compiler == nil {
570 return
571 }
572 library, ok := m.compiler.(libraryInterface)
573 if !ok {
574 return
575 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400576
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200577 var variants []string
578 // The source variant is used for SourceProvider modules. The other variants (i.e. rlib and dylib)
579 // depend on this variant. It must be the first variant to be declared.
580 sourceVariant := false
581 if m.sourceProvider != nil {
582 variants = append(variants, "source")
583 sourceVariant = true
584 }
585 if library.buildRlib() {
586 variants = append(variants, rlibVariation)
587 }
588 if library.buildDylib() {
589 variants = append(variants, dylibVariation)
590 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400591
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200592 if len(variants) == 0 {
593 return
594 }
595 modules := mctx.CreateLocalVariations(variants...)
596
597 // The order of the variations (modules) matches the variant names provided. Iterate
598 // through the new variation modules and set their mutated properties.
599 for i, v := range modules {
600 switch variants[i] {
601 case rlibVariation:
602 v.(*Module).compiler.(libraryInterface).setRlib()
603 case dylibVariation:
604 v.(*Module).compiler.(libraryInterface).setDylib()
605 case "source":
606 v.(*Module).compiler.(libraryInterface).setSource()
607 // The source variant does not produce any library.
608 // Disable the compilation steps.
609 v.(*Module).compiler.SetDisabled()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700610 }
611 }
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200612
613 // If a source variant is created, add an inter-variant dependency
614 // between the other variants and the source variant.
615 if sourceVariant {
616 sv := modules[0]
617 for _, v := range modules[1:] {
618 if !v.Enabled() {
619 continue
620 }
621 mctx.AddInterVariantDependency(sourceDepTag, v, sv)
622 }
623 // Alias the source variation so it can be named directly in "srcs" properties.
624 mctx.AliasVariation("source")
625 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700626}
Ivan Lozano2b081132020-09-08 12:46:52 -0400627
628func LibstdMutator(mctx android.BottomUpMutatorContext) {
629 if m, ok := mctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() {
630 switch library := m.compiler.(type) {
631 case libraryInterface:
632 // Only create a variant if a library is actually being built.
633 if library.rlib() && !library.sysroot() {
634 variants := []string{"rlib-std", "dylib-std"}
635 modules := mctx.CreateLocalVariations(variants...)
636
637 rlib := modules[0].(*Module)
638 dylib := modules[1].(*Module)
639 rlib.compiler.(libraryInterface).setRlibStd()
640 dylib.compiler.(libraryInterface).setDylibStd()
641 rlib.Properties.SubName += RlibStdlibSuffix
642 dylib.Properties.SubName += DylibStdlibSuffix
643 }
644 }
645 }
646}