blob: 3f480a259eefc3ec7fe8605acc4b90b8a8bedbf8 [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() {
469 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
470 }
471
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800472 return flags
473}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700474
Sasha Smundaka76acba2022-04-18 20:12:56 -0700475func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
476 var outputFile android.ModuleOutPath
477 var ret buildOutput
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200478 var fileName string
Dan Albert06feee92021-03-19 15:06:02 -0700479 srcPath := library.srcPath(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700480
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400481 if library.sourceProvider != nil {
Ivan Lozano9d74a522020-12-01 09:25:22 -0500482 deps.srcProviderFiles = append(deps.srcProviderFiles, library.sourceProvider.Srcs()...)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400483 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700484
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400485 // Calculate output filename
486 if library.rlib() {
487 fileName = library.getStem(ctx) + ctx.toolchain().RlibSuffix()
488 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700489 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400490 } else if library.dylib() {
491 fileName = library.getStem(ctx) + ctx.toolchain().DylibSuffix()
492 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700493 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400494 } else if library.static() {
495 fileName = library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
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.shared() {
499 fileName = library.sharedLibFilename(ctx)
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 }
503
504 if !library.rlib() && !library.static() && library.stripper.NeedsStrip(ctx) {
505 strippedOutputFile := outputFile
506 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
507 library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
508
509 library.baseCompiler.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
510 }
511 library.baseCompiler.unstrippedOutputFile = outputFile
512
Ivan Lozanoffee3342019-08-27 12:03:00 -0700513 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500514 flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...)
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700515 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects.Strings()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700516
Matthew Maurer46c46cc2020-01-13 16:34:34 -0800517 if library.dylib() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700518 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
519 // https://github.com/rust-lang/rust/issues/19680
520 // https://github.com/rust-lang/rust/issues/34909
521 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
522 }
523
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400524 // Call the appropriate builder for this library type
Ivan Lozanoffee3342019-08-27 12:03:00 -0700525 if library.rlib() {
Sasha Smundaka76acba2022-04-18 20:12:56 -0700526 ret.kytheFile = TransformSrctoRlib(ctx, srcPath, deps, flags, outputFile).kytheFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700527 } else if library.dylib() {
Sasha Smundaka76acba2022-04-18 20:12:56 -0700528 ret.kytheFile = TransformSrctoDylib(ctx, srcPath, deps, flags, outputFile).kytheFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700529 } else if library.static() {
Sasha Smundaka76acba2022-04-18 20:12:56 -0700530 ret.kytheFile = TransformSrctoStatic(ctx, srcPath, deps, flags, outputFile).kytheFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700531 } else if library.shared() {
Sasha Smundaka76acba2022-04-18 20:12:56 -0700532 ret.kytheFile = TransformSrctoShared(ctx, srcPath, deps, flags, outputFile).kytheFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700533 }
534
Ivan Lozano52767be2019-10-18 14:49:46 -0700535 if library.rlib() || library.dylib() {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700536 library.flagExporter.exportLinkDirs(deps.linkDirs...)
Colin Cross0de8a1e2020-09-18 14:15:30 -0700537 library.flagExporter.exportLinkObjects(deps.linkObjects...)
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700538 library.flagExporter.exportLibDeps(deps.LibDeps...)
Ivan Lozano52767be2019-10-18 14:49:46 -0700539 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700540
Colin Cross0de8a1e2020-09-18 14:15:30 -0700541 if library.static() || library.shared() {
542 ctx.SetProvider(cc.FlagExporterInfoProvider, cc.FlagExporterInfo{
543 IncludeDirs: library.includeDirs,
544 })
545 }
546
547 if library.shared() {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400548 // Optimize out relinking against shared libraries whose interface hasn't changed by
549 // depending on a table of contents file instead of the library itself.
550 tocFile := outputFile.ReplaceExtension(ctx, flags.Toolchain.SharedLibSuffix()[1:]+".toc")
551 library.tocFile = android.OptionalPathForPath(tocFile)
552 cc.TransformSharedObjectToToc(ctx, outputFile, tocFile)
553
Colin Cross0de8a1e2020-09-18 14:15:30 -0700554 ctx.SetProvider(cc.SharedLibraryInfoProvider, cc.SharedLibraryInfo{
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400555 TableOfContents: android.OptionalPathForPath(tocFile),
556 SharedLibrary: outputFile,
557 Target: ctx.Target(),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700558 })
559 }
560
561 if library.static() {
Colin Crossc85750b2022-04-21 12:50:51 -0700562 depSet := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).Direct(outputFile).Build()
Colin Cross0de8a1e2020-09-18 14:15:30 -0700563 ctx.SetProvider(cc.StaticLibraryInfoProvider, cc.StaticLibraryInfo{
564 StaticLibrary: outputFile,
565
566 TransitiveStaticLibrariesForOrdering: depSet,
567 })
568 }
569
570 library.flagExporter.setProvider(ctx)
571
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400572 return ret
Ivan Lozanoffee3342019-08-27 12:03:00 -0700573}
574
Sasha Smundaka76acba2022-04-18 20:12:56 -0700575func (library *libraryDecorator) srcPath(ctx ModuleContext, _ PathDeps) android.Path {
Dan Albert06feee92021-03-19 15:06:02 -0700576 if library.sourceProvider != nil {
577 // Assume the first source from the source provider is the library entry point.
578 return library.sourceProvider.Srcs()[0]
579 } else {
580 path, _ := srcPathFromModuleSrcs(ctx, library.baseCompiler.Properties.Srcs)
581 return path
582 }
583}
584
585func (library *libraryDecorator) rustdoc(ctx ModuleContext, flags Flags,
586 deps PathDeps) android.OptionalPath {
587 // rustdoc has builtin support for documenting config specific information
588 // regardless of the actual config it was given
589 // (https://doc.rust-lang.org/rustdoc/advanced-features.html#cfgdoc-documenting-platform-specific-or-feature-specific-information),
590 // so we generate the rustdoc for only the primary module so that we have a
591 // single set of docs to refer to.
592 if ctx.Module() != ctx.PrimaryModule() {
593 return android.OptionalPath{}
594 }
595
596 return android.OptionalPathForPath(Rustdoc(ctx, library.srcPath(ctx, deps),
597 deps, flags))
598}
599
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700600func (library *libraryDecorator) getStem(ctx ModuleContext) string {
601 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
602 validateLibraryStem(ctx, stem, library.crateName())
603
604 return stem + String(library.baseCompiler.Properties.Suffix)
605}
606
Ivan Lozano2b081132020-09-08 12:46:52 -0400607func (library *libraryDecorator) install(ctx ModuleContext) {
608 // Only shared and dylib variants make sense to install.
609 if library.shared() || library.dylib() {
610 library.baseCompiler.install(ctx)
611 }
612}
613
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400614func (library *libraryDecorator) Disabled() bool {
615 return library.MutatedProperties.VariantIsDisabled
616}
617
618func (library *libraryDecorator) SetDisabled() {
619 library.MutatedProperties.VariantIsDisabled = true
620}
621
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700622var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
623
624func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
625 if crate_name == "" {
626 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
627 }
628
629 // crate_names are used for the library output file, and rustc expects these
630 // to be alphanumeric with underscores allowed.
631 if validCrateName.MatchString(crate_name) {
632 ctx.PropertyErrorf("crate_name",
633 "library crate_names must be alphanumeric with underscores allowed")
634 }
635
636 // Libraries are expected to begin with "lib" followed by the crate_name
637 if !strings.HasPrefix(filename, "lib"+crate_name) {
638 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
639 }
640}
641
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200642// LibraryMutator mutates the libraries into variants according to the
643// build{Rlib,Dylib} attributes.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700644func LibraryMutator(mctx android.BottomUpMutatorContext) {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200645 // Only mutate on Rust libraries.
646 m, ok := mctx.Module().(*Module)
647 if !ok || m.compiler == nil {
648 return
649 }
650 library, ok := m.compiler.(libraryInterface)
651 if !ok {
652 return
653 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400654
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200655 var variants []string
656 // The source variant is used for SourceProvider modules. The other variants (i.e. rlib and dylib)
657 // depend on this variant. It must be the first variant to be declared.
658 sourceVariant := false
659 if m.sourceProvider != nil {
660 variants = append(variants, "source")
661 sourceVariant = true
662 }
663 if library.buildRlib() {
664 variants = append(variants, rlibVariation)
665 }
666 if library.buildDylib() {
667 variants = append(variants, dylibVariation)
668 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400669
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200670 if len(variants) == 0 {
671 return
672 }
673 modules := mctx.CreateLocalVariations(variants...)
674
675 // The order of the variations (modules) matches the variant names provided. Iterate
676 // through the new variation modules and set their mutated properties.
677 for i, v := range modules {
678 switch variants[i] {
679 case rlibVariation:
680 v.(*Module).compiler.(libraryInterface).setRlib()
681 case dylibVariation:
682 v.(*Module).compiler.(libraryInterface).setDylib()
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400683 if v.(*Module).ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
Ivan Lozano6a884432020-12-02 09:15:16 -0500684 // TODO(b/165791368)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400685 // Disable dylib Vendor Ramdisk variations until we support these.
Ivan Lozano6a884432020-12-02 09:15:16 -0500686 v.(*Module).Disable()
687 }
Ivan Lozano1921e802021-05-20 13:39:16 -0400688
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200689 case "source":
690 v.(*Module).compiler.(libraryInterface).setSource()
691 // The source variant does not produce any library.
692 // Disable the compilation steps.
693 v.(*Module).compiler.SetDisabled()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700694 }
695 }
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200696
697 // If a source variant is created, add an inter-variant dependency
698 // between the other variants and the source variant.
699 if sourceVariant {
700 sv := modules[0]
701 for _, v := range modules[1:] {
702 if !v.Enabled() {
703 continue
704 }
705 mctx.AddInterVariantDependency(sourceDepTag, v, sv)
706 }
707 // Alias the source variation so it can be named directly in "srcs" properties.
708 mctx.AliasVariation("source")
709 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700710}
Ivan Lozano2b081132020-09-08 12:46:52 -0400711
712func LibstdMutator(mctx android.BottomUpMutatorContext) {
713 if m, ok := mctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() {
714 switch library := m.compiler.(type) {
715 case libraryInterface:
716 // Only create a variant if a library is actually being built.
717 if library.rlib() && !library.sysroot() {
718 variants := []string{"rlib-std", "dylib-std"}
719 modules := mctx.CreateLocalVariations(variants...)
720
721 rlib := modules[0].(*Module)
722 dylib := modules[1].(*Module)
723 rlib.compiler.(libraryInterface).setRlibStd()
724 dylib.compiler.(libraryInterface).setDylibStd()
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400725 if dylib.ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
Ivan Lozano6a884432020-12-02 09:15:16 -0500726 // TODO(b/165791368)
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400727 // Disable rlibs that link against dylib-std on vendor ramdisk variations until those dylib
Ivan Lozano6a884432020-12-02 09:15:16 -0500728 // variants are properly supported.
729 dylib.Disable()
730 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400731 rlib.Properties.RustSubName += RlibStdlibSuffix
Ivan Lozano2b081132020-09-08 12:46:52 -0400732 }
733 }
734 }
735}
Ivan Lozano1921e802021-05-20 13:39:16 -0400736
737func (l *libraryDecorator) snapshotHeaders() android.Paths {
738 if l.collectedSnapshotHeaders == nil {
739 panic("snapshotHeaders() must be called after collectHeadersForSnapshot()")
740 }
741 return l.collectedSnapshotHeaders
742}
743
744// collectHeadersForSnapshot collects all exported headers from library.
745// It globs header files in the source tree for exported include directories,
746// and tracks generated header files separately.
747//
748// This is to be called from GenerateAndroidBuildActions, and then collected
749// header files can be retrieved by snapshotHeaders().
750func (l *libraryDecorator) collectHeadersForSnapshot(ctx android.ModuleContext, deps PathDeps) {
751 ret := android.Paths{}
752
753 // Glob together the headers from the modules include_dirs property
754 for _, path := range android.CopyOfPaths(l.includeDirs) {
755 dir := path.String()
Liz Kammer0ea79982022-02-07 08:51:47 -0500756 globDir := dir + "/**/*"
757 glob, err := ctx.GlobWithDeps(globDir, nil)
Ivan Lozano1921e802021-05-20 13:39:16 -0400758 if err != nil {
Liz Kammer0ea79982022-02-07 08:51:47 -0500759 ctx.ModuleErrorf("glob of %q failed: %s", globDir, err)
Ivan Lozano1921e802021-05-20 13:39:16 -0400760 return
761 }
762
763 for _, header := range glob {
764 // Filter out only the files with extensions that are headers.
765 found := false
766 for _, ext := range cc.HeaderExts {
767 if strings.HasSuffix(header, ext) {
768 found = true
769 break
770 }
771 }
772 if !found {
773 continue
774 }
775 ret = append(ret, android.PathForSource(ctx, header))
776 }
777 }
778
779 // Glob together the headers from C dependencies as well, starting with non-generated headers.
780 ret = append(ret, cc.GlobHeadersForSnapshot(ctx, append(android.CopyOfPaths(deps.depIncludePaths), deps.depSystemIncludePaths...))...)
781
782 // Collect generated headers from C dependencies.
783 ret = append(ret, cc.GlobGeneratedHeadersForSnapshot(ctx, deps.depGeneratedHeaders)...)
784
785 // TODO(185577950): If support for generated headers is added, they need to be collected here as well.
786 l.collectedSnapshotHeaders = ret
787}