blob: 9912c946e616afc2ca4b4c1ae2e59d2c1ecab1ee [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 Lozano9eaacc82024-10-30 14:28:17 +0000280var _ cc.VersionedInterface = (*libraryDecorator)(nil)
281
282func (library *libraryDecorator) HasLLNDKStubs() bool {
283 // Rust does not support LLNDK yet.
284 return false
285}
286
287func (library *libraryDecorator) HasVendorPublicLibrary() bool {
288 // Rust does not support vendor public library.
289 return false
290}
291
292func (library *libraryDecorator) HasLLNDKHeaders() bool {
293 // Rust does not support LLNDK yet.
294 return false
295}
296
297func (library *libraryDecorator) HasStubsVariants() bool {
298 return false
299}
300
301func (library *libraryDecorator) IsStubsImplementationRequired() bool {
302 return false
303}
304
305func (library *libraryDecorator) GetAPIListCoverageXMLPath() android.ModuleOutPath {
306 panic(fmt.Errorf("GetAPIListCoverageXMLPath called on unsupported Rust module"))
307}
308
309func (library *libraryDecorator) AllStubsVersions() []string {
310 panic(fmt.Errorf("AllStubsVersions called on unsupported Rust module"))
311}
312
313func (library *libraryDecorator) SetAllStubsVersions(versions []string) {
314 panic(fmt.Errorf("ApexSdkVersion called on unsupported Rust module"))
315}
316
317func (library *libraryDecorator) SetStubsVersion(version string) {
318 panic(fmt.Errorf("SetStubsVersion called on unsupported Rust module"))
319}
320
321func (library *libraryDecorator) SetBuildStubs(isLatest bool) {
322 panic(fmt.Errorf("SetBuildStubs called on unsupported Rust module"))
323}
324
325func (library *libraryDecorator) BuildStubs() bool {
326 return false
327}
328
329func (library *libraryDecorator) ImplementationModuleName(name string) string {
330 panic(fmt.Errorf("ImplementationModuleName called on unsupported Rust module"))
331}
332
333func (library *libraryDecorator) IsLLNDKMovedToApex() bool {
334 // Rust does not support LLNDK.
335 return false
336}
337
338func (library *libraryDecorator) StubsVersions(ctx android.BaseModuleContext) []string {
339 panic(fmt.Errorf("StubsVersions called on unsupported Rust module"))
340}
341
342func (library *libraryDecorator) StubsVersion() string {
343 panic(fmt.Errorf("StubsVersions called on unsupported Rust module"))
344}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700345
Martin Geisler67ec0542022-11-18 12:08:55 +0100346// rust_library produces all Rust variants (rust_library_dylib and
347// rust_library_rlib).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700348func RustLibraryFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700349 module, library := NewRustLibrary(android.HostAndDeviceSupported)
350 library.BuildOnlyRust()
351 return module.Init()
352}
353
Ivan Lozano61848422024-12-13 19:45:00 +0000354// rust_ffi produces all FFI variants (rust_ffi_shared, rust_ffi_static).
Matthew Maurer2ae05132020-06-23 14:28:53 -0700355func RustFFIFactory() android.Module {
356 module, library := NewRustLibrary(android.HostAndDeviceSupported)
357 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700358 return module.Init()
359}
360
Martin Geisler67ec0542022-11-18 12:08:55 +0100361// rust_library_dylib produces a Rust dylib (Rust crate type "dylib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700362func RustLibraryDylibFactory() android.Module {
363 module, library := NewRustLibrary(android.HostAndDeviceSupported)
364 library.BuildOnlyDylib()
365 return module.Init()
366}
367
Ivan Lozano806efd32024-12-11 21:38:53 +0000368// rust_library_rlib and rust_ffi_static produces an rlib (Rust crate type "rlib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700369func RustLibraryRlibFactory() android.Module {
370 module, library := NewRustLibrary(android.HostAndDeviceSupported)
371 library.BuildOnlyRlib()
372 return module.Init()
373}
374
Martin Geisler67ec0542022-11-18 12:08:55 +0100375// rust_ffi_shared produces a shared library (Rust crate type
376// "cdylib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700377func RustFFISharedFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700378 module, library := NewRustLibrary(android.HostAndDeviceSupported)
379 library.BuildOnlyShared()
380 return module.Init()
381}
382
Martin Geisler67ec0542022-11-18 12:08:55 +0100383// rust_library_host produces all Rust variants for the host
384// (rust_library_dylib_host and rust_library_rlib_host).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700385func RustLibraryHostFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700386 module, library := NewRustLibrary(android.HostSupported)
387 library.BuildOnlyRust()
388 return module.Init()
389}
390
Martin Geisler67ec0542022-11-18 12:08:55 +0100391// rust_ffi_host produces all FFI variants for the host
Ivan Lozano61848422024-12-13 19:45:00 +0000392// (rust_ffi_static_host and rust_ffi_shared_host).
Matthew Maurer2ae05132020-06-23 14:28:53 -0700393func RustFFIHostFactory() android.Module {
394 module, library := NewRustLibrary(android.HostSupported)
395 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700396 return module.Init()
397}
398
Martin Geisler67ec0542022-11-18 12:08:55 +0100399// rust_library_dylib_host produces a dylib for the host (Rust crate
400// type "dylib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700401func RustLibraryDylibHostFactory() android.Module {
402 module, library := NewRustLibrary(android.HostSupported)
403 library.BuildOnlyDylib()
404 return module.Init()
405}
406
Ivan Lozano806efd32024-12-11 21:38:53 +0000407// rust_library_rlib_host and rust_ffi_static_host produces an rlib for the host
408// (Rust crate type "rlib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700409func RustLibraryRlibHostFactory() android.Module {
410 module, library := NewRustLibrary(android.HostSupported)
411 library.BuildOnlyRlib()
412 return module.Init()
413}
414
Martin Geisler67ec0542022-11-18 12:08:55 +0100415// rust_ffi_shared_host produces an shared library for the host (Rust
416// crate type "cdylib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700417func RustFFISharedHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700418 module, library := NewRustLibrary(android.HostSupported)
419 library.BuildOnlyShared()
420 return module.Init()
421}
422
Matthew Maurer2ae05132020-06-23 14:28:53 -0700423func (library *libraryDecorator) BuildOnlyFFI() {
424 library.MutatedProperties.BuildDylib = false
Ivan Lozano0a468a42024-05-13 21:03:34 -0400425 // we build rlibs for later static ffi linkage.
426 library.MutatedProperties.BuildRlib = true
Matthew Maurer2ae05132020-06-23 14:28:53 -0700427 library.MutatedProperties.BuildShared = true
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400428 library.MutatedProperties.BuildStatic = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700429}
430
431func (library *libraryDecorator) BuildOnlyRust() {
432 library.MutatedProperties.BuildDylib = true
433 library.MutatedProperties.BuildRlib = true
434 library.MutatedProperties.BuildShared = false
435 library.MutatedProperties.BuildStatic = false
436}
437
Ivan Lozanoffee3342019-08-27 12:03:00 -0700438func (library *libraryDecorator) BuildOnlyDylib() {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700439 library.MutatedProperties.BuildDylib = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700440 library.MutatedProperties.BuildRlib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700441 library.MutatedProperties.BuildShared = false
442 library.MutatedProperties.BuildStatic = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700443}
444
445func (library *libraryDecorator) BuildOnlyRlib() {
446 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700447 library.MutatedProperties.BuildRlib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700448 library.MutatedProperties.BuildShared = false
449 library.MutatedProperties.BuildStatic = false
450}
451
452func (library *libraryDecorator) BuildOnlyStatic() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700453 library.MutatedProperties.BuildRlib = false
454 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700455 library.MutatedProperties.BuildShared = false
456 library.MutatedProperties.BuildStatic = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700457}
458
459func (library *libraryDecorator) BuildOnlyShared() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700460 library.MutatedProperties.BuildRlib = false
461 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700462 library.MutatedProperties.BuildStatic = false
463 library.MutatedProperties.BuildShared = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700464}
465
466func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400467 module := newModule(hod, android.MultilibBoth)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700468
469 library := &libraryDecorator{
470 MutatedProperties: LibraryMutatedProperties{
Matthew Maurer2ae05132020-06-23 14:28:53 -0700471 BuildDylib: false,
472 BuildRlib: false,
473 BuildShared: false,
474 BuildStatic: false,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700475 },
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800476 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Matthew Maurerbb3add12020-06-25 09:34:12 -0700477 flagExporter: NewFlagExporter(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700478 }
479
480 module.compiler = library
481
482 return module, library
483}
484
485func (library *libraryDecorator) compilerProps() []interface{} {
486 return append(library.baseCompiler.compilerProps(),
487 &library.Properties,
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200488 &library.MutatedProperties,
489 &library.stripper.StripProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700490}
491
Ivan Lozanof1c84332019-09-20 11:00:37 -0700492func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
493 deps = library.baseCompiler.compilerDeps(ctx, deps)
494
Colin Crosse32f0932022-01-23 20:48:36 -0800495 if library.dylib() || library.shared() {
496 if ctx.toolchain().Bionic() {
497 deps = bionicDeps(ctx, deps, false)
498 deps.CrtBegin = []string{"crtbegin_so"}
499 deps.CrtEnd = []string{"crtend_so"}
500 } else if ctx.Os() == android.LinuxMusl {
501 deps = muslDeps(ctx, deps, false)
502 deps.CrtBegin = []string{"libc_musl_crtbegin_so"}
503 deps.CrtEnd = []string{"libc_musl_crtend_so"}
504 }
Ivan Lozanof1c84332019-09-20 11:00:37 -0700505 }
506
507 return deps
508}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400509
510func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string {
511 return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
512}
513
Ivan Lozano28ed8f42024-05-06 21:46:39 -0400514// Library cfg flags common to all variants
515func CommonLibraryCfgFlags(ctx android.ModuleContext, flags Flags) Flags {
516 return flags
517}
518
Ivan Lozano67eada32021-09-23 11:50:33 -0400519func (library *libraryDecorator) cfgFlags(ctx ModuleContext, flags Flags) Flags {
520 flags = library.baseCompiler.cfgFlags(ctx, flags)
Ivan Lozano28ed8f42024-05-06 21:46:39 -0400521 flags = CommonLibraryCfgFlags(ctx, flags)
522
Cole Faustfdec8722024-05-22 11:38:29 -0700523 cfgs := library.baseCompiler.Properties.Cfgs.GetOrDefault(ctx, nil)
524
Cole Faustfdec8722024-05-22 11:38:29 -0700525 cfgFlags := cfgsToFlags(cfgs)
526
527 flags.RustFlags = append(flags.RustFlags, cfgFlags...)
528 flags.RustdocFlags = append(flags.RustdocFlags, cfgFlags...)
Ivan Lozano28ed8f42024-05-06 21:46:39 -0400529
530 return flags
531}
532
533// Common flags applied to all libraries irrespective of properties or variant should be included here
534func CommonLibraryCompilerFlags(ctx android.ModuleContext, flags Flags) Flags {
535 flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
Ivan Lozano67eada32021-09-23 11:50:33 -0400536
537 return flags
538}
539
540func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800541 flags = library.baseCompiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400542
Ivan Lozano28ed8f42024-05-06 21:46:39 -0400543 flags = CommonLibraryCompilerFlags(ctx, flags)
544
Ivan Lozano806efd32024-12-11 21:38:53 +0000545 if library.rlib() || library.shared() {
546 // rlibs collect include dirs as well since they are used to
547 // produce staticlibs in the final C linkages
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800548 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
Ivan Lozanof033ca62024-03-21 13:43:14 -0400549 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Export_include_dirs)...)
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800550 }
Ivan Lozano0a468a42024-05-13 21:03:34 -0400551
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400552 if library.shared() {
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -0700553 if ctx.Darwin() {
554 flags.LinkFlags = append(
555 flags.LinkFlags,
556 "-dynamic_lib",
557 "-install_name @rpath/"+library.sharedLibFilename(ctx),
558 )
559 } else {
560 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
561 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400562 }
563
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800564 return flags
565}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700566
Sasha Smundaka76acba2022-04-18 20:12:56 -0700567func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
568 var outputFile android.ModuleOutPath
569 var ret buildOutput
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200570 var fileName string
Matthew Maurera28404a2023-11-20 23:33:28 +0000571 crateRootPath := crateRootPath(ctx, library)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700572
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400573 if library.sourceProvider != nil {
Ivan Lozano9d74a522020-12-01 09:25:22 -0500574 deps.srcProviderFiles = append(deps.srcProviderFiles, library.sourceProvider.Srcs()...)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400575 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700576
Ivan Lozano0a468a42024-05-13 21:03:34 -0400577 // Ensure link dirs are not duplicated
578 deps.linkDirs = android.FirstUniqueStrings(deps.linkDirs)
579
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400580 // Calculate output filename
581 if library.rlib() {
582 fileName = library.getStem(ctx) + ctx.toolchain().RlibSuffix()
583 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700584 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400585 } else if library.dylib() {
586 fileName = library.getStem(ctx) + ctx.toolchain().DylibSuffix()
587 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700588 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400589 } else if library.static() {
590 fileName = library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
591 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700592 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400593 } else if library.shared() {
594 fileName = library.sharedLibFilename(ctx)
595 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700596 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400597 }
598
599 if !library.rlib() && !library.static() && library.stripper.NeedsStrip(ctx) {
600 strippedOutputFile := outputFile
601 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
602 library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
603
604 library.baseCompiler.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
605 }
606 library.baseCompiler.unstrippedOutputFile = outputFile
607
Ivan Lozanoffee3342019-08-27 12:03:00 -0700608 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500609 flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...)
Colin Cross004bd3f2023-10-02 11:39:17 -0700610 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700611
Ivan Lozanof4589012024-11-20 22:18:11 +0000612 if String(library.Properties.Version_script) != "" {
613 if String(library.Properties.Extra_exported_symbols) != "" {
614 ctx.ModuleErrorf("version_script and extra_exported_symbols cannot both be set.")
615 }
616
617 if library.shared() {
618 // "-Wl,--android-version-script" signals to the rustcLinker script
619 // that the default version script should be removed.
620 flags.LinkFlags = append(flags.LinkFlags, "-Wl,--android-version-script="+android.PathForModuleSrc(ctx, String(library.Properties.Version_script)).String())
621 deps.LinkerDeps = append(deps.LinkerDeps, android.PathForModuleSrc(ctx, String(library.Properties.Version_script)))
622 } else if !library.static() && !library.rlib() {
623 // We include rlibs here because rust_ffi produces rlib variants
624 ctx.PropertyErrorf("version_script", "can only be set for rust_ffi modules")
625 }
626 }
627
628 if String(library.Properties.Extra_exported_symbols) != "" {
629 // Passing a second version script (rustc calculates and emits a
630 // default version script) will concatenate the first version script.
631 flags.LinkFlags = append(flags.LinkFlags, "-Wl,--version-script="+android.PathForModuleSrc(ctx, String(library.Properties.Extra_exported_symbols)).String())
632 deps.LinkerDeps = append(deps.LinkerDeps, android.PathForModuleSrc(ctx, String(library.Properties.Extra_exported_symbols)))
633 }
634
Matthew Maurer46c46cc2020-01-13 16:34:34 -0800635 if library.dylib() {
Ivan Lozanof4589012024-11-20 22:18:11 +0000636
Ivan Lozano52767be2019-10-18 14:49:46 -0700637 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
638 // https://github.com/rust-lang/rust/issues/19680
639 // https://github.com/rust-lang/rust/issues/34909
640 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
641 }
642
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400643 // Call the appropriate builder for this library type
Ivan Lozanoffee3342019-08-27 12:03:00 -0700644 if library.rlib() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000645 ret.kytheFile = TransformSrctoRlib(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700646 } else if library.dylib() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000647 ret.kytheFile = TransformSrctoDylib(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700648 } else if library.static() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000649 ret.kytheFile = TransformSrctoStatic(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700650 } else if library.shared() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000651 ret.kytheFile = TransformSrctoShared(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700652 }
653
Ivan Lozano52767be2019-10-18 14:49:46 -0700654 if library.rlib() || library.dylib() {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700655 library.flagExporter.exportLinkDirs(deps.linkDirs...)
Colin Cross0de8a1e2020-09-18 14:15:30 -0700656 library.flagExporter.exportLinkObjects(deps.linkObjects...)
Ivan Lozano52767be2019-10-18 14:49:46 -0700657 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700658
Ivan Lozano0a468a42024-05-13 21:03:34 -0400659 // Since we have FFI rlibs, we need to collect their includes as well
660 if library.static() || library.shared() || library.rlib() {
Colin Cross40213022023-12-13 15:19:49 -0800661 android.SetProvider(ctx, cc.FlagExporterInfoProvider, cc.FlagExporterInfo{
Ivan Lozano0a468a42024-05-13 21:03:34 -0400662 IncludeDirs: android.FirstUniquePaths(library.includeDirs),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700663 })
664 }
665
666 if library.shared() {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400667 // Optimize out relinking against shared libraries whose interface hasn't changed by
668 // depending on a table of contents file instead of the library itself.
669 tocFile := outputFile.ReplaceExtension(ctx, flags.Toolchain.SharedLibSuffix()[1:]+".toc")
670 library.tocFile = android.OptionalPathForPath(tocFile)
671 cc.TransformSharedObjectToToc(ctx, outputFile, tocFile)
672
Colin Cross40213022023-12-13 15:19:49 -0800673 android.SetProvider(ctx, cc.SharedLibraryInfoProvider, cc.SharedLibraryInfo{
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400674 TableOfContents: android.OptionalPathForPath(tocFile),
675 SharedLibrary: outputFile,
676 Target: ctx.Target(),
Colin Crossb614cd42024-10-11 12:52:21 -0700677 // TODO: when rust supports stubs uses the stubs state rather than inferring it from
678 // apex_exclude.
679 IsStubs: Bool(library.Properties.Apex_exclude),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700680 })
681 }
682
683 if library.static() {
Colin Crossa14fb6a2024-10-23 16:57:06 -0700684 depSet := depset.NewBuilder[android.Path](depset.TOPOLOGICAL).Direct(outputFile).Build()
Colin Cross40213022023-12-13 15:19:49 -0800685 android.SetProvider(ctx, cc.StaticLibraryInfoProvider, cc.StaticLibraryInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700686 StaticLibrary: outputFile,
687
688 TransitiveStaticLibrariesForOrdering: depSet,
689 })
690 }
691
692 library.flagExporter.setProvider(ctx)
693
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400694 return ret
Ivan Lozanoffee3342019-08-27 12:03:00 -0700695}
696
Matthew Maurera28404a2023-11-20 23:33:28 +0000697func (library *libraryDecorator) checkedCrateRootPath() (android.Path, error) {
Dan Albert06feee92021-03-19 15:06:02 -0700698 if library.sourceProvider != nil {
Matthew Maurera28404a2023-11-20 23:33:28 +0000699 srcs := library.sourceProvider.Srcs()
700 if len(srcs) == 0 {
701 return nil, errors.New("Source provider generated 0 sources")
702 }
Dan Albert06feee92021-03-19 15:06:02 -0700703 // Assume the first source from the source provider is the library entry point.
Matthew Maurera28404a2023-11-20 23:33:28 +0000704 return srcs[0], nil
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000705 } else {
Matthew Maurera28404a2023-11-20 23:33:28 +0000706 return library.baseCompiler.checkedCrateRootPath()
Dan Albert06feee92021-03-19 15:06:02 -0700707 }
708}
709
710func (library *libraryDecorator) rustdoc(ctx ModuleContext, flags Flags,
711 deps PathDeps) android.OptionalPath {
712 // rustdoc has builtin support for documenting config specific information
713 // regardless of the actual config it was given
714 // (https://doc.rust-lang.org/rustdoc/advanced-features.html#cfgdoc-documenting-platform-specific-or-feature-specific-information),
715 // so we generate the rustdoc for only the primary module so that we have a
716 // single set of docs to refer to.
717 if ctx.Module() != ctx.PrimaryModule() {
718 return android.OptionalPath{}
719 }
720
Matthew Maurera28404a2023-11-20 23:33:28 +0000721 return android.OptionalPathForPath(Rustdoc(ctx, crateRootPath(ctx, library),
Dan Albert06feee92021-03-19 15:06:02 -0700722 deps, flags))
723}
724
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700725func (library *libraryDecorator) getStem(ctx ModuleContext) string {
726 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
727 validateLibraryStem(ctx, stem, library.crateName())
728
729 return stem + String(library.baseCompiler.Properties.Suffix)
730}
731
Ivan Lozano2b081132020-09-08 12:46:52 -0400732func (library *libraryDecorator) install(ctx ModuleContext) {
733 // Only shared and dylib variants make sense to install.
734 if library.shared() || library.dylib() {
735 library.baseCompiler.install(ctx)
736 }
737}
738
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400739func (library *libraryDecorator) Disabled() bool {
740 return library.MutatedProperties.VariantIsDisabled
741}
742
743func (library *libraryDecorator) SetDisabled() {
744 library.MutatedProperties.VariantIsDisabled = true
745}
746
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700747var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
748
749func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
750 if crate_name == "" {
751 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
752 }
753
754 // crate_names are used for the library output file, and rustc expects these
755 // to be alphanumeric with underscores allowed.
756 if validCrateName.MatchString(crate_name) {
757 ctx.PropertyErrorf("crate_name",
758 "library crate_names must be alphanumeric with underscores allowed")
759 }
760
761 // Libraries are expected to begin with "lib" followed by the crate_name
762 if !strings.HasPrefix(filename, "lib"+crate_name) {
763 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
764 }
765}
766
Colin Cross8a49a3d2024-05-20 12:22:27 -0700767type libraryTransitionMutator struct{}
768
769func (libraryTransitionMutator) Split(ctx android.BaseModuleContext) []string {
770 m, ok := ctx.Module().(*Module)
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200771 if !ok || m.compiler == nil {
Colin Cross8a49a3d2024-05-20 12:22:27 -0700772 return []string{""}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200773 }
774 library, ok := m.compiler.(libraryInterface)
775 if !ok {
Colin Cross8a49a3d2024-05-20 12:22:27 -0700776 return []string{""}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200777 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400778
Ivan Lozano0a468a42024-05-13 21:03:34 -0400779 // Don't produce rlib/dylib/source variants for shared or static variants
780 if library.shared() || library.static() {
Colin Cross8a49a3d2024-05-20 12:22:27 -0700781 return []string{""}
Ivan Lozano0a468a42024-05-13 21:03:34 -0400782 }
783
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200784 var variants []string
785 // The source variant is used for SourceProvider modules. The other variants (i.e. rlib and dylib)
786 // depend on this variant. It must be the first variant to be declared.
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200787 if m.sourceProvider != nil {
Colin Cross8a49a3d2024-05-20 12:22:27 -0700788 variants = append(variants, sourceVariation)
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200789 }
790 if library.buildRlib() {
791 variants = append(variants, rlibVariation)
792 }
793 if library.buildDylib() {
794 variants = append(variants, dylibVariation)
795 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400796
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200797 if len(variants) == 0 {
Colin Cross8a49a3d2024-05-20 12:22:27 -0700798 return []string{""}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200799 }
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200800
Colin Cross8a49a3d2024-05-20 12:22:27 -0700801 return variants
802}
Ivan Lozano1921e802021-05-20 13:39:16 -0400803
Colin Cross8a49a3d2024-05-20 12:22:27 -0700804func (libraryTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
805 return ""
806}
807
808func (libraryTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
809 m, ok := ctx.Module().(*Module)
810 if !ok || m.compiler == nil {
811 return ""
812 }
813 library, ok := m.compiler.(libraryInterface)
814 if !ok {
815 return ""
816 }
817
818 if incomingVariation == "" {
819 if m.sourceProvider != nil {
820 return sourceVariation
821 }
822 if library.shared() {
823 return ""
824 }
825 if library.buildRlib() {
826 return rlibVariation
827 }
828 if library.buildDylib() {
829 return dylibVariation
Ivan Lozanoffee3342019-08-27 12:03:00 -0700830 }
831 }
Colin Cross8a49a3d2024-05-20 12:22:27 -0700832 return incomingVariation
833}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200834
Colin Cross8a49a3d2024-05-20 12:22:27 -0700835func (libraryTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) {
836 m, ok := ctx.Module().(*Module)
837 if !ok || m.compiler == nil {
838 return
839 }
840 library, ok := m.compiler.(libraryInterface)
841 if !ok {
842 return
843 }
844
845 switch variation {
846 case rlibVariation:
847 library.setRlib()
848 case dylibVariation:
849 library.setDylib()
850 if m.ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
851 // TODO(b/165791368)
852 // Disable dylib Vendor Ramdisk variations until we support these.
853 m.Disable()
854 }
855
856 case sourceVariation:
857 library.setSource()
858 // The source variant does not produce any library.
859 // Disable the compilation steps.
860 m.compiler.SetDisabled()
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400861 }
862
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200863 // If a source variant is created, add an inter-variant dependency
864 // between the other variants and the source variant.
Colin Cross8a49a3d2024-05-20 12:22:27 -0700865 if m.sourceProvider != nil && variation != sourceVariation {
866 ctx.AddVariationDependencies(
867 []blueprint.Variation{
868 {"rust_libraries", sourceVariation},
869 },
870 sourceDepTag, ctx.ModuleName())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200871 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700872}
Ivan Lozano2b081132020-09-08 12:46:52 -0400873
Colin Cross8a49a3d2024-05-20 12:22:27 -0700874type libstdTransitionMutator struct{}
Ivan Lozano2b081132020-09-08 12:46:52 -0400875
Colin Cross8a49a3d2024-05-20 12:22:27 -0700876func (libstdTransitionMutator) Split(ctx android.BaseModuleContext) []string {
877 if m, ok := ctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() {
878 // Only create a variant if a library is actually being built.
879 if library, ok := m.compiler.(libraryInterface); ok {
880 if library.rlib() && !library.sysroot() {
Ivan Lozano806efd32024-12-11 21:38:53 +0000881 return []string{"rlib-std", "dylib-std"}
Ivan Lozano2b081132020-09-08 12:46:52 -0400882 }
883 }
884 }
Colin Cross8a49a3d2024-05-20 12:22:27 -0700885 return []string{""}
886}
887
888func (libstdTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
889 return ""
890}
891
892func (libstdTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
893 if m, ok := ctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() {
894 if library, ok := m.compiler.(libraryInterface); ok {
895 if library.shared() {
896 return ""
897 }
898 if library.rlib() && !library.sysroot() {
899 if incomingVariation != "" {
900 return incomingVariation
901 }
902 return "rlib-std"
903 }
904 }
905 }
906 return ""
907}
908
909func (libstdTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) {
910 if variation == "rlib-std" {
911 rlib := ctx.Module().(*Module)
912 rlib.compiler.(libraryInterface).setRlibStd()
913 rlib.Properties.RustSubName += RlibStdlibSuffix
914 } else if variation == "dylib-std" {
915 dylib := ctx.Module().(*Module)
916 dylib.compiler.(libraryInterface).setDylibStd()
917 if dylib.ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
918 // TODO(b/165791368)
919 // Disable rlibs that link against dylib-std on vendor ramdisk variations until those dylib
920 // variants are properly supported.
921 dylib.Disable()
922 }
923 }
Ivan Lozano2b081132020-09-08 12:46:52 -0400924}