blob: 7e5d4c495a61683d3405675d9abd6a2a52da1160 [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
Ivan Lozanoffee3342019-08-27 12:03:00 -070023 "android/soong/android"
Colin Cross0de8a1e2020-09-18 14:15:30 -070024 "android/soong/cc"
Ivan Lozanoffee3342019-08-27 12:03:00 -070025)
26
Ivan Lozano2b081132020-09-08 12:46:52 -040027var (
Ivan Lozano4df02572023-06-15 14:21:09 -040028 RlibStdlibSuffix = ".rlib-std"
Ivan Lozano2b081132020-09-08 12:46:52 -040029)
30
Ivan Lozanoffee3342019-08-27 12:03:00 -070031func init() {
32 android.RegisterModuleType("rust_library", RustLibraryFactory)
33 android.RegisterModuleType("rust_library_dylib", RustLibraryDylibFactory)
34 android.RegisterModuleType("rust_library_rlib", RustLibraryRlibFactory)
35 android.RegisterModuleType("rust_library_host", RustLibraryHostFactory)
36 android.RegisterModuleType("rust_library_host_dylib", RustLibraryDylibHostFactory)
37 android.RegisterModuleType("rust_library_host_rlib", RustLibraryRlibHostFactory)
Matthew Maurer2ae05132020-06-23 14:28:53 -070038 android.RegisterModuleType("rust_ffi", RustFFIFactory)
39 android.RegisterModuleType("rust_ffi_shared", RustFFISharedFactory)
40 android.RegisterModuleType("rust_ffi_static", RustFFIStaticFactory)
41 android.RegisterModuleType("rust_ffi_host", RustFFIHostFactory)
42 android.RegisterModuleType("rust_ffi_host_shared", RustFFISharedHostFactory)
43 android.RegisterModuleType("rust_ffi_host_static", RustFFIStaticHostFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070044}
45
46type VariantLibraryProperties struct {
Matthew Maurerc761eec2020-06-25 00:47:46 -070047 Enabled *bool `android:"arch_variant"`
48 Srcs []string `android:"path,arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070049}
50
51type LibraryCompilerProperties struct {
Ivan Lozano52767be2019-10-18 14:49:46 -070052 Rlib VariantLibraryProperties `android:"arch_variant"`
53 Dylib VariantLibraryProperties `android:"arch_variant"`
54 Shared VariantLibraryProperties `android:"arch_variant"`
55 Static VariantLibraryProperties `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070056
Ivan Lozanof033ca62024-03-21 13:43:14 -040057 // TODO: Remove this when all instances of Include_dirs have been removed from rust_ffi modules.
58 // 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 -070059 Include_dirs []string `android:"path,arch_variant"`
Ivan Lozano2b081132020-09-08 12:46:52 -040060
Ivan Lozanof033ca62024-03-21 13:43:14 -040061 // path to include directories to export to cc_* modules, only relevant for static/shared variants.
62 Export_include_dirs []string `android:"path,arch_variant"`
63
Ivan Lozano2b081132020-09-08 12:46:52 -040064 // Whether this library is part of the Rust toolchain sysroot.
65 Sysroot *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070066}
67
68type LibraryMutatedProperties struct {
Ivan Lozanoffee3342019-08-27 12:03:00 -070069 // Build a dylib variant
70 BuildDylib bool `blueprint:"mutated"`
71 // Build an rlib variant
72 BuildRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070073 // Build a shared library variant
74 BuildShared bool `blueprint:"mutated"`
75 // Build a static library variant
76 BuildStatic bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070077
78 // This variant is a dylib
79 VariantIsDylib bool `blueprint:"mutated"`
80 // This variant is an rlib
81 VariantIsRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070082 // This variant is a shared library
83 VariantIsShared bool `blueprint:"mutated"`
84 // This variant is a static library
85 VariantIsStatic bool `blueprint:"mutated"`
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +020086 // This variant is a source provider
87 VariantIsSource bool `blueprint:"mutated"`
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040088
89 // This variant is disabled and should not be compiled
90 // (used for SourceProvider variants that produce only source)
91 VariantIsDisabled bool `blueprint:"mutated"`
Ivan Lozano2b081132020-09-08 12:46:52 -040092
93 // Whether this library variant should be link libstd via rlibs
94 VariantIsStaticStd bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070095}
96
97type libraryDecorator struct {
98 *baseCompiler
Matthew Maurerbb3add12020-06-25 09:34:12 -070099 *flagExporter
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200100 stripper Stripper
Ivan Lozanoffee3342019-08-27 12:03:00 -0700101
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400102 Properties LibraryCompilerProperties
103 MutatedProperties LibraryMutatedProperties
104 includeDirs android.Paths
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400105 sourceProvider SourceProvider
Ivan Lozano1921e802021-05-20 13:39:16 -0400106
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400107 // table-of-contents file for cdylib crates to optimize out relinking when possible
108 tocFile android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700109}
110
111type libraryInterface interface {
112 rlib() bool
113 dylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700114 static() bool
115 shared() bool
Ivan Lozano2b081132020-09-08 12:46:52 -0400116 sysroot() bool
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200117 source() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700118
119 // Returns true if the build options for the module have selected a particular build type
120 buildRlib() bool
121 buildDylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700122 buildShared() bool
123 buildStatic() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700124
125 // Sets a particular variant type
126 setRlib()
127 setDylib()
Ivan Lozano52767be2019-10-18 14:49:46 -0700128 setShared()
129 setStatic()
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200130 setSource()
Ivan Lozano52767be2019-10-18 14:49:46 -0700131
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400132 // libstd linkage functions
133 rlibStd() bool
Ivan Lozano2b081132020-09-08 12:46:52 -0400134 setRlibStd()
135 setDylibStd()
136
Ivan Lozano52767be2019-10-18 14:49:46 -0700137 // Build a specific library variant
Matthew Maurer2ae05132020-06-23 14:28:53 -0700138 BuildOnlyFFI()
139 BuildOnlyRust()
Ivan Lozano52767be2019-10-18 14:49:46 -0700140 BuildOnlyRlib()
141 BuildOnlyDylib()
142 BuildOnlyStatic()
143 BuildOnlyShared()
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400144
145 toc() android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700146}
147
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400148func (library *libraryDecorator) nativeCoverage() bool {
149 return true
150}
151
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400152func (library *libraryDecorator) toc() android.OptionalPath {
153 return library.tocFile
154}
155
Ivan Lozanoffee3342019-08-27 12:03:00 -0700156func (library *libraryDecorator) rlib() bool {
157 return library.MutatedProperties.VariantIsRlib
158}
159
Ivan Lozano2b081132020-09-08 12:46:52 -0400160func (library *libraryDecorator) sysroot() bool {
161 return Bool(library.Properties.Sysroot)
162}
163
Ivan Lozanoffee3342019-08-27 12:03:00 -0700164func (library *libraryDecorator) dylib() bool {
165 return library.MutatedProperties.VariantIsDylib
166}
167
Ivan Lozano52767be2019-10-18 14:49:46 -0700168func (library *libraryDecorator) shared() bool {
169 return library.MutatedProperties.VariantIsShared
170}
171
172func (library *libraryDecorator) static() bool {
173 return library.MutatedProperties.VariantIsStatic
174}
175
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200176func (library *libraryDecorator) source() bool {
177 return library.MutatedProperties.VariantIsSource
178}
179
Ivan Lozanoffee3342019-08-27 12:03:00 -0700180func (library *libraryDecorator) buildRlib() bool {
181 return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true)
182}
183
184func (library *libraryDecorator) buildDylib() bool {
185 return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true)
186}
187
Ivan Lozano52767be2019-10-18 14:49:46 -0700188func (library *libraryDecorator) buildShared() bool {
189 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
190}
191
192func (library *libraryDecorator) buildStatic() bool {
193 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
194}
195
Ivan Lozanoffee3342019-08-27 12:03:00 -0700196func (library *libraryDecorator) setRlib() {
197 library.MutatedProperties.VariantIsRlib = true
198 library.MutatedProperties.VariantIsDylib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700199 library.MutatedProperties.VariantIsStatic = false
200 library.MutatedProperties.VariantIsShared = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700201}
202
203func (library *libraryDecorator) setDylib() {
204 library.MutatedProperties.VariantIsRlib = false
205 library.MutatedProperties.VariantIsDylib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700206 library.MutatedProperties.VariantIsStatic = false
207 library.MutatedProperties.VariantIsShared = false
208}
209
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400210func (library *libraryDecorator) rlibStd() bool {
211 return library.MutatedProperties.VariantIsStaticStd
212}
213
Ivan Lozano2b081132020-09-08 12:46:52 -0400214func (library *libraryDecorator) setRlibStd() {
215 library.MutatedProperties.VariantIsStaticStd = true
216}
217
218func (library *libraryDecorator) setDylibStd() {
219 library.MutatedProperties.VariantIsStaticStd = false
220}
221
Ivan Lozano52767be2019-10-18 14:49:46 -0700222func (library *libraryDecorator) setShared() {
223 library.MutatedProperties.VariantIsStatic = false
224 library.MutatedProperties.VariantIsShared = true
225 library.MutatedProperties.VariantIsRlib = false
226 library.MutatedProperties.VariantIsDylib = false
227}
228
229func (library *libraryDecorator) setStatic() {
230 library.MutatedProperties.VariantIsStatic = true
231 library.MutatedProperties.VariantIsShared = false
232 library.MutatedProperties.VariantIsRlib = false
233 library.MutatedProperties.VariantIsDylib = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700234}
235
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200236func (library *libraryDecorator) setSource() {
237 library.MutatedProperties.VariantIsSource = true
238}
239
Liz Kammer356f7d42021-01-26 09:18:53 -0500240func (library *libraryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400241 if library.preferRlib() {
Ivan Lozanoea086132020-12-08 14:43:00 -0500242 return rlibAutoDep
243 } else if library.rlib() || library.static() {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700244 return rlibAutoDep
245 } else if library.dylib() || library.shared() {
246 return dylibAutoDep
247 } else {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200248 panic(fmt.Errorf("autoDep called on library %q that has no enabled variants.", ctx.ModuleName()))
Matthew Maurer0f003b12020-06-29 14:34:06 -0700249 }
250}
251
Ivan Lozanoea086132020-12-08 14:43:00 -0500252func (library *libraryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400253 if library.static() || library.MutatedProperties.VariantIsStaticStd {
Ivan Lozanoea086132020-12-08 14:43:00 -0500254 return RlibLinkage
255 } else if library.baseCompiler.preferRlib() {
256 return RlibLinkage
257 }
258 return DefaultLinkage
259}
260
Ivan Lozanoffee3342019-08-27 12:03:00 -0700261var _ compiler = (*libraryDecorator)(nil)
Ivan Lozano52767be2019-10-18 14:49:46 -0700262var _ libraryInterface = (*libraryDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700263var _ exportedFlagsProducer = (*libraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700264
Martin Geisler67ec0542022-11-18 12:08:55 +0100265// rust_library produces all Rust variants (rust_library_dylib and
266// rust_library_rlib).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700267func RustLibraryFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700268 module, library := NewRustLibrary(android.HostAndDeviceSupported)
269 library.BuildOnlyRust()
270 return module.Init()
271}
272
Martin Geisler67ec0542022-11-18 12:08:55 +0100273// rust_ffi produces all FFI variants (rust_ffi_shared and
274// rust_ffi_static).
Matthew Maurer2ae05132020-06-23 14:28:53 -0700275func RustFFIFactory() android.Module {
276 module, library := NewRustLibrary(android.HostAndDeviceSupported)
277 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700278 return module.Init()
279}
280
Martin Geisler67ec0542022-11-18 12:08:55 +0100281// rust_library_dylib produces a Rust dylib (Rust crate type "dylib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700282func RustLibraryDylibFactory() android.Module {
283 module, library := NewRustLibrary(android.HostAndDeviceSupported)
284 library.BuildOnlyDylib()
285 return module.Init()
286}
287
Martin Geisler67ec0542022-11-18 12:08:55 +0100288// rust_library_rlib produces an rlib (Rust crate type "rlib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700289func RustLibraryRlibFactory() android.Module {
290 module, library := NewRustLibrary(android.HostAndDeviceSupported)
291 library.BuildOnlyRlib()
292 return module.Init()
293}
294
Martin Geisler67ec0542022-11-18 12:08:55 +0100295// rust_ffi_shared produces a shared library (Rust crate type
296// "cdylib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700297func RustFFISharedFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700298 module, library := NewRustLibrary(android.HostAndDeviceSupported)
299 library.BuildOnlyShared()
300 return module.Init()
301}
302
Martin Geisler67ec0542022-11-18 12:08:55 +0100303// rust_ffi_static produces a static library (Rust crate type
304// "staticlib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700305func RustFFIStaticFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700306 module, library := NewRustLibrary(android.HostAndDeviceSupported)
307 library.BuildOnlyStatic()
308 return module.Init()
309}
310
Martin Geisler67ec0542022-11-18 12:08:55 +0100311// rust_library_host produces all Rust variants for the host
312// (rust_library_dylib_host and rust_library_rlib_host).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700313func RustLibraryHostFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700314 module, library := NewRustLibrary(android.HostSupported)
315 library.BuildOnlyRust()
316 return module.Init()
317}
318
Martin Geisler67ec0542022-11-18 12:08:55 +0100319// rust_ffi_host produces all FFI variants for the host
320// (rust_ffi_static_host and rust_ffi_shared_host).
Matthew Maurer2ae05132020-06-23 14:28:53 -0700321func RustFFIHostFactory() android.Module {
322 module, library := NewRustLibrary(android.HostSupported)
323 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700324 return module.Init()
325}
326
Martin Geisler67ec0542022-11-18 12:08:55 +0100327// rust_library_dylib_host produces a dylib for the host (Rust crate
328// type "dylib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700329func RustLibraryDylibHostFactory() android.Module {
330 module, library := NewRustLibrary(android.HostSupported)
331 library.BuildOnlyDylib()
332 return module.Init()
333}
334
Martin Geisler67ec0542022-11-18 12:08:55 +0100335// rust_library_rlib_host produces an rlib for the host (Rust crate
336// type "rlib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700337func RustLibraryRlibHostFactory() android.Module {
338 module, library := NewRustLibrary(android.HostSupported)
339 library.BuildOnlyRlib()
340 return module.Init()
341}
342
Martin Geisler67ec0542022-11-18 12:08:55 +0100343// rust_ffi_static_host produces a static library for the host (Rust
344// crate type "staticlib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700345func RustFFIStaticHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700346 module, library := NewRustLibrary(android.HostSupported)
347 library.BuildOnlyStatic()
348 return module.Init()
349}
350
Martin Geisler67ec0542022-11-18 12:08:55 +0100351// rust_ffi_shared_host produces an shared library for the host (Rust
352// crate type "cdylib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700353func RustFFISharedHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700354 module, library := NewRustLibrary(android.HostSupported)
355 library.BuildOnlyShared()
356 return module.Init()
357}
358
Matthew Maurer2ae05132020-06-23 14:28:53 -0700359func (library *libraryDecorator) BuildOnlyFFI() {
360 library.MutatedProperties.BuildDylib = false
361 library.MutatedProperties.BuildRlib = false
362 library.MutatedProperties.BuildShared = true
363 library.MutatedProperties.BuildStatic = true
364}
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
Stephen Crane0dbfc562021-07-07 19:05:02 -0700458 if library.dylib() {
459 // We need to add a dependency on std in order to link crates as dylibs.
460 // The hack to add this dependency is guarded by the following cfg so
461 // that we don't force a dependency when it isn't needed.
462 library.baseCompiler.Properties.Cfgs = append(library.baseCompiler.Properties.Cfgs, "android_dylib")
463 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400464
Ivan Lozano28ed8f42024-05-06 21:46:39 -0400465 flags.RustFlags = append(flags.RustFlags, cfgsToFlags(library.baseCompiler.Properties.Cfgs)...)
466 flags.RustdocFlags = append(flags.RustdocFlags, cfgsToFlags(library.baseCompiler.Properties.Cfgs)...)
467
468 return flags
469}
470
471// Common flags applied to all libraries irrespective of properties or variant should be included here
472func CommonLibraryCompilerFlags(ctx android.ModuleContext, flags Flags) Flags {
473 flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
Ivan Lozano67eada32021-09-23 11:50:33 -0400474
475 return flags
476}
477
478func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800479 flags = library.baseCompiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400480
Ivan Lozano28ed8f42024-05-06 21:46:39 -0400481 flags = CommonLibraryCompilerFlags(ctx, flags)
482
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800483 if library.shared() || library.static() {
484 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
Ivan Lozanof033ca62024-03-21 13:43:14 -0400485 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Export_include_dirs)...)
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800486 }
Ivan 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 Lozano8d10fc32021-11-05 16:36:47 -0400512 // Calculate output filename
513 if library.rlib() {
514 fileName = library.getStem(ctx) + ctx.toolchain().RlibSuffix()
515 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700516 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400517 } else if library.dylib() {
518 fileName = library.getStem(ctx) + ctx.toolchain().DylibSuffix()
519 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700520 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400521 } else if library.static() {
522 fileName = library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
523 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700524 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400525 } else if library.shared() {
526 fileName = library.sharedLibFilename(ctx)
527 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700528 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400529 }
530
531 if !library.rlib() && !library.static() && library.stripper.NeedsStrip(ctx) {
532 strippedOutputFile := outputFile
533 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
534 library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
535
536 library.baseCompiler.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
537 }
538 library.baseCompiler.unstrippedOutputFile = outputFile
539
Ivan Lozanoffee3342019-08-27 12:03:00 -0700540 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500541 flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...)
Colin Cross004bd3f2023-10-02 11:39:17 -0700542 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700543
Matthew Maurer46c46cc2020-01-13 16:34:34 -0800544 if library.dylib() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700545 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
546 // https://github.com/rust-lang/rust/issues/19680
547 // https://github.com/rust-lang/rust/issues/34909
548 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
549 }
550
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400551 // Call the appropriate builder for this library type
Ivan Lozanoffee3342019-08-27 12:03:00 -0700552 if library.rlib() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000553 ret.kytheFile = TransformSrctoRlib(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700554 } else if library.dylib() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000555 ret.kytheFile = TransformSrctoDylib(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700556 } else if library.static() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000557 ret.kytheFile = TransformSrctoStatic(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700558 } else if library.shared() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000559 ret.kytheFile = TransformSrctoShared(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700560 }
561
Ivan Lozano52767be2019-10-18 14:49:46 -0700562 if library.rlib() || library.dylib() {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700563 library.flagExporter.exportLinkDirs(deps.linkDirs...)
Colin Cross0de8a1e2020-09-18 14:15:30 -0700564 library.flagExporter.exportLinkObjects(deps.linkObjects...)
Ivan Lozano52767be2019-10-18 14:49:46 -0700565 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700566
Colin Cross0de8a1e2020-09-18 14:15:30 -0700567 if library.static() || library.shared() {
Colin Cross40213022023-12-13 15:19:49 -0800568 android.SetProvider(ctx, cc.FlagExporterInfoProvider, cc.FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700569 IncludeDirs: library.includeDirs,
570 })
571 }
572
573 if library.shared() {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400574 // Optimize out relinking against shared libraries whose interface hasn't changed by
575 // depending on a table of contents file instead of the library itself.
576 tocFile := outputFile.ReplaceExtension(ctx, flags.Toolchain.SharedLibSuffix()[1:]+".toc")
577 library.tocFile = android.OptionalPathForPath(tocFile)
578 cc.TransformSharedObjectToToc(ctx, outputFile, tocFile)
579
Colin Cross40213022023-12-13 15:19:49 -0800580 android.SetProvider(ctx, cc.SharedLibraryInfoProvider, cc.SharedLibraryInfo{
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400581 TableOfContents: android.OptionalPathForPath(tocFile),
582 SharedLibrary: outputFile,
583 Target: ctx.Target(),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700584 })
585 }
586
587 if library.static() {
Colin Crossc85750b2022-04-21 12:50:51 -0700588 depSet := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).Direct(outputFile).Build()
Colin Cross40213022023-12-13 15:19:49 -0800589 android.SetProvider(ctx, cc.StaticLibraryInfoProvider, cc.StaticLibraryInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700590 StaticLibrary: outputFile,
591
592 TransitiveStaticLibrariesForOrdering: depSet,
593 })
594 }
595
596 library.flagExporter.setProvider(ctx)
597
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400598 return ret
Ivan Lozanoffee3342019-08-27 12:03:00 -0700599}
600
Matthew Maurera28404a2023-11-20 23:33:28 +0000601func (library *libraryDecorator) checkedCrateRootPath() (android.Path, error) {
Dan Albert06feee92021-03-19 15:06:02 -0700602 if library.sourceProvider != nil {
Matthew Maurera28404a2023-11-20 23:33:28 +0000603 srcs := library.sourceProvider.Srcs()
604 if len(srcs) == 0 {
605 return nil, errors.New("Source provider generated 0 sources")
606 }
Dan Albert06feee92021-03-19 15:06:02 -0700607 // Assume the first source from the source provider is the library entry point.
Matthew Maurera28404a2023-11-20 23:33:28 +0000608 return srcs[0], nil
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000609 } else {
Matthew Maurera28404a2023-11-20 23:33:28 +0000610 return library.baseCompiler.checkedCrateRootPath()
Dan Albert06feee92021-03-19 15:06:02 -0700611 }
612}
613
614func (library *libraryDecorator) rustdoc(ctx ModuleContext, flags Flags,
615 deps PathDeps) android.OptionalPath {
616 // rustdoc has builtin support for documenting config specific information
617 // regardless of the actual config it was given
618 // (https://doc.rust-lang.org/rustdoc/advanced-features.html#cfgdoc-documenting-platform-specific-or-feature-specific-information),
619 // so we generate the rustdoc for only the primary module so that we have a
620 // single set of docs to refer to.
621 if ctx.Module() != ctx.PrimaryModule() {
622 return android.OptionalPath{}
623 }
624
Matthew Maurera28404a2023-11-20 23:33:28 +0000625 return android.OptionalPathForPath(Rustdoc(ctx, crateRootPath(ctx, library),
Dan Albert06feee92021-03-19 15:06:02 -0700626 deps, flags))
627}
628
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700629func (library *libraryDecorator) getStem(ctx ModuleContext) string {
630 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
631 validateLibraryStem(ctx, stem, library.crateName())
632
633 return stem + String(library.baseCompiler.Properties.Suffix)
634}
635
Ivan Lozano2b081132020-09-08 12:46:52 -0400636func (library *libraryDecorator) install(ctx ModuleContext) {
637 // Only shared and dylib variants make sense to install.
638 if library.shared() || library.dylib() {
639 library.baseCompiler.install(ctx)
640 }
641}
642
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400643func (library *libraryDecorator) Disabled() bool {
644 return library.MutatedProperties.VariantIsDisabled
645}
646
647func (library *libraryDecorator) SetDisabled() {
648 library.MutatedProperties.VariantIsDisabled = true
649}
650
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700651var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
652
653func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
654 if crate_name == "" {
655 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
656 }
657
658 // crate_names are used for the library output file, and rustc expects these
659 // to be alphanumeric with underscores allowed.
660 if validCrateName.MatchString(crate_name) {
661 ctx.PropertyErrorf("crate_name",
662 "library crate_names must be alphanumeric with underscores allowed")
663 }
664
665 // Libraries are expected to begin with "lib" followed by the crate_name
666 if !strings.HasPrefix(filename, "lib"+crate_name) {
667 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
668 }
669}
670
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200671// LibraryMutator mutates the libraries into variants according to the
672// build{Rlib,Dylib} attributes.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700673func LibraryMutator(mctx android.BottomUpMutatorContext) {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200674 // Only mutate on Rust libraries.
675 m, ok := mctx.Module().(*Module)
676 if !ok || m.compiler == nil {
677 return
678 }
679 library, ok := m.compiler.(libraryInterface)
680 if !ok {
681 return
682 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400683
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200684 var variants []string
685 // The source variant is used for SourceProvider modules. The other variants (i.e. rlib and dylib)
686 // depend on this variant. It must be the first variant to be declared.
687 sourceVariant := false
688 if m.sourceProvider != nil {
689 variants = append(variants, "source")
690 sourceVariant = true
691 }
692 if library.buildRlib() {
693 variants = append(variants, rlibVariation)
694 }
695 if library.buildDylib() {
696 variants = append(variants, dylibVariation)
697 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400698
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200699 if len(variants) == 0 {
700 return
701 }
702 modules := mctx.CreateLocalVariations(variants...)
703
704 // The order of the variations (modules) matches the variant names provided. Iterate
705 // through the new variation modules and set their mutated properties.
706 for i, v := range modules {
707 switch variants[i] {
708 case rlibVariation:
709 v.(*Module).compiler.(libraryInterface).setRlib()
710 case dylibVariation:
711 v.(*Module).compiler.(libraryInterface).setDylib()
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400712 if v.(*Module).ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
Ivan Lozano6a884432020-12-02 09:15:16 -0500713 // TODO(b/165791368)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400714 // Disable dylib Vendor Ramdisk variations until we support these.
Ivan Lozano6a884432020-12-02 09:15:16 -0500715 v.(*Module).Disable()
716 }
Ivan Lozano1921e802021-05-20 13:39:16 -0400717
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200718 case "source":
719 v.(*Module).compiler.(libraryInterface).setSource()
720 // The source variant does not produce any library.
721 // Disable the compilation steps.
722 v.(*Module).compiler.SetDisabled()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700723 }
724 }
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200725
726 // If a source variant is created, add an inter-variant dependency
727 // between the other variants and the source variant.
728 if sourceVariant {
729 sv := modules[0]
730 for _, v := range modules[1:] {
731 if !v.Enabled() {
732 continue
733 }
734 mctx.AddInterVariantDependency(sourceDepTag, v, sv)
735 }
736 // Alias the source variation so it can be named directly in "srcs" properties.
737 mctx.AliasVariation("source")
738 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700739}
Ivan Lozano2b081132020-09-08 12:46:52 -0400740
741func LibstdMutator(mctx android.BottomUpMutatorContext) {
742 if m, ok := mctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() {
743 switch library := m.compiler.(type) {
744 case libraryInterface:
745 // Only create a variant if a library is actually being built.
746 if library.rlib() && !library.sysroot() {
747 variants := []string{"rlib-std", "dylib-std"}
748 modules := mctx.CreateLocalVariations(variants...)
749
750 rlib := modules[0].(*Module)
751 dylib := modules[1].(*Module)
752 rlib.compiler.(libraryInterface).setRlibStd()
753 dylib.compiler.(libraryInterface).setDylibStd()
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400754 if dylib.ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
Ivan Lozano6a884432020-12-02 09:15:16 -0500755 // TODO(b/165791368)
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400756 // Disable rlibs that link against dylib-std on vendor ramdisk variations until those dylib
Ivan Lozano6a884432020-12-02 09:15:16 -0500757 // variants are properly supported.
758 dylib.Disable()
759 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400760 rlib.Properties.RustSubName += RlibStdlibSuffix
Ivan Lozano2b081132020-09-08 12:46:52 -0400761 }
762 }
763 }
764}