blob: 5a36bd13f3fc34aaaaf44b59339f31e3ebac2ed8 [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 Lozano3149e6e2021-06-01 15:09:53 -0400127 // libstd linkage functions
128 rlibStd() bool
Ivan Lozano2b081132020-09-08 12:46:52 -0400129 setRlibStd()
130 setDylibStd()
131
Ivan Lozano52767be2019-10-18 14:49:46 -0700132 // Build a specific library variant
Matthew Maurer2ae05132020-06-23 14:28:53 -0700133 BuildOnlyFFI()
134 BuildOnlyRust()
Ivan Lozano52767be2019-10-18 14:49:46 -0700135 BuildOnlyRlib()
136 BuildOnlyDylib()
137 BuildOnlyStatic()
138 BuildOnlyShared()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700139}
140
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400141func (library *libraryDecorator) nativeCoverage() bool {
142 return true
143}
144
Ivan Lozanoffee3342019-08-27 12:03:00 -0700145func (library *libraryDecorator) rlib() bool {
146 return library.MutatedProperties.VariantIsRlib
147}
148
Ivan Lozano2b081132020-09-08 12:46:52 -0400149func (library *libraryDecorator) sysroot() bool {
150 return Bool(library.Properties.Sysroot)
151}
152
Ivan Lozanoffee3342019-08-27 12:03:00 -0700153func (library *libraryDecorator) dylib() bool {
154 return library.MutatedProperties.VariantIsDylib
155}
156
Ivan Lozano52767be2019-10-18 14:49:46 -0700157func (library *libraryDecorator) shared() bool {
158 return library.MutatedProperties.VariantIsShared
159}
160
161func (library *libraryDecorator) static() bool {
162 return library.MutatedProperties.VariantIsStatic
163}
164
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200165func (library *libraryDecorator) source() bool {
166 return library.MutatedProperties.VariantIsSource
167}
168
Ivan Lozanoffee3342019-08-27 12:03:00 -0700169func (library *libraryDecorator) buildRlib() bool {
170 return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true)
171}
172
173func (library *libraryDecorator) buildDylib() bool {
174 return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true)
175}
176
Ivan Lozano52767be2019-10-18 14:49:46 -0700177func (library *libraryDecorator) buildShared() bool {
178 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
179}
180
181func (library *libraryDecorator) buildStatic() bool {
182 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
183}
184
Ivan Lozanoffee3342019-08-27 12:03:00 -0700185func (library *libraryDecorator) setRlib() {
186 library.MutatedProperties.VariantIsRlib = true
187 library.MutatedProperties.VariantIsDylib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700188 library.MutatedProperties.VariantIsStatic = false
189 library.MutatedProperties.VariantIsShared = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700190}
191
192func (library *libraryDecorator) setDylib() {
193 library.MutatedProperties.VariantIsRlib = false
194 library.MutatedProperties.VariantIsDylib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700195 library.MutatedProperties.VariantIsStatic = false
196 library.MutatedProperties.VariantIsShared = false
197}
198
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400199func (library *libraryDecorator) rlibStd() bool {
200 return library.MutatedProperties.VariantIsStaticStd
201}
202
Ivan Lozano2b081132020-09-08 12:46:52 -0400203func (library *libraryDecorator) setRlibStd() {
204 library.MutatedProperties.VariantIsStaticStd = true
205}
206
207func (library *libraryDecorator) setDylibStd() {
208 library.MutatedProperties.VariantIsStaticStd = false
209}
210
Ivan Lozano52767be2019-10-18 14:49:46 -0700211func (library *libraryDecorator) setShared() {
212 library.MutatedProperties.VariantIsStatic = false
213 library.MutatedProperties.VariantIsShared = true
214 library.MutatedProperties.VariantIsRlib = false
215 library.MutatedProperties.VariantIsDylib = false
216}
217
218func (library *libraryDecorator) setStatic() {
219 library.MutatedProperties.VariantIsStatic = true
220 library.MutatedProperties.VariantIsShared = false
221 library.MutatedProperties.VariantIsRlib = false
222 library.MutatedProperties.VariantIsDylib = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700223}
224
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200225func (library *libraryDecorator) setSource() {
226 library.MutatedProperties.VariantIsSource = true
227}
228
Liz Kammer356f7d42021-01-26 09:18:53 -0500229func (library *libraryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Ivan Lozano1921e802021-05-20 13:39:16 -0400230 if ctx.Module().(*Module).InVendor() {
231 // Vendor modules should statically link libstd.
232 return rlibAutoDep
233 } else if library.preferRlib() {
Ivan Lozanoea086132020-12-08 14:43:00 -0500234 return rlibAutoDep
235 } else if library.rlib() || library.static() {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700236 return rlibAutoDep
237 } else if library.dylib() || library.shared() {
238 return dylibAutoDep
Liz Kammer356f7d42021-01-26 09:18:53 -0500239 } else if ctx.BazelConversionMode() {
240 // In Bazel conversion mode, we are currently ignoring the deptag, so we just need to supply a
241 // compatible tag in order to add the dependency.
242 return rlibAutoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700243 } else {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200244 panic(fmt.Errorf("autoDep called on library %q that has no enabled variants.", ctx.ModuleName()))
Matthew Maurer0f003b12020-06-29 14:34:06 -0700245 }
246}
247
Ivan Lozanoea086132020-12-08 14:43:00 -0500248func (library *libraryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozano1921e802021-05-20 13:39:16 -0400249 if ctx.RustModule().InVendor() {
250 // Vendor modules should statically link libstd.
251 return RlibLinkage
252 } else if library.static() || library.MutatedProperties.VariantIsStaticStd {
Ivan Lozanoea086132020-12-08 14:43:00 -0500253 return RlibLinkage
254 } else if library.baseCompiler.preferRlib() {
255 return RlibLinkage
256 }
257 return DefaultLinkage
258}
259
Ivan Lozanoffee3342019-08-27 12:03:00 -0700260var _ compiler = (*libraryDecorator)(nil)
Ivan Lozano52767be2019-10-18 14:49:46 -0700261var _ libraryInterface = (*libraryDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700262var _ exportedFlagsProducer = (*libraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700263
Matthew Maurer2ae05132020-06-23 14:28:53 -0700264// rust_library produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700265func RustLibraryFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700266 module, library := NewRustLibrary(android.HostAndDeviceSupported)
267 library.BuildOnlyRust()
268 return module.Init()
269}
270
271// rust_ffi produces all ffi variants.
272func RustFFIFactory() android.Module {
273 module, library := NewRustLibrary(android.HostAndDeviceSupported)
274 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700275 return module.Init()
276}
277
278// rust_library_dylib produces a dylib.
279func RustLibraryDylibFactory() android.Module {
280 module, library := NewRustLibrary(android.HostAndDeviceSupported)
281 library.BuildOnlyDylib()
282 return module.Init()
283}
284
285// rust_library_rlib produces an rlib.
286func RustLibraryRlibFactory() android.Module {
287 module, library := NewRustLibrary(android.HostAndDeviceSupported)
288 library.BuildOnlyRlib()
289 return module.Init()
290}
291
Matthew Maurer2ae05132020-06-23 14:28:53 -0700292// rust_ffi_shared produces a shared library.
293func RustFFISharedFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700294 module, library := NewRustLibrary(android.HostAndDeviceSupported)
295 library.BuildOnlyShared()
296 return module.Init()
297}
298
Matthew Maurer2ae05132020-06-23 14:28:53 -0700299// rust_ffi_static produces a static library.
300func RustFFIStaticFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700301 module, library := NewRustLibrary(android.HostAndDeviceSupported)
302 library.BuildOnlyStatic()
303 return module.Init()
304}
305
Matthew Maurer2ae05132020-06-23 14:28:53 -0700306// rust_library_host produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700307func RustLibraryHostFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700308 module, library := NewRustLibrary(android.HostSupported)
309 library.BuildOnlyRust()
310 return module.Init()
311}
312
313// rust_ffi_host produces all FFI variants.
314func RustFFIHostFactory() android.Module {
315 module, library := NewRustLibrary(android.HostSupported)
316 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700317 return module.Init()
318}
319
320// rust_library_dylib_host produces a dylib.
321func RustLibraryDylibHostFactory() android.Module {
322 module, library := NewRustLibrary(android.HostSupported)
323 library.BuildOnlyDylib()
324 return module.Init()
325}
326
327// rust_library_rlib_host produces an rlib.
328func RustLibraryRlibHostFactory() android.Module {
329 module, library := NewRustLibrary(android.HostSupported)
330 library.BuildOnlyRlib()
331 return module.Init()
332}
333
Matthew Maurer2ae05132020-06-23 14:28:53 -0700334// rust_ffi_static_host produces a static library.
335func RustFFIStaticHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700336 module, library := NewRustLibrary(android.HostSupported)
337 library.BuildOnlyStatic()
338 return module.Init()
339}
340
Matthew Maurer2ae05132020-06-23 14:28:53 -0700341// rust_ffi_shared_host produces an shared library.
342func RustFFISharedHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700343 module, library := NewRustLibrary(android.HostSupported)
344 library.BuildOnlyShared()
345 return module.Init()
346}
347
Matthew Maurer2ae05132020-06-23 14:28:53 -0700348func (library *libraryDecorator) BuildOnlyFFI() {
349 library.MutatedProperties.BuildDylib = false
350 library.MutatedProperties.BuildRlib = false
351 library.MutatedProperties.BuildShared = true
352 library.MutatedProperties.BuildStatic = true
353}
354
355func (library *libraryDecorator) BuildOnlyRust() {
356 library.MutatedProperties.BuildDylib = true
357 library.MutatedProperties.BuildRlib = true
358 library.MutatedProperties.BuildShared = false
359 library.MutatedProperties.BuildStatic = false
360}
361
Ivan Lozanoffee3342019-08-27 12:03:00 -0700362func (library *libraryDecorator) BuildOnlyDylib() {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700363 library.MutatedProperties.BuildDylib = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700364 library.MutatedProperties.BuildRlib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700365 library.MutatedProperties.BuildShared = false
366 library.MutatedProperties.BuildStatic = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700367}
368
369func (library *libraryDecorator) BuildOnlyRlib() {
370 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700371 library.MutatedProperties.BuildRlib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700372 library.MutatedProperties.BuildShared = false
373 library.MutatedProperties.BuildStatic = false
374}
375
376func (library *libraryDecorator) BuildOnlyStatic() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700377 library.MutatedProperties.BuildRlib = false
378 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700379 library.MutatedProperties.BuildShared = false
380 library.MutatedProperties.BuildStatic = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700381}
382
383func (library *libraryDecorator) BuildOnlyShared() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700384 library.MutatedProperties.BuildRlib = false
385 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700386 library.MutatedProperties.BuildStatic = false
387 library.MutatedProperties.BuildShared = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700388}
389
390func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400391 module := newModule(hod, android.MultilibBoth)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700392
393 library := &libraryDecorator{
394 MutatedProperties: LibraryMutatedProperties{
Matthew Maurer2ae05132020-06-23 14:28:53 -0700395 BuildDylib: false,
396 BuildRlib: false,
397 BuildShared: false,
398 BuildStatic: false,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700399 },
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800400 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Matthew Maurerbb3add12020-06-25 09:34:12 -0700401 flagExporter: NewFlagExporter(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700402 }
403
404 module.compiler = library
405
406 return module, library
407}
408
409func (library *libraryDecorator) compilerProps() []interface{} {
410 return append(library.baseCompiler.compilerProps(),
411 &library.Properties,
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200412 &library.MutatedProperties,
413 &library.stripper.StripProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700414}
415
Ivan Lozanof1c84332019-09-20 11:00:37 -0700416func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
417 deps = library.baseCompiler.compilerDeps(ctx, deps)
418
Ivan Lozano52767be2019-10-18 14:49:46 -0700419 if ctx.toolchain().Bionic() && (library.dylib() || library.shared()) {
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +0100420 deps = bionicDeps(ctx, deps, false)
Ivan Lozano12ee9ca2020-04-07 13:19:44 -0400421 deps.CrtBegin = "crtbegin_so"
422 deps.CrtEnd = "crtend_so"
Ivan Lozanof1c84332019-09-20 11:00:37 -0700423 }
424
425 return deps
426}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400427
428func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string {
429 return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
430}
431
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800432func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200433 flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
Stephen Crane0dbfc562021-07-07 19:05:02 -0700434 if library.dylib() {
435 // We need to add a dependency on std in order to link crates as dylibs.
436 // The hack to add this dependency is guarded by the following cfg so
437 // that we don't force a dependency when it isn't needed.
438 library.baseCompiler.Properties.Cfgs = append(library.baseCompiler.Properties.Cfgs, "android_dylib")
439 }
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800440 flags = library.baseCompiler.compilerFlags(ctx, flags)
441 if library.shared() || library.static() {
442 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
443 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400444 if library.shared() {
445 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
446 }
447
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800448 return flags
449}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700450
Ivan Lozanoffee3342019-08-27 12:03:00 -0700451func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200452 var outputFile android.ModuleOutPath
453 var fileName string
Dan Albert06feee92021-03-19 15:06:02 -0700454 srcPath := library.srcPath(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700455
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400456 if library.sourceProvider != nil {
Ivan Lozano9d74a522020-12-01 09:25:22 -0500457 deps.srcProviderFiles = append(deps.srcProviderFiles, library.sourceProvider.Srcs()...)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400458 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700459
460 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500461 flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...)
Ivan Lozano2093af22020-08-25 12:48:19 -0400462 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700463
Matthew Maurer46c46cc2020-01-13 16:34:34 -0800464 if library.dylib() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700465 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
466 // https://github.com/rust-lang/rust/issues/19680
467 // https://github.com/rust-lang/rust/issues/34909
468 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
469 }
470
Ivan Lozanoffee3342019-08-27 12:03:00 -0700471 if library.rlib() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200472 fileName = library.getStem(ctx) + ctx.toolchain().RlibSuffix()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700473 outputFile = android.PathForModuleOut(ctx, fileName)
474
Dan Albert06feee92021-03-19 15:06:02 -0700475 TransformSrctoRlib(ctx, srcPath, deps, flags, outputFile)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700476 } else if library.dylib() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200477 fileName = library.getStem(ctx) + ctx.toolchain().DylibSuffix()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700478 outputFile = android.PathForModuleOut(ctx, fileName)
479
Dan Albert06feee92021-03-19 15:06:02 -0700480 TransformSrctoDylib(ctx, srcPath, deps, flags, outputFile)
Ivan Lozano52767be2019-10-18 14:49:46 -0700481 } else if library.static() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200482 fileName = library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
Ivan Lozano52767be2019-10-18 14:49:46 -0700483 outputFile = android.PathForModuleOut(ctx, fileName)
484
Dan Albert06feee92021-03-19 15:06:02 -0700485 TransformSrctoStatic(ctx, srcPath, deps, flags, outputFile)
Ivan Lozano52767be2019-10-18 14:49:46 -0700486 } else if library.shared() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200487 fileName = library.sharedLibFilename(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700488 outputFile = android.PathForModuleOut(ctx, fileName)
489
Dan Albert06feee92021-03-19 15:06:02 -0700490 TransformSrctoShared(ctx, srcPath, deps, flags, outputFile)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700491 }
492
Thiébaud Weksteen4fab05a2021-04-14 19:15:48 +0200493 if !library.rlib() && !library.static() && library.stripper.NeedsStrip(ctx) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200494 strippedOutputFile := android.PathForModuleOut(ctx, "stripped", fileName)
495 library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
496 library.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
497 }
498
Ivan Lozano52767be2019-10-18 14:49:46 -0700499 if library.rlib() || library.dylib() {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700500 library.flagExporter.exportLinkDirs(deps.linkDirs...)
Colin Cross0de8a1e2020-09-18 14:15:30 -0700501 library.flagExporter.exportLinkObjects(deps.linkObjects...)
Ivan Lozano52767be2019-10-18 14:49:46 -0700502 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700503
Colin Cross0de8a1e2020-09-18 14:15:30 -0700504 if library.static() || library.shared() {
505 ctx.SetProvider(cc.FlagExporterInfoProvider, cc.FlagExporterInfo{
506 IncludeDirs: library.includeDirs,
507 })
508 }
509
510 if library.shared() {
511 ctx.SetProvider(cc.SharedLibraryInfoProvider, cc.SharedLibraryInfo{
Liz Kammeref6dfea2021-06-08 15:37:09 -0400512 SharedLibrary: outputFile,
513 Target: ctx.Target(),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700514 })
515 }
516
517 if library.static() {
518 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(outputFile).Build()
519 ctx.SetProvider(cc.StaticLibraryInfoProvider, cc.StaticLibraryInfo{
520 StaticLibrary: outputFile,
521
522 TransitiveStaticLibrariesForOrdering: depSet,
523 })
524 }
525
526 library.flagExporter.setProvider(ctx)
527
Ivan Lozanoffee3342019-08-27 12:03:00 -0700528 return outputFile
529}
530
Dan Albert06feee92021-03-19 15:06:02 -0700531func (library *libraryDecorator) srcPath(ctx ModuleContext, deps PathDeps) android.Path {
532 if library.sourceProvider != nil {
533 // Assume the first source from the source provider is the library entry point.
534 return library.sourceProvider.Srcs()[0]
535 } else {
536 path, _ := srcPathFromModuleSrcs(ctx, library.baseCompiler.Properties.Srcs)
537 return path
538 }
539}
540
541func (library *libraryDecorator) rustdoc(ctx ModuleContext, flags Flags,
542 deps PathDeps) android.OptionalPath {
543 // rustdoc has builtin support for documenting config specific information
544 // regardless of the actual config it was given
545 // (https://doc.rust-lang.org/rustdoc/advanced-features.html#cfgdoc-documenting-platform-specific-or-feature-specific-information),
546 // so we generate the rustdoc for only the primary module so that we have a
547 // single set of docs to refer to.
548 if ctx.Module() != ctx.PrimaryModule() {
549 return android.OptionalPath{}
550 }
551
552 return android.OptionalPathForPath(Rustdoc(ctx, library.srcPath(ctx, deps),
553 deps, flags))
554}
555
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700556func (library *libraryDecorator) getStem(ctx ModuleContext) string {
557 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
558 validateLibraryStem(ctx, stem, library.crateName())
559
560 return stem + String(library.baseCompiler.Properties.Suffix)
561}
562
Ivan Lozano2b081132020-09-08 12:46:52 -0400563func (library *libraryDecorator) install(ctx ModuleContext) {
564 // Only shared and dylib variants make sense to install.
565 if library.shared() || library.dylib() {
566 library.baseCompiler.install(ctx)
567 }
568}
569
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400570func (library *libraryDecorator) Disabled() bool {
571 return library.MutatedProperties.VariantIsDisabled
572}
573
574func (library *libraryDecorator) SetDisabled() {
575 library.MutatedProperties.VariantIsDisabled = true
576}
577
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700578var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
579
580func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
581 if crate_name == "" {
582 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
583 }
584
585 // crate_names are used for the library output file, and rustc expects these
586 // to be alphanumeric with underscores allowed.
587 if validCrateName.MatchString(crate_name) {
588 ctx.PropertyErrorf("crate_name",
589 "library crate_names must be alphanumeric with underscores allowed")
590 }
591
592 // Libraries are expected to begin with "lib" followed by the crate_name
593 if !strings.HasPrefix(filename, "lib"+crate_name) {
594 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
595 }
596}
597
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200598// LibraryMutator mutates the libraries into variants according to the
599// build{Rlib,Dylib} attributes.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700600func LibraryMutator(mctx android.BottomUpMutatorContext) {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200601 // Only mutate on Rust libraries.
602 m, ok := mctx.Module().(*Module)
603 if !ok || m.compiler == nil {
604 return
605 }
606 library, ok := m.compiler.(libraryInterface)
607 if !ok {
608 return
609 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400610
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200611 var variants []string
612 // The source variant is used for SourceProvider modules. The other variants (i.e. rlib and dylib)
613 // depend on this variant. It must be the first variant to be declared.
614 sourceVariant := false
615 if m.sourceProvider != nil {
616 variants = append(variants, "source")
617 sourceVariant = true
618 }
619 if library.buildRlib() {
620 variants = append(variants, rlibVariation)
621 }
622 if library.buildDylib() {
623 variants = append(variants, dylibVariation)
624 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400625
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200626 if len(variants) == 0 {
627 return
628 }
629 modules := mctx.CreateLocalVariations(variants...)
630
631 // The order of the variations (modules) matches the variant names provided. Iterate
632 // through the new variation modules and set their mutated properties.
633 for i, v := range modules {
634 switch variants[i] {
635 case rlibVariation:
636 v.(*Module).compiler.(libraryInterface).setRlib()
637 case dylibVariation:
638 v.(*Module).compiler.(libraryInterface).setDylib()
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400639 if v.(*Module).ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
Ivan Lozano6a884432020-12-02 09:15:16 -0500640 // TODO(b/165791368)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400641 // Disable dylib Vendor Ramdisk variations until we support these.
Ivan Lozano6a884432020-12-02 09:15:16 -0500642 v.(*Module).Disable()
643 }
Ivan Lozano1921e802021-05-20 13:39:16 -0400644
645 variation := v.(*Module).ModuleBase.ImageVariation().Variation
646 if strings.HasPrefix(variation, cc.VendorVariationPrefix) &&
647 m.HasVendorVariant() &&
648 !cc.IsVendorProprietaryModule(mctx) &&
649 strings.TrimPrefix(variation, cc.VendorVariationPrefix) == mctx.DeviceConfig().VndkVersion() {
650
651 // cc.MutateImage runs before LibraryMutator, so vendor variations which are meant for rlibs only are
652 // produced for Dylibs; however, dylibs should not be enabled for boardVndkVersion for
653 // non-vendor proprietary modules.
654 v.(*Module).Disable()
655 }
656
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200657 case "source":
658 v.(*Module).compiler.(libraryInterface).setSource()
659 // The source variant does not produce any library.
660 // Disable the compilation steps.
661 v.(*Module).compiler.SetDisabled()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700662 }
663 }
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200664
665 // If a source variant is created, add an inter-variant dependency
666 // between the other variants and the source variant.
667 if sourceVariant {
668 sv := modules[0]
669 for _, v := range modules[1:] {
670 if !v.Enabled() {
671 continue
672 }
673 mctx.AddInterVariantDependency(sourceDepTag, v, sv)
674 }
675 // Alias the source variation so it can be named directly in "srcs" properties.
676 mctx.AliasVariation("source")
677 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700678}
Ivan Lozano2b081132020-09-08 12:46:52 -0400679
680func LibstdMutator(mctx android.BottomUpMutatorContext) {
681 if m, ok := mctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() {
682 switch library := m.compiler.(type) {
683 case libraryInterface:
684 // Only create a variant if a library is actually being built.
685 if library.rlib() && !library.sysroot() {
686 variants := []string{"rlib-std", "dylib-std"}
687 modules := mctx.CreateLocalVariations(variants...)
688
689 rlib := modules[0].(*Module)
690 dylib := modules[1].(*Module)
691 rlib.compiler.(libraryInterface).setRlibStd()
692 dylib.compiler.(libraryInterface).setDylibStd()
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400693 if dylib.ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation ||
694 strings.HasPrefix(dylib.ModuleBase.ImageVariation().Variation, cc.VendorVariationPrefix) {
Ivan Lozano6a884432020-12-02 09:15:16 -0500695 // TODO(b/165791368)
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400696 // Disable rlibs that link against dylib-std on vendor and vendor ramdisk variations until those dylib
Ivan Lozano6a884432020-12-02 09:15:16 -0500697 // variants are properly supported.
698 dylib.Disable()
699 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400700 rlib.Properties.RustSubName += RlibStdlibSuffix
701 dylib.Properties.RustSubName += DylibStdlibSuffix
Ivan Lozano2b081132020-09-08 12:46:52 -0400702 }
703 }
704 }
705}
Ivan Lozano1921e802021-05-20 13:39:16 -0400706
707func (l *libraryDecorator) snapshotHeaders() android.Paths {
708 if l.collectedSnapshotHeaders == nil {
709 panic("snapshotHeaders() must be called after collectHeadersForSnapshot()")
710 }
711 return l.collectedSnapshotHeaders
712}
713
714// collectHeadersForSnapshot collects all exported headers from library.
715// It globs header files in the source tree for exported include directories,
716// and tracks generated header files separately.
717//
718// This is to be called from GenerateAndroidBuildActions, and then collected
719// header files can be retrieved by snapshotHeaders().
720func (l *libraryDecorator) collectHeadersForSnapshot(ctx android.ModuleContext, deps PathDeps) {
721 ret := android.Paths{}
722
723 // Glob together the headers from the modules include_dirs property
724 for _, path := range android.CopyOfPaths(l.includeDirs) {
725 dir := path.String()
726 glob, err := ctx.GlobWithDeps(dir+"/**/*", nil)
727 if err != nil {
728 ctx.ModuleErrorf("glob failed: %#v", err)
729 return
730 }
731
732 for _, header := range glob {
733 // Filter out only the files with extensions that are headers.
734 found := false
735 for _, ext := range cc.HeaderExts {
736 if strings.HasSuffix(header, ext) {
737 found = true
738 break
739 }
740 }
741 if !found {
742 continue
743 }
744 ret = append(ret, android.PathForSource(ctx, header))
745 }
746 }
747
748 // Glob together the headers from C dependencies as well, starting with non-generated headers.
749 ret = append(ret, cc.GlobHeadersForSnapshot(ctx, append(android.CopyOfPaths(deps.depIncludePaths), deps.depSystemIncludePaths...))...)
750
751 // Collect generated headers from C dependencies.
752 ret = append(ret, cc.GlobGeneratedHeadersForSnapshot(ctx, deps.depGeneratedHeaders)...)
753
754 // TODO(185577950): If support for generated headers is added, they need to be collected here as well.
755 l.collectedSnapshotHeaders = ret
756}