blob: 8aa033c0f7e2d175a6ec0b425c085929eb9adc68 [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"
Ivan Lozano88f2b1c2019-11-21 14:35:20 -080022 "android/soong/rust/config"
Ivan Lozanoffee3342019-08-27 12:03:00 -070023)
24
25func init() {
26 android.RegisterModuleType("rust_library", RustLibraryFactory)
27 android.RegisterModuleType("rust_library_dylib", RustLibraryDylibFactory)
28 android.RegisterModuleType("rust_library_rlib", RustLibraryRlibFactory)
29 android.RegisterModuleType("rust_library_host", RustLibraryHostFactory)
30 android.RegisterModuleType("rust_library_host_dylib", RustLibraryDylibHostFactory)
31 android.RegisterModuleType("rust_library_host_rlib", RustLibraryRlibHostFactory)
Ivan Lozano52767be2019-10-18 14:49:46 -070032 android.RegisterModuleType("rust_library_shared", RustLibrarySharedFactory)
33 android.RegisterModuleType("rust_library_static", RustLibraryStaticFactory)
34 android.RegisterModuleType("rust_library_host_shared", RustLibrarySharedHostFactory)
35 android.RegisterModuleType("rust_library_host_static", RustLibraryStaticHostFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070036}
37
38type VariantLibraryProperties struct {
39 Enabled *bool `android:"arch_variant"`
40}
41
42type LibraryCompilerProperties struct {
Ivan Lozano52767be2019-10-18 14:49:46 -070043 Rlib VariantLibraryProperties `android:"arch_variant"`
44 Dylib VariantLibraryProperties `android:"arch_variant"`
45 Shared VariantLibraryProperties `android:"arch_variant"`
46 Static VariantLibraryProperties `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070047
48 // path to the source file that is the main entry point of the program (e.g. src/lib.rs)
49 Srcs []string `android:"path,arch_variant"`
Ivan Lozano52767be2019-10-18 14:49:46 -070050
51 // path to include directories to pass to cc_* modules, only relevant for static/shared variants.
52 Include_dirs []string `android:"path,arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070053}
54
55type LibraryMutatedProperties struct {
Ivan Lozanoffee3342019-08-27 12:03:00 -070056 // Build a dylib variant
57 BuildDylib bool `blueprint:"mutated"`
58 // Build an rlib variant
59 BuildRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070060 // Build a shared library variant
61 BuildShared bool `blueprint:"mutated"`
62 // Build a static library variant
63 BuildStatic bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070064
65 // This variant is a dylib
66 VariantIsDylib bool `blueprint:"mutated"`
67 // This variant is an rlib
68 VariantIsRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070069 // This variant is a shared library
70 VariantIsShared bool `blueprint:"mutated"`
71 // This variant is a static library
72 VariantIsStatic bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070073}
74
75type libraryDecorator struct {
76 *baseCompiler
77
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040078 Properties LibraryCompilerProperties
79 MutatedProperties LibraryMutatedProperties
80 distFile android.OptionalPath
81 coverageOutputZipFile android.OptionalPath
82 unstrippedOutputFile android.Path
83 includeDirs android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -070084}
85
86type libraryInterface interface {
87 rlib() bool
88 dylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -070089 static() bool
90 shared() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070091
92 // Returns true if the build options for the module have selected a particular build type
93 buildRlib() bool
94 buildDylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -070095 buildShared() bool
96 buildStatic() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070097
98 // Sets a particular variant type
99 setRlib()
100 setDylib()
Ivan Lozano52767be2019-10-18 14:49:46 -0700101 setShared()
102 setStatic()
103
104 // Build a specific library variant
105 BuildOnlyRlib()
106 BuildOnlyDylib()
107 BuildOnlyStatic()
108 BuildOnlyShared()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700109}
110
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400111func (library *libraryDecorator) nativeCoverage() bool {
112 return true
113}
114
Ivan Lozanoffee3342019-08-27 12:03:00 -0700115func (library *libraryDecorator) exportedDirs() []string {
116 return library.linkDirs
117}
118
119func (library *libraryDecorator) exportedDepFlags() []string {
120 return library.depFlags
121}
122
123func (library *libraryDecorator) reexportDirs(dirs ...string) {
124 library.linkDirs = android.FirstUniqueStrings(append(library.linkDirs, dirs...))
125}
126
127func (library *libraryDecorator) reexportDepFlags(flags ...string) {
128 library.depFlags = android.FirstUniqueStrings(append(library.depFlags, flags...))
129}
130
131func (library *libraryDecorator) rlib() bool {
132 return library.MutatedProperties.VariantIsRlib
133}
134
135func (library *libraryDecorator) dylib() bool {
136 return library.MutatedProperties.VariantIsDylib
137}
138
Ivan Lozano52767be2019-10-18 14:49:46 -0700139func (library *libraryDecorator) shared() bool {
140 return library.MutatedProperties.VariantIsShared
141}
142
143func (library *libraryDecorator) static() bool {
144 return library.MutatedProperties.VariantIsStatic
145}
146
Ivan Lozanoffee3342019-08-27 12:03:00 -0700147func (library *libraryDecorator) buildRlib() bool {
148 return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true)
149}
150
151func (library *libraryDecorator) buildDylib() bool {
152 return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true)
153}
154
Ivan Lozano52767be2019-10-18 14:49:46 -0700155func (library *libraryDecorator) buildShared() bool {
156 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
157}
158
159func (library *libraryDecorator) buildStatic() bool {
160 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
161}
162
Ivan Lozanoffee3342019-08-27 12:03:00 -0700163func (library *libraryDecorator) setRlib() {
164 library.MutatedProperties.VariantIsRlib = true
165 library.MutatedProperties.VariantIsDylib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700166 library.MutatedProperties.VariantIsStatic = false
167 library.MutatedProperties.VariantIsShared = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700168}
169
170func (library *libraryDecorator) setDylib() {
171 library.MutatedProperties.VariantIsRlib = false
172 library.MutatedProperties.VariantIsDylib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700173 library.MutatedProperties.VariantIsStatic = false
174 library.MutatedProperties.VariantIsShared = false
175}
176
177func (library *libraryDecorator) setShared() {
178 library.MutatedProperties.VariantIsStatic = false
179 library.MutatedProperties.VariantIsShared = true
180 library.MutatedProperties.VariantIsRlib = false
181 library.MutatedProperties.VariantIsDylib = false
182}
183
184func (library *libraryDecorator) setStatic() {
185 library.MutatedProperties.VariantIsStatic = true
186 library.MutatedProperties.VariantIsShared = false
187 library.MutatedProperties.VariantIsRlib = false
188 library.MutatedProperties.VariantIsDylib = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700189}
190
191var _ compiler = (*libraryDecorator)(nil)
Ivan Lozano52767be2019-10-18 14:49:46 -0700192var _ libraryInterface = (*libraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700193
194// rust_library produces all variants.
195func RustLibraryFactory() android.Module {
196 module, _ := NewRustLibrary(android.HostAndDeviceSupported)
197 return module.Init()
198}
199
200// rust_library_dylib produces a dylib.
201func RustLibraryDylibFactory() android.Module {
202 module, library := NewRustLibrary(android.HostAndDeviceSupported)
203 library.BuildOnlyDylib()
204 return module.Init()
205}
206
207// rust_library_rlib produces an rlib.
208func RustLibraryRlibFactory() android.Module {
209 module, library := NewRustLibrary(android.HostAndDeviceSupported)
210 library.BuildOnlyRlib()
211 return module.Init()
212}
213
Ivan Lozano52767be2019-10-18 14:49:46 -0700214// rust_library_shared produces a shared library.
215func RustLibrarySharedFactory() android.Module {
216 module, library := NewRustLibrary(android.HostAndDeviceSupported)
217 library.BuildOnlyShared()
218 return module.Init()
219}
220
221// rust_library_static produces a static library.
222func RustLibraryStaticFactory() android.Module {
223 module, library := NewRustLibrary(android.HostAndDeviceSupported)
224 library.BuildOnlyStatic()
225 return module.Init()
226}
227
Ivan Lozanoffee3342019-08-27 12:03:00 -0700228// rust_library_host produces all variants.
229func RustLibraryHostFactory() android.Module {
230 module, _ := NewRustLibrary(android.HostSupported)
231 return module.Init()
232}
233
234// rust_library_dylib_host produces a dylib.
235func RustLibraryDylibHostFactory() android.Module {
236 module, library := NewRustLibrary(android.HostSupported)
237 library.BuildOnlyDylib()
238 return module.Init()
239}
240
241// rust_library_rlib_host produces an rlib.
242func RustLibraryRlibHostFactory() android.Module {
243 module, library := NewRustLibrary(android.HostSupported)
244 library.BuildOnlyRlib()
245 return module.Init()
246}
247
Ivan Lozano52767be2019-10-18 14:49:46 -0700248// rust_library_static_host produces a static library.
249func RustLibraryStaticHostFactory() android.Module {
250 module, library := NewRustLibrary(android.HostSupported)
251 library.BuildOnlyStatic()
252 return module.Init()
253}
254
255// rust_library_shared_host produces an shared library.
256func RustLibrarySharedHostFactory() android.Module {
257 module, library := NewRustLibrary(android.HostSupported)
258 library.BuildOnlyShared()
259 return module.Init()
260}
261
Ivan Lozanoffee3342019-08-27 12:03:00 -0700262func (library *libraryDecorator) BuildOnlyDylib() {
263 library.MutatedProperties.BuildRlib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700264 library.MutatedProperties.BuildShared = false
265 library.MutatedProperties.BuildStatic = false
266
Ivan Lozanoffee3342019-08-27 12:03:00 -0700267}
268
269func (library *libraryDecorator) BuildOnlyRlib() {
270 library.MutatedProperties.BuildDylib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700271 library.MutatedProperties.BuildShared = false
272 library.MutatedProperties.BuildStatic = false
273}
274
275func (library *libraryDecorator) BuildOnlyStatic() {
276 library.MutatedProperties.BuildShared = false
277 library.MutatedProperties.BuildRlib = false
278 library.MutatedProperties.BuildDylib = false
279
280}
281
282func (library *libraryDecorator) BuildOnlyShared() {
283 library.MutatedProperties.BuildStatic = false
284 library.MutatedProperties.BuildRlib = false
285 library.MutatedProperties.BuildDylib = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700286}
287
288func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400289 module := newModule(hod, android.MultilibBoth)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700290
291 library := &libraryDecorator{
292 MutatedProperties: LibraryMutatedProperties{
Ivan Lozano52767be2019-10-18 14:49:46 -0700293 BuildDylib: true,
294 BuildRlib: true,
295 BuildShared: true,
296 BuildStatic: true,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700297 },
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800298 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700299 }
300
301 module.compiler = library
302
303 return module, library
304}
305
306func (library *libraryDecorator) compilerProps() []interface{} {
307 return append(library.baseCompiler.compilerProps(),
308 &library.Properties,
309 &library.MutatedProperties)
310}
311
Ivan Lozanof1c84332019-09-20 11:00:37 -0700312func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
Ivan Lozano88f2b1c2019-11-21 14:35:20 -0800313
314 // TODO(b/144861059) Remove if C libraries support dylib linkage in the future.
315 if !ctx.Host() && (library.static() || library.shared()) {
316 library.setNoStdlibs()
317 for _, stdlib := range config.Stdlibs {
318 deps.Rlibs = append(deps.Rlibs, stdlib+".static")
319 }
320 }
321
Ivan Lozanof1c84332019-09-20 11:00:37 -0700322 deps = library.baseCompiler.compilerDeps(ctx, deps)
323
Ivan Lozano52767be2019-10-18 14:49:46 -0700324 if ctx.toolchain().Bionic() && (library.dylib() || library.shared()) {
Ivan Lozanof1c84332019-09-20 11:00:37 -0700325 deps = library.baseCompiler.bionicDeps(ctx, deps)
Ivan Lozano12ee9ca2020-04-07 13:19:44 -0400326 deps.CrtBegin = "crtbegin_so"
327 deps.CrtEnd = "crtend_so"
Ivan Lozanof1c84332019-09-20 11:00:37 -0700328 }
329
330 return deps
331}
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800332func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Matthew Maurer22cef962020-01-13 16:31:13 -0800333 flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.baseModuleName())
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800334 flags = library.baseCompiler.compilerFlags(ctx, flags)
335 if library.shared() || library.static() {
336 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
337 }
338 return flags
339}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700340
Ivan Lozanoffee3342019-08-27 12:03:00 -0700341func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
342 var outputFile android.WritablePath
343
344 srcPath := srcPathFromModuleSrcs(ctx, library.Properties.Srcs)
345
346 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
347
Matthew Maurer46c46cc2020-01-13 16:34:34 -0800348 if library.dylib() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700349 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
350 // https://github.com/rust-lang/rust/issues/19680
351 // https://github.com/rust-lang/rust/issues/34909
352 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
353 }
354
Ivan Lozanoffee3342019-08-27 12:03:00 -0700355 if library.rlib() {
356 fileName := library.getStem(ctx) + ctx.toolchain().RlibSuffix()
357 outputFile = android.PathForModuleOut(ctx, fileName)
358
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400359 outputs := TransformSrctoRlib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
360 library.coverageFile = outputs.coverageFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700361 } else if library.dylib() {
362 fileName := library.getStem(ctx) + ctx.toolchain().DylibSuffix()
363 outputFile = android.PathForModuleOut(ctx, fileName)
364
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400365 outputs := TransformSrctoDylib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
366 library.coverageFile = outputs.coverageFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700367 } else if library.static() {
368 fileName := library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
369 outputFile = android.PathForModuleOut(ctx, fileName)
370
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400371 outputs := TransformSrctoStatic(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
372 library.coverageFile = outputs.coverageFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700373 } else if library.shared() {
374 fileName := library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
375 outputFile = android.PathForModuleOut(ctx, fileName)
376
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400377 outputs := TransformSrctoShared(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
378 library.coverageFile = outputs.coverageFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700379 }
380
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400381 var coverageFiles android.Paths
382 if library.coverageFile != nil {
383 coverageFiles = append(coverageFiles, library.coverageFile)
384 }
385 if len(deps.coverageFiles) > 0 {
386 coverageFiles = append(coverageFiles, deps.coverageFiles...)
387 }
388 library.coverageOutputZipFile = TransformCoverageFilesToZip(ctx, coverageFiles, library.getStem(ctx))
389
Ivan Lozano52767be2019-10-18 14:49:46 -0700390 if library.rlib() || library.dylib() {
391 library.reexportDirs(deps.linkDirs...)
392 library.reexportDepFlags(deps.depFlags...)
393 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700394 library.unstrippedOutputFile = outputFile
395
396 return outputFile
397}
398
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700399func (library *libraryDecorator) getStem(ctx ModuleContext) string {
400 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
401 validateLibraryStem(ctx, stem, library.crateName())
402
403 return stem + String(library.baseCompiler.Properties.Suffix)
404}
405
406var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
407
408func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
409 if crate_name == "" {
410 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
411 }
412
413 // crate_names are used for the library output file, and rustc expects these
414 // to be alphanumeric with underscores allowed.
415 if validCrateName.MatchString(crate_name) {
416 ctx.PropertyErrorf("crate_name",
417 "library crate_names must be alphanumeric with underscores allowed")
418 }
419
420 // Libraries are expected to begin with "lib" followed by the crate_name
421 if !strings.HasPrefix(filename, "lib"+crate_name) {
422 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
423 }
424}
425
Ivan Lozanoffee3342019-08-27 12:03:00 -0700426func LibraryMutator(mctx android.BottomUpMutatorContext) {
427 if m, ok := mctx.Module().(*Module); ok && m.compiler != nil {
428 switch library := m.compiler.(type) {
429 case libraryInterface:
Ivan Lozanoffee3342019-08-27 12:03:00 -0700430
Ivan Lozano52767be2019-10-18 14:49:46 -0700431 // We only build the rust library variants here. This assumes that
432 // LinkageMutator runs first and there's an empty variant
433 // if rust variants are required.
434 if !library.static() && !library.shared() {
435 if library.buildRlib() && library.buildDylib() {
436 modules := mctx.CreateLocalVariations("rlib", "dylib")
437 rlib := modules[0].(*Module)
438 dylib := modules[1].(*Module)
439
440 rlib.compiler.(libraryInterface).setRlib()
441 dylib.compiler.(libraryInterface).setDylib()
442 } else if library.buildRlib() {
443 modules := mctx.CreateLocalVariations("rlib")
444 modules[0].(*Module).compiler.(libraryInterface).setRlib()
445 } else if library.buildDylib() {
446 modules := mctx.CreateLocalVariations("dylib")
447 modules[0].(*Module).compiler.(libraryInterface).setDylib()
448 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700449 }
450 }
451 }
452}