blob: c0ff741db703e5d91deaec0b596f7841399b712a [file] [log] [blame]
Ivan Lozanoffee3342019-08-27 12:03:00 -07001// Copyright 2019 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package rust
16
17import (
Matthew Maurera28404a2023-11-20 23:33:28 +000018 "errors"
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +020019 "fmt"
Ivan Lozanoad8b18b2019-10-31 19:38:29 -070020 "regexp"
21 "strings"
22
Ivan Lozanoffee3342019-08-27 12:03:00 -070023 "android/soong/android"
Vinh Tranbcb5f572023-08-24 11:10:01 -040024 "android/soong/bazel"
Colin Cross0de8a1e2020-09-18 14:15:30 -070025 "android/soong/cc"
Vinh Tranbcb5f572023-08-24 11:10:01 -040026
27 "github.com/google/blueprint/proptools"
Ivan Lozanoffee3342019-08-27 12:03:00 -070028)
29
Ivan Lozano2b081132020-09-08 12:46:52 -040030var (
Ivan Lozano4df02572023-06-15 14:21:09 -040031 RlibStdlibSuffix = ".rlib-std"
Ivan Lozano2b081132020-09-08 12:46:52 -040032)
33
Ivan Lozanoffee3342019-08-27 12:03:00 -070034func init() {
35 android.RegisterModuleType("rust_library", RustLibraryFactory)
36 android.RegisterModuleType("rust_library_dylib", RustLibraryDylibFactory)
37 android.RegisterModuleType("rust_library_rlib", RustLibraryRlibFactory)
38 android.RegisterModuleType("rust_library_host", RustLibraryHostFactory)
39 android.RegisterModuleType("rust_library_host_dylib", RustLibraryDylibHostFactory)
40 android.RegisterModuleType("rust_library_host_rlib", RustLibraryRlibHostFactory)
Matthew Maurer2ae05132020-06-23 14:28:53 -070041 android.RegisterModuleType("rust_ffi", RustFFIFactory)
42 android.RegisterModuleType("rust_ffi_shared", RustFFISharedFactory)
43 android.RegisterModuleType("rust_ffi_static", RustFFIStaticFactory)
44 android.RegisterModuleType("rust_ffi_host", RustFFIHostFactory)
45 android.RegisterModuleType("rust_ffi_host_shared", RustFFISharedHostFactory)
46 android.RegisterModuleType("rust_ffi_host_static", RustFFIStaticHostFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070047}
48
49type VariantLibraryProperties struct {
Matthew Maurerc761eec2020-06-25 00:47:46 -070050 Enabled *bool `android:"arch_variant"`
51 Srcs []string `android:"path,arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070052}
53
54type LibraryCompilerProperties struct {
Ivan Lozano52767be2019-10-18 14:49:46 -070055 Rlib VariantLibraryProperties `android:"arch_variant"`
56 Dylib VariantLibraryProperties `android:"arch_variant"`
57 Shared VariantLibraryProperties `android:"arch_variant"`
58 Static VariantLibraryProperties `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070059
Ivan Lozano52767be2019-10-18 14:49:46 -070060 // path to include directories to pass to cc_* modules, only relevant for static/shared variants.
61 Include_dirs []string `android:"path,arch_variant"`
Ivan Lozano2b081132020-09-08 12:46:52 -040062
63 // Whether this library is part of the Rust toolchain sysroot.
64 Sysroot *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070065}
66
67type LibraryMutatedProperties struct {
Ivan Lozanoffee3342019-08-27 12:03:00 -070068 // Build a dylib variant
69 BuildDylib bool `blueprint:"mutated"`
70 // Build an rlib variant
71 BuildRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070072 // Build a shared library variant
73 BuildShared bool `blueprint:"mutated"`
74 // Build a static library variant
75 BuildStatic bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070076
77 // This variant is a dylib
78 VariantIsDylib bool `blueprint:"mutated"`
79 // This variant is an rlib
80 VariantIsRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070081 // This variant is a shared library
82 VariantIsShared bool `blueprint:"mutated"`
83 // This variant is a static library
84 VariantIsStatic bool `blueprint:"mutated"`
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +020085 // This variant is a source provider
86 VariantIsSource bool `blueprint:"mutated"`
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040087
88 // This variant is disabled and should not be compiled
89 // (used for SourceProvider variants that produce only source)
90 VariantIsDisabled bool `blueprint:"mutated"`
Ivan Lozano2b081132020-09-08 12:46:52 -040091
92 // Whether this library variant should be link libstd via rlibs
93 VariantIsStaticStd bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070094}
95
96type libraryDecorator struct {
97 *baseCompiler
Matthew Maurerbb3add12020-06-25 09:34:12 -070098 *flagExporter
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +020099 stripper Stripper
Ivan Lozanoffee3342019-08-27 12:03:00 -0700100
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400101 Properties LibraryCompilerProperties
102 MutatedProperties LibraryMutatedProperties
103 includeDirs android.Paths
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400104 sourceProvider SourceProvider
Ivan Lozano1921e802021-05-20 13:39:16 -0400105
106 collectedSnapshotHeaders android.Paths
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400107
108 // table-of-contents file for cdylib crates to optimize out relinking when possible
109 tocFile android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700110}
111
112type libraryInterface interface {
113 rlib() bool
114 dylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700115 static() bool
116 shared() bool
Ivan Lozano2b081132020-09-08 12:46:52 -0400117 sysroot() bool
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200118 source() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700119
120 // Returns true if the build options for the module have selected a particular build type
121 buildRlib() bool
122 buildDylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700123 buildShared() bool
124 buildStatic() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700125
126 // Sets a particular variant type
127 setRlib()
128 setDylib()
Ivan Lozano52767be2019-10-18 14:49:46 -0700129 setShared()
130 setStatic()
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200131 setSource()
Ivan Lozano52767be2019-10-18 14:49:46 -0700132
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400133 // libstd linkage functions
134 rlibStd() bool
Ivan Lozano2b081132020-09-08 12:46:52 -0400135 setRlibStd()
136 setDylibStd()
137
Ivan Lozano52767be2019-10-18 14:49:46 -0700138 // Build a specific library variant
Matthew Maurer2ae05132020-06-23 14:28:53 -0700139 BuildOnlyFFI()
140 BuildOnlyRust()
Ivan Lozano52767be2019-10-18 14:49:46 -0700141 BuildOnlyRlib()
142 BuildOnlyDylib()
143 BuildOnlyStatic()
144 BuildOnlyShared()
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400145
146 toc() android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700147}
148
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400149func (library *libraryDecorator) nativeCoverage() bool {
150 return true
151}
152
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400153func (library *libraryDecorator) toc() android.OptionalPath {
154 return library.tocFile
155}
156
Ivan Lozanoffee3342019-08-27 12:03:00 -0700157func (library *libraryDecorator) rlib() bool {
158 return library.MutatedProperties.VariantIsRlib
159}
160
Ivan Lozano2b081132020-09-08 12:46:52 -0400161func (library *libraryDecorator) sysroot() bool {
162 return Bool(library.Properties.Sysroot)
163}
164
Ivan Lozanoffee3342019-08-27 12:03:00 -0700165func (library *libraryDecorator) dylib() bool {
166 return library.MutatedProperties.VariantIsDylib
167}
168
Ivan Lozano52767be2019-10-18 14:49:46 -0700169func (library *libraryDecorator) shared() bool {
170 return library.MutatedProperties.VariantIsShared
171}
172
173func (library *libraryDecorator) static() bool {
174 return library.MutatedProperties.VariantIsStatic
175}
176
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200177func (library *libraryDecorator) source() bool {
178 return library.MutatedProperties.VariantIsSource
179}
180
Ivan Lozanoffee3342019-08-27 12:03:00 -0700181func (library *libraryDecorator) buildRlib() bool {
182 return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true)
183}
184
185func (library *libraryDecorator) buildDylib() bool {
186 return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true)
187}
188
Ivan Lozano52767be2019-10-18 14:49:46 -0700189func (library *libraryDecorator) buildShared() bool {
190 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
191}
192
193func (library *libraryDecorator) buildStatic() bool {
194 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
195}
196
Ivan Lozanoffee3342019-08-27 12:03:00 -0700197func (library *libraryDecorator) setRlib() {
198 library.MutatedProperties.VariantIsRlib = true
199 library.MutatedProperties.VariantIsDylib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700200 library.MutatedProperties.VariantIsStatic = false
201 library.MutatedProperties.VariantIsShared = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700202}
203
204func (library *libraryDecorator) setDylib() {
205 library.MutatedProperties.VariantIsRlib = false
206 library.MutatedProperties.VariantIsDylib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700207 library.MutatedProperties.VariantIsStatic = false
208 library.MutatedProperties.VariantIsShared = false
209}
210
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400211func (library *libraryDecorator) rlibStd() bool {
212 return library.MutatedProperties.VariantIsStaticStd
213}
214
Ivan Lozano2b081132020-09-08 12:46:52 -0400215func (library *libraryDecorator) setRlibStd() {
216 library.MutatedProperties.VariantIsStaticStd = true
217}
218
219func (library *libraryDecorator) setDylibStd() {
220 library.MutatedProperties.VariantIsStaticStd = false
221}
222
Ivan Lozano52767be2019-10-18 14:49:46 -0700223func (library *libraryDecorator) setShared() {
224 library.MutatedProperties.VariantIsStatic = false
225 library.MutatedProperties.VariantIsShared = true
226 library.MutatedProperties.VariantIsRlib = false
227 library.MutatedProperties.VariantIsDylib = false
228}
229
230func (library *libraryDecorator) setStatic() {
231 library.MutatedProperties.VariantIsStatic = true
232 library.MutatedProperties.VariantIsShared = false
233 library.MutatedProperties.VariantIsRlib = false
234 library.MutatedProperties.VariantIsDylib = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700235}
236
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200237func (library *libraryDecorator) setSource() {
238 library.MutatedProperties.VariantIsSource = true
239}
240
Liz Kammer356f7d42021-01-26 09:18:53 -0500241func (library *libraryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400242 if library.preferRlib() {
Ivan Lozanoea086132020-12-08 14:43:00 -0500243 return rlibAutoDep
244 } else if library.rlib() || library.static() {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700245 return rlibAutoDep
246 } else if library.dylib() || library.shared() {
247 return dylibAutoDep
248 } else {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200249 panic(fmt.Errorf("autoDep called on library %q that has no enabled variants.", ctx.ModuleName()))
Matthew Maurer0f003b12020-06-29 14:34:06 -0700250 }
251}
252
Ivan Lozanoea086132020-12-08 14:43:00 -0500253func (library *libraryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400254 if library.static() || library.MutatedProperties.VariantIsStaticStd {
Ivan Lozanoea086132020-12-08 14:43:00 -0500255 return RlibLinkage
256 } else if library.baseCompiler.preferRlib() {
257 return RlibLinkage
258 }
259 return DefaultLinkage
260}
261
Ivan Lozanoffee3342019-08-27 12:03:00 -0700262var _ compiler = (*libraryDecorator)(nil)
Ivan Lozano52767be2019-10-18 14:49:46 -0700263var _ libraryInterface = (*libraryDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700264var _ exportedFlagsProducer = (*libraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700265
Martin Geisler67ec0542022-11-18 12:08:55 +0100266// rust_library produces all Rust variants (rust_library_dylib and
267// rust_library_rlib).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700268func RustLibraryFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700269 module, library := NewRustLibrary(android.HostAndDeviceSupported)
270 library.BuildOnlyRust()
271 return module.Init()
272}
273
Martin Geisler67ec0542022-11-18 12:08:55 +0100274// rust_ffi produces all FFI variants (rust_ffi_shared and
275// rust_ffi_static).
Matthew Maurer2ae05132020-06-23 14:28:53 -0700276func RustFFIFactory() android.Module {
277 module, library := NewRustLibrary(android.HostAndDeviceSupported)
278 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700279 return module.Init()
280}
281
Martin Geisler67ec0542022-11-18 12:08:55 +0100282// rust_library_dylib produces a Rust dylib (Rust crate type "dylib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700283func RustLibraryDylibFactory() android.Module {
284 module, library := NewRustLibrary(android.HostAndDeviceSupported)
285 library.BuildOnlyDylib()
286 return module.Init()
287}
288
Martin Geisler67ec0542022-11-18 12:08:55 +0100289// rust_library_rlib produces an rlib (Rust crate type "rlib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700290func RustLibraryRlibFactory() android.Module {
291 module, library := NewRustLibrary(android.HostAndDeviceSupported)
292 library.BuildOnlyRlib()
293 return module.Init()
294}
295
Martin Geisler67ec0542022-11-18 12:08:55 +0100296// rust_ffi_shared produces a shared library (Rust crate type
297// "cdylib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700298func RustFFISharedFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700299 module, library := NewRustLibrary(android.HostAndDeviceSupported)
300 library.BuildOnlyShared()
301 return module.Init()
302}
303
Martin Geisler67ec0542022-11-18 12:08:55 +0100304// rust_ffi_static produces a static library (Rust crate type
305// "staticlib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700306func RustFFIStaticFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700307 module, library := NewRustLibrary(android.HostAndDeviceSupported)
308 library.BuildOnlyStatic()
309 return module.Init()
310}
311
Martin Geisler67ec0542022-11-18 12:08:55 +0100312// rust_library_host produces all Rust variants for the host
313// (rust_library_dylib_host and rust_library_rlib_host).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700314func RustLibraryHostFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700315 module, library := NewRustLibrary(android.HostSupported)
316 library.BuildOnlyRust()
317 return module.Init()
318}
319
Martin Geisler67ec0542022-11-18 12:08:55 +0100320// rust_ffi_host produces all FFI variants for the host
321// (rust_ffi_static_host and rust_ffi_shared_host).
Matthew Maurer2ae05132020-06-23 14:28:53 -0700322func RustFFIHostFactory() android.Module {
323 module, library := NewRustLibrary(android.HostSupported)
324 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700325 return module.Init()
326}
327
Martin Geisler67ec0542022-11-18 12:08:55 +0100328// rust_library_dylib_host produces a dylib for the host (Rust crate
329// type "dylib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700330func RustLibraryDylibHostFactory() android.Module {
331 module, library := NewRustLibrary(android.HostSupported)
332 library.BuildOnlyDylib()
333 return module.Init()
334}
335
Martin Geisler67ec0542022-11-18 12:08:55 +0100336// rust_library_rlib_host produces an rlib for the host (Rust crate
337// type "rlib").
Ivan Lozanoffee3342019-08-27 12:03:00 -0700338func RustLibraryRlibHostFactory() android.Module {
339 module, library := NewRustLibrary(android.HostSupported)
340 library.BuildOnlyRlib()
341 return module.Init()
342}
343
Martin Geisler67ec0542022-11-18 12:08:55 +0100344// rust_ffi_static_host produces a static library for the host (Rust
345// crate type "staticlib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700346func RustFFIStaticHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700347 module, library := NewRustLibrary(android.HostSupported)
348 library.BuildOnlyStatic()
349 return module.Init()
350}
351
Martin Geisler67ec0542022-11-18 12:08:55 +0100352// rust_ffi_shared_host produces an shared library for the host (Rust
353// crate type "cdylib").
Matthew Maurer2ae05132020-06-23 14:28:53 -0700354func RustFFISharedHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700355 module, library := NewRustLibrary(android.HostSupported)
356 library.BuildOnlyShared()
357 return module.Init()
358}
359
Matthew Maurer2ae05132020-06-23 14:28:53 -0700360func (library *libraryDecorator) BuildOnlyFFI() {
361 library.MutatedProperties.BuildDylib = false
362 library.MutatedProperties.BuildRlib = false
363 library.MutatedProperties.BuildShared = true
364 library.MutatedProperties.BuildStatic = true
365}
366
367func (library *libraryDecorator) BuildOnlyRust() {
368 library.MutatedProperties.BuildDylib = true
369 library.MutatedProperties.BuildRlib = true
370 library.MutatedProperties.BuildShared = false
371 library.MutatedProperties.BuildStatic = false
372}
373
Ivan Lozanoffee3342019-08-27 12:03:00 -0700374func (library *libraryDecorator) BuildOnlyDylib() {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700375 library.MutatedProperties.BuildDylib = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700376 library.MutatedProperties.BuildRlib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700377 library.MutatedProperties.BuildShared = false
378 library.MutatedProperties.BuildStatic = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700379}
380
381func (library *libraryDecorator) BuildOnlyRlib() {
382 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700383 library.MutatedProperties.BuildRlib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700384 library.MutatedProperties.BuildShared = false
385 library.MutatedProperties.BuildStatic = false
386}
387
388func (library *libraryDecorator) BuildOnlyStatic() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700389 library.MutatedProperties.BuildRlib = false
390 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700391 library.MutatedProperties.BuildShared = false
392 library.MutatedProperties.BuildStatic = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700393}
394
395func (library *libraryDecorator) BuildOnlyShared() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700396 library.MutatedProperties.BuildRlib = false
397 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700398 library.MutatedProperties.BuildStatic = false
399 library.MutatedProperties.BuildShared = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700400}
401
402func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400403 module := newModule(hod, android.MultilibBoth)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700404
Vinh Tranbcb5f572023-08-24 11:10:01 -0400405 android.InitBazelModule(module)
406
Ivan Lozanoffee3342019-08-27 12:03:00 -0700407 library := &libraryDecorator{
408 MutatedProperties: LibraryMutatedProperties{
Matthew Maurer2ae05132020-06-23 14:28:53 -0700409 BuildDylib: false,
410 BuildRlib: false,
411 BuildShared: false,
412 BuildStatic: false,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700413 },
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800414 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Matthew Maurerbb3add12020-06-25 09:34:12 -0700415 flagExporter: NewFlagExporter(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700416 }
417
418 module.compiler = library
419
420 return module, library
421}
422
423func (library *libraryDecorator) compilerProps() []interface{} {
424 return append(library.baseCompiler.compilerProps(),
425 &library.Properties,
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200426 &library.MutatedProperties,
427 &library.stripper.StripProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700428}
429
Ivan Lozanof1c84332019-09-20 11:00:37 -0700430func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
431 deps = library.baseCompiler.compilerDeps(ctx, deps)
432
Colin Crosse32f0932022-01-23 20:48:36 -0800433 if library.dylib() || library.shared() {
434 if ctx.toolchain().Bionic() {
435 deps = bionicDeps(ctx, deps, false)
436 deps.CrtBegin = []string{"crtbegin_so"}
437 deps.CrtEnd = []string{"crtend_so"}
438 } else if ctx.Os() == android.LinuxMusl {
439 deps = muslDeps(ctx, deps, false)
440 deps.CrtBegin = []string{"libc_musl_crtbegin_so"}
441 deps.CrtEnd = []string{"libc_musl_crtend_so"}
442 }
Ivan Lozanof1c84332019-09-20 11:00:37 -0700443 }
444
445 return deps
446}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400447
448func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string {
449 return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
450}
451
Ivan Lozano67eada32021-09-23 11:50:33 -0400452func (library *libraryDecorator) cfgFlags(ctx ModuleContext, flags Flags) Flags {
453 flags = library.baseCompiler.cfgFlags(ctx, flags)
Stephen Crane0dbfc562021-07-07 19:05:02 -0700454 if library.dylib() {
455 // We need to add a dependency on std in order to link crates as dylibs.
456 // The hack to add this dependency is guarded by the following cfg so
457 // that we don't force a dependency when it isn't needed.
458 library.baseCompiler.Properties.Cfgs = append(library.baseCompiler.Properties.Cfgs, "android_dylib")
459 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400460
461 flags.RustFlags = append(flags.RustFlags, library.baseCompiler.cfgsToFlags()...)
462 flags.RustdocFlags = append(flags.RustdocFlags, library.baseCompiler.cfgsToFlags()...)
463
464 return flags
465}
466
467func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800468 flags = library.baseCompiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400469
470 flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800471 if library.shared() || library.static() {
472 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
473 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400474 if library.shared() {
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -0700475 if ctx.Darwin() {
476 flags.LinkFlags = append(
477 flags.LinkFlags,
478 "-dynamic_lib",
479 "-install_name @rpath/"+library.sharedLibFilename(ctx),
480 )
481 } else {
482 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
483 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400484 }
485
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800486 return flags
487}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700488
Sasha Smundaka76acba2022-04-18 20:12:56 -0700489func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
490 var outputFile android.ModuleOutPath
491 var ret buildOutput
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200492 var fileName string
Matthew Maurera28404a2023-11-20 23:33:28 +0000493 crateRootPath := crateRootPath(ctx, library)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700494
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400495 if library.sourceProvider != nil {
Ivan Lozano9d74a522020-12-01 09:25:22 -0500496 deps.srcProviderFiles = append(deps.srcProviderFiles, library.sourceProvider.Srcs()...)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400497 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700498
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400499 // Calculate output filename
500 if library.rlib() {
501 fileName = library.getStem(ctx) + ctx.toolchain().RlibSuffix()
502 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700503 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400504 } else if library.dylib() {
505 fileName = library.getStem(ctx) + ctx.toolchain().DylibSuffix()
506 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700507 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400508 } else if library.static() {
509 fileName = library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
510 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700511 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400512 } else if library.shared() {
513 fileName = library.sharedLibFilename(ctx)
514 outputFile = android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700515 ret.outputFile = outputFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400516 }
517
518 if !library.rlib() && !library.static() && library.stripper.NeedsStrip(ctx) {
519 strippedOutputFile := outputFile
520 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
521 library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
522
523 library.baseCompiler.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
524 }
525 library.baseCompiler.unstrippedOutputFile = outputFile
526
Ivan Lozanoffee3342019-08-27 12:03:00 -0700527 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500528 flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...)
Colin Cross004bd3f2023-10-02 11:39:17 -0700529 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700530
Matthew Maurer46c46cc2020-01-13 16:34:34 -0800531 if library.dylib() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700532 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
533 // https://github.com/rust-lang/rust/issues/19680
534 // https://github.com/rust-lang/rust/issues/34909
535 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
536 }
537
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400538 // Call the appropriate builder for this library type
Ivan Lozanoffee3342019-08-27 12:03:00 -0700539 if library.rlib() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000540 ret.kytheFile = TransformSrctoRlib(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700541 } else if library.dylib() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000542 ret.kytheFile = TransformSrctoDylib(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700543 } else if library.static() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000544 ret.kytheFile = TransformSrctoStatic(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700545 } else if library.shared() {
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000546 ret.kytheFile = TransformSrctoShared(ctx, crateRootPath, deps, flags, outputFile).kytheFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700547 }
548
Ivan Lozano52767be2019-10-18 14:49:46 -0700549 if library.rlib() || library.dylib() {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700550 library.flagExporter.exportLinkDirs(deps.linkDirs...)
Colin Cross0de8a1e2020-09-18 14:15:30 -0700551 library.flagExporter.exportLinkObjects(deps.linkObjects...)
Ivan Lozano52767be2019-10-18 14:49:46 -0700552 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700553
Colin Cross0de8a1e2020-09-18 14:15:30 -0700554 if library.static() || library.shared() {
555 ctx.SetProvider(cc.FlagExporterInfoProvider, cc.FlagExporterInfo{
556 IncludeDirs: library.includeDirs,
557 })
558 }
559
560 if library.shared() {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400561 // Optimize out relinking against shared libraries whose interface hasn't changed by
562 // depending on a table of contents file instead of the library itself.
563 tocFile := outputFile.ReplaceExtension(ctx, flags.Toolchain.SharedLibSuffix()[1:]+".toc")
564 library.tocFile = android.OptionalPathForPath(tocFile)
565 cc.TransformSharedObjectToToc(ctx, outputFile, tocFile)
566
Colin Cross0de8a1e2020-09-18 14:15:30 -0700567 ctx.SetProvider(cc.SharedLibraryInfoProvider, cc.SharedLibraryInfo{
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400568 TableOfContents: android.OptionalPathForPath(tocFile),
569 SharedLibrary: outputFile,
570 Target: ctx.Target(),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700571 })
572 }
573
574 if library.static() {
Colin Crossc85750b2022-04-21 12:50:51 -0700575 depSet := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).Direct(outputFile).Build()
Colin Cross0de8a1e2020-09-18 14:15:30 -0700576 ctx.SetProvider(cc.StaticLibraryInfoProvider, cc.StaticLibraryInfo{
577 StaticLibrary: outputFile,
578
579 TransitiveStaticLibrariesForOrdering: depSet,
580 })
581 }
582
583 library.flagExporter.setProvider(ctx)
584
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400585 return ret
Ivan Lozanoffee3342019-08-27 12:03:00 -0700586}
587
Matthew Maurera28404a2023-11-20 23:33:28 +0000588func (library *libraryDecorator) checkedCrateRootPath() (android.Path, error) {
Dan Albert06feee92021-03-19 15:06:02 -0700589 if library.sourceProvider != nil {
Matthew Maurera28404a2023-11-20 23:33:28 +0000590 srcs := library.sourceProvider.Srcs()
591 if len(srcs) == 0 {
592 return nil, errors.New("Source provider generated 0 sources")
593 }
Dan Albert06feee92021-03-19 15:06:02 -0700594 // Assume the first source from the source provider is the library entry point.
Matthew Maurera28404a2023-11-20 23:33:28 +0000595 return srcs[0], nil
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000596 } else {
Matthew Maurera28404a2023-11-20 23:33:28 +0000597 return library.baseCompiler.checkedCrateRootPath()
Dan Albert06feee92021-03-19 15:06:02 -0700598 }
599}
600
601func (library *libraryDecorator) rustdoc(ctx ModuleContext, flags Flags,
602 deps PathDeps) android.OptionalPath {
603 // rustdoc has builtin support for documenting config specific information
604 // regardless of the actual config it was given
605 // (https://doc.rust-lang.org/rustdoc/advanced-features.html#cfgdoc-documenting-platform-specific-or-feature-specific-information),
606 // so we generate the rustdoc for only the primary module so that we have a
607 // single set of docs to refer to.
608 if ctx.Module() != ctx.PrimaryModule() {
609 return android.OptionalPath{}
610 }
611
Matthew Maurera28404a2023-11-20 23:33:28 +0000612 return android.OptionalPathForPath(Rustdoc(ctx, crateRootPath(ctx, library),
Dan Albert06feee92021-03-19 15:06:02 -0700613 deps, flags))
614}
615
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700616func (library *libraryDecorator) getStem(ctx ModuleContext) string {
617 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
618 validateLibraryStem(ctx, stem, library.crateName())
619
620 return stem + String(library.baseCompiler.Properties.Suffix)
621}
622
Ivan Lozano2b081132020-09-08 12:46:52 -0400623func (library *libraryDecorator) install(ctx ModuleContext) {
624 // Only shared and dylib variants make sense to install.
625 if library.shared() || library.dylib() {
626 library.baseCompiler.install(ctx)
627 }
628}
629
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400630func (library *libraryDecorator) Disabled() bool {
631 return library.MutatedProperties.VariantIsDisabled
632}
633
634func (library *libraryDecorator) SetDisabled() {
635 library.MutatedProperties.VariantIsDisabled = true
636}
637
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700638var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
639
640func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
641 if crate_name == "" {
642 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
643 }
644
645 // crate_names are used for the library output file, and rustc expects these
646 // to be alphanumeric with underscores allowed.
647 if validCrateName.MatchString(crate_name) {
648 ctx.PropertyErrorf("crate_name",
649 "library crate_names must be alphanumeric with underscores allowed")
650 }
651
652 // Libraries are expected to begin with "lib" followed by the crate_name
653 if !strings.HasPrefix(filename, "lib"+crate_name) {
654 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
655 }
656}
657
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200658// LibraryMutator mutates the libraries into variants according to the
659// build{Rlib,Dylib} attributes.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700660func LibraryMutator(mctx android.BottomUpMutatorContext) {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200661 // Only mutate on Rust libraries.
662 m, ok := mctx.Module().(*Module)
663 if !ok || m.compiler == nil {
664 return
665 }
666 library, ok := m.compiler.(libraryInterface)
667 if !ok {
668 return
669 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400670
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200671 var variants []string
672 // The source variant is used for SourceProvider modules. The other variants (i.e. rlib and dylib)
673 // depend on this variant. It must be the first variant to be declared.
674 sourceVariant := false
675 if m.sourceProvider != nil {
676 variants = append(variants, "source")
677 sourceVariant = true
678 }
679 if library.buildRlib() {
680 variants = append(variants, rlibVariation)
681 }
682 if library.buildDylib() {
683 variants = append(variants, dylibVariation)
684 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400685
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200686 if len(variants) == 0 {
687 return
688 }
689 modules := mctx.CreateLocalVariations(variants...)
690
691 // The order of the variations (modules) matches the variant names provided. Iterate
692 // through the new variation modules and set their mutated properties.
693 for i, v := range modules {
694 switch variants[i] {
695 case rlibVariation:
696 v.(*Module).compiler.(libraryInterface).setRlib()
697 case dylibVariation:
698 v.(*Module).compiler.(libraryInterface).setDylib()
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400699 if v.(*Module).ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
Ivan Lozano6a884432020-12-02 09:15:16 -0500700 // TODO(b/165791368)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400701 // Disable dylib Vendor Ramdisk variations until we support these.
Ivan Lozano6a884432020-12-02 09:15:16 -0500702 v.(*Module).Disable()
703 }
Ivan Lozano1921e802021-05-20 13:39:16 -0400704
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200705 case "source":
706 v.(*Module).compiler.(libraryInterface).setSource()
707 // The source variant does not produce any library.
708 // Disable the compilation steps.
709 v.(*Module).compiler.SetDisabled()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700710 }
711 }
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200712
713 // If a source variant is created, add an inter-variant dependency
714 // between the other variants and the source variant.
715 if sourceVariant {
716 sv := modules[0]
717 for _, v := range modules[1:] {
718 if !v.Enabled() {
719 continue
720 }
721 mctx.AddInterVariantDependency(sourceDepTag, v, sv)
722 }
723 // Alias the source variation so it can be named directly in "srcs" properties.
724 mctx.AliasVariation("source")
725 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700726}
Ivan Lozano2b081132020-09-08 12:46:52 -0400727
728func LibstdMutator(mctx android.BottomUpMutatorContext) {
729 if m, ok := mctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() {
730 switch library := m.compiler.(type) {
731 case libraryInterface:
732 // Only create a variant if a library is actually being built.
733 if library.rlib() && !library.sysroot() {
734 variants := []string{"rlib-std", "dylib-std"}
735 modules := mctx.CreateLocalVariations(variants...)
736
737 rlib := modules[0].(*Module)
738 dylib := modules[1].(*Module)
739 rlib.compiler.(libraryInterface).setRlibStd()
740 dylib.compiler.(libraryInterface).setDylibStd()
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400741 if dylib.ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
Ivan Lozano6a884432020-12-02 09:15:16 -0500742 // TODO(b/165791368)
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400743 // Disable rlibs that link against dylib-std on vendor ramdisk variations until those dylib
Ivan Lozano6a884432020-12-02 09:15:16 -0500744 // variants are properly supported.
745 dylib.Disable()
746 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400747 rlib.Properties.RustSubName += RlibStdlibSuffix
Ivan Lozano2b081132020-09-08 12:46:52 -0400748 }
749 }
750 }
751}
Ivan Lozano1921e802021-05-20 13:39:16 -0400752
753func (l *libraryDecorator) snapshotHeaders() android.Paths {
754 if l.collectedSnapshotHeaders == nil {
755 panic("snapshotHeaders() must be called after collectHeadersForSnapshot()")
756 }
757 return l.collectedSnapshotHeaders
758}
759
760// collectHeadersForSnapshot collects all exported headers from library.
761// It globs header files in the source tree for exported include directories,
762// and tracks generated header files separately.
763//
764// This is to be called from GenerateAndroidBuildActions, and then collected
765// header files can be retrieved by snapshotHeaders().
766func (l *libraryDecorator) collectHeadersForSnapshot(ctx android.ModuleContext, deps PathDeps) {
767 ret := android.Paths{}
768
769 // Glob together the headers from the modules include_dirs property
770 for _, path := range android.CopyOfPaths(l.includeDirs) {
771 dir := path.String()
Liz Kammer0ea79982022-02-07 08:51:47 -0500772 globDir := dir + "/**/*"
773 glob, err := ctx.GlobWithDeps(globDir, nil)
Ivan Lozano1921e802021-05-20 13:39:16 -0400774 if err != nil {
Liz Kammer0ea79982022-02-07 08:51:47 -0500775 ctx.ModuleErrorf("glob of %q failed: %s", globDir, err)
Ivan Lozano1921e802021-05-20 13:39:16 -0400776 return
777 }
778
779 for _, header := range glob {
780 // Filter out only the files with extensions that are headers.
781 found := false
782 for _, ext := range cc.HeaderExts {
783 if strings.HasSuffix(header, ext) {
784 found = true
785 break
786 }
787 }
788 if !found {
789 continue
790 }
791 ret = append(ret, android.PathForSource(ctx, header))
792 }
793 }
794
795 // Glob together the headers from C dependencies as well, starting with non-generated headers.
796 ret = append(ret, cc.GlobHeadersForSnapshot(ctx, append(android.CopyOfPaths(deps.depIncludePaths), deps.depSystemIncludePaths...))...)
797
798 // Collect generated headers from C dependencies.
799 ret = append(ret, cc.GlobGeneratedHeadersForSnapshot(ctx, deps.depGeneratedHeaders)...)
800
801 // TODO(185577950): If support for generated headers is added, they need to be collected here as well.
802 l.collectedSnapshotHeaders = ret
803}
Vinh Tranbcb5f572023-08-24 11:10:01 -0400804
805type rustLibraryAttributes struct {
Vinh Tran75159942023-10-17 16:28:04 -0400806 commonLibraryAttrs
807}
808
809type commonLibraryAttrs struct {
Vinh Tranb4bb20f2023-08-24 11:10:01 -0400810 Srcs bazel.LabelListAttribute
811 Compile_data bazel.LabelListAttribute
812 Crate_name bazel.StringAttribute
813 Edition bazel.StringAttribute
814 Crate_features bazel.StringListAttribute
815 Deps bazel.LabelListAttribute
816 Rustc_flags bazel.StringListAttribute
817 Proc_macro_deps bazel.LabelListAttribute
Vinh Tranbcb5f572023-08-24 11:10:01 -0400818}
819
Vinh Tran75159942023-10-17 16:28:04 -0400820func commonLibraryAttrsBp2build(ctx android.Bp2buildMutatorContext, m *Module) *commonLibraryAttrs {
Vinh Tranbcb5f572023-08-24 11:10:01 -0400821 lib := m.compiler.(*libraryDecorator)
Vinh Tran9b846782023-08-24 12:55:12 -0400822
Vinh Tranb4bb20f2023-08-24 11:10:01 -0400823 srcs, compileData := srcsAndCompileDataAttrs(ctx, *lib.baseCompiler)
Vinh Tran9b846782023-08-24 12:55:12 -0400824
Vinh Tranbcb5f572023-08-24 11:10:01 -0400825 deps := android.BazelLabelForModuleDeps(ctx, append(
826 lib.baseCompiler.Properties.Rustlibs,
827 lib.baseCompiler.Properties.Rlibs...,
828 ))
Vinh Tran9b846782023-08-24 12:55:12 -0400829
830 cargoBuildScript := cargoBuildScriptBp2build(ctx, m)
831 if cargoBuildScript != nil {
832 deps.Add(&bazel.Label{
833 Label: ":" + *cargoBuildScript,
834 })
835 }
836
Vinh Tranb4bb20f2023-08-24 11:10:01 -0400837 procMacroDeps := android.BazelLabelForModuleDeps(ctx, lib.baseCompiler.Properties.Proc_macros)
838
839 var rustcFLags []string
840 for _, cfg := range lib.baseCompiler.Properties.Cfgs {
841 rustcFLags = append(rustcFLags, fmt.Sprintf("--cfg=%s", cfg))
842 }
Vinh Tranbcb5f572023-08-24 11:10:01 -0400843
Vinh Tran75159942023-10-17 16:28:04 -0400844 return &commonLibraryAttrs{
Vinh Tranbcb5f572023-08-24 11:10:01 -0400845 Srcs: bazel.MakeLabelListAttribute(
846 srcs,
847 ),
848 Compile_data: bazel.MakeLabelListAttribute(
849 compileData,
850 ),
851 Crate_name: bazel.StringAttribute{
852 Value: &lib.baseCompiler.Properties.Crate_name,
853 },
854 Edition: bazel.StringAttribute{
855 Value: lib.baseCompiler.Properties.Edition,
856 },
857 Crate_features: bazel.StringListAttribute{
858 Value: lib.baseCompiler.Properties.Features,
859 },
860 Deps: bazel.MakeLabelListAttribute(
861 deps,
862 ),
Vinh Tranb4bb20f2023-08-24 11:10:01 -0400863 Proc_macro_deps: bazel.MakeLabelListAttribute(
864 procMacroDeps,
865 ),
Vinh Tranbcb5f572023-08-24 11:10:01 -0400866 Rustc_flags: bazel.StringListAttribute{
867 Value: append(
868 rustcFLags,
869 lib.baseCompiler.Properties.Flags...,
870 ),
871 },
872 }
873
Vinh Tran75159942023-10-17 16:28:04 -0400874}
875
876func libraryBp2build(ctx android.Bp2buildMutatorContext, m *Module) {
Vinh Tranfb8c5a52023-10-12 15:28:14 -0400877 ctx.CreateBazelTargetModule(
Vinh Tranbcb5f572023-08-24 11:10:01 -0400878 bazel.BazelTargetModuleProperties{
879 Rule_class: "rust_library",
880 Bzl_load_location: "@rules_rust//rust:defs.bzl",
881 },
882 android.CommonAttributes{
883 Name: m.Name(),
884 },
Vinh Tran75159942023-10-17 16:28:04 -0400885 commonLibraryAttrsBp2build(ctx, m),
Vinh Tranbcb5f572023-08-24 11:10:01 -0400886 )
887}
Vinh Tran9b846782023-08-24 12:55:12 -0400888
889type cargoBuildScriptAttributes struct {
890 Srcs bazel.LabelListAttribute
891 Edition bazel.StringAttribute
892 Version bazel.StringAttribute
893}
894
Chris Parsons637458d2023-09-19 20:09:00 +0000895func cargoBuildScriptBp2build(ctx android.Bp2buildMutatorContext, m *Module) *string {
Vinh Tran9b846782023-08-24 12:55:12 -0400896 // Soong treats some crates like libprotobuf as special in that they have
897 // cargo build script ran to produce an out folder and check it into AOSP
898 // For example, https://cs.android.com/android/platform/superproject/main/+/main:external/rust/crates/protobuf/out/
899 // is produced by cargo build script https://cs.android.com/android/platform/superproject/main/+/main:external/rust/crates/protobuf/build.rs
900 // The out folder is then fed into `rust_library` by a genrule
901 // https://cs.android.com/android/platform/superproject/main/+/main:external/rust/crates/protobuf/Android.bp;l=22
902 // This allows Soong to decouple from cargo completely.
903
904 // Soong decouples from cargo so that it has control over cc compilation.
905 // https://cs.android.com/android/platform/superproject/main/+/main:development/scripts/cargo2android.py;l=1033-1041;drc=8449944a50a0445a5ecaf9b7aed12608c81bf3f1
906 // generates a `cc_library_static` module to have custom cc flags.
907 // Since bp2build will convert the cc modules to cc targets which include the cflags,
908 // Bazel does not need to have this optimization.
909
910 // Performance-wise: rust_library -> cargo_build_script vs rust_library -> genrule (like Soong)
911 // don't have any major difference in build time in Bazel. So using cargo_build_script does not slow
912 // down the build.
913
914 // The benefit of using `cargo_build_script` here is that it would take care of setting correct
915 // `OUT_DIR` for us - similar to what Soong does here
916 // https://cs.android.com/android/platform/superproject/main/+/main:build/soong/rust/builder.go;l=202-218;drc=f29ca58e88c5846bbe8955e5192135e5ab4f14a1
917
918 // TODO(b/297364081): cargo2android.py has logic for when generate/not cc_library_static and out directory
919 // bp2build might be able use the same logic for when to use `cargo_build_script`.
920 // For now, we're building libprotobuf_build_script as a one-off until we have a more principled solution
921 if m.Name() != "libprotobuf" {
922 return nil
923 }
924
925 lib := m.compiler.(*libraryDecorator)
926
927 name := m.Name() + "_build_script"
928 attrs := &cargoBuildScriptAttributes{
929 Srcs: bazel.MakeLabelListAttribute(
930 android.BazelLabelForModuleSrc(ctx, []string{"build.rs"}),
931 ),
932 Edition: bazel.StringAttribute{
933 Value: lib.baseCompiler.Properties.Edition,
934 },
935 Version: bazel.StringAttribute{
936 Value: lib.baseCompiler.Properties.Cargo_pkg_version,
937 },
938 }
939
940 // TODO(b/290790800): Remove the restriction when rust toolchain for android is implemented
941 var restriction bazel.BoolAttribute
942 restriction.SetSelectValue(bazel.OsConfigurationAxis, "android", proptools.BoolPtr(false))
943
944 ctx.CreateBazelTargetModuleWithRestrictions(
945 bazel.BazelTargetModuleProperties{
946 Rule_class: "cargo_build_script",
947 Bzl_load_location: "@rules_rust//cargo:cargo_build_script.bzl",
948 },
949 android.CommonAttributes{
950 Name: name,
951 },
952 attrs,
953 restriction,
954 )
955
956 return &name
957}
Vinh Tran75159942023-10-17 16:28:04 -0400958
959type ffiStaticAttributes struct {
960 commonLibraryAttrs
961 Export_includes bazel.StringListAttribute
962}
963
964func ffiStaticBp2build(ctx android.Bp2buildMutatorContext, m *Module) {
965 lib := m.compiler.(*libraryDecorator)
966
967 attrs := &ffiStaticAttributes{
968 Export_includes: bazel.StringListAttribute{
969 Value: lib.Properties.Include_dirs,
970 },
971 commonLibraryAttrs: *commonLibraryAttrsBp2build(ctx, m),
972 }
973
974 ctx.CreateBazelTargetModule(
975 bazel.BazelTargetModuleProperties{
976 Rule_class: "rust_ffi_static",
977 Bzl_load_location: "//build/bazel/rules/rust:rust_ffi_static.bzl",
978 },
979 android.CommonAttributes{
980 Name: m.Name(),
981 },
982 attrs,
983 )
984}