blob: 386ea47134b6a7b538640684dcc5ae98f8c882e8 [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)
Ivan Lozano52767be2019-10-18 14:49:46 -070031 android.RegisterModuleType("rust_library_shared", RustLibrarySharedFactory)
32 android.RegisterModuleType("rust_library_static", RustLibraryStaticFactory)
33 android.RegisterModuleType("rust_library_host_shared", RustLibrarySharedHostFactory)
34 android.RegisterModuleType("rust_library_host_static", RustLibraryStaticHostFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070035}
36
37type VariantLibraryProperties struct {
38 Enabled *bool `android:"arch_variant"`
39}
40
41type LibraryCompilerProperties struct {
Ivan Lozano52767be2019-10-18 14:49:46 -070042 Rlib VariantLibraryProperties `android:"arch_variant"`
43 Dylib VariantLibraryProperties `android:"arch_variant"`
44 Shared VariantLibraryProperties `android:"arch_variant"`
45 Static VariantLibraryProperties `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070046
47 // path to the source file that is the main entry point of the program (e.g. src/lib.rs)
48 Srcs []string `android:"path,arch_variant"`
Ivan Lozano52767be2019-10-18 14:49:46 -070049
50 // 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 Lozanoffee3342019-08-27 12:03:00 -070072}
73
74type libraryDecorator struct {
75 *baseCompiler
76
77 Properties LibraryCompilerProperties
78 MutatedProperties LibraryMutatedProperties
79 distFile android.OptionalPath
80 unstrippedOutputFile android.Path
Ivan Lozanoe0833b12019-11-06 19:15:49 -080081 includeDirs android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -070082}
83
84type libraryInterface interface {
85 rlib() bool
86 dylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -070087 static() bool
88 shared() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070089
90 // Returns true if the build options for the module have selected a particular build type
91 buildRlib() bool
92 buildDylib() bool
Ivan Lozano52767be2019-10-18 14:49:46 -070093 buildShared() bool
94 buildStatic() bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070095
96 // Sets a particular variant type
97 setRlib()
98 setDylib()
Ivan Lozano52767be2019-10-18 14:49:46 -070099 setShared()
100 setStatic()
101
102 // Build a specific library variant
103 BuildOnlyRlib()
104 BuildOnlyDylib()
105 BuildOnlyStatic()
106 BuildOnlyShared()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700107}
108
109func (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) {
283 module := newModule(hod, android.MultilibFirst)
284
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 },
292 baseCompiler: NewBaseCompiler("lib", "lib64"),
293 }
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 {
307 deps = library.baseCompiler.compilerDeps(ctx, deps)
308
Ivan Lozano52767be2019-10-18 14:49:46 -0700309 if ctx.toolchain().Bionic() && (library.dylib() || library.shared()) {
Ivan Lozanof1c84332019-09-20 11:00:37 -0700310 deps = library.baseCompiler.bionicDeps(ctx, deps)
311 }
312
313 return deps
314}
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800315func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
316 flags = library.baseCompiler.compilerFlags(ctx, flags)
317 if library.shared() || library.static() {
318 library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
319 }
320 return flags
321}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700322
Ivan Lozanoffee3342019-08-27 12:03:00 -0700323func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
324 var outputFile android.WritablePath
325
326 srcPath := srcPathFromModuleSrcs(ctx, library.Properties.Srcs)
327
328 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
329
Ivan Lozano52767be2019-10-18 14:49:46 -0700330 if library.dylib() || library.shared() {
331 // We need prefer-dynamic for now to avoid linking in the static stdlib. See:
332 // https://github.com/rust-lang/rust/issues/19680
333 // https://github.com/rust-lang/rust/issues/34909
334 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
335 }
336
Ivan Lozanoffee3342019-08-27 12:03:00 -0700337 if library.rlib() {
338 fileName := library.getStem(ctx) + ctx.toolchain().RlibSuffix()
339 outputFile = android.PathForModuleOut(ctx, fileName)
340
341 TransformSrctoRlib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
342 } else if library.dylib() {
343 fileName := library.getStem(ctx) + ctx.toolchain().DylibSuffix()
344 outputFile = android.PathForModuleOut(ctx, fileName)
345
Ivan Lozanoffee3342019-08-27 12:03:00 -0700346 TransformSrctoDylib(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
Ivan Lozano52767be2019-10-18 14:49:46 -0700347 } else if library.static() {
348 fileName := library.getStem(ctx) + ctx.toolchain().StaticLibSuffix()
349 outputFile = android.PathForModuleOut(ctx, fileName)
350
351 TransformSrctoStatic(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
352 } else if library.shared() {
353 fileName := library.getStem(ctx) + ctx.toolchain().SharedLibSuffix()
354 outputFile = android.PathForModuleOut(ctx, fileName)
355
356 TransformSrctoShared(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700357 }
358
Ivan Lozano52767be2019-10-18 14:49:46 -0700359 if library.rlib() || library.dylib() {
360 library.reexportDirs(deps.linkDirs...)
361 library.reexportDepFlags(deps.depFlags...)
362 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700363 library.unstrippedOutputFile = outputFile
364
365 return outputFile
366}
367
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700368func (library *libraryDecorator) getStem(ctx ModuleContext) string {
369 stem := library.baseCompiler.getStemWithoutSuffix(ctx)
370 validateLibraryStem(ctx, stem, library.crateName())
371
372 return stem + String(library.baseCompiler.Properties.Suffix)
373}
374
375var validCrateName = regexp.MustCompile("[^a-zA-Z0-9_]+")
376
377func validateLibraryStem(ctx BaseModuleContext, filename string, crate_name string) {
378 if crate_name == "" {
379 ctx.PropertyErrorf("crate_name", "crate_name must be defined.")
380 }
381
382 // crate_names are used for the library output file, and rustc expects these
383 // to be alphanumeric with underscores allowed.
384 if validCrateName.MatchString(crate_name) {
385 ctx.PropertyErrorf("crate_name",
386 "library crate_names must be alphanumeric with underscores allowed")
387 }
388
389 // Libraries are expected to begin with "lib" followed by the crate_name
390 if !strings.HasPrefix(filename, "lib"+crate_name) {
391 ctx.ModuleErrorf("Invalid name or stem property; library filenames must start with lib<crate_name>")
392 }
393}
394
Ivan Lozanoffee3342019-08-27 12:03:00 -0700395func LibraryMutator(mctx android.BottomUpMutatorContext) {
396 if m, ok := mctx.Module().(*Module); ok && m.compiler != nil {
397 switch library := m.compiler.(type) {
398 case libraryInterface:
Ivan Lozanoffee3342019-08-27 12:03:00 -0700399
Ivan Lozano52767be2019-10-18 14:49:46 -0700400 // We only build the rust library variants here. This assumes that
401 // LinkageMutator runs first and there's an empty variant
402 // if rust variants are required.
403 if !library.static() && !library.shared() {
404 if library.buildRlib() && library.buildDylib() {
405 modules := mctx.CreateLocalVariations("rlib", "dylib")
406 rlib := modules[0].(*Module)
407 dylib := modules[1].(*Module)
408
409 rlib.compiler.(libraryInterface).setRlib()
410 dylib.compiler.(libraryInterface).setDylib()
411 } else if library.buildRlib() {
412 modules := mctx.CreateLocalVariations("rlib")
413 modules[0].(*Module).compiler.(libraryInterface).setRlib()
414 } else if library.buildDylib() {
415 modules := mctx.CreateLocalVariations("dylib")
416 modules[0].(*Module).compiler.(libraryInterface).setDylib()
417 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700418 }
419 }
420 }
421}