blob: c25ae0969c0b71bb2895a0f0ee1aff88a768b937 [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 (
18 "android/soong/android"
Ivan Lozanoffee3342019-08-27 12:03:00 -070019)
20
21func init() {
22 android.RegisterModuleType("rust_binary", RustBinaryFactory)
23 android.RegisterModuleType("rust_binary_host", RustBinaryHostFactory)
24}
25
26type BinaryCompilerProperties struct {
27 // path to the main source file that contains the program entry point (e.g. src/main.rs)
28 Srcs []string `android:"path,arch_variant"`
29
Ivan Lozanob2df9f82019-11-05 12:16:46 -080030 // passes -C prefer-dynamic to rustc, which tells it to dynamically link the stdlib
31 // (assuming it has no dylib dependencies already)
Ivan Lozanoffee3342019-08-27 12:03:00 -070032 Prefer_dynamic *bool
33}
34
35type binaryDecorator struct {
36 *baseCompiler
37
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040038 Properties BinaryCompilerProperties
39 distFile android.OptionalPath
40 coverageOutputZipFile android.OptionalPath
41 unstrippedOutputFile android.Path
Ivan Lozanoffee3342019-08-27 12:03:00 -070042}
43
44var _ compiler = (*binaryDecorator)(nil)
45
46// rust_binary produces a binary that is runnable on a device.
47func RustBinaryFactory() android.Module {
48 module, _ := NewRustBinary(android.HostAndDeviceSupported)
49 return module.Init()
50}
51
52func RustBinaryHostFactory() android.Module {
53 module, _ := NewRustBinary(android.HostSupported)
54 return module.Init()
55}
56
57func NewRustBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
58 module := newModule(hod, android.MultilibFirst)
59
60 binary := &binaryDecorator{
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080061 baseCompiler: NewBaseCompiler("bin", "", InstallInSystem),
Ivan Lozanoffee3342019-08-27 12:03:00 -070062 }
63
64 module.compiler = binary
65
66 return module, binary
67}
68
69func (binary *binaryDecorator) preferDynamic() bool {
70 return Bool(binary.Properties.Prefer_dynamic)
71}
72
73func (binary *binaryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
74 flags = binary.baseCompiler.compilerFlags(ctx, flags)
Ivan Lozanof1c84332019-09-20 11:00:37 -070075
76 if ctx.toolchain().Bionic() {
Ivan Lozanob2df9f82019-11-05 12:16:46 -080077 // no-undefined-version breaks dylib compilation since __rust_*alloc* functions aren't defined,
78 // but we can apply this to binaries.
Ivan Lozanof1c84332019-09-20 11:00:37 -070079 flags.LinkFlags = append(flags.LinkFlags,
80 "-Wl,--gc-sections",
81 "-Wl,-z,nocopyreloc",
82 "-Wl,--no-undefined-version")
83 }
84
Ivan Lozanoffee3342019-08-27 12:03:00 -070085 if binary.preferDynamic() {
86 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
87 }
88 return flags
89}
90
91func (binary *binaryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
92 deps = binary.baseCompiler.compilerDeps(ctx, deps)
93
Ivan Lozanof1c84332019-09-20 11:00:37 -070094 if ctx.toolchain().Bionic() {
95 deps = binary.baseCompiler.bionicDeps(ctx, deps)
96 deps.CrtBegin = "crtbegin_dynamic"
97 deps.CrtEnd = "crtend_android"
98 }
99
Ivan Lozanoffee3342019-08-27 12:03:00 -0700100 return deps
101}
102
103func (binary *binaryDecorator) compilerProps() []interface{} {
104 return append(binary.baseCompiler.compilerProps(),
105 &binary.Properties)
106}
107
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400108func (binary *binaryDecorator) nativeCoverage() bool {
109 return true
110}
111
Ivan Lozanoffee3342019-08-27 12:03:00 -0700112func (binary *binaryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
113 fileName := binary.getStem(ctx) + ctx.toolchain().ExecutableSuffix()
114
115 srcPath := srcPathFromModuleSrcs(ctx, binary.Properties.Srcs)
116
117 outputFile := android.PathForModuleOut(ctx, fileName)
118 binary.unstrippedOutputFile = outputFile
119
120 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
121
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400122 outputs := TransformSrcToBinary(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
123 binary.coverageFile = outputs.coverageFile
124
125 var coverageFiles android.Paths
126 if outputs.coverageFile != nil {
127 coverageFiles = append(coverageFiles, binary.coverageFile)
128 }
129 if len(deps.coverageFiles) > 0 {
130 coverageFiles = append(coverageFiles, deps.coverageFiles...)
131 }
132 binary.coverageOutputZipFile = TransformCoverageFilesToZip(ctx, coverageFiles, binary.getStem(ctx))
Ivan Lozanoffee3342019-08-27 12:03:00 -0700133
134 return outputFile
135}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400136
137func (binary *binaryDecorator) coverageOutputZipPath() android.OptionalPath {
138 return binary.coverageOutputZipFile
139}