blob: 419fcfca2a36fa83eda9d6d3ab6b6219de8c9e1c [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"
Ivan Lozanoffee3342019-08-27 12:03:00 -070024)
25
Ivan Lozano2b081132020-09-08 12:46:52 -040026var (
Ivan Lozano4df02572023-06-15 14:21:09 -040027 RlibStdlibSuffix = ".rlib-std"
Ivan Lozano2b081132020-09-08 12:46:52 -040028)
29
Ivan Lozanoffee3342019-08-27 12:03:00 -070030func init() {
31 android.RegisterModuleType("rust_library", RustLibraryFactory)
32 android.RegisterModuleType("rust_library_dylib", RustLibraryDylibFactory)
33 android.RegisterModuleType("rust_library_rlib", RustLibraryRlibFactory)
34 android.RegisterModuleType("rust_library_host", RustLibraryHostFactory)
35 android.RegisterModuleType("rust_library_host_dylib", RustLibraryDylibHostFactory)
36 android.RegisterModuleType("rust_library_host_rlib", RustLibraryRlibHostFactory)
Matthew Maurer2ae05132020-06-23 14:28:53 -070037 android.RegisterModuleType("rust_ffi", RustFFIFactory)
38 android.RegisterModuleType("rust_ffi_shared", RustFFISharedFactory)
39 android.RegisterModuleType("rust_ffi_static", RustFFIStaticFactory)
40 android.RegisterModuleType("rust_ffi_host", RustFFIHostFactory)
41 android.RegisterModuleType("rust_ffi_host_shared", RustFFISharedHostFactory)
42 android.RegisterModuleType("rust_ffi_host_static", RustFFIStaticHostFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070043}
44
45type VariantLibraryProperties struct {
Matthew Maurerc761eec2020-06-25 00:47:46 -070046 Enabled *bool `android:"arch_variant"`
47 Srcs []string `android:"path,arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070048}
49
50type LibraryCompilerProperties struct {
Ivan Lozano52767be2019-10-18 14:49:46 -070051 Rlib VariantLibraryProperties `android:"arch_variant"`
52 Dylib VariantLibraryProperties `android:"arch_variant"`
53 Shared VariantLibraryProperties `android:"arch_variant"`
54 Static VariantLibraryProperties `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070055
Ivan Lozano52767be2019-10-18 14:49:46 -070056 // path to include directories to pass to cc_* modules, only relevant for static/shared variants.
57 Include_dirs []string `android:"path,arch_variant"`
Ivan Lozano2b081132020-09-08 12:46:52 -040058
59 // Whether this library is part of the Rust toolchain sysroot.
60 Sysroot *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070061}
62
63type LibraryMutatedProperties struct {
Ivan Lozanoffee3342019-08-27 12:03:00 -070064 // Build a dylib variant
65 BuildDylib bool `blueprint:"mutated"`
66 // Build an rlib variant
67 BuildRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070068 // Build a shared library variant
69 BuildShared bool `blueprint:"mutated"`
70 // Build a static library variant
71 BuildStatic bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070072
73 // This variant is a dylib
74 VariantIsDylib bool `blueprint:"mutated"`
75 // This variant is an rlib
76 VariantIsRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070077 // This variant is a shared library
78 VariantIsShared bool `blueprint:"mutated"`
79 // This variant is a static library
80 VariantIsStatic bool `blueprint:"mutated"`
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +020081 // This variant is a source provider
82 VariantIsSource bool `blueprint:"mutated"`
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040083
84 // This variant is disabled and should not be compiled
85 // (used for SourceProvider variants that produce only source)
86 VariantIsDisabled bool `blueprint:"mutated"`
Ivan Lozano2b081132020-09-08 12:46:52 -040087
88 // Whether this library variant should be link libstd via rlibs
89 VariantIsStaticStd bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070090}
91
92type libraryDecorator struct {
93 *baseCompiler
Matthew Maurerbb3add12020-06-25 09:34:12 -070094 *flagExporter
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +020095 stripper Stripper
Ivan Lozanoffee3342019-08-27 12:03:00 -070096
Ivan Lozano8a23fa42020-06-16 10:26:57 -040097 Properties LibraryCompilerProperties
98 MutatedProperties LibraryMutatedProperties
99 includeDirs android.Paths
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400100 sourceProvider SourceProvider
Ivan Lozano1921e802021-05-20 13:39:16 -0400101
102 collectedSnapshotHeaders android.Paths
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400103
104 // table-of-contents file for cdylib crates to optimize out relinking when possible
105 tocFile android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700106}
107
108type libraryInterface interface {
109 rlib() bool
110 dylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700111 static() bool
112 shared() bool
Ivan Lozano2b081132020-09-08 12:46:52 -0400113 sysroot() bool
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200114 source() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700115
116 // Returns true if the build options for the module have selected a particular build type
117 buildRlib() bool
118 buildDylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700119 buildShared() bool
120 buildStatic() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700121
122 // Sets a particular variant type
123 setRlib()
124 setDylib()
Ivan Lozano52767be2019-10-18 14:49:46 -0700125 setShared()
126 setStatic()
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200127 setSource()
Ivan Lozano52767be2019-10-18 14:49:46 -0700128
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400129 // libstd linkage functions
130 rlibStd() bool
Ivan Lozano2b081132020-09-08 12:46:52 -0400131 setRlibStd()
132 setDylibStd()
133
Ivan Lozano52767be2019-10-18 14:49:46 -0700134 // Build a specific library variant
Matthew Maurer2ae05132020-06-23 14:28:53 -0700135 BuildOnlyFFI()
136 BuildOnlyRust()
Ivan Lozano52767be2019-10-18 14:49:46 -0700137 BuildOnlyRlib()
138 BuildOnlyDylib()
139 BuildOnlyStatic()
140 BuildOnlyShared()
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400141
142 toc() android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700143}
144
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400145func (library *libraryDecorator) nativeCoverage() bool {
146 return true
147}
148
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400149func (library *libraryDecorator) toc() android.OptionalPath {
150 return library.tocFile
151}
152
Ivan Lozanoffee3342019-08-27 12:03:00 -0700153func (library *libraryDecorator) rlib() bool {
154 return library.MutatedProperties.VariantIsRlib
155}
156
Ivan Lozano2b081132020-09-08 12:46:52 -0400157func (library *libraryDecorator) sysroot() bool {
158 return Bool(library.Properties.Sysroot)
159}
160
Ivan Lozanoffee3342019-08-27 12:03:00 -0700161func (library *libraryDecorator) dylib() bool {
162 return library.MutatedProperties.VariantIsDylib
163}
164
Ivan Lozano52767be2019-10-18 14:49:46 -0700165func (library *libraryDecorator) shared() bool {
166 return library.MutatedProperties.VariantIsShared
167}
168
169func (library *libraryDecorator) static() bool {
170 return library.MutatedProperties.VariantIsStatic
171}
172
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200173func (library *libraryDecorator) source() bool {
174 return library.MutatedProperties.VariantIsSource
175}
176
Ivan Lozanoffee3342019-08-27 12:03:00 -0700177func (library *libraryDecorator) buildRlib() bool {
178 return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true)
179}
180
181func (library *libraryDecorator) buildDylib() bool {
182 return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true)
183}
184
Ivan Lozano52767be2019-10-18 14:49:46 -0700185func (library *libraryDecorator) buildShared() bool {
186 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
187}
188
189func (library *libraryDecorator) buildStatic() bool {
190 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
191}
192
Ivan Lozanoffee3342019-08-27 12:03:00 -0700193func (library *libraryDecorator) setRlib() {
194 library.MutatedProperties.VariantIsRlib = true
195 library.MutatedProperties.VariantIsDylib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700196 library.MutatedProperties.VariantIsStatic = false
197 library.MutatedProperties.VariantIsShared = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700198}
199
200func (library *libraryDecorator) setDylib() {
201 library.MutatedProperties.VariantIsRlib = false
202 library.MutatedProperties.VariantIsDylib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700203 library.MutatedProperties.VariantIsStatic = false
204 library.MutatedProperties.VariantIsShared = false
205}
206
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400207func (library *libraryDecorator) rlibStd() bool {
208 return library.MutatedProperties.VariantIsStaticStd
209}
210
Ivan Lozano2b081132020-09-08 12:46:52 -0400211func (library *libraryDecorator) setRlibStd() {
212 library.MutatedProperties.VariantIsStaticStd = true
213}
214
215func (library *libraryDecorator) setDylibStd() {
216 library.MutatedProperties.VariantIsStaticStd = false
217}
218
Ivan Lozano52767be2019-10-18 14:49:46 -0700219func (library *libraryDecorator) setShared() {
220 library.MutatedProperties.VariantIsStatic = false
221 library.MutatedProperties.VariantIsShared = true
222 library.MutatedProperties.VariantIsRlib = false
223 library.MutatedProperties.VariantIsDylib = false
224}
225
226func (library *libraryDecorator) setStatic() {
227 library.MutatedProperties.VariantIsStatic = true
228 library.MutatedProperties.VariantIsShared = false
229 library.MutatedProperties.VariantIsRlib = false
230 library.MutatedProperties.VariantIsDylib = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700231}
232
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200233func (library *libraryDecorator) setSource() {
234 library.MutatedProperties.VariantIsSource = true
235}
236
Liz Kammer356f7d42021-01-26 09:18:53 -0500237func (library *libraryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400238 if library.preferRlib() {
Ivan Lozanoea086132020-12-08 14:43:00 -0500239 return rlibAutoDep
240 } else if library.rlib() || library.static() {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700241 return rlibAutoDep
242 } else if library.dylib() || library.shared() {
243 return dylibAutoDep
244 } else {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200245 panic(fmt.Errorf("autoDep called on library %q that has no enabled variants.", ctx.ModuleName()))
Matthew Maurer0f003b12020-06-29 14:34:06 -0700246 }
247}
248
Ivan Lozanoea086132020-12-08 14:43:00 -0500249func (library *libraryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400250 if library.static() || library.MutatedProperties.VariantIsStaticStd {
Ivan Lozanoea086132020-12-08 14:43:00 -0500251 return RlibLinkage
252 } else if library.baseCompiler.preferRlib() {
253 return RlibLinkage
254 }
255 return DefaultLinkage
256}
257
Ivan Lozanoffee3342019-08-27 12:03:00 -0700258var _ compiler = (*libraryDecorator)(nil)
Ivan Lozano52767be2019-10-18 14:49:46 -0700259var _ libraryInterface = (*libraryDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700260var _ exportedFlagsProducer = (*libraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700261
Martin Geisler67ec0542022-11-18 12:08:55 +0100262// rust_library produces all Rust variants (rust_library_dylib and
263// rust_library_rlib).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700264func RustLibraryFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700265 module, library := NewRustLibrary(android.HostAndDeviceSupported)
266 library.BuildOnlyRust()
267 return module.Init()
268}
269
Martin Geisler67ec0542022-11-18 12:08:55 +0100270// rust_ffi produces all FFI variants (rust_ffi_shared and
271// rust_ffi_static).
Matthew Maurer2ae05132020-06-23 14:28:53 -0700272func RustFFIFactory() android.Module {
273 module, library := NewRustLibrary(android.HostAndDeviceSupported)
274 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700275 return module.Init()
276}
277
Martin Geisler67ec0542022-11-18 12:08:55 +0100278// rust_library_dylib produces a Rust dylib (Rust crate type "dylib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700279func RustLibraryDylibFactory() android.Module {
280 module, library := NewRustLibrary(android.HostAndDeviceSupported)
281 library.BuildOnlyDylib()
282 return module.Init()
283}
284
Martin Geisler67ec0542022-11-18 12:08:55 +0100285// rust_library_rlib produces an rlib (Rust crate type "rlib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700286func RustLibraryRlibFactory() android.Module {
287 module, library := NewRustLibrary(android.HostAndDeviceSupported)
288 library.BuildOnlyRlib()
289 return module.Init()
290}
291
Martin Geisler67ec0542022-11-18 12:08:55 +0100292// rust_ffi_shared produces a shared library (Rust crate type
293// "cdylib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700294func RustFFISharedFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700295 module, library := NewRustLibrary(android.HostAndDeviceSupported)
296 library.BuildOnlyShared()
297 return module.Init()
298}
299
Martin Geisler67ec0542022-11-18 12:08:55 +0100300// rust_ffi_static produces a static library (Rust crate type
301// "staticlib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700302func RustFFIStaticFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700303 module, library := NewRustLibrary(android.HostAndDeviceSupported)
304 library.BuildOnlyStatic()
305 return module.Init()
306}
307
Martin Geisler67ec0542022-11-18 12:08:55 +0100308// rust_library_host produces all Rust variants for the host
309// (rust_library_dylib_host and rust_library_rlib_host).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700310func RustLibraryHostFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700311 module, library := NewRustLibrary(android.HostSupported)
312 library.BuildOnlyRust()
313 return module.Init()
314}
315
Martin Geisler67ec0542022-11-18 12:08:55 +0100316// rust_ffi_host produces all FFI variants for the host
317// (rust_ffi_static_host and rust_ffi_shared_host).
Matthew Maurer2ae05132020-06-23 14:28:53 -0700318func RustFFIHostFactory() android.Module {
319 module, library := NewRustLibrary(android.HostSupported)
320 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700321 return module.Init()
322}
323
Martin Geisler67ec0542022-11-18 12:08:55 +0100324// rust_library_dylib_host produces a dylib for the host (Rust crate
325// type "dylib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700326func RustLibraryDylibHostFactory() android.Module {
327 module, library := NewRustLibrary(android.HostSupported)
328 library.BuildOnlyDylib()
329 return module.Init()
330}
331
Martin Geisler67ec0542022-11-18 12:08:55 +0100332// rust_library_rlib_host produces an rlib for the host (Rust crate
333// type "rlib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700334func RustLibraryRlibHostFactory() android.Module {
335 module, library := NewRustLibrary(android.HostSupported)
336 library.BuildOnlyRlib()
337 return module.Init()
338}
339
Martin Geisler67ec0542022-11-18 12:08:55 +0100340// rust_ffi_static_host produces a static library for the host (Rust
341// crate type "staticlib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700342func RustFFIStaticHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700343 module, library := NewRustLibrary(android.HostSupported)
344 library.BuildOnlyStatic()
345 return module.Init()
346}
347
Martin Geisler67ec0542022-11-18 12:08:55 +0100348// rust_ffi_shared_host produces an shared library for the host (Rust
349// crate type "cdylib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700350func RustFFISharedHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700351 module, library := NewRustLibrary(android.HostSupported)
352 library.BuildOnlyShared()
353 return module.Init()
354}
355
Matthew Maurer2ae05132020-06-23 14:28:53 -0700356func (library *libraryDecorator) BuildOnlyFFI() {
357 library.MutatedProperties.BuildDylib = false
358 library.MutatedProperties.BuildRlib = false
359 library.MutatedProperties.BuildShared = true
360 library.MutatedProperties.BuildStatic = true
361}
362
363func (library *libraryDecorator) BuildOnlyRust() {
364 library.MutatedProperties.BuildDylib = true
365 library.MutatedProperties.BuildRlib = true
366 library.MutatedProperties.BuildShared = false
367 library.MutatedProperties.BuildStatic = false
368}
369
Ivan Lozanoffee3342019-08-27 12:03:00 -0700370func (library *libraryDecorator) BuildOnlyDylib() {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700371 library.MutatedProperties.BuildDylib = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700372 library.MutatedProperties.BuildRlib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700373 library.MutatedProperties.BuildShared = false
374 library.MutatedProperties.BuildStatic = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700375}
376
377func (library *libraryDecorator) BuildOnlyRlib() {
378 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700379 library.MutatedProperties.BuildRlib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700380 library.MutatedProperties.BuildShared = false
381 library.MutatedProperties.BuildStatic = false
382}
383
384func (library *libraryDecorator) BuildOnlyStatic() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700385 library.MutatedProperties.BuildRlib = false
386 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700387 library.MutatedProperties.BuildShared = false
388 library.MutatedProperties.BuildStatic = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700389}
390
391func (library *libraryDecorator) BuildOnlyShared() {
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.BuildStatic = false
395 library.MutatedProperties.BuildShared = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700396}
397
398func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400399 module := newModule(hod, android.MultilibBoth)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700400
401 library := &libraryDecorator{
402 MutatedProperties: LibraryMutatedProperties{
Matthew Maurer2ae05132020-06-23 14:28:53 -0700403 BuildDylib: false,
404 BuildRlib: false,
405 BuildShared: false,
406 BuildStatic: false,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700407 },
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800408 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Matthew Maurerbb3add12020-06-25 09:34:12 -0700409 flagExporter: NewFlagExporter(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700410 }
411
412 module.compiler = library
413
414 return module, library
415}
416
417func (library *libraryDecorator) compilerProps() []interface{} {
418 return append(library.baseCompiler.compilerProps(),
419 &library.Properties,
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200420 &library.MutatedProperties,
421 &library.stripper.StripProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700422}
423
Ivan Lozanof1c84332019-09-20 11:00:37 -0700424func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
425 deps = library.baseCompiler.compilerDeps(ctx, deps)
426
Colin Crosse32f0932022-01-23 20:48:36 -0800427 if library.dylib() || library.shared() {
428 if ctx.toolchain().Bionic() {
429 deps = bionicDeps(ctx, deps, false)
430 deps.CrtBegin = []string{"crtbegin_so"}
431 deps.CrtEnd = []string{"crtend_so"}
432 } else if ctx.Os() == android.LinuxMusl {
433 deps = muslDeps(ctx, deps, false)
434 deps.CrtBegin = []string{"libc_musl_crtbegin_so"}
435 deps.CrtEnd = []string{"libc_musl_crtend_so"}
436 }
Ivan Lozanof1c84332019-09-20 11:00:37 -0700437 }
438
439 return deps
440}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400441
442func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string {
443 return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
444}
445
Ivan Lozano67eada32021-09-23 11:50:33 -0400446func (library *libraryDecorator) cfgFlags(ctx ModuleContext, flags Flags) Flags {
447 flags = library.baseCompiler.cfgFlags(ctx, flags)
Stephen Crane0dbfc562021-07-07 19:05:02 -0700448 if library.dylib() {
449 // We need to add a dependency on std in order to link crates as dylibs.
450 // The hack to add this dependency is guarded by the following cfg so
451 // that we don't force a dependency when it isn't needed.
452 library.baseCompiler.Properties.Cfgs = append(library.baseCompiler.Properties.Cfgs, "android_dylib")
453 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400454
455 flags.RustFlags = append(flags.RustFlags, library.baseCompiler.cfgsToFlags()...)
456 flags.RustdocFlags = append(flags.RustdocFlags, library.baseCompiler.cfgsToFlags()...)
457
458 return flags
459}
460
461func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800462 flags = library.baseCompiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400463
464 flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800465 if library.shared() || library.static() {
466 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
467 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400468 if library.shared() {
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -0700469 if ctx.Darwin() {
470 flags.LinkFlags = append(
471 flags.LinkFlags,
472 "-dynamic_lib",
473 "-install_name @rpath/"+library.sharedLibFilename(ctx),
474 )
475 } else {
476 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
477 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400478 }
479
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800480 return flags
481}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700482
Sasha Smundaka76acba2022-04-18 20:12:56 -0700483func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
484 var outputFile android.ModuleOutPath
485 var ret buildOutput
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200486 var fileName string
Dan Albert06feee92021-03-19 15:06:02 -0700487 srcPath := library.srcPath(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700488
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400489 if library.sourceProvider != nil {
Ivan Lozano9d74a522020-12-01 09:25:22 -0500490 deps.srcProviderFiles = append(deps.srcProviderFiles, library.sourceProvider.Srcs()...)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400491 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700492
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400493 // Calculate output filename
494 if library.rlib() {
495 fileName = library.getStem(ctx) + ctx.toolchain().RlibSuffix()
496 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700497 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400498 } else if library.dylib() {
499 fileName = library.getStem(ctx) + ctx.toolchain().DylibSuffix()
500 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700501 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400502 } else if library.static() {
503 fileName = library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
504 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700505 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400506 } else if library.shared() {
507 fileName = library.sharedLibFilename(ctx)
508 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700509 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400510 }
511
512 if !library.rlib() && !library.static() && library.stripper.NeedsStrip(ctx) {
513 strippedOutputFile := outputFile
514 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
515 library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
516
517 library.baseCompiler.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
518 }
519 library.baseCompiler.unstrippedOutputFile = outputFile
520
Ivan Lozanoffee3342019-08-27 12:03:00 -0700521 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500522 flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...)
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700523 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects.Strings()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700524
Matthew Maurer46c46cc2020-01-13 16:34:34 -0800525 if library.dylib() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700526 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
527 // https://github.com/rust-lang/rust/issues/19680
528 // https://github.com/rust-lang/rust/issues/34909
529 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
530 }
531
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400532 // Call the appropriate builder for this library type
Ivan Lozanoffee3342019-08-27 12:03:00 -0700533 if library.rlib() {
Sasha Smundaka76acba2022-04-18 20:12:56 -0700534 ret.kytheFile = TransformSrctoRlib(ctx, srcPath, deps, flags, outputFile).kytheFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700535 } else if library.dylib() {
Sasha Smundaka76acba2022-04-18 20:12:56 -0700536 ret.kytheFile = TransformSrctoDylib(ctx, srcPath, deps, flags, outputFile).kytheFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700537 } else if library.static() {
Sasha Smundaka76acba2022-04-18 20:12:56 -0700538 ret.kytheFile = TransformSrctoStatic(ctx, srcPath, deps, flags, outputFile).kytheFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700539 } else if library.shared() {
Sasha Smundaka76acba2022-04-18 20:12:56 -0700540 ret.kytheFile = TransformSrctoShared(ctx, srcPath, deps, flags, outputFile).kytheFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700541 }
542
Ivan Lozano52767be2019-10-18 14:49:46 -0700543 if library.rlib() || library.dylib() {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700544 library.flagExporter.exportLinkDirs(deps.linkDirs...)
Colin Cross0de8a1e2020-09-18 14:15:30 -0700545 library.flagExporter.exportLinkObjects(deps.linkObjects...)
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700546 library.flagExporter.exportLibDeps(deps.LibDeps...)
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() {
550 ctx.SetProvider(cc.FlagExporterInfoProvider, cc.FlagExporterInfo{
551 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 Cross0de8a1e2020-09-18 14:15:30 -0700562 ctx.SetProvider(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 Cross0de8a1e2020-09-18 14:15:30 -0700571 ctx.SetProvider(cc.StaticLibraryInfoProvider, cc.StaticLibraryInfo{
572 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
Sasha Smundaka76acba2022-04-18 20:12:56 -0700583func (library *libraryDecorator) srcPath(ctx ModuleContext, _ PathDeps) android.Path {
Dan Albert06feee92021-03-19 15:06:02 -0700584 if library.sourceProvider != nil {
585 // Assume the first source from the source provider is the library entry point.
586 return library.sourceProvider.Srcs()[0]
587 } else {
588 path, _ := srcPathFromModuleSrcs(ctx, library.baseCompiler.Properties.Srcs)
589 return path
590 }
591}
592
593func (library *libraryDecorator) rustdoc(ctx ModuleContext, flags Flags,
594 deps PathDeps) android.OptionalPath {
595 // rustdoc has builtin support for documenting config specific information
596 // regardless of the actual config it was given
597 // (https://doc.rust-lang.org/rustdoc/advanced-features.html#cfgdoc-documenting-platform-specific-or-feature-specific-information),
598 // so we generate the rustdoc for only the primary module so that we have a
599 // single set of docs to refer to.
600 if ctx.Module() != ctx.PrimaryModule() {
601 return android.OptionalPath{}
602 }
603
604 return android.OptionalPathForPath(Rustdoc(ctx, library.srcPath(ctx, deps),
605 deps, flags))
606}
607
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700608func (library *libraryDecorator) getStem(ctx ModuleContext) string {
609 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
610 validateLibraryStem(ctx, stem, library.crateName())
611
612 return stem + String(library.baseCompiler.Properties.Suffix)
613}
614
Ivan Lozano2b081132020-09-08 12:46:52 -0400615func (library *libraryDecorator) install(ctx ModuleContext) {
616 // Only shared and dylib variants make sense to install.
617 if library.shared() || library.dylib() {
618 library.baseCompiler.install(ctx)
619 }
620}
621
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400622func (library *libraryDecorator) Disabled() bool {
623 return library.MutatedProperties.VariantIsDisabled
624}
625
626func (library *libraryDecorator) SetDisabled() {
627 library.MutatedProperties.VariantIsDisabled = true
628}
629
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700630var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
631
632func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
633 if crate_name == "" {
634 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
635 }
636
637 // crate_names are used for the library output file, and rustc expects these
638 // to be alphanumeric with underscores allowed.
639 if validCrateName.MatchString(crate_name) {
640 ctx.PropertyErrorf("crate_name",
641 "library crate_names must be alphanumeric with underscores allowed")
642 }
643
644 // Libraries are expected to begin with "lib" followed by the crate_name
645 if !strings.HasPrefix(filename, "lib"+crate_name) {
646 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
647 }
648}
649
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200650// LibraryMutator mutates the libraries into variants according to the
651// build{Rlib,Dylib} attributes.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700652func LibraryMutator(mctx android.BottomUpMutatorContext) {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200653 // Only mutate on Rust libraries.
654 m, ok := mctx.Module().(*Module)
655 if !ok || m.compiler == nil {
656 return
657 }
658 library, ok := m.compiler.(libraryInterface)
659 if !ok {
660 return
661 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400662
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200663 var variants []string
664 // The source variant is used for SourceProvider modules. The other variants (i.e. rlib and dylib)
665 // depend on this variant. It must be the first variant to be declared.
666 sourceVariant := false
667 if m.sourceProvider != nil {
668 variants = append(variants, "source")
669 sourceVariant = true
670 }
671 if library.buildRlib() {
672 variants = append(variants, rlibVariation)
673 }
674 if library.buildDylib() {
675 variants = append(variants, dylibVariation)
676 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400677
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200678 if len(variants) == 0 {
679 return
680 }
681 modules := mctx.CreateLocalVariations(variants...)
682
683 // The order of the variations (modules) matches the variant names provided. Iterate
684 // through the new variation modules and set their mutated properties.
685 for i, v := range modules {
686 switch variants[i] {
687 case rlibVariation:
688 v.(*Module).compiler.(libraryInterface).setRlib()
689 case dylibVariation:
690 v.(*Module).compiler.(libraryInterface).setDylib()
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400691 if v.(*Module).ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
Ivan Lozano6a884432020-12-02 09:15:16 -0500692 // TODO(b/165791368)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400693 // Disable dylib Vendor Ramdisk variations until we support these.
Ivan Lozano6a884432020-12-02 09:15:16 -0500694 v.(*Module).Disable()
695 }
Ivan Lozano1921e802021-05-20 13:39:16 -0400696
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200697 case "source":
698 v.(*Module).compiler.(libraryInterface).setSource()
699 // The source variant does not produce any library.
700 // Disable the compilation steps.
701 v.(*Module).compiler.SetDisabled()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700702 }
703 }
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200704
705 // If a source variant is created, add an inter-variant dependency
706 // between the other variants and the source variant.
707 if sourceVariant {
708 sv := modules[0]
709 for _, v := range modules[1:] {
710 if !v.Enabled() {
711 continue
712 }
713 mctx.AddInterVariantDependency(sourceDepTag, v, sv)
714 }
715 // Alias the source variation so it can be named directly in "srcs" properties.
716 mctx.AliasVariation("source")
717 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700718}
Ivan Lozano2b081132020-09-08 12:46:52 -0400719
720func LibstdMutator(mctx android.BottomUpMutatorContext) {
721 if m, ok := mctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() {
722 switch library := m.compiler.(type) {
723 case libraryInterface:
724 // Only create a variant if a library is actually being built.
725 if library.rlib() && !library.sysroot() {
726 variants := []string{"rlib-std", "dylib-std"}
727 modules := mctx.CreateLocalVariations(variants...)
728
729 rlib := modules[0].(*Module)
730 dylib := modules[1].(*Module)
731 rlib.compiler.(libraryInterface).setRlibStd()
732 dylib.compiler.(libraryInterface).setDylibStd()
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400733 if dylib.ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
Ivan Lozano6a884432020-12-02 09:15:16 -0500734 // TODO(b/165791368)
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400735 // Disable rlibs that link against dylib-std on vendor ramdisk variations until those dylib
Ivan Lozano6a884432020-12-02 09:15:16 -0500736 // variants are properly supported.
737 dylib.Disable()
738 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400739 rlib.Properties.RustSubName += RlibStdlibSuffix
Ivan Lozano2b081132020-09-08 12:46:52 -0400740 }
741 }
742 }
743}
Ivan Lozano1921e802021-05-20 13:39:16 -0400744
745func (l *libraryDecorator) snapshotHeaders() android.Paths {
746 if l.collectedSnapshotHeaders == nil {
747 panic("snapshotHeaders() must be called after collectHeadersForSnapshot()")
748 }
749 return l.collectedSnapshotHeaders
750}
751
752// collectHeadersForSnapshot collects all exported headers from library.
753// It globs header files in the source tree for exported include directories,
754// and tracks generated header files separately.
755//
756// This is to be called from GenerateAndroidBuildActions, and then collected
757// header files can be retrieved by snapshotHeaders().
758func (l *libraryDecorator) collectHeadersForSnapshot(ctx android.ModuleContext, deps PathDeps) {
759 ret := android.Paths{}
760
761 // Glob together the headers from the modules include_dirs property
762 for _, path := range android.CopyOfPaths(l.includeDirs) {
763 dir := path.String()
Liz Kammer0ea79982022-02-07 08:51:47 -0500764 globDir := dir + "/**/*"
765 glob, err := ctx.GlobWithDeps(globDir, nil)
Ivan Lozano1921e802021-05-20 13:39:16 -0400766 if err != nil {
Liz Kammer0ea79982022-02-07 08:51:47 -0500767 ctx.ModuleErrorf("glob of %q failed: %s", globDir, err)
Ivan Lozano1921e802021-05-20 13:39:16 -0400768 return
769 }
770
771 for _, header := range glob {
772 // Filter out only the files with extensions that are headers.
773 found := false
774 for _, ext := range cc.HeaderExts {
775 if strings.HasSuffix(header, ext) {
776 found = true
777 break
778 }
779 }
780 if !found {
781 continue
782 }
783 ret = append(ret, android.PathForSource(ctx, header))
784 }
785 }
786
787 // Glob together the headers from C dependencies as well, starting with non-generated headers.
788 ret = append(ret, cc.GlobHeadersForSnapshot(ctx, append(android.CopyOfPaths(deps.depIncludePaths), deps.depSystemIncludePaths...))...)
789
790 // Collect generated headers from C dependencies.
791 ret = append(ret, cc.GlobGeneratedHeadersForSnapshot(ctx, deps.depGeneratedHeaders)...)
792
793 // TODO(185577950): If support for generated headers is added, they need to be collected here as well.
794 l.collectedSnapshotHeaders = ret
795}