blob: 3bba089e6390743331dc8370a15f4273daff4b2c [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 Lozanodd055472020-09-28 13:22:45 -0400161func (library *libraryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
162 // libraries should only request the RlibLinkage when building a static FFI or when variant is StaticStd
163 if library.static() || library.MutatedProperties.VariantIsStaticStd {
164 return RlibLinkage
165 }
166 return DefaultLinkage
Ivan Lozano2b081132020-09-08 12:46:52 -0400167}
168
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200169func (library *libraryDecorator) source() bool {
170 return library.MutatedProperties.VariantIsSource
171}
172
Ivan Lozanoffee3342019-08-27 12:03:00 -0700173func (library *libraryDecorator) buildRlib() bool {
174 return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true)
175}
176
177func (library *libraryDecorator) buildDylib() bool {
178 return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true)
179}
180
Ivan Lozano52767be2019-10-18 14:49:46 -0700181func (library *libraryDecorator) buildShared() bool {
182 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
183}
184
185func (library *libraryDecorator) buildStatic() bool {
186 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
187}
188
Ivan Lozanoffee3342019-08-27 12:03:00 -0700189func (library *libraryDecorator) setRlib() {
190 library.MutatedProperties.VariantIsRlib = true
191 library.MutatedProperties.VariantIsDylib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700192 library.MutatedProperties.VariantIsStatic = false
193 library.MutatedProperties.VariantIsShared = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700194}
195
196func (library *libraryDecorator) setDylib() {
197 library.MutatedProperties.VariantIsRlib = false
198 library.MutatedProperties.VariantIsDylib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700199 library.MutatedProperties.VariantIsStatic = false
200 library.MutatedProperties.VariantIsShared = false
201}
202
Ivan Lozano2b081132020-09-08 12:46:52 -0400203func (library *libraryDecorator) setRlibStd() {
204 library.MutatedProperties.VariantIsStaticStd = true
205}
206
207func (library *libraryDecorator) setDylibStd() {
208 library.MutatedProperties.VariantIsStaticStd = false
209}
210
Ivan Lozano52767be2019-10-18 14:49:46 -0700211func (library *libraryDecorator) setShared() {
212 library.MutatedProperties.VariantIsStatic = false
213 library.MutatedProperties.VariantIsShared = true
214 library.MutatedProperties.VariantIsRlib = false
215 library.MutatedProperties.VariantIsDylib = false
216}
217
218func (library *libraryDecorator) setStatic() {
219 library.MutatedProperties.VariantIsStatic = true
220 library.MutatedProperties.VariantIsShared = false
221 library.MutatedProperties.VariantIsRlib = false
222 library.MutatedProperties.VariantIsDylib = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700223}
224
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200225func (library *libraryDecorator) setSource() {
226 library.MutatedProperties.VariantIsSource = true
227}
228
Ivan Lozano042504f2020-08-18 14:31:23 -0400229func (library *libraryDecorator) autoDep(ctx BaseModuleContext) autoDep {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700230 if library.rlib() || library.static() {
231 return rlibAutoDep
232 } else if library.dylib() || library.shared() {
233 return dylibAutoDep
234 } else {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200235 panic(fmt.Errorf("autoDep called on library %q that has no enabled variants.", ctx.ModuleName()))
Matthew Maurer0f003b12020-06-29 14:34:06 -0700236 }
237}
238
Ivan Lozanoffee3342019-08-27 12:03:00 -0700239var _ compiler = (*libraryDecorator)(nil)
Ivan Lozano52767be2019-10-18 14:49:46 -0700240var _ libraryInterface = (*libraryDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700241var _ exportedFlagsProducer = (*libraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700242
Matthew Maurer2ae05132020-06-23 14:28:53 -0700243// rust_library produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700244func RustLibraryFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700245 module, library := NewRustLibrary(android.HostAndDeviceSupported)
246 library.BuildOnlyRust()
247 return module.Init()
248}
249
250// rust_ffi produces all ffi variants.
251func RustFFIFactory() android.Module {
252 module, library := NewRustLibrary(android.HostAndDeviceSupported)
253 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700254 return module.Init()
255}
256
257// rust_library_dylib produces a dylib.
258func RustLibraryDylibFactory() android.Module {
259 module, library := NewRustLibrary(android.HostAndDeviceSupported)
260 library.BuildOnlyDylib()
261 return module.Init()
262}
263
264// rust_library_rlib produces an rlib.
265func RustLibraryRlibFactory() android.Module {
266 module, library := NewRustLibrary(android.HostAndDeviceSupported)
267 library.BuildOnlyRlib()
268 return module.Init()
269}
270
Matthew Maurer2ae05132020-06-23 14:28:53 -0700271// rust_ffi_shared produces a shared library.
272func RustFFISharedFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700273 module, library := NewRustLibrary(android.HostAndDeviceSupported)
274 library.BuildOnlyShared()
275 return module.Init()
276}
277
Matthew Maurer2ae05132020-06-23 14:28:53 -0700278// rust_ffi_static produces a static library.
279func RustFFIStaticFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700280 module, library := NewRustLibrary(android.HostAndDeviceSupported)
281 library.BuildOnlyStatic()
282 return module.Init()
283}
284
Matthew Maurer2ae05132020-06-23 14:28:53 -0700285// rust_library_host produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700286func RustLibraryHostFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700287 module, library := NewRustLibrary(android.HostSupported)
288 library.BuildOnlyRust()
289 return module.Init()
290}
291
292// rust_ffi_host produces all FFI variants.
293func RustFFIHostFactory() android.Module {
294 module, library := NewRustLibrary(android.HostSupported)
295 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700296 return module.Init()
297}
298
299// rust_library_dylib_host produces a dylib.
300func RustLibraryDylibHostFactory() android.Module {
301 module, library := NewRustLibrary(android.HostSupported)
302 library.BuildOnlyDylib()
303 return module.Init()
304}
305
306// rust_library_rlib_host produces an rlib.
307func RustLibraryRlibHostFactory() android.Module {
308 module, library := NewRustLibrary(android.HostSupported)
309 library.BuildOnlyRlib()
310 return module.Init()
311}
312
Matthew Maurer2ae05132020-06-23 14:28:53 -0700313// rust_ffi_static_host produces a static library.
314func RustFFIStaticHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700315 module, library := NewRustLibrary(android.HostSupported)
316 library.BuildOnlyStatic()
317 return module.Init()
318}
319
Matthew Maurer2ae05132020-06-23 14:28:53 -0700320// rust_ffi_shared_host produces an shared library.
321func RustFFISharedHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700322 module, library := NewRustLibrary(android.HostSupported)
323 library.BuildOnlyShared()
324 return module.Init()
325}
326
Matthew Maurer2ae05132020-06-23 14:28:53 -0700327func (library *libraryDecorator) BuildOnlyFFI() {
328 library.MutatedProperties.BuildDylib = false
329 library.MutatedProperties.BuildRlib = false
330 library.MutatedProperties.BuildShared = true
331 library.MutatedProperties.BuildStatic = true
332}
333
334func (library *libraryDecorator) BuildOnlyRust() {
335 library.MutatedProperties.BuildDylib = true
336 library.MutatedProperties.BuildRlib = true
337 library.MutatedProperties.BuildShared = false
338 library.MutatedProperties.BuildStatic = false
339}
340
Ivan Lozanoffee3342019-08-27 12:03:00 -0700341func (library *libraryDecorator) BuildOnlyDylib() {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700342 library.MutatedProperties.BuildDylib = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700343 library.MutatedProperties.BuildRlib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700344 library.MutatedProperties.BuildShared = false
345 library.MutatedProperties.BuildStatic = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700346}
347
348func (library *libraryDecorator) BuildOnlyRlib() {
349 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700350 library.MutatedProperties.BuildRlib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700351 library.MutatedProperties.BuildShared = false
352 library.MutatedProperties.BuildStatic = false
353}
354
355func (library *libraryDecorator) BuildOnlyStatic() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700356 library.MutatedProperties.BuildRlib = false
357 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700358 library.MutatedProperties.BuildShared = false
359 library.MutatedProperties.BuildStatic = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700360}
361
362func (library *libraryDecorator) BuildOnlyShared() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700363 library.MutatedProperties.BuildRlib = false
364 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700365 library.MutatedProperties.BuildStatic = false
366 library.MutatedProperties.BuildShared = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700367}
368
369func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400370 module := newModule(hod, android.MultilibBoth)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700371
372 library := &libraryDecorator{
373 MutatedProperties: LibraryMutatedProperties{
Matthew Maurer2ae05132020-06-23 14:28:53 -0700374 BuildDylib: false,
375 BuildRlib: false,
376 BuildShared: false,
377 BuildStatic: false,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700378 },
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800379 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Matthew Maurerbb3add12020-06-25 09:34:12 -0700380 flagExporter: NewFlagExporter(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700381 }
382
383 module.compiler = library
384
385 return module, library
386}
387
388func (library *libraryDecorator) compilerProps() []interface{} {
389 return append(library.baseCompiler.compilerProps(),
390 &library.Properties,
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200391 &library.MutatedProperties,
392 &library.stripper.StripProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700393}
394
Ivan Lozanof1c84332019-09-20 11:00:37 -0700395func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
396 deps = library.baseCompiler.compilerDeps(ctx, deps)
397
Ivan Lozano52767be2019-10-18 14:49:46 -0700398 if ctx.toolchain().Bionic() && (library.dylib() || library.shared()) {
Ivan Lozanobf63d002020-10-02 10:03:23 -0400399 deps = bionicDeps(deps, false)
Ivan Lozano12ee9ca2020-04-07 13:19:44 -0400400 deps.CrtBegin = "crtbegin_so"
401 deps.CrtEnd = "crtend_so"
Ivan Lozanof1c84332019-09-20 11:00:37 -0700402 }
403
404 return deps
405}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400406
407func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string {
408 return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
409}
410
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800411func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200412 flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800413 flags = library.baseCompiler.compilerFlags(ctx, flags)
414 if library.shared() || library.static() {
415 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
416 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400417 if library.shared() {
418 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
419 }
420
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800421 return flags
422}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700423
Ivan Lozanoffee3342019-08-27 12:03:00 -0700424func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200425 var outputFile android.ModuleOutPath
426 var fileName string
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400427 var srcPath android.Path
Ivan Lozanoffee3342019-08-27 12:03:00 -0700428
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400429 if library.sourceProvider != nil {
430 srcPath = library.sourceProvider.Srcs()[0]
431 } else {
432 srcPath, _ = srcPathFromModuleSrcs(ctx, library.baseCompiler.Properties.Srcs)
433 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700434
435 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano2093af22020-08-25 12:48:19 -0400436 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700437
Matthew Maurer46c46cc2020-01-13 16:34:34 -0800438 if library.dylib() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700439 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
440 // https://github.com/rust-lang/rust/issues/19680
441 // https://github.com/rust-lang/rust/issues/34909
442 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
443 }
444
Ivan Lozanoffee3342019-08-27 12:03:00 -0700445 if library.rlib() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200446 fileName = library.getStem(ctx) + ctx.toolchain().RlibSuffix()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700447 outputFile = android.PathForModuleOut(ctx, fileName)
448
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400449 outputs := TransformSrctoRlib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
450 library.coverageFile = outputs.coverageFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700451 } else if library.dylib() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200452 fileName = library.getStem(ctx) + ctx.toolchain().DylibSuffix()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700453 outputFile = android.PathForModuleOut(ctx, fileName)
454
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400455 outputs := TransformSrctoDylib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
456 library.coverageFile = outputs.coverageFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700457 } else if library.static() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200458 fileName = library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
Ivan Lozano52767be2019-10-18 14:49:46 -0700459 outputFile = android.PathForModuleOut(ctx, fileName)
460
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400461 outputs := TransformSrctoStatic(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
462 library.coverageFile = outputs.coverageFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700463 } else if library.shared() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200464 fileName = library.sharedLibFilename(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700465 outputFile = android.PathForModuleOut(ctx, fileName)
466
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400467 outputs := TransformSrctoShared(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
468 library.coverageFile = outputs.coverageFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700469 }
470
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200471 if !library.rlib() && library.stripper.NeedsStrip(ctx) {
472 strippedOutputFile := android.PathForModuleOut(ctx, "stripped", fileName)
473 library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
474 library.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
475 }
476
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400477 var coverageFiles android.Paths
478 if library.coverageFile != nil {
479 coverageFiles = append(coverageFiles, library.coverageFile)
480 }
481 if len(deps.coverageFiles) > 0 {
482 coverageFiles = append(coverageFiles, deps.coverageFiles...)
483 }
484 library.coverageOutputZipFile = TransformCoverageFilesToZip(ctx, coverageFiles, library.getStem(ctx))
485
Ivan Lozano52767be2019-10-18 14:49:46 -0700486 if library.rlib() || library.dylib() {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700487 library.exportLinkDirs(deps.linkDirs...)
488 library.exportDepFlags(deps.depFlags...)
Ivan Lozano2093af22020-08-25 12:48:19 -0400489 library.exportLinkObjects(deps.linkObjects...)
Ivan Lozano52767be2019-10-18 14:49:46 -0700490 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700491
492 return outputFile
493}
494
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700495func (library *libraryDecorator) getStem(ctx ModuleContext) string {
496 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
497 validateLibraryStem(ctx, stem, library.crateName())
498
499 return stem + String(library.baseCompiler.Properties.Suffix)
500}
501
Ivan Lozano2b081132020-09-08 12:46:52 -0400502func (library *libraryDecorator) install(ctx ModuleContext) {
503 // Only shared and dylib variants make sense to install.
504 if library.shared() || library.dylib() {
505 library.baseCompiler.install(ctx)
506 }
507}
508
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400509func (library *libraryDecorator) Disabled() bool {
510 return library.MutatedProperties.VariantIsDisabled
511}
512
513func (library *libraryDecorator) SetDisabled() {
514 library.MutatedProperties.VariantIsDisabled = true
515}
516
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700517var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
518
519func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
520 if crate_name == "" {
521 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
522 }
523
524 // crate_names are used for the library output file, and rustc expects these
525 // to be alphanumeric with underscores allowed.
526 if validCrateName.MatchString(crate_name) {
527 ctx.PropertyErrorf("crate_name",
528 "library crate_names must be alphanumeric with underscores allowed")
529 }
530
531 // Libraries are expected to begin with "lib" followed by the crate_name
532 if !strings.HasPrefix(filename, "lib"+crate_name) {
533 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
534 }
535}
536
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200537// LibraryMutator mutates the libraries into variants according to the
538// build{Rlib,Dylib} attributes.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700539func LibraryMutator(mctx android.BottomUpMutatorContext) {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200540 // Only mutate on Rust libraries.
541 m, ok := mctx.Module().(*Module)
542 if !ok || m.compiler == nil {
543 return
544 }
545 library, ok := m.compiler.(libraryInterface)
546 if !ok {
547 return
548 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400549
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200550 var variants []string
551 // The source variant is used for SourceProvider modules. The other variants (i.e. rlib and dylib)
552 // depend on this variant. It must be the first variant to be declared.
553 sourceVariant := false
554 if m.sourceProvider != nil {
555 variants = append(variants, "source")
556 sourceVariant = true
557 }
558 if library.buildRlib() {
559 variants = append(variants, rlibVariation)
560 }
561 if library.buildDylib() {
562 variants = append(variants, dylibVariation)
563 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400564
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200565 if len(variants) == 0 {
566 return
567 }
568 modules := mctx.CreateLocalVariations(variants...)
569
570 // The order of the variations (modules) matches the variant names provided. Iterate
571 // through the new variation modules and set their mutated properties.
572 for i, v := range modules {
573 switch variants[i] {
574 case rlibVariation:
575 v.(*Module).compiler.(libraryInterface).setRlib()
576 case dylibVariation:
577 v.(*Module).compiler.(libraryInterface).setDylib()
578 case "source":
579 v.(*Module).compiler.(libraryInterface).setSource()
580 // The source variant does not produce any library.
581 // Disable the compilation steps.
582 v.(*Module).compiler.SetDisabled()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700583 }
584 }
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200585
586 // If a source variant is created, add an inter-variant dependency
587 // between the other variants and the source variant.
588 if sourceVariant {
589 sv := modules[0]
590 for _, v := range modules[1:] {
591 if !v.Enabled() {
592 continue
593 }
594 mctx.AddInterVariantDependency(sourceDepTag, v, sv)
595 }
596 // Alias the source variation so it can be named directly in "srcs" properties.
597 mctx.AliasVariation("source")
598 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700599}
Ivan Lozano2b081132020-09-08 12:46:52 -0400600
601func LibstdMutator(mctx android.BottomUpMutatorContext) {
602 if m, ok := mctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() {
603 switch library := m.compiler.(type) {
604 case libraryInterface:
605 // Only create a variant if a library is actually being built.
606 if library.rlib() && !library.sysroot() {
607 variants := []string{"rlib-std", "dylib-std"}
608 modules := mctx.CreateLocalVariations(variants...)
609
610 rlib := modules[0].(*Module)
611 dylib := modules[1].(*Module)
612 rlib.compiler.(libraryInterface).setRlibStd()
613 dylib.compiler.(libraryInterface).setDylibStd()
614 rlib.Properties.SubName += RlibStdlibSuffix
615 dylib.Properties.SubName += DylibStdlibSuffix
616 }
617 }
618 }
619}