blob: 6766d618b044b4799536fd03debb84c37dd52a41 [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 (
Ivan Lozanoad8b18b2019-10-31 19:38:29 -070018 "regexp"
19 "strings"
20
Ivan Lozanoffee3342019-08-27 12:03:00 -070021 "android/soong/android"
22)
23
24func init() {
25 android.RegisterModuleType("rust_library", RustLibraryFactory)
26 android.RegisterModuleType("rust_library_dylib", RustLibraryDylibFactory)
27 android.RegisterModuleType("rust_library_rlib", RustLibraryRlibFactory)
28 android.RegisterModuleType("rust_library_host", RustLibraryHostFactory)
29 android.RegisterModuleType("rust_library_host_dylib", RustLibraryDylibHostFactory)
30 android.RegisterModuleType("rust_library_host_rlib", RustLibraryRlibHostFactory)
Matthew Maurer2ae05132020-06-23 14:28:53 -070031 android.RegisterModuleType("rust_ffi", RustFFIFactory)
32 android.RegisterModuleType("rust_ffi_shared", RustFFISharedFactory)
33 android.RegisterModuleType("rust_ffi_static", RustFFIStaticFactory)
34 android.RegisterModuleType("rust_ffi_host", RustFFIHostFactory)
35 android.RegisterModuleType("rust_ffi_host_shared", RustFFISharedHostFactory)
36 android.RegisterModuleType("rust_ffi_host_static", RustFFIStaticHostFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070037}
38
39type VariantLibraryProperties struct {
Matthew Maurerc761eec2020-06-25 00:47:46 -070040 Enabled *bool `android:"arch_variant"`
41 Srcs []string `android:"path,arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070042}
43
44type LibraryCompilerProperties struct {
Ivan Lozano52767be2019-10-18 14:49:46 -070045 Rlib VariantLibraryProperties `android:"arch_variant"`
46 Dylib VariantLibraryProperties `android:"arch_variant"`
47 Shared VariantLibraryProperties `android:"arch_variant"`
48 Static VariantLibraryProperties `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070049
Ivan Lozano52767be2019-10-18 14:49:46 -070050 // path to include directories to pass to cc_* modules, only relevant for static/shared variants.
51 Include_dirs []string `android:"path,arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070052}
53
54type LibraryMutatedProperties struct {
Ivan Lozanoffee3342019-08-27 12:03:00 -070055 // Build a dylib variant
56 BuildDylib bool `blueprint:"mutated"`
57 // Build an rlib variant
58 BuildRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070059 // Build a shared library variant
60 BuildShared bool `blueprint:"mutated"`
61 // Build a static library variant
62 BuildStatic bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070063
64 // This variant is a dylib
65 VariantIsDylib bool `blueprint:"mutated"`
66 // This variant is an rlib
67 VariantIsRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070068 // This variant is a shared library
69 VariantIsShared bool `blueprint:"mutated"`
70 // This variant is a static library
71 VariantIsStatic bool `blueprint:"mutated"`
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040072
73 // This variant is disabled and should not be compiled
74 // (used for SourceProvider variants that produce only source)
75 VariantIsDisabled bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070076}
77
78type libraryDecorator struct {
79 *baseCompiler
Matthew Maurerbb3add12020-06-25 09:34:12 -070080 *flagExporter
Ivan Lozanoffee3342019-08-27 12:03:00 -070081
Ivan Lozano8a23fa42020-06-16 10:26:57 -040082 Properties LibraryCompilerProperties
83 MutatedProperties LibraryMutatedProperties
84 includeDirs android.Paths
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040085 sourceProvider SourceProvider
Ivan Lozanoffee3342019-08-27 12:03:00 -070086}
87
88type libraryInterface interface {
89 rlib() bool
90 dylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -070091 static() bool
92 shared() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070093
94 // Returns true if the build options for the module have selected a particular build type
95 buildRlib() bool
96 buildDylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -070097 buildShared() bool
98 buildStatic() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070099
100 // Sets a particular variant type
101 setRlib()
102 setDylib()
Ivan Lozano52767be2019-10-18 14:49:46 -0700103 setShared()
104 setStatic()
105
106 // Build a specific library variant
Matthew Maurer2ae05132020-06-23 14:28:53 -0700107 BuildOnlyFFI()
108 BuildOnlyRust()
Ivan Lozano52767be2019-10-18 14:49:46 -0700109 BuildOnlyRlib()
110 BuildOnlyDylib()
111 BuildOnlyStatic()
112 BuildOnlyShared()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700113}
114
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400115func (library *libraryDecorator) nativeCoverage() bool {
116 return true
117}
118
Ivan Lozanoffee3342019-08-27 12:03:00 -0700119func (library *libraryDecorator) rlib() bool {
120 return library.MutatedProperties.VariantIsRlib
121}
122
123func (library *libraryDecorator) dylib() bool {
124 return library.MutatedProperties.VariantIsDylib
125}
126
Ivan Lozano52767be2019-10-18 14:49:46 -0700127func (library *libraryDecorator) shared() bool {
128 return library.MutatedProperties.VariantIsShared
129}
130
131func (library *libraryDecorator) static() bool {
132 return library.MutatedProperties.VariantIsStatic
133}
134
Ivan Lozanoffee3342019-08-27 12:03:00 -0700135func (library *libraryDecorator) buildRlib() bool {
136 return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true)
137}
138
139func (library *libraryDecorator) buildDylib() bool {
140 return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true)
141}
142
Ivan Lozano52767be2019-10-18 14:49:46 -0700143func (library *libraryDecorator) buildShared() bool {
144 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
145}
146
147func (library *libraryDecorator) buildStatic() bool {
148 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
149}
150
Ivan Lozanoffee3342019-08-27 12:03:00 -0700151func (library *libraryDecorator) setRlib() {
152 library.MutatedProperties.VariantIsRlib = true
153 library.MutatedProperties.VariantIsDylib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700154 library.MutatedProperties.VariantIsStatic = false
155 library.MutatedProperties.VariantIsShared = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700156}
157
158func (library *libraryDecorator) setDylib() {
159 library.MutatedProperties.VariantIsRlib = false
160 library.MutatedProperties.VariantIsDylib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700161 library.MutatedProperties.VariantIsStatic = false
162 library.MutatedProperties.VariantIsShared = false
163}
164
165func (library *libraryDecorator) setShared() {
166 library.MutatedProperties.VariantIsStatic = false
167 library.MutatedProperties.VariantIsShared = true
168 library.MutatedProperties.VariantIsRlib = false
169 library.MutatedProperties.VariantIsDylib = false
170}
171
172func (library *libraryDecorator) setStatic() {
173 library.MutatedProperties.VariantIsStatic = true
174 library.MutatedProperties.VariantIsShared = false
175 library.MutatedProperties.VariantIsRlib = false
176 library.MutatedProperties.VariantIsDylib = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700177}
178
Matthew Maurer0f003b12020-06-29 14:34:06 -0700179func (library *libraryDecorator) autoDep() autoDep {
180 if library.rlib() || library.static() {
181 return rlibAutoDep
182 } else if library.dylib() || library.shared() {
183 return dylibAutoDep
184 } else {
185 return rlibAutoDep
186 }
187}
188
Ivan Lozanoffee3342019-08-27 12:03:00 -0700189var _ compiler = (*libraryDecorator)(nil)
Ivan Lozano52767be2019-10-18 14:49:46 -0700190var _ libraryInterface = (*libraryDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700191var _ exportedFlagsProducer = (*libraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700192
Matthew Maurer2ae05132020-06-23 14:28:53 -0700193// rust_library produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700194func RustLibraryFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700195 module, library := NewRustLibrary(android.HostAndDeviceSupported)
196 library.BuildOnlyRust()
197 return module.Init()
198}
199
200// rust_ffi produces all ffi variants.
201func RustFFIFactory() android.Module {
202 module, library := NewRustLibrary(android.HostAndDeviceSupported)
203 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700204 return module.Init()
205}
206
207// rust_library_dylib produces a dylib.
208func RustLibraryDylibFactory() android.Module {
209 module, library := NewRustLibrary(android.HostAndDeviceSupported)
210 library.BuildOnlyDylib()
211 return module.Init()
212}
213
214// rust_library_rlib produces an rlib.
215func RustLibraryRlibFactory() android.Module {
216 module, library := NewRustLibrary(android.HostAndDeviceSupported)
217 library.BuildOnlyRlib()
218 return module.Init()
219}
220
Matthew Maurer2ae05132020-06-23 14:28:53 -0700221// rust_ffi_shared produces a shared library.
222func RustFFISharedFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700223 module, library := NewRustLibrary(android.HostAndDeviceSupported)
224 library.BuildOnlyShared()
225 return module.Init()
226}
227
Matthew Maurer2ae05132020-06-23 14:28:53 -0700228// rust_ffi_static produces a static library.
229func RustFFIStaticFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700230 module, library := NewRustLibrary(android.HostAndDeviceSupported)
231 library.BuildOnlyStatic()
232 return module.Init()
233}
234
Matthew Maurer2ae05132020-06-23 14:28:53 -0700235// rust_library_host produces all rust variants.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700236func RustLibraryHostFactory() android.Module {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700237 module, library := NewRustLibrary(android.HostSupported)
238 library.BuildOnlyRust()
239 return module.Init()
240}
241
242// rust_ffi_host produces all FFI variants.
243func RustFFIHostFactory() android.Module {
244 module, library := NewRustLibrary(android.HostSupported)
245 library.BuildOnlyFFI()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700246 return module.Init()
247}
248
249// rust_library_dylib_host produces a dylib.
250func RustLibraryDylibHostFactory() android.Module {
251 module, library := NewRustLibrary(android.HostSupported)
252 library.BuildOnlyDylib()
253 return module.Init()
254}
255
256// rust_library_rlib_host produces an rlib.
257func RustLibraryRlibHostFactory() android.Module {
258 module, library := NewRustLibrary(android.HostSupported)
259 library.BuildOnlyRlib()
260 return module.Init()
261}
262
Matthew Maurer2ae05132020-06-23 14:28:53 -0700263// rust_ffi_static_host produces a static library.
264func RustFFIStaticHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700265 module, library := NewRustLibrary(android.HostSupported)
266 library.BuildOnlyStatic()
267 return module.Init()
268}
269
Matthew Maurer2ae05132020-06-23 14:28:53 -0700270// rust_ffi_shared_host produces an shared library.
271func RustFFISharedHostFactory() android.Module {
Ivan Lozano52767be2019-10-18 14:49:46 -0700272 module, library := NewRustLibrary(android.HostSupported)
273 library.BuildOnlyShared()
274 return module.Init()
275}
276
Matthew Maurer2ae05132020-06-23 14:28:53 -0700277func (library *libraryDecorator) BuildOnlyFFI() {
278 library.MutatedProperties.BuildDylib = false
279 library.MutatedProperties.BuildRlib = false
280 library.MutatedProperties.BuildShared = true
281 library.MutatedProperties.BuildStatic = true
282}
283
284func (library *libraryDecorator) BuildOnlyRust() {
285 library.MutatedProperties.BuildDylib = true
286 library.MutatedProperties.BuildRlib = true
287 library.MutatedProperties.BuildShared = false
288 library.MutatedProperties.BuildStatic = false
289}
290
Ivan Lozanoffee3342019-08-27 12:03:00 -0700291func (library *libraryDecorator) BuildOnlyDylib() {
Matthew Maurer2ae05132020-06-23 14:28:53 -0700292 library.MutatedProperties.BuildDylib = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700293 library.MutatedProperties.BuildRlib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700294 library.MutatedProperties.BuildShared = false
295 library.MutatedProperties.BuildStatic = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700296}
297
298func (library *libraryDecorator) BuildOnlyRlib() {
299 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700300 library.MutatedProperties.BuildRlib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700301 library.MutatedProperties.BuildShared = false
302 library.MutatedProperties.BuildStatic = false
303}
304
305func (library *libraryDecorator) BuildOnlyStatic() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700306 library.MutatedProperties.BuildRlib = false
307 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700308 library.MutatedProperties.BuildShared = false
309 library.MutatedProperties.BuildStatic = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700310}
311
312func (library *libraryDecorator) BuildOnlyShared() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700313 library.MutatedProperties.BuildRlib = false
314 library.MutatedProperties.BuildDylib = false
Matthew Maurer2ae05132020-06-23 14:28:53 -0700315 library.MutatedProperties.BuildStatic = false
316 library.MutatedProperties.BuildShared = true
Ivan Lozanoffee3342019-08-27 12:03:00 -0700317}
318
319func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400320 module := newModule(hod, android.MultilibBoth)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700321
322 library := &libraryDecorator{
323 MutatedProperties: LibraryMutatedProperties{
Matthew Maurer2ae05132020-06-23 14:28:53 -0700324 BuildDylib: false,
325 BuildRlib: false,
326 BuildShared: false,
327 BuildStatic: false,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700328 },
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800329 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Matthew Maurerbb3add12020-06-25 09:34:12 -0700330 flagExporter: NewFlagExporter(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700331 }
332
333 module.compiler = library
334
335 return module, library
336}
337
338func (library *libraryDecorator) compilerProps() []interface{} {
339 return append(library.baseCompiler.compilerProps(),
340 &library.Properties,
341 &library.MutatedProperties)
342}
343
Ivan Lozanof1c84332019-09-20 11:00:37 -0700344func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
345 deps = library.baseCompiler.compilerDeps(ctx, deps)
346
Ivan Lozano52767be2019-10-18 14:49:46 -0700347 if ctx.toolchain().Bionic() && (library.dylib() || library.shared()) {
Ivan Lozano45901ed2020-07-24 16:05:01 -0400348 deps = bionicDeps(deps)
Ivan Lozano12ee9ca2020-04-07 13:19:44 -0400349 deps.CrtBegin = "crtbegin_so"
350 deps.CrtEnd = "crtend_so"
Ivan Lozanof1c84332019-09-20 11:00:37 -0700351 }
352
353 return deps
354}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400355
356func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string {
357 return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
358}
359
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800360func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
ThiƩbaud Weksteen1f7f70f2020-06-24 11:32:48 +0200361 flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800362 flags = library.baseCompiler.compilerFlags(ctx, flags)
363 if library.shared() || library.static() {
364 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
365 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400366 if library.shared() {
367 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
368 }
369
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800370 return flags
371}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700372
Ivan Lozanoffee3342019-08-27 12:03:00 -0700373func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
374 var outputFile android.WritablePath
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400375 var srcPath android.Path
Ivan Lozanoffee3342019-08-27 12:03:00 -0700376
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400377 if library.sourceProvider != nil {
378 srcPath = library.sourceProvider.Srcs()[0]
379 } else {
380 srcPath, _ = srcPathFromModuleSrcs(ctx, library.baseCompiler.Properties.Srcs)
381 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700382
383 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
384
Matthew Maurer46c46cc2020-01-13 16:34:34 -0800385 if library.dylib() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700386 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
387 // https://github.com/rust-lang/rust/issues/19680
388 // https://github.com/rust-lang/rust/issues/34909
389 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
390 }
391
Ivan Lozanoffee3342019-08-27 12:03:00 -0700392 if library.rlib() {
393 fileName := library.getStem(ctx) + ctx.toolchain().RlibSuffix()
394 outputFile = android.PathForModuleOut(ctx, fileName)
395
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400396 outputs := TransformSrctoRlib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
397 library.coverageFile = outputs.coverageFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700398 } else if library.dylib() {
399 fileName := library.getStem(ctx) + ctx.toolchain().DylibSuffix()
400 outputFile = android.PathForModuleOut(ctx, fileName)
401
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400402 outputs := TransformSrctoDylib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
403 library.coverageFile = outputs.coverageFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700404 } else if library.static() {
405 fileName := library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
406 outputFile = android.PathForModuleOut(ctx, fileName)
407
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400408 outputs := TransformSrctoStatic(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
409 library.coverageFile = outputs.coverageFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700410 } else if library.shared() {
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400411 fileName := library.sharedLibFilename(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700412 outputFile = android.PathForModuleOut(ctx, fileName)
413
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400414 outputs := TransformSrctoShared(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
415 library.coverageFile = outputs.coverageFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700416 }
417
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400418 var coverageFiles android.Paths
419 if library.coverageFile != nil {
420 coverageFiles = append(coverageFiles, library.coverageFile)
421 }
422 if len(deps.coverageFiles) > 0 {
423 coverageFiles = append(coverageFiles, deps.coverageFiles...)
424 }
425 library.coverageOutputZipFile = TransformCoverageFilesToZip(ctx, coverageFiles, library.getStem(ctx))
426
Ivan Lozano52767be2019-10-18 14:49:46 -0700427 if library.rlib() || library.dylib() {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700428 library.exportLinkDirs(deps.linkDirs...)
429 library.exportDepFlags(deps.depFlags...)
Ivan Lozano52767be2019-10-18 14:49:46 -0700430 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700431 library.unstrippedOutputFile = outputFile
432
433 return outputFile
434}
435
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700436func (library *libraryDecorator) getStem(ctx ModuleContext) string {
437 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
438 validateLibraryStem(ctx, stem, library.crateName())
439
440 return stem + String(library.baseCompiler.Properties.Suffix)
441}
442
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400443func (library *libraryDecorator) Disabled() bool {
444 return library.MutatedProperties.VariantIsDisabled
445}
446
447func (library *libraryDecorator) SetDisabled() {
448 library.MutatedProperties.VariantIsDisabled = true
449}
450
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700451var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
452
453func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
454 if crate_name == "" {
455 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
456 }
457
458 // crate_names are used for the library output file, and rustc expects these
459 // to be alphanumeric with underscores allowed.
460 if validCrateName.MatchString(crate_name) {
461 ctx.PropertyErrorf("crate_name",
462 "library crate_names must be alphanumeric with underscores allowed")
463 }
464
465 // Libraries are expected to begin with "lib" followed by the crate_name
466 if !strings.HasPrefix(filename, "lib"+crate_name) {
467 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
468 }
469}
470
Ivan Lozanoffee3342019-08-27 12:03:00 -0700471func LibraryMutator(mctx android.BottomUpMutatorContext) {
472 if m, ok := mctx.Module().(*Module); ok && m.compiler != nil {
473 switch library := m.compiler.(type) {
474 case libraryInterface:
Ivan Lozano89435d12020-07-31 11:01:18 -0400475 if library.buildRlib() && library.buildDylib() {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400476 variants := []string{"rlib", "dylib"}
477 if m.sourceProvider != nil {
478 variants = append(variants, "")
479 }
480 modules := mctx.CreateLocalVariations(variants...)
481
Ivan Lozano89435d12020-07-31 11:01:18 -0400482 rlib := modules[0].(*Module)
483 dylib := modules[1].(*Module)
Ivan Lozano89435d12020-07-31 11:01:18 -0400484 rlib.compiler.(libraryInterface).setRlib()
485 dylib.compiler.(libraryInterface).setDylib()
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400486
487 if m.sourceProvider != nil {
488 // This library is SourceProvider generated, so the non-library-producing
489 // variant needs to disable it's compiler and skip installation.
490 sourceProvider := modules[2].(*Module)
491 sourceProvider.compiler.SetDisabled()
492 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400493 } else if library.buildRlib() {
494 modules := mctx.CreateLocalVariations("rlib")
495 modules[0].(*Module).compiler.(libraryInterface).setRlib()
496 } else if library.buildDylib() {
497 modules := mctx.CreateLocalVariations("dylib")
498 modules[0].(*Module).compiler.(libraryInterface).setDylib()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700499 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400500
501 if m.sourceProvider != nil {
502 // Alias the non-library variant to the empty-string variant.
503 mctx.AliasVariation("")
504 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700505 }
506 }
507}