blob: d5ab1a477f312354b5625b0a4c0c2eb7b758a36a [file] [log] [blame]
Ivan Lozanoffee3342019-08-27 12:03:00 -07001// Copyright 2019 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package rust
16
17import (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +020018 "fmt"
Ivan Lozanoad8b18b2019-10-31 19:38:29 -070019 "regexp"
20 "strings"
21
Ivan Lozanoffee3342019-08-27 12:03:00 -070022 "android/soong/android"
Colin Cross0de8a1e2020-09-18 14:15:30 -070023 "android/soong/cc"
Kiyoung Kim48f37782021-07-07 12:42:39 +090024 "android/soong/snapshot"
Ivan Lozanoffee3342019-08-27 12:03:00 -070025)
26
Ivan Lozano2b081132020-09-08 12:46:52 -040027var (
28 DylibStdlibSuffix = ".dylib-std"
29 RlibStdlibSuffix = ".rlib-std"
30)
31
Ivan Lozanoffee3342019-08-27 12:03:00 -070032func init() {
33 android.RegisterModuleType("rust_library", RustLibraryFactory)
34 android.RegisterModuleType("rust_library_dylib", RustLibraryDylibFactory)
35 android.RegisterModuleType("rust_library_rlib", RustLibraryRlibFactory)
36 android.RegisterModuleType("rust_library_host", RustLibraryHostFactory)
37 android.RegisterModuleType("rust_library_host_dylib", RustLibraryDylibHostFactory)
38 android.RegisterModuleType("rust_library_host_rlib", RustLibraryRlibHostFactory)
Matthew Maurer2ae05132020-06-23 14:28:53 -070039 android.RegisterModuleType("rust_ffi", RustFFIFactory)
40 android.RegisterModuleType("rust_ffi_shared", RustFFISharedFactory)
41 android.RegisterModuleType("rust_ffi_static", RustFFIStaticFactory)
42 android.RegisterModuleType("rust_ffi_host", RustFFIHostFactory)
43 android.RegisterModuleType("rust_ffi_host_shared", RustFFISharedHostFactory)
44 android.RegisterModuleType("rust_ffi_host_static", RustFFIStaticHostFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070045}
46
47type VariantLibraryProperties struct {
Matthew Maurerc761eec2020-06-25 00:47:46 -070048 Enabled *bool `android:"arch_variant"`
49 Srcs []string `android:"path,arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070050}
51
52type LibraryCompilerProperties struct {
Ivan Lozano52767be2019-10-18 14:49:46 -070053 Rlib VariantLibraryProperties `android:"arch_variant"`
54 Dylib VariantLibraryProperties `android:"arch_variant"`
55 Shared VariantLibraryProperties `android:"arch_variant"`
56 Static VariantLibraryProperties `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070057
Ivan Lozano52767be2019-10-18 14:49:46 -070058 // path to include directories to pass to cc_* modules, only relevant for static/shared variants.
59 Include_dirs []string `android:"path,arch_variant"`
Ivan Lozano2b081132020-09-08 12:46:52 -040060
61 // Whether this library is part of the Rust toolchain sysroot.
62 Sysroot *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070063}
64
65type LibraryMutatedProperties struct {
Ivan Lozanoffee3342019-08-27 12:03:00 -070066 // Build a dylib variant
67 BuildDylib bool `blueprint:"mutated"`
68 // Build an rlib variant
69 BuildRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070070 // Build a shared library variant
71 BuildShared bool `blueprint:"mutated"`
72 // Build a static library variant
73 BuildStatic bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070074
75 // This variant is a dylib
76 VariantIsDylib bool `blueprint:"mutated"`
77 // This variant is an rlib
78 VariantIsRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070079 // This variant is a shared library
80 VariantIsShared bool `blueprint:"mutated"`
81 // This variant is a static library
82 VariantIsStatic bool `blueprint:"mutated"`
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +020083 // This variant is a source provider
84 VariantIsSource bool `blueprint:"mutated"`
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040085
86 // This variant is disabled and should not be compiled
87 // (used for SourceProvider variants that produce only source)
88 VariantIsDisabled bool `blueprint:"mutated"`
Ivan Lozano2b081132020-09-08 12:46:52 -040089
90 // Whether this library variant should be link libstd via rlibs
91 VariantIsStaticStd bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070092}
93
94type libraryDecorator struct {
95 *baseCompiler
Matthew Maurerbb3add12020-06-25 09:34:12 -070096 *flagExporter
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +020097 stripper Stripper
Ivan Lozanoffee3342019-08-27 12:03:00 -070098
Ivan Lozano8a23fa42020-06-16 10:26:57 -040099 Properties LibraryCompilerProperties
100 MutatedProperties LibraryMutatedProperties
101 includeDirs android.Paths
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400102 sourceProvider SourceProvider
Ivan Lozano1921e802021-05-20 13:39:16 -0400103
104 collectedSnapshotHeaders android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700105}
106
107type libraryInterface interface {
108 rlib() bool
109 dylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700110 static() bool
111 shared() bool
Ivan Lozano2b081132020-09-08 12:46:52 -0400112 sysroot() bool
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200113 source() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700114
115 // Returns true if the build options for the module have selected a particular build type
116 buildRlib() bool
117 buildDylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700118 buildShared() bool
119 buildStatic() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700120
121 // Sets a particular variant type
122 setRlib()
123 setDylib()
Ivan Lozano52767be2019-10-18 14:49:46 -0700124 setShared()
125 setStatic()
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200126 setSource()
Ivan Lozano52767be2019-10-18 14:49:46 -0700127
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400128 // libstd linkage functions
129 rlibStd() bool
Ivan Lozano2b081132020-09-08 12:46:52 -0400130 setRlibStd()
131 setDylibStd()
132
Ivan Lozano52767be2019-10-18 14:49:46 -0700133 // Build a specific library variant
Matthew Maurer2ae05132020-06-23 14:28:53 -0700134 BuildOnlyFFI()
135 BuildOnlyRust()
Ivan Lozano52767be2019-10-18 14:49:46 -0700136 BuildOnlyRlib()
137 BuildOnlyDylib()
138 BuildOnlyStatic()
139 BuildOnlyShared()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700140}
141
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400142func (library *libraryDecorator) nativeCoverage() bool {
143 return true
144}
145
Ivan Lozanoffee3342019-08-27 12:03:00 -0700146func (library *libraryDecorator) rlib() bool {
147 return library.MutatedProperties.VariantIsRlib
148}
149
Ivan Lozano2b081132020-09-08 12:46:52 -0400150func (library *libraryDecorator) sysroot() bool {
151 return Bool(library.Properties.Sysroot)
152}
153
Ivan Lozanoffee3342019-08-27 12:03:00 -0700154func (library *libraryDecorator) dylib() bool {
155 return library.MutatedProperties.VariantIsDylib
156}
157
Ivan Lozano52767be2019-10-18 14:49:46 -0700158func (library *libraryDecorator) shared() bool {
159 return library.MutatedProperties.VariantIsShared
160}
161
162func (library *libraryDecorator) static() bool {
163 return library.MutatedProperties.VariantIsStatic
164}
165
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200166func (library *libraryDecorator) source() bool {
167 return library.MutatedProperties.VariantIsSource
168}
169
Ivan Lozanoffee3342019-08-27 12:03:00 -0700170func (library *libraryDecorator) buildRlib() bool {
171 return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true)
172}
173
174func (library *libraryDecorator) buildDylib() bool {
175 return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true)
176}
177
Ivan Lozano52767be2019-10-18 14:49:46 -0700178func (library *libraryDecorator) buildShared() bool {
179 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
180}
181
182func (library *libraryDecorator) buildStatic() bool {
183 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
184}
185
Ivan Lozanoffee3342019-08-27 12:03:00 -0700186func (library *libraryDecorator) setRlib() {
187 library.MutatedProperties.VariantIsRlib = true
188 library.MutatedProperties.VariantIsDylib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700189 library.MutatedProperties.VariantIsStatic = false
190 library.MutatedProperties.VariantIsShared = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700191}
192
193func (library *libraryDecorator) setDylib() {
194 library.MutatedProperties.VariantIsRlib = false
195 library.MutatedProperties.VariantIsDylib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700196 library.MutatedProperties.VariantIsStatic = false
197 library.MutatedProperties.VariantIsShared = false
198}
199
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400200func (library *libraryDecorator) rlibStd() bool {
201 return library.MutatedProperties.VariantIsStaticStd
202}
203
Ivan Lozano2b081132020-09-08 12:46:52 -0400204func (library *libraryDecorator) setRlibStd() {
205 library.MutatedProperties.VariantIsStaticStd = true
206}
207
208func (library *libraryDecorator) setDylibStd() {
209 library.MutatedProperties.VariantIsStaticStd = false
210}
211
Ivan Lozano52767be2019-10-18 14:49:46 -0700212func (library *libraryDecorator) setShared() {
213 library.MutatedProperties.VariantIsStatic = false
214 library.MutatedProperties.VariantIsShared = true
215 library.MutatedProperties.VariantIsRlib = false
216 library.MutatedProperties.VariantIsDylib = false
217}
218
219func (library *libraryDecorator) setStatic() {
220 library.MutatedProperties.VariantIsStatic = true
221 library.MutatedProperties.VariantIsShared = false
222 library.MutatedProperties.VariantIsRlib = false
223 library.MutatedProperties.VariantIsDylib = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700224}
225
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200226func (library *libraryDecorator) setSource() {
227 library.MutatedProperties.VariantIsSource = true
228}
229
Liz Kammer356f7d42021-01-26 09:18:53 -0500230func (library *libraryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Ivan Lozano1921e802021-05-20 13:39:16 -0400231 if ctx.Module().(*Module).InVendor() {
232 // Vendor modules should statically link libstd.
233 return rlibAutoDep
234 } else if library.preferRlib() {
Ivan Lozanoea086132020-12-08 14:43:00 -0500235 return rlibAutoDep
236 } else if library.rlib() || library.static() {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700237 return rlibAutoDep
238 } else if library.dylib() || library.shared() {
239 return dylibAutoDep
Liz Kammer356f7d42021-01-26 09:18:53 -0500240 } else if ctx.BazelConversionMode() {
241 // In Bazel conversion mode, we are currently ignoring the deptag, so we just need to supply a
242 // compatible tag in order to add the dependency.
243 return rlibAutoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700244 } else {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200245 panic(fmt.Errorf("autoDep called on library %q that has no enabled variants.", ctx.ModuleName()))
Matthew Maurer0f003b12020-06-29 14:34:06 -0700246 }
247}
248
Ivan Lozanoea086132020-12-08 14:43:00 -0500249func (library *libraryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozano1921e802021-05-20 13:39:16 -0400250 if ctx.RustModule().InVendor() {
251 // Vendor modules should statically link libstd.
252 return RlibLinkage
253 } else if library.static() || library.MutatedProperties.VariantIsStaticStd {
Ivan Lozanoea086132020-12-08 14:43:00 -0500254 return RlibLinkage
255 } else if library.baseCompiler.preferRlib() {
256 return RlibLinkage
257 }
258 return DefaultLinkage
259}
260
Ivan Lozanoffee3342019-08-27 12:03:00 -0700261var _ compiler = (*libraryDecorator)(nil)
Ivan Lozano52767be2019-10-18 14:49:46 -0700262var _ libraryInterface = (*libraryDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700263var _ exportedFlagsProducer = (*libraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700264
Matthew Maurer2ae05132020-06-23 14:28:53 -0700265// rust_library produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700266func RustLibraryFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700267 module, library := NewRustLibrary(android.HostAndDeviceSupported)
268 library.BuildOnlyRust()
269 return module.Init()
270}
271
272// rust_ffi produces all ffi variants.
273func RustFFIFactory() android.Module {
274 module, library := NewRustLibrary(android.HostAndDeviceSupported)
275 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700276 return module.Init()
277}
278
279// rust_library_dylib produces a dylib.
280func RustLibraryDylibFactory() android.Module {
281 module, library := NewRustLibrary(android.HostAndDeviceSupported)
282 library.BuildOnlyDylib()
283 return module.Init()
284}
285
286// rust_library_rlib produces an rlib.
287func RustLibraryRlibFactory() android.Module {
288 module, library := NewRustLibrary(android.HostAndDeviceSupported)
289 library.BuildOnlyRlib()
290 return module.Init()
291}
292
Matthew Maurer2ae05132020-06-23 14:28:53 -0700293// rust_ffi_shared produces a shared library.
294func RustFFISharedFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700295 module, library := NewRustLibrary(android.HostAndDeviceSupported)
296 library.BuildOnlyShared()
297 return module.Init()
298}
299
Matthew Maurer2ae05132020-06-23 14:28:53 -0700300// rust_ffi_static produces a static library.
301func RustFFIStaticFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700302 module, library := NewRustLibrary(android.HostAndDeviceSupported)
303 library.BuildOnlyStatic()
304 return module.Init()
305}
306
Matthew Maurer2ae05132020-06-23 14:28:53 -0700307// rust_library_host produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700308func RustLibraryHostFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700309 module, library := NewRustLibrary(android.HostSupported)
310 library.BuildOnlyRust()
311 return module.Init()
312}
313
314// rust_ffi_host produces all FFI variants.
315func RustFFIHostFactory() android.Module {
316 module, library := NewRustLibrary(android.HostSupported)
317 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700318 return module.Init()
319}
320
321// rust_library_dylib_host produces a dylib.
322func RustLibraryDylibHostFactory() android.Module {
323 module, library := NewRustLibrary(android.HostSupported)
324 library.BuildOnlyDylib()
325 return module.Init()
326}
327
328// rust_library_rlib_host produces an rlib.
329func RustLibraryRlibHostFactory() android.Module {
330 module, library := NewRustLibrary(android.HostSupported)
331 library.BuildOnlyRlib()
332 return module.Init()
333}
334
Matthew Maurer2ae05132020-06-23 14:28:53 -0700335// rust_ffi_static_host produces a static library.
336func RustFFIStaticHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700337 module, library := NewRustLibrary(android.HostSupported)
338 library.BuildOnlyStatic()
339 return module.Init()
340}
341
Matthew Maurer2ae05132020-06-23 14:28:53 -0700342// rust_ffi_shared_host produces an shared library.
343func RustFFISharedHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700344 module, library := NewRustLibrary(android.HostSupported)
345 library.BuildOnlyShared()
346 return module.Init()
347}
348
Matthew Maurer2ae05132020-06-23 14:28:53 -0700349func (library *libraryDecorator) BuildOnlyFFI() {
350 library.MutatedProperties.BuildDylib = false
351 library.MutatedProperties.BuildRlib = false
352 library.MutatedProperties.BuildShared = true
353 library.MutatedProperties.BuildStatic = true
354}
355
356func (library *libraryDecorator) BuildOnlyRust() {
357 library.MutatedProperties.BuildDylib = true
358 library.MutatedProperties.BuildRlib = true
359 library.MutatedProperties.BuildShared = false
360 library.MutatedProperties.BuildStatic = false
361}
362
Ivan Lozanoffee3342019-08-27 12:03:00 -0700363func (library *libraryDecorator) BuildOnlyDylib() {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700364 library.MutatedProperties.BuildDylib = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700365 library.MutatedProperties.BuildRlib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700366 library.MutatedProperties.BuildShared = false
367 library.MutatedProperties.BuildStatic = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700368}
369
370func (library *libraryDecorator) BuildOnlyRlib() {
371 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700372 library.MutatedProperties.BuildRlib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700373 library.MutatedProperties.BuildShared = false
374 library.MutatedProperties.BuildStatic = false
375}
376
377func (library *libraryDecorator) BuildOnlyStatic() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700378 library.MutatedProperties.BuildRlib = false
379 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700380 library.MutatedProperties.BuildShared = false
381 library.MutatedProperties.BuildStatic = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700382}
383
384func (library *libraryDecorator) BuildOnlyShared() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700385 library.MutatedProperties.BuildRlib = false
386 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700387 library.MutatedProperties.BuildStatic = false
388 library.MutatedProperties.BuildShared = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700389}
390
391func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400392 module := newModule(hod, android.MultilibBoth)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700393
394 library := &libraryDecorator{
395 MutatedProperties: LibraryMutatedProperties{
Matthew Maurer2ae05132020-06-23 14:28:53 -0700396 BuildDylib: false,
397 BuildRlib: false,
398 BuildShared: false,
399 BuildStatic: false,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700400 },
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800401 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Matthew Maurerbb3add12020-06-25 09:34:12 -0700402 flagExporter: NewFlagExporter(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700403 }
404
405 module.compiler = library
406
407 return module, library
408}
409
410func (library *libraryDecorator) compilerProps() []interface{} {
411 return append(library.baseCompiler.compilerProps(),
412 &library.Properties,
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200413 &library.MutatedProperties,
414 &library.stripper.StripProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700415}
416
Ivan Lozanof1c84332019-09-20 11:00:37 -0700417func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
418 deps = library.baseCompiler.compilerDeps(ctx, deps)
419
Ivan Lozano52767be2019-10-18 14:49:46 -0700420 if ctx.toolchain().Bionic() && (library.dylib() || library.shared()) {
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +0100421 deps = bionicDeps(ctx, deps, false)
Ivan Lozano12ee9ca2020-04-07 13:19:44 -0400422 deps.CrtBegin = "crtbegin_so"
423 deps.CrtEnd = "crtend_so"
Ivan Lozanof1c84332019-09-20 11:00:37 -0700424 }
425
426 return deps
427}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400428
429func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string {
430 return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
431}
432
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800433func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200434 flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800435 flags = library.baseCompiler.compilerFlags(ctx, flags)
436 if library.shared() || library.static() {
437 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
438 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400439 if library.shared() {
440 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
441 }
442
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800443 return flags
444}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700445
Ivan Lozanoffee3342019-08-27 12:03:00 -0700446func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200447 var outputFile android.ModuleOutPath
448 var fileName string
Dan Albert06feee92021-03-19 15:06:02 -0700449 srcPath := library.srcPath(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700450
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400451 if library.sourceProvider != nil {
Ivan Lozano9d74a522020-12-01 09:25:22 -0500452 deps.srcProviderFiles = append(deps.srcProviderFiles, library.sourceProvider.Srcs()...)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400453 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700454
455 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500456 flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...)
Ivan Lozano2093af22020-08-25 12:48:19 -0400457 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700458
Matthew Maurer46c46cc2020-01-13 16:34:34 -0800459 if library.dylib() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700460 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
461 // https://github.com/rust-lang/rust/issues/19680
462 // https://github.com/rust-lang/rust/issues/34909
463 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
464 }
465
Ivan Lozanoffee3342019-08-27 12:03:00 -0700466 if library.rlib() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200467 fileName = library.getStem(ctx) + ctx.toolchain().RlibSuffix()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700468 outputFile = android.PathForModuleOut(ctx, fileName)
469
Dan Albert06feee92021-03-19 15:06:02 -0700470 TransformSrctoRlib(ctx, srcPath, deps, flags, outputFile)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700471 } else if library.dylib() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200472 fileName = library.getStem(ctx) + ctx.toolchain().DylibSuffix()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700473 outputFile = android.PathForModuleOut(ctx, fileName)
474
Dan Albert06feee92021-03-19 15:06:02 -0700475 TransformSrctoDylib(ctx, srcPath, deps, flags, outputFile)
Ivan Lozano52767be2019-10-18 14:49:46 -0700476 } else if library.static() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200477 fileName = library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
Ivan Lozano52767be2019-10-18 14:49:46 -0700478 outputFile = android.PathForModuleOut(ctx, fileName)
479
Dan Albert06feee92021-03-19 15:06:02 -0700480 TransformSrctoStatic(ctx, srcPath, deps, flags, outputFile)
Ivan Lozano52767be2019-10-18 14:49:46 -0700481 } else if library.shared() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200482 fileName = library.sharedLibFilename(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700483 outputFile = android.PathForModuleOut(ctx, fileName)
484
Dan Albert06feee92021-03-19 15:06:02 -0700485 TransformSrctoShared(ctx, srcPath, deps, flags, outputFile)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700486 }
487
Thiébaud Weksteen4fab05a2021-04-14 19:15:48 +0200488 if !library.rlib() && !library.static() && library.stripper.NeedsStrip(ctx) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200489 strippedOutputFile := android.PathForModuleOut(ctx, "stripped", fileName)
490 library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
491 library.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
492 }
493
Ivan Lozano52767be2019-10-18 14:49:46 -0700494 if library.rlib() || library.dylib() {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700495 library.flagExporter.exportLinkDirs(deps.linkDirs...)
Colin Cross0de8a1e2020-09-18 14:15:30 -0700496 library.flagExporter.exportLinkObjects(deps.linkObjects...)
Ivan Lozano52767be2019-10-18 14:49:46 -0700497 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700498
Colin Cross0de8a1e2020-09-18 14:15:30 -0700499 if library.static() || library.shared() {
500 ctx.SetProvider(cc.FlagExporterInfoProvider, cc.FlagExporterInfo{
501 IncludeDirs: library.includeDirs,
502 })
503 }
504
505 if library.shared() {
506 ctx.SetProvider(cc.SharedLibraryInfoProvider, cc.SharedLibraryInfo{
Liz Kammeref6dfea2021-06-08 15:37:09 -0400507 SharedLibrary: outputFile,
508 Target: ctx.Target(),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700509 })
510 }
511
512 if library.static() {
513 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(outputFile).Build()
514 ctx.SetProvider(cc.StaticLibraryInfoProvider, cc.StaticLibraryInfo{
515 StaticLibrary: outputFile,
516
517 TransitiveStaticLibrariesForOrdering: depSet,
518 })
519 }
520
521 library.flagExporter.setProvider(ctx)
522
Ivan Lozanoffee3342019-08-27 12:03:00 -0700523 return outputFile
524}
525
Dan Albert06feee92021-03-19 15:06:02 -0700526func (library *libraryDecorator) srcPath(ctx ModuleContext, deps PathDeps) android.Path {
527 if library.sourceProvider != nil {
528 // Assume the first source from the source provider is the library entry point.
529 return library.sourceProvider.Srcs()[0]
530 } else {
531 path, _ := srcPathFromModuleSrcs(ctx, library.baseCompiler.Properties.Srcs)
532 return path
533 }
534}
535
536func (library *libraryDecorator) rustdoc(ctx ModuleContext, flags Flags,
537 deps PathDeps) android.OptionalPath {
538 // rustdoc has builtin support for documenting config specific information
539 // regardless of the actual config it was given
540 // (https://doc.rust-lang.org/rustdoc/advanced-features.html#cfgdoc-documenting-platform-specific-or-feature-specific-information),
541 // so we generate the rustdoc for only the primary module so that we have a
542 // single set of docs to refer to.
543 if ctx.Module() != ctx.PrimaryModule() {
544 return android.OptionalPath{}
545 }
546
547 return android.OptionalPathForPath(Rustdoc(ctx, library.srcPath(ctx, deps),
548 deps, flags))
549}
550
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700551func (library *libraryDecorator) getStem(ctx ModuleContext) string {
552 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
553 validateLibraryStem(ctx, stem, library.crateName())
554
555 return stem + String(library.baseCompiler.Properties.Suffix)
556}
557
Ivan Lozano2b081132020-09-08 12:46:52 -0400558func (library *libraryDecorator) install(ctx ModuleContext) {
559 // Only shared and dylib variants make sense to install.
560 if library.shared() || library.dylib() {
561 library.baseCompiler.install(ctx)
562 }
563}
564
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400565func (library *libraryDecorator) Disabled() bool {
566 return library.MutatedProperties.VariantIsDisabled
567}
568
569func (library *libraryDecorator) SetDisabled() {
570 library.MutatedProperties.VariantIsDisabled = true
571}
572
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700573var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
574
575func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
576 if crate_name == "" {
577 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
578 }
579
580 // crate_names are used for the library output file, and rustc expects these
581 // to be alphanumeric with underscores allowed.
582 if validCrateName.MatchString(crate_name) {
583 ctx.PropertyErrorf("crate_name",
584 "library crate_names must be alphanumeric with underscores allowed")
585 }
586
587 // Libraries are expected to begin with "lib" followed by the crate_name
588 if !strings.HasPrefix(filename, "lib"+crate_name) {
589 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
590 }
591}
592
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200593// LibraryMutator mutates the libraries into variants according to the
594// build{Rlib,Dylib} attributes.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700595func LibraryMutator(mctx android.BottomUpMutatorContext) {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200596 // Only mutate on Rust libraries.
597 m, ok := mctx.Module().(*Module)
598 if !ok || m.compiler == nil {
599 return
600 }
601 library, ok := m.compiler.(libraryInterface)
602 if !ok {
603 return
604 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400605
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200606 var variants []string
607 // The source variant is used for SourceProvider modules. The other variants (i.e. rlib and dylib)
608 // depend on this variant. It must be the first variant to be declared.
609 sourceVariant := false
610 if m.sourceProvider != nil {
611 variants = append(variants, "source")
612 sourceVariant = true
613 }
614 if library.buildRlib() {
615 variants = append(variants, rlibVariation)
616 }
617 if library.buildDylib() {
618 variants = append(variants, dylibVariation)
619 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400620
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200621 if len(variants) == 0 {
622 return
623 }
624 modules := mctx.CreateLocalVariations(variants...)
625
626 // The order of the variations (modules) matches the variant names provided. Iterate
627 // through the new variation modules and set their mutated properties.
628 for i, v := range modules {
629 switch variants[i] {
630 case rlibVariation:
631 v.(*Module).compiler.(libraryInterface).setRlib()
632 case dylibVariation:
633 v.(*Module).compiler.(libraryInterface).setDylib()
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400634 if v.(*Module).ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
Ivan Lozano6a884432020-12-02 09:15:16 -0500635 // TODO(b/165791368)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400636 // Disable dylib Vendor Ramdisk variations until we support these.
Ivan Lozano6a884432020-12-02 09:15:16 -0500637 v.(*Module).Disable()
638 }
Ivan Lozano1921e802021-05-20 13:39:16 -0400639
640 variation := v.(*Module).ModuleBase.ImageVariation().Variation
641 if strings.HasPrefix(variation, cc.VendorVariationPrefix) &&
642 m.HasVendorVariant() &&
Kiyoung Kim48f37782021-07-07 12:42:39 +0900643 !snapshot.IsVendorProprietaryModule(mctx) &&
Ivan Lozano1921e802021-05-20 13:39:16 -0400644 strings.TrimPrefix(variation, cc.VendorVariationPrefix) == mctx.DeviceConfig().VndkVersion() {
645
646 // cc.MutateImage runs before LibraryMutator, so vendor variations which are meant for rlibs only are
647 // produced for Dylibs; however, dylibs should not be enabled for boardVndkVersion for
648 // non-vendor proprietary modules.
649 v.(*Module).Disable()
650 }
651
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200652 case "source":
653 v.(*Module).compiler.(libraryInterface).setSource()
654 // The source variant does not produce any library.
655 // Disable the compilation steps.
656 v.(*Module).compiler.SetDisabled()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700657 }
658 }
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200659
660 // If a source variant is created, add an inter-variant dependency
661 // between the other variants and the source variant.
662 if sourceVariant {
663 sv := modules[0]
664 for _, v := range modules[1:] {
665 if !v.Enabled() {
666 continue
667 }
668 mctx.AddInterVariantDependency(sourceDepTag, v, sv)
669 }
670 // Alias the source variation so it can be named directly in "srcs" properties.
671 mctx.AliasVariation("source")
672 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700673}
Ivan Lozano2b081132020-09-08 12:46:52 -0400674
675func LibstdMutator(mctx android.BottomUpMutatorContext) {
676 if m, ok := mctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() {
677 switch library := m.compiler.(type) {
678 case libraryInterface:
679 // Only create a variant if a library is actually being built.
680 if library.rlib() && !library.sysroot() {
681 variants := []string{"rlib-std", "dylib-std"}
682 modules := mctx.CreateLocalVariations(variants...)
683
684 rlib := modules[0].(*Module)
685 dylib := modules[1].(*Module)
686 rlib.compiler.(libraryInterface).setRlibStd()
687 dylib.compiler.(libraryInterface).setDylibStd()
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400688 if dylib.ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation ||
689 strings.HasPrefix(dylib.ModuleBase.ImageVariation().Variation, cc.VendorVariationPrefix) {
Ivan Lozano6a884432020-12-02 09:15:16 -0500690 // TODO(b/165791368)
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400691 // Disable rlibs that link against dylib-std on vendor and vendor ramdisk variations until those dylib
Ivan Lozano6a884432020-12-02 09:15:16 -0500692 // variants are properly supported.
693 dylib.Disable()
694 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400695 rlib.Properties.RustSubName += RlibStdlibSuffix
696 dylib.Properties.RustSubName += DylibStdlibSuffix
Ivan Lozano2b081132020-09-08 12:46:52 -0400697 }
698 }
699 }
700}
Ivan Lozano1921e802021-05-20 13:39:16 -0400701
702func (l *libraryDecorator) snapshotHeaders() android.Paths {
703 if l.collectedSnapshotHeaders == nil {
704 panic("snapshotHeaders() must be called after collectHeadersForSnapshot()")
705 }
706 return l.collectedSnapshotHeaders
707}
708
709// collectHeadersForSnapshot collects all exported headers from library.
710// It globs header files in the source tree for exported include directories,
711// and tracks generated header files separately.
712//
713// This is to be called from GenerateAndroidBuildActions, and then collected
714// header files can be retrieved by snapshotHeaders().
715func (l *libraryDecorator) collectHeadersForSnapshot(ctx android.ModuleContext, deps PathDeps) {
716 ret := android.Paths{}
717
718 // Glob together the headers from the modules include_dirs property
719 for _, path := range android.CopyOfPaths(l.includeDirs) {
720 dir := path.String()
721 glob, err := ctx.GlobWithDeps(dir+"/**/*", nil)
722 if err != nil {
723 ctx.ModuleErrorf("glob failed: %#v", err)
724 return
725 }
726
727 for _, header := range glob {
728 // Filter out only the files with extensions that are headers.
729 found := false
730 for _, ext := range cc.HeaderExts {
731 if strings.HasSuffix(header, ext) {
732 found = true
733 break
734 }
735 }
736 if !found {
737 continue
738 }
739 ret = append(ret, android.PathForSource(ctx, header))
740 }
741 }
742
743 // Glob together the headers from C dependencies as well, starting with non-generated headers.
744 ret = append(ret, cc.GlobHeadersForSnapshot(ctx, append(android.CopyOfPaths(deps.depIncludePaths), deps.depSystemIncludePaths...))...)
745
746 // Collect generated headers from C dependencies.
747 ret = append(ret, cc.GlobGeneratedHeadersForSnapshot(ctx, deps.depGeneratedHeaders)...)
748
749 // TODO(185577950): If support for generated headers is added, they need to be collected here as well.
750 l.collectedSnapshotHeaders = ret
751}