blob: 2d62dcfe47af27a94950dea04bda7de7a63c1089 [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)
Matthew Maurer2ae05132020-06-23 14:28:53 -070043 android.RegisterModuleType("rust_ffi_host", RustFFIHostFactory)
44 android.RegisterModuleType("rust_ffi_host_shared", RustFFISharedHostFactory)
Ivan Lozano806efd32024-12-11 21:38:53 +000045 android.RegisterModuleType("rust_ffi_static", RustLibraryRlibFactory)
46 android.RegisterModuleType("rust_ffi_host_static", RustLibraryRlibHostFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070047}
48
49type VariantLibraryProperties struct {
Matthew Maurerc761eec2020-06-25 00:47:46 -070050 Enabled *bool `android:"arch_variant"`
51 Srcs []string `android:"path,arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070052}
53
54type LibraryCompilerProperties struct {
Ivan Lozano52767be2019-10-18 14:49:46 -070055 Rlib VariantLibraryProperties `android:"arch_variant"`
56 Dylib VariantLibraryProperties `android:"arch_variant"`
57 Shared VariantLibraryProperties `android:"arch_variant"`
58 Static VariantLibraryProperties `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070059
Ivan Lozanof033ca62024-03-21 13:43:14 -040060 // TODO: Remove this when all instances of Include_dirs have been removed from rust_ffi modules.
61 // 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 -070062 Include_dirs []string `android:"path,arch_variant"`
Ivan Lozano2b081132020-09-08 12:46:52 -040063
Ivan Lozanof033ca62024-03-21 13:43:14 -040064 // path to include directories to export to cc_* modules, only relevant for static/shared variants.
65 Export_include_dirs []string `android:"path,arch_variant"`
66
Ivan Lozanof4589012024-11-20 22:18:11 +000067 // Version script to pass to the linker. By default this will replace the
68 // implicit rustc emitted version script to mirror expected behavior in CC.
69 // This is only relevant for rust_ffi_shared modules which are exposing a
70 // versioned C API.
71 Version_script *string `android:"path,arch_variant"`
72
73 // A version_script formatted text file with additional symbols to export
74 // for rust shared or dylibs which the rustc compiler does not automatically
75 // export, e.g. additional symbols from whole_static_libs. Unlike
76 // Version_script, this is not meant to imply a stable API.
77 Extra_exported_symbols *string `android:"path,arch_variant"`
78
Ivan Lozano2b081132020-09-08 12:46:52 -040079 // Whether this library is part of the Rust toolchain sysroot.
80 Sysroot *bool
Ashutosh Agarwal46e4fad2024-08-27 17:13:12 +000081
82 // Exclude this rust_ffi target from being included in APEXes.
83 // TODO(b/362509506): remove this once stubs are properly supported by rust_ffi targets.
84 Apex_exclude *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070085}
86
87type LibraryMutatedProperties struct {
Ivan Lozanoffee3342019-08-27 12:03:00 -070088 // Build a dylib variant
89 BuildDylib bool `blueprint:"mutated"`
90 // Build an rlib variant
91 BuildRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070092 // Build a shared library variant
93 BuildShared bool `blueprint:"mutated"`
94 // Build a static library variant
95 BuildStatic bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070096
97 // This variant is a dylib
98 VariantIsDylib bool `blueprint:"mutated"`
99 // This variant is an rlib
100 VariantIsRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -0700101 // This variant is a shared library
102 VariantIsShared bool `blueprint:"mutated"`
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200103 // This variant is a source provider
104 VariantIsSource bool `blueprint:"mutated"`
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400105
106 // This variant is disabled and should not be compiled
107 // (used for SourceProvider variants that produce only source)
108 VariantIsDisabled bool `blueprint:"mutated"`
Ivan Lozano2b081132020-09-08 12:46:52 -0400109
110 // Whether this library variant should be link libstd via rlibs
111 VariantIsStaticStd bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -0700112}
113
114type libraryDecorator struct {
115 *baseCompiler
Matthew Maurerbb3add12020-06-25 09:34:12 -0700116 *flagExporter
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200117 stripper Stripper
Ivan Lozanoffee3342019-08-27 12:03:00 -0700118
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400119 Properties LibraryCompilerProperties
120 MutatedProperties LibraryMutatedProperties
121 includeDirs android.Paths
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400122 sourceProvider SourceProvider
Ivan Lozano1921e802021-05-20 13:39:16 -0400123
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400124 // table-of-contents file for cdylib crates to optimize out relinking when possible
125 tocFile android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700126}
127
128type libraryInterface interface {
129 rlib() bool
130 dylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700131 static() bool
132 shared() bool
Ivan Lozano2b081132020-09-08 12:46:52 -0400133 sysroot() bool
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200134 source() bool
Ashutosh Agarwal46e4fad2024-08-27 17:13:12 +0000135 apexExclude() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700136
137 // Returns true if the build options for the module have selected a particular build type
138 buildRlib() bool
139 buildDylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700140 buildShared() bool
141 buildStatic() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700142
143 // Sets a particular variant type
144 setRlib()
145 setDylib()
Ivan Lozano52767be2019-10-18 14:49:46 -0700146 setShared()
147 setStatic()
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200148 setSource()
Ivan Lozano52767be2019-10-18 14:49:46 -0700149
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400150 // libstd linkage functions
151 rlibStd() bool
Ivan Lozano2b081132020-09-08 12:46:52 -0400152 setRlibStd()
153 setDylibStd()
154
Ivan Lozano52767be2019-10-18 14:49:46 -0700155 // Build a specific library variant
Matthew Maurer2ae05132020-06-23 14:28:53 -0700156 BuildOnlyFFI()
157 BuildOnlyRust()
Ivan Lozano52767be2019-10-18 14:49:46 -0700158 BuildOnlyRlib()
159 BuildOnlyDylib()
160 BuildOnlyStatic()
161 BuildOnlyShared()
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400162
163 toc() android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700164}
165
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400166func (library *libraryDecorator) nativeCoverage() bool {
167 return true
168}
169
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400170func (library *libraryDecorator) toc() android.OptionalPath {
171 return library.tocFile
172}
173
Ivan Lozanoffee3342019-08-27 12:03:00 -0700174func (library *libraryDecorator) rlib() bool {
175 return library.MutatedProperties.VariantIsRlib
176}
177
Ivan Lozano2b081132020-09-08 12:46:52 -0400178func (library *libraryDecorator) sysroot() bool {
179 return Bool(library.Properties.Sysroot)
180}
181
Ivan Lozanoffee3342019-08-27 12:03:00 -0700182func (library *libraryDecorator) dylib() bool {
183 return library.MutatedProperties.VariantIsDylib
184}
185
Ivan Lozano52767be2019-10-18 14:49:46 -0700186func (library *libraryDecorator) shared() bool {
187 return library.MutatedProperties.VariantIsShared
188}
189
190func (library *libraryDecorator) static() bool {
Colin Cross17f9dc52024-07-01 20:05:54 -0700191 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700192}
193
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200194func (library *libraryDecorator) source() bool {
195 return library.MutatedProperties.VariantIsSource
196}
197
Ashutosh Agarwal46e4fad2024-08-27 17:13:12 +0000198func (library *libraryDecorator) apexExclude() bool {
199 return Bool(library.Properties.Apex_exclude)
200}
201
Ivan Lozanoffee3342019-08-27 12:03:00 -0700202func (library *libraryDecorator) buildRlib() bool {
203 return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true)
204}
205
206func (library *libraryDecorator) buildDylib() bool {
207 return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true)
208}
209
Ivan Lozano52767be2019-10-18 14:49:46 -0700210func (library *libraryDecorator) buildShared() bool {
211 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
212}
213
214func (library *libraryDecorator) buildStatic() bool {
215 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
216}
217
Ivan Lozanoffee3342019-08-27 12:03:00 -0700218func (library *libraryDecorator) setRlib() {
219 library.MutatedProperties.VariantIsRlib = true
220 library.MutatedProperties.VariantIsDylib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700221 library.MutatedProperties.VariantIsShared = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700222}
223
224func (library *libraryDecorator) setDylib() {
225 library.MutatedProperties.VariantIsRlib = false
226 library.MutatedProperties.VariantIsDylib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700227 library.MutatedProperties.VariantIsShared = false
228}
229
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400230func (library *libraryDecorator) rlibStd() bool {
231 return library.MutatedProperties.VariantIsStaticStd
232}
233
Ivan Lozano2b081132020-09-08 12:46:52 -0400234func (library *libraryDecorator) setRlibStd() {
235 library.MutatedProperties.VariantIsStaticStd = true
236}
237
238func (library *libraryDecorator) setDylibStd() {
239 library.MutatedProperties.VariantIsStaticStd = false
240}
241
Ivan Lozano52767be2019-10-18 14:49:46 -0700242func (library *libraryDecorator) setShared() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700243 library.MutatedProperties.VariantIsShared = true
244 library.MutatedProperties.VariantIsRlib = false
245 library.MutatedProperties.VariantIsDylib = false
246}
247
248func (library *libraryDecorator) setStatic() {
Colin Cross17f9dc52024-07-01 20:05:54 -0700249 panic(fmt.Errorf("static variant is not supported for rust modules, use the rlib variant instead"))
Ivan Lozanoffee3342019-08-27 12:03:00 -0700250}
251
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200252func (library *libraryDecorator) setSource() {
253 library.MutatedProperties.VariantIsSource = true
254}
255
Liz Kammer356f7d42021-01-26 09:18:53 -0500256func (library *libraryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400257 if library.preferRlib() {
Ivan Lozanoea086132020-12-08 14:43:00 -0500258 return rlibAutoDep
259 } else if library.rlib() || library.static() {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700260 return rlibAutoDep
261 } else if library.dylib() || library.shared() {
262 return dylibAutoDep
263 } else {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200264 panic(fmt.Errorf("autoDep called on library %q that has no enabled variants.", ctx.ModuleName()))
Matthew Maurer0f003b12020-06-29 14:34:06 -0700265 }
266}
267
Ivan Lozano806efd32024-12-11 21:38:53 +0000268func (library *libraryDecorator) stdLinkage(device bool) RustLinkage {
269 if library.static() || library.MutatedProperties.VariantIsStaticStd {
Ivan Lozanoea086132020-12-08 14:43:00 -0500270 return RlibLinkage
271 } else if library.baseCompiler.preferRlib() {
272 return RlibLinkage
273 }
Ivan Lozano806efd32024-12-11 21:38:53 +0000274 return DylibLinkage
Ivan Lozanoea086132020-12-08 14:43:00 -0500275}
276
Ivan Lozanoffee3342019-08-27 12:03:00 -0700277var _ compiler = (*libraryDecorator)(nil)
Ivan Lozano52767be2019-10-18 14:49:46 -0700278var _ libraryInterface = (*libraryDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700279var _ exportedFlagsProducer = (*libraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700280
Martin Geisler67ec0542022-11-18 12:08:55 +0100281// rust_library produces all Rust variants (rust_library_dylib and
282// rust_library_rlib).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700283func RustLibraryFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700284 module, library := NewRustLibrary(android.HostAndDeviceSupported)
285 library.BuildOnlyRust()
286 return module.Init()
287}
288
Ivan Lozano61848422024-12-13 19:45:00 +0000289// rust_ffi produces all FFI variants (rust_ffi_shared, rust_ffi_static).
Matthew Maurer2ae05132020-06-23 14:28:53 -0700290func RustFFIFactory() android.Module {
291 module, library := NewRustLibrary(android.HostAndDeviceSupported)
292 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700293 return module.Init()
294}
295
Martin Geisler67ec0542022-11-18 12:08:55 +0100296// rust_library_dylib produces a Rust dylib (Rust crate type "dylib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700297func RustLibraryDylibFactory() android.Module {
298 module, library := NewRustLibrary(android.HostAndDeviceSupported)
299 library.BuildOnlyDylib()
300 return module.Init()
301}
302
Ivan Lozano806efd32024-12-11 21:38:53 +0000303// rust_library_rlib and rust_ffi_static produces an rlib (Rust crate type "rlib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700304func RustLibraryRlibFactory() android.Module {
305 module, library := NewRustLibrary(android.HostAndDeviceSupported)
306 library.BuildOnlyRlib()
307 return module.Init()
308}
309
Martin Geisler67ec0542022-11-18 12:08:55 +0100310// rust_ffi_shared produces a shared library (Rust crate type
311// "cdylib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700312func RustFFISharedFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700313 module, library := NewRustLibrary(android.HostAndDeviceSupported)
314 library.BuildOnlyShared()
315 return module.Init()
316}
317
Martin Geisler67ec0542022-11-18 12:08:55 +0100318// rust_library_host produces all Rust variants for the host
319// (rust_library_dylib_host and rust_library_rlib_host).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700320func RustLibraryHostFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700321 module, library := NewRustLibrary(android.HostSupported)
322 library.BuildOnlyRust()
323 return module.Init()
324}
325
Martin Geisler67ec0542022-11-18 12:08:55 +0100326// rust_ffi_host produces all FFI variants for the host
Ivan Lozano61848422024-12-13 19:45:00 +0000327// (rust_ffi_static_host and rust_ffi_shared_host).
Matthew Maurer2ae05132020-06-23 14:28:53 -0700328func RustFFIHostFactory() android.Module {
329 module, library := NewRustLibrary(android.HostSupported)
330 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700331 return module.Init()
332}
333
Martin Geisler67ec0542022-11-18 12:08:55 +0100334// rust_library_dylib_host produces a dylib for the host (Rust crate
335// type "dylib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700336func RustLibraryDylibHostFactory() android.Module {
337 module, library := NewRustLibrary(android.HostSupported)
338 library.BuildOnlyDylib()
339 return module.Init()
340}
341
Ivan Lozano806efd32024-12-11 21:38:53 +0000342// rust_library_rlib_host and rust_ffi_static_host produces an rlib for the host
343// (Rust crate type "rlib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700344func RustLibraryRlibHostFactory() android.Module {
345 module, library := NewRustLibrary(android.HostSupported)
346 library.BuildOnlyRlib()
347 return module.Init()
348}
349
Martin Geisler67ec0542022-11-18 12:08:55 +0100350// rust_ffi_shared_host produces an shared library for the host (Rust
351// crate type "cdylib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700352func RustFFISharedHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700353 module, library := NewRustLibrary(android.HostSupported)
354 library.BuildOnlyShared()
355 return module.Init()
356}
357
Matthew Maurer2ae05132020-06-23 14:28:53 -0700358func (library *libraryDecorator) BuildOnlyFFI() {
359 library.MutatedProperties.BuildDylib = false
Ivan Lozano0a468a42024-05-13 21:03:34 -0400360 // we build rlibs for later static ffi linkage.
361 library.MutatedProperties.BuildRlib = true
Matthew Maurer2ae05132020-06-23 14:28:53 -0700362 library.MutatedProperties.BuildShared = true
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400363 library.MutatedProperties.BuildStatic = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700364}
365
366func (library *libraryDecorator) BuildOnlyRust() {
367 library.MutatedProperties.BuildDylib = true
368 library.MutatedProperties.BuildRlib = true
369 library.MutatedProperties.BuildShared = false
370 library.MutatedProperties.BuildStatic = false
371}
372
Ivan Lozanoffee3342019-08-27 12:03:00 -0700373func (library *libraryDecorator) BuildOnlyDylib() {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700374 library.MutatedProperties.BuildDylib = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700375 library.MutatedProperties.BuildRlib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700376 library.MutatedProperties.BuildShared = false
377 library.MutatedProperties.BuildStatic = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700378}
379
380func (library *libraryDecorator) BuildOnlyRlib() {
381 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700382 library.MutatedProperties.BuildRlib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700383 library.MutatedProperties.BuildShared = false
384 library.MutatedProperties.BuildStatic = false
385}
386
387func (library *libraryDecorator) BuildOnlyStatic() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700388 library.MutatedProperties.BuildRlib = false
389 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700390 library.MutatedProperties.BuildShared = false
391 library.MutatedProperties.BuildStatic = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700392}
393
394func (library *libraryDecorator) BuildOnlyShared() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700395 library.MutatedProperties.BuildRlib = false
396 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700397 library.MutatedProperties.BuildStatic = false
398 library.MutatedProperties.BuildShared = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700399}
400
401func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400402 module := newModule(hod, android.MultilibBoth)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700403
404 library := &libraryDecorator{
405 MutatedProperties: LibraryMutatedProperties{
Matthew Maurer2ae05132020-06-23 14:28:53 -0700406 BuildDylib: false,
407 BuildRlib: false,
408 BuildShared: false,
409 BuildStatic: false,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700410 },
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800411 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Matthew Maurerbb3add12020-06-25 09:34:12 -0700412 flagExporter: NewFlagExporter(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700413 }
414
415 module.compiler = library
416
417 return module, library
418}
419
420func (library *libraryDecorator) compilerProps() []interface{} {
421 return append(library.baseCompiler.compilerProps(),
422 &library.Properties,
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200423 &library.MutatedProperties,
424 &library.stripper.StripProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700425}
426
Ivan Lozanof1c84332019-09-20 11:00:37 -0700427func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
428 deps = library.baseCompiler.compilerDeps(ctx, deps)
429
Colin Crosse32f0932022-01-23 20:48:36 -0800430 if library.dylib() || library.shared() {
431 if ctx.toolchain().Bionic() {
432 deps = bionicDeps(ctx, deps, false)
433 deps.CrtBegin = []string{"crtbegin_so"}
434 deps.CrtEnd = []string{"crtend_so"}
435 } else if ctx.Os() == android.LinuxMusl {
436 deps = muslDeps(ctx, deps, false)
437 deps.CrtBegin = []string{"libc_musl_crtbegin_so"}
438 deps.CrtEnd = []string{"libc_musl_crtend_so"}
439 }
Ivan Lozanof1c84332019-09-20 11:00:37 -0700440 }
441
442 return deps
443}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400444
445func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string {
446 return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
447}
448
Ivan Lozano28ed8f42024-05-06 21:46:39 -0400449// Library cfg flags common to all variants
450func CommonLibraryCfgFlags(ctx android.ModuleContext, flags Flags) Flags {
451 return flags
452}
453
Ivan Lozano67eada32021-09-23 11:50:33 -0400454func (library *libraryDecorator) cfgFlags(ctx ModuleContext, flags Flags) Flags {
455 flags = library.baseCompiler.cfgFlags(ctx, flags)
Ivan Lozano28ed8f42024-05-06 21:46:39 -0400456 flags = CommonLibraryCfgFlags(ctx, flags)
457
Cole Faustfdec8722024-05-22 11:38:29 -0700458 cfgs := library.baseCompiler.Properties.Cfgs.GetOrDefault(ctx, nil)
459
Cole Faustfdec8722024-05-22 11:38:29 -0700460 cfgFlags := cfgsToFlags(cfgs)
461
462 flags.RustFlags = append(flags.RustFlags, cfgFlags...)
463 flags.RustdocFlags = append(flags.RustdocFlags, cfgFlags...)
Ivan Lozano28ed8f42024-05-06 21:46:39 -0400464
465 return flags
466}
467
468// Common flags applied to all libraries irrespective of properties or variant should be included here
469func CommonLibraryCompilerFlags(ctx android.ModuleContext, flags Flags) Flags {
470 flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
Ivan Lozano67eada32021-09-23 11:50:33 -0400471
472 return flags
473}
474
475func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800476 flags = library.baseCompiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400477
Ivan Lozano28ed8f42024-05-06 21:46:39 -0400478 flags = CommonLibraryCompilerFlags(ctx, flags)
479
Ivan Lozano806efd32024-12-11 21:38:53 +0000480 if library.rlib() || library.shared() {
481 // rlibs collect include dirs as well since they are used to
482 // produce staticlibs in the final C linkages
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800483 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
Ivan Lozanof033ca62024-03-21 13:43:14 -0400484 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Export_include_dirs)...)
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800485 }
Ivan Lozano0a468a42024-05-13 21:03:34 -0400486
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400487 if library.shared() {
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -0700488 if ctx.Darwin() {
489 flags.LinkFlags = append(
490 flags.LinkFlags,
491 "-dynamic_lib",
492 "-install_name @rpath/"+library.sharedLibFilename(ctx),
493 )
494 } else {
495 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
496 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400497 }
498
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800499 return flags
500}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700501
Sasha Smundaka76acba2022-04-18 20:12:56 -0700502func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
503 var outputFile android.ModuleOutPath
504 var ret buildOutput
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200505 var fileName string
Matthew Maurera28404a2023-11-20 23:33:28 +0000506 crateRootPath := crateRootPath(ctx, library)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700507
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400508 if library.sourceProvider != nil {
Ivan Lozano9d74a522020-12-01 09:25:22 -0500509 deps.srcProviderFiles = append(deps.srcProviderFiles, library.sourceProvider.Srcs()...)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400510 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700511
Ivan Lozano0a468a42024-05-13 21:03:34 -0400512 // Ensure link dirs are not duplicated
513 deps.linkDirs = android.FirstUniqueStrings(deps.linkDirs)
514
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400515 // Calculate output filename
516 if library.rlib() {
517 fileName = library.getStem(ctx) + ctx.toolchain().RlibSuffix()
518 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700519 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400520 } else if library.dylib() {
521 fileName = library.getStem(ctx) + ctx.toolchain().DylibSuffix()
522 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700523 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400524 } else if library.static() {
525 fileName = library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
526 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700527 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400528 } else if library.shared() {
529 fileName = library.sharedLibFilename(ctx)
530 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700531 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400532 }
533
534 if !library.rlib() && !library.static() && library.stripper.NeedsStrip(ctx) {
535 strippedOutputFile := outputFile
536 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
537 library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
538
539 library.baseCompiler.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
540 }
541 library.baseCompiler.unstrippedOutputFile = outputFile
542
Ivan Lozanoffee3342019-08-27 12:03:00 -0700543 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500544 flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...)
Colin Cross004bd3f2023-10-02 11:39:17 -0700545 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700546
Ivan Lozanof4589012024-11-20 22:18:11 +0000547 if String(library.Properties.Version_script) != "" {
548 if String(library.Properties.Extra_exported_symbols) != "" {
549 ctx.ModuleErrorf("version_script and extra_exported_symbols cannot both be set.")
550 }
551
552 if library.shared() {
553 // "-Wl,--android-version-script" signals to the rustcLinker script
554 // that the default version script should be removed.
555 flags.LinkFlags = append(flags.LinkFlags, "-Wl,--android-version-script="+android.PathForModuleSrc(ctx, String(library.Properties.Version_script)).String())
556 deps.LinkerDeps = append(deps.LinkerDeps, android.PathForModuleSrc(ctx, String(library.Properties.Version_script)))
557 } else if !library.static() && !library.rlib() {
558 // We include rlibs here because rust_ffi produces rlib variants
559 ctx.PropertyErrorf("version_script", "can only be set for rust_ffi modules")
560 }
561 }
562
563 if String(library.Properties.Extra_exported_symbols) != "" {
564 // Passing a second version script (rustc calculates and emits a
565 // default version script) will concatenate the first version script.
566 flags.LinkFlags = append(flags.LinkFlags, "-Wl,--version-script="+android.PathForModuleSrc(ctx, String(library.Properties.Extra_exported_symbols)).String())
567 deps.LinkerDeps = append(deps.LinkerDeps, android.PathForModuleSrc(ctx, String(library.Properties.Extra_exported_symbols)))
568 }
569
Matthew Maurer46c46cc2020-01-13 16:34:34 -0800570 if library.dylib() {
Ivan Lozanof4589012024-11-20 22:18:11 +0000571
Ivan Lozano52767be2019-10-18 14:49:46 -0700572 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
573 // https://github.com/rust-lang/rust/issues/19680
574 // https://github.com/rust-lang/rust/issues/34909
575 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
576 }
577
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400578 // Call the appropriate builder for this library type
Ivan Lozanoffee3342019-08-27 12:03:00 -0700579 if library.rlib() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000580 ret.kytheFile = TransformSrctoRlib(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700581 } else if library.dylib() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000582 ret.kytheFile = TransformSrctoDylib(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700583 } else if library.static() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000584 ret.kytheFile = TransformSrctoStatic(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700585 } else if library.shared() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000586 ret.kytheFile = TransformSrctoShared(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700587 }
588
Ivan Lozano52767be2019-10-18 14:49:46 -0700589 if library.rlib() || library.dylib() {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700590 library.flagExporter.exportLinkDirs(deps.linkDirs...)
Colin Cross0de8a1e2020-09-18 14:15:30 -0700591 library.flagExporter.exportLinkObjects(deps.linkObjects...)
Ivan Lozano52767be2019-10-18 14:49:46 -0700592 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700593
Ivan Lozano0a468a42024-05-13 21:03:34 -0400594 // Since we have FFI rlibs, we need to collect their includes as well
595 if library.static() || library.shared() || library.rlib() {
Colin Cross40213022023-12-13 15:19:49 -0800596 android.SetProvider(ctx, cc.FlagExporterInfoProvider, cc.FlagExporterInfo{
Ivan Lozano0a468a42024-05-13 21:03:34 -0400597 IncludeDirs: android.FirstUniquePaths(library.includeDirs),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700598 })
599 }
600
601 if library.shared() {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400602 // Optimize out relinking against shared libraries whose interface hasn't changed by
603 // depending on a table of contents file instead of the library itself.
604 tocFile := outputFile.ReplaceExtension(ctx, flags.Toolchain.SharedLibSuffix()[1:]+".toc")
605 library.tocFile = android.OptionalPathForPath(tocFile)
606 cc.TransformSharedObjectToToc(ctx, outputFile, tocFile)
607
Colin Cross40213022023-12-13 15:19:49 -0800608 android.SetProvider(ctx, cc.SharedLibraryInfoProvider, cc.SharedLibraryInfo{
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400609 TableOfContents: android.OptionalPathForPath(tocFile),
610 SharedLibrary: outputFile,
611 Target: ctx.Target(),
Colin Crossb614cd42024-10-11 12:52:21 -0700612 // TODO: when rust supports stubs uses the stubs state rather than inferring it from
613 // apex_exclude.
614 IsStubs: Bool(library.Properties.Apex_exclude),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700615 })
616 }
617
618 if library.static() {
Colin Crossa14fb6a2024-10-23 16:57:06 -0700619 depSet := depset.NewBuilder[android.Path](depset.TOPOLOGICAL).Direct(outputFile).Build()
Colin Cross40213022023-12-13 15:19:49 -0800620 android.SetProvider(ctx, cc.StaticLibraryInfoProvider, cc.StaticLibraryInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700621 StaticLibrary: outputFile,
622
623 TransitiveStaticLibrariesForOrdering: depSet,
624 })
625 }
626
627 library.flagExporter.setProvider(ctx)
628
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400629 return ret
Ivan Lozanoffee3342019-08-27 12:03:00 -0700630}
631
Matthew Maurera28404a2023-11-20 23:33:28 +0000632func (library *libraryDecorator) checkedCrateRootPath() (android.Path, error) {
Dan Albert06feee92021-03-19 15:06:02 -0700633 if library.sourceProvider != nil {
Matthew Maurera28404a2023-11-20 23:33:28 +0000634 srcs := library.sourceProvider.Srcs()
635 if len(srcs) == 0 {
636 return nil, errors.New("Source provider generated 0 sources")
637 }
Dan Albert06feee92021-03-19 15:06:02 -0700638 // Assume the first source from the source provider is the library entry point.
Matthew Maurera28404a2023-11-20 23:33:28 +0000639 return srcs[0], nil
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000640 } else {
Matthew Maurera28404a2023-11-20 23:33:28 +0000641 return library.baseCompiler.checkedCrateRootPath()
Dan Albert06feee92021-03-19 15:06:02 -0700642 }
643}
644
645func (library *libraryDecorator) rustdoc(ctx ModuleContext, flags Flags,
646 deps PathDeps) android.OptionalPath {
647 // rustdoc has builtin support for documenting config specific information
648 // regardless of the actual config it was given
649 // (https://doc.rust-lang.org/rustdoc/advanced-features.html#cfgdoc-documenting-platform-specific-or-feature-specific-information),
650 // so we generate the rustdoc for only the primary module so that we have a
651 // single set of docs to refer to.
652 if ctx.Module() != ctx.PrimaryModule() {
653 return android.OptionalPath{}
654 }
655
Matthew Maurera28404a2023-11-20 23:33:28 +0000656 return android.OptionalPathForPath(Rustdoc(ctx, crateRootPath(ctx, library),
Dan Albert06feee92021-03-19 15:06:02 -0700657 deps, flags))
658}
659
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700660func (library *libraryDecorator) getStem(ctx ModuleContext) string {
661 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
662 validateLibraryStem(ctx, stem, library.crateName())
663
664 return stem + String(library.baseCompiler.Properties.Suffix)
665}
666
Ivan Lozano2b081132020-09-08 12:46:52 -0400667func (library *libraryDecorator) install(ctx ModuleContext) {
668 // Only shared and dylib variants make sense to install.
669 if library.shared() || library.dylib() {
670 library.baseCompiler.install(ctx)
671 }
672}
673
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400674func (library *libraryDecorator) Disabled() bool {
675 return library.MutatedProperties.VariantIsDisabled
676}
677
678func (library *libraryDecorator) SetDisabled() {
679 library.MutatedProperties.VariantIsDisabled = true
680}
681
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700682var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
683
684func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
685 if crate_name == "" {
686 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
687 }
688
689 // crate_names are used for the library output file, and rustc expects these
690 // to be alphanumeric with underscores allowed.
691 if validCrateName.MatchString(crate_name) {
692 ctx.PropertyErrorf("crate_name",
693 "library crate_names must be alphanumeric with underscores allowed")
694 }
695
696 // Libraries are expected to begin with "lib" followed by the crate_name
697 if !strings.HasPrefix(filename, "lib"+crate_name) {
698 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
699 }
700}
701
Colin Cross8a49a3d2024-05-20 12:22:27 -0700702type libraryTransitionMutator struct{}
703
704func (libraryTransitionMutator) Split(ctx android.BaseModuleContext) []string {
705 m, ok := ctx.Module().(*Module)
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200706 if !ok || m.compiler == nil {
Colin Cross8a49a3d2024-05-20 12:22:27 -0700707 return []string{""}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200708 }
709 library, ok := m.compiler.(libraryInterface)
710 if !ok {
Colin Cross8a49a3d2024-05-20 12:22:27 -0700711 return []string{""}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200712 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400713
Ivan Lozano0a468a42024-05-13 21:03:34 -0400714 // Don't produce rlib/dylib/source variants for shared or static variants
715 if library.shared() || library.static() {
Colin Cross8a49a3d2024-05-20 12:22:27 -0700716 return []string{""}
Ivan Lozano0a468a42024-05-13 21:03:34 -0400717 }
718
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200719 var variants []string
720 // The source variant is used for SourceProvider modules. The other variants (i.e. rlib and dylib)
721 // depend on this variant. It must be the first variant to be declared.
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200722 if m.sourceProvider != nil {
Colin Cross8a49a3d2024-05-20 12:22:27 -0700723 variants = append(variants, sourceVariation)
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200724 }
725 if library.buildRlib() {
726 variants = append(variants, rlibVariation)
727 }
728 if library.buildDylib() {
729 variants = append(variants, dylibVariation)
730 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400731
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200732 if len(variants) == 0 {
Colin Cross8a49a3d2024-05-20 12:22:27 -0700733 return []string{""}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200734 }
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200735
Colin Cross8a49a3d2024-05-20 12:22:27 -0700736 return variants
737}
Ivan Lozano1921e802021-05-20 13:39:16 -0400738
Colin Cross8a49a3d2024-05-20 12:22:27 -0700739func (libraryTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
740 return ""
741}
742
743func (libraryTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
744 m, ok := ctx.Module().(*Module)
745 if !ok || m.compiler == nil {
746 return ""
747 }
748 library, ok := m.compiler.(libraryInterface)
749 if !ok {
750 return ""
751 }
752
753 if incomingVariation == "" {
754 if m.sourceProvider != nil {
755 return sourceVariation
756 }
757 if library.shared() {
758 return ""
759 }
760 if library.buildRlib() {
761 return rlibVariation
762 }
763 if library.buildDylib() {
764 return dylibVariation
Ivan Lozanoffee3342019-08-27 12:03:00 -0700765 }
766 }
Colin Cross8a49a3d2024-05-20 12:22:27 -0700767 return incomingVariation
768}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200769
Colin Cross8a49a3d2024-05-20 12:22:27 -0700770func (libraryTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) {
771 m, ok := ctx.Module().(*Module)
772 if !ok || m.compiler == nil {
773 return
774 }
775 library, ok := m.compiler.(libraryInterface)
776 if !ok {
777 return
778 }
779
780 switch variation {
781 case rlibVariation:
782 library.setRlib()
783 case dylibVariation:
784 library.setDylib()
785 if m.ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
786 // TODO(b/165791368)
787 // Disable dylib Vendor Ramdisk variations until we support these.
788 m.Disable()
789 }
790
791 case sourceVariation:
792 library.setSource()
793 // The source variant does not produce any library.
794 // Disable the compilation steps.
795 m.compiler.SetDisabled()
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400796 }
797
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200798 // If a source variant is created, add an inter-variant dependency
799 // between the other variants and the source variant.
Colin Cross8a49a3d2024-05-20 12:22:27 -0700800 if m.sourceProvider != nil && variation != sourceVariation {
801 ctx.AddVariationDependencies(
802 []blueprint.Variation{
803 {"rust_libraries", sourceVariation},
804 },
805 sourceDepTag, ctx.ModuleName())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200806 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700807}
Ivan Lozano2b081132020-09-08 12:46:52 -0400808
Colin Cross8a49a3d2024-05-20 12:22:27 -0700809type libstdTransitionMutator struct{}
Ivan Lozano2b081132020-09-08 12:46:52 -0400810
Colin Cross8a49a3d2024-05-20 12:22:27 -0700811func (libstdTransitionMutator) Split(ctx android.BaseModuleContext) []string {
812 if m, ok := ctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() {
813 // Only create a variant if a library is actually being built.
814 if library, ok := m.compiler.(libraryInterface); ok {
815 if library.rlib() && !library.sysroot() {
Ivan Lozano806efd32024-12-11 21:38:53 +0000816 return []string{"rlib-std", "dylib-std"}
Ivan Lozano2b081132020-09-08 12:46:52 -0400817 }
818 }
819 }
Colin Cross8a49a3d2024-05-20 12:22:27 -0700820 return []string{""}
821}
822
823func (libstdTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
824 return ""
825}
826
827func (libstdTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
828 if m, ok := ctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() {
829 if library, ok := m.compiler.(libraryInterface); ok {
830 if library.shared() {
831 return ""
832 }
833 if library.rlib() && !library.sysroot() {
834 if incomingVariation != "" {
835 return incomingVariation
836 }
837 return "rlib-std"
838 }
839 }
840 }
841 return ""
842}
843
844func (libstdTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) {
845 if variation == "rlib-std" {
846 rlib := ctx.Module().(*Module)
847 rlib.compiler.(libraryInterface).setRlibStd()
848 rlib.Properties.RustSubName += RlibStdlibSuffix
849 } else if variation == "dylib-std" {
850 dylib := ctx.Module().(*Module)
851 dylib.compiler.(libraryInterface).setDylibStd()
852 if dylib.ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
853 // TODO(b/165791368)
854 // Disable rlibs that link against dylib-std on vendor ramdisk variations until those dylib
855 // variants are properly supported.
856 dylib.Disable()
857 }
858 }
Ivan Lozano2b081132020-09-08 12:46:52 -0400859}