blob: 704c77b4005a73197e6fa2b4da0414b4806f3cb8 [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
Ivan Lozano52767be2019-10-18 14:49:46 -070048 // path to include directories to pass to cc_* modules, only relevant for static/shared variants.
49 Include_dirs []string `android:"path,arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070050}
51
52type LibraryMutatedProperties struct {
Ivan Lozanoffee3342019-08-27 12:03:00 -070053 // Build a dylib variant
54 BuildDylib bool `blueprint:"mutated"`
55 // Build an rlib variant
56 BuildRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070057 // Build a shared library variant
58 BuildShared bool `blueprint:"mutated"`
59 // Build a static library variant
60 BuildStatic bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070061
62 // This variant is a dylib
63 VariantIsDylib bool `blueprint:"mutated"`
64 // This variant is an rlib
65 VariantIsRlib bool `blueprint:"mutated"`
Ivan Lozano52767be2019-10-18 14:49:46 -070066 // This variant is a shared library
67 VariantIsShared bool `blueprint:"mutated"`
68 // This variant is a static library
69 VariantIsStatic bool `blueprint:"mutated"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070070}
71
72type libraryDecorator struct {
73 *baseCompiler
74
Ivan Lozano8a23fa42020-06-16 10:26:57 -040075 Properties LibraryCompilerProperties
76 MutatedProperties LibraryMutatedProperties
77 includeDirs android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -070078}
79
80type libraryInterface interface {
81 rlib() bool
82 dylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -070083 static() bool
84 shared() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070085
86 // Returns true if the build options for the module have selected a particular build type
87 buildRlib() bool
88 buildDylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -070089 buildShared() bool
90 buildStatic() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070091
92 // Sets a particular variant type
93 setRlib()
94 setDylib()
Ivan Lozano52767be2019-10-18 14:49:46 -070095 setShared()
96 setStatic()
97
98 // Build a specific library variant
99 BuildOnlyRlib()
100 BuildOnlyDylib()
101 BuildOnlyStatic()
102 BuildOnlyShared()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700103}
104
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400105func (library *libraryDecorator) nativeCoverage() bool {
106 return true
107}
108
Ivan Lozanoffee3342019-08-27 12:03:00 -0700109func (library *libraryDecorator) exportedDirs() []string {
110 return library.linkDirs
111}
112
113func (library *libraryDecorator) exportedDepFlags() []string {
114 return library.depFlags
115}
116
117func (library *libraryDecorator) reexportDirs(dirs ...string) {
118 library.linkDirs = android.FirstUniqueStrings(append(library.linkDirs, dirs...))
119}
120
121func (library *libraryDecorator) reexportDepFlags(flags ...string) {
122 library.depFlags = android.FirstUniqueStrings(append(library.depFlags, flags...))
123}
124
125func (library *libraryDecorator) rlib() bool {
126 return library.MutatedProperties.VariantIsRlib
127}
128
129func (library *libraryDecorator) dylib() bool {
130 return library.MutatedProperties.VariantIsDylib
131}
132
Ivan Lozano52767be2019-10-18 14:49:46 -0700133func (library *libraryDecorator) shared() bool {
134 return library.MutatedProperties.VariantIsShared
135}
136
137func (library *libraryDecorator) static() bool {
138 return library.MutatedProperties.VariantIsStatic
139}
140
Ivan Lozanoffee3342019-08-27 12:03:00 -0700141func (library *libraryDecorator) buildRlib() bool {
142 return library.MutatedProperties.BuildRlib && BoolDefault(library.Properties.Rlib.Enabled, true)
143}
144
145func (library *libraryDecorator) buildDylib() bool {
146 return library.MutatedProperties.BuildDylib && BoolDefault(library.Properties.Dylib.Enabled, true)
147}
148
Ivan Lozano52767be2019-10-18 14:49:46 -0700149func (library *libraryDecorator) buildShared() bool {
150 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
151}
152
153func (library *libraryDecorator) buildStatic() bool {
154 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
155}
156
Ivan Lozanoffee3342019-08-27 12:03:00 -0700157func (library *libraryDecorator) setRlib() {
158 library.MutatedProperties.VariantIsRlib = true
159 library.MutatedProperties.VariantIsDylib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700160 library.MutatedProperties.VariantIsStatic = false
161 library.MutatedProperties.VariantIsShared = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700162}
163
164func (library *libraryDecorator) setDylib() {
165 library.MutatedProperties.VariantIsRlib = false
166 library.MutatedProperties.VariantIsDylib = true
Ivan Lozano52767be2019-10-18 14:49:46 -0700167 library.MutatedProperties.VariantIsStatic = false
168 library.MutatedProperties.VariantIsShared = false
169}
170
171func (library *libraryDecorator) setShared() {
172 library.MutatedProperties.VariantIsStatic = false
173 library.MutatedProperties.VariantIsShared = true
174 library.MutatedProperties.VariantIsRlib = false
175 library.MutatedProperties.VariantIsDylib = false
176}
177
178func (library *libraryDecorator) setStatic() {
179 library.MutatedProperties.VariantIsStatic = true
180 library.MutatedProperties.VariantIsShared = false
181 library.MutatedProperties.VariantIsRlib = false
182 library.MutatedProperties.VariantIsDylib = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700183}
184
185var _ compiler = (*libraryDecorator)(nil)
Ivan Lozano52767be2019-10-18 14:49:46 -0700186var _ libraryInterface = (*libraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700187
188// rust_library produces all variants.
189func RustLibraryFactory() android.Module {
190 module, _ := NewRustLibrary(android.HostAndDeviceSupported)
191 return module.Init()
192}
193
194// rust_library_dylib produces a dylib.
195func RustLibraryDylibFactory() android.Module {
196 module, library := NewRustLibrary(android.HostAndDeviceSupported)
197 library.BuildOnlyDylib()
198 return module.Init()
199}
200
201// rust_library_rlib produces an rlib.
202func RustLibraryRlibFactory() android.Module {
203 module, library := NewRustLibrary(android.HostAndDeviceSupported)
204 library.BuildOnlyRlib()
205 return module.Init()
206}
207
Ivan Lozano52767be2019-10-18 14:49:46 -0700208// rust_library_shared produces a shared library.
209func RustLibrarySharedFactory() android.Module {
210 module, library := NewRustLibrary(android.HostAndDeviceSupported)
211 library.BuildOnlyShared()
212 return module.Init()
213}
214
215// rust_library_static produces a static library.
216func RustLibraryStaticFactory() android.Module {
217 module, library := NewRustLibrary(android.HostAndDeviceSupported)
218 library.BuildOnlyStatic()
219 return module.Init()
220}
221
Ivan Lozanoffee3342019-08-27 12:03:00 -0700222// rust_library_host produces all variants.
223func RustLibraryHostFactory() android.Module {
224 module, _ := NewRustLibrary(android.HostSupported)
225 return module.Init()
226}
227
228// rust_library_dylib_host produces a dylib.
229func RustLibraryDylibHostFactory() android.Module {
230 module, library := NewRustLibrary(android.HostSupported)
231 library.BuildOnlyDylib()
232 return module.Init()
233}
234
235// rust_library_rlib_host produces an rlib.
236func RustLibraryRlibHostFactory() android.Module {
237 module, library := NewRustLibrary(android.HostSupported)
238 library.BuildOnlyRlib()
239 return module.Init()
240}
241
Ivan Lozano52767be2019-10-18 14:49:46 -0700242// rust_library_static_host produces a static library.
243func RustLibraryStaticHostFactory() android.Module {
244 module, library := NewRustLibrary(android.HostSupported)
245 library.BuildOnlyStatic()
246 return module.Init()
247}
248
249// rust_library_shared_host produces an shared library.
250func RustLibrarySharedHostFactory() android.Module {
251 module, library := NewRustLibrary(android.HostSupported)
252 library.BuildOnlyShared()
253 return module.Init()
254}
255
Ivan Lozanoffee3342019-08-27 12:03:00 -0700256func (library *libraryDecorator) BuildOnlyDylib() {
257 library.MutatedProperties.BuildRlib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700258 library.MutatedProperties.BuildShared = false
259 library.MutatedProperties.BuildStatic = false
260
Ivan Lozanoffee3342019-08-27 12:03:00 -0700261}
262
263func (library *libraryDecorator) BuildOnlyRlib() {
264 library.MutatedProperties.BuildDylib = false
Ivan Lozano52767be2019-10-18 14:49:46 -0700265 library.MutatedProperties.BuildShared = false
266 library.MutatedProperties.BuildStatic = false
267}
268
269func (library *libraryDecorator) BuildOnlyStatic() {
270 library.MutatedProperties.BuildShared = false
271 library.MutatedProperties.BuildRlib = false
272 library.MutatedProperties.BuildDylib = false
273
274}
275
276func (library *libraryDecorator) BuildOnlyShared() {
277 library.MutatedProperties.BuildStatic = false
278 library.MutatedProperties.BuildRlib = false
279 library.MutatedProperties.BuildDylib = false
Ivan Lozanoffee3342019-08-27 12:03:00 -0700280}
281
282func NewRustLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400283 module := newModule(hod, android.MultilibBoth)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700284
285 library := &libraryDecorator{
286 MutatedProperties: LibraryMutatedProperties{
Ivan Lozano52767be2019-10-18 14:49:46 -0700287 BuildDylib: true,
288 BuildRlib: true,
289 BuildShared: true,
290 BuildStatic: true,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700291 },
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800292 baseCompiler: NewBaseCompiler("lib", "lib64", InstallInSystem),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700293 }
294
295 module.compiler = library
296
297 return module, library
298}
299
300func (library *libraryDecorator) compilerProps() []interface{} {
301 return append(library.baseCompiler.compilerProps(),
302 &library.Properties,
303 &library.MutatedProperties)
304}
305
Ivan Lozanof1c84332019-09-20 11:00:37 -0700306func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
Ivan Lozano88f2b1c2019-11-21 14:35:20 -0800307
308 // TODO(b/144861059) Remove if C libraries support dylib linkage in the future.
309 if !ctx.Host() && (library.static() || library.shared()) {
310 library.setNoStdlibs()
311 for _, stdlib := range config.Stdlibs {
312 deps.Rlibs = append(deps.Rlibs, stdlib+".static")
313 }
314 }
315
Ivan Lozanof1c84332019-09-20 11:00:37 -0700316 deps = library.baseCompiler.compilerDeps(ctx, deps)
317
Ivan Lozano52767be2019-10-18 14:49:46 -0700318 if ctx.toolchain().Bionic() && (library.dylib() || library.shared()) {
Ivan Lozanof1c84332019-09-20 11:00:37 -0700319 deps = library.baseCompiler.bionicDeps(ctx, deps)
Ivan Lozano12ee9ca2020-04-07 13:19:44 -0400320 deps.CrtBegin = "crtbegin_so"
321 deps.CrtEnd = "crtend_so"
Ivan Lozanof1c84332019-09-20 11:00:37 -0700322 }
323
324 return deps
325}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400326
327func (library *libraryDecorator) sharedLibFilename(ctx ModuleContext) string {
328 return library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
329}
330
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800331func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Matthew Maurer22cef962020-01-13 16:31:13 -0800332 flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.baseModuleName())
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800333 flags = library.baseCompiler.compilerFlags(ctx, flags)
334 if library.shared() || library.static() {
335 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
336 }
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400337 if library.shared() {
338 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-soname="+library.sharedLibFilename(ctx))
339 }
340
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800341 return flags
342}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700343
Ivan Lozanoffee3342019-08-27 12:03:00 -0700344func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
345 var outputFile android.WritablePath
346
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400347 srcPath := srcPathFromModuleSrcs(ctx, library.baseCompiler.Properties.Srcs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700348
349 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
350
Matthew Maurer46c46cc2020-01-13 16:34:34 -0800351 if library.dylib() {
Ivan Lozano52767be2019-10-18 14:49:46 -0700352 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
353 // https://github.com/rust-lang/rust/issues/19680
354 // https://github.com/rust-lang/rust/issues/34909
355 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
356 }
357
Ivan Lozanoffee3342019-08-27 12:03:00 -0700358 if library.rlib() {
359 fileName := library.getStem(ctx) + ctx.toolchain().RlibSuffix()
360 outputFile = android.PathForModuleOut(ctx, fileName)
361
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400362 outputs := TransformSrctoRlib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
363 library.coverageFile = outputs.coverageFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700364 } else if library.dylib() {
365 fileName := library.getStem(ctx) + ctx.toolchain().DylibSuffix()
366 outputFile = android.PathForModuleOut(ctx, fileName)
367
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400368 outputs := TransformSrctoDylib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
369 library.coverageFile = outputs.coverageFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700370 } else if library.static() {
371 fileName := library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
372 outputFile = android.PathForModuleOut(ctx, fileName)
373
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400374 outputs := TransformSrctoStatic(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
375 library.coverageFile = outputs.coverageFile
Ivan Lozano52767be2019-10-18 14:49:46 -0700376 } else if library.shared() {
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400377 fileName := library.sharedLibFilename(ctx)
Ivan Lozano52767be2019-10-18 14:49:46 -0700378 outputFile = android.PathForModuleOut(ctx, fileName)
379
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400380 outputs := TransformSrctoShared(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
381 library.coverageFile = outputs.coverageFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700382 }
383
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400384 var coverageFiles android.Paths
385 if library.coverageFile != nil {
386 coverageFiles = append(coverageFiles, library.coverageFile)
387 }
388 if len(deps.coverageFiles) > 0 {
389 coverageFiles = append(coverageFiles, deps.coverageFiles...)
390 }
391 library.coverageOutputZipFile = TransformCoverageFilesToZip(ctx, coverageFiles, library.getStem(ctx))
392
Ivan Lozano52767be2019-10-18 14:49:46 -0700393 if library.rlib() || library.dylib() {
394 library.reexportDirs(deps.linkDirs...)
395 library.reexportDepFlags(deps.depFlags...)
396 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700397 library.unstrippedOutputFile = outputFile
398
399 return outputFile
400}
401
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700402func (library *libraryDecorator) getStem(ctx ModuleContext) string {
403 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
404 validateLibraryStem(ctx, stem, library.crateName())
405
406 return stem + String(library.baseCompiler.Properties.Suffix)
407}
408
409var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
410
411func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
412 if crate_name == "" {
413 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
414 }
415
416 // crate_names are used for the library output file, and rustc expects these
417 // to be alphanumeric with underscores allowed.
418 if validCrateName.MatchString(crate_name) {
419 ctx.PropertyErrorf("crate_name",
420 "library crate_names must be alphanumeric with underscores allowed")
421 }
422
423 // Libraries are expected to begin with "lib" followed by the crate_name
424 if !strings.HasPrefix(filename, "lib"+crate_name) {
425 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
426 }
427}
428
Ivan Lozanoffee3342019-08-27 12:03:00 -0700429func LibraryMutator(mctx android.BottomUpMutatorContext) {
430 if m, ok := mctx.Module().(*Module); ok && m.compiler != nil {
431 switch library := m.compiler.(type) {
432 case libraryInterface:
Ivan Lozanoffee3342019-08-27 12:03:00 -0700433
Ivan Lozano52767be2019-10-18 14:49:46 -0700434 // We only build the rust library variants here. This assumes that
435 // LinkageMutator runs first and there's an empty variant
436 // if rust variants are required.
437 if !library.static() && !library.shared() {
438 if library.buildRlib() && library.buildDylib() {
439 modules := mctx.CreateLocalVariations("rlib", "dylib")
440 rlib := modules[0].(*Module)
441 dylib := modules[1].(*Module)
442
443 rlib.compiler.(libraryInterface).setRlib()
444 dylib.compiler.(libraryInterface).setDylib()
445 } else if library.buildRlib() {
446 modules := mctx.CreateLocalVariations("rlib")
447 modules[0].(*Module).compiler.(libraryInterface).setRlib()
448 } else if library.buildDylib() {
449 modules := mctx.CreateLocalVariations("dylib")
450 modules[0].(*Module).compiler.(libraryInterface).setDylib()
451 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700452 }
453 }
454 }
455}