blob: ea14e6d48825026260a84a62275ae8470c08fb6d [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
Liz Kammer356f7d42021-01-26 09:18:53 -0500249 } else if ctx.BazelConversionMode() {
250 // In Bazel conversion mode, we are currently ignoring the deptag, so we just need to supply a
251 // compatible tag in order to add the dependency.
252 return rlibAutoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700253 } else {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200254 panic(fmt.Errorf("autoDep called on library %q that has no enabled variants.", ctx.ModuleName()))
Matthew Maurer0f003b12020-06-29 14:34:06 -0700255 }
256}
257
Ivan Lozanoea086132020-12-08 14:43:00 -0500258func (library *libraryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozano1921e802021-05-20 13:39:16 -0400259 if ctx.RustModule().InVendor() {
260 // Vendor modules should statically link libstd.
261 return RlibLinkage
262 } else if library.static() || library.MutatedProperties.VariantIsStaticStd {
Ivan Lozanoea086132020-12-08 14:43:00 -0500263 return RlibLinkage
264 } else if library.baseCompiler.preferRlib() {
265 return RlibLinkage
266 }
267 return DefaultLinkage
268}
269
Ivan Lozanoffee3342019-08-27 12:03:00 -0700270var _ compiler = (*libraryDecorator)(nil)
Ivan Lozano52767be2019-10-18 14:49:46 -0700271var _ libraryInterface = (*libraryDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700272var _ exportedFlagsProducer = (*libraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700273
Matthew Maurer2ae05132020-06-23 14:28:53 -0700274// rust_library produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700275func RustLibraryFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700276 module, library := NewRustLibrary(android.HostAndDeviceSupported)
277 library.BuildOnlyRust()
278 return module.Init()
279}
280
281// rust_ffi produces all ffi variants.
282func RustFFIFactory() android.Module {
283 module, library := NewRustLibrary(android.HostAndDeviceSupported)
284 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700285 return module.Init()
286}
287
288// rust_library_dylib produces a dylib.
289func RustLibraryDylibFactory() android.Module {
290 module, library := NewRustLibrary(android.HostAndDeviceSupported)
291 library.BuildOnlyDylib()
292 return module.Init()
293}
294
295// rust_library_rlib produces an rlib.
296func RustLibraryRlibFactory() android.Module {
297 module, library := NewRustLibrary(android.HostAndDeviceSupported)
298 library.BuildOnlyRlib()
299 return module.Init()
300}
301
Matthew Maurer2ae05132020-06-23 14:28:53 -0700302// rust_ffi_shared produces a shared library.
303func RustFFISharedFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700304 module, library := NewRustLibrary(android.HostAndDeviceSupported)
305 library.BuildOnlyShared()
306 return module.Init()
307}
308
Matthew Maurer2ae05132020-06-23 14:28:53 -0700309// rust_ffi_static produces a static library.
310func 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
Matthew Maurer2ae05132020-06-23 14:28:53 -0700316// rust_library_host produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700317func RustLibraryHostFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700318 module, library := NewRustLibrary(android.HostSupported)
319 library.BuildOnlyRust()
320 return module.Init()
321}
322
323// rust_ffi_host produces all FFI variants.
324func RustFFIHostFactory() android.Module {
325 module, library := NewRustLibrary(android.HostSupported)
326 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700327 return module.Init()
328}
329
330// rust_library_dylib_host produces a dylib.
331func RustLibraryDylibHostFactory() android.Module {
332 module, library := NewRustLibrary(android.HostSupported)
333 library.BuildOnlyDylib()
334 return module.Init()
335}
336
337// rust_library_rlib_host produces an rlib.
338func RustLibraryRlibHostFactory() android.Module {
339 module, library := NewRustLibrary(android.HostSupported)
340 library.BuildOnlyRlib()
341 return module.Init()
342}
343
Matthew Maurer2ae05132020-06-23 14:28:53 -0700344// rust_ffi_static_host produces a static library.
345func RustFFIStaticHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700346 module, library := NewRustLibrary(android.HostSupported)
347 library.BuildOnlyStatic()
348 return module.Init()
349}
350
Matthew Maurer2ae05132020-06-23 14:28:53 -0700351// rust_ffi_shared_host produces an shared library.
352func RustFFISharedHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700353 module, library := NewRustLibrary(android.HostSupported)
354 library.BuildOnlyShared()
355 return module.Init()
356}
357
Matthew Maurer2ae05132020-06-23 14:28:53 -0700358func (library *libraryDecorator) BuildOnlyFFI() {
359 library.MutatedProperties.BuildDylib = false
360 library.MutatedProperties.BuildRlib = false
361 library.MutatedProperties.BuildShared = true
362 library.MutatedProperties.BuildStatic = true
363}
364
365func (library *libraryDecorator) BuildOnlyRust() {
366 library.MutatedProperties.BuildDylib = true
367 library.MutatedProperties.BuildRlib = true
368 library.MutatedProperties.BuildShared = false
369 library.MutatedProperties.BuildStatic = false
370}
371
Ivan Lozanoffee3342019-08-27 12:03:00 -0700372func (library *libraryDecorator) BuildOnlyDylib() {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700373 library.MutatedProperties.BuildDylib = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700374 library.MutatedProperties.BuildRlib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700375 library.MutatedProperties.BuildShared = false
376 library.MutatedProperties.BuildStatic = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700377}
378
379func (library *libraryDecorator) BuildOnlyRlib() {
380 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700381 library.MutatedProperties.BuildRlib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700382 library.MutatedProperties.BuildShared = false
383 library.MutatedProperties.BuildStatic = false
384}
385
386func (library *libraryDecorator) BuildOnlyStatic() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700387 library.MutatedProperties.BuildRlib = false
388 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700389 library.MutatedProperties.BuildShared = false
390 library.MutatedProperties.BuildStatic = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700391}
392
393func (library *libraryDecorator) BuildOnlyShared() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700394 library.MutatedProperties.BuildRlib = false
395 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700396 library.MutatedProperties.BuildStatic = false
397 library.MutatedProperties.BuildShared = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700398}
399
400func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400401 module := newModule(hod, android.MultilibBoth)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700402
403 library := &libraryDecorator{
404 MutatedProperties: LibraryMutatedProperties{
Matthew Maurer2ae05132020-06-23 14:28:53 -0700405 BuildDylib: false,
406 BuildRlib: false,
407 BuildShared: false,
408 BuildStatic: false,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700409 },
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800410 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Matthew Maurerbb3add12020-06-25 09:34:12 -0700411 flagExporter: NewFlagExporter(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700412 }
413
414 module.compiler = library
415
416 return module, library
417}
418
419func (library *libraryDecorator) compilerProps() []interface{} {
420 return append(library.baseCompiler.compilerProps(),
421 &library.Properties,
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200422 &library.MutatedProperties,
423 &library.stripper.StripProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700424}
425
Ivan Lozanof1c84332019-09-20 11:00:37 -0700426func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
427 deps = library.baseCompiler.compilerDeps(ctx, deps)
428
Ivan Lozano52767be2019-10-18 14:49:46 -0700429 if ctx.toolchain().Bionic() && (library.dylib() || library.shared()) {
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +0100430 deps = bionicDeps(ctx, deps, false)
Ivan Lozano12ee9ca2020-04-07 13:19:44 -0400431 deps.CrtBegin = "crtbegin_so"
432 deps.CrtEnd = "crtend_so"
Ivan Lozanof1c84332019-09-20 11:00:37 -0700433 }
434
435 return deps
436}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400437
438func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string {
439 return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
440}
441
Ivan Lozano67eada32021-09-23 11:50:33 -0400442func (library *libraryDecorator) cfgFlags(ctx ModuleContext, flags Flags) Flags {
443 flags = library.baseCompiler.cfgFlags(ctx, flags)
Stephen Crane0dbfc562021-07-07 19:05:02 -0700444 if library.dylib() {
445 // We need to add a dependency on std in order to link crates as dylibs.
446 // The hack to add this dependency is guarded by the following cfg so
447 // that we don't force a dependency when it isn't needed.
448 library.baseCompiler.Properties.Cfgs = append(library.baseCompiler.Properties.Cfgs, "android_dylib")
449 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400450
451 flags.RustFlags = append(flags.RustFlags, library.baseCompiler.cfgsToFlags()...)
452 flags.RustdocFlags = append(flags.RustdocFlags, library.baseCompiler.cfgsToFlags()...)
453
454 return flags
455}
456
457func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800458 flags = library.baseCompiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400459
460 flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800461 if library.shared() || library.static() {
462 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
463 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400464 if library.shared() {
465 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
466 }
467
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800468 return flags
469}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700470
Ivan Lozanoffee3342019-08-27 12:03:00 -0700471func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200472 var outputFile android.ModuleOutPath
473 var fileName string
Dan Albert06feee92021-03-19 15:06:02 -0700474 srcPath := library.srcPath(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700475
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400476 if library.sourceProvider != nil {
Ivan Lozano9d74a522020-12-01 09:25:22 -0500477 deps.srcProviderFiles = append(deps.srcProviderFiles, library.sourceProvider.Srcs()...)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400478 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700479
480 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500481 flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...)
Ivan Lozano2093af22020-08-25 12:48:19 -0400482 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700483
Matthew Maurer46c46cc2020-01-13 16:34:34 -0800484 if library.dylib() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700485 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
486 // https://github.com/rust-lang/rust/issues/19680
487 // https://github.com/rust-lang/rust/issues/34909
488 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
489 }
490
Ivan Lozanoffee3342019-08-27 12:03:00 -0700491 if library.rlib() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200492 fileName = library.getStem(ctx) + ctx.toolchain().RlibSuffix()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700493 outputFile = android.PathForModuleOut(ctx, fileName)
494
Dan Albert06feee92021-03-19 15:06:02 -0700495 TransformSrctoRlib(ctx, srcPath, deps, flags, outputFile)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700496 } else if library.dylib() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200497 fileName = library.getStem(ctx) + ctx.toolchain().DylibSuffix()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700498 outputFile = android.PathForModuleOut(ctx, fileName)
499
Dan Albert06feee92021-03-19 15:06:02 -0700500 TransformSrctoDylib(ctx, srcPath, deps, flags, outputFile)
Ivan Lozano52767be2019-10-18 14:49:46 -0700501 } else if library.static() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200502 fileName = library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
Ivan Lozano52767be2019-10-18 14:49:46 -0700503 outputFile = android.PathForModuleOut(ctx, fileName)
504
Dan Albert06feee92021-03-19 15:06:02 -0700505 TransformSrctoStatic(ctx, srcPath, deps, flags, outputFile)
Ivan Lozano52767be2019-10-18 14:49:46 -0700506 } else if library.shared() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200507 fileName = library.sharedLibFilename(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700508 outputFile = android.PathForModuleOut(ctx, fileName)
509
Dan Albert06feee92021-03-19 15:06:02 -0700510 TransformSrctoShared(ctx, srcPath, deps, flags, outputFile)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700511 }
512
Thiébaud Weksteen4fab05a2021-04-14 19:15:48 +0200513 if !library.rlib() && !library.static() && library.stripper.NeedsStrip(ctx) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200514 strippedOutputFile := android.PathForModuleOut(ctx, "stripped", fileName)
515 library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
516 library.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
517 }
518
Ivan Lozano52767be2019-10-18 14:49:46 -0700519 if library.rlib() || library.dylib() {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700520 library.flagExporter.exportLinkDirs(deps.linkDirs...)
Colin Cross0de8a1e2020-09-18 14:15:30 -0700521 library.flagExporter.exportLinkObjects(deps.linkObjects...)
Ivan Lozano52767be2019-10-18 14:49:46 -0700522 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700523
Colin Cross0de8a1e2020-09-18 14:15:30 -0700524 if library.static() || library.shared() {
525 ctx.SetProvider(cc.FlagExporterInfoProvider, cc.FlagExporterInfo{
526 IncludeDirs: library.includeDirs,
527 })
528 }
529
530 if library.shared() {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400531 // Optimize out relinking against shared libraries whose interface hasn't changed by
532 // depending on a table of contents file instead of the library itself.
533 tocFile := outputFile.ReplaceExtension(ctx, flags.Toolchain.SharedLibSuffix()[1:]+".toc")
534 library.tocFile = android.OptionalPathForPath(tocFile)
535 cc.TransformSharedObjectToToc(ctx, outputFile, tocFile)
536
Colin Cross0de8a1e2020-09-18 14:15:30 -0700537 ctx.SetProvider(cc.SharedLibraryInfoProvider, cc.SharedLibraryInfo{
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400538 TableOfContents: android.OptionalPathForPath(tocFile),
539 SharedLibrary: outputFile,
540 Target: ctx.Target(),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700541 })
542 }
543
544 if library.static() {
545 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(outputFile).Build()
546 ctx.SetProvider(cc.StaticLibraryInfoProvider, cc.StaticLibraryInfo{
547 StaticLibrary: outputFile,
548
549 TransitiveStaticLibrariesForOrdering: depSet,
550 })
551 }
552
553 library.flagExporter.setProvider(ctx)
554
Ivan Lozanoffee3342019-08-27 12:03:00 -0700555 return outputFile
556}
557
Dan Albert06feee92021-03-19 15:06:02 -0700558func (library *libraryDecorator) srcPath(ctx ModuleContext, deps PathDeps) android.Path {
559 if library.sourceProvider != nil {
560 // Assume the first source from the source provider is the library entry point.
561 return library.sourceProvider.Srcs()[0]
562 } else {
563 path, _ := srcPathFromModuleSrcs(ctx, library.baseCompiler.Properties.Srcs)
564 return path
565 }
566}
567
568func (library *libraryDecorator) rustdoc(ctx ModuleContext, flags Flags,
569 deps PathDeps) android.OptionalPath {
570 // rustdoc has builtin support for documenting config specific information
571 // regardless of the actual config it was given
572 // (https://doc.rust-lang.org/rustdoc/advanced-features.html#cfgdoc-documenting-platform-specific-or-feature-specific-information),
573 // so we generate the rustdoc for only the primary module so that we have a
574 // single set of docs to refer to.
575 if ctx.Module() != ctx.PrimaryModule() {
576 return android.OptionalPath{}
577 }
578
579 return android.OptionalPathForPath(Rustdoc(ctx, library.srcPath(ctx, deps),
580 deps, flags))
581}
582
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700583func (library *libraryDecorator) getStem(ctx ModuleContext) string {
584 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
585 validateLibraryStem(ctx, stem, library.crateName())
586
587 return stem + String(library.baseCompiler.Properties.Suffix)
588}
589
Ivan Lozano2b081132020-09-08 12:46:52 -0400590func (library *libraryDecorator) install(ctx ModuleContext) {
591 // Only shared and dylib variants make sense to install.
592 if library.shared() || library.dylib() {
593 library.baseCompiler.install(ctx)
594 }
595}
596
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400597func (library *libraryDecorator) Disabled() bool {
598 return library.MutatedProperties.VariantIsDisabled
599}
600
601func (library *libraryDecorator) SetDisabled() {
602 library.MutatedProperties.VariantIsDisabled = true
603}
604
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700605var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
606
607func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
608 if crate_name == "" {
609 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
610 }
611
612 // crate_names are used for the library output file, and rustc expects these
613 // to be alphanumeric with underscores allowed.
614 if validCrateName.MatchString(crate_name) {
615 ctx.PropertyErrorf("crate_name",
616 "library crate_names must be alphanumeric with underscores allowed")
617 }
618
619 // Libraries are expected to begin with "lib" followed by the crate_name
620 if !strings.HasPrefix(filename, "lib"+crate_name) {
621 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
622 }
623}
624
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200625// LibraryMutator mutates the libraries into variants according to the
626// build{Rlib,Dylib} attributes.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700627func LibraryMutator(mctx android.BottomUpMutatorContext) {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200628 // Only mutate on Rust libraries.
629 m, ok := mctx.Module().(*Module)
630 if !ok || m.compiler == nil {
631 return
632 }
633 library, ok := m.compiler.(libraryInterface)
634 if !ok {
635 return
636 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400637
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200638 var variants []string
639 // The source variant is used for SourceProvider modules. The other variants (i.e. rlib and dylib)
640 // depend on this variant. It must be the first variant to be declared.
641 sourceVariant := false
642 if m.sourceProvider != nil {
643 variants = append(variants, "source")
644 sourceVariant = true
645 }
646 if library.buildRlib() {
647 variants = append(variants, rlibVariation)
648 }
649 if library.buildDylib() {
650 variants = append(variants, dylibVariation)
651 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400652
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200653 if len(variants) == 0 {
654 return
655 }
656 modules := mctx.CreateLocalVariations(variants...)
657
658 // The order of the variations (modules) matches the variant names provided. Iterate
659 // through the new variation modules and set their mutated properties.
660 for i, v := range modules {
661 switch variants[i] {
662 case rlibVariation:
663 v.(*Module).compiler.(libraryInterface).setRlib()
664 case dylibVariation:
665 v.(*Module).compiler.(libraryInterface).setDylib()
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400666 if v.(*Module).ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
Ivan Lozano6a884432020-12-02 09:15:16 -0500667 // TODO(b/165791368)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400668 // Disable dylib Vendor Ramdisk variations until we support these.
Ivan Lozano6a884432020-12-02 09:15:16 -0500669 v.(*Module).Disable()
670 }
Ivan Lozano1921e802021-05-20 13:39:16 -0400671
672 variation := v.(*Module).ModuleBase.ImageVariation().Variation
673 if strings.HasPrefix(variation, cc.VendorVariationPrefix) &&
674 m.HasVendorVariant() &&
Kiyoung Kim48f37782021-07-07 12:42:39 +0900675 !snapshot.IsVendorProprietaryModule(mctx) &&
Ivan Lozano1921e802021-05-20 13:39:16 -0400676 strings.TrimPrefix(variation, cc.VendorVariationPrefix) == mctx.DeviceConfig().VndkVersion() {
677
678 // cc.MutateImage runs before LibraryMutator, so vendor variations which are meant for rlibs only are
679 // produced for Dylibs; however, dylibs should not be enabled for boardVndkVersion for
680 // non-vendor proprietary modules.
681 v.(*Module).Disable()
682 }
683
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200684 case "source":
685 v.(*Module).compiler.(libraryInterface).setSource()
686 // The source variant does not produce any library.
687 // Disable the compilation steps.
688 v.(*Module).compiler.SetDisabled()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700689 }
690 }
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200691
692 // If a source variant is created, add an inter-variant dependency
693 // between the other variants and the source variant.
694 if sourceVariant {
695 sv := modules[0]
696 for _, v := range modules[1:] {
697 if !v.Enabled() {
698 continue
699 }
700 mctx.AddInterVariantDependency(sourceDepTag, v, sv)
701 }
702 // Alias the source variation so it can be named directly in "srcs" properties.
703 mctx.AliasVariation("source")
704 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700705}
Ivan Lozano2b081132020-09-08 12:46:52 -0400706
707func LibstdMutator(mctx android.BottomUpMutatorContext) {
708 if m, ok := mctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() {
709 switch library := m.compiler.(type) {
710 case libraryInterface:
711 // Only create a variant if a library is actually being built.
712 if library.rlib() && !library.sysroot() {
713 variants := []string{"rlib-std", "dylib-std"}
714 modules := mctx.CreateLocalVariations(variants...)
715
716 rlib := modules[0].(*Module)
717 dylib := modules[1].(*Module)
718 rlib.compiler.(libraryInterface).setRlibStd()
719 dylib.compiler.(libraryInterface).setDylibStd()
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400720 if dylib.ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation ||
721 strings.HasPrefix(dylib.ModuleBase.ImageVariation().Variation, cc.VendorVariationPrefix) {
Ivan Lozano6a884432020-12-02 09:15:16 -0500722 // TODO(b/165791368)
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400723 // Disable rlibs that link against dylib-std on vendor and vendor ramdisk variations until those dylib
Ivan Lozano6a884432020-12-02 09:15:16 -0500724 // variants are properly supported.
725 dylib.Disable()
726 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400727 rlib.Properties.RustSubName += RlibStdlibSuffix
728 dylib.Properties.RustSubName += DylibStdlibSuffix
Ivan Lozano2b081132020-09-08 12:46:52 -0400729 }
730 }
731 }
732}
Ivan Lozano1921e802021-05-20 13:39:16 -0400733
734func (l *libraryDecorator) snapshotHeaders() android.Paths {
735 if l.collectedSnapshotHeaders == nil {
736 panic("snapshotHeaders() must be called after collectHeadersForSnapshot()")
737 }
738 return l.collectedSnapshotHeaders
739}
740
741// collectHeadersForSnapshot collects all exported headers from library.
742// It globs header files in the source tree for exported include directories,
743// and tracks generated header files separately.
744//
745// This is to be called from GenerateAndroidBuildActions, and then collected
746// header files can be retrieved by snapshotHeaders().
747func (l *libraryDecorator) collectHeadersForSnapshot(ctx android.ModuleContext, deps PathDeps) {
748 ret := android.Paths{}
749
750 // Glob together the headers from the modules include_dirs property
751 for _, path := range android.CopyOfPaths(l.includeDirs) {
752 dir := path.String()
753 glob, err := ctx.GlobWithDeps(dir+"/**/*", nil)
754 if err != nil {
755 ctx.ModuleErrorf("glob failed: %#v", err)
756 return
757 }
758
759 for _, header := range glob {
760 // Filter out only the files with extensions that are headers.
761 found := false
762 for _, ext := range cc.HeaderExts {
763 if strings.HasSuffix(header, ext) {
764 found = true
765 break
766 }
767 }
768 if !found {
769 continue
770 }
771 ret = append(ret, android.PathForSource(ctx, header))
772 }
773 }
774
775 // Glob together the headers from C dependencies as well, starting with non-generated headers.
776 ret = append(ret, cc.GlobHeadersForSnapshot(ctx, append(android.CopyOfPaths(deps.depIncludePaths), deps.depSystemIncludePaths...))...)
777
778 // Collect generated headers from C dependencies.
779 ret = append(ret, cc.GlobGeneratedHeadersForSnapshot(ctx, deps.depGeneratedHeaders)...)
780
781 // TODO(185577950): If support for generated headers is added, they need to be collected here as well.
782 l.collectedSnapshotHeaders = ret
783}