blob: 50d5a72ae745398ed9a6a111293e3489f2653eda [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"
24
Ivan Lozanoffee3342019-08-27 12:03:00 -070025 "android/soong/android"
Colin Cross0de8a1e2020-09-18 14:15:30 -070026 "android/soong/cc"
Ivan Lozanoffee3342019-08-27 12:03:00 -070027)
28
Ivan Lozano2b081132020-09-08 12:46:52 -040029var (
Ivan Lozano4df02572023-06-15 14:21:09 -040030 RlibStdlibSuffix = ".rlib-std"
Ivan Lozano2b081132020-09-08 12:46:52 -040031)
32
Ivan Lozanoffee3342019-08-27 12:03:00 -070033func init() {
34 android.RegisterModuleType("rust_library", RustLibraryFactory)
35 android.RegisterModuleType("rust_library_dylib", RustLibraryDylibFactory)
36 android.RegisterModuleType("rust_library_rlib", RustLibraryRlibFactory)
37 android.RegisterModuleType("rust_library_host", RustLibraryHostFactory)
38 android.RegisterModuleType("rust_library_host_dylib", RustLibraryDylibHostFactory)
39 android.RegisterModuleType("rust_library_host_rlib", RustLibraryRlibHostFactory)
Matthew Maurer2ae05132020-06-23 14:28:53 -070040 android.RegisterModuleType("rust_ffi", RustFFIFactory)
41 android.RegisterModuleType("rust_ffi_shared", RustFFISharedFactory)
Ivan Lozano0a468a42024-05-13 21:03:34 -040042 android.RegisterModuleType("rust_ffi_rlib", RustFFIRlibFactory)
Matthew Maurer2ae05132020-06-23 14:28:53 -070043 android.RegisterModuleType("rust_ffi_host", RustFFIHostFactory)
44 android.RegisterModuleType("rust_ffi_host_shared", RustFFISharedHostFactory)
Ivan Lozano0a468a42024-05-13 21:03:34 -040045 android.RegisterModuleType("rust_ffi_host_rlib", RustFFIRlibHostFactory)
46
47 // TODO: Remove when all instances of rust_ffi_static have been switched to rust_ffi_rlib
Ivan Lozanofd47b1a2024-05-17 14:13:41 -040048 // Alias rust_ffi_static to the rust_ffi_rlib factory
49 android.RegisterModuleType("rust_ffi_static", RustFFIRlibFactory)
50 android.RegisterModuleType("rust_ffi_host_static", RustFFIRlibHostFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070051}
52
53type VariantLibraryProperties struct {
Matthew Maurerc761eec2020-06-25 00:47:46 -070054 Enabled *bool `android:"arch_variant"`
55 Srcs []string `android:"path,arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070056}
57
58type LibraryCompilerProperties struct {
Ivan Lozano52767be2019-10-18 14:49:46 -070059 Rlib VariantLibraryProperties `android:"arch_variant"`
60 Dylib VariantLibraryProperties `android:"arch_variant"`
61 Shared VariantLibraryProperties `android:"arch_variant"`
62 Static VariantLibraryProperties `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070063
Ivan Lozanof033ca62024-03-21 13:43:14 -040064 // TODO: Remove this when all instances of Include_dirs have been removed from rust_ffi modules.
65 // 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 -070066 Include_dirs []string `android:"path,arch_variant"`
Ivan Lozano2b081132020-09-08 12:46:52 -040067
Ivan Lozanof033ca62024-03-21 13:43:14 -040068 // path to include directories to export to cc_* modules, only relevant for static/shared variants.
69 Export_include_dirs []string `android:"path,arch_variant"`
70
Ivan Lozano2b081132020-09-08 12:46:52 -040071 // Whether this library is part of the Rust toolchain sysroot.
72 Sysroot *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070073}
74
75type LibraryMutatedProperties struct {
Ivan Lozanoffee3342019-08-27 12:03:00 -070076 // Build a dylib variant
77 BuildDylib bool `blueprint:"mutated"`
78 // Build an rlib variant
79 BuildRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070080 // Build a shared library variant
81 BuildShared bool `blueprint:"mutated"`
82 // Build a static library variant
83 BuildStatic bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070084
85 // This variant is a dylib
86 VariantIsDylib bool `blueprint:"mutated"`
87 // This variant is an rlib
88 VariantIsRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070089 // This variant is a shared library
90 VariantIsShared bool `blueprint:"mutated"`
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +020091 // This variant is a source provider
92 VariantIsSource bool `blueprint:"mutated"`
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040093
94 // This variant is disabled and should not be compiled
95 // (used for SourceProvider variants that produce only source)
96 VariantIsDisabled bool `blueprint:"mutated"`
Ivan Lozano2b081132020-09-08 12:46:52 -040097
98 // Whether this library variant should be link libstd via rlibs
99 VariantIsStaticStd bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -0700100}
101
102type libraryDecorator struct {
103 *baseCompiler
Matthew Maurerbb3add12020-06-25 09:34:12 -0700104 *flagExporter
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200105 stripper Stripper
Ivan Lozanoffee3342019-08-27 12:03:00 -0700106
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400107 Properties LibraryCompilerProperties
108 MutatedProperties LibraryMutatedProperties
109 includeDirs android.Paths
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400110 sourceProvider SourceProvider
Ivan Lozano1921e802021-05-20 13:39:16 -0400111
Ivan Lozano0a468a42024-05-13 21:03:34 -0400112 isFFI bool
113
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400114 // table-of-contents file for cdylib crates to optimize out relinking when possible
115 tocFile android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700116}
117
118type libraryInterface interface {
119 rlib() bool
120 dylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700121 static() bool
122 shared() bool
Ivan Lozano2b081132020-09-08 12:46:52 -0400123 sysroot() bool
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200124 source() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700125
126 // Returns true if the build options for the module have selected a particular build type
127 buildRlib() bool
128 buildDylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700129 buildShared() bool
130 buildStatic() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700131
132 // Sets a particular variant type
133 setRlib()
134 setDylib()
Ivan Lozano52767be2019-10-18 14:49:46 -0700135 setShared()
136 setStatic()
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200137 setSource()
Ivan Lozano52767be2019-10-18 14:49:46 -0700138
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400139 // libstd linkage functions
140 rlibStd() bool
Ivan Lozano2b081132020-09-08 12:46:52 -0400141 setRlibStd()
142 setDylibStd()
143
Ivan Lozano52767be2019-10-18 14:49:46 -0700144 // Build a specific library variant
Matthew Maurer2ae05132020-06-23 14:28:53 -0700145 BuildOnlyFFI()
146 BuildOnlyRust()
Ivan Lozano52767be2019-10-18 14:49:46 -0700147 BuildOnlyRlib()
148 BuildOnlyDylib()
149 BuildOnlyStatic()
150 BuildOnlyShared()
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400151
152 toc() android.OptionalPath
Ivan Lozano0a468a42024-05-13 21:03:34 -0400153
154 isFFILibrary() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700155}
156
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400157func (library *libraryDecorator) nativeCoverage() bool {
158 return true
159}
160
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400161func (library *libraryDecorator) toc() android.OptionalPath {
162 return library.tocFile
163}
164
Ivan Lozanoffee3342019-08-27 12:03:00 -0700165func (library *libraryDecorator) rlib() bool {
166 return library.MutatedProperties.VariantIsRlib
167}
168
Ivan Lozano2b081132020-09-08 12:46:52 -0400169func (library *libraryDecorator) sysroot() bool {
170 return Bool(library.Properties.Sysroot)
171}
172
Ivan Lozanoffee3342019-08-27 12:03:00 -0700173func (library *libraryDecorator) dylib() bool {
174 return library.MutatedProperties.VariantIsDylib
175}
176
Ivan Lozano52767be2019-10-18 14:49:46 -0700177func (library *libraryDecorator) shared() bool {
178 return library.MutatedProperties.VariantIsShared
179}
180
181func (library *libraryDecorator) static() bool {
Colin Cross17f9dc52024-07-01 20:05:54 -0700182 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700183}
184
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200185func (library *libraryDecorator) source() bool {
186 return library.MutatedProperties.VariantIsSource
187}
188
Ivan Lozanoffee3342019-08-27 12:03:00 -0700189func (library *libraryDecorator) buildRlib() bool {
190 return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true)
191}
192
193func (library *libraryDecorator) buildDylib() bool {
194 return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true)
195}
196
Ivan Lozano52767be2019-10-18 14:49:46 -0700197func (library *libraryDecorator) buildShared() bool {
198 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
199}
200
201func (library *libraryDecorator) buildStatic() bool {
202 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
203}
204
Ivan Lozanoffee3342019-08-27 12:03:00 -0700205func (library *libraryDecorator) setRlib() {
206 library.MutatedProperties.VariantIsRlib = true
207 library.MutatedProperties.VariantIsDylib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700208 library.MutatedProperties.VariantIsShared = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700209}
210
211func (library *libraryDecorator) setDylib() {
212 library.MutatedProperties.VariantIsRlib = false
213 library.MutatedProperties.VariantIsDylib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700214 library.MutatedProperties.VariantIsShared = false
215}
216
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400217func (library *libraryDecorator) rlibStd() bool {
218 return library.MutatedProperties.VariantIsStaticStd
219}
220
Ivan Lozano2b081132020-09-08 12:46:52 -0400221func (library *libraryDecorator) setRlibStd() {
222 library.MutatedProperties.VariantIsStaticStd = true
223}
224
225func (library *libraryDecorator) setDylibStd() {
226 library.MutatedProperties.VariantIsStaticStd = false
227}
228
Ivan Lozano52767be2019-10-18 14:49:46 -0700229func (library *libraryDecorator) setShared() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700230 library.MutatedProperties.VariantIsShared = true
231 library.MutatedProperties.VariantIsRlib = false
232 library.MutatedProperties.VariantIsDylib = false
233}
234
235func (library *libraryDecorator) setStatic() {
Colin Cross17f9dc52024-07-01 20:05:54 -0700236 panic(fmt.Errorf("static variant is not supported for rust modules, use the rlib variant instead"))
Ivan Lozanoffee3342019-08-27 12:03:00 -0700237}
238
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200239func (library *libraryDecorator) setSource() {
240 library.MutatedProperties.VariantIsSource = true
241}
242
Liz Kammer356f7d42021-01-26 09:18:53 -0500243func (library *libraryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400244 if library.preferRlib() {
Ivan Lozanoea086132020-12-08 14:43:00 -0500245 return rlibAutoDep
246 } else if library.rlib() || library.static() {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700247 return rlibAutoDep
248 } else if library.dylib() || library.shared() {
249 return dylibAutoDep
250 } else {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200251 panic(fmt.Errorf("autoDep called on library %q that has no enabled variants.", ctx.ModuleName()))
Matthew Maurer0f003b12020-06-29 14:34:06 -0700252 }
253}
254
Ivan Lozanoea086132020-12-08 14:43:00 -0500255func (library *libraryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400256 if library.static() || library.MutatedProperties.VariantIsStaticStd || (library.rlib() && library.isFFILibrary()) {
Ivan Lozanoea086132020-12-08 14:43:00 -0500257 return RlibLinkage
258 } else if library.baseCompiler.preferRlib() {
259 return RlibLinkage
260 }
261 return DefaultLinkage
262}
263
Ivan Lozanoffee3342019-08-27 12:03:00 -0700264var _ compiler = (*libraryDecorator)(nil)
Ivan Lozano52767be2019-10-18 14:49:46 -0700265var _ libraryInterface = (*libraryDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700266var _ exportedFlagsProducer = (*libraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700267
Martin Geisler67ec0542022-11-18 12:08:55 +0100268// rust_library produces all Rust variants (rust_library_dylib and
269// rust_library_rlib).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700270func RustLibraryFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700271 module, library := NewRustLibrary(android.HostAndDeviceSupported)
272 library.BuildOnlyRust()
273 return module.Init()
274}
275
Ivan Lozano0a468a42024-05-13 21:03:34 -0400276// rust_ffi produces all FFI variants (rust_ffi_shared, rust_ffi_static, and
277// rust_ffi_rlib).
Matthew Maurer2ae05132020-06-23 14:28:53 -0700278func RustFFIFactory() android.Module {
279 module, library := NewRustLibrary(android.HostAndDeviceSupported)
280 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700281 return module.Init()
282}
283
Martin Geisler67ec0542022-11-18 12:08:55 +0100284// rust_library_dylib produces a Rust dylib (Rust crate type "dylib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700285func RustLibraryDylibFactory() android.Module {
286 module, library := NewRustLibrary(android.HostAndDeviceSupported)
287 library.BuildOnlyDylib()
288 return module.Init()
289}
290
Martin Geisler67ec0542022-11-18 12:08:55 +0100291// rust_library_rlib produces an rlib (Rust crate type "rlib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700292func RustLibraryRlibFactory() android.Module {
293 module, library := NewRustLibrary(android.HostAndDeviceSupported)
294 library.BuildOnlyRlib()
295 return module.Init()
296}
297
Martin Geisler67ec0542022-11-18 12:08:55 +0100298// rust_ffi_shared produces a shared library (Rust crate type
299// "cdylib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700300func RustFFISharedFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700301 module, library := NewRustLibrary(android.HostAndDeviceSupported)
302 library.BuildOnlyShared()
303 return module.Init()
304}
305
Martin Geisler67ec0542022-11-18 12:08:55 +0100306// rust_library_host produces all Rust variants for the host
307// (rust_library_dylib_host and rust_library_rlib_host).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700308func RustLibraryHostFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700309 module, library := NewRustLibrary(android.HostSupported)
310 library.BuildOnlyRust()
311 return module.Init()
312}
313
Martin Geisler67ec0542022-11-18 12:08:55 +0100314// rust_ffi_host produces all FFI variants for the host
Ivan Lozano0a468a42024-05-13 21:03:34 -0400315// (rust_ffi_rlib_host, rust_ffi_static_host, and rust_ffi_shared_host).
Matthew Maurer2ae05132020-06-23 14:28:53 -0700316func RustFFIHostFactory() android.Module {
317 module, library := NewRustLibrary(android.HostSupported)
318 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700319 return module.Init()
320}
321
Martin Geisler67ec0542022-11-18 12:08:55 +0100322// rust_library_dylib_host produces a dylib for the host (Rust crate
323// type "dylib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700324func RustLibraryDylibHostFactory() android.Module {
325 module, library := NewRustLibrary(android.HostSupported)
326 library.BuildOnlyDylib()
327 return module.Init()
328}
329
Martin Geisler67ec0542022-11-18 12:08:55 +0100330// rust_library_rlib_host produces an rlib for the host (Rust crate
331// type "rlib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700332func RustLibraryRlibHostFactory() android.Module {
333 module, library := NewRustLibrary(android.HostSupported)
334 library.BuildOnlyRlib()
335 return module.Init()
336}
337
Martin Geisler67ec0542022-11-18 12:08:55 +0100338// rust_ffi_shared_host produces an shared library for the host (Rust
339// crate type "cdylib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700340func RustFFISharedHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700341 module, library := NewRustLibrary(android.HostSupported)
342 library.BuildOnlyShared()
343 return module.Init()
344}
345
Ivan Lozano0a468a42024-05-13 21:03:34 -0400346// rust_ffi_rlib_host produces an rlib for the host (Rust crate
347// type "rlib").
348func RustFFIRlibHostFactory() android.Module {
349 module, library := NewRustLibrary(android.HostSupported)
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400350 library.BuildOnlyRlib()
Ivan Lozano0a468a42024-05-13 21:03:34 -0400351
352 library.isFFI = true
353 return module.Init()
354}
355
356// rust_ffi_rlib produces an rlib (Rust crate type "rlib").
357func RustFFIRlibFactory() android.Module {
358 module, library := NewRustLibrary(android.HostAndDeviceSupported)
359 library.BuildOnlyRlib()
360
361 library.isFFI = true
362 return module.Init()
363}
364
Matthew Maurer2ae05132020-06-23 14:28:53 -0700365func (library *libraryDecorator) BuildOnlyFFI() {
366 library.MutatedProperties.BuildDylib = false
Ivan Lozano0a468a42024-05-13 21:03:34 -0400367 // we build rlibs for later static ffi linkage.
368 library.MutatedProperties.BuildRlib = true
Matthew Maurer2ae05132020-06-23 14:28:53 -0700369 library.MutatedProperties.BuildShared = true
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400370 library.MutatedProperties.BuildStatic = false
Ivan Lozano0a468a42024-05-13 21:03:34 -0400371
372 library.isFFI = true
Matthew Maurer2ae05132020-06-23 14:28:53 -0700373}
374
375func (library *libraryDecorator) BuildOnlyRust() {
376 library.MutatedProperties.BuildDylib = true
377 library.MutatedProperties.BuildRlib = true
378 library.MutatedProperties.BuildShared = false
379 library.MutatedProperties.BuildStatic = false
380}
381
Ivan Lozanoffee3342019-08-27 12:03:00 -0700382func (library *libraryDecorator) BuildOnlyDylib() {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700383 library.MutatedProperties.BuildDylib = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700384 library.MutatedProperties.BuildRlib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700385 library.MutatedProperties.BuildShared = false
386 library.MutatedProperties.BuildStatic = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700387}
388
389func (library *libraryDecorator) BuildOnlyRlib() {
390 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700391 library.MutatedProperties.BuildRlib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700392 library.MutatedProperties.BuildShared = false
393 library.MutatedProperties.BuildStatic = false
394}
395
396func (library *libraryDecorator) BuildOnlyStatic() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700397 library.MutatedProperties.BuildRlib = false
398 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700399 library.MutatedProperties.BuildShared = false
400 library.MutatedProperties.BuildStatic = true
Ivan Lozano0a468a42024-05-13 21:03:34 -0400401
402 library.isFFI = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700403}
404
405func (library *libraryDecorator) BuildOnlyShared() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700406 library.MutatedProperties.BuildRlib = false
407 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700408 library.MutatedProperties.BuildStatic = false
409 library.MutatedProperties.BuildShared = true
Ivan Lozano0a468a42024-05-13 21:03:34 -0400410
411 library.isFFI = true
412}
413
414func (library *libraryDecorator) isFFILibrary() bool {
415 return library.isFFI
Ivan Lozanoffee3342019-08-27 12:03:00 -0700416}
417
418func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400419 module := newModule(hod, android.MultilibBoth)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700420
421 library := &libraryDecorator{
422 MutatedProperties: LibraryMutatedProperties{
Matthew Maurer2ae05132020-06-23 14:28:53 -0700423 BuildDylib: false,
424 BuildRlib: false,
425 BuildShared: false,
426 BuildStatic: false,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700427 },
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800428 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Matthew Maurerbb3add12020-06-25 09:34:12 -0700429 flagExporter: NewFlagExporter(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700430 }
431
432 module.compiler = library
433
434 return module, library
435}
436
437func (library *libraryDecorator) compilerProps() []interface{} {
438 return append(library.baseCompiler.compilerProps(),
439 &library.Properties,
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200440 &library.MutatedProperties,
441 &library.stripper.StripProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700442}
443
Ivan Lozanof1c84332019-09-20 11:00:37 -0700444func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
445 deps = library.baseCompiler.compilerDeps(ctx, deps)
446
Colin Crosse32f0932022-01-23 20:48:36 -0800447 if library.dylib() || library.shared() {
448 if ctx.toolchain().Bionic() {
449 deps = bionicDeps(ctx, deps, false)
450 deps.CrtBegin = []string{"crtbegin_so"}
451 deps.CrtEnd = []string{"crtend_so"}
452 } else if ctx.Os() == android.LinuxMusl {
453 deps = muslDeps(ctx, deps, false)
454 deps.CrtBegin = []string{"libc_musl_crtbegin_so"}
455 deps.CrtEnd = []string{"libc_musl_crtend_so"}
456 }
Ivan Lozanof1c84332019-09-20 11:00:37 -0700457 }
458
459 return deps
460}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400461
462func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string {
463 return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
464}
465
Ivan Lozano28ed8f42024-05-06 21:46:39 -0400466// Library cfg flags common to all variants
467func CommonLibraryCfgFlags(ctx android.ModuleContext, flags Flags) Flags {
468 return flags
469}
470
Ivan Lozano67eada32021-09-23 11:50:33 -0400471func (library *libraryDecorator) cfgFlags(ctx ModuleContext, flags Flags) Flags {
472 flags = library.baseCompiler.cfgFlags(ctx, flags)
Ivan Lozano28ed8f42024-05-06 21:46:39 -0400473 flags = CommonLibraryCfgFlags(ctx, flags)
474
Cole Faustfdec8722024-05-22 11:38:29 -0700475 cfgs := library.baseCompiler.Properties.Cfgs.GetOrDefault(ctx, nil)
476
Stephen Crane0dbfc562021-07-07 19:05:02 -0700477 if library.dylib() {
478 // We need to add a dependency on std in order to link crates as dylibs.
479 // The hack to add this dependency is guarded by the following cfg so
480 // that we don't force a dependency when it isn't needed.
Cole Faustfdec8722024-05-22 11:38:29 -0700481 cfgs = append(cfgs, "android_dylib")
Stephen Crane0dbfc562021-07-07 19:05:02 -0700482 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400483
Cole Faustfdec8722024-05-22 11:38:29 -0700484 cfgFlags := cfgsToFlags(cfgs)
485
486 flags.RustFlags = append(flags.RustFlags, cfgFlags...)
487 flags.RustdocFlags = append(flags.RustdocFlags, cfgFlags...)
Ivan Lozano28ed8f42024-05-06 21:46:39 -0400488
489 return flags
490}
491
492// Common flags applied to all libraries irrespective of properties or variant should be included here
493func CommonLibraryCompilerFlags(ctx android.ModuleContext, flags Flags) Flags {
494 flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
Ivan Lozano67eada32021-09-23 11:50:33 -0400495
496 return flags
497}
498
499func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800500 flags = library.baseCompiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400501
Ivan Lozano28ed8f42024-05-06 21:46:39 -0400502 flags = CommonLibraryCompilerFlags(ctx, flags)
503
Ivan Lozano0a468a42024-05-13 21:03:34 -0400504 if library.isFFI {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800505 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
Ivan Lozanof033ca62024-03-21 13:43:14 -0400506 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Export_include_dirs)...)
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800507 }
Ivan Lozano0a468a42024-05-13 21:03:34 -0400508
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400509 if library.shared() {
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -0700510 if ctx.Darwin() {
511 flags.LinkFlags = append(
512 flags.LinkFlags,
513 "-dynamic_lib",
514 "-install_name @rpath/"+library.sharedLibFilename(ctx),
515 )
516 } else {
517 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
518 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400519 }
520
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800521 return flags
522}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700523
Sasha Smundaka76acba2022-04-18 20:12:56 -0700524func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
525 var outputFile android.ModuleOutPath
526 var ret buildOutput
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200527 var fileName string
Matthew Maurera28404a2023-11-20 23:33:28 +0000528 crateRootPath := crateRootPath(ctx, library)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700529
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400530 if library.sourceProvider != nil {
Ivan Lozano9d74a522020-12-01 09:25:22 -0500531 deps.srcProviderFiles = append(deps.srcProviderFiles, library.sourceProvider.Srcs()...)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400532 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700533
Ivan Lozano0a468a42024-05-13 21:03:34 -0400534 // Ensure link dirs are not duplicated
535 deps.linkDirs = android.FirstUniqueStrings(deps.linkDirs)
536
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400537 // Calculate output filename
538 if library.rlib() {
539 fileName = library.getStem(ctx) + ctx.toolchain().RlibSuffix()
540 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700541 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400542 } else if library.dylib() {
543 fileName = library.getStem(ctx) + ctx.toolchain().DylibSuffix()
544 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700545 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400546 } else if library.static() {
547 fileName = library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
548 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700549 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400550 } else if library.shared() {
551 fileName = library.sharedLibFilename(ctx)
552 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700553 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400554 }
555
556 if !library.rlib() && !library.static() && library.stripper.NeedsStrip(ctx) {
557 strippedOutputFile := outputFile
558 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
559 library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
560
561 library.baseCompiler.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
562 }
563 library.baseCompiler.unstrippedOutputFile = outputFile
564
Ivan Lozanoffee3342019-08-27 12:03:00 -0700565 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500566 flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...)
Colin Cross004bd3f2023-10-02 11:39:17 -0700567 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700568
Matthew Maurer46c46cc2020-01-13 16:34:34 -0800569 if library.dylib() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700570 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
571 // https://github.com/rust-lang/rust/issues/19680
572 // https://github.com/rust-lang/rust/issues/34909
573 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
574 }
575
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400576 // Call the appropriate builder for this library type
Ivan Lozanoffee3342019-08-27 12:03:00 -0700577 if library.rlib() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000578 ret.kytheFile = TransformSrctoRlib(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700579 } else if library.dylib() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000580 ret.kytheFile = TransformSrctoDylib(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700581 } else if library.static() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000582 ret.kytheFile = TransformSrctoStatic(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700583 } else if library.shared() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000584 ret.kytheFile = TransformSrctoShared(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700585 }
586
Ivan Lozano52767be2019-10-18 14:49:46 -0700587 if library.rlib() || library.dylib() {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700588 library.flagExporter.exportLinkDirs(deps.linkDirs...)
Colin Cross0de8a1e2020-09-18 14:15:30 -0700589 library.flagExporter.exportLinkObjects(deps.linkObjects...)
Ivan Lozano52767be2019-10-18 14:49:46 -0700590 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700591
Ivan Lozano0a468a42024-05-13 21:03:34 -0400592 // Since we have FFI rlibs, we need to collect their includes as well
593 if library.static() || library.shared() || library.rlib() {
Colin Cross40213022023-12-13 15:19:49 -0800594 android.SetProvider(ctx, cc.FlagExporterInfoProvider, cc.FlagExporterInfo{
Ivan Lozano0a468a42024-05-13 21:03:34 -0400595 IncludeDirs: android.FirstUniquePaths(library.includeDirs),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700596 })
597 }
598
599 if library.shared() {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400600 // Optimize out relinking against shared libraries whose interface hasn't changed by
601 // depending on a table of contents file instead of the library itself.
602 tocFile := outputFile.ReplaceExtension(ctx, flags.Toolchain.SharedLibSuffix()[1:]+".toc")
603 library.tocFile = android.OptionalPathForPath(tocFile)
604 cc.TransformSharedObjectToToc(ctx, outputFile, tocFile)
605
Colin Cross40213022023-12-13 15:19:49 -0800606 android.SetProvider(ctx, cc.SharedLibraryInfoProvider, cc.SharedLibraryInfo{
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400607 TableOfContents: android.OptionalPathForPath(tocFile),
608 SharedLibrary: outputFile,
609 Target: ctx.Target(),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700610 })
611 }
612
613 if library.static() {
Colin Crossc85750b2022-04-21 12:50:51 -0700614 depSet := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).Direct(outputFile).Build()
Colin Cross40213022023-12-13 15:19:49 -0800615 android.SetProvider(ctx, cc.StaticLibraryInfoProvider, cc.StaticLibraryInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700616 StaticLibrary: outputFile,
617
618 TransitiveStaticLibrariesForOrdering: depSet,
619 })
620 }
621
622 library.flagExporter.setProvider(ctx)
623
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400624 return ret
Ivan Lozanoffee3342019-08-27 12:03:00 -0700625}
626
Matthew Maurera28404a2023-11-20 23:33:28 +0000627func (library *libraryDecorator) checkedCrateRootPath() (android.Path, error) {
Dan Albert06feee92021-03-19 15:06:02 -0700628 if library.sourceProvider != nil {
Matthew Maurera28404a2023-11-20 23:33:28 +0000629 srcs := library.sourceProvider.Srcs()
630 if len(srcs) == 0 {
631 return nil, errors.New("Source provider generated 0 sources")
632 }
Dan Albert06feee92021-03-19 15:06:02 -0700633 // Assume the first source from the source provider is the library entry point.
Matthew Maurera28404a2023-11-20 23:33:28 +0000634 return srcs[0], nil
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000635 } else {
Matthew Maurera28404a2023-11-20 23:33:28 +0000636 return library.baseCompiler.checkedCrateRootPath()
Dan Albert06feee92021-03-19 15:06:02 -0700637 }
638}
639
640func (library *libraryDecorator) rustdoc(ctx ModuleContext, flags Flags,
641 deps PathDeps) android.OptionalPath {
642 // rustdoc has builtin support for documenting config specific information
643 // regardless of the actual config it was given
644 // (https://doc.rust-lang.org/rustdoc/advanced-features.html#cfgdoc-documenting-platform-specific-or-feature-specific-information),
645 // so we generate the rustdoc for only the primary module so that we have a
646 // single set of docs to refer to.
647 if ctx.Module() != ctx.PrimaryModule() {
648 return android.OptionalPath{}
649 }
650
Matthew Maurera28404a2023-11-20 23:33:28 +0000651 return android.OptionalPathForPath(Rustdoc(ctx, crateRootPath(ctx, library),
Dan Albert06feee92021-03-19 15:06:02 -0700652 deps, flags))
653}
654
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700655func (library *libraryDecorator) getStem(ctx ModuleContext) string {
656 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
657 validateLibraryStem(ctx, stem, library.crateName())
658
659 return stem + String(library.baseCompiler.Properties.Suffix)
660}
661
Ivan Lozano2b081132020-09-08 12:46:52 -0400662func (library *libraryDecorator) install(ctx ModuleContext) {
663 // Only shared and dylib variants make sense to install.
664 if library.shared() || library.dylib() {
665 library.baseCompiler.install(ctx)
666 }
667}
668
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400669func (library *libraryDecorator) Disabled() bool {
670 return library.MutatedProperties.VariantIsDisabled
671}
672
673func (library *libraryDecorator) SetDisabled() {
674 library.MutatedProperties.VariantIsDisabled = true
675}
676
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700677var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
678
679func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
680 if crate_name == "" {
681 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
682 }
683
684 // crate_names are used for the library output file, and rustc expects these
685 // to be alphanumeric with underscores allowed.
686 if validCrateName.MatchString(crate_name) {
687 ctx.PropertyErrorf("crate_name",
688 "library crate_names must be alphanumeric with underscores allowed")
689 }
690
691 // Libraries are expected to begin with "lib" followed by the crate_name
692 if !strings.HasPrefix(filename, "lib"+crate_name) {
693 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
694 }
695}
696
Colin Cross8a49a3d2024-05-20 12:22:27 -0700697type libraryTransitionMutator struct{}
698
699func (libraryTransitionMutator) Split(ctx android.BaseModuleContext) []string {
700 m, ok := ctx.Module().(*Module)
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200701 if !ok || m.compiler == nil {
Colin Cross8a49a3d2024-05-20 12:22:27 -0700702 return []string{""}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200703 }
704 library, ok := m.compiler.(libraryInterface)
705 if !ok {
Colin Cross8a49a3d2024-05-20 12:22:27 -0700706 return []string{""}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200707 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400708
Ivan Lozano0a468a42024-05-13 21:03:34 -0400709 // Don't produce rlib/dylib/source variants for shared or static variants
710 if library.shared() || library.static() {
Colin Cross8a49a3d2024-05-20 12:22:27 -0700711 return []string{""}
Ivan Lozano0a468a42024-05-13 21:03:34 -0400712 }
713
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200714 var variants []string
715 // The source variant is used for SourceProvider modules. The other variants (i.e. rlib and dylib)
716 // depend on this variant. It must be the first variant to be declared.
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200717 if m.sourceProvider != nil {
Colin Cross8a49a3d2024-05-20 12:22:27 -0700718 variants = append(variants, sourceVariation)
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200719 }
720 if library.buildRlib() {
721 variants = append(variants, rlibVariation)
722 }
723 if library.buildDylib() {
724 variants = append(variants, dylibVariation)
725 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400726
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200727 if len(variants) == 0 {
Colin Cross8a49a3d2024-05-20 12:22:27 -0700728 return []string{""}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200729 }
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200730
Colin Cross8a49a3d2024-05-20 12:22:27 -0700731 return variants
732}
Ivan Lozano1921e802021-05-20 13:39:16 -0400733
Colin Cross8a49a3d2024-05-20 12:22:27 -0700734func (libraryTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
735 return ""
736}
737
738func (libraryTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
739 m, ok := ctx.Module().(*Module)
740 if !ok || m.compiler == nil {
741 return ""
742 }
743 library, ok := m.compiler.(libraryInterface)
744 if !ok {
745 return ""
746 }
747
748 if incomingVariation == "" {
749 if m.sourceProvider != nil {
750 return sourceVariation
751 }
752 if library.shared() {
753 return ""
754 }
755 if library.buildRlib() {
756 return rlibVariation
757 }
758 if library.buildDylib() {
759 return dylibVariation
Ivan Lozanoffee3342019-08-27 12:03:00 -0700760 }
761 }
Colin Cross8a49a3d2024-05-20 12:22:27 -0700762 return incomingVariation
763}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200764
Colin Cross8a49a3d2024-05-20 12:22:27 -0700765func (libraryTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) {
766 m, ok := ctx.Module().(*Module)
767 if !ok || m.compiler == nil {
768 return
769 }
770 library, ok := m.compiler.(libraryInterface)
771 if !ok {
772 return
773 }
774
775 switch variation {
776 case rlibVariation:
777 library.setRlib()
778 case dylibVariation:
779 library.setDylib()
780 if m.ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
781 // TODO(b/165791368)
782 // Disable dylib Vendor Ramdisk variations until we support these.
783 m.Disable()
784 }
785
786 case sourceVariation:
787 library.setSource()
788 // The source variant does not produce any library.
789 // Disable the compilation steps.
790 m.compiler.SetDisabled()
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400791 }
792
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200793 // If a source variant is created, add an inter-variant dependency
794 // between the other variants and the source variant.
Colin Cross8a49a3d2024-05-20 12:22:27 -0700795 if m.sourceProvider != nil && variation != sourceVariation {
796 ctx.AddVariationDependencies(
797 []blueprint.Variation{
798 {"rust_libraries", sourceVariation},
799 },
800 sourceDepTag, ctx.ModuleName())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200801 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700802}
Ivan Lozano2b081132020-09-08 12:46:52 -0400803
Colin Cross8a49a3d2024-05-20 12:22:27 -0700804type libstdTransitionMutator struct{}
Ivan Lozano2b081132020-09-08 12:46:52 -0400805
Colin Cross8a49a3d2024-05-20 12:22:27 -0700806func (libstdTransitionMutator) Split(ctx android.BaseModuleContext) []string {
807 if m, ok := ctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() {
808 // Only create a variant if a library is actually being built.
809 if library, ok := m.compiler.(libraryInterface); ok {
810 if library.rlib() && !library.sysroot() {
811 if library.isFFILibrary() {
812 return []string{"rlib-std"}
813 } else {
814 return []string{"rlib-std", "dylib-std"}
Ivan Lozano6a884432020-12-02 09:15:16 -0500815 }
Ivan Lozano2b081132020-09-08 12:46:52 -0400816 }
817 }
818 }
Colin Cross8a49a3d2024-05-20 12:22:27 -0700819 return []string{""}
820}
821
822func (libstdTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
823 return ""
824}
825
826func (libstdTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
827 if m, ok := ctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() {
828 if library, ok := m.compiler.(libraryInterface); ok {
829 if library.shared() {
830 return ""
831 }
832 if library.rlib() && !library.sysroot() {
833 if incomingVariation != "" {
834 return incomingVariation
835 }
836 return "rlib-std"
837 }
838 }
839 }
840 return ""
841}
842
843func (libstdTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) {
844 if variation == "rlib-std" {
845 rlib := ctx.Module().(*Module)
846 rlib.compiler.(libraryInterface).setRlibStd()
847 rlib.Properties.RustSubName += RlibStdlibSuffix
848 } else if variation == "dylib-std" {
849 dylib := ctx.Module().(*Module)
850 dylib.compiler.(libraryInterface).setDylibStd()
851 if dylib.ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
852 // TODO(b/165791368)
853 // Disable rlibs that link against dylib-std on vendor ramdisk variations until those dylib
854 // variants are properly supported.
855 dylib.Disable()
856 }
857 }
Ivan Lozano2b081132020-09-08 12:46:52 -0400858}