blob: 5e445a92e0d6ee26546ac713f7bcc14bb722bf64 [file] [log] [blame]
Ivan Lozanoffee3342019-08-27 12:03:00 -07001// Copyright 2019 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package rust
16
17import (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +020018 "fmt"
Ivan Lozanoad8b18b2019-10-31 19:38:29 -070019 "regexp"
20 "strings"
21
Ivan Lozanoffee3342019-08-27 12:03:00 -070022 "android/soong/android"
Colin Cross0de8a1e2020-09-18 14:15:30 -070023 "android/soong/cc"
Kiyoung Kim48f37782021-07-07 12:42:39 +090024 "android/soong/snapshot"
Ivan Lozanoffee3342019-08-27 12:03:00 -070025)
26
Ivan Lozano2b081132020-09-08 12:46:52 -040027var (
28 DylibStdlibSuffix = ".dylib-std"
29 RlibStdlibSuffix = ".rlib-std"
30)
31
Ivan Lozanoffee3342019-08-27 12:03:00 -070032func init() {
33 android.RegisterModuleType("rust_library", RustLibraryFactory)
34 android.RegisterModuleType("rust_library_dylib", RustLibraryDylibFactory)
35 android.RegisterModuleType("rust_library_rlib", RustLibraryRlibFactory)
36 android.RegisterModuleType("rust_library_host", RustLibraryHostFactory)
37 android.RegisterModuleType("rust_library_host_dylib", RustLibraryDylibHostFactory)
38 android.RegisterModuleType("rust_library_host_rlib", RustLibraryRlibHostFactory)
Matthew Maurer2ae05132020-06-23 14:28:53 -070039 android.RegisterModuleType("rust_ffi", RustFFIFactory)
40 android.RegisterModuleType("rust_ffi_shared", RustFFISharedFactory)
41 android.RegisterModuleType("rust_ffi_static", RustFFIStaticFactory)
42 android.RegisterModuleType("rust_ffi_host", RustFFIHostFactory)
43 android.RegisterModuleType("rust_ffi_host_shared", RustFFISharedHostFactory)
44 android.RegisterModuleType("rust_ffi_host_static", RustFFIStaticHostFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070045}
46
47type VariantLibraryProperties struct {
Matthew Maurerc761eec2020-06-25 00:47:46 -070048 Enabled *bool `android:"arch_variant"`
49 Srcs []string `android:"path,arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070050}
51
52type LibraryCompilerProperties struct {
Ivan Lozano52767be2019-10-18 14:49:46 -070053 Rlib VariantLibraryProperties `android:"arch_variant"`
54 Dylib VariantLibraryProperties `android:"arch_variant"`
55 Shared VariantLibraryProperties `android:"arch_variant"`
56 Static VariantLibraryProperties `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070057
Ivan Lozano52767be2019-10-18 14:49:46 -070058 // path to include directories to pass to cc_* modules, only relevant for static/shared variants.
59 Include_dirs []string `android:"path,arch_variant"`
Ivan Lozano2b081132020-09-08 12:46:52 -040060
61 // Whether this library is part of the Rust toolchain sysroot.
62 Sysroot *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070063}
64
65type LibraryMutatedProperties struct {
Ivan Lozanoffee3342019-08-27 12:03:00 -070066 // Build a dylib variant
67 BuildDylib bool `blueprint:"mutated"`
68 // Build an rlib variant
69 BuildRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070070 // Build a shared library variant
71 BuildShared bool `blueprint:"mutated"`
72 // Build a static library variant
73 BuildStatic bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070074
75 // This variant is a dylib
76 VariantIsDylib bool `blueprint:"mutated"`
77 // This variant is an rlib
78 VariantIsRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070079 // This variant is a shared library
80 VariantIsShared bool `blueprint:"mutated"`
81 // This variant is a static library
82 VariantIsStatic bool `blueprint:"mutated"`
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +020083 // This variant is a source provider
84 VariantIsSource bool `blueprint:"mutated"`
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040085
86 // This variant is disabled and should not be compiled
87 // (used for SourceProvider variants that produce only source)
88 VariantIsDisabled bool `blueprint:"mutated"`
Ivan Lozano2b081132020-09-08 12:46:52 -040089
90 // Whether this library variant should be link libstd via rlibs
91 VariantIsStaticStd bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070092}
93
94type libraryDecorator struct {
95 *baseCompiler
Matthew Maurerbb3add12020-06-25 09:34:12 -070096 *flagExporter
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +020097 stripper Stripper
Ivan Lozanoffee3342019-08-27 12:03:00 -070098
Ivan Lozano8a23fa42020-06-16 10:26:57 -040099 Properties LibraryCompilerProperties
100 MutatedProperties LibraryMutatedProperties
101 includeDirs android.Paths
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400102 sourceProvider SourceProvider
Ivan Lozano1921e802021-05-20 13:39:16 -0400103
104 collectedSnapshotHeaders android.Paths
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400105
106 // table-of-contents file for cdylib crates to optimize out relinking when possible
107 tocFile android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700108}
109
110type libraryInterface interface {
111 rlib() bool
112 dylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700113 static() bool
114 shared() bool
Ivan Lozano2b081132020-09-08 12:46:52 -0400115 sysroot() bool
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200116 source() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700117
118 // Returns true if the build options for the module have selected a particular build type
119 buildRlib() bool
120 buildDylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700121 buildShared() bool
122 buildStatic() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700123
124 // Sets a particular variant type
125 setRlib()
126 setDylib()
Ivan Lozano52767be2019-10-18 14:49:46 -0700127 setShared()
128 setStatic()
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200129 setSource()
Ivan Lozano52767be2019-10-18 14:49:46 -0700130
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400131 // libstd linkage functions
132 rlibStd() bool
Ivan Lozano2b081132020-09-08 12:46:52 -0400133 setRlibStd()
134 setDylibStd()
135
Ivan Lozano52767be2019-10-18 14:49:46 -0700136 // Build a specific library variant
Matthew Maurer2ae05132020-06-23 14:28:53 -0700137 BuildOnlyFFI()
138 BuildOnlyRust()
Ivan Lozano52767be2019-10-18 14:49:46 -0700139 BuildOnlyRlib()
140 BuildOnlyDylib()
141 BuildOnlyStatic()
142 BuildOnlyShared()
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400143
144 toc() android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700145}
146
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400147func (library *libraryDecorator) nativeCoverage() bool {
148 return true
149}
150
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400151func (library *libraryDecorator) toc() android.OptionalPath {
152 return library.tocFile
153}
154
Ivan Lozanoffee3342019-08-27 12:03:00 -0700155func (library *libraryDecorator) rlib() bool {
156 return library.MutatedProperties.VariantIsRlib
157}
158
Ivan Lozano2b081132020-09-08 12:46:52 -0400159func (library *libraryDecorator) sysroot() bool {
160 return Bool(library.Properties.Sysroot)
161}
162
Ivan Lozanoffee3342019-08-27 12:03:00 -0700163func (library *libraryDecorator) dylib() bool {
164 return library.MutatedProperties.VariantIsDylib
165}
166
Ivan Lozano52767be2019-10-18 14:49:46 -0700167func (library *libraryDecorator) shared() bool {
168 return library.MutatedProperties.VariantIsShared
169}
170
171func (library *libraryDecorator) static() bool {
172 return library.MutatedProperties.VariantIsStatic
173}
174
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200175func (library *libraryDecorator) source() bool {
176 return library.MutatedProperties.VariantIsSource
177}
178
Ivan Lozanoffee3342019-08-27 12:03:00 -0700179func (library *libraryDecorator) buildRlib() bool {
180 return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true)
181}
182
183func (library *libraryDecorator) buildDylib() bool {
184 return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true)
185}
186
Ivan Lozano52767be2019-10-18 14:49:46 -0700187func (library *libraryDecorator) buildShared() bool {
188 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
189}
190
191func (library *libraryDecorator) buildStatic() bool {
192 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
193}
194
Ivan Lozanoffee3342019-08-27 12:03:00 -0700195func (library *libraryDecorator) setRlib() {
196 library.MutatedProperties.VariantIsRlib = true
197 library.MutatedProperties.VariantIsDylib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700198 library.MutatedProperties.VariantIsStatic = false
199 library.MutatedProperties.VariantIsShared = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700200}
201
202func (library *libraryDecorator) setDylib() {
203 library.MutatedProperties.VariantIsRlib = false
204 library.MutatedProperties.VariantIsDylib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700205 library.MutatedProperties.VariantIsStatic = false
206 library.MutatedProperties.VariantIsShared = false
207}
208
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400209func (library *libraryDecorator) rlibStd() bool {
210 return library.MutatedProperties.VariantIsStaticStd
211}
212
Ivan Lozano2b081132020-09-08 12:46:52 -0400213func (library *libraryDecorator) setRlibStd() {
214 library.MutatedProperties.VariantIsStaticStd = true
215}
216
217func (library *libraryDecorator) setDylibStd() {
218 library.MutatedProperties.VariantIsStaticStd = false
219}
220
Ivan Lozano52767be2019-10-18 14:49:46 -0700221func (library *libraryDecorator) setShared() {
222 library.MutatedProperties.VariantIsStatic = false
223 library.MutatedProperties.VariantIsShared = true
224 library.MutatedProperties.VariantIsRlib = false
225 library.MutatedProperties.VariantIsDylib = false
226}
227
228func (library *libraryDecorator) setStatic() {
229 library.MutatedProperties.VariantIsStatic = true
230 library.MutatedProperties.VariantIsShared = false
231 library.MutatedProperties.VariantIsRlib = false
232 library.MutatedProperties.VariantIsDylib = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700233}
234
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200235func (library *libraryDecorator) setSource() {
236 library.MutatedProperties.VariantIsSource = true
237}
238
Liz Kammer356f7d42021-01-26 09:18:53 -0500239func (library *libraryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Ivan Lozano1921e802021-05-20 13:39:16 -0400240 if ctx.Module().(*Module).InVendor() {
241 // Vendor modules should statically link libstd.
242 return rlibAutoDep
243 } else if library.preferRlib() {
Ivan Lozanoea086132020-12-08 14:43:00 -0500244 return rlibAutoDep
245 } else if library.rlib() || library.static() {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700246 return rlibAutoDep
247 } else if library.dylib() || library.shared() {
248 return dylibAutoDep
249 } else {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200250 panic(fmt.Errorf("autoDep called on library %q that has no enabled variants.", ctx.ModuleName()))
Matthew Maurer0f003b12020-06-29 14:34:06 -0700251 }
252}
253
Ivan Lozanoea086132020-12-08 14:43:00 -0500254func (library *libraryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozano1921e802021-05-20 13:39:16 -0400255 if ctx.RustModule().InVendor() {
256 // Vendor modules should statically link libstd.
257 return RlibLinkage
258 } else if library.static() || library.MutatedProperties.VariantIsStaticStd {
Ivan Lozanoea086132020-12-08 14:43:00 -0500259 return RlibLinkage
260 } else if library.baseCompiler.preferRlib() {
261 return RlibLinkage
262 }
263 return DefaultLinkage
264}
265
Ivan Lozanoffee3342019-08-27 12:03:00 -0700266var _ compiler = (*libraryDecorator)(nil)
Ivan Lozano52767be2019-10-18 14:49:46 -0700267var _ libraryInterface = (*libraryDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700268var _ exportedFlagsProducer = (*libraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700269
Martin Geisler67ec0542022-11-18 12:08:55 +0100270// rust_library produces all Rust variants (rust_library_dylib and
271// rust_library_rlib).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700272func RustLibraryFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700273 module, library := NewRustLibrary(android.HostAndDeviceSupported)
274 library.BuildOnlyRust()
275 return module.Init()
276}
277
Martin Geisler67ec0542022-11-18 12:08:55 +0100278// rust_ffi produces all FFI variants (rust_ffi_shared and
279// rust_ffi_static).
Matthew Maurer2ae05132020-06-23 14:28:53 -0700280func RustFFIFactory() android.Module {
281 module, library := NewRustLibrary(android.HostAndDeviceSupported)
282 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700283 return module.Init()
284}
285
Martin Geisler67ec0542022-11-18 12:08:55 +0100286// rust_library_dylib produces a Rust dylib (Rust crate type "dylib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700287func RustLibraryDylibFactory() android.Module {
288 module, library := NewRustLibrary(android.HostAndDeviceSupported)
289 library.BuildOnlyDylib()
290 return module.Init()
291}
292
Martin Geisler67ec0542022-11-18 12:08:55 +0100293// rust_library_rlib produces an rlib (Rust crate type "rlib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700294func RustLibraryRlibFactory() android.Module {
295 module, library := NewRustLibrary(android.HostAndDeviceSupported)
296 library.BuildOnlyRlib()
297 return module.Init()
298}
299
Martin Geisler67ec0542022-11-18 12:08:55 +0100300// rust_ffi_shared produces a shared library (Rust crate type
301// "cdylib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700302func RustFFISharedFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700303 module, library := NewRustLibrary(android.HostAndDeviceSupported)
304 library.BuildOnlyShared()
305 return module.Init()
306}
307
Martin Geisler67ec0542022-11-18 12:08:55 +0100308// rust_ffi_static produces a static library (Rust crate type
309// "staticlib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700310func RustFFIStaticFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700311 module, library := NewRustLibrary(android.HostAndDeviceSupported)
312 library.BuildOnlyStatic()
313 return module.Init()
314}
315
Martin Geisler67ec0542022-11-18 12:08:55 +0100316// rust_library_host produces all Rust variants for the host
317// (rust_library_dylib_host and rust_library_rlib_host).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700318func RustLibraryHostFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700319 module, library := NewRustLibrary(android.HostSupported)
320 library.BuildOnlyRust()
321 return module.Init()
322}
323
Martin Geisler67ec0542022-11-18 12:08:55 +0100324// rust_ffi_host produces all FFI variants for the host
325// (rust_ffi_static_host and rust_ffi_shared_host).
Matthew Maurer2ae05132020-06-23 14:28:53 -0700326func RustFFIHostFactory() android.Module {
327 module, library := NewRustLibrary(android.HostSupported)
328 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700329 return module.Init()
330}
331
Martin Geisler67ec0542022-11-18 12:08:55 +0100332// rust_library_dylib_host produces a dylib for the host (Rust crate
333// type "dylib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700334func RustLibraryDylibHostFactory() android.Module {
335 module, library := NewRustLibrary(android.HostSupported)
336 library.BuildOnlyDylib()
337 return module.Init()
338}
339
Martin Geisler67ec0542022-11-18 12:08:55 +0100340// rust_library_rlib_host produces an rlib for the host (Rust crate
341// type "rlib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700342func RustLibraryRlibHostFactory() android.Module {
343 module, library := NewRustLibrary(android.HostSupported)
344 library.BuildOnlyRlib()
345 return module.Init()
346}
347
Martin Geisler67ec0542022-11-18 12:08:55 +0100348// rust_ffi_static_host produces a static library for the host (Rust
349// crate type "staticlib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700350func RustFFIStaticHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700351 module, library := NewRustLibrary(android.HostSupported)
352 library.BuildOnlyStatic()
353 return module.Init()
354}
355
Martin Geisler67ec0542022-11-18 12:08:55 +0100356// rust_ffi_shared_host produces an shared library for the host (Rust
357// crate type "cdylib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700358func RustFFISharedHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700359 module, library := NewRustLibrary(android.HostSupported)
360 library.BuildOnlyShared()
361 return module.Init()
362}
363
Matthew Maurer2ae05132020-06-23 14:28:53 -0700364func (library *libraryDecorator) BuildOnlyFFI() {
365 library.MutatedProperties.BuildDylib = false
366 library.MutatedProperties.BuildRlib = false
367 library.MutatedProperties.BuildShared = true
368 library.MutatedProperties.BuildStatic = true
369}
370
371func (library *libraryDecorator) BuildOnlyRust() {
372 library.MutatedProperties.BuildDylib = true
373 library.MutatedProperties.BuildRlib = true
374 library.MutatedProperties.BuildShared = false
375 library.MutatedProperties.BuildStatic = false
376}
377
Ivan Lozanoffee3342019-08-27 12:03:00 -0700378func (library *libraryDecorator) BuildOnlyDylib() {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700379 library.MutatedProperties.BuildDylib = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700380 library.MutatedProperties.BuildRlib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700381 library.MutatedProperties.BuildShared = false
382 library.MutatedProperties.BuildStatic = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700383}
384
385func (library *libraryDecorator) BuildOnlyRlib() {
386 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700387 library.MutatedProperties.BuildRlib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700388 library.MutatedProperties.BuildShared = false
389 library.MutatedProperties.BuildStatic = false
390}
391
392func (library *libraryDecorator) BuildOnlyStatic() {
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.BuildShared = false
396 library.MutatedProperties.BuildStatic = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700397}
398
399func (library *libraryDecorator) BuildOnlyShared() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700400 library.MutatedProperties.BuildRlib = false
401 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700402 library.MutatedProperties.BuildStatic = false
403 library.MutatedProperties.BuildShared = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700404}
405
406func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400407 module := newModule(hod, android.MultilibBoth)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700408
409 library := &libraryDecorator{
410 MutatedProperties: LibraryMutatedProperties{
Matthew Maurer2ae05132020-06-23 14:28:53 -0700411 BuildDylib: false,
412 BuildRlib: false,
413 BuildShared: false,
414 BuildStatic: false,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700415 },
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800416 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Matthew Maurerbb3add12020-06-25 09:34:12 -0700417 flagExporter: NewFlagExporter(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700418 }
419
420 module.compiler = library
421
422 return module, library
423}
424
425func (library *libraryDecorator) compilerProps() []interface{} {
426 return append(library.baseCompiler.compilerProps(),
427 &library.Properties,
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200428 &library.MutatedProperties,
429 &library.stripper.StripProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700430}
431
Ivan Lozanof1c84332019-09-20 11:00:37 -0700432func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
433 deps = library.baseCompiler.compilerDeps(ctx, deps)
434
Colin Crosse32f0932022-01-23 20:48:36 -0800435 if library.dylib() || library.shared() {
436 if ctx.toolchain().Bionic() {
437 deps = bionicDeps(ctx, deps, false)
438 deps.CrtBegin = []string{"crtbegin_so"}
439 deps.CrtEnd = []string{"crtend_so"}
440 } else if ctx.Os() == android.LinuxMusl {
441 deps = muslDeps(ctx, deps, false)
442 deps.CrtBegin = []string{"libc_musl_crtbegin_so"}
443 deps.CrtEnd = []string{"libc_musl_crtend_so"}
444 }
Ivan Lozanof1c84332019-09-20 11:00:37 -0700445 }
446
447 return deps
448}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400449
450func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string {
451 return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
452}
453
Ivan Lozano67eada32021-09-23 11:50:33 -0400454func (library *libraryDecorator) cfgFlags(ctx ModuleContext, flags Flags) Flags {
455 flags = library.baseCompiler.cfgFlags(ctx, flags)
Stephen Crane0dbfc562021-07-07 19:05:02 -0700456 if library.dylib() {
457 // We need to add a dependency on std in order to link crates as dylibs.
458 // The hack to add this dependency is guarded by the following cfg so
459 // that we don't force a dependency when it isn't needed.
460 library.baseCompiler.Properties.Cfgs = append(library.baseCompiler.Properties.Cfgs, "android_dylib")
461 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400462
463 flags.RustFlags = append(flags.RustFlags, library.baseCompiler.cfgsToFlags()...)
464 flags.RustdocFlags = append(flags.RustdocFlags, library.baseCompiler.cfgsToFlags()...)
465
466 return flags
467}
468
469func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800470 flags = library.baseCompiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400471
472 flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800473 if library.shared() || library.static() {
474 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
475 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400476 if library.shared() {
477 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
478 }
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
697 variation := v.(*Module).ModuleBase.ImageVariation().Variation
Matthew Maurerbdda9102021-12-16 23:42:47 +0000698 if strings.HasPrefix(variation, cc.VendorVariationPrefix) {
699 // TODO(b/204303985)
700 // Disable vendor dylibs until they are supported
701 v.(*Module).Disable()
702 }
703
Ivan Lozano1921e802021-05-20 13:39:16 -0400704 if strings.HasPrefix(variation, cc.VendorVariationPrefix) &&
705 m.HasVendorVariant() &&
Kiyoung Kim48f37782021-07-07 12:42:39 +0900706 !snapshot.IsVendorProprietaryModule(mctx) &&
Ivan Lozano1921e802021-05-20 13:39:16 -0400707 strings.TrimPrefix(variation, cc.VendorVariationPrefix) == mctx.DeviceConfig().VndkVersion() {
708
709 // cc.MutateImage runs before LibraryMutator, so vendor variations which are meant for rlibs only are
710 // produced for Dylibs; however, dylibs should not be enabled for boardVndkVersion for
711 // non-vendor proprietary modules.
712 v.(*Module).Disable()
713 }
714
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200715 case "source":
716 v.(*Module).compiler.(libraryInterface).setSource()
717 // The source variant does not produce any library.
718 // Disable the compilation steps.
719 v.(*Module).compiler.SetDisabled()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700720 }
721 }
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200722
723 // If a source variant is created, add an inter-variant dependency
724 // between the other variants and the source variant.
725 if sourceVariant {
726 sv := modules[0]
727 for _, v := range modules[1:] {
728 if !v.Enabled() {
729 continue
730 }
731 mctx.AddInterVariantDependency(sourceDepTag, v, sv)
732 }
733 // Alias the source variation so it can be named directly in "srcs" properties.
734 mctx.AliasVariation("source")
735 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700736}
Ivan Lozano2b081132020-09-08 12:46:52 -0400737
738func LibstdMutator(mctx android.BottomUpMutatorContext) {
739 if m, ok := mctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() {
740 switch library := m.compiler.(type) {
741 case libraryInterface:
742 // Only create a variant if a library is actually being built.
743 if library.rlib() && !library.sysroot() {
744 variants := []string{"rlib-std", "dylib-std"}
745 modules := mctx.CreateLocalVariations(variants...)
746
747 rlib := modules[0].(*Module)
748 dylib := modules[1].(*Module)
749 rlib.compiler.(libraryInterface).setRlibStd()
750 dylib.compiler.(libraryInterface).setDylibStd()
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400751 if dylib.ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation ||
752 strings.HasPrefix(dylib.ModuleBase.ImageVariation().Variation, cc.VendorVariationPrefix) {
Ivan Lozano6a884432020-12-02 09:15:16 -0500753 // TODO(b/165791368)
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400754 // Disable rlibs that link against dylib-std on vendor and vendor ramdisk variations until those dylib
Ivan Lozano6a884432020-12-02 09:15:16 -0500755 // variants are properly supported.
756 dylib.Disable()
757 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400758 rlib.Properties.RustSubName += RlibStdlibSuffix
759 dylib.Properties.RustSubName += DylibStdlibSuffix
Ivan Lozano2b081132020-09-08 12:46:52 -0400760 }
761 }
762 }
763}
Ivan Lozano1921e802021-05-20 13:39:16 -0400764
765func (l *libraryDecorator) snapshotHeaders() android.Paths {
766 if l.collectedSnapshotHeaders == nil {
767 panic("snapshotHeaders() must be called after collectHeadersForSnapshot()")
768 }
769 return l.collectedSnapshotHeaders
770}
771
772// collectHeadersForSnapshot collects all exported headers from library.
773// It globs header files in the source tree for exported include directories,
774// and tracks generated header files separately.
775//
776// This is to be called from GenerateAndroidBuildActions, and then collected
777// header files can be retrieved by snapshotHeaders().
778func (l *libraryDecorator) collectHeadersForSnapshot(ctx android.ModuleContext, deps PathDeps) {
779 ret := android.Paths{}
780
781 // Glob together the headers from the modules include_dirs property
782 for _, path := range android.CopyOfPaths(l.includeDirs) {
783 dir := path.String()
Liz Kammer0ea79982022-02-07 08:51:47 -0500784 globDir := dir + "/**/*"
785 glob, err := ctx.GlobWithDeps(globDir, nil)
Ivan Lozano1921e802021-05-20 13:39:16 -0400786 if err != nil {
Liz Kammer0ea79982022-02-07 08:51:47 -0500787 ctx.ModuleErrorf("glob of %q failed: %s", globDir, err)
Ivan Lozano1921e802021-05-20 13:39:16 -0400788 return
789 }
790
791 for _, header := range glob {
792 // Filter out only the files with extensions that are headers.
793 found := false
794 for _, ext := range cc.HeaderExts {
795 if strings.HasSuffix(header, ext) {
796 found = true
797 break
798 }
799 }
800 if !found {
801 continue
802 }
803 ret = append(ret, android.PathForSource(ctx, header))
804 }
805 }
806
807 // Glob together the headers from C dependencies as well, starting with non-generated headers.
808 ret = append(ret, cc.GlobHeadersForSnapshot(ctx, append(android.CopyOfPaths(deps.depIncludePaths), deps.depSystemIncludePaths...))...)
809
810 // Collect generated headers from C dependencies.
811 ret = append(ret, cc.GlobGeneratedHeadersForSnapshot(ctx, deps.depGeneratedHeaders)...)
812
813 // TODO(185577950): If support for generated headers is added, they need to be collected here as well.
814 l.collectedSnapshotHeaders = ret
815}