blob: 4e6031d86ae3642fbcfd3049533c95eb64358299 [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 Lozanoffee3342019-08-27 12:03:00 -0700102}
103
104type libraryInterface interface {
105 rlib() bool
106 dylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700107 static() bool
108 shared() bool
Ivan Lozano2b081132020-09-08 12:46:52 -0400109 sysroot() bool
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200110 source() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700111
112 // Returns true if the build options for the module have selected a particular build type
113 buildRlib() bool
114 buildDylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -0700115 buildShared() bool
116 buildStatic() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700117
118 // Sets a particular variant type
119 setRlib()
120 setDylib()
Ivan Lozano52767be2019-10-18 14:49:46 -0700121 setShared()
122 setStatic()
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200123 setSource()
Ivan Lozano52767be2019-10-18 14:49:46 -0700124
Ivan Lozano2b081132020-09-08 12:46:52 -0400125 // Set libstd linkage
126 setRlibStd()
127 setDylibStd()
128
Ivan Lozano52767be2019-10-18 14:49:46 -0700129 // Build a specific library variant
Matthew Maurer2ae05132020-06-23 14:28:53 -0700130 BuildOnlyFFI()
131 BuildOnlyRust()
Ivan Lozano52767be2019-10-18 14:49:46 -0700132 BuildOnlyRlib()
133 BuildOnlyDylib()
134 BuildOnlyStatic()
135 BuildOnlyShared()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700136}
137
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400138func (library *libraryDecorator) nativeCoverage() bool {
139 return true
140}
141
Ivan Lozanoffee3342019-08-27 12:03:00 -0700142func (library *libraryDecorator) rlib() bool {
143 return library.MutatedProperties.VariantIsRlib
144}
145
Ivan Lozano2b081132020-09-08 12:46:52 -0400146func (library *libraryDecorator) sysroot() bool {
147 return Bool(library.Properties.Sysroot)
148}
149
Ivan Lozanoffee3342019-08-27 12:03:00 -0700150func (library *libraryDecorator) dylib() bool {
151 return library.MutatedProperties.VariantIsDylib
152}
153
Ivan Lozano52767be2019-10-18 14:49:46 -0700154func (library *libraryDecorator) shared() bool {
155 return library.MutatedProperties.VariantIsShared
156}
157
158func (library *libraryDecorator) static() bool {
159 return library.MutatedProperties.VariantIsStatic
160}
161
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200162func (library *libraryDecorator) source() bool {
163 return library.MutatedProperties.VariantIsSource
164}
165
Ivan Lozanoffee3342019-08-27 12:03:00 -0700166func (library *libraryDecorator) buildRlib() bool {
167 return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true)
168}
169
170func (library *libraryDecorator) buildDylib() bool {
171 return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true)
172}
173
Ivan Lozano52767be2019-10-18 14:49:46 -0700174func (library *libraryDecorator) buildShared() bool {
175 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
176}
177
178func (library *libraryDecorator) buildStatic() bool {
179 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
180}
181
Ivan Lozanoffee3342019-08-27 12:03:00 -0700182func (library *libraryDecorator) setRlib() {
183 library.MutatedProperties.VariantIsRlib = true
184 library.MutatedProperties.VariantIsDylib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700185 library.MutatedProperties.VariantIsStatic = false
186 library.MutatedProperties.VariantIsShared = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700187}
188
189func (library *libraryDecorator) setDylib() {
190 library.MutatedProperties.VariantIsRlib = false
191 library.MutatedProperties.VariantIsDylib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700192 library.MutatedProperties.VariantIsStatic = false
193 library.MutatedProperties.VariantIsShared = false
194}
195
Ivan Lozano2b081132020-09-08 12:46:52 -0400196func (library *libraryDecorator) setRlibStd() {
197 library.MutatedProperties.VariantIsStaticStd = true
198}
199
200func (library *libraryDecorator) setDylibStd() {
201 library.MutatedProperties.VariantIsStaticStd = false
202}
203
Ivan Lozano52767be2019-10-18 14:49:46 -0700204func (library *libraryDecorator) setShared() {
205 library.MutatedProperties.VariantIsStatic = false
206 library.MutatedProperties.VariantIsShared = true
207 library.MutatedProperties.VariantIsRlib = false
208 library.MutatedProperties.VariantIsDylib = false
209}
210
211func (library *libraryDecorator) setStatic() {
212 library.MutatedProperties.VariantIsStatic = true
213 library.MutatedProperties.VariantIsShared = false
214 library.MutatedProperties.VariantIsRlib = false
215 library.MutatedProperties.VariantIsDylib = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700216}
217
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200218func (library *libraryDecorator) setSource() {
219 library.MutatedProperties.VariantIsSource = true
220}
221
Liz Kammer356f7d42021-01-26 09:18:53 -0500222func (library *libraryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Ivan Lozanoea086132020-12-08 14:43:00 -0500223 if library.preferRlib() {
224 return rlibAutoDep
225 } else if library.rlib() || library.static() {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700226 return rlibAutoDep
227 } else if library.dylib() || library.shared() {
228 return dylibAutoDep
Liz Kammer356f7d42021-01-26 09:18:53 -0500229 } else if ctx.BazelConversionMode() {
230 // In Bazel conversion mode, we are currently ignoring the deptag, so we just need to supply a
231 // compatible tag in order to add the dependency.
232 return rlibAutoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700233 } else {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200234 panic(fmt.Errorf("autoDep called on library %q that has no enabled variants.", ctx.ModuleName()))
Matthew Maurer0f003b12020-06-29 14:34:06 -0700235 }
236}
237
Ivan Lozanoea086132020-12-08 14:43:00 -0500238func (library *libraryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
239 if library.static() || library.MutatedProperties.VariantIsStaticStd {
240 return RlibLinkage
241 } else if library.baseCompiler.preferRlib() {
242 return RlibLinkage
243 }
244 return DefaultLinkage
245}
246
Ivan Lozanoffee3342019-08-27 12:03:00 -0700247var _ compiler = (*libraryDecorator)(nil)
Ivan Lozano52767be2019-10-18 14:49:46 -0700248var _ libraryInterface = (*libraryDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700249var _ exportedFlagsProducer = (*libraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700250
Matthew Maurer2ae05132020-06-23 14:28:53 -0700251// rust_library produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700252func RustLibraryFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700253 module, library := NewRustLibrary(android.HostAndDeviceSupported)
254 library.BuildOnlyRust()
255 return module.Init()
256}
257
258// rust_ffi produces all ffi variants.
259func RustFFIFactory() android.Module {
260 module, library := NewRustLibrary(android.HostAndDeviceSupported)
261 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700262 return module.Init()
263}
264
265// rust_library_dylib produces a dylib.
266func RustLibraryDylibFactory() android.Module {
267 module, library := NewRustLibrary(android.HostAndDeviceSupported)
268 library.BuildOnlyDylib()
269 return module.Init()
270}
271
272// rust_library_rlib produces an rlib.
273func RustLibraryRlibFactory() android.Module {
274 module, library := NewRustLibrary(android.HostAndDeviceSupported)
275 library.BuildOnlyRlib()
276 return module.Init()
277}
278
Matthew Maurer2ae05132020-06-23 14:28:53 -0700279// rust_ffi_shared produces a shared library.
280func RustFFISharedFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700281 module, library := NewRustLibrary(android.HostAndDeviceSupported)
282 library.BuildOnlyShared()
283 return module.Init()
284}
285
Matthew Maurer2ae05132020-06-23 14:28:53 -0700286// rust_ffi_static produces a static library.
287func RustFFIStaticFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700288 module, library := NewRustLibrary(android.HostAndDeviceSupported)
289 library.BuildOnlyStatic()
290 return module.Init()
291}
292
Matthew Maurer2ae05132020-06-23 14:28:53 -0700293// rust_library_host produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700294func RustLibraryHostFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700295 module, library := NewRustLibrary(android.HostSupported)
296 library.BuildOnlyRust()
297 return module.Init()
298}
299
300// rust_ffi_host produces all FFI variants.
301func RustFFIHostFactory() android.Module {
302 module, library := NewRustLibrary(android.HostSupported)
303 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700304 return module.Init()
305}
306
307// rust_library_dylib_host produces a dylib.
308func RustLibraryDylibHostFactory() android.Module {
309 module, library := NewRustLibrary(android.HostSupported)
310 library.BuildOnlyDylib()
311 return module.Init()
312}
313
314// rust_library_rlib_host produces an rlib.
315func RustLibraryRlibHostFactory() android.Module {
316 module, library := NewRustLibrary(android.HostSupported)
317 library.BuildOnlyRlib()
318 return module.Init()
319}
320
Matthew Maurer2ae05132020-06-23 14:28:53 -0700321// rust_ffi_static_host produces a static library.
322func RustFFIStaticHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700323 module, library := NewRustLibrary(android.HostSupported)
324 library.BuildOnlyStatic()
325 return module.Init()
326}
327
Matthew Maurer2ae05132020-06-23 14:28:53 -0700328// rust_ffi_shared_host produces an shared library.
329func RustFFISharedHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700330 module, library := NewRustLibrary(android.HostSupported)
331 library.BuildOnlyShared()
332 return module.Init()
333}
334
Matthew Maurer2ae05132020-06-23 14:28:53 -0700335func (library *libraryDecorator) BuildOnlyFFI() {
336 library.MutatedProperties.BuildDylib = false
337 library.MutatedProperties.BuildRlib = false
338 library.MutatedProperties.BuildShared = true
339 library.MutatedProperties.BuildStatic = true
340}
341
342func (library *libraryDecorator) BuildOnlyRust() {
343 library.MutatedProperties.BuildDylib = true
344 library.MutatedProperties.BuildRlib = true
345 library.MutatedProperties.BuildShared = false
346 library.MutatedProperties.BuildStatic = false
347}
348
Ivan Lozanoffee3342019-08-27 12:03:00 -0700349func (library *libraryDecorator) BuildOnlyDylib() {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700350 library.MutatedProperties.BuildDylib = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700351 library.MutatedProperties.BuildRlib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700352 library.MutatedProperties.BuildShared = false
353 library.MutatedProperties.BuildStatic = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700354}
355
356func (library *libraryDecorator) BuildOnlyRlib() {
357 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700358 library.MutatedProperties.BuildRlib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700359 library.MutatedProperties.BuildShared = false
360 library.MutatedProperties.BuildStatic = false
361}
362
363func (library *libraryDecorator) BuildOnlyStatic() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700364 library.MutatedProperties.BuildRlib = false
365 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700366 library.MutatedProperties.BuildShared = false
367 library.MutatedProperties.BuildStatic = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700368}
369
370func (library *libraryDecorator) BuildOnlyShared() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700371 library.MutatedProperties.BuildRlib = false
372 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700373 library.MutatedProperties.BuildStatic = false
374 library.MutatedProperties.BuildShared = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700375}
376
377func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400378 module := newModule(hod, android.MultilibBoth)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700379
380 library := &libraryDecorator{
381 MutatedProperties: LibraryMutatedProperties{
Matthew Maurer2ae05132020-06-23 14:28:53 -0700382 BuildDylib: false,
383 BuildRlib: false,
384 BuildShared: false,
385 BuildStatic: false,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700386 },
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800387 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Matthew Maurerbb3add12020-06-25 09:34:12 -0700388 flagExporter: NewFlagExporter(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700389 }
390
391 module.compiler = library
392
393 return module, library
394}
395
396func (library *libraryDecorator) compilerProps() []interface{} {
397 return append(library.baseCompiler.compilerProps(),
398 &library.Properties,
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200399 &library.MutatedProperties,
400 &library.stripper.StripProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700401}
402
Ivan Lozanof1c84332019-09-20 11:00:37 -0700403func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
404 deps = library.baseCompiler.compilerDeps(ctx, deps)
405
Ivan Lozano52767be2019-10-18 14:49:46 -0700406 if ctx.toolchain().Bionic() && (library.dylib() || library.shared()) {
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +0100407 deps = bionicDeps(ctx, deps, false)
Ivan Lozano12ee9ca2020-04-07 13:19:44 -0400408 deps.CrtBegin = "crtbegin_so"
409 deps.CrtEnd = "crtend_so"
Ivan Lozanof1c84332019-09-20 11:00:37 -0700410 }
411
412 return deps
413}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400414
415func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string {
416 return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
417}
418
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800419func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200420 flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800421 flags = library.baseCompiler.compilerFlags(ctx, flags)
422 if library.shared() || library.static() {
423 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
424 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400425 if library.shared() {
426 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
427 }
428
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800429 return flags
430}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700431
Ivan Lozanoffee3342019-08-27 12:03:00 -0700432func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200433 var outputFile android.ModuleOutPath
434 var fileName string
Dan Albert06feee92021-03-19 15:06:02 -0700435 srcPath := library.srcPath(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700436
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400437 if library.sourceProvider != nil {
Ivan Lozano9d74a522020-12-01 09:25:22 -0500438 deps.srcProviderFiles = append(deps.srcProviderFiles, library.sourceProvider.Srcs()...)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400439 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700440
441 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500442 flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...)
Ivan Lozano2093af22020-08-25 12:48:19 -0400443 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700444
Matthew Maurer46c46cc2020-01-13 16:34:34 -0800445 if library.dylib() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700446 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
447 // https://github.com/rust-lang/rust/issues/19680
448 // https://github.com/rust-lang/rust/issues/34909
449 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
450 }
451
Ivan Lozanoffee3342019-08-27 12:03:00 -0700452 if library.rlib() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200453 fileName = library.getStem(ctx) + ctx.toolchain().RlibSuffix()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700454 outputFile = android.PathForModuleOut(ctx, fileName)
455
Dan Albert06feee92021-03-19 15:06:02 -0700456 TransformSrctoRlib(ctx, srcPath, deps, flags, outputFile)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700457 } else if library.dylib() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200458 fileName = library.getStem(ctx) + ctx.toolchain().DylibSuffix()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700459 outputFile = android.PathForModuleOut(ctx, fileName)
460
Dan Albert06feee92021-03-19 15:06:02 -0700461 TransformSrctoDylib(ctx, srcPath, deps, flags, outputFile)
Ivan Lozano52767be2019-10-18 14:49:46 -0700462 } else if library.static() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200463 fileName = library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
Ivan Lozano52767be2019-10-18 14:49:46 -0700464 outputFile = android.PathForModuleOut(ctx, fileName)
465
Dan Albert06feee92021-03-19 15:06:02 -0700466 TransformSrctoStatic(ctx, srcPath, deps, flags, outputFile)
Ivan Lozano52767be2019-10-18 14:49:46 -0700467 } else if library.shared() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200468 fileName = library.sharedLibFilename(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700469 outputFile = android.PathForModuleOut(ctx, fileName)
470
Dan Albert06feee92021-03-19 15:06:02 -0700471 TransformSrctoShared(ctx, srcPath, deps, flags, outputFile)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700472 }
473
Thiébaud Weksteen4fab05a2021-04-14 19:15:48 +0200474 if !library.rlib() && !library.static() && library.stripper.NeedsStrip(ctx) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200475 strippedOutputFile := android.PathForModuleOut(ctx, "stripped", fileName)
476 library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
477 library.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
478 }
479
Ivan Lozano52767be2019-10-18 14:49:46 -0700480 if library.rlib() || library.dylib() {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700481 library.flagExporter.exportLinkDirs(deps.linkDirs...)
Colin Cross0de8a1e2020-09-18 14:15:30 -0700482 library.flagExporter.exportLinkObjects(deps.linkObjects...)
Ivan Lozano52767be2019-10-18 14:49:46 -0700483 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700484
Colin Cross0de8a1e2020-09-18 14:15:30 -0700485 if library.static() || library.shared() {
486 ctx.SetProvider(cc.FlagExporterInfoProvider, cc.FlagExporterInfo{
487 IncludeDirs: library.includeDirs,
488 })
489 }
490
491 if library.shared() {
492 ctx.SetProvider(cc.SharedLibraryInfoProvider, cc.SharedLibraryInfo{
493 SharedLibrary: outputFile,
494 UnstrippedSharedLibrary: outputFile,
495 })
496 }
497
498 if library.static() {
499 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(outputFile).Build()
500 ctx.SetProvider(cc.StaticLibraryInfoProvider, cc.StaticLibraryInfo{
501 StaticLibrary: outputFile,
502
503 TransitiveStaticLibrariesForOrdering: depSet,
504 })
505 }
506
507 library.flagExporter.setProvider(ctx)
508
Ivan Lozanoffee3342019-08-27 12:03:00 -0700509 return outputFile
510}
511
Dan Albert06feee92021-03-19 15:06:02 -0700512func (library *libraryDecorator) srcPath(ctx ModuleContext, deps PathDeps) android.Path {
513 if library.sourceProvider != nil {
514 // Assume the first source from the source provider is the library entry point.
515 return library.sourceProvider.Srcs()[0]
516 } else {
517 path, _ := srcPathFromModuleSrcs(ctx, library.baseCompiler.Properties.Srcs)
518 return path
519 }
520}
521
522func (library *libraryDecorator) rustdoc(ctx ModuleContext, flags Flags,
523 deps PathDeps) android.OptionalPath {
524 // rustdoc has builtin support for documenting config specific information
525 // regardless of the actual config it was given
526 // (https://doc.rust-lang.org/rustdoc/advanced-features.html#cfgdoc-documenting-platform-specific-or-feature-specific-information),
527 // so we generate the rustdoc for only the primary module so that we have a
528 // single set of docs to refer to.
529 if ctx.Module() != ctx.PrimaryModule() {
530 return android.OptionalPath{}
531 }
532
533 return android.OptionalPathForPath(Rustdoc(ctx, library.srcPath(ctx, deps),
534 deps, flags))
535}
536
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700537func (library *libraryDecorator) getStem(ctx ModuleContext) string {
538 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
539 validateLibraryStem(ctx, stem, library.crateName())
540
541 return stem + String(library.baseCompiler.Properties.Suffix)
542}
543
Ivan Lozano2b081132020-09-08 12:46:52 -0400544func (library *libraryDecorator) install(ctx ModuleContext) {
545 // Only shared and dylib variants make sense to install.
546 if library.shared() || library.dylib() {
547 library.baseCompiler.install(ctx)
548 }
549}
550
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400551func (library *libraryDecorator) Disabled() bool {
552 return library.MutatedProperties.VariantIsDisabled
553}
554
555func (library *libraryDecorator) SetDisabled() {
556 library.MutatedProperties.VariantIsDisabled = true
557}
558
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700559var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
560
561func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
562 if crate_name == "" {
563 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
564 }
565
566 // crate_names are used for the library output file, and rustc expects these
567 // to be alphanumeric with underscores allowed.
568 if validCrateName.MatchString(crate_name) {
569 ctx.PropertyErrorf("crate_name",
570 "library crate_names must be alphanumeric with underscores allowed")
571 }
572
573 // Libraries are expected to begin with "lib" followed by the crate_name
574 if !strings.HasPrefix(filename, "lib"+crate_name) {
575 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
576 }
577}
578
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200579// LibraryMutator mutates the libraries into variants according to the
580// build{Rlib,Dylib} attributes.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700581func LibraryMutator(mctx android.BottomUpMutatorContext) {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200582 // Only mutate on Rust libraries.
583 m, ok := mctx.Module().(*Module)
584 if !ok || m.compiler == nil {
585 return
586 }
587 library, ok := m.compiler.(libraryInterface)
588 if !ok {
589 return
590 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400591
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200592 var variants []string
593 // The source variant is used for SourceProvider modules. The other variants (i.e. rlib and dylib)
594 // depend on this variant. It must be the first variant to be declared.
595 sourceVariant := false
596 if m.sourceProvider != nil {
597 variants = append(variants, "source")
598 sourceVariant = true
599 }
600 if library.buildRlib() {
601 variants = append(variants, rlibVariation)
602 }
603 if library.buildDylib() {
604 variants = append(variants, dylibVariation)
605 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400606
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200607 if len(variants) == 0 {
608 return
609 }
610 modules := mctx.CreateLocalVariations(variants...)
611
612 // The order of the variations (modules) matches the variant names provided. Iterate
613 // through the new variation modules and set their mutated properties.
614 for i, v := range modules {
615 switch variants[i] {
616 case rlibVariation:
617 v.(*Module).compiler.(libraryInterface).setRlib()
618 case dylibVariation:
619 v.(*Module).compiler.(libraryInterface).setDylib()
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400620 if v.(*Module).ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
Ivan Lozano6a884432020-12-02 09:15:16 -0500621 // TODO(b/165791368)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400622 // Disable dylib Vendor Ramdisk variations until we support these.
Ivan Lozano6a884432020-12-02 09:15:16 -0500623 v.(*Module).Disable()
624 }
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200625 case "source":
626 v.(*Module).compiler.(libraryInterface).setSource()
627 // The source variant does not produce any library.
628 // Disable the compilation steps.
629 v.(*Module).compiler.SetDisabled()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700630 }
631 }
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200632
633 // If a source variant is created, add an inter-variant dependency
634 // between the other variants and the source variant.
635 if sourceVariant {
636 sv := modules[0]
637 for _, v := range modules[1:] {
638 if !v.Enabled() {
639 continue
640 }
641 mctx.AddInterVariantDependency(sourceDepTag, v, sv)
642 }
643 // Alias the source variation so it can be named directly in "srcs" properties.
644 mctx.AliasVariation("source")
645 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700646}
Ivan Lozano2b081132020-09-08 12:46:52 -0400647
648func LibstdMutator(mctx android.BottomUpMutatorContext) {
649 if m, ok := mctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() {
650 switch library := m.compiler.(type) {
651 case libraryInterface:
652 // Only create a variant if a library is actually being built.
653 if library.rlib() && !library.sysroot() {
654 variants := []string{"rlib-std", "dylib-std"}
655 modules := mctx.CreateLocalVariations(variants...)
656
657 rlib := modules[0].(*Module)
658 dylib := modules[1].(*Module)
659 rlib.compiler.(libraryInterface).setRlibStd()
660 dylib.compiler.(libraryInterface).setDylibStd()
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400661 if dylib.ModuleBase.ImageVariation().Variation == android.VendorRamdiskVariation {
Ivan Lozano6a884432020-12-02 09:15:16 -0500662 // TODO(b/165791368)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400663 // Disable rlibs that link against dylib-std on vendor ramdisk variations until those dylib
Ivan Lozano6a884432020-12-02 09:15:16 -0500664 // variants are properly supported.
665 dylib.Disable()
666 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400667 rlib.Properties.RustSubName += RlibStdlibSuffix
668 dylib.Properties.RustSubName += DylibStdlibSuffix
Ivan Lozano2b081132020-09-08 12:46:52 -0400669 }
670 }
671 }
672}