blob: 9f9c40281346641fbdb4659235ae8f0c3d44d67a [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 (
Matthew Maurera28404a2023-11-20 23:33:28 +000018 "errors"
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +020019 "fmt"
Ivan Lozanoad8b18b2019-10-31 19:38:29 -070020 "regexp"
21 "strings"
22
Colin Cross8a49a3d2024-05-20 12:22:27 -070023 "github.com/google/blueprint"
Colin Crossa14fb6a2024-10-23 16:57:06 -070024 "github.com/google/blueprint/depset"
Colin Cross8a49a3d2024-05-20 12:22:27 -070025
Ivan Lozanoffee3342019-08-27 12:03:00 -070026 "android/soong/android"
Colin Cross0de8a1e2020-09-18 14:15:30 -070027 "android/soong/cc"
Ivan Lozanoffee3342019-08-27 12:03:00 -070028)
29
Ivan Lozano2b081132020-09-08 12:46:52 -040030var (
Ivan Lozano4df02572023-06-15 14:21:09 -040031 RlibStdlibSuffix = ".rlib-std"
Ivan Lozano2b081132020-09-08 12:46:52 -040032)
33
Ivan Lozanoffee3342019-08-27 12:03:00 -070034func init() {
35 android.RegisterModuleType("rust_library", RustLibraryFactory)
36 android.RegisterModuleType("rust_library_dylib", RustLibraryDylibFactory)
37 android.RegisterModuleType("rust_library_rlib", RustLibraryRlibFactory)
38 android.RegisterModuleType("rust_library_host", RustLibraryHostFactory)
39 android.RegisterModuleType("rust_library_host_dylib", RustLibraryDylibHostFactory)
40 android.RegisterModuleType("rust_library_host_rlib", RustLibraryRlibHostFactory)
Matthew Maurer2ae05132020-06-23 14:28:53 -070041 android.RegisterModuleType("rust_ffi", RustFFIFactory)
42 android.RegisterModuleType("rust_ffi_shared", RustFFISharedFactory)
Ivan Lozano0a468a42024-05-13 21:03:34 -040043 android.RegisterModuleType("rust_ffi_rlib", RustFFIRlibFactory)
Matthew Maurer2ae05132020-06-23 14:28:53 -070044 android.RegisterModuleType("rust_ffi_host", RustFFIHostFactory)
45 android.RegisterModuleType("rust_ffi_host_shared", RustFFISharedHostFactory)
Ivan Lozano0a468a42024-05-13 21:03:34 -040046 android.RegisterModuleType("rust_ffi_host_rlib", RustFFIRlibHostFactory)
47
48 // TODO: Remove when all instances of rust_ffi_static have been switched to rust_ffi_rlib
Ivan Lozanofd47b1a2024-05-17 14:13:41 -040049 // Alias rust_ffi_static to the rust_ffi_rlib factory
50 android.RegisterModuleType("rust_ffi_static", RustFFIRlibFactory)
51 android.RegisterModuleType("rust_ffi_host_static", RustFFIRlibHostFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070052}
53
54type VariantLibraryProperties struct {
Matthew Maurerc761eec2020-06-25 00:47:46 -070055 Enabled *bool `android:"arch_variant"`
56 Srcs []string `android:"path,arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070057}
58
59type LibraryCompilerProperties struct {
Ivan Lozano52767be2019-10-18 14:49:46 -070060 Rlib VariantLibraryProperties `android:"arch_variant"`
61 Dylib VariantLibraryProperties `android:"arch_variant"`
62 Shared VariantLibraryProperties `android:"arch_variant"`
63 Static VariantLibraryProperties `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070064
Ivan Lozanof033ca62024-03-21 13:43:14 -040065 // TODO: Remove this when all instances of Include_dirs have been removed from rust_ffi modules.
66 // path to include directories to pass to cc_* modules, only relevant for static/shared variants (deprecated, use export_include_dirs instead).
Ivan Lozano52767be2019-10-18 14:49:46 -070067 Include_dirs []string `android:"path,arch_variant"`
Ivan Lozano2b081132020-09-08 12:46:52 -040068
Ivan Lozanof033ca62024-03-21 13:43:14 -040069 // path to include directories to export to cc_* modules, only relevant for static/shared variants.
70 Export_include_dirs []string `android:"path,arch_variant"`
71
Ivan Lozanof4589012024-11-20 22:18:11 +000072 // Version script to pass to the linker. By default this will replace the
73 // implicit rustc emitted version script to mirror expected behavior in CC.
74 // This is only relevant for rust_ffi_shared modules which are exposing a
75 // versioned C API.
76 Version_script *string `android:"path,arch_variant"`
77
78 // A version_script formatted text file with additional symbols to export
79 // for rust shared or dylibs which the rustc compiler does not automatically
80 // export, e.g. additional symbols from whole_static_libs. Unlike
81 // Version_script, this is not meant to imply a stable API.
82 Extra_exported_symbols *string `android:"path,arch_variant"`
83
Ivan Lozano2b081132020-09-08 12:46:52 -040084 // Whether this library is part of the Rust toolchain sysroot.
85 Sysroot *bool
Ashutosh Agarwal46e4fad2024-08-27 17:13:12 +000086
87 // Exclude this rust_ffi target from being included in APEXes.
88 // TODO(b/362509506): remove this once stubs are properly supported by rust_ffi targets.
89 Apex_exclude *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070090}
91
92type LibraryMutatedProperties struct {
Ivan Lozanoffee3342019-08-27 12:03:00 -070093 // Build a dylib variant
94 BuildDylib bool `blueprint:"mutated"`
95 // Build an rlib variant
96 BuildRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070097 // Build a shared library variant
98 BuildShared bool `blueprint:"mutated"`
99 // Build a static library variant
100 BuildStatic bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -0700101
102 // This variant is a dylib
103 VariantIsDylib bool `blueprint:"mutated"`
104 // This variant is an rlib
105 VariantIsRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -0700106 // This variant is a shared library
107 VariantIsShared bool `blueprint:"mutated"`
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200108 // This variant is a source provider
109 VariantIsSource bool `blueprint:"mutated"`
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400110
111 // This variant is disabled and should not be compiled
112 // (used for SourceProvider variants that produce only source)
113 VariantIsDisabled bool `blueprint:"mutated"`
Ivan Lozano2b081132020-09-08 12:46:52 -0400114
115 // Whether this library variant should be link libstd via rlibs
116 VariantIsStaticStd bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -0700117}
118
119type libraryDecorator struct {
120 *baseCompiler
Matthew Maurerbb3add12020-06-25 09:34:12 -0700121 *flagExporter
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200122 stripper Stripper
Ivan Lozanoffee3342019-08-27 12:03:00 -0700123
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400124 Properties LibraryCompilerProperties
125 MutatedProperties LibraryMutatedProperties
126 includeDirs android.Paths
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400127 sourceProvider SourceProvider
Ivan Lozano1921e802021-05-20 13:39:16 -0400128
Ivan Lozano0a468a42024-05-13 21:03:34 -0400129 isFFI bool
130
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400131 // table-of-contents file for cdylib crates to optimize out relinking when possible
132 tocFile android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700133}
134
135type libraryInterface interface {
136 rlib() bool
137 dylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700138 static() bool
139 shared() bool
Ivan Lozano2b081132020-09-08 12:46:52 -0400140 sysroot() bool
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200141 source() bool
Ashutosh Agarwal46e4fad2024-08-27 17:13:12 +0000142 apexExclude() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700143
144 // Returns true if the build options for the module have selected a particular build type
145 buildRlib() bool
146 buildDylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700147 buildShared() bool
148 buildStatic() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700149
150 // Sets a particular variant type
151 setRlib()
152 setDylib()
Ivan Lozano52767be2019-10-18 14:49:46 -0700153 setShared()
154 setStatic()
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200155 setSource()
Ivan Lozano52767be2019-10-18 14:49:46 -0700156
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400157 // libstd linkage functions
158 rlibStd() bool
Ivan Lozano2b081132020-09-08 12:46:52 -0400159 setRlibStd()
160 setDylibStd()
161
Ivan Lozano52767be2019-10-18 14:49:46 -0700162 // Build a specific library variant
Matthew Maurer2ae05132020-06-23 14:28:53 -0700163 BuildOnlyFFI()
164 BuildOnlyRust()
Ivan Lozano52767be2019-10-18 14:49:46 -0700165 BuildOnlyRlib()
166 BuildOnlyDylib()
167 BuildOnlyStatic()
168 BuildOnlyShared()
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400169
170 toc() android.OptionalPath
Ivan Lozano0a468a42024-05-13 21:03:34 -0400171
172 isFFILibrary() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700173}
174
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400175func (library *libraryDecorator) nativeCoverage() bool {
176 return true
177}
178
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400179func (library *libraryDecorator) toc() android.OptionalPath {
180 return library.tocFile
181}
182
Ivan Lozanoffee3342019-08-27 12:03:00 -0700183func (library *libraryDecorator) rlib() bool {
184 return library.MutatedProperties.VariantIsRlib
185}
186
Ivan Lozano2b081132020-09-08 12:46:52 -0400187func (library *libraryDecorator) sysroot() bool {
188 return Bool(library.Properties.Sysroot)
189}
190
Ivan Lozanoffee3342019-08-27 12:03:00 -0700191func (library *libraryDecorator) dylib() bool {
192 return library.MutatedProperties.VariantIsDylib
193}
194
Ivan Lozano52767be2019-10-18 14:49:46 -0700195func (library *libraryDecorator) shared() bool {
196 return library.MutatedProperties.VariantIsShared
197}
198
199func (library *libraryDecorator) static() bool {
Colin Cross17f9dc52024-07-01 20:05:54 -0700200 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700201}
202
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200203func (library *libraryDecorator) source() bool {
204 return library.MutatedProperties.VariantIsSource
205}
206
Ashutosh Agarwal46e4fad2024-08-27 17:13:12 +0000207func (library *libraryDecorator) apexExclude() bool {
208 return Bool(library.Properties.Apex_exclude)
209}
210
Ivan Lozanoffee3342019-08-27 12:03:00 -0700211func (library *libraryDecorator) buildRlib() bool {
212 return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true)
213}
214
215func (library *libraryDecorator) buildDylib() bool {
216 return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true)
217}
218
Ivan Lozano52767be2019-10-18 14:49:46 -0700219func (library *libraryDecorator) buildShared() bool {
220 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
221}
222
223func (library *libraryDecorator) buildStatic() bool {
224 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
225}
226
Ivan Lozanoffee3342019-08-27 12:03:00 -0700227func (library *libraryDecorator) setRlib() {
228 library.MutatedProperties.VariantIsRlib = true
229 library.MutatedProperties.VariantIsDylib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700230 library.MutatedProperties.VariantIsShared = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700231}
232
233func (library *libraryDecorator) setDylib() {
234 library.MutatedProperties.VariantIsRlib = false
235 library.MutatedProperties.VariantIsDylib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700236 library.MutatedProperties.VariantIsShared = false
237}
238
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400239func (library *libraryDecorator) rlibStd() bool {
240 return library.MutatedProperties.VariantIsStaticStd
241}
242
Ivan Lozano2b081132020-09-08 12:46:52 -0400243func (library *libraryDecorator) setRlibStd() {
244 library.MutatedProperties.VariantIsStaticStd = true
245}
246
247func (library *libraryDecorator) setDylibStd() {
248 library.MutatedProperties.VariantIsStaticStd = false
249}
250
Ivan Lozano52767be2019-10-18 14:49:46 -0700251func (library *libraryDecorator) setShared() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700252 library.MutatedProperties.VariantIsShared = true
253 library.MutatedProperties.VariantIsRlib = false
254 library.MutatedProperties.VariantIsDylib = false
255}
256
257func (library *libraryDecorator) setStatic() {
Colin Cross17f9dc52024-07-01 20:05:54 -0700258 panic(fmt.Errorf("static variant is not supported for rust modules, use the rlib variant instead"))
Ivan Lozanoffee3342019-08-27 12:03:00 -0700259}
260
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200261func (library *libraryDecorator) setSource() {
262 library.MutatedProperties.VariantIsSource = true
263}
264
Liz Kammer356f7d42021-01-26 09:18:53 -0500265func (library *libraryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400266 if library.preferRlib() {
Ivan Lozanoea086132020-12-08 14:43:00 -0500267 return rlibAutoDep
268 } else if library.rlib() || library.static() {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700269 return rlibAutoDep
270 } else if library.dylib() || library.shared() {
271 return dylibAutoDep
272 } else {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200273 panic(fmt.Errorf("autoDep called on library %q that has no enabled variants.", ctx.ModuleName()))
Matthew Maurer0f003b12020-06-29 14:34:06 -0700274 }
275}
276
Ivan Lozanoea086132020-12-08 14:43:00 -0500277func (library *libraryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400278 if library.static() || library.MutatedProperties.VariantIsStaticStd || (library.rlib() && library.isFFILibrary()) {
Ivan Lozanoea086132020-12-08 14:43:00 -0500279 return RlibLinkage
280 } else if library.baseCompiler.preferRlib() {
281 return RlibLinkage
282 }
283 return DefaultLinkage
284}
285
Ivan Lozanoffee3342019-08-27 12:03:00 -0700286var _ compiler = (*libraryDecorator)(nil)
Ivan Lozano52767be2019-10-18 14:49:46 -0700287var _ libraryInterface = (*libraryDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700288var _ exportedFlagsProducer = (*libraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700289
Martin Geisler67ec0542022-11-18 12:08:55 +0100290// rust_library produces all Rust variants (rust_library_dylib and
291// rust_library_rlib).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700292func RustLibraryFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700293 module, library := NewRustLibrary(android.HostAndDeviceSupported)
294 library.BuildOnlyRust()
295 return module.Init()
296}
297
Ivan Lozano0a468a42024-05-13 21:03:34 -0400298// rust_ffi produces all FFI variants (rust_ffi_shared, rust_ffi_static, and
299// rust_ffi_rlib).
Matthew Maurer2ae05132020-06-23 14:28:53 -0700300func RustFFIFactory() android.Module {
301 module, library := NewRustLibrary(android.HostAndDeviceSupported)
302 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700303 return module.Init()
304}
305
Martin Geisler67ec0542022-11-18 12:08:55 +0100306// rust_library_dylib produces a Rust dylib (Rust crate type "dylib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700307func RustLibraryDylibFactory() android.Module {
308 module, library := NewRustLibrary(android.HostAndDeviceSupported)
309 library.BuildOnlyDylib()
310 return module.Init()
311}
312
Martin Geisler67ec0542022-11-18 12:08:55 +0100313// rust_library_rlib produces an rlib (Rust crate type "rlib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700314func RustLibraryRlibFactory() android.Module {
315 module, library := NewRustLibrary(android.HostAndDeviceSupported)
316 library.BuildOnlyRlib()
317 return module.Init()
318}
319
Martin Geisler67ec0542022-11-18 12:08:55 +0100320// rust_ffi_shared produces a shared library (Rust crate type
321// "cdylib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700322func RustFFISharedFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700323 module, library := NewRustLibrary(android.HostAndDeviceSupported)
324 library.BuildOnlyShared()
325 return module.Init()
326}
327
Martin Geisler67ec0542022-11-18 12:08:55 +0100328// rust_library_host produces all Rust variants for the host
329// (rust_library_dylib_host and rust_library_rlib_host).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700330func RustLibraryHostFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700331 module, library := NewRustLibrary(android.HostSupported)
332 library.BuildOnlyRust()
333 return module.Init()
334}
335
Martin Geisler67ec0542022-11-18 12:08:55 +0100336// rust_ffi_host produces all FFI variants for the host
Ivan Lozano0a468a42024-05-13 21:03:34 -0400337// (rust_ffi_rlib_host, rust_ffi_static_host, and rust_ffi_shared_host).
Matthew Maurer2ae05132020-06-23 14:28:53 -0700338func RustFFIHostFactory() android.Module {
339 module, library := NewRustLibrary(android.HostSupported)
340 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700341 return module.Init()
342}
343
Martin Geisler67ec0542022-11-18 12:08:55 +0100344// rust_library_dylib_host produces a dylib for the host (Rust crate
345// type "dylib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700346func RustLibraryDylibHostFactory() android.Module {
347 module, library := NewRustLibrary(android.HostSupported)
348 library.BuildOnlyDylib()
349 return module.Init()
350}
351
Martin Geisler67ec0542022-11-18 12:08:55 +0100352// rust_library_rlib_host produces an rlib for the host (Rust crate
353// type "rlib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700354func RustLibraryRlibHostFactory() android.Module {
355 module, library := NewRustLibrary(android.HostSupported)
356 library.BuildOnlyRlib()
357 return module.Init()
358}
359
Martin Geisler67ec0542022-11-18 12:08:55 +0100360// rust_ffi_shared_host produces an shared library for the host (Rust
361// crate type "cdylib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700362func RustFFISharedHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700363 module, library := NewRustLibrary(android.HostSupported)
364 library.BuildOnlyShared()
365 return module.Init()
366}
367
Ivan Lozano0a468a42024-05-13 21:03:34 -0400368// rust_ffi_rlib_host produces an rlib for the host (Rust crate
369// type "rlib").
370func RustFFIRlibHostFactory() android.Module {
371 module, library := NewRustLibrary(android.HostSupported)
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400372 library.BuildOnlyRlib()
Ivan Lozano0a468a42024-05-13 21:03:34 -0400373
374 library.isFFI = true
375 return module.Init()
376}
377
378// rust_ffi_rlib produces an rlib (Rust crate type "rlib").
379func RustFFIRlibFactory() android.Module {
380 module, library := NewRustLibrary(android.HostAndDeviceSupported)
381 library.BuildOnlyRlib()
382
383 library.isFFI = true
384 return module.Init()
385}
386
Matthew Maurer2ae05132020-06-23 14:28:53 -0700387func (library *libraryDecorator) BuildOnlyFFI() {
388 library.MutatedProperties.BuildDylib = false
Ivan Lozano0a468a42024-05-13 21:03:34 -0400389 // we build rlibs for later static ffi linkage.
390 library.MutatedProperties.BuildRlib = true
Matthew Maurer2ae05132020-06-23 14:28:53 -0700391 library.MutatedProperties.BuildShared = true
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400392 library.MutatedProperties.BuildStatic = false
Ivan Lozano0a468a42024-05-13 21:03:34 -0400393
394 library.isFFI = true
Matthew Maurer2ae05132020-06-23 14:28:53 -0700395}
396
397func (library *libraryDecorator) BuildOnlyRust() {
398 library.MutatedProperties.BuildDylib = true
399 library.MutatedProperties.BuildRlib = true
400 library.MutatedProperties.BuildShared = false
401 library.MutatedProperties.BuildStatic = false
402}
403
Ivan Lozanoffee3342019-08-27 12:03:00 -0700404func (library *libraryDecorator) BuildOnlyDylib() {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700405 library.MutatedProperties.BuildDylib = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700406 library.MutatedProperties.BuildRlib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700407 library.MutatedProperties.BuildShared = false
408 library.MutatedProperties.BuildStatic = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700409}
410
411func (library *libraryDecorator) BuildOnlyRlib() {
412 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700413 library.MutatedProperties.BuildRlib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700414 library.MutatedProperties.BuildShared = false
415 library.MutatedProperties.BuildStatic = false
416}
417
418func (library *libraryDecorator) BuildOnlyStatic() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700419 library.MutatedProperties.BuildRlib = false
420 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700421 library.MutatedProperties.BuildShared = false
422 library.MutatedProperties.BuildStatic = true
Ivan Lozano0a468a42024-05-13 21:03:34 -0400423
424 library.isFFI = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700425}
426
427func (library *libraryDecorator) BuildOnlyShared() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700428 library.MutatedProperties.BuildRlib = false
429 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700430 library.MutatedProperties.BuildStatic = false
431 library.MutatedProperties.BuildShared = true
Ivan Lozano0a468a42024-05-13 21:03:34 -0400432
433 library.isFFI = true
434}
435
436func (library *libraryDecorator) isFFILibrary() bool {
437 return library.isFFI
Ivan Lozanoffee3342019-08-27 12:03:00 -0700438}
439
440func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400441 module := newModule(hod, android.MultilibBoth)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700442
443 library := &libraryDecorator{
444 MutatedProperties: LibraryMutatedProperties{
Matthew Maurer2ae05132020-06-23 14:28:53 -0700445 BuildDylib: false,
446 BuildRlib: false,
447 BuildShared: false,
448 BuildStatic: false,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700449 },
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800450 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Matthew Maurerbb3add12020-06-25 09:34:12 -0700451 flagExporter: NewFlagExporter(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700452 }
453
454 module.compiler = library
455
456 return module, library
457}
458
459func (library *libraryDecorator) compilerProps() []interface{} {
460 return append(library.baseCompiler.compilerProps(),
461 &library.Properties,
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200462 &library.MutatedProperties,
463 &library.stripper.StripProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700464}
465
Ivan Lozanof1c84332019-09-20 11:00:37 -0700466func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
467 deps = library.baseCompiler.compilerDeps(ctx, deps)
468
Colin Crosse32f0932022-01-23 20:48:36 -0800469 if library.dylib() || library.shared() {
470 if ctx.toolchain().Bionic() {
471 deps = bionicDeps(ctx, deps, false)
472 deps.CrtBegin = []string{"crtbegin_so"}
473 deps.CrtEnd = []string{"crtend_so"}
474 } else if ctx.Os() == android.LinuxMusl {
475 deps = muslDeps(ctx, deps, false)
476 deps.CrtBegin = []string{"libc_musl_crtbegin_so"}
477 deps.CrtEnd = []string{"libc_musl_crtend_so"}
478 }
Ivan Lozanof1c84332019-09-20 11:00:37 -0700479 }
480
481 return deps
482}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400483
484func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string {
485 return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
486}
487
Ivan Lozano28ed8f42024-05-06 21:46:39 -0400488// Library cfg flags common to all variants
489func CommonLibraryCfgFlags(ctx android.ModuleContext, flags Flags) Flags {
490 return flags
491}
492
Ivan Lozano67eada32021-09-23 11:50:33 -0400493func (library *libraryDecorator) cfgFlags(ctx ModuleContext, flags Flags) Flags {
494 flags = library.baseCompiler.cfgFlags(ctx, flags)
Ivan Lozano28ed8f42024-05-06 21:46:39 -0400495 flags = CommonLibraryCfgFlags(ctx, flags)
496
Cole Faustfdec8722024-05-22 11:38:29 -0700497 cfgs := library.baseCompiler.Properties.Cfgs.GetOrDefault(ctx, nil)
498
Stephen Crane0dbfc562021-07-07 19:05:02 -0700499 if library.dylib() {
500 // We need to add a dependency on std in order to link crates as dylibs.
501 // The hack to add this dependency is guarded by the following cfg so
502 // that we don't force a dependency when it isn't needed.
Cole Faustfdec8722024-05-22 11:38:29 -0700503 cfgs = append(cfgs, "android_dylib")
Stephen Crane0dbfc562021-07-07 19:05:02 -0700504 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400505
Cole Faustfdec8722024-05-22 11:38:29 -0700506 cfgFlags := cfgsToFlags(cfgs)
507
508 flags.RustFlags = append(flags.RustFlags, cfgFlags...)
509 flags.RustdocFlags = append(flags.RustdocFlags, cfgFlags...)
Ivan Lozano28ed8f42024-05-06 21:46:39 -0400510
511 return flags
512}
513
514// Common flags applied to all libraries irrespective of properties or variant should be included here
515func CommonLibraryCompilerFlags(ctx android.ModuleContext, flags Flags) Flags {
516 flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
Ivan Lozano67eada32021-09-23 11:50:33 -0400517
518 return flags
519}
520
521func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800522 flags = library.baseCompiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400523
Ivan Lozano28ed8f42024-05-06 21:46:39 -0400524 flags = CommonLibraryCompilerFlags(ctx, flags)
525
Ivan Lozano0a468a42024-05-13 21:03:34 -0400526 if library.isFFI {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800527 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
Ivan Lozanof033ca62024-03-21 13:43:14 -0400528 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Export_include_dirs)...)
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800529 }
Ivan Lozano0a468a42024-05-13 21:03:34 -0400530
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400531 if library.shared() {
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -0700532 if ctx.Darwin() {
533 flags.LinkFlags = append(
534 flags.LinkFlags,
535 "-dynamic_lib",
536 "-install_name @rpath/"+library.sharedLibFilename(ctx),
537 )
538 } else {
539 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
540 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400541 }
542
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800543 return flags
544}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700545
Sasha Smundaka76acba2022-04-18 20:12:56 -0700546func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
547 var outputFile android.ModuleOutPath
548 var ret buildOutput
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200549 var fileName string
Matthew Maurera28404a2023-11-20 23:33:28 +0000550 crateRootPath := crateRootPath(ctx, library)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700551
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400552 if library.sourceProvider != nil {
Ivan Lozano9d74a522020-12-01 09:25:22 -0500553 deps.srcProviderFiles = append(deps.srcProviderFiles, library.sourceProvider.Srcs()...)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400554 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700555
Ivan Lozano0a468a42024-05-13 21:03:34 -0400556 // Ensure link dirs are not duplicated
557 deps.linkDirs = android.FirstUniqueStrings(deps.linkDirs)
558
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400559 // Calculate output filename
560 if library.rlib() {
561 fileName = library.getStem(ctx) + ctx.toolchain().RlibSuffix()
562 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700563 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400564 } else if library.dylib() {
565 fileName = library.getStem(ctx) + ctx.toolchain().DylibSuffix()
566 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700567 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400568 } else if library.static() {
569 fileName = library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
570 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700571 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400572 } else if library.shared() {
573 fileName = library.sharedLibFilename(ctx)
574 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700575 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400576 }
577
578 if !library.rlib() && !library.static() && library.stripper.NeedsStrip(ctx) {
579 strippedOutputFile := outputFile
580 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
581 library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
582
583 library.baseCompiler.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
584 }
585 library.baseCompiler.unstrippedOutputFile = outputFile
586
Ivan Lozanoffee3342019-08-27 12:03:00 -0700587 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500588 flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...)
Colin Cross004bd3f2023-10-02 11:39:17 -0700589 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700590
Ivan Lozanof4589012024-11-20 22:18:11 +0000591 if String(library.Properties.Version_script) != "" {
592 if String(library.Properties.Extra_exported_symbols) != "" {
593 ctx.ModuleErrorf("version_script and extra_exported_symbols cannot both be set.")
594 }
595
596 if library.shared() {
597 // "-Wl,--android-version-script" signals to the rustcLinker script
598 // that the default version script should be removed.
599 flags.LinkFlags = append(flags.LinkFlags, "-Wl,--android-version-script="+android.PathForModuleSrc(ctx, String(library.Properties.Version_script)).String())
600 deps.LinkerDeps = append(deps.LinkerDeps, android.PathForModuleSrc(ctx, String(library.Properties.Version_script)))
601 } else if !library.static() && !library.rlib() {
602 // We include rlibs here because rust_ffi produces rlib variants
603 ctx.PropertyErrorf("version_script", "can only be set for rust_ffi modules")
604 }
605 }
606
607 if String(library.Properties.Extra_exported_symbols) != "" {
608 // Passing a second version script (rustc calculates and emits a
609 // default version script) will concatenate the first version script.
610 flags.LinkFlags = append(flags.LinkFlags, "-Wl,--version-script="+android.PathForModuleSrc(ctx, String(library.Properties.Extra_exported_symbols)).String())
611 deps.LinkerDeps = append(deps.LinkerDeps, android.PathForModuleSrc(ctx, String(library.Properties.Extra_exported_symbols)))
612 }
613
Matthew Maurer46c46cc2020-01-13 16:34:34 -0800614 if library.dylib() {
Ivan Lozanof4589012024-11-20 22:18:11 +0000615
Ivan Lozano52767be2019-10-18 14:49:46 -0700616 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
617 // https://github.com/rust-lang/rust/issues/19680
618 // https://github.com/rust-lang/rust/issues/34909
619 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
620 }
621
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400622 // Call the appropriate builder for this library type
Ivan Lozanoffee3342019-08-27 12:03:00 -0700623 if library.rlib() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000624 ret.kytheFile = TransformSrctoRlib(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700625 } else if library.dylib() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000626 ret.kytheFile = TransformSrctoDylib(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700627 } else if library.static() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000628 ret.kytheFile = TransformSrctoStatic(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700629 } else if library.shared() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000630 ret.kytheFile = TransformSrctoShared(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700631 }
632
Ivan Lozano52767be2019-10-18 14:49:46 -0700633 if library.rlib() || library.dylib() {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700634 library.flagExporter.exportLinkDirs(deps.linkDirs...)
Colin Cross0de8a1e2020-09-18 14:15:30 -0700635 library.flagExporter.exportLinkObjects(deps.linkObjects...)
Ivan Lozano52767be2019-10-18 14:49:46 -0700636 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700637
Ivan Lozano0a468a42024-05-13 21:03:34 -0400638 // Since we have FFI rlibs, we need to collect their includes as well
639 if library.static() || library.shared() || library.rlib() {
Colin Cross40213022023-12-13 15:19:49 -0800640 android.SetProvider(ctx, cc.FlagExporterInfoProvider, cc.FlagExporterInfo{
Ivan Lozano0a468a42024-05-13 21:03:34 -0400641 IncludeDirs: android.FirstUniquePaths(library.includeDirs),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700642 })
643 }
644
645 if library.shared() {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400646 // Optimize out relinking against shared libraries whose interface hasn't changed by
647 // depending on a table of contents file instead of the library itself.
648 tocFile := outputFile.ReplaceExtension(ctx, flags.Toolchain.SharedLibSuffix()[1:]+".toc")
649 library.tocFile = android.OptionalPathForPath(tocFile)
650 cc.TransformSharedObjectToToc(ctx, outputFile, tocFile)
651
Colin Cross40213022023-12-13 15:19:49 -0800652 android.SetProvider(ctx, cc.SharedLibraryInfoProvider, cc.SharedLibraryInfo{
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400653 TableOfContents: android.OptionalPathForPath(tocFile),
654 SharedLibrary: outputFile,
655 Target: ctx.Target(),
Colin Crossb614cd42024-10-11 12:52:21 -0700656 // TODO: when rust supports stubs uses the stubs state rather than inferring it from
657 // apex_exclude.
658 IsStubs: Bool(library.Properties.Apex_exclude),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700659 })
660 }
661
662 if library.static() {
Colin Crossa14fb6a2024-10-23 16:57:06 -0700663 depSet := depset.NewBuilder[android.Path](depset.TOPOLOGICAL).Direct(outputFile).Build()
Colin Cross40213022023-12-13 15:19:49 -0800664 android.SetProvider(ctx, cc.StaticLibraryInfoProvider, cc.StaticLibraryInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700665 StaticLibrary: outputFile,
666
667 TransitiveStaticLibrariesForOrdering: depSet,
668 })
669 }
670
671 library.flagExporter.setProvider(ctx)
672
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400673 return ret
Ivan Lozanoffee3342019-08-27 12:03:00 -0700674}
675
Matthew Maurera28404a2023-11-20 23:33:28 +0000676func (library *libraryDecorator) checkedCrateRootPath() (android.Path, error) {
Dan Albert06feee92021-03-19 15:06:02 -0700677 if library.sourceProvider != nil {
Matthew Maurera28404a2023-11-20 23:33:28 +0000678 srcs := library.sourceProvider.Srcs()
679 if len(srcs) == 0 {
680 return nil, errors.New("Source provider generated 0 sources")
681 }
Dan Albert06feee92021-03-19 15:06:02 -0700682 // Assume the first source from the source provider is the library entry point.
Matthew Maurera28404a2023-11-20 23:33:28 +0000683 return srcs[0], nil
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000684 } else {
Matthew Maurera28404a2023-11-20 23:33:28 +0000685 return library.baseCompiler.checkedCrateRootPath()
Dan Albert06feee92021-03-19 15:06:02 -0700686 }
687}
688
689func (library *libraryDecorator) rustdoc(ctx ModuleContext, flags Flags,
690 deps PathDeps) android.OptionalPath {
691 // rustdoc has builtin support for documenting config specific information
692 // regardless of the actual config it was given
693 // (https://doc.rust-lang.org/rustdoc/advanced-features.html#cfgdoc-documenting-platform-specific-or-feature-specific-information),
694 // so we generate the rustdoc for only the primary module so that we have a
695 // single set of docs to refer to.
696 if ctx.Module() != ctx.PrimaryModule() {
697 return android.OptionalPath{}
698 }
699
Matthew Maurera28404a2023-11-20 23:33:28 +0000700 return android.OptionalPathForPath(Rustdoc(ctx, crateRootPath(ctx, library),
Dan Albert06feee92021-03-19 15:06:02 -0700701 deps, flags))
702}
703
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700704func (library *libraryDecorator) getStem(ctx ModuleContext) string {
705 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
706 validateLibraryStem(ctx, stem, library.crateName())
707
708 return stem + String(library.baseCompiler.Properties.Suffix)
709}
710
Ivan Lozano2b081132020-09-08 12:46:52 -0400711func (library *libraryDecorator) install(ctx ModuleContext) {
712 // Only shared and dylib variants make sense to install.
713 if library.shared() || library.dylib() {
714 library.baseCompiler.install(ctx)
715 }
716}
717
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400718func (library *libraryDecorator) Disabled() bool {
719 return library.MutatedProperties.VariantIsDisabled
720}
721
722func (library *libraryDecorator) SetDisabled() {
723 library.MutatedProperties.VariantIsDisabled = true
724}
725
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700726var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
727
728func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
729 if crate_name == "" {
730 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
731 }
732
733 // crate_names are used for the library output file, and rustc expects these
734 // to be alphanumeric with underscores allowed.
735 if validCrateName.MatchString(crate_name) {
736 ctx.PropertyErrorf("crate_name",
737 "library crate_names must be alphanumeric with underscores allowed")
738 }
739
740 // Libraries are expected to begin with "lib" followed by the crate_name
741 if !strings.HasPrefix(filename, "lib"+crate_name) {
742 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
743 }
744}
745
Colin Cross8a49a3d2024-05-20 12:22:27 -0700746type libraryTransitionMutator struct{}
747
748func (libraryTransitionMutator) Split(ctx android.BaseModuleContext) []string {
749 m, ok := ctx.Module().(*Module)
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200750 if !ok || m.compiler == nil {
Colin Cross8a49a3d2024-05-20 12:22:27 -0700751 return []string{""}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200752 }
753 library, ok := m.compiler.(libraryInterface)
754 if !ok {
Colin Cross8a49a3d2024-05-20 12:22:27 -0700755 return []string{""}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200756 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400757
Ivan Lozano0a468a42024-05-13 21:03:34 -0400758 // Don't produce rlib/dylib/source variants for shared or static variants
759 if library.shared() || library.static() {
Colin Cross8a49a3d2024-05-20 12:22:27 -0700760 return []string{""}
Ivan Lozano0a468a42024-05-13 21:03:34 -0400761 }
762
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200763 var variants []string
764 // The source variant is used for SourceProvider modules. The other variants (i.e. rlib and dylib)
765 // depend on this variant. It must be the first variant to be declared.
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200766 if m.sourceProvider != nil {
Colin Cross8a49a3d2024-05-20 12:22:27 -0700767 variants = append(variants, sourceVariation)
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200768 }
769 if library.buildRlib() {
770 variants = append(variants, rlibVariation)
771 }
772 if library.buildDylib() {
773 variants = append(variants, dylibVariation)
774 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400775
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200776 if len(variants) == 0 {
Colin Cross8a49a3d2024-05-20 12:22:27 -0700777 return []string{""}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200778 }
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200779
Colin Cross8a49a3d2024-05-20 12:22:27 -0700780 return variants
781}
Ivan Lozano1921e802021-05-20 13:39:16 -0400782
Colin Cross8a49a3d2024-05-20 12:22:27 -0700783func (libraryTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
784 return ""
785}
786
787func (libraryTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
788 m, ok := ctx.Module().(*Module)
789 if !ok || m.compiler == nil {
790 return ""
791 }
792 library, ok := m.compiler.(libraryInterface)
793 if !ok {
794 return ""
795 }
796
797 if incomingVariation == "" {
798 if m.sourceProvider != nil {
799 return sourceVariation
800 }
801 if library.shared() {
802 return ""
803 }
804 if library.buildRlib() {
805 return rlibVariation
806 }
807 if library.buildDylib() {
808 return dylibVariation
Ivan Lozanoffee3342019-08-27 12:03:00 -0700809 }
810 }
Colin Cross8a49a3d2024-05-20 12:22:27 -0700811 return incomingVariation
812}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200813
Colin Cross8a49a3d2024-05-20 12:22:27 -0700814func (libraryTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) {
815 m, ok := ctx.Module().(*Module)
816 if !ok || m.compiler == nil {
817 return
818 }
819 library, ok := m.compiler.(libraryInterface)
820 if !ok {
821 return
822 }
823
824 switch variation {
825 case rlibVariation:
826 library.setRlib()
827 case dylibVariation:
828 library.setDylib()
829 if m.ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
830 // TODO(b/165791368)
831 // Disable dylib Vendor Ramdisk variations until we support these.
832 m.Disable()
833 }
834
835 case sourceVariation:
836 library.setSource()
837 // The source variant does not produce any library.
838 // Disable the compilation steps.
839 m.compiler.SetDisabled()
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400840 }
841
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200842 // If a source variant is created, add an inter-variant dependency
843 // between the other variants and the source variant.
Colin Cross8a49a3d2024-05-20 12:22:27 -0700844 if m.sourceProvider != nil && variation != sourceVariation {
845 ctx.AddVariationDependencies(
846 []blueprint.Variation{
847 {"rust_libraries", sourceVariation},
848 },
849 sourceDepTag, ctx.ModuleName())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200850 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700851}
Ivan Lozano2b081132020-09-08 12:46:52 -0400852
Colin Cross8a49a3d2024-05-20 12:22:27 -0700853type libstdTransitionMutator struct{}
Ivan Lozano2b081132020-09-08 12:46:52 -0400854
Colin Cross8a49a3d2024-05-20 12:22:27 -0700855func (libstdTransitionMutator) Split(ctx android.BaseModuleContext) []string {
856 if m, ok := ctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() {
857 // Only create a variant if a library is actually being built.
858 if library, ok := m.compiler.(libraryInterface); ok {
859 if library.rlib() && !library.sysroot() {
860 if library.isFFILibrary() {
861 return []string{"rlib-std"}
862 } else {
863 return []string{"rlib-std", "dylib-std"}
Ivan Lozano6a884432020-12-02 09:15:16 -0500864 }
Ivan Lozano2b081132020-09-08 12:46:52 -0400865 }
866 }
867 }
Colin Cross8a49a3d2024-05-20 12:22:27 -0700868 return []string{""}
869}
870
871func (libstdTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
872 return ""
873}
874
875func (libstdTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
876 if m, ok := ctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() {
877 if library, ok := m.compiler.(libraryInterface); ok {
878 if library.shared() {
879 return ""
880 }
881 if library.rlib() && !library.sysroot() {
882 if incomingVariation != "" {
883 return incomingVariation
884 }
885 return "rlib-std"
886 }
887 }
888 }
889 return ""
890}
891
892func (libstdTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) {
893 if variation == "rlib-std" {
894 rlib := ctx.Module().(*Module)
895 rlib.compiler.(libraryInterface).setRlibStd()
896 rlib.Properties.RustSubName += RlibStdlibSuffix
897 } else if variation == "dylib-std" {
898 dylib := ctx.Module().(*Module)
899 dylib.compiler.(libraryInterface).setDylibStd()
900 if dylib.ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
901 // TODO(b/165791368)
902 // Disable rlibs that link against dylib-std on vendor ramdisk variations until those dylib
903 // variants are properly supported.
904 dylib.Disable()
905 }
906 }
Ivan Lozano2b081132020-09-08 12:46:52 -0400907}