blob: 93a9a3dab23d3b00d1d011d0e7403ca0b1eabe2d [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 Lozano806efd32024-12-11 21:38:53 +000043 android.RegisterModuleType("rust_ffi_rlib", RustLibraryRlibFactory)
Matthew Maurer2ae05132020-06-23 14:28:53 -070044 android.RegisterModuleType("rust_ffi_host", RustFFIHostFactory)
45 android.RegisterModuleType("rust_ffi_host_shared", RustFFISharedHostFactory)
Ivan Lozano806efd32024-12-11 21:38:53 +000046 android.RegisterModuleType("rust_ffi_host_rlib", RustLibraryRlibHostFactory)
Ivan Lozano0a468a42024-05-13 21:03:34 -040047
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
Ivan Lozano806efd32024-12-11 21:38:53 +000050 android.RegisterModuleType("rust_ffi_static", RustLibraryRlibFactory)
51 android.RegisterModuleType("rust_ffi_host_static", RustLibraryRlibHostFactory)
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 Lozano2b081132020-09-08 12:46:52 -040072 // Whether this library is part of the Rust toolchain sysroot.
73 Sysroot *bool
Ashutosh Agarwal46e4fad2024-08-27 17:13:12 +000074
75 // Exclude this rust_ffi target from being included in APEXes.
76 // TODO(b/362509506): remove this once stubs are properly supported by rust_ffi targets.
77 Apex_exclude *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070078}
79
80type LibraryMutatedProperties struct {
Ivan Lozanoffee3342019-08-27 12:03:00 -070081 // Build a dylib variant
82 BuildDylib bool `blueprint:"mutated"`
83 // Build an rlib variant
84 BuildRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070085 // Build a shared library variant
86 BuildShared bool `blueprint:"mutated"`
87 // Build a static library variant
88 BuildStatic bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070089
90 // This variant is a dylib
91 VariantIsDylib bool `blueprint:"mutated"`
92 // This variant is an rlib
93 VariantIsRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070094 // This variant is a shared library
95 VariantIsShared bool `blueprint:"mutated"`
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +020096 // This variant is a source provider
97 VariantIsSource bool `blueprint:"mutated"`
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040098
99 // This variant is disabled and should not be compiled
100 // (used for SourceProvider variants that produce only source)
101 VariantIsDisabled bool `blueprint:"mutated"`
Ivan Lozano2b081132020-09-08 12:46:52 -0400102
103 // Whether this library variant should be link libstd via rlibs
104 VariantIsStaticStd bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -0700105}
106
107type libraryDecorator struct {
108 *baseCompiler
Matthew Maurerbb3add12020-06-25 09:34:12 -0700109 *flagExporter
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200110 stripper Stripper
Ivan Lozanoffee3342019-08-27 12:03:00 -0700111
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400112 Properties LibraryCompilerProperties
113 MutatedProperties LibraryMutatedProperties
114 includeDirs android.Paths
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400115 sourceProvider SourceProvider
Ivan Lozano1921e802021-05-20 13:39:16 -0400116
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400117 // table-of-contents file for cdylib crates to optimize out relinking when possible
118 tocFile android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700119}
120
121type libraryInterface interface {
122 rlib() bool
123 dylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700124 static() bool
125 shared() bool
Ivan Lozano2b081132020-09-08 12:46:52 -0400126 sysroot() bool
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200127 source() bool
Ashutosh Agarwal46e4fad2024-08-27 17:13:12 +0000128 apexExclude() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700129
130 // Returns true if the build options for the module have selected a particular build type
131 buildRlib() bool
132 buildDylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700133 buildShared() bool
134 buildStatic() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700135
136 // Sets a particular variant type
137 setRlib()
138 setDylib()
Ivan Lozano52767be2019-10-18 14:49:46 -0700139 setShared()
140 setStatic()
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200141 setSource()
Ivan Lozano52767be2019-10-18 14:49:46 -0700142
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400143 // libstd linkage functions
144 rlibStd() bool
Ivan Lozano2b081132020-09-08 12:46:52 -0400145 setRlibStd()
146 setDylibStd()
147
Ivan Lozano52767be2019-10-18 14:49:46 -0700148 // Build a specific library variant
Matthew Maurer2ae05132020-06-23 14:28:53 -0700149 BuildOnlyFFI()
150 BuildOnlyRust()
Ivan Lozano52767be2019-10-18 14:49:46 -0700151 BuildOnlyRlib()
152 BuildOnlyDylib()
153 BuildOnlyStatic()
154 BuildOnlyShared()
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400155
156 toc() android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700157}
158
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400159func (library *libraryDecorator) nativeCoverage() bool {
160 return true
161}
162
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400163func (library *libraryDecorator) toc() android.OptionalPath {
164 return library.tocFile
165}
166
Ivan Lozanoffee3342019-08-27 12:03:00 -0700167func (library *libraryDecorator) rlib() bool {
168 return library.MutatedProperties.VariantIsRlib
169}
170
Ivan Lozano2b081132020-09-08 12:46:52 -0400171func (library *libraryDecorator) sysroot() bool {
172 return Bool(library.Properties.Sysroot)
173}
174
Ivan Lozanoffee3342019-08-27 12:03:00 -0700175func (library *libraryDecorator) dylib() bool {
176 return library.MutatedProperties.VariantIsDylib
177}
178
Ivan Lozano52767be2019-10-18 14:49:46 -0700179func (library *libraryDecorator) shared() bool {
180 return library.MutatedProperties.VariantIsShared
181}
182
183func (library *libraryDecorator) static() bool {
Colin Cross17f9dc52024-07-01 20:05:54 -0700184 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700185}
186
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200187func (library *libraryDecorator) source() bool {
188 return library.MutatedProperties.VariantIsSource
189}
190
Ashutosh Agarwal46e4fad2024-08-27 17:13:12 +0000191func (library *libraryDecorator) apexExclude() bool {
192 return Bool(library.Properties.Apex_exclude)
193}
194
Ivan Lozanoffee3342019-08-27 12:03:00 -0700195func (library *libraryDecorator) buildRlib() bool {
196 return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true)
197}
198
199func (library *libraryDecorator) buildDylib() bool {
200 return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true)
201}
202
Ivan Lozano52767be2019-10-18 14:49:46 -0700203func (library *libraryDecorator) buildShared() bool {
204 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
205}
206
207func (library *libraryDecorator) buildStatic() bool {
208 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
209}
210
Ivan Lozanoffee3342019-08-27 12:03:00 -0700211func (library *libraryDecorator) setRlib() {
212 library.MutatedProperties.VariantIsRlib = true
213 library.MutatedProperties.VariantIsDylib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700214 library.MutatedProperties.VariantIsShared = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700215}
216
217func (library *libraryDecorator) setDylib() {
218 library.MutatedProperties.VariantIsRlib = false
219 library.MutatedProperties.VariantIsDylib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700220 library.MutatedProperties.VariantIsShared = false
221}
222
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400223func (library *libraryDecorator) rlibStd() bool {
224 return library.MutatedProperties.VariantIsStaticStd
225}
226
Ivan Lozano2b081132020-09-08 12:46:52 -0400227func (library *libraryDecorator) setRlibStd() {
228 library.MutatedProperties.VariantIsStaticStd = true
229}
230
231func (library *libraryDecorator) setDylibStd() {
232 library.MutatedProperties.VariantIsStaticStd = false
233}
234
Ivan Lozano52767be2019-10-18 14:49:46 -0700235func (library *libraryDecorator) setShared() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700236 library.MutatedProperties.VariantIsShared = true
237 library.MutatedProperties.VariantIsRlib = false
238 library.MutatedProperties.VariantIsDylib = false
239}
240
241func (library *libraryDecorator) setStatic() {
Colin Cross17f9dc52024-07-01 20:05:54 -0700242 panic(fmt.Errorf("static variant is not supported for rust modules, use the rlib variant instead"))
Ivan Lozanoffee3342019-08-27 12:03:00 -0700243}
244
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200245func (library *libraryDecorator) setSource() {
246 library.MutatedProperties.VariantIsSource = true
247}
248
Liz Kammer356f7d42021-01-26 09:18:53 -0500249func (library *libraryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400250 if library.preferRlib() {
Ivan Lozanoea086132020-12-08 14:43:00 -0500251 return rlibAutoDep
252 } else if library.rlib() || library.static() {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700253 return rlibAutoDep
254 } else if library.dylib() || library.shared() {
255 return dylibAutoDep
256 } else {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200257 panic(fmt.Errorf("autoDep called on library %q that has no enabled variants.", ctx.ModuleName()))
Matthew Maurer0f003b12020-06-29 14:34:06 -0700258 }
259}
260
Ivan Lozano806efd32024-12-11 21:38:53 +0000261func (library *libraryDecorator) stdLinkage(device bool) RustLinkage {
262 if library.static() || library.MutatedProperties.VariantIsStaticStd {
Ivan Lozanoea086132020-12-08 14:43:00 -0500263 return RlibLinkage
264 } else if library.baseCompiler.preferRlib() {
265 return RlibLinkage
266 }
Ivan Lozano806efd32024-12-11 21:38:53 +0000267 return DylibLinkage
Ivan Lozanoea086132020-12-08 14:43:00 -0500268}
269
Ivan Lozanoffee3342019-08-27 12:03:00 -0700270var _ compiler = (*libraryDecorator)(nil)
Ivan Lozano52767be2019-10-18 14:49:46 -0700271var _ libraryInterface = (*libraryDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700272var _ exportedFlagsProducer = (*libraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700273
Martin Geisler67ec0542022-11-18 12:08:55 +0100274// rust_library produces all Rust variants (rust_library_dylib and
275// rust_library_rlib).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700276func RustLibraryFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700277 module, library := NewRustLibrary(android.HostAndDeviceSupported)
278 library.BuildOnlyRust()
279 return module.Init()
280}
281
Ivan Lozano0a468a42024-05-13 21:03:34 -0400282// rust_ffi produces all FFI variants (rust_ffi_shared, rust_ffi_static, and
283// rust_ffi_rlib).
Matthew Maurer2ae05132020-06-23 14:28:53 -0700284func RustFFIFactory() android.Module {
285 module, library := NewRustLibrary(android.HostAndDeviceSupported)
286 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700287 return module.Init()
288}
289
Martin Geisler67ec0542022-11-18 12:08:55 +0100290// rust_library_dylib produces a Rust dylib (Rust crate type "dylib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700291func RustLibraryDylibFactory() android.Module {
292 module, library := NewRustLibrary(android.HostAndDeviceSupported)
293 library.BuildOnlyDylib()
294 return module.Init()
295}
296
Ivan Lozano806efd32024-12-11 21:38:53 +0000297// rust_library_rlib and rust_ffi_static produces an rlib (Rust crate type "rlib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700298func RustLibraryRlibFactory() android.Module {
299 module, library := NewRustLibrary(android.HostAndDeviceSupported)
300 library.BuildOnlyRlib()
301 return module.Init()
302}
303
Martin Geisler67ec0542022-11-18 12:08:55 +0100304// rust_ffi_shared produces a shared library (Rust crate type
305// "cdylib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700306func RustFFISharedFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700307 module, library := NewRustLibrary(android.HostAndDeviceSupported)
308 library.BuildOnlyShared()
309 return module.Init()
310}
311
Martin Geisler67ec0542022-11-18 12:08:55 +0100312// rust_library_host produces all Rust variants for the host
313// (rust_library_dylib_host and rust_library_rlib_host).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700314func RustLibraryHostFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700315 module, library := NewRustLibrary(android.HostSupported)
316 library.BuildOnlyRust()
317 return module.Init()
318}
319
Martin Geisler67ec0542022-11-18 12:08:55 +0100320// rust_ffi_host produces all FFI variants for the host
Ivan Lozano0a468a42024-05-13 21:03:34 -0400321// (rust_ffi_rlib_host, rust_ffi_static_host, and rust_ffi_shared_host).
Matthew Maurer2ae05132020-06-23 14:28:53 -0700322func RustFFIHostFactory() android.Module {
323 module, library := NewRustLibrary(android.HostSupported)
324 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700325 return module.Init()
326}
327
Martin Geisler67ec0542022-11-18 12:08:55 +0100328// rust_library_dylib_host produces a dylib for the host (Rust crate
329// type "dylib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700330func RustLibraryDylibHostFactory() android.Module {
331 module, library := NewRustLibrary(android.HostSupported)
332 library.BuildOnlyDylib()
333 return module.Init()
334}
335
Ivan Lozano806efd32024-12-11 21:38:53 +0000336// rust_library_rlib_host and rust_ffi_static_host produces an rlib for the host
337// (Rust crate type "rlib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700338func RustLibraryRlibHostFactory() android.Module {
339 module, library := NewRustLibrary(android.HostSupported)
340 library.BuildOnlyRlib()
341 return module.Init()
342}
343
Martin Geisler67ec0542022-11-18 12:08:55 +0100344// rust_ffi_shared_host produces an shared library for the host (Rust
345// crate type "cdylib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700346func RustFFISharedHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700347 module, library := NewRustLibrary(android.HostSupported)
348 library.BuildOnlyShared()
349 return module.Init()
350}
351
Matthew Maurer2ae05132020-06-23 14:28:53 -0700352func (library *libraryDecorator) BuildOnlyFFI() {
353 library.MutatedProperties.BuildDylib = false
Ivan Lozano0a468a42024-05-13 21:03:34 -0400354 // we build rlibs for later static ffi linkage.
355 library.MutatedProperties.BuildRlib = true
Matthew Maurer2ae05132020-06-23 14:28:53 -0700356 library.MutatedProperties.BuildShared = true
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400357 library.MutatedProperties.BuildStatic = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700358}
359
360func (library *libraryDecorator) BuildOnlyRust() {
361 library.MutatedProperties.BuildDylib = true
362 library.MutatedProperties.BuildRlib = true
363 library.MutatedProperties.BuildShared = false
364 library.MutatedProperties.BuildStatic = false
365}
366
Ivan Lozanoffee3342019-08-27 12:03:00 -0700367func (library *libraryDecorator) BuildOnlyDylib() {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700368 library.MutatedProperties.BuildDylib = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700369 library.MutatedProperties.BuildRlib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700370 library.MutatedProperties.BuildShared = false
371 library.MutatedProperties.BuildStatic = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700372}
373
374func (library *libraryDecorator) BuildOnlyRlib() {
375 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700376 library.MutatedProperties.BuildRlib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700377 library.MutatedProperties.BuildShared = false
378 library.MutatedProperties.BuildStatic = false
379}
380
381func (library *libraryDecorator) BuildOnlyStatic() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700382 library.MutatedProperties.BuildRlib = false
383 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700384 library.MutatedProperties.BuildShared = false
385 library.MutatedProperties.BuildStatic = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700386}
387
388func (library *libraryDecorator) BuildOnlyShared() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700389 library.MutatedProperties.BuildRlib = false
390 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700391 library.MutatedProperties.BuildStatic = false
392 library.MutatedProperties.BuildShared = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700393}
394
395func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400396 module := newModule(hod, android.MultilibBoth)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700397
398 library := &libraryDecorator{
399 MutatedProperties: LibraryMutatedProperties{
Matthew Maurer2ae05132020-06-23 14:28:53 -0700400 BuildDylib: false,
401 BuildRlib: false,
402 BuildShared: false,
403 BuildStatic: false,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700404 },
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800405 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Matthew Maurerbb3add12020-06-25 09:34:12 -0700406 flagExporter: NewFlagExporter(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700407 }
408
409 module.compiler = library
410
411 return module, library
412}
413
414func (library *libraryDecorator) compilerProps() []interface{} {
415 return append(library.baseCompiler.compilerProps(),
416 &library.Properties,
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200417 &library.MutatedProperties,
418 &library.stripper.StripProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700419}
420
Ivan Lozanof1c84332019-09-20 11:00:37 -0700421func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
422 deps = library.baseCompiler.compilerDeps(ctx, deps)
423
Colin Crosse32f0932022-01-23 20:48:36 -0800424 if library.dylib() || library.shared() {
425 if ctx.toolchain().Bionic() {
426 deps = bionicDeps(ctx, deps, false)
427 deps.CrtBegin = []string{"crtbegin_so"}
428 deps.CrtEnd = []string{"crtend_so"}
429 } else if ctx.Os() == android.LinuxMusl {
430 deps = muslDeps(ctx, deps, false)
431 deps.CrtBegin = []string{"libc_musl_crtbegin_so"}
432 deps.CrtEnd = []string{"libc_musl_crtend_so"}
433 }
Ivan Lozanof1c84332019-09-20 11:00:37 -0700434 }
435
436 return deps
437}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400438
439func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string {
440 return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
441}
442
Ivan Lozano28ed8f42024-05-06 21:46:39 -0400443// Library cfg flags common to all variants
444func CommonLibraryCfgFlags(ctx android.ModuleContext, flags Flags) Flags {
445 return flags
446}
447
Ivan Lozano67eada32021-09-23 11:50:33 -0400448func (library *libraryDecorator) cfgFlags(ctx ModuleContext, flags Flags) Flags {
449 flags = library.baseCompiler.cfgFlags(ctx, flags)
Ivan Lozano28ed8f42024-05-06 21:46:39 -0400450 flags = CommonLibraryCfgFlags(ctx, flags)
451
Cole Faustfdec8722024-05-22 11:38:29 -0700452 cfgs := library.baseCompiler.Properties.Cfgs.GetOrDefault(ctx, nil)
453
Stephen Crane0dbfc562021-07-07 19:05:02 -0700454 if library.dylib() {
455 // We need to add a dependency on std in order to link crates as dylibs.
456 // The hack to add this dependency is guarded by the following cfg so
457 // that we don't force a dependency when it isn't needed.
Cole Faustfdec8722024-05-22 11:38:29 -0700458 cfgs = append(cfgs, "android_dylib")
Stephen Crane0dbfc562021-07-07 19:05:02 -0700459 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400460
Cole Faustfdec8722024-05-22 11:38:29 -0700461 cfgFlags := cfgsToFlags(cfgs)
462
463 flags.RustFlags = append(flags.RustFlags, cfgFlags...)
464 flags.RustdocFlags = append(flags.RustdocFlags, cfgFlags...)
Ivan Lozano28ed8f42024-05-06 21:46:39 -0400465
466 return flags
467}
468
469// Common flags applied to all libraries irrespective of properties or variant should be included here
470func CommonLibraryCompilerFlags(ctx android.ModuleContext, flags Flags) Flags {
471 flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
Ivan Lozano67eada32021-09-23 11:50:33 -0400472
473 return flags
474}
475
476func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800477 flags = library.baseCompiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400478
Ivan Lozano28ed8f42024-05-06 21:46:39 -0400479 flags = CommonLibraryCompilerFlags(ctx, flags)
480
Ivan Lozano806efd32024-12-11 21:38:53 +0000481 if library.rlib() || library.shared() {
482 // rlibs collect include dirs as well since they are used to
483 // produce staticlibs in the final C linkages
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800484 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
Ivan Lozanof033ca62024-03-21 13:43:14 -0400485 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Export_include_dirs)...)
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800486 }
Ivan Lozano0a468a42024-05-13 21:03:34 -0400487
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400488 if library.shared() {
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -0700489 if ctx.Darwin() {
490 flags.LinkFlags = append(
491 flags.LinkFlags,
492 "-dynamic_lib",
493 "-install_name @rpath/"+library.sharedLibFilename(ctx),
494 )
495 } else {
496 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
497 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400498 }
499
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800500 return flags
501}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700502
Sasha Smundaka76acba2022-04-18 20:12:56 -0700503func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
504 var outputFile android.ModuleOutPath
505 var ret buildOutput
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200506 var fileName string
Matthew Maurera28404a2023-11-20 23:33:28 +0000507 crateRootPath := crateRootPath(ctx, library)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700508
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400509 if library.sourceProvider != nil {
Ivan Lozano9d74a522020-12-01 09:25:22 -0500510 deps.srcProviderFiles = append(deps.srcProviderFiles, library.sourceProvider.Srcs()...)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400511 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700512
Ivan Lozano0a468a42024-05-13 21:03:34 -0400513 // Ensure link dirs are not duplicated
514 deps.linkDirs = android.FirstUniqueStrings(deps.linkDirs)
515
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400516 // Calculate output filename
517 if library.rlib() {
518 fileName = library.getStem(ctx) + ctx.toolchain().RlibSuffix()
519 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700520 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400521 } else if library.dylib() {
522 fileName = library.getStem(ctx) + ctx.toolchain().DylibSuffix()
523 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700524 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400525 } else if library.static() {
526 fileName = library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
527 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700528 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400529 } else if library.shared() {
530 fileName = library.sharedLibFilename(ctx)
531 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700532 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400533 }
534
535 if !library.rlib() && !library.static() && library.stripper.NeedsStrip(ctx) {
536 strippedOutputFile := outputFile
537 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
538 library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
539
540 library.baseCompiler.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
541 }
542 library.baseCompiler.unstrippedOutputFile = outputFile
543
Ivan Lozanoffee3342019-08-27 12:03:00 -0700544 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500545 flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...)
Colin Cross004bd3f2023-10-02 11:39:17 -0700546 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700547
Matthew Maurer46c46cc2020-01-13 16:34:34 -0800548 if library.dylib() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700549 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
550 // https://github.com/rust-lang/rust/issues/19680
551 // https://github.com/rust-lang/rust/issues/34909
552 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
553 }
554
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400555 // Call the appropriate builder for this library type
Ivan Lozanoffee3342019-08-27 12:03:00 -0700556 if library.rlib() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000557 ret.kytheFile = TransformSrctoRlib(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700558 } else if library.dylib() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000559 ret.kytheFile = TransformSrctoDylib(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700560 } else if library.static() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000561 ret.kytheFile = TransformSrctoStatic(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700562 } else if library.shared() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000563 ret.kytheFile = TransformSrctoShared(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700564 }
565
Ivan Lozano52767be2019-10-18 14:49:46 -0700566 if library.rlib() || library.dylib() {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700567 library.flagExporter.exportLinkDirs(deps.linkDirs...)
Colin Cross0de8a1e2020-09-18 14:15:30 -0700568 library.flagExporter.exportLinkObjects(deps.linkObjects...)
Ivan Lozano52767be2019-10-18 14:49:46 -0700569 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700570
Ivan Lozano0a468a42024-05-13 21:03:34 -0400571 // Since we have FFI rlibs, we need to collect their includes as well
572 if library.static() || library.shared() || library.rlib() {
Colin Cross40213022023-12-13 15:19:49 -0800573 android.SetProvider(ctx, cc.FlagExporterInfoProvider, cc.FlagExporterInfo{
Ivan Lozano0a468a42024-05-13 21:03:34 -0400574 IncludeDirs: android.FirstUniquePaths(library.includeDirs),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700575 })
576 }
577
578 if library.shared() {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400579 // Optimize out relinking against shared libraries whose interface hasn't changed by
580 // depending on a table of contents file instead of the library itself.
581 tocFile := outputFile.ReplaceExtension(ctx, flags.Toolchain.SharedLibSuffix()[1:]+".toc")
582 library.tocFile = android.OptionalPathForPath(tocFile)
583 cc.TransformSharedObjectToToc(ctx, outputFile, tocFile)
584
Colin Cross40213022023-12-13 15:19:49 -0800585 android.SetProvider(ctx, cc.SharedLibraryInfoProvider, cc.SharedLibraryInfo{
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400586 TableOfContents: android.OptionalPathForPath(tocFile),
587 SharedLibrary: outputFile,
588 Target: ctx.Target(),
Colin Crossb614cd42024-10-11 12:52:21 -0700589 // TODO: when rust supports stubs uses the stubs state rather than inferring it from
590 // apex_exclude.
591 IsStubs: Bool(library.Properties.Apex_exclude),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700592 })
593 }
594
595 if library.static() {
Colin Crossa14fb6a2024-10-23 16:57:06 -0700596 depSet := depset.NewBuilder[android.Path](depset.TOPOLOGICAL).Direct(outputFile).Build()
Colin Cross40213022023-12-13 15:19:49 -0800597 android.SetProvider(ctx, cc.StaticLibraryInfoProvider, cc.StaticLibraryInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700598 StaticLibrary: outputFile,
599
600 TransitiveStaticLibrariesForOrdering: depSet,
601 })
602 }
603
604 library.flagExporter.setProvider(ctx)
605
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400606 return ret
Ivan Lozanoffee3342019-08-27 12:03:00 -0700607}
608
Matthew Maurera28404a2023-11-20 23:33:28 +0000609func (library *libraryDecorator) checkedCrateRootPath() (android.Path, error) {
Dan Albert06feee92021-03-19 15:06:02 -0700610 if library.sourceProvider != nil {
Matthew Maurera28404a2023-11-20 23:33:28 +0000611 srcs := library.sourceProvider.Srcs()
612 if len(srcs) == 0 {
613 return nil, errors.New("Source provider generated 0 sources")
614 }
Dan Albert06feee92021-03-19 15:06:02 -0700615 // Assume the first source from the source provider is the library entry point.
Matthew Maurera28404a2023-11-20 23:33:28 +0000616 return srcs[0], nil
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000617 } else {
Matthew Maurera28404a2023-11-20 23:33:28 +0000618 return library.baseCompiler.checkedCrateRootPath()
Dan Albert06feee92021-03-19 15:06:02 -0700619 }
620}
621
622func (library *libraryDecorator) rustdoc(ctx ModuleContext, flags Flags,
623 deps PathDeps) android.OptionalPath {
624 // rustdoc has builtin support for documenting config specific information
625 // regardless of the actual config it was given
626 // (https://doc.rust-lang.org/rustdoc/advanced-features.html#cfgdoc-documenting-platform-specific-or-feature-specific-information),
627 // so we generate the rustdoc for only the primary module so that we have a
628 // single set of docs to refer to.
629 if ctx.Module() != ctx.PrimaryModule() {
630 return android.OptionalPath{}
631 }
632
Matthew Maurera28404a2023-11-20 23:33:28 +0000633 return android.OptionalPathForPath(Rustdoc(ctx, crateRootPath(ctx, library),
Dan Albert06feee92021-03-19 15:06:02 -0700634 deps, flags))
635}
636
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700637func (library *libraryDecorator) getStem(ctx ModuleContext) string {
638 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
639 validateLibraryStem(ctx, stem, library.crateName())
640
641 return stem + String(library.baseCompiler.Properties.Suffix)
642}
643
Ivan Lozano2b081132020-09-08 12:46:52 -0400644func (library *libraryDecorator) install(ctx ModuleContext) {
645 // Only shared and dylib variants make sense to install.
646 if library.shared() || library.dylib() {
647 library.baseCompiler.install(ctx)
648 }
649}
650
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400651func (library *libraryDecorator) Disabled() bool {
652 return library.MutatedProperties.VariantIsDisabled
653}
654
655func (library *libraryDecorator) SetDisabled() {
656 library.MutatedProperties.VariantIsDisabled = true
657}
658
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700659var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
660
661func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
662 if crate_name == "" {
663 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
664 }
665
666 // crate_names are used for the library output file, and rustc expects these
667 // to be alphanumeric with underscores allowed.
668 if validCrateName.MatchString(crate_name) {
669 ctx.PropertyErrorf("crate_name",
670 "library crate_names must be alphanumeric with underscores allowed")
671 }
672
673 // Libraries are expected to begin with "lib" followed by the crate_name
674 if !strings.HasPrefix(filename, "lib"+crate_name) {
675 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
676 }
677}
678
Colin Cross8a49a3d2024-05-20 12:22:27 -0700679type libraryTransitionMutator struct{}
680
681func (libraryTransitionMutator) Split(ctx android.BaseModuleContext) []string {
682 m, ok := ctx.Module().(*Module)
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200683 if !ok || m.compiler == nil {
Colin Cross8a49a3d2024-05-20 12:22:27 -0700684 return []string{""}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200685 }
686 library, ok := m.compiler.(libraryInterface)
687 if !ok {
Colin Cross8a49a3d2024-05-20 12:22:27 -0700688 return []string{""}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200689 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400690
Ivan Lozano0a468a42024-05-13 21:03:34 -0400691 // Don't produce rlib/dylib/source variants for shared or static variants
692 if library.shared() || library.static() {
Colin Cross8a49a3d2024-05-20 12:22:27 -0700693 return []string{""}
Ivan Lozano0a468a42024-05-13 21:03:34 -0400694 }
695
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200696 var variants []string
697 // The source variant is used for SourceProvider modules. The other variants (i.e. rlib and dylib)
698 // depend on this variant. It must be the first variant to be declared.
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200699 if m.sourceProvider != nil {
Colin Cross8a49a3d2024-05-20 12:22:27 -0700700 variants = append(variants, sourceVariation)
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200701 }
702 if library.buildRlib() {
703 variants = append(variants, rlibVariation)
704 }
705 if library.buildDylib() {
706 variants = append(variants, dylibVariation)
707 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400708
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200709 if len(variants) == 0 {
Colin Cross8a49a3d2024-05-20 12:22:27 -0700710 return []string{""}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200711 }
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200712
Colin Cross8a49a3d2024-05-20 12:22:27 -0700713 return variants
714}
Ivan Lozano1921e802021-05-20 13:39:16 -0400715
Colin Cross8a49a3d2024-05-20 12:22:27 -0700716func (libraryTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
717 return ""
718}
719
720func (libraryTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
721 m, ok := ctx.Module().(*Module)
722 if !ok || m.compiler == nil {
723 return ""
724 }
725 library, ok := m.compiler.(libraryInterface)
726 if !ok {
727 return ""
728 }
729
730 if incomingVariation == "" {
731 if m.sourceProvider != nil {
732 return sourceVariation
733 }
734 if library.shared() {
735 return ""
736 }
737 if library.buildRlib() {
738 return rlibVariation
739 }
740 if library.buildDylib() {
741 return dylibVariation
Ivan Lozanoffee3342019-08-27 12:03:00 -0700742 }
743 }
Colin Cross8a49a3d2024-05-20 12:22:27 -0700744 return incomingVariation
745}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200746
Colin Cross8a49a3d2024-05-20 12:22:27 -0700747func (libraryTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) {
748 m, ok := ctx.Module().(*Module)
749 if !ok || m.compiler == nil {
750 return
751 }
752 library, ok := m.compiler.(libraryInterface)
753 if !ok {
754 return
755 }
756
757 switch variation {
758 case rlibVariation:
759 library.setRlib()
760 case dylibVariation:
761 library.setDylib()
762 if m.ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
763 // TODO(b/165791368)
764 // Disable dylib Vendor Ramdisk variations until we support these.
765 m.Disable()
766 }
767
768 case sourceVariation:
769 library.setSource()
770 // The source variant does not produce any library.
771 // Disable the compilation steps.
772 m.compiler.SetDisabled()
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400773 }
774
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200775 // If a source variant is created, add an inter-variant dependency
776 // between the other variants and the source variant.
Colin Cross8a49a3d2024-05-20 12:22:27 -0700777 if m.sourceProvider != nil && variation != sourceVariation {
778 ctx.AddVariationDependencies(
779 []blueprint.Variation{
780 {"rust_libraries", sourceVariation},
781 },
782 sourceDepTag, ctx.ModuleName())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200783 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700784}
Ivan Lozano2b081132020-09-08 12:46:52 -0400785
Colin Cross8a49a3d2024-05-20 12:22:27 -0700786type libstdTransitionMutator struct{}
Ivan Lozano2b081132020-09-08 12:46:52 -0400787
Colin Cross8a49a3d2024-05-20 12:22:27 -0700788func (libstdTransitionMutator) Split(ctx android.BaseModuleContext) []string {
789 if m, ok := ctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() {
790 // Only create a variant if a library is actually being built.
791 if library, ok := m.compiler.(libraryInterface); ok {
792 if library.rlib() && !library.sysroot() {
Ivan Lozano806efd32024-12-11 21:38:53 +0000793 return []string{"rlib-std", "dylib-std"}
Ivan Lozano2b081132020-09-08 12:46:52 -0400794 }
795 }
796 }
Colin Cross8a49a3d2024-05-20 12:22:27 -0700797 return []string{""}
798}
799
800func (libstdTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
801 return ""
802}
803
804func (libstdTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
805 if m, ok := ctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() {
806 if library, ok := m.compiler.(libraryInterface); ok {
807 if library.shared() {
808 return ""
809 }
810 if library.rlib() && !library.sysroot() {
811 if incomingVariation != "" {
812 return incomingVariation
813 }
814 return "rlib-std"
815 }
816 }
817 }
818 return ""
819}
820
821func (libstdTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) {
822 if variation == "rlib-std" {
823 rlib := ctx.Module().(*Module)
824 rlib.compiler.(libraryInterface).setRlibStd()
825 rlib.Properties.RustSubName += RlibStdlibSuffix
826 } else if variation == "dylib-std" {
827 dylib := ctx.Module().(*Module)
828 dylib.compiler.(libraryInterface).setDylibStd()
829 if dylib.ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
830 // TODO(b/165791368)
831 // Disable rlibs that link against dylib-std on vendor ramdisk variations until those dylib
832 // variants are properly supported.
833 dylib.Disable()
834 }
835 }
Ivan Lozano2b081132020-09-08 12:46:52 -0400836}