blob: ca5ad1443d4ec2c4b143e6ce4a21a42ec8a2afb8 [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 (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +020018 "fmt"
Ivan Lozanoad8b18b2019-10-31 19:38:29 -070019 "regexp"
20 "strings"
21
Ivan Lozanoffee3342019-08-27 12:03:00 -070022 "android/soong/android"
Colin Cross0de8a1e2020-09-18 14:15:30 -070023 "android/soong/cc"
Kiyoung Kim48f37782021-07-07 12:42:39 +090024 "android/soong/snapshot"
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 Lozano52767be2019-10-18 14:49:46 -070057 // path to include directories to pass to cc_* modules, only relevant for static/shared variants.
58 Include_dirs []string `android:"path,arch_variant"`
Ivan Lozano2b081132020-09-08 12:46:52 -040059
60 // Whether this library is part of the Rust toolchain sysroot.
61 Sysroot *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070062}
63
64type LibraryMutatedProperties struct {
Ivan Lozanoffee3342019-08-27 12:03:00 -070065 // Build a dylib variant
66 BuildDylib bool `blueprint:"mutated"`
67 // Build an rlib variant
68 BuildRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070069 // Build a shared library variant
70 BuildShared bool `blueprint:"mutated"`
71 // Build a static library variant
72 BuildStatic bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070073
74 // This variant is a dylib
75 VariantIsDylib bool `blueprint:"mutated"`
76 // This variant is an rlib
77 VariantIsRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070078 // This variant is a shared library
79 VariantIsShared bool `blueprint:"mutated"`
80 // This variant is a static library
81 VariantIsStatic bool `blueprint:"mutated"`
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +020082 // This variant is a source provider
83 VariantIsSource bool `blueprint:"mutated"`
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040084
85 // This variant is disabled and should not be compiled
86 // (used for SourceProvider variants that produce only source)
87 VariantIsDisabled bool `blueprint:"mutated"`
Ivan Lozano2b081132020-09-08 12:46:52 -040088
89 // Whether this library variant should be link libstd via rlibs
90 VariantIsStaticStd bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070091}
92
93type libraryDecorator struct {
94 *baseCompiler
Matthew Maurerbb3add12020-06-25 09:34:12 -070095 *flagExporter
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +020096 stripper Stripper
Ivan Lozanoffee3342019-08-27 12:03:00 -070097
Ivan Lozano8a23fa42020-06-16 10:26:57 -040098 Properties LibraryCompilerProperties
99 MutatedProperties LibraryMutatedProperties
100 includeDirs android.Paths
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400101 sourceProvider SourceProvider
Ivan Lozano1921e802021-05-20 13:39:16 -0400102
103 collectedSnapshotHeaders android.Paths
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400104
105 // table-of-contents file for cdylib crates to optimize out relinking when possible
106 tocFile android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700107}
108
109type libraryInterface interface {
110 rlib() bool
111 dylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700112 static() bool
113 shared() bool
Ivan Lozano2b081132020-09-08 12:46:52 -0400114 sysroot() bool
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200115 source() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700116
117 // Returns true if the build options for the module have selected a particular build type
118 buildRlib() bool
119 buildDylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700120 buildShared() bool
121 buildStatic() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700122
123 // Sets a particular variant type
124 setRlib()
125 setDylib()
Ivan Lozano52767be2019-10-18 14:49:46 -0700126 setShared()
127 setStatic()
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200128 setSource()
Ivan Lozano52767be2019-10-18 14:49:46 -0700129
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400130 // libstd linkage functions
131 rlibStd() bool
Ivan Lozano2b081132020-09-08 12:46:52 -0400132 setRlibStd()
133 setDylibStd()
134
Ivan Lozano52767be2019-10-18 14:49:46 -0700135 // Build a specific library variant
Matthew Maurer2ae05132020-06-23 14:28:53 -0700136 BuildOnlyFFI()
137 BuildOnlyRust()
Ivan Lozano52767be2019-10-18 14:49:46 -0700138 BuildOnlyRlib()
139 BuildOnlyDylib()
140 BuildOnlyStatic()
141 BuildOnlyShared()
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400142
143 toc() android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700144}
145
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400146func (library *libraryDecorator) nativeCoverage() bool {
147 return true
148}
149
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400150func (library *libraryDecorator) toc() android.OptionalPath {
151 return library.tocFile
152}
153
Ivan Lozanoffee3342019-08-27 12:03:00 -0700154func (library *libraryDecorator) rlib() bool {
155 return library.MutatedProperties.VariantIsRlib
156}
157
Ivan Lozano2b081132020-09-08 12:46:52 -0400158func (library *libraryDecorator) sysroot() bool {
159 return Bool(library.Properties.Sysroot)
160}
161
Ivan Lozanoffee3342019-08-27 12:03:00 -0700162func (library *libraryDecorator) dylib() bool {
163 return library.MutatedProperties.VariantIsDylib
164}
165
Ivan Lozano52767be2019-10-18 14:49:46 -0700166func (library *libraryDecorator) shared() bool {
167 return library.MutatedProperties.VariantIsShared
168}
169
170func (library *libraryDecorator) static() bool {
171 return library.MutatedProperties.VariantIsStatic
172}
173
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200174func (library *libraryDecorator) source() bool {
175 return library.MutatedProperties.VariantIsSource
176}
177
Ivan Lozanoffee3342019-08-27 12:03:00 -0700178func (library *libraryDecorator) buildRlib() bool {
179 return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true)
180}
181
182func (library *libraryDecorator) buildDylib() bool {
183 return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true)
184}
185
Ivan Lozano52767be2019-10-18 14:49:46 -0700186func (library *libraryDecorator) buildShared() bool {
187 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
188}
189
190func (library *libraryDecorator) buildStatic() bool {
191 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
192}
193
Ivan Lozanoffee3342019-08-27 12:03:00 -0700194func (library *libraryDecorator) setRlib() {
195 library.MutatedProperties.VariantIsRlib = true
196 library.MutatedProperties.VariantIsDylib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700197 library.MutatedProperties.VariantIsStatic = false
198 library.MutatedProperties.VariantIsShared = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700199}
200
201func (library *libraryDecorator) setDylib() {
202 library.MutatedProperties.VariantIsRlib = false
203 library.MutatedProperties.VariantIsDylib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700204 library.MutatedProperties.VariantIsStatic = false
205 library.MutatedProperties.VariantIsShared = false
206}
207
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400208func (library *libraryDecorator) rlibStd() bool {
209 return library.MutatedProperties.VariantIsStaticStd
210}
211
Ivan Lozano2b081132020-09-08 12:46:52 -0400212func (library *libraryDecorator) setRlibStd() {
213 library.MutatedProperties.VariantIsStaticStd = true
214}
215
216func (library *libraryDecorator) setDylibStd() {
217 library.MutatedProperties.VariantIsStaticStd = false
218}
219
Ivan Lozano52767be2019-10-18 14:49:46 -0700220func (library *libraryDecorator) setShared() {
221 library.MutatedProperties.VariantIsStatic = false
222 library.MutatedProperties.VariantIsShared = true
223 library.MutatedProperties.VariantIsRlib = false
224 library.MutatedProperties.VariantIsDylib = false
225}
226
227func (library *libraryDecorator) setStatic() {
228 library.MutatedProperties.VariantIsStatic = true
229 library.MutatedProperties.VariantIsShared = false
230 library.MutatedProperties.VariantIsRlib = false
231 library.MutatedProperties.VariantIsDylib = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700232}
233
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200234func (library *libraryDecorator) setSource() {
235 library.MutatedProperties.VariantIsSource = true
236}
237
Liz Kammer356f7d42021-01-26 09:18:53 -0500238func (library *libraryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Ivan Lozano1921e802021-05-20 13:39:16 -0400239 if ctx.Module().(*Module).InVendor() {
240 // Vendor modules should statically link libstd.
241 return rlibAutoDep
242 } else if library.preferRlib() {
Ivan Lozanoea086132020-12-08 14:43:00 -0500243 return rlibAutoDep
244 } else if library.rlib() || library.static() {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700245 return rlibAutoDep
246 } else if library.dylib() || library.shared() {
247 return dylibAutoDep
248 } else {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200249 panic(fmt.Errorf("autoDep called on library %q that has no enabled variants.", ctx.ModuleName()))
Matthew Maurer0f003b12020-06-29 14:34:06 -0700250 }
251}
252
Ivan Lozanoea086132020-12-08 14:43:00 -0500253func (library *libraryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozano1921e802021-05-20 13:39:16 -0400254 if ctx.RustModule().InVendor() {
255 // Vendor modules should statically link libstd.
256 return RlibLinkage
257 } else if library.static() || library.MutatedProperties.VariantIsStaticStd {
Ivan Lozanoea086132020-12-08 14:43:00 -0500258 return RlibLinkage
259 } else if library.baseCompiler.preferRlib() {
260 return RlibLinkage
261 }
262 return DefaultLinkage
263}
264
Ivan Lozanoffee3342019-08-27 12:03:00 -0700265var _ compiler = (*libraryDecorator)(nil)
Ivan Lozano52767be2019-10-18 14:49:46 -0700266var _ libraryInterface = (*libraryDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700267var _ exportedFlagsProducer = (*libraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700268
Martin Geisler67ec0542022-11-18 12:08:55 +0100269// rust_library produces all Rust variants (rust_library_dylib and
270// rust_library_rlib).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700271func RustLibraryFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700272 module, library := NewRustLibrary(android.HostAndDeviceSupported)
273 library.BuildOnlyRust()
274 return module.Init()
275}
276
Martin Geisler67ec0542022-11-18 12:08:55 +0100277// rust_ffi produces all FFI variants (rust_ffi_shared and
278// rust_ffi_static).
Matthew Maurer2ae05132020-06-23 14:28:53 -0700279func RustFFIFactory() android.Module {
280 module, library := NewRustLibrary(android.HostAndDeviceSupported)
281 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700282 return module.Init()
283}
284
Martin Geisler67ec0542022-11-18 12:08:55 +0100285// rust_library_dylib produces a Rust dylib (Rust crate type "dylib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700286func RustLibraryDylibFactory() android.Module {
287 module, library := NewRustLibrary(android.HostAndDeviceSupported)
288 library.BuildOnlyDylib()
289 return module.Init()
290}
291
Martin Geisler67ec0542022-11-18 12:08:55 +0100292// rust_library_rlib produces an rlib (Rust crate type "rlib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700293func RustLibraryRlibFactory() android.Module {
294 module, library := NewRustLibrary(android.HostAndDeviceSupported)
295 library.BuildOnlyRlib()
296 return module.Init()
297}
298
Martin Geisler67ec0542022-11-18 12:08:55 +0100299// rust_ffi_shared produces a shared library (Rust crate type
300// "cdylib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700301func RustFFISharedFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700302 module, library := NewRustLibrary(android.HostAndDeviceSupported)
303 library.BuildOnlyShared()
304 return module.Init()
305}
306
Martin Geisler67ec0542022-11-18 12:08:55 +0100307// rust_ffi_static produces a static library (Rust crate type
308// "staticlib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700309func RustFFIStaticFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700310 module, library := NewRustLibrary(android.HostAndDeviceSupported)
311 library.BuildOnlyStatic()
312 return module.Init()
313}
314
Martin Geisler67ec0542022-11-18 12:08:55 +0100315// rust_library_host produces all Rust variants for the host
316// (rust_library_dylib_host and rust_library_rlib_host).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700317func RustLibraryHostFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700318 module, library := NewRustLibrary(android.HostSupported)
319 library.BuildOnlyRust()
320 return module.Init()
321}
322
Martin Geisler67ec0542022-11-18 12:08:55 +0100323// rust_ffi_host produces all FFI variants for the host
324// (rust_ffi_static_host and rust_ffi_shared_host).
Matthew Maurer2ae05132020-06-23 14:28:53 -0700325func RustFFIHostFactory() android.Module {
326 module, library := NewRustLibrary(android.HostSupported)
327 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700328 return module.Init()
329}
330
Martin Geisler67ec0542022-11-18 12:08:55 +0100331// rust_library_dylib_host produces a dylib for the host (Rust crate
332// type "dylib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700333func RustLibraryDylibHostFactory() android.Module {
334 module, library := NewRustLibrary(android.HostSupported)
335 library.BuildOnlyDylib()
336 return module.Init()
337}
338
Martin Geisler67ec0542022-11-18 12:08:55 +0100339// rust_library_rlib_host produces an rlib for the host (Rust crate
340// type "rlib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700341func RustLibraryRlibHostFactory() android.Module {
342 module, library := NewRustLibrary(android.HostSupported)
343 library.BuildOnlyRlib()
344 return module.Init()
345}
346
Martin Geisler67ec0542022-11-18 12:08:55 +0100347// rust_ffi_static_host produces a static library for the host (Rust
348// crate type "staticlib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700349func RustFFIStaticHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700350 module, library := NewRustLibrary(android.HostSupported)
351 library.BuildOnlyStatic()
352 return module.Init()
353}
354
Martin Geisler67ec0542022-11-18 12:08:55 +0100355// rust_ffi_shared_host produces an shared library for the host (Rust
356// crate type "cdylib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700357func RustFFISharedHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700358 module, library := NewRustLibrary(android.HostSupported)
359 library.BuildOnlyShared()
360 return module.Init()
361}
362
Matthew Maurer2ae05132020-06-23 14:28:53 -0700363func (library *libraryDecorator) BuildOnlyFFI() {
364 library.MutatedProperties.BuildDylib = false
365 library.MutatedProperties.BuildRlib = false
366 library.MutatedProperties.BuildShared = true
367 library.MutatedProperties.BuildStatic = true
368}
369
370func (library *libraryDecorator) BuildOnlyRust() {
371 library.MutatedProperties.BuildDylib = true
372 library.MutatedProperties.BuildRlib = true
373 library.MutatedProperties.BuildShared = false
374 library.MutatedProperties.BuildStatic = false
375}
376
Ivan Lozanoffee3342019-08-27 12:03:00 -0700377func (library *libraryDecorator) BuildOnlyDylib() {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700378 library.MutatedProperties.BuildDylib = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700379 library.MutatedProperties.BuildRlib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700380 library.MutatedProperties.BuildShared = false
381 library.MutatedProperties.BuildStatic = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700382}
383
384func (library *libraryDecorator) BuildOnlyRlib() {
385 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700386 library.MutatedProperties.BuildRlib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700387 library.MutatedProperties.BuildShared = false
388 library.MutatedProperties.BuildStatic = false
389}
390
391func (library *libraryDecorator) BuildOnlyStatic() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700392 library.MutatedProperties.BuildRlib = false
393 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700394 library.MutatedProperties.BuildShared = false
395 library.MutatedProperties.BuildStatic = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700396}
397
398func (library *libraryDecorator) BuildOnlyShared() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700399 library.MutatedProperties.BuildRlib = false
400 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700401 library.MutatedProperties.BuildStatic = false
402 library.MutatedProperties.BuildShared = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700403}
404
405func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400406 module := newModule(hod, android.MultilibBoth)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700407
408 library := &libraryDecorator{
409 MutatedProperties: LibraryMutatedProperties{
Matthew Maurer2ae05132020-06-23 14:28:53 -0700410 BuildDylib: false,
411 BuildRlib: false,
412 BuildShared: false,
413 BuildStatic: false,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700414 },
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800415 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Matthew Maurerbb3add12020-06-25 09:34:12 -0700416 flagExporter: NewFlagExporter(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700417 }
418
419 module.compiler = library
420
421 return module, library
422}
423
424func (library *libraryDecorator) compilerProps() []interface{} {
425 return append(library.baseCompiler.compilerProps(),
426 &library.Properties,
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200427 &library.MutatedProperties,
428 &library.stripper.StripProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700429}
430
Ivan Lozanof1c84332019-09-20 11:00:37 -0700431func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
432 deps = library.baseCompiler.compilerDeps(ctx, deps)
433
Colin Crosse32f0932022-01-23 20:48:36 -0800434 if library.dylib() || library.shared() {
435 if ctx.toolchain().Bionic() {
436 deps = bionicDeps(ctx, deps, false)
437 deps.CrtBegin = []string{"crtbegin_so"}
438 deps.CrtEnd = []string{"crtend_so"}
439 } else if ctx.Os() == android.LinuxMusl {
440 deps = muslDeps(ctx, deps, false)
441 deps.CrtBegin = []string{"libc_musl_crtbegin_so"}
442 deps.CrtEnd = []string{"libc_musl_crtend_so"}
443 }
Ivan Lozanof1c84332019-09-20 11:00:37 -0700444 }
445
446 return deps
447}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400448
449func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string {
450 return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
451}
452
Ivan Lozano67eada32021-09-23 11:50:33 -0400453func (library *libraryDecorator) cfgFlags(ctx ModuleContext, flags Flags) Flags {
454 flags = library.baseCompiler.cfgFlags(ctx, flags)
Stephen Crane0dbfc562021-07-07 19:05:02 -0700455 if library.dylib() {
456 // We need to add a dependency on std in order to link crates as dylibs.
457 // The hack to add this dependency is guarded by the following cfg so
458 // that we don't force a dependency when it isn't needed.
459 library.baseCompiler.Properties.Cfgs = append(library.baseCompiler.Properties.Cfgs, "android_dylib")
460 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400461
462 flags.RustFlags = append(flags.RustFlags, library.baseCompiler.cfgsToFlags()...)
463 flags.RustdocFlags = append(flags.RustdocFlags, library.baseCompiler.cfgsToFlags()...)
464
465 return flags
466}
467
468func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800469 flags = library.baseCompiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400470
471 flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800472 if library.shared() || library.static() {
473 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
474 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400475 if library.shared() {
476 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
477 }
478
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800479 return flags
480}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700481
Sasha Smundaka76acba2022-04-18 20:12:56 -0700482func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
483 var outputFile android.ModuleOutPath
484 var ret buildOutput
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200485 var fileName string
Dan Albert06feee92021-03-19 15:06:02 -0700486 srcPath := library.srcPath(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700487
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400488 if library.sourceProvider != nil {
Ivan Lozano9d74a522020-12-01 09:25:22 -0500489 deps.srcProviderFiles = append(deps.srcProviderFiles, library.sourceProvider.Srcs()...)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400490 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700491
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400492 // Calculate output filename
493 if library.rlib() {
494 fileName = library.getStem(ctx) + ctx.toolchain().RlibSuffix()
495 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700496 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400497 } else if library.dylib() {
498 fileName = library.getStem(ctx) + ctx.toolchain().DylibSuffix()
499 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700500 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400501 } else if library.static() {
502 fileName = library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
503 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700504 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400505 } else if library.shared() {
506 fileName = library.sharedLibFilename(ctx)
507 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700508 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400509 }
510
511 if !library.rlib() && !library.static() && library.stripper.NeedsStrip(ctx) {
512 strippedOutputFile := outputFile
513 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
514 library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
515
516 library.baseCompiler.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
517 }
518 library.baseCompiler.unstrippedOutputFile = outputFile
519
Ivan Lozanoffee3342019-08-27 12:03:00 -0700520 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500521 flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...)
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700522 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects.Strings()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700523
Matthew Maurer46c46cc2020-01-13 16:34:34 -0800524 if library.dylib() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700525 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
526 // https://github.com/rust-lang/rust/issues/19680
527 // https://github.com/rust-lang/rust/issues/34909
528 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
529 }
530
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400531 // Call the appropriate builder for this library type
Ivan Lozanoffee3342019-08-27 12:03:00 -0700532 if library.rlib() {
Sasha Smundaka76acba2022-04-18 20:12:56 -0700533 ret.kytheFile = TransformSrctoRlib(ctx, srcPath, deps, flags, outputFile).kytheFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700534 } else if library.dylib() {
Sasha Smundaka76acba2022-04-18 20:12:56 -0700535 ret.kytheFile = TransformSrctoDylib(ctx, srcPath, deps, flags, outputFile).kytheFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700536 } else if library.static() {
Sasha Smundaka76acba2022-04-18 20:12:56 -0700537 ret.kytheFile = TransformSrctoStatic(ctx, srcPath, deps, flags, outputFile).kytheFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700538 } else if library.shared() {
Sasha Smundaka76acba2022-04-18 20:12:56 -0700539 ret.kytheFile = TransformSrctoShared(ctx, srcPath, deps, flags, outputFile).kytheFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700540 }
541
Ivan Lozano52767be2019-10-18 14:49:46 -0700542 if library.rlib() || library.dylib() {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700543 library.flagExporter.exportLinkDirs(deps.linkDirs...)
Colin Cross0de8a1e2020-09-18 14:15:30 -0700544 library.flagExporter.exportLinkObjects(deps.linkObjects...)
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700545 library.flagExporter.exportLibDeps(deps.LibDeps...)
Ivan Lozano52767be2019-10-18 14:49:46 -0700546 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700547
Colin Cross0de8a1e2020-09-18 14:15:30 -0700548 if library.static() || library.shared() {
549 ctx.SetProvider(cc.FlagExporterInfoProvider, cc.FlagExporterInfo{
550 IncludeDirs: library.includeDirs,
551 })
552 }
553
554 if library.shared() {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400555 // Optimize out relinking against shared libraries whose interface hasn't changed by
556 // depending on a table of contents file instead of the library itself.
557 tocFile := outputFile.ReplaceExtension(ctx, flags.Toolchain.SharedLibSuffix()[1:]+".toc")
558 library.tocFile = android.OptionalPathForPath(tocFile)
559 cc.TransformSharedObjectToToc(ctx, outputFile, tocFile)
560
Colin Cross0de8a1e2020-09-18 14:15:30 -0700561 ctx.SetProvider(cc.SharedLibraryInfoProvider, cc.SharedLibraryInfo{
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400562 TableOfContents: android.OptionalPathForPath(tocFile),
563 SharedLibrary: outputFile,
564 Target: ctx.Target(),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700565 })
566 }
567
568 if library.static() {
569 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(outputFile).Build()
570 ctx.SetProvider(cc.StaticLibraryInfoProvider, cc.StaticLibraryInfo{
571 StaticLibrary: outputFile,
572
573 TransitiveStaticLibrariesForOrdering: depSet,
574 })
575 }
576
577 library.flagExporter.setProvider(ctx)
578
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400579 return ret
Ivan Lozanoffee3342019-08-27 12:03:00 -0700580}
581
Sasha Smundaka76acba2022-04-18 20:12:56 -0700582func (library *libraryDecorator) srcPath(ctx ModuleContext, _ PathDeps) android.Path {
Dan Albert06feee92021-03-19 15:06:02 -0700583 if library.sourceProvider != nil {
584 // Assume the first source from the source provider is the library entry point.
585 return library.sourceProvider.Srcs()[0]
586 } else {
587 path, _ := srcPathFromModuleSrcs(ctx, library.baseCompiler.Properties.Srcs)
588 return path
589 }
590}
591
592func (library *libraryDecorator) rustdoc(ctx ModuleContext, flags Flags,
593 deps PathDeps) android.OptionalPath {
594 // rustdoc has builtin support for documenting config specific information
595 // regardless of the actual config it was given
596 // (https://doc.rust-lang.org/rustdoc/advanced-features.html#cfgdoc-documenting-platform-specific-or-feature-specific-information),
597 // so we generate the rustdoc for only the primary module so that we have a
598 // single set of docs to refer to.
599 if ctx.Module() != ctx.PrimaryModule() {
600 return android.OptionalPath{}
601 }
602
603 return android.OptionalPathForPath(Rustdoc(ctx, library.srcPath(ctx, deps),
604 deps, flags))
605}
606
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700607func (library *libraryDecorator) getStem(ctx ModuleContext) string {
608 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
609 validateLibraryStem(ctx, stem, library.crateName())
610
611 return stem + String(library.baseCompiler.Properties.Suffix)
612}
613
Ivan Lozano2b081132020-09-08 12:46:52 -0400614func (library *libraryDecorator) install(ctx ModuleContext) {
615 // Only shared and dylib variants make sense to install.
616 if library.shared() || library.dylib() {
617 library.baseCompiler.install(ctx)
618 }
619}
620
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400621func (library *libraryDecorator) Disabled() bool {
622 return library.MutatedProperties.VariantIsDisabled
623}
624
625func (library *libraryDecorator) SetDisabled() {
626 library.MutatedProperties.VariantIsDisabled = true
627}
628
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700629var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
630
631func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
632 if crate_name == "" {
633 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
634 }
635
636 // crate_names are used for the library output file, and rustc expects these
637 // to be alphanumeric with underscores allowed.
638 if validCrateName.MatchString(crate_name) {
639 ctx.PropertyErrorf("crate_name",
640 "library crate_names must be alphanumeric with underscores allowed")
641 }
642
643 // Libraries are expected to begin with "lib" followed by the crate_name
644 if !strings.HasPrefix(filename, "lib"+crate_name) {
645 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
646 }
647}
648
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200649// LibraryMutator mutates the libraries into variants according to the
650// build{Rlib,Dylib} attributes.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700651func LibraryMutator(mctx android.BottomUpMutatorContext) {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200652 // Only mutate on Rust libraries.
653 m, ok := mctx.Module().(*Module)
654 if !ok || m.compiler == nil {
655 return
656 }
657 library, ok := m.compiler.(libraryInterface)
658 if !ok {
659 return
660 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400661
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200662 var variants []string
663 // The source variant is used for SourceProvider modules. The other variants (i.e. rlib and dylib)
664 // depend on this variant. It must be the first variant to be declared.
665 sourceVariant := false
666 if m.sourceProvider != nil {
667 variants = append(variants, "source")
668 sourceVariant = true
669 }
670 if library.buildRlib() {
671 variants = append(variants, rlibVariation)
672 }
673 if library.buildDylib() {
674 variants = append(variants, dylibVariation)
675 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400676
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200677 if len(variants) == 0 {
678 return
679 }
680 modules := mctx.CreateLocalVariations(variants...)
681
682 // The order of the variations (modules) matches the variant names provided. Iterate
683 // through the new variation modules and set their mutated properties.
684 for i, v := range modules {
685 switch variants[i] {
686 case rlibVariation:
687 v.(*Module).compiler.(libraryInterface).setRlib()
688 case dylibVariation:
689 v.(*Module).compiler.(libraryInterface).setDylib()
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400690 if v.(*Module).ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
Ivan Lozano6a884432020-12-02 09:15:16 -0500691 // TODO(b/165791368)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400692 // Disable dylib Vendor Ramdisk variations until we support these.
Ivan Lozano6a884432020-12-02 09:15:16 -0500693 v.(*Module).Disable()
694 }
Ivan Lozano1921e802021-05-20 13:39:16 -0400695
696 variation := v.(*Module).ModuleBase.ImageVariation().Variation
Matthew Maurerbdda9102021-12-16 23:42:47 +0000697 if strings.HasPrefix(variation, cc.VendorVariationPrefix) {
698 // TODO(b/204303985)
699 // Disable vendor dylibs until they are supported
700 v.(*Module).Disable()
701 }
702
Ivan Lozano1921e802021-05-20 13:39:16 -0400703 if strings.HasPrefix(variation, cc.VendorVariationPrefix) &&
704 m.HasVendorVariant() &&
Kiyoung Kim48f37782021-07-07 12:42:39 +0900705 !snapshot.IsVendorProprietaryModule(mctx) &&
Ivan Lozano1921e802021-05-20 13:39:16 -0400706 strings.TrimPrefix(variation, cc.VendorVariationPrefix) == mctx.DeviceConfig().VndkVersion() {
707
708 // cc.MutateImage runs before LibraryMutator, so vendor variations which are meant for rlibs only are
709 // produced for Dylibs; however, dylibs should not be enabled for boardVndkVersion for
710 // non-vendor proprietary modules.
711 v.(*Module).Disable()
712 }
713
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200714 case "source":
715 v.(*Module).compiler.(libraryInterface).setSource()
716 // The source variant does not produce any library.
717 // Disable the compilation steps.
718 v.(*Module).compiler.SetDisabled()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700719 }
720 }
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200721
722 // If a source variant is created, add an inter-variant dependency
723 // between the other variants and the source variant.
724 if sourceVariant {
725 sv := modules[0]
726 for _, v := range modules[1:] {
727 if !v.Enabled() {
728 continue
729 }
730 mctx.AddInterVariantDependency(sourceDepTag, v, sv)
731 }
732 // Alias the source variation so it can be named directly in "srcs" properties.
733 mctx.AliasVariation("source")
734 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700735}
Ivan Lozano2b081132020-09-08 12:46:52 -0400736
737func LibstdMutator(mctx android.BottomUpMutatorContext) {
738 if m, ok := mctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() {
739 switch library := m.compiler.(type) {
740 case libraryInterface:
741 // Only create a variant if a library is actually being built.
742 if library.rlib() && !library.sysroot() {
743 variants := []string{"rlib-std", "dylib-std"}
744 modules := mctx.CreateLocalVariations(variants...)
745
746 rlib := modules[0].(*Module)
747 dylib := modules[1].(*Module)
748 rlib.compiler.(libraryInterface).setRlibStd()
749 dylib.compiler.(libraryInterface).setDylibStd()
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400750 if dylib.ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation ||
751 strings.HasPrefix(dylib.ModuleBase.ImageVariation().Variation, cc.VendorVariationPrefix) {
Ivan Lozano6a884432020-12-02 09:15:16 -0500752 // TODO(b/165791368)
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400753 // Disable rlibs that link against dylib-std on vendor and vendor ramdisk variations until those dylib
Ivan Lozano6a884432020-12-02 09:15:16 -0500754 // variants are properly supported.
755 dylib.Disable()
756 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400757 rlib.Properties.RustSubName += RlibStdlibSuffix
Ivan Lozano2b081132020-09-08 12:46:52 -0400758 }
759 }
760 }
761}
Ivan Lozano1921e802021-05-20 13:39:16 -0400762
763func (l *libraryDecorator) snapshotHeaders() android.Paths {
764 if l.collectedSnapshotHeaders == nil {
765 panic("snapshotHeaders() must be called after collectHeadersForSnapshot()")
766 }
767 return l.collectedSnapshotHeaders
768}
769
770// collectHeadersForSnapshot collects all exported headers from library.
771// It globs header files in the source tree for exported include directories,
772// and tracks generated header files separately.
773//
774// This is to be called from GenerateAndroidBuildActions, and then collected
775// header files can be retrieved by snapshotHeaders().
776func (l *libraryDecorator) collectHeadersForSnapshot(ctx android.ModuleContext, deps PathDeps) {
777 ret := android.Paths{}
778
779 // Glob together the headers from the modules include_dirs property
780 for _, path := range android.CopyOfPaths(l.includeDirs) {
781 dir := path.String()
Liz Kammer0ea79982022-02-07 08:51:47 -0500782 globDir := dir + "/**/*"
783 glob, err := ctx.GlobWithDeps(globDir, nil)
Ivan Lozano1921e802021-05-20 13:39:16 -0400784 if err != nil {
Liz Kammer0ea79982022-02-07 08:51:47 -0500785 ctx.ModuleErrorf("glob of %q failed: %s", globDir, err)
Ivan Lozano1921e802021-05-20 13:39:16 -0400786 return
787 }
788
789 for _, header := range glob {
790 // Filter out only the files with extensions that are headers.
791 found := false
792 for _, ext := range cc.HeaderExts {
793 if strings.HasSuffix(header, ext) {
794 found = true
795 break
796 }
797 }
798 if !found {
799 continue
800 }
801 ret = append(ret, android.PathForSource(ctx, header))
802 }
803 }
804
805 // Glob together the headers from C dependencies as well, starting with non-generated headers.
806 ret = append(ret, cc.GlobHeadersForSnapshot(ctx, append(android.CopyOfPaths(deps.depIncludePaths), deps.depSystemIncludePaths...))...)
807
808 // Collect generated headers from C dependencies.
809 ret = append(ret, cc.GlobGeneratedHeadersForSnapshot(ctx, deps.depGeneratedHeaders)...)
810
811 // TODO(185577950): If support for generated headers is added, they need to be collected here as well.
812 l.collectedSnapshotHeaders = ret
813}