blob: 747a29d72637f83a4658ea86e544f2ace3bd98ff [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())
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800434 flags = library.baseCompiler.compilerFlags(ctx, flags)
435 if library.shared() || library.static() {
436 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
437 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400438 if library.shared() {
439 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
440 }
441
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800442 return flags
443}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700444
Ivan Lozanoffee3342019-08-27 12:03:00 -0700445func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200446 var outputFile android.ModuleOutPath
447 var fileName string
Dan Albert06feee92021-03-19 15:06:02 -0700448 srcPath := library.srcPath(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700449
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400450 if library.sourceProvider != nil {
Ivan Lozano9d74a522020-12-01 09:25:22 -0500451 deps.srcProviderFiles = append(deps.srcProviderFiles, library.sourceProvider.Srcs()...)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400452 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700453
454 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500455 flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...)
Ivan Lozano2093af22020-08-25 12:48:19 -0400456 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700457
Matthew Maurer46c46cc2020-01-13 16:34:34 -0800458 if library.dylib() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700459 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
460 // https://github.com/rust-lang/rust/issues/19680
461 // https://github.com/rust-lang/rust/issues/34909
462 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
463 }
464
Ivan Lozanoffee3342019-08-27 12:03:00 -0700465 if library.rlib() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200466 fileName = library.getStem(ctx) + ctx.toolchain().RlibSuffix()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700467 outputFile = android.PathForModuleOut(ctx, fileName)
468
Dan Albert06feee92021-03-19 15:06:02 -0700469 TransformSrctoRlib(ctx, srcPath, deps, flags, outputFile)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700470 } else if library.dylib() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200471 fileName = library.getStem(ctx) + ctx.toolchain().DylibSuffix()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700472 outputFile = android.PathForModuleOut(ctx, fileName)
473
Dan Albert06feee92021-03-19 15:06:02 -0700474 TransformSrctoDylib(ctx, srcPath, deps, flags, outputFile)
Ivan Lozano52767be2019-10-18 14:49:46 -0700475 } else if library.static() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200476 fileName = library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
Ivan Lozano52767be2019-10-18 14:49:46 -0700477 outputFile = android.PathForModuleOut(ctx, fileName)
478
Dan Albert06feee92021-03-19 15:06:02 -0700479 TransformSrctoStatic(ctx, srcPath, deps, flags, outputFile)
Ivan Lozano52767be2019-10-18 14:49:46 -0700480 } else if library.shared() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200481 fileName = library.sharedLibFilename(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700482 outputFile = android.PathForModuleOut(ctx, fileName)
483
Dan Albert06feee92021-03-19 15:06:02 -0700484 TransformSrctoShared(ctx, srcPath, deps, flags, outputFile)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700485 }
486
Thiébaud Weksteen4fab05a2021-04-14 19:15:48 +0200487 if !library.rlib() && !library.static() && library.stripper.NeedsStrip(ctx) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200488 strippedOutputFile := android.PathForModuleOut(ctx, "stripped", fileName)
489 library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
490 library.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
491 }
492
Ivan Lozano52767be2019-10-18 14:49:46 -0700493 if library.rlib() || library.dylib() {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700494 library.flagExporter.exportLinkDirs(deps.linkDirs...)
Colin Cross0de8a1e2020-09-18 14:15:30 -0700495 library.flagExporter.exportLinkObjects(deps.linkObjects...)
Ivan Lozano52767be2019-10-18 14:49:46 -0700496 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700497
Colin Cross0de8a1e2020-09-18 14:15:30 -0700498 if library.static() || library.shared() {
499 ctx.SetProvider(cc.FlagExporterInfoProvider, cc.FlagExporterInfo{
500 IncludeDirs: library.includeDirs,
501 })
502 }
503
504 if library.shared() {
505 ctx.SetProvider(cc.SharedLibraryInfoProvider, cc.SharedLibraryInfo{
Liz Kammeref6dfea2021-06-08 15:37:09 -0400506 SharedLibrary: outputFile,
507 Target: ctx.Target(),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700508 })
509 }
510
511 if library.static() {
512 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(outputFile).Build()
513 ctx.SetProvider(cc.StaticLibraryInfoProvider, cc.StaticLibraryInfo{
514 StaticLibrary: outputFile,
515
516 TransitiveStaticLibrariesForOrdering: depSet,
517 })
518 }
519
520 library.flagExporter.setProvider(ctx)
521
Ivan Lozanoffee3342019-08-27 12:03:00 -0700522 return outputFile
523}
524
Dan Albert06feee92021-03-19 15:06:02 -0700525func (library *libraryDecorator) srcPath(ctx ModuleContext, deps PathDeps) android.Path {
526 if library.sourceProvider != nil {
527 // Assume the first source from the source provider is the library entry point.
528 return library.sourceProvider.Srcs()[0]
529 } else {
530 path, _ := srcPathFromModuleSrcs(ctx, library.baseCompiler.Properties.Srcs)
531 return path
532 }
533}
534
535func (library *libraryDecorator) rustdoc(ctx ModuleContext, flags Flags,
536 deps PathDeps) android.OptionalPath {
537 // rustdoc has builtin support for documenting config specific information
538 // regardless of the actual config it was given
539 // (https://doc.rust-lang.org/rustdoc/advanced-features.html#cfgdoc-documenting-platform-specific-or-feature-specific-information),
540 // so we generate the rustdoc for only the primary module so that we have a
541 // single set of docs to refer to.
542 if ctx.Module() != ctx.PrimaryModule() {
543 return android.OptionalPath{}
544 }
545
546 return android.OptionalPathForPath(Rustdoc(ctx, library.srcPath(ctx, deps),
547 deps, flags))
548}
549
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700550func (library *libraryDecorator) getStem(ctx ModuleContext) string {
551 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
552 validateLibraryStem(ctx, stem, library.crateName())
553
554 return stem + String(library.baseCompiler.Properties.Suffix)
555}
556
Ivan Lozano2b081132020-09-08 12:46:52 -0400557func (library *libraryDecorator) install(ctx ModuleContext) {
558 // Only shared and dylib variants make sense to install.
559 if library.shared() || library.dylib() {
560 library.baseCompiler.install(ctx)
561 }
562}
563
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400564func (library *libraryDecorator) Disabled() bool {
565 return library.MutatedProperties.VariantIsDisabled
566}
567
568func (library *libraryDecorator) SetDisabled() {
569 library.MutatedProperties.VariantIsDisabled = true
570}
571
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700572var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
573
574func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
575 if crate_name == "" {
576 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
577 }
578
579 // crate_names are used for the library output file, and rustc expects these
580 // to be alphanumeric with underscores allowed.
581 if validCrateName.MatchString(crate_name) {
582 ctx.PropertyErrorf("crate_name",
583 "library crate_names must be alphanumeric with underscores allowed")
584 }
585
586 // Libraries are expected to begin with "lib" followed by the crate_name
587 if !strings.HasPrefix(filename, "lib"+crate_name) {
588 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
589 }
590}
591
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200592// LibraryMutator mutates the libraries into variants according to the
593// build{Rlib,Dylib} attributes.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700594func LibraryMutator(mctx android.BottomUpMutatorContext) {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200595 // Only mutate on Rust libraries.
596 m, ok := mctx.Module().(*Module)
597 if !ok || m.compiler == nil {
598 return
599 }
600 library, ok := m.compiler.(libraryInterface)
601 if !ok {
602 return
603 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400604
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200605 var variants []string
606 // The source variant is used for SourceProvider modules. The other variants (i.e. rlib and dylib)
607 // depend on this variant. It must be the first variant to be declared.
608 sourceVariant := false
609 if m.sourceProvider != nil {
610 variants = append(variants, "source")
611 sourceVariant = true
612 }
613 if library.buildRlib() {
614 variants = append(variants, rlibVariation)
615 }
616 if library.buildDylib() {
617 variants = append(variants, dylibVariation)
618 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400619
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200620 if len(variants) == 0 {
621 return
622 }
623 modules := mctx.CreateLocalVariations(variants...)
624
625 // The order of the variations (modules) matches the variant names provided. Iterate
626 // through the new variation modules and set their mutated properties.
627 for i, v := range modules {
628 switch variants[i] {
629 case rlibVariation:
630 v.(*Module).compiler.(libraryInterface).setRlib()
631 case dylibVariation:
632 v.(*Module).compiler.(libraryInterface).setDylib()
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400633 if v.(*Module).ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
Ivan Lozano6a884432020-12-02 09:15:16 -0500634 // TODO(b/165791368)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400635 // Disable dylib Vendor Ramdisk variations until we support these.
Ivan Lozano6a884432020-12-02 09:15:16 -0500636 v.(*Module).Disable()
637 }
Ivan Lozano1921e802021-05-20 13:39:16 -0400638
639 variation := v.(*Module).ModuleBase.ImageVariation().Variation
640 if strings.HasPrefix(variation, cc.VendorVariationPrefix) &&
641 m.HasVendorVariant() &&
642 !cc.IsVendorProprietaryModule(mctx) &&
643 strings.TrimPrefix(variation, cc.VendorVariationPrefix) == mctx.DeviceConfig().VndkVersion() {
644
645 // cc.MutateImage runs before LibraryMutator, so vendor variations which are meant for rlibs only are
646 // produced for Dylibs; however, dylibs should not be enabled for boardVndkVersion for
647 // non-vendor proprietary modules.
648 v.(*Module).Disable()
649 }
650
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200651 case "source":
652 v.(*Module).compiler.(libraryInterface).setSource()
653 // The source variant does not produce any library.
654 // Disable the compilation steps.
655 v.(*Module).compiler.SetDisabled()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700656 }
657 }
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200658
659 // If a source variant is created, add an inter-variant dependency
660 // between the other variants and the source variant.
661 if sourceVariant {
662 sv := modules[0]
663 for _, v := range modules[1:] {
664 if !v.Enabled() {
665 continue
666 }
667 mctx.AddInterVariantDependency(sourceDepTag, v, sv)
668 }
669 // Alias the source variation so it can be named directly in "srcs" properties.
670 mctx.AliasVariation("source")
671 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700672}
Ivan Lozano2b081132020-09-08 12:46:52 -0400673
674func LibstdMutator(mctx android.BottomUpMutatorContext) {
675 if m, ok := mctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() {
676 switch library := m.compiler.(type) {
677 case libraryInterface:
678 // Only create a variant if a library is actually being built.
679 if library.rlib() && !library.sysroot() {
680 variants := []string{"rlib-std", "dylib-std"}
681 modules := mctx.CreateLocalVariations(variants...)
682
683 rlib := modules[0].(*Module)
684 dylib := modules[1].(*Module)
685 rlib.compiler.(libraryInterface).setRlibStd()
686 dylib.compiler.(libraryInterface).setDylibStd()
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400687 if dylib.ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation ||
688 strings.HasPrefix(dylib.ModuleBase.ImageVariation().Variation, cc.VendorVariationPrefix) {
Ivan Lozano6a884432020-12-02 09:15:16 -0500689 // TODO(b/165791368)
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400690 // Disable rlibs that link against dylib-std on vendor and vendor ramdisk variations until those dylib
Ivan Lozano6a884432020-12-02 09:15:16 -0500691 // variants are properly supported.
692 dylib.Disable()
693 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400694 rlib.Properties.RustSubName += RlibStdlibSuffix
695 dylib.Properties.RustSubName += DylibStdlibSuffix
Ivan Lozano2b081132020-09-08 12:46:52 -0400696 }
697 }
698 }
699}
Ivan Lozano1921e802021-05-20 13:39:16 -0400700
701func (l *libraryDecorator) snapshotHeaders() android.Paths {
702 if l.collectedSnapshotHeaders == nil {
703 panic("snapshotHeaders() must be called after collectHeadersForSnapshot()")
704 }
705 return l.collectedSnapshotHeaders
706}
707
708// collectHeadersForSnapshot collects all exported headers from library.
709// It globs header files in the source tree for exported include directories,
710// and tracks generated header files separately.
711//
712// This is to be called from GenerateAndroidBuildActions, and then collected
713// header files can be retrieved by snapshotHeaders().
714func (l *libraryDecorator) collectHeadersForSnapshot(ctx android.ModuleContext, deps PathDeps) {
715 ret := android.Paths{}
716
717 // Glob together the headers from the modules include_dirs property
718 for _, path := range android.CopyOfPaths(l.includeDirs) {
719 dir := path.String()
720 glob, err := ctx.GlobWithDeps(dir+"/**/*", nil)
721 if err != nil {
722 ctx.ModuleErrorf("glob failed: %#v", err)
723 return
724 }
725
726 for _, header := range glob {
727 // Filter out only the files with extensions that are headers.
728 found := false
729 for _, ext := range cc.HeaderExts {
730 if strings.HasSuffix(header, ext) {
731 found = true
732 break
733 }
734 }
735 if !found {
736 continue
737 }
738 ret = append(ret, android.PathForSource(ctx, header))
739 }
740 }
741
742 // Glob together the headers from C dependencies as well, starting with non-generated headers.
743 ret = append(ret, cc.GlobHeadersForSnapshot(ctx, append(android.CopyOfPaths(deps.depIncludePaths), deps.depSystemIncludePaths...))...)
744
745 // Collect generated headers from C dependencies.
746 ret = append(ret, cc.GlobGeneratedHeadersForSnapshot(ctx, deps.depGeneratedHeaders)...)
747
748 // TODO(185577950): If support for generated headers is added, they need to be collected here as well.
749 l.collectedSnapshotHeaders = ret
750}