blob: 7f004fc20245dff22fd667c71758e8f063d6047a [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 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 Lozanoadd122a2023-07-13 11:01:41 -0400239 if library.preferRlib() {
Ivan Lozanoea086132020-12-08 14:43:00 -0500240 return rlibAutoDep
241 } else if library.rlib() || library.static() {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700242 return rlibAutoDep
243 } else if library.dylib() || library.shared() {
244 return dylibAutoDep
245 } else {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200246 panic(fmt.Errorf("autoDep called on library %q that has no enabled variants.", ctx.ModuleName()))
Matthew Maurer0f003b12020-06-29 14:34:06 -0700247 }
248}
249
Ivan Lozanoea086132020-12-08 14:43:00 -0500250func (library *libraryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400251 if library.static() || library.MutatedProperties.VariantIsStaticStd {
Ivan Lozanoea086132020-12-08 14:43:00 -0500252 return RlibLinkage
253 } else if library.baseCompiler.preferRlib() {
254 return RlibLinkage
255 }
256 return DefaultLinkage
257}
258
Ivan Lozanoffee3342019-08-27 12:03:00 -0700259var _ compiler = (*libraryDecorator)(nil)
Ivan Lozano52767be2019-10-18 14:49:46 -0700260var _ libraryInterface = (*libraryDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700261var _ exportedFlagsProducer = (*libraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700262
Martin Geisler67ec0542022-11-18 12:08:55 +0100263// rust_library produces all Rust variants (rust_library_dylib and
264// rust_library_rlib).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700265func RustLibraryFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700266 module, library := NewRustLibrary(android.HostAndDeviceSupported)
267 library.BuildOnlyRust()
268 return module.Init()
269}
270
Martin Geisler67ec0542022-11-18 12:08:55 +0100271// rust_ffi produces all FFI variants (rust_ffi_shared and
272// rust_ffi_static).
Matthew Maurer2ae05132020-06-23 14:28:53 -0700273func RustFFIFactory() android.Module {
274 module, library := NewRustLibrary(android.HostAndDeviceSupported)
275 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700276 return module.Init()
277}
278
Martin Geisler67ec0542022-11-18 12:08:55 +0100279// rust_library_dylib produces a Rust dylib (Rust crate type "dylib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700280func RustLibraryDylibFactory() android.Module {
281 module, library := NewRustLibrary(android.HostAndDeviceSupported)
282 library.BuildOnlyDylib()
283 return module.Init()
284}
285
Martin Geisler67ec0542022-11-18 12:08:55 +0100286// rust_library_rlib produces an rlib (Rust crate type "rlib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700287func RustLibraryRlibFactory() android.Module {
288 module, library := NewRustLibrary(android.HostAndDeviceSupported)
289 library.BuildOnlyRlib()
290 return module.Init()
291}
292
Martin Geisler67ec0542022-11-18 12:08:55 +0100293// rust_ffi_shared produces a shared library (Rust crate type
294// "cdylib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700295func RustFFISharedFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700296 module, library := NewRustLibrary(android.HostAndDeviceSupported)
297 library.BuildOnlyShared()
298 return module.Init()
299}
300
Martin Geisler67ec0542022-11-18 12:08:55 +0100301// rust_ffi_static produces a static library (Rust crate type
302// "staticlib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700303func RustFFIStaticFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700304 module, library := NewRustLibrary(android.HostAndDeviceSupported)
305 library.BuildOnlyStatic()
306 return module.Init()
307}
308
Martin Geisler67ec0542022-11-18 12:08:55 +0100309// rust_library_host produces all Rust variants for the host
310// (rust_library_dylib_host and rust_library_rlib_host).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700311func RustLibraryHostFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700312 module, library := NewRustLibrary(android.HostSupported)
313 library.BuildOnlyRust()
314 return module.Init()
315}
316
Martin Geisler67ec0542022-11-18 12:08:55 +0100317// rust_ffi_host produces all FFI variants for the host
318// (rust_ffi_static_host and rust_ffi_shared_host).
Matthew Maurer2ae05132020-06-23 14:28:53 -0700319func RustFFIHostFactory() android.Module {
320 module, library := NewRustLibrary(android.HostSupported)
321 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700322 return module.Init()
323}
324
Martin Geisler67ec0542022-11-18 12:08:55 +0100325// rust_library_dylib_host produces a dylib for the host (Rust crate
326// type "dylib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700327func RustLibraryDylibHostFactory() android.Module {
328 module, library := NewRustLibrary(android.HostSupported)
329 library.BuildOnlyDylib()
330 return module.Init()
331}
332
Martin Geisler67ec0542022-11-18 12:08:55 +0100333// rust_library_rlib_host produces an rlib for the host (Rust crate
334// type "rlib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700335func RustLibraryRlibHostFactory() android.Module {
336 module, library := NewRustLibrary(android.HostSupported)
337 library.BuildOnlyRlib()
338 return module.Init()
339}
340
Martin Geisler67ec0542022-11-18 12:08:55 +0100341// rust_ffi_static_host produces a static library for the host (Rust
342// crate type "staticlib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700343func RustFFIStaticHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700344 module, library := NewRustLibrary(android.HostSupported)
345 library.BuildOnlyStatic()
346 return module.Init()
347}
348
Martin Geisler67ec0542022-11-18 12:08:55 +0100349// rust_ffi_shared_host produces an shared library for the host (Rust
350// crate type "cdylib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700351func RustFFISharedHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700352 module, library := NewRustLibrary(android.HostSupported)
353 library.BuildOnlyShared()
354 return module.Init()
355}
356
Matthew Maurer2ae05132020-06-23 14:28:53 -0700357func (library *libraryDecorator) BuildOnlyFFI() {
358 library.MutatedProperties.BuildDylib = false
359 library.MutatedProperties.BuildRlib = false
360 library.MutatedProperties.BuildShared = true
361 library.MutatedProperties.BuildStatic = true
362}
363
364func (library *libraryDecorator) BuildOnlyRust() {
365 library.MutatedProperties.BuildDylib = true
366 library.MutatedProperties.BuildRlib = true
367 library.MutatedProperties.BuildShared = false
368 library.MutatedProperties.BuildStatic = false
369}
370
Ivan Lozanoffee3342019-08-27 12:03:00 -0700371func (library *libraryDecorator) BuildOnlyDylib() {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700372 library.MutatedProperties.BuildDylib = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700373 library.MutatedProperties.BuildRlib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700374 library.MutatedProperties.BuildShared = false
375 library.MutatedProperties.BuildStatic = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700376}
377
378func (library *libraryDecorator) BuildOnlyRlib() {
379 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700380 library.MutatedProperties.BuildRlib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700381 library.MutatedProperties.BuildShared = false
382 library.MutatedProperties.BuildStatic = false
383}
384
385func (library *libraryDecorator) BuildOnlyStatic() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700386 library.MutatedProperties.BuildRlib = false
387 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700388 library.MutatedProperties.BuildShared = false
389 library.MutatedProperties.BuildStatic = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700390}
391
392func (library *libraryDecorator) BuildOnlyShared() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700393 library.MutatedProperties.BuildRlib = false
394 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700395 library.MutatedProperties.BuildStatic = false
396 library.MutatedProperties.BuildShared = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700397}
398
399func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400400 module := newModule(hod, android.MultilibBoth)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700401
402 library := &libraryDecorator{
403 MutatedProperties: LibraryMutatedProperties{
Matthew Maurer2ae05132020-06-23 14:28:53 -0700404 BuildDylib: false,
405 BuildRlib: false,
406 BuildShared: false,
407 BuildStatic: false,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700408 },
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800409 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Matthew Maurerbb3add12020-06-25 09:34:12 -0700410 flagExporter: NewFlagExporter(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700411 }
412
413 module.compiler = library
414
415 return module, library
416}
417
418func (library *libraryDecorator) compilerProps() []interface{} {
419 return append(library.baseCompiler.compilerProps(),
420 &library.Properties,
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200421 &library.MutatedProperties,
422 &library.stripper.StripProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700423}
424
Ivan Lozanof1c84332019-09-20 11:00:37 -0700425func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
426 deps = library.baseCompiler.compilerDeps(ctx, deps)
427
Colin Crosse32f0932022-01-23 20:48:36 -0800428 if library.dylib() || library.shared() {
429 if ctx.toolchain().Bionic() {
430 deps = bionicDeps(ctx, deps, false)
431 deps.CrtBegin = []string{"crtbegin_so"}
432 deps.CrtEnd = []string{"crtend_so"}
433 } else if ctx.Os() == android.LinuxMusl {
434 deps = muslDeps(ctx, deps, false)
435 deps.CrtBegin = []string{"libc_musl_crtbegin_so"}
436 deps.CrtEnd = []string{"libc_musl_crtend_so"}
437 }
Ivan Lozanof1c84332019-09-20 11:00:37 -0700438 }
439
440 return deps
441}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400442
443func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string {
444 return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
445}
446
Ivan Lozano67eada32021-09-23 11:50:33 -0400447func (library *libraryDecorator) cfgFlags(ctx ModuleContext, flags Flags) Flags {
448 flags = library.baseCompiler.cfgFlags(ctx, flags)
Stephen Crane0dbfc562021-07-07 19:05:02 -0700449 if library.dylib() {
450 // We need to add a dependency on std in order to link crates as dylibs.
451 // The hack to add this dependency is guarded by the following cfg so
452 // that we don't force a dependency when it isn't needed.
453 library.baseCompiler.Properties.Cfgs = append(library.baseCompiler.Properties.Cfgs, "android_dylib")
454 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400455
456 flags.RustFlags = append(flags.RustFlags, library.baseCompiler.cfgsToFlags()...)
457 flags.RustdocFlags = append(flags.RustdocFlags, library.baseCompiler.cfgsToFlags()...)
458
459 return flags
460}
461
462func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800463 flags = library.baseCompiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400464
465 flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800466 if library.shared() || library.static() {
467 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
468 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400469 if library.shared() {
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -0700470 if ctx.Darwin() {
471 flags.LinkFlags = append(
472 flags.LinkFlags,
473 "-dynamic_lib",
474 "-install_name @rpath/"+library.sharedLibFilename(ctx),
475 )
476 } else {
477 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
478 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400479 }
480
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800481 return flags
482}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700483
Sasha Smundaka76acba2022-04-18 20:12:56 -0700484func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
485 var outputFile android.ModuleOutPath
486 var ret buildOutput
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200487 var fileName string
Matthew Maurera28404a2023-11-20 23:33:28 +0000488 crateRootPath := crateRootPath(ctx, library)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700489
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400490 if library.sourceProvider != nil {
Ivan Lozano9d74a522020-12-01 09:25:22 -0500491 deps.srcProviderFiles = append(deps.srcProviderFiles, library.sourceProvider.Srcs()...)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400492 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700493
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400494 // Calculate output filename
495 if library.rlib() {
496 fileName = library.getStem(ctx) + ctx.toolchain().RlibSuffix()
497 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700498 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400499 } else if library.dylib() {
500 fileName = library.getStem(ctx) + ctx.toolchain().DylibSuffix()
501 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700502 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400503 } else if library.static() {
504 fileName = library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
505 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700506 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400507 } else if library.shared() {
508 fileName = library.sharedLibFilename(ctx)
509 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700510 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400511 }
512
513 if !library.rlib() && !library.static() && library.stripper.NeedsStrip(ctx) {
514 strippedOutputFile := outputFile
515 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
516 library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
517
518 library.baseCompiler.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
519 }
520 library.baseCompiler.unstrippedOutputFile = outputFile
521
Ivan Lozanoffee3342019-08-27 12:03:00 -0700522 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500523 flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...)
Colin Cross004bd3f2023-10-02 11:39:17 -0700524 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700525
Matthew Maurer46c46cc2020-01-13 16:34:34 -0800526 if library.dylib() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700527 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
528 // https://github.com/rust-lang/rust/issues/19680
529 // https://github.com/rust-lang/rust/issues/34909
530 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
531 }
532
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400533 // Call the appropriate builder for this library type
Ivan Lozanoffee3342019-08-27 12:03:00 -0700534 if library.rlib() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000535 ret.kytheFile = TransformSrctoRlib(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700536 } else if library.dylib() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000537 ret.kytheFile = TransformSrctoDylib(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700538 } else if library.static() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000539 ret.kytheFile = TransformSrctoStatic(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700540 } else if library.shared() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000541 ret.kytheFile = TransformSrctoShared(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700542 }
543
Ivan Lozano52767be2019-10-18 14:49:46 -0700544 if library.rlib() || library.dylib() {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700545 library.flagExporter.exportLinkDirs(deps.linkDirs...)
Colin Cross0de8a1e2020-09-18 14:15:30 -0700546 library.flagExporter.exportLinkObjects(deps.linkObjects...)
Ivan Lozano52767be2019-10-18 14:49:46 -0700547 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700548
Colin Cross0de8a1e2020-09-18 14:15:30 -0700549 if library.static() || library.shared() {
Colin Cross40213022023-12-13 15:19:49 -0800550 android.SetProvider(ctx, cc.FlagExporterInfoProvider, cc.FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700551 IncludeDirs: library.includeDirs,
552 })
553 }
554
555 if library.shared() {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400556 // Optimize out relinking against shared libraries whose interface hasn't changed by
557 // depending on a table of contents file instead of the library itself.
558 tocFile := outputFile.ReplaceExtension(ctx, flags.Toolchain.SharedLibSuffix()[1:]+".toc")
559 library.tocFile = android.OptionalPathForPath(tocFile)
560 cc.TransformSharedObjectToToc(ctx, outputFile, tocFile)
561
Colin Cross40213022023-12-13 15:19:49 -0800562 android.SetProvider(ctx, cc.SharedLibraryInfoProvider, cc.SharedLibraryInfo{
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400563 TableOfContents: android.OptionalPathForPath(tocFile),
564 SharedLibrary: outputFile,
565 Target: ctx.Target(),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700566 })
567 }
568
569 if library.static() {
Colin Crossc85750b2022-04-21 12:50:51 -0700570 depSet := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).Direct(outputFile).Build()
Colin Cross40213022023-12-13 15:19:49 -0800571 android.SetProvider(ctx, cc.StaticLibraryInfoProvider, cc.StaticLibraryInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700572 StaticLibrary: outputFile,
573
574 TransitiveStaticLibrariesForOrdering: depSet,
575 })
576 }
577
578 library.flagExporter.setProvider(ctx)
579
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400580 return ret
Ivan Lozanoffee3342019-08-27 12:03:00 -0700581}
582
Matthew Maurera28404a2023-11-20 23:33:28 +0000583func (library *libraryDecorator) checkedCrateRootPath() (android.Path, error) {
Dan Albert06feee92021-03-19 15:06:02 -0700584 if library.sourceProvider != nil {
Matthew Maurera28404a2023-11-20 23:33:28 +0000585 srcs := library.sourceProvider.Srcs()
586 if len(srcs) == 0 {
587 return nil, errors.New("Source provider generated 0 sources")
588 }
Dan Albert06feee92021-03-19 15:06:02 -0700589 // Assume the first source from the source provider is the library entry point.
Matthew Maurera28404a2023-11-20 23:33:28 +0000590 return srcs[0], nil
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000591 } else {
Matthew Maurera28404a2023-11-20 23:33:28 +0000592 return library.baseCompiler.checkedCrateRootPath()
Dan Albert06feee92021-03-19 15:06:02 -0700593 }
594}
595
596func (library *libraryDecorator) rustdoc(ctx ModuleContext, flags Flags,
597 deps PathDeps) android.OptionalPath {
598 // rustdoc has builtin support for documenting config specific information
599 // regardless of the actual config it was given
600 // (https://doc.rust-lang.org/rustdoc/advanced-features.html#cfgdoc-documenting-platform-specific-or-feature-specific-information),
601 // so we generate the rustdoc for only the primary module so that we have a
602 // single set of docs to refer to.
603 if ctx.Module() != ctx.PrimaryModule() {
604 return android.OptionalPath{}
605 }
606
Matthew Maurera28404a2023-11-20 23:33:28 +0000607 return android.OptionalPathForPath(Rustdoc(ctx, crateRootPath(ctx, library),
Dan Albert06feee92021-03-19 15:06:02 -0700608 deps, flags))
609}
610
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700611func (library *libraryDecorator) getStem(ctx ModuleContext) string {
612 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
613 validateLibraryStem(ctx, stem, library.crateName())
614
615 return stem + String(library.baseCompiler.Properties.Suffix)
616}
617
Ivan Lozano2b081132020-09-08 12:46:52 -0400618func (library *libraryDecorator) install(ctx ModuleContext) {
619 // Only shared and dylib variants make sense to install.
620 if library.shared() || library.dylib() {
621 library.baseCompiler.install(ctx)
622 }
623}
624
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400625func (library *libraryDecorator) Disabled() bool {
626 return library.MutatedProperties.VariantIsDisabled
627}
628
629func (library *libraryDecorator) SetDisabled() {
630 library.MutatedProperties.VariantIsDisabled = true
631}
632
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700633var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
634
635func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
636 if crate_name == "" {
637 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
638 }
639
640 // crate_names are used for the library output file, and rustc expects these
641 // to be alphanumeric with underscores allowed.
642 if validCrateName.MatchString(crate_name) {
643 ctx.PropertyErrorf("crate_name",
644 "library crate_names must be alphanumeric with underscores allowed")
645 }
646
647 // Libraries are expected to begin with "lib" followed by the crate_name
648 if !strings.HasPrefix(filename, "lib"+crate_name) {
649 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
650 }
651}
652
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200653// LibraryMutator mutates the libraries into variants according to the
654// build{Rlib,Dylib} attributes.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700655func LibraryMutator(mctx android.BottomUpMutatorContext) {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200656 // Only mutate on Rust libraries.
657 m, ok := mctx.Module().(*Module)
658 if !ok || m.compiler == nil {
659 return
660 }
661 library, ok := m.compiler.(libraryInterface)
662 if !ok {
663 return
664 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400665
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200666 var variants []string
667 // The source variant is used for SourceProvider modules. The other variants (i.e. rlib and dylib)
668 // depend on this variant. It must be the first variant to be declared.
669 sourceVariant := false
670 if m.sourceProvider != nil {
671 variants = append(variants, "source")
672 sourceVariant = true
673 }
674 if library.buildRlib() {
675 variants = append(variants, rlibVariation)
676 }
677 if library.buildDylib() {
678 variants = append(variants, dylibVariation)
679 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400680
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200681 if len(variants) == 0 {
682 return
683 }
684 modules := mctx.CreateLocalVariations(variants...)
685
686 // The order of the variations (modules) matches the variant names provided. Iterate
687 // through the new variation modules and set their mutated properties.
688 for i, v := range modules {
689 switch variants[i] {
690 case rlibVariation:
691 v.(*Module).compiler.(libraryInterface).setRlib()
692 case dylibVariation:
693 v.(*Module).compiler.(libraryInterface).setDylib()
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400694 if v.(*Module).ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
Ivan Lozano6a884432020-12-02 09:15:16 -0500695 // TODO(b/165791368)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400696 // Disable dylib Vendor Ramdisk variations until we support these.
Ivan Lozano6a884432020-12-02 09:15:16 -0500697 v.(*Module).Disable()
698 }
Ivan Lozano1921e802021-05-20 13:39:16 -0400699
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200700 case "source":
701 v.(*Module).compiler.(libraryInterface).setSource()
702 // The source variant does not produce any library.
703 // Disable the compilation steps.
704 v.(*Module).compiler.SetDisabled()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700705 }
706 }
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200707
708 // If a source variant is created, add an inter-variant dependency
709 // between the other variants and the source variant.
710 if sourceVariant {
711 sv := modules[0]
712 for _, v := range modules[1:] {
713 if !v.Enabled() {
714 continue
715 }
716 mctx.AddInterVariantDependency(sourceDepTag, v, sv)
717 }
718 // Alias the source variation so it can be named directly in "srcs" properties.
719 mctx.AliasVariation("source")
720 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700721}
Ivan Lozano2b081132020-09-08 12:46:52 -0400722
723func LibstdMutator(mctx android.BottomUpMutatorContext) {
724 if m, ok := mctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() {
725 switch library := m.compiler.(type) {
726 case libraryInterface:
727 // Only create a variant if a library is actually being built.
728 if library.rlib() && !library.sysroot() {
729 variants := []string{"rlib-std", "dylib-std"}
730 modules := mctx.CreateLocalVariations(variants...)
731
732 rlib := modules[0].(*Module)
733 dylib := modules[1].(*Module)
734 rlib.compiler.(libraryInterface).setRlibStd()
735 dylib.compiler.(libraryInterface).setDylibStd()
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400736 if dylib.ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
Ivan Lozano6a884432020-12-02 09:15:16 -0500737 // TODO(b/165791368)
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400738 // Disable rlibs that link against dylib-std on vendor ramdisk variations until those dylib
Ivan Lozano6a884432020-12-02 09:15:16 -0500739 // variants are properly supported.
740 dylib.Disable()
741 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400742 rlib.Properties.RustSubName += RlibStdlibSuffix
Ivan Lozano2b081132020-09-08 12:46:52 -0400743 }
744 }
745 }
746}
Ivan Lozano1921e802021-05-20 13:39:16 -0400747
748func (l *libraryDecorator) snapshotHeaders() android.Paths {
749 if l.collectedSnapshotHeaders == nil {
750 panic("snapshotHeaders() must be called after collectHeadersForSnapshot()")
751 }
752 return l.collectedSnapshotHeaders
753}
754
755// collectHeadersForSnapshot collects all exported headers from library.
756// It globs header files in the source tree for exported include directories,
757// and tracks generated header files separately.
758//
759// This is to be called from GenerateAndroidBuildActions, and then collected
760// header files can be retrieved by snapshotHeaders().
761func (l *libraryDecorator) collectHeadersForSnapshot(ctx android.ModuleContext, deps PathDeps) {
762 ret := android.Paths{}
763
764 // Glob together the headers from the modules include_dirs property
765 for _, path := range android.CopyOfPaths(l.includeDirs) {
766 dir := path.String()
Liz Kammer0ea79982022-02-07 08:51:47 -0500767 globDir := dir + "/**/*"
768 glob, err := ctx.GlobWithDeps(globDir, nil)
Ivan Lozano1921e802021-05-20 13:39:16 -0400769 if err != nil {
Liz Kammer0ea79982022-02-07 08:51:47 -0500770 ctx.ModuleErrorf("glob of %q failed: %s", globDir, err)
Ivan Lozano1921e802021-05-20 13:39:16 -0400771 return
772 }
773
774 for _, header := range glob {
775 // Filter out only the files with extensions that are headers.
776 found := false
777 for _, ext := range cc.HeaderExts {
778 if strings.HasSuffix(header, ext) {
779 found = true
780 break
781 }
782 }
783 if !found {
784 continue
785 }
786 ret = append(ret, android.PathForSource(ctx, header))
787 }
788 }
789
790 // Glob together the headers from C dependencies as well, starting with non-generated headers.
791 ret = append(ret, cc.GlobHeadersForSnapshot(ctx, append(android.CopyOfPaths(deps.depIncludePaths), deps.depSystemIncludePaths...))...)
792
793 // Collect generated headers from C dependencies.
794 ret = append(ret, cc.GlobGeneratedHeadersForSnapshot(ctx, deps.depGeneratedHeaders)...)
795
796 // TODO(185577950): If support for generated headers is added, they need to be collected here as well.
797 l.collectedSnapshotHeaders = ret
798}