blob: 7421817ccf4991dcd3d45a71609b7fb50a17fd26 [file] [log] [blame]
Ivan Lozanoffee3342019-08-27 12:03:00 -07001// Copyright 2019 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package rust
16
17import (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +020018 "fmt"
Ivan Lozanoad8b18b2019-10-31 19:38:29 -070019 "regexp"
20 "strings"
21
Ivan Lozanoffee3342019-08-27 12:03:00 -070022 "android/soong/android"
Colin Cross0de8a1e2020-09-18 14:15:30 -070023 "android/soong/cc"
Ivan Lozanoffee3342019-08-27 12:03:00 -070024)
25
Ivan Lozano2b081132020-09-08 12:46:52 -040026var (
27 DylibStdlibSuffix = ".dylib-std"
28 RlibStdlibSuffix = ".rlib-std"
29)
30
Ivan Lozanoffee3342019-08-27 12:03:00 -070031func init() {
32 android.RegisterModuleType("rust_library", RustLibraryFactory)
33 android.RegisterModuleType("rust_library_dylib", RustLibraryDylibFactory)
34 android.RegisterModuleType("rust_library_rlib", RustLibraryRlibFactory)
35 android.RegisterModuleType("rust_library_host", RustLibraryHostFactory)
36 android.RegisterModuleType("rust_library_host_dylib", RustLibraryDylibHostFactory)
37 android.RegisterModuleType("rust_library_host_rlib", RustLibraryRlibHostFactory)
Matthew Maurer2ae05132020-06-23 14:28:53 -070038 android.RegisterModuleType("rust_ffi", RustFFIFactory)
39 android.RegisterModuleType("rust_ffi_shared", RustFFISharedFactory)
40 android.RegisterModuleType("rust_ffi_static", RustFFIStaticFactory)
41 android.RegisterModuleType("rust_ffi_host", RustFFIHostFactory)
42 android.RegisterModuleType("rust_ffi_host_shared", RustFFISharedHostFactory)
43 android.RegisterModuleType("rust_ffi_host_static", RustFFIStaticHostFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070044}
45
46type VariantLibraryProperties struct {
Matthew Maurerc761eec2020-06-25 00:47:46 -070047 Enabled *bool `android:"arch_variant"`
48 Srcs []string `android:"path,arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070049}
50
51type LibraryCompilerProperties struct {
Ivan Lozano52767be2019-10-18 14:49:46 -070052 Rlib VariantLibraryProperties `android:"arch_variant"`
53 Dylib VariantLibraryProperties `android:"arch_variant"`
54 Shared VariantLibraryProperties `android:"arch_variant"`
55 Static VariantLibraryProperties `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070056
Ivan Lozano52767be2019-10-18 14:49:46 -070057 // path to include directories to pass to cc_* modules, only relevant for static/shared variants.
58 Include_dirs []string `android:"path,arch_variant"`
Ivan Lozano2b081132020-09-08 12:46:52 -040059
60 // Whether this library is part of the Rust toolchain sysroot.
61 Sysroot *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070062}
63
64type LibraryMutatedProperties struct {
Ivan Lozanoffee3342019-08-27 12:03:00 -070065 // Build a dylib variant
66 BuildDylib bool `blueprint:"mutated"`
67 // Build an rlib variant
68 BuildRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070069 // Build a shared library variant
70 BuildShared bool `blueprint:"mutated"`
71 // Build a static library variant
72 BuildStatic bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070073
74 // This variant is a dylib
75 VariantIsDylib bool `blueprint:"mutated"`
76 // This variant is an rlib
77 VariantIsRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070078 // This variant is a shared library
79 VariantIsShared bool `blueprint:"mutated"`
80 // This variant is a static library
81 VariantIsStatic bool `blueprint:"mutated"`
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +020082 // This variant is a source provider
83 VariantIsSource bool `blueprint:"mutated"`
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040084
85 // This variant is disabled and should not be compiled
86 // (used for SourceProvider variants that produce only source)
87 VariantIsDisabled bool `blueprint:"mutated"`
Ivan Lozano2b081132020-09-08 12:46:52 -040088
89 // Whether this library variant should be link libstd via rlibs
90 VariantIsStaticStd bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070091}
92
93type libraryDecorator struct {
94 *baseCompiler
Matthew Maurerbb3add12020-06-25 09:34:12 -070095 *flagExporter
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +020096 stripper Stripper
Ivan Lozanoffee3342019-08-27 12:03:00 -070097
Ivan Lozano8a23fa42020-06-16 10:26:57 -040098 Properties LibraryCompilerProperties
99 MutatedProperties LibraryMutatedProperties
100 includeDirs android.Paths
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400101 sourceProvider SourceProvider
Ivan Lozano1921e802021-05-20 13:39:16 -0400102
103 collectedSnapshotHeaders android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700104}
105
106type libraryInterface interface {
107 rlib() bool
108 dylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700109 static() bool
110 shared() bool
Ivan Lozano2b081132020-09-08 12:46:52 -0400111 sysroot() bool
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200112 source() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700113
114 // Returns true if the build options for the module have selected a particular build type
115 buildRlib() bool
116 buildDylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700117 buildShared() bool
118 buildStatic() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700119
120 // Sets a particular variant type
121 setRlib()
122 setDylib()
Ivan Lozano52767be2019-10-18 14:49:46 -0700123 setShared()
124 setStatic()
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200125 setSource()
Ivan Lozano52767be2019-10-18 14:49:46 -0700126
Ivan Lozano2b081132020-09-08 12:46:52 -0400127 // Set libstd linkage
128 setRlibStd()
129 setDylibStd()
130
Ivan Lozano52767be2019-10-18 14:49:46 -0700131 // Build a specific library variant
Matthew Maurer2ae05132020-06-23 14:28:53 -0700132 BuildOnlyFFI()
133 BuildOnlyRust()
Ivan Lozano52767be2019-10-18 14:49:46 -0700134 BuildOnlyRlib()
135 BuildOnlyDylib()
136 BuildOnlyStatic()
137 BuildOnlyShared()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700138}
139
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400140func (library *libraryDecorator) nativeCoverage() bool {
141 return true
142}
143
Ivan Lozanoffee3342019-08-27 12:03:00 -0700144func (library *libraryDecorator) rlib() bool {
145 return library.MutatedProperties.VariantIsRlib
146}
147
Ivan Lozano2b081132020-09-08 12:46:52 -0400148func (library *libraryDecorator) sysroot() bool {
149 return Bool(library.Properties.Sysroot)
150}
151
Ivan Lozanoffee3342019-08-27 12:03:00 -0700152func (library *libraryDecorator) dylib() bool {
153 return library.MutatedProperties.VariantIsDylib
154}
155
Ivan Lozano52767be2019-10-18 14:49:46 -0700156func (library *libraryDecorator) shared() bool {
157 return library.MutatedProperties.VariantIsShared
158}
159
160func (library *libraryDecorator) static() bool {
161 return library.MutatedProperties.VariantIsStatic
162}
163
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200164func (library *libraryDecorator) source() bool {
165 return library.MutatedProperties.VariantIsSource
166}
167
Ivan Lozanoffee3342019-08-27 12:03:00 -0700168func (library *libraryDecorator) buildRlib() bool {
169 return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true)
170}
171
172func (library *libraryDecorator) buildDylib() bool {
173 return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true)
174}
175
Ivan Lozano52767be2019-10-18 14:49:46 -0700176func (library *libraryDecorator) buildShared() bool {
177 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
178}
179
180func (library *libraryDecorator) buildStatic() bool {
181 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
182}
183
Ivan Lozanoffee3342019-08-27 12:03:00 -0700184func (library *libraryDecorator) setRlib() {
185 library.MutatedProperties.VariantIsRlib = true
186 library.MutatedProperties.VariantIsDylib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700187 library.MutatedProperties.VariantIsStatic = false
188 library.MutatedProperties.VariantIsShared = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700189}
190
191func (library *libraryDecorator) setDylib() {
192 library.MutatedProperties.VariantIsRlib = false
193 library.MutatedProperties.VariantIsDylib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700194 library.MutatedProperties.VariantIsStatic = false
195 library.MutatedProperties.VariantIsShared = false
196}
197
Ivan Lozano2b081132020-09-08 12:46:52 -0400198func (library *libraryDecorator) setRlibStd() {
199 library.MutatedProperties.VariantIsStaticStd = true
200}
201
202func (library *libraryDecorator) setDylibStd() {
203 library.MutatedProperties.VariantIsStaticStd = false
204}
205
Ivan Lozano52767be2019-10-18 14:49:46 -0700206func (library *libraryDecorator) setShared() {
207 library.MutatedProperties.VariantIsStatic = false
208 library.MutatedProperties.VariantIsShared = true
209 library.MutatedProperties.VariantIsRlib = false
210 library.MutatedProperties.VariantIsDylib = false
211}
212
213func (library *libraryDecorator) setStatic() {
214 library.MutatedProperties.VariantIsStatic = true
215 library.MutatedProperties.VariantIsShared = false
216 library.MutatedProperties.VariantIsRlib = false
217 library.MutatedProperties.VariantIsDylib = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700218}
219
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200220func (library *libraryDecorator) setSource() {
221 library.MutatedProperties.VariantIsSource = true
222}
223
Liz Kammer356f7d42021-01-26 09:18:53 -0500224func (library *libraryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Ivan Lozano1921e802021-05-20 13:39:16 -0400225 if ctx.Module().(*Module).InVendor() {
226 // Vendor modules should statically link libstd.
227 return rlibAutoDep
228 } else if library.preferRlib() {
Ivan Lozanoea086132020-12-08 14:43:00 -0500229 return rlibAutoDep
230 } else if library.rlib() || library.static() {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700231 return rlibAutoDep
232 } else if library.dylib() || library.shared() {
233 return dylibAutoDep
Liz Kammer356f7d42021-01-26 09:18:53 -0500234 } else if ctx.BazelConversionMode() {
235 // In Bazel conversion mode, we are currently ignoring the deptag, so we just need to supply a
236 // compatible tag in order to add the dependency.
237 return rlibAutoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700238 } else {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200239 panic(fmt.Errorf("autoDep called on library %q that has no enabled variants.", ctx.ModuleName()))
Matthew Maurer0f003b12020-06-29 14:34:06 -0700240 }
241}
242
Ivan Lozanoea086132020-12-08 14:43:00 -0500243func (library *libraryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozano1921e802021-05-20 13:39:16 -0400244 if ctx.RustModule().InVendor() {
245 // Vendor modules should statically link libstd.
246 return RlibLinkage
247 } else if library.static() || library.MutatedProperties.VariantIsStaticStd {
Ivan Lozanoea086132020-12-08 14:43:00 -0500248 return RlibLinkage
249 } else if library.baseCompiler.preferRlib() {
250 return RlibLinkage
251 }
252 return DefaultLinkage
253}
254
Ivan Lozanoffee3342019-08-27 12:03:00 -0700255var _ compiler = (*libraryDecorator)(nil)
Ivan Lozano52767be2019-10-18 14:49:46 -0700256var _ libraryInterface = (*libraryDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700257var _ exportedFlagsProducer = (*libraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700258
Matthew Maurer2ae05132020-06-23 14:28:53 -0700259// rust_library produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700260func RustLibraryFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700261 module, library := NewRustLibrary(android.HostAndDeviceSupported)
262 library.BuildOnlyRust()
263 return module.Init()
264}
265
266// rust_ffi produces all ffi variants.
267func RustFFIFactory() android.Module {
268 module, library := NewRustLibrary(android.HostAndDeviceSupported)
269 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700270 return module.Init()
271}
272
273// rust_library_dylib produces a dylib.
274func RustLibraryDylibFactory() android.Module {
275 module, library := NewRustLibrary(android.HostAndDeviceSupported)
276 library.BuildOnlyDylib()
277 return module.Init()
278}
279
280// rust_library_rlib produces an rlib.
281func RustLibraryRlibFactory() android.Module {
282 module, library := NewRustLibrary(android.HostAndDeviceSupported)
283 library.BuildOnlyRlib()
284 return module.Init()
285}
286
Matthew Maurer2ae05132020-06-23 14:28:53 -0700287// rust_ffi_shared produces a shared library.
288func RustFFISharedFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700289 module, library := NewRustLibrary(android.HostAndDeviceSupported)
290 library.BuildOnlyShared()
291 return module.Init()
292}
293
Matthew Maurer2ae05132020-06-23 14:28:53 -0700294// rust_ffi_static produces a static library.
295func RustFFIStaticFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700296 module, library := NewRustLibrary(android.HostAndDeviceSupported)
297 library.BuildOnlyStatic()
298 return module.Init()
299}
300
Matthew Maurer2ae05132020-06-23 14:28:53 -0700301// rust_library_host produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700302func RustLibraryHostFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700303 module, library := NewRustLibrary(android.HostSupported)
304 library.BuildOnlyRust()
305 return module.Init()
306}
307
308// rust_ffi_host produces all FFI variants.
309func RustFFIHostFactory() android.Module {
310 module, library := NewRustLibrary(android.HostSupported)
311 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700312 return module.Init()
313}
314
315// rust_library_dylib_host produces a dylib.
316func RustLibraryDylibHostFactory() android.Module {
317 module, library := NewRustLibrary(android.HostSupported)
318 library.BuildOnlyDylib()
319 return module.Init()
320}
321
322// rust_library_rlib_host produces an rlib.
323func RustLibraryRlibHostFactory() android.Module {
324 module, library := NewRustLibrary(android.HostSupported)
325 library.BuildOnlyRlib()
326 return module.Init()
327}
328
Matthew Maurer2ae05132020-06-23 14:28:53 -0700329// rust_ffi_static_host produces a static library.
330func RustFFIStaticHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700331 module, library := NewRustLibrary(android.HostSupported)
332 library.BuildOnlyStatic()
333 return module.Init()
334}
335
Matthew Maurer2ae05132020-06-23 14:28:53 -0700336// rust_ffi_shared_host produces an shared library.
337func RustFFISharedHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700338 module, library := NewRustLibrary(android.HostSupported)
339 library.BuildOnlyShared()
340 return module.Init()
341}
342
Matthew Maurer2ae05132020-06-23 14:28:53 -0700343func (library *libraryDecorator) BuildOnlyFFI() {
344 library.MutatedProperties.BuildDylib = false
345 library.MutatedProperties.BuildRlib = false
346 library.MutatedProperties.BuildShared = true
347 library.MutatedProperties.BuildStatic = true
348}
349
350func (library *libraryDecorator) BuildOnlyRust() {
351 library.MutatedProperties.BuildDylib = true
352 library.MutatedProperties.BuildRlib = true
353 library.MutatedProperties.BuildShared = false
354 library.MutatedProperties.BuildStatic = false
355}
356
Ivan Lozanoffee3342019-08-27 12:03:00 -0700357func (library *libraryDecorator) BuildOnlyDylib() {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700358 library.MutatedProperties.BuildDylib = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700359 library.MutatedProperties.BuildRlib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700360 library.MutatedProperties.BuildShared = false
361 library.MutatedProperties.BuildStatic = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700362}
363
364func (library *libraryDecorator) BuildOnlyRlib() {
365 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700366 library.MutatedProperties.BuildRlib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700367 library.MutatedProperties.BuildShared = false
368 library.MutatedProperties.BuildStatic = false
369}
370
371func (library *libraryDecorator) BuildOnlyStatic() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700372 library.MutatedProperties.BuildRlib = false
373 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700374 library.MutatedProperties.BuildShared = false
375 library.MutatedProperties.BuildStatic = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700376}
377
378func (library *libraryDecorator) BuildOnlyShared() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700379 library.MutatedProperties.BuildRlib = false
380 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700381 library.MutatedProperties.BuildStatic = false
382 library.MutatedProperties.BuildShared = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700383}
384
385func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400386 module := newModule(hod, android.MultilibBoth)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700387
388 library := &libraryDecorator{
389 MutatedProperties: LibraryMutatedProperties{
Matthew Maurer2ae05132020-06-23 14:28:53 -0700390 BuildDylib: false,
391 BuildRlib: false,
392 BuildShared: false,
393 BuildStatic: false,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700394 },
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800395 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Matthew Maurerbb3add12020-06-25 09:34:12 -0700396 flagExporter: NewFlagExporter(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700397 }
398
399 module.compiler = library
400
401 return module, library
402}
403
404func (library *libraryDecorator) compilerProps() []interface{} {
405 return append(library.baseCompiler.compilerProps(),
406 &library.Properties,
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200407 &library.MutatedProperties,
408 &library.stripper.StripProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700409}
410
Ivan Lozanof1c84332019-09-20 11:00:37 -0700411func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
412 deps = library.baseCompiler.compilerDeps(ctx, deps)
413
Ivan Lozano52767be2019-10-18 14:49:46 -0700414 if ctx.toolchain().Bionic() && (library.dylib() || library.shared()) {
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +0100415 deps = bionicDeps(ctx, deps, false)
Ivan Lozano12ee9ca2020-04-07 13:19:44 -0400416 deps.CrtBegin = "crtbegin_so"
417 deps.CrtEnd = "crtend_so"
Ivan Lozanof1c84332019-09-20 11:00:37 -0700418 }
419
420 return deps
421}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400422
423func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string {
424 return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
425}
426
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800427func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200428 flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800429 flags = library.baseCompiler.compilerFlags(ctx, flags)
430 if library.shared() || library.static() {
431 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
432 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400433 if library.shared() {
434 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
435 }
436
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800437 return flags
438}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700439
Ivan Lozanoffee3342019-08-27 12:03:00 -0700440func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200441 var outputFile android.ModuleOutPath
442 var fileName string
Dan Albert06feee92021-03-19 15:06:02 -0700443 srcPath := library.srcPath(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700444
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400445 if library.sourceProvider != nil {
Ivan Lozano9d74a522020-12-01 09:25:22 -0500446 deps.srcProviderFiles = append(deps.srcProviderFiles, library.sourceProvider.Srcs()...)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400447 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700448
449 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500450 flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...)
Ivan Lozano2093af22020-08-25 12:48:19 -0400451 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700452
Matthew Maurer46c46cc2020-01-13 16:34:34 -0800453 if library.dylib() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700454 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
455 // https://github.com/rust-lang/rust/issues/19680
456 // https://github.com/rust-lang/rust/issues/34909
457 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
458 }
459
Ivan Lozanoffee3342019-08-27 12:03:00 -0700460 if library.rlib() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200461 fileName = library.getStem(ctx) + ctx.toolchain().RlibSuffix()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700462 outputFile = android.PathForModuleOut(ctx, fileName)
463
Dan Albert06feee92021-03-19 15:06:02 -0700464 TransformSrctoRlib(ctx, srcPath, deps, flags, outputFile)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700465 } else if library.dylib() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200466 fileName = library.getStem(ctx) + ctx.toolchain().DylibSuffix()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700467 outputFile = android.PathForModuleOut(ctx, fileName)
468
Dan Albert06feee92021-03-19 15:06:02 -0700469 TransformSrctoDylib(ctx, srcPath, deps, flags, outputFile)
Ivan Lozano52767be2019-10-18 14:49:46 -0700470 } else if library.static() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200471 fileName = library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
Ivan Lozano52767be2019-10-18 14:49:46 -0700472 outputFile = android.PathForModuleOut(ctx, fileName)
473
Dan Albert06feee92021-03-19 15:06:02 -0700474 TransformSrctoStatic(ctx, srcPath, deps, flags, outputFile)
Ivan Lozano52767be2019-10-18 14:49:46 -0700475 } else if library.shared() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200476 fileName = library.sharedLibFilename(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700477 outputFile = android.PathForModuleOut(ctx, fileName)
478
Dan Albert06feee92021-03-19 15:06:02 -0700479 TransformSrctoShared(ctx, srcPath, deps, flags, outputFile)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700480 }
481
Thiébaud Weksteen4fab05a2021-04-14 19:15:48 +0200482 if !library.rlib() && !library.static() && library.stripper.NeedsStrip(ctx) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200483 strippedOutputFile := android.PathForModuleOut(ctx, "stripped", fileName)
484 library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
485 library.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
486 }
487
Ivan Lozano52767be2019-10-18 14:49:46 -0700488 if library.rlib() || library.dylib() {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700489 library.flagExporter.exportLinkDirs(deps.linkDirs...)
Colin Cross0de8a1e2020-09-18 14:15:30 -0700490 library.flagExporter.exportLinkObjects(deps.linkObjects...)
Ivan Lozano52767be2019-10-18 14:49:46 -0700491 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700492
Colin Cross0de8a1e2020-09-18 14:15:30 -0700493 if library.static() || library.shared() {
494 ctx.SetProvider(cc.FlagExporterInfoProvider, cc.FlagExporterInfo{
495 IncludeDirs: library.includeDirs,
496 })
497 }
498
499 if library.shared() {
500 ctx.SetProvider(cc.SharedLibraryInfoProvider, cc.SharedLibraryInfo{
501 SharedLibrary: outputFile,
502 UnstrippedSharedLibrary: outputFile,
Colin Cross2df81772021-01-26 11:00:18 -0800503 Target: ctx.Target(),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700504 })
505 }
506
507 if library.static() {
508 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(outputFile).Build()
509 ctx.SetProvider(cc.StaticLibraryInfoProvider, cc.StaticLibraryInfo{
510 StaticLibrary: outputFile,
511
512 TransitiveStaticLibrariesForOrdering: depSet,
513 })
514 }
515
516 library.flagExporter.setProvider(ctx)
517
Ivan Lozanoffee3342019-08-27 12:03:00 -0700518 return outputFile
519}
520
Dan Albert06feee92021-03-19 15:06:02 -0700521func (library *libraryDecorator) srcPath(ctx ModuleContext, deps PathDeps) android.Path {
522 if library.sourceProvider != nil {
523 // Assume the first source from the source provider is the library entry point.
524 return library.sourceProvider.Srcs()[0]
525 } else {
526 path, _ := srcPathFromModuleSrcs(ctx, library.baseCompiler.Properties.Srcs)
527 return path
528 }
529}
530
531func (library *libraryDecorator) rustdoc(ctx ModuleContext, flags Flags,
532 deps PathDeps) android.OptionalPath {
533 // rustdoc has builtin support for documenting config specific information
534 // regardless of the actual config it was given
535 // (https://doc.rust-lang.org/rustdoc/advanced-features.html#cfgdoc-documenting-platform-specific-or-feature-specific-information),
536 // so we generate the rustdoc for only the primary module so that we have a
537 // single set of docs to refer to.
538 if ctx.Module() != ctx.PrimaryModule() {
539 return android.OptionalPath{}
540 }
541
542 return android.OptionalPathForPath(Rustdoc(ctx, library.srcPath(ctx, deps),
543 deps, flags))
544}
545
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700546func (library *libraryDecorator) getStem(ctx ModuleContext) string {
547 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
548 validateLibraryStem(ctx, stem, library.crateName())
549
550 return stem + String(library.baseCompiler.Properties.Suffix)
551}
552
Ivan Lozano2b081132020-09-08 12:46:52 -0400553func (library *libraryDecorator) install(ctx ModuleContext) {
554 // Only shared and dylib variants make sense to install.
555 if library.shared() || library.dylib() {
556 library.baseCompiler.install(ctx)
557 }
558}
559
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400560func (library *libraryDecorator) Disabled() bool {
561 return library.MutatedProperties.VariantIsDisabled
562}
563
564func (library *libraryDecorator) SetDisabled() {
565 library.MutatedProperties.VariantIsDisabled = true
566}
567
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700568var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
569
570func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
571 if crate_name == "" {
572 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
573 }
574
575 // crate_names are used for the library output file, and rustc expects these
576 // to be alphanumeric with underscores allowed.
577 if validCrateName.MatchString(crate_name) {
578 ctx.PropertyErrorf("crate_name",
579 "library crate_names must be alphanumeric with underscores allowed")
580 }
581
582 // Libraries are expected to begin with "lib" followed by the crate_name
583 if !strings.HasPrefix(filename, "lib"+crate_name) {
584 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
585 }
586}
587
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200588// LibraryMutator mutates the libraries into variants according to the
589// build{Rlib,Dylib} attributes.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700590func LibraryMutator(mctx android.BottomUpMutatorContext) {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200591 // Only mutate on Rust libraries.
592 m, ok := mctx.Module().(*Module)
593 if !ok || m.compiler == nil {
594 return
595 }
596 library, ok := m.compiler.(libraryInterface)
597 if !ok {
598 return
599 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400600
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200601 var variants []string
602 // The source variant is used for SourceProvider modules. The other variants (i.e. rlib and dylib)
603 // depend on this variant. It must be the first variant to be declared.
604 sourceVariant := false
605 if m.sourceProvider != nil {
606 variants = append(variants, "source")
607 sourceVariant = true
608 }
609 if library.buildRlib() {
610 variants = append(variants, rlibVariation)
611 }
612 if library.buildDylib() {
613 variants = append(variants, dylibVariation)
614 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400615
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200616 if len(variants) == 0 {
617 return
618 }
619 modules := mctx.CreateLocalVariations(variants...)
620
621 // The order of the variations (modules) matches the variant names provided. Iterate
622 // through the new variation modules and set their mutated properties.
623 for i, v := range modules {
624 switch variants[i] {
625 case rlibVariation:
626 v.(*Module).compiler.(libraryInterface).setRlib()
627 case dylibVariation:
628 v.(*Module).compiler.(libraryInterface).setDylib()
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400629 if v.(*Module).ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
Ivan Lozano6a884432020-12-02 09:15:16 -0500630 // TODO(b/165791368)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400631 // Disable dylib Vendor Ramdisk variations until we support these.
Ivan Lozano6a884432020-12-02 09:15:16 -0500632 v.(*Module).Disable()
633 }
Ivan Lozano1921e802021-05-20 13:39:16 -0400634
635 variation := v.(*Module).ModuleBase.ImageVariation().Variation
636 if strings.HasPrefix(variation, cc.VendorVariationPrefix) &&
637 m.HasVendorVariant() &&
638 !cc.IsVendorProprietaryModule(mctx) &&
639 strings.TrimPrefix(variation, cc.VendorVariationPrefix) == mctx.DeviceConfig().VndkVersion() {
640
641 // cc.MutateImage runs before LibraryMutator, so vendor variations which are meant for rlibs only are
642 // produced for Dylibs; however, dylibs should not be enabled for boardVndkVersion for
643 // non-vendor proprietary modules.
644 v.(*Module).Disable()
645 }
646
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200647 case "source":
648 v.(*Module).compiler.(libraryInterface).setSource()
649 // The source variant does not produce any library.
650 // Disable the compilation steps.
651 v.(*Module).compiler.SetDisabled()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700652 }
653 }
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200654
655 // If a source variant is created, add an inter-variant dependency
656 // between the other variants and the source variant.
657 if sourceVariant {
658 sv := modules[0]
659 for _, v := range modules[1:] {
660 if !v.Enabled() {
661 continue
662 }
663 mctx.AddInterVariantDependency(sourceDepTag, v, sv)
664 }
665 // Alias the source variation so it can be named directly in "srcs" properties.
666 mctx.AliasVariation("source")
667 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700668}
Ivan Lozano2b081132020-09-08 12:46:52 -0400669
670func LibstdMutator(mctx android.BottomUpMutatorContext) {
671 if m, ok := mctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() {
672 switch library := m.compiler.(type) {
673 case libraryInterface:
674 // Only create a variant if a library is actually being built.
675 if library.rlib() && !library.sysroot() {
676 variants := []string{"rlib-std", "dylib-std"}
677 modules := mctx.CreateLocalVariations(variants...)
678
679 rlib := modules[0].(*Module)
680 dylib := modules[1].(*Module)
681 rlib.compiler.(libraryInterface).setRlibStd()
682 dylib.compiler.(libraryInterface).setDylibStd()
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400683 if dylib.ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
Ivan Lozano6a884432020-12-02 09:15:16 -0500684 // TODO(b/165791368)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400685 // Disable rlibs that link against dylib-std on vendor ramdisk variations until those dylib
Ivan Lozano6a884432020-12-02 09:15:16 -0500686 // variants are properly supported.
687 dylib.Disable()
688 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400689 rlib.Properties.RustSubName += RlibStdlibSuffix
690 dylib.Properties.RustSubName += DylibStdlibSuffix
Ivan Lozano2b081132020-09-08 12:46:52 -0400691 }
692 }
693 }
694}
Ivan Lozano1921e802021-05-20 13:39:16 -0400695
696func (l *libraryDecorator) snapshotHeaders() android.Paths {
697 if l.collectedSnapshotHeaders == nil {
698 panic("snapshotHeaders() must be called after collectHeadersForSnapshot()")
699 }
700 return l.collectedSnapshotHeaders
701}
702
703// collectHeadersForSnapshot collects all exported headers from library.
704// It globs header files in the source tree for exported include directories,
705// and tracks generated header files separately.
706//
707// This is to be called from GenerateAndroidBuildActions, and then collected
708// header files can be retrieved by snapshotHeaders().
709func (l *libraryDecorator) collectHeadersForSnapshot(ctx android.ModuleContext, deps PathDeps) {
710 ret := android.Paths{}
711
712 // Glob together the headers from the modules include_dirs property
713 for _, path := range android.CopyOfPaths(l.includeDirs) {
714 dir := path.String()
715 glob, err := ctx.GlobWithDeps(dir+"/**/*", nil)
716 if err != nil {
717 ctx.ModuleErrorf("glob failed: %#v", err)
718 return
719 }
720
721 for _, header := range glob {
722 // Filter out only the files with extensions that are headers.
723 found := false
724 for _, ext := range cc.HeaderExts {
725 if strings.HasSuffix(header, ext) {
726 found = true
727 break
728 }
729 }
730 if !found {
731 continue
732 }
733 ret = append(ret, android.PathForSource(ctx, header))
734 }
735 }
736
737 // Glob together the headers from C dependencies as well, starting with non-generated headers.
738 ret = append(ret, cc.GlobHeadersForSnapshot(ctx, append(android.CopyOfPaths(deps.depIncludePaths), deps.depSystemIncludePaths...))...)
739
740 // Collect generated headers from C dependencies.
741 ret = append(ret, cc.GlobGeneratedHeadersForSnapshot(ctx, deps.depGeneratedHeaders)...)
742
743 // TODO(185577950): If support for generated headers is added, they need to be collected here as well.
744 l.collectedSnapshotHeaders = ret
745}