blob: 22d9f7e110b2007c26df205d8384c46e2404050b [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
Ivan Lozano042504f2020-08-18 14:31:23 -0400222func (library *libraryDecorator) autoDep(ctx BaseModuleContext) 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
229 } else {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200230 panic(fmt.Errorf("autoDep called on library %q that has no enabled variants.", ctx.ModuleName()))
Matthew Maurer0f003b12020-06-29 14:34:06 -0700231 }
232}
233
Ivan Lozanoea086132020-12-08 14:43:00 -0500234func (library *libraryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
235 if library.static() || library.MutatedProperties.VariantIsStaticStd {
236 return RlibLinkage
237 } else if library.baseCompiler.preferRlib() {
238 return RlibLinkage
239 }
240 return DefaultLinkage
241}
242
Ivan Lozanoffee3342019-08-27 12:03:00 -0700243var _ compiler = (*libraryDecorator)(nil)
Ivan Lozano52767be2019-10-18 14:49:46 -0700244var _ libraryInterface = (*libraryDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700245var _ exportedFlagsProducer = (*libraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700246
Matthew Maurer2ae05132020-06-23 14:28:53 -0700247// rust_library produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700248func RustLibraryFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700249 module, library := NewRustLibrary(android.HostAndDeviceSupported)
250 library.BuildOnlyRust()
251 return module.Init()
252}
253
254// rust_ffi produces all ffi variants.
255func RustFFIFactory() android.Module {
256 module, library := NewRustLibrary(android.HostAndDeviceSupported)
257 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700258 return module.Init()
259}
260
261// rust_library_dylib produces a dylib.
262func RustLibraryDylibFactory() android.Module {
263 module, library := NewRustLibrary(android.HostAndDeviceSupported)
264 library.BuildOnlyDylib()
265 return module.Init()
266}
267
268// rust_library_rlib produces an rlib.
269func RustLibraryRlibFactory() android.Module {
270 module, library := NewRustLibrary(android.HostAndDeviceSupported)
271 library.BuildOnlyRlib()
272 return module.Init()
273}
274
Matthew Maurer2ae05132020-06-23 14:28:53 -0700275// rust_ffi_shared produces a shared library.
276func RustFFISharedFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700277 module, library := NewRustLibrary(android.HostAndDeviceSupported)
278 library.BuildOnlyShared()
279 return module.Init()
280}
281
Matthew Maurer2ae05132020-06-23 14:28:53 -0700282// rust_ffi_static produces a static library.
283func RustFFIStaticFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700284 module, library := NewRustLibrary(android.HostAndDeviceSupported)
285 library.BuildOnlyStatic()
286 return module.Init()
287}
288
Matthew Maurer2ae05132020-06-23 14:28:53 -0700289// rust_library_host produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700290func RustLibraryHostFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700291 module, library := NewRustLibrary(android.HostSupported)
292 library.BuildOnlyRust()
293 return module.Init()
294}
295
296// rust_ffi_host produces all FFI variants.
297func RustFFIHostFactory() android.Module {
298 module, library := NewRustLibrary(android.HostSupported)
299 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700300 return module.Init()
301}
302
303// rust_library_dylib_host produces a dylib.
304func RustLibraryDylibHostFactory() android.Module {
305 module, library := NewRustLibrary(android.HostSupported)
306 library.BuildOnlyDylib()
307 return module.Init()
308}
309
310// rust_library_rlib_host produces an rlib.
311func RustLibraryRlibHostFactory() android.Module {
312 module, library := NewRustLibrary(android.HostSupported)
313 library.BuildOnlyRlib()
314 return module.Init()
315}
316
Matthew Maurer2ae05132020-06-23 14:28:53 -0700317// rust_ffi_static_host produces a static library.
318func RustFFIStaticHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700319 module, library := NewRustLibrary(android.HostSupported)
320 library.BuildOnlyStatic()
321 return module.Init()
322}
323
Matthew Maurer2ae05132020-06-23 14:28:53 -0700324// rust_ffi_shared_host produces an shared library.
325func RustFFISharedHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700326 module, library := NewRustLibrary(android.HostSupported)
327 library.BuildOnlyShared()
328 return module.Init()
329}
330
Matthew Maurer2ae05132020-06-23 14:28:53 -0700331func (library *libraryDecorator) BuildOnlyFFI() {
332 library.MutatedProperties.BuildDylib = false
333 library.MutatedProperties.BuildRlib = false
334 library.MutatedProperties.BuildShared = true
335 library.MutatedProperties.BuildStatic = true
336}
337
338func (library *libraryDecorator) BuildOnlyRust() {
339 library.MutatedProperties.BuildDylib = true
340 library.MutatedProperties.BuildRlib = true
341 library.MutatedProperties.BuildShared = false
342 library.MutatedProperties.BuildStatic = false
343}
344
Ivan Lozanoffee3342019-08-27 12:03:00 -0700345func (library *libraryDecorator) BuildOnlyDylib() {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700346 library.MutatedProperties.BuildDylib = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700347 library.MutatedProperties.BuildRlib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700348 library.MutatedProperties.BuildShared = false
349 library.MutatedProperties.BuildStatic = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700350}
351
352func (library *libraryDecorator) BuildOnlyRlib() {
353 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700354 library.MutatedProperties.BuildRlib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700355 library.MutatedProperties.BuildShared = false
356 library.MutatedProperties.BuildStatic = false
357}
358
359func (library *libraryDecorator) BuildOnlyStatic() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700360 library.MutatedProperties.BuildRlib = false
361 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700362 library.MutatedProperties.BuildShared = false
363 library.MutatedProperties.BuildStatic = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700364}
365
366func (library *libraryDecorator) BuildOnlyShared() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700367 library.MutatedProperties.BuildRlib = false
368 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700369 library.MutatedProperties.BuildStatic = false
370 library.MutatedProperties.BuildShared = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700371}
372
373func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400374 module := newModule(hod, android.MultilibBoth)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700375
376 library := &libraryDecorator{
377 MutatedProperties: LibraryMutatedProperties{
Matthew Maurer2ae05132020-06-23 14:28:53 -0700378 BuildDylib: false,
379 BuildRlib: false,
380 BuildShared: false,
381 BuildStatic: false,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700382 },
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800383 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Matthew Maurerbb3add12020-06-25 09:34:12 -0700384 flagExporter: NewFlagExporter(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700385 }
386
387 module.compiler = library
388
389 return module, library
390}
391
392func (library *libraryDecorator) compilerProps() []interface{} {
393 return append(library.baseCompiler.compilerProps(),
394 &library.Properties,
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200395 &library.MutatedProperties,
396 &library.stripper.StripProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700397}
398
Ivan Lozanof1c84332019-09-20 11:00:37 -0700399func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
400 deps = library.baseCompiler.compilerDeps(ctx, deps)
401
Ivan Lozano52767be2019-10-18 14:49:46 -0700402 if ctx.toolchain().Bionic() && (library.dylib() || library.shared()) {
Ivan Lozanobf63d002020-10-02 10:03:23 -0400403 deps = bionicDeps(deps, false)
Ivan Lozano12ee9ca2020-04-07 13:19:44 -0400404 deps.CrtBegin = "crtbegin_so"
405 deps.CrtEnd = "crtend_so"
Ivan Lozanof1c84332019-09-20 11:00:37 -0700406 }
407
408 return deps
409}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400410
411func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string {
412 return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
413}
414
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800415func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200416 flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800417 flags = library.baseCompiler.compilerFlags(ctx, flags)
418 if library.shared() || library.static() {
419 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
420 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400421 if library.shared() {
422 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
423 }
424
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800425 return flags
426}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700427
Ivan Lozanoffee3342019-08-27 12:03:00 -0700428func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200429 var outputFile android.ModuleOutPath
430 var fileName string
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400431 var srcPath android.Path
Ivan Lozanoffee3342019-08-27 12:03:00 -0700432
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400433 if library.sourceProvider != nil {
Ivan Lozano57f434e2020-10-28 09:32:10 -0400434 // Assume the first source from the source provider is the library entry point.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400435 srcPath = library.sourceProvider.Srcs()[0]
Ivan Lozano9d74a522020-12-01 09:25:22 -0500436 deps.srcProviderFiles = append(deps.srcProviderFiles, library.sourceProvider.Srcs()...)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400437 } else {
438 srcPath, _ = srcPathFromModuleSrcs(ctx, library.baseCompiler.Properties.Srcs)
439 }
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
Joel Galensonfa049382021-01-14 16:03:18 -0800456 TransformSrctoRlib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
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
Joel Galensonfa049382021-01-14 16:03:18 -0800461 TransformSrctoDylib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
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
Joel Galensonfa049382021-01-14 16:03:18 -0800466 TransformSrctoStatic(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
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
Joel Galensonfa049382021-01-14 16:03:18 -0800471 TransformSrctoShared(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700472 }
473
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200474 if !library.rlib() && library.stripper.NeedsStrip(ctx) {
475 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
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700512func (library *libraryDecorator) getStem(ctx ModuleContext) string {
513 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
514 validateLibraryStem(ctx, stem, library.crateName())
515
516 return stem + String(library.baseCompiler.Properties.Suffix)
517}
518
Ivan Lozano2b081132020-09-08 12:46:52 -0400519func (library *libraryDecorator) install(ctx ModuleContext) {
520 // Only shared and dylib variants make sense to install.
521 if library.shared() || library.dylib() {
522 library.baseCompiler.install(ctx)
523 }
524}
525
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400526func (library *libraryDecorator) Disabled() bool {
527 return library.MutatedProperties.VariantIsDisabled
528}
529
530func (library *libraryDecorator) SetDisabled() {
531 library.MutatedProperties.VariantIsDisabled = true
532}
533
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700534var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
535
536func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
537 if crate_name == "" {
538 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
539 }
540
541 // crate_names are used for the library output file, and rustc expects these
542 // to be alphanumeric with underscores allowed.
543 if validCrateName.MatchString(crate_name) {
544 ctx.PropertyErrorf("crate_name",
545 "library crate_names must be alphanumeric with underscores allowed")
546 }
547
548 // Libraries are expected to begin with "lib" followed by the crate_name
549 if !strings.HasPrefix(filename, "lib"+crate_name) {
550 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
551 }
552}
553
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200554// LibraryMutator mutates the libraries into variants according to the
555// build{Rlib,Dylib} attributes.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700556func LibraryMutator(mctx android.BottomUpMutatorContext) {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200557 // Only mutate on Rust libraries.
558 m, ok := mctx.Module().(*Module)
559 if !ok || m.compiler == nil {
560 return
561 }
562 library, ok := m.compiler.(libraryInterface)
563 if !ok {
564 return
565 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400566
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200567 var variants []string
568 // The source variant is used for SourceProvider modules. The other variants (i.e. rlib and dylib)
569 // depend on this variant. It must be the first variant to be declared.
570 sourceVariant := false
571 if m.sourceProvider != nil {
572 variants = append(variants, "source")
573 sourceVariant = true
574 }
575 if library.buildRlib() {
576 variants = append(variants, rlibVariation)
577 }
578 if library.buildDylib() {
579 variants = append(variants, dylibVariation)
580 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400581
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200582 if len(variants) == 0 {
583 return
584 }
585 modules := mctx.CreateLocalVariations(variants...)
586
587 // The order of the variations (modules) matches the variant names provided. Iterate
588 // through the new variation modules and set their mutated properties.
589 for i, v := range modules {
590 switch variants[i] {
591 case rlibVariation:
592 v.(*Module).compiler.(libraryInterface).setRlib()
593 case dylibVariation:
594 v.(*Module).compiler.(libraryInterface).setDylib()
Ivan Lozano6a884432020-12-02 09:15:16 -0500595 if v.(*Module).ModuleBase.ImageVariation().Variation != android.CoreVariation {
596 // TODO(b/165791368)
597 // Disable dylib non-core variations until we support these.
598 v.(*Module).Disable()
599 }
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200600 case "source":
601 v.(*Module).compiler.(libraryInterface).setSource()
602 // The source variant does not produce any library.
603 // Disable the compilation steps.
604 v.(*Module).compiler.SetDisabled()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700605 }
606 }
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200607
608 // If a source variant is created, add an inter-variant dependency
609 // between the other variants and the source variant.
610 if sourceVariant {
611 sv := modules[0]
612 for _, v := range modules[1:] {
613 if !v.Enabled() {
614 continue
615 }
616 mctx.AddInterVariantDependency(sourceDepTag, v, sv)
617 }
618 // Alias the source variation so it can be named directly in "srcs" properties.
619 mctx.AliasVariation("source")
620 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700621}
Ivan Lozano2b081132020-09-08 12:46:52 -0400622
623func LibstdMutator(mctx android.BottomUpMutatorContext) {
624 if m, ok := mctx.Module().(*Module); ok && m.compiler != nil && !m.compiler.Disabled() {
625 switch library := m.compiler.(type) {
626 case libraryInterface:
627 // Only create a variant if a library is actually being built.
628 if library.rlib() && !library.sysroot() {
629 variants := []string{"rlib-std", "dylib-std"}
630 modules := mctx.CreateLocalVariations(variants...)
631
632 rlib := modules[0].(*Module)
633 dylib := modules[1].(*Module)
634 rlib.compiler.(libraryInterface).setRlibStd()
635 dylib.compiler.(libraryInterface).setDylibStd()
Ivan Lozano6a884432020-12-02 09:15:16 -0500636 if dylib.ModuleBase.ImageVariation().Variation != android.CoreVariation {
637 // TODO(b/165791368)
638 // Disable rlibs that link against dylib-std on non-core variations until non-core dylib
639 // variants are properly supported.
640 dylib.Disable()
641 }
Ivan Lozano2b081132020-09-08 12:46:52 -0400642 rlib.Properties.SubName += RlibStdlibSuffix
643 dylib.Properties.SubName += DylibStdlibSuffix
644 }
645 }
646 }
647}