blob: 38dae4d33673644a09461d48f8f5d19a58e153bf [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 Lozanoffee3342019-08-27 12:03:00 -0700105}
106
107type libraryInterface interface {
108 rlib() bool
109 dylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700110 static() bool
111 shared() bool
Ivan Lozano2b081132020-09-08 12:46:52 -0400112 sysroot() bool
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200113 source() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700114
115 // Returns true if the build options for the module have selected a particular build type
116 buildRlib() bool
117 buildDylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700118 buildShared() bool
119 buildStatic() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700120
121 // Sets a particular variant type
122 setRlib()
123 setDylib()
Ivan Lozano52767be2019-10-18 14:49:46 -0700124 setShared()
125 setStatic()
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200126 setSource()
Ivan Lozano52767be2019-10-18 14:49:46 -0700127
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400128 // libstd linkage functions
129 rlibStd() bool
Ivan Lozano2b081132020-09-08 12:46:52 -0400130 setRlibStd()
131 setDylibStd()
132
Ivan Lozano52767be2019-10-18 14:49:46 -0700133 // Build a specific library variant
Matthew Maurer2ae05132020-06-23 14:28:53 -0700134 BuildOnlyFFI()
135 BuildOnlyRust()
Ivan Lozano52767be2019-10-18 14:49:46 -0700136 BuildOnlyRlib()
137 BuildOnlyDylib()
138 BuildOnlyStatic()
139 BuildOnlyShared()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700140}
141
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400142func (library *libraryDecorator) nativeCoverage() bool {
143 return true
144}
145
Ivan Lozanoffee3342019-08-27 12:03:00 -0700146func (library *libraryDecorator) rlib() bool {
147 return library.MutatedProperties.VariantIsRlib
148}
149
Ivan Lozano2b081132020-09-08 12:46:52 -0400150func (library *libraryDecorator) sysroot() bool {
151 return Bool(library.Properties.Sysroot)
152}
153
Ivan Lozanoffee3342019-08-27 12:03:00 -0700154func (library *libraryDecorator) dylib() bool {
155 return library.MutatedProperties.VariantIsDylib
156}
157
Ivan Lozano52767be2019-10-18 14:49:46 -0700158func (library *libraryDecorator) shared() bool {
159 return library.MutatedProperties.VariantIsShared
160}
161
162func (library *libraryDecorator) static() bool {
163 return library.MutatedProperties.VariantIsStatic
164}
165
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200166func (library *libraryDecorator) source() bool {
167 return library.MutatedProperties.VariantIsSource
168}
169
Ivan Lozanoffee3342019-08-27 12:03:00 -0700170func (library *libraryDecorator) buildRlib() bool {
171 return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true)
172}
173
174func (library *libraryDecorator) buildDylib() bool {
175 return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true)
176}
177
Ivan Lozano52767be2019-10-18 14:49:46 -0700178func (library *libraryDecorator) buildShared() bool {
179 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
180}
181
182func (library *libraryDecorator) buildStatic() bool {
183 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
184}
185
Ivan Lozanoffee3342019-08-27 12:03:00 -0700186func (library *libraryDecorator) setRlib() {
187 library.MutatedProperties.VariantIsRlib = true
188 library.MutatedProperties.VariantIsDylib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700189 library.MutatedProperties.VariantIsStatic = false
190 library.MutatedProperties.VariantIsShared = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700191}
192
193func (library *libraryDecorator) setDylib() {
194 library.MutatedProperties.VariantIsRlib = false
195 library.MutatedProperties.VariantIsDylib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700196 library.MutatedProperties.VariantIsStatic = false
197 library.MutatedProperties.VariantIsShared = false
198}
199
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400200func (library *libraryDecorator) rlibStd() bool {
201 return library.MutatedProperties.VariantIsStaticStd
202}
203
Ivan Lozano2b081132020-09-08 12:46:52 -0400204func (library *libraryDecorator) setRlibStd() {
205 library.MutatedProperties.VariantIsStaticStd = true
206}
207
208func (library *libraryDecorator) setDylibStd() {
209 library.MutatedProperties.VariantIsStaticStd = false
210}
211
Ivan Lozano52767be2019-10-18 14:49:46 -0700212func (library *libraryDecorator) setShared() {
213 library.MutatedProperties.VariantIsStatic = false
214 library.MutatedProperties.VariantIsShared = true
215 library.MutatedProperties.VariantIsRlib = false
216 library.MutatedProperties.VariantIsDylib = false
217}
218
219func (library *libraryDecorator) setStatic() {
220 library.MutatedProperties.VariantIsStatic = true
221 library.MutatedProperties.VariantIsShared = false
222 library.MutatedProperties.VariantIsRlib = false
223 library.MutatedProperties.VariantIsDylib = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700224}
225
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200226func (library *libraryDecorator) setSource() {
227 library.MutatedProperties.VariantIsSource = true
228}
229
Liz Kammer356f7d42021-01-26 09:18:53 -0500230func (library *libraryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Ivan Lozano1921e802021-05-20 13:39:16 -0400231 if ctx.Module().(*Module).InVendor() {
232 // Vendor modules should statically link libstd.
233 return rlibAutoDep
234 } else if library.preferRlib() {
Ivan Lozanoea086132020-12-08 14:43:00 -0500235 return rlibAutoDep
236 } else if library.rlib() || library.static() {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700237 return rlibAutoDep
238 } else if library.dylib() || library.shared() {
239 return dylibAutoDep
Liz Kammer356f7d42021-01-26 09:18:53 -0500240 } else if ctx.BazelConversionMode() {
241 // In Bazel conversion mode, we are currently ignoring the deptag, so we just need to supply a
242 // compatible tag in order to add the dependency.
243 return rlibAutoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700244 } else {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200245 panic(fmt.Errorf("autoDep called on library %q that has no enabled variants.", ctx.ModuleName()))
Matthew Maurer0f003b12020-06-29 14:34:06 -0700246 }
247}
248
Ivan Lozanoea086132020-12-08 14:43:00 -0500249func (library *libraryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozano1921e802021-05-20 13:39:16 -0400250 if ctx.RustModule().InVendor() {
251 // Vendor modules should statically link libstd.
252 return RlibLinkage
253 } else if library.static() || library.MutatedProperties.VariantIsStaticStd {
Ivan Lozanoea086132020-12-08 14:43:00 -0500254 return RlibLinkage
255 } else if library.baseCompiler.preferRlib() {
256 return RlibLinkage
257 }
258 return DefaultLinkage
259}
260
Ivan Lozanoffee3342019-08-27 12:03:00 -0700261var _ compiler = (*libraryDecorator)(nil)
Ivan Lozano52767be2019-10-18 14:49:46 -0700262var _ libraryInterface = (*libraryDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700263var _ exportedFlagsProducer = (*libraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700264
Matthew Maurer2ae05132020-06-23 14:28:53 -0700265// rust_library produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700266func RustLibraryFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700267 module, library := NewRustLibrary(android.HostAndDeviceSupported)
268 library.BuildOnlyRust()
269 return module.Init()
270}
271
272// rust_ffi produces all ffi variants.
273func RustFFIFactory() android.Module {
274 module, library := NewRustLibrary(android.HostAndDeviceSupported)
275 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700276 return module.Init()
277}
278
279// rust_library_dylib produces a dylib.
280func RustLibraryDylibFactory() android.Module {
281 module, library := NewRustLibrary(android.HostAndDeviceSupported)
282 library.BuildOnlyDylib()
283 return module.Init()
284}
285
286// rust_library_rlib produces an rlib.
287func RustLibraryRlibFactory() android.Module {
288 module, library := NewRustLibrary(android.HostAndDeviceSupported)
289 library.BuildOnlyRlib()
290 return module.Init()
291}
292
Matthew Maurer2ae05132020-06-23 14:28:53 -0700293// rust_ffi_shared produces a shared library.
294func RustFFISharedFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700295 module, library := NewRustLibrary(android.HostAndDeviceSupported)
296 library.BuildOnlyShared()
297 return module.Init()
298}
299
Matthew Maurer2ae05132020-06-23 14:28:53 -0700300// rust_ffi_static produces a static library.
301func RustFFIStaticFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700302 module, library := NewRustLibrary(android.HostAndDeviceSupported)
303 library.BuildOnlyStatic()
304 return module.Init()
305}
306
Matthew Maurer2ae05132020-06-23 14:28:53 -0700307// rust_library_host produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700308func RustLibraryHostFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700309 module, library := NewRustLibrary(android.HostSupported)
310 library.BuildOnlyRust()
311 return module.Init()
312}
313
314// rust_ffi_host produces all FFI variants.
315func RustFFIHostFactory() android.Module {
316 module, library := NewRustLibrary(android.HostSupported)
317 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700318 return module.Init()
319}
320
321// rust_library_dylib_host produces a dylib.
322func RustLibraryDylibHostFactory() android.Module {
323 module, library := NewRustLibrary(android.HostSupported)
324 library.BuildOnlyDylib()
325 return module.Init()
326}
327
328// rust_library_rlib_host produces an rlib.
329func RustLibraryRlibHostFactory() android.Module {
330 module, library := NewRustLibrary(android.HostSupported)
331 library.BuildOnlyRlib()
332 return module.Init()
333}
334
Matthew Maurer2ae05132020-06-23 14:28:53 -0700335// rust_ffi_static_host produces a static library.
336func RustFFIStaticHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700337 module, library := NewRustLibrary(android.HostSupported)
338 library.BuildOnlyStatic()
339 return module.Init()
340}
341
Matthew Maurer2ae05132020-06-23 14:28:53 -0700342// rust_ffi_shared_host produces an shared library.
343func RustFFISharedHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700344 module, library := NewRustLibrary(android.HostSupported)
345 library.BuildOnlyShared()
346 return module.Init()
347}
348
Matthew Maurer2ae05132020-06-23 14:28:53 -0700349func (library *libraryDecorator) BuildOnlyFFI() {
350 library.MutatedProperties.BuildDylib = false
351 library.MutatedProperties.BuildRlib = false
352 library.MutatedProperties.BuildShared = true
353 library.MutatedProperties.BuildStatic = true
354}
355
356func (library *libraryDecorator) BuildOnlyRust() {
357 library.MutatedProperties.BuildDylib = true
358 library.MutatedProperties.BuildRlib = true
359 library.MutatedProperties.BuildShared = false
360 library.MutatedProperties.BuildStatic = false
361}
362
Ivan Lozanoffee3342019-08-27 12:03:00 -0700363func (library *libraryDecorator) BuildOnlyDylib() {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700364 library.MutatedProperties.BuildDylib = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700365 library.MutatedProperties.BuildRlib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700366 library.MutatedProperties.BuildShared = false
367 library.MutatedProperties.BuildStatic = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700368}
369
370func (library *libraryDecorator) BuildOnlyRlib() {
371 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700372 library.MutatedProperties.BuildRlib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700373 library.MutatedProperties.BuildShared = false
374 library.MutatedProperties.BuildStatic = false
375}
376
377func (library *libraryDecorator) BuildOnlyStatic() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700378 library.MutatedProperties.BuildRlib = false
379 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700380 library.MutatedProperties.BuildShared = false
381 library.MutatedProperties.BuildStatic = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700382}
383
384func (library *libraryDecorator) BuildOnlyShared() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700385 library.MutatedProperties.BuildRlib = false
386 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700387 library.MutatedProperties.BuildStatic = false
388 library.MutatedProperties.BuildShared = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700389}
390
391func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400392 module := newModule(hod, android.MultilibBoth)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700393
394 library := &libraryDecorator{
395 MutatedProperties: LibraryMutatedProperties{
Matthew Maurer2ae05132020-06-23 14:28:53 -0700396 BuildDylib: false,
397 BuildRlib: false,
398 BuildShared: false,
399 BuildStatic: false,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700400 },
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800401 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Matthew Maurerbb3add12020-06-25 09:34:12 -0700402 flagExporter: NewFlagExporter(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700403 }
404
405 module.compiler = library
406
407 return module, library
408}
409
410func (library *libraryDecorator) compilerProps() []interface{} {
411 return append(library.baseCompiler.compilerProps(),
412 &library.Properties,
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200413 &library.MutatedProperties,
414 &library.stripper.StripProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700415}
416
Ivan Lozanof1c84332019-09-20 11:00:37 -0700417func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
418 deps = library.baseCompiler.compilerDeps(ctx, deps)
419
Ivan Lozano52767be2019-10-18 14:49:46 -0700420 if ctx.toolchain().Bionic() && (library.dylib() || library.shared()) {
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +0100421 deps = bionicDeps(ctx, deps, false)
Ivan Lozano12ee9ca2020-04-07 13:19:44 -0400422 deps.CrtBegin = "crtbegin_so"
423 deps.CrtEnd = "crtend_so"
Ivan Lozanof1c84332019-09-20 11:00:37 -0700424 }
425
426 return deps
427}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400428
429func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string {
430 return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
431}
432
Ivan Lozano67eada32021-09-23 11:50:33 -0400433func (library *libraryDecorator) cfgFlags(ctx ModuleContext, flags Flags) Flags {
434 flags = library.baseCompiler.cfgFlags(ctx, flags)
Stephen Crane0dbfc562021-07-07 19:05:02 -0700435 if library.dylib() {
436 // We need to add a dependency on std in order to link crates as dylibs.
437 // The hack to add this dependency is guarded by the following cfg so
438 // that we don't force a dependency when it isn't needed.
439 library.baseCompiler.Properties.Cfgs = append(library.baseCompiler.Properties.Cfgs, "android_dylib")
440 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400441
442 flags.RustFlags = append(flags.RustFlags, library.baseCompiler.cfgsToFlags()...)
443 flags.RustdocFlags = append(flags.RustdocFlags, library.baseCompiler.cfgsToFlags()...)
444
445 return flags
446}
447
448func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800449 flags = library.baseCompiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400450
451 flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800452 if library.shared() || library.static() {
453 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
454 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400455 if library.shared() {
456 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
457 }
458
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800459 return flags
460}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700461
Ivan Lozanoffee3342019-08-27 12:03:00 -0700462func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200463 var outputFile android.ModuleOutPath
464 var fileName string
Dan Albert06feee92021-03-19 15:06:02 -0700465 srcPath := library.srcPath(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700466
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400467 if library.sourceProvider != nil {
Ivan Lozano9d74a522020-12-01 09:25:22 -0500468 deps.srcProviderFiles = append(deps.srcProviderFiles, library.sourceProvider.Srcs()...)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400469 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700470
471 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500472 flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...)
Ivan Lozano2093af22020-08-25 12:48:19 -0400473 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700474
Matthew Maurer46c46cc2020-01-13 16:34:34 -0800475 if library.dylib() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700476 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
477 // https://github.com/rust-lang/rust/issues/19680
478 // https://github.com/rust-lang/rust/issues/34909
479 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
480 }
481
Ivan Lozanoffee3342019-08-27 12:03:00 -0700482 if library.rlib() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200483 fileName = library.getStem(ctx) + ctx.toolchain().RlibSuffix()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700484 outputFile = android.PathForModuleOut(ctx, fileName)
485
Dan Albert06feee92021-03-19 15:06:02 -0700486 TransformSrctoRlib(ctx, srcPath, deps, flags, outputFile)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700487 } else if library.dylib() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200488 fileName = library.getStem(ctx) + ctx.toolchain().DylibSuffix()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700489 outputFile = android.PathForModuleOut(ctx, fileName)
490
Dan Albert06feee92021-03-19 15:06:02 -0700491 TransformSrctoDylib(ctx, srcPath, deps, flags, outputFile)
Ivan Lozano52767be2019-10-18 14:49:46 -0700492 } else if library.static() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200493 fileName = library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
Ivan Lozano52767be2019-10-18 14:49:46 -0700494 outputFile = android.PathForModuleOut(ctx, fileName)
495
Dan Albert06feee92021-03-19 15:06:02 -0700496 TransformSrctoStatic(ctx, srcPath, deps, flags, outputFile)
Ivan Lozano52767be2019-10-18 14:49:46 -0700497 } else if library.shared() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200498 fileName = library.sharedLibFilename(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700499 outputFile = android.PathForModuleOut(ctx, fileName)
500
Dan Albert06feee92021-03-19 15:06:02 -0700501 TransformSrctoShared(ctx, srcPath, deps, flags, outputFile)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700502 }
503
Thiébaud Weksteen4fab05a2021-04-14 19:15:48 +0200504 if !library.rlib() && !library.static() && library.stripper.NeedsStrip(ctx) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200505 strippedOutputFile := android.PathForModuleOut(ctx, "stripped", fileName)
506 library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
507 library.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
508 }
509
Ivan Lozano52767be2019-10-18 14:49:46 -0700510 if library.rlib() || library.dylib() {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700511 library.flagExporter.exportLinkDirs(deps.linkDirs...)
Colin Cross0de8a1e2020-09-18 14:15:30 -0700512 library.flagExporter.exportLinkObjects(deps.linkObjects...)
Ivan Lozano52767be2019-10-18 14:49:46 -0700513 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700514
Colin Cross0de8a1e2020-09-18 14:15:30 -0700515 if library.static() || library.shared() {
516 ctx.SetProvider(cc.FlagExporterInfoProvider, cc.FlagExporterInfo{
517 IncludeDirs: library.includeDirs,
518 })
519 }
520
521 if library.shared() {
522 ctx.SetProvider(cc.SharedLibraryInfoProvider, cc.SharedLibraryInfo{
Liz Kammeref6dfea2021-06-08 15:37:09 -0400523 SharedLibrary: outputFile,
524 Target: ctx.Target(),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700525 })
526 }
527
528 if library.static() {
529 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(outputFile).Build()
530 ctx.SetProvider(cc.StaticLibraryInfoProvider, cc.StaticLibraryInfo{
531 StaticLibrary: outputFile,
532
533 TransitiveStaticLibrariesForOrdering: depSet,
534 })
535 }
536
537 library.flagExporter.setProvider(ctx)
538
Ivan Lozanoffee3342019-08-27 12:03:00 -0700539 return outputFile
540}
541
Dan Albert06feee92021-03-19 15:06:02 -0700542func (library *libraryDecorator) srcPath(ctx ModuleContext, deps PathDeps) android.Path {
543 if library.sourceProvider != nil {
544 // Assume the first source from the source provider is the library entry point.
545 return library.sourceProvider.Srcs()[0]
546 } else {
547 path, _ := srcPathFromModuleSrcs(ctx, library.baseCompiler.Properties.Srcs)
548 return path
549 }
550}
551
552func (library *libraryDecorator) rustdoc(ctx ModuleContext, flags Flags,
553 deps PathDeps) android.OptionalPath {
554 // rustdoc has builtin support for documenting config specific information
555 // regardless of the actual config it was given
556 // (https://doc.rust-lang.org/rustdoc/advanced-features.html#cfgdoc-documenting-platform-specific-or-feature-specific-information),
557 // so we generate the rustdoc for only the primary module so that we have a
558 // single set of docs to refer to.
559 if ctx.Module() != ctx.PrimaryModule() {
560 return android.OptionalPath{}
561 }
562
563 return android.OptionalPathForPath(Rustdoc(ctx, library.srcPath(ctx, deps),
564 deps, flags))
565}
566
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700567func (library *libraryDecorator) getStem(ctx ModuleContext) string {
568 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
569 validateLibraryStem(ctx, stem, library.crateName())
570
571 return stem + String(library.baseCompiler.Properties.Suffix)
572}
573
Ivan Lozano2b081132020-09-08 12:46:52 -0400574func (library *libraryDecorator) install(ctx ModuleContext) {
575 // Only shared and dylib variants make sense to install.
576 if library.shared() || library.dylib() {
577 library.baseCompiler.install(ctx)
578 }
579}
580
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400581func (library *libraryDecorator) Disabled() bool {
582 return library.MutatedProperties.VariantIsDisabled
583}
584
585func (library *libraryDecorator) SetDisabled() {
586 library.MutatedProperties.VariantIsDisabled = true
587}
588
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700589var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
590
591func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
592 if crate_name == "" {
593 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
594 }
595
596 // crate_names are used for the library output file, and rustc expects these
597 // to be alphanumeric with underscores allowed.
598 if validCrateName.MatchString(crate_name) {
599 ctx.PropertyErrorf("crate_name",
600 "library crate_names must be alphanumeric with underscores allowed")
601 }
602
603 // Libraries are expected to begin with "lib" followed by the crate_name
604 if !strings.HasPrefix(filename, "lib"+crate_name) {
605 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
606 }
607}
608
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200609// LibraryMutator mutates the libraries into variants according to the
610// build{Rlib,Dylib} attributes.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700611func LibraryMutator(mctx android.BottomUpMutatorContext) {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200612 // Only mutate on Rust libraries.
613 m, ok := mctx.Module().(*Module)
614 if !ok || m.compiler == nil {
615 return
616 }
617 library, ok := m.compiler.(libraryInterface)
618 if !ok {
619 return
620 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400621
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200622 var variants []string
623 // The source variant is used for SourceProvider modules. The other variants (i.e. rlib and dylib)
624 // depend on this variant. It must be the first variant to be declared.
625 sourceVariant := false
626 if m.sourceProvider != nil {
627 variants = append(variants, "source")
628 sourceVariant = true
629 }
630 if library.buildRlib() {
631 variants = append(variants, rlibVariation)
632 }
633 if library.buildDylib() {
634 variants = append(variants, dylibVariation)
635 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400636
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200637 if len(variants) == 0 {
638 return
639 }
640 modules := mctx.CreateLocalVariations(variants...)
641
642 // The order of the variations (modules) matches the variant names provided. Iterate
643 // through the new variation modules and set their mutated properties.
644 for i, v := range modules {
645 switch variants[i] {
646 case rlibVariation:
647 v.(*Module).compiler.(libraryInterface).setRlib()
648 case dylibVariation:
649 v.(*Module).compiler.(libraryInterface).setDylib()
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400650 if v.(*Module).ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
Ivan Lozano6a884432020-12-02 09:15:16 -0500651 // TODO(b/165791368)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400652 // Disable dylib Vendor Ramdisk variations until we support these.
Ivan Lozano6a884432020-12-02 09:15:16 -0500653 v.(*Module).Disable()
654 }
Ivan Lozano1921e802021-05-20 13:39:16 -0400655
656 variation := v.(*Module).ModuleBase.ImageVariation().Variation
657 if strings.HasPrefix(variation, cc.VendorVariationPrefix) &&
658 m.HasVendorVariant() &&
Kiyoung Kim48f37782021-07-07 12:42:39 +0900659 !snapshot.IsVendorProprietaryModule(mctx) &&
Ivan Lozano1921e802021-05-20 13:39:16 -0400660 strings.TrimPrefix(variation, cc.VendorVariationPrefix) == mctx.DeviceConfig().VndkVersion() {
661
662 // cc.MutateImage runs before LibraryMutator, so vendor variations which are meant for rlibs only are
663 // produced for Dylibs; however, dylibs should not be enabled for boardVndkVersion for
664 // non-vendor proprietary modules.
665 v.(*Module).Disable()
666 }
667
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200668 case "source":
669 v.(*Module).compiler.(libraryInterface).setSource()
670 // The source variant does not produce any library.
671 // Disable the compilation steps.
672 v.(*Module).compiler.SetDisabled()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700673 }
674 }
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200675
676 // If a source variant is created, add an inter-variant dependency
677 // between the other variants and the source variant.
678 if sourceVariant {
679 sv := modules[0]
680 for _, v := range modules[1:] {
681 if !v.Enabled() {
682 continue
683 }
684 mctx.AddInterVariantDependency(sourceDepTag, v, sv)
685 }
686 // Alias the source variation so it can be named directly in "srcs" properties.
687 mctx.AliasVariation("source")
688 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700689}
Ivan Lozano2b081132020-09-08 12:46:52 -0400690
691func LibstdMutator(mctx android.BottomUpMutatorContext) {
692 if m, ok := mctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() {
693 switch library := m.compiler.(type) {
694 case libraryInterface:
695 // Only create a variant if a library is actually being built.
696 if library.rlib() && !library.sysroot() {
697 variants := []string{"rlib-std", "dylib-std"}
698 modules := mctx.CreateLocalVariations(variants...)
699
700 rlib := modules[0].(*Module)
701 dylib := modules[1].(*Module)
702 rlib.compiler.(libraryInterface).setRlibStd()
703 dylib.compiler.(libraryInterface).setDylibStd()
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400704 if dylib.ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation ||
705 strings.HasPrefix(dylib.ModuleBase.ImageVariation().Variation, cc.VendorVariationPrefix) {
Ivan Lozano6a884432020-12-02 09:15:16 -0500706 // TODO(b/165791368)
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400707 // Disable rlibs that link against dylib-std on vendor and vendor ramdisk variations until those dylib
Ivan Lozano6a884432020-12-02 09:15:16 -0500708 // variants are properly supported.
709 dylib.Disable()
710 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400711 rlib.Properties.RustSubName += RlibStdlibSuffix
712 dylib.Properties.RustSubName += DylibStdlibSuffix
Ivan Lozano2b081132020-09-08 12:46:52 -0400713 }
714 }
715 }
716}
Ivan Lozano1921e802021-05-20 13:39:16 -0400717
718func (l *libraryDecorator) snapshotHeaders() android.Paths {
719 if l.collectedSnapshotHeaders == nil {
720 panic("snapshotHeaders() must be called after collectHeadersForSnapshot()")
721 }
722 return l.collectedSnapshotHeaders
723}
724
725// collectHeadersForSnapshot collects all exported headers from library.
726// It globs header files in the source tree for exported include directories,
727// and tracks generated header files separately.
728//
729// This is to be called from GenerateAndroidBuildActions, and then collected
730// header files can be retrieved by snapshotHeaders().
731func (l *libraryDecorator) collectHeadersForSnapshot(ctx android.ModuleContext, deps PathDeps) {
732 ret := android.Paths{}
733
734 // Glob together the headers from the modules include_dirs property
735 for _, path := range android.CopyOfPaths(l.includeDirs) {
736 dir := path.String()
737 glob, err := ctx.GlobWithDeps(dir+"/**/*", nil)
738 if err != nil {
739 ctx.ModuleErrorf("glob failed: %#v", err)
740 return
741 }
742
743 for _, header := range glob {
744 // Filter out only the files with extensions that are headers.
745 found := false
746 for _, ext := range cc.HeaderExts {
747 if strings.HasSuffix(header, ext) {
748 found = true
749 break
750 }
751 }
752 if !found {
753 continue
754 }
755 ret = append(ret, android.PathForSource(ctx, header))
756 }
757 }
758
759 // Glob together the headers from C dependencies as well, starting with non-generated headers.
760 ret = append(ret, cc.GlobHeadersForSnapshot(ctx, append(android.CopyOfPaths(deps.depIncludePaths), deps.depSystemIncludePaths...))...)
761
762 // Collect generated headers from C dependencies.
763 ret = append(ret, cc.GlobGeneratedHeadersForSnapshot(ctx, deps.depGeneratedHeaders)...)
764
765 // TODO(185577950): If support for generated headers is added, they need to be collected here as well.
766 l.collectedSnapshotHeaders = ret
767}