blob: 8490e93a78492a03d9e21700177af8a574eaead7 [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 {
Ivan Lozanob2df9f82019-11-05 12:16:46 -080027 // passes -C prefer-dynamic to rustc, which tells it to dynamically link the stdlib
28 // (assuming it has no dylib dependencies already)
Ivan Lozanoffee3342019-08-27 12:03:00 -070029 Prefer_dynamic *bool
30}
31
32type binaryDecorator struct {
33 *baseCompiler
34
Ivan Lozano8a23fa42020-06-16 10:26:57 -040035 Properties BinaryCompilerProperties
Ivan Lozanoffee3342019-08-27 12:03:00 -070036}
37
38var _ compiler = (*binaryDecorator)(nil)
39
40// rust_binary produces a binary that is runnable on a device.
41func RustBinaryFactory() android.Module {
42 module, _ := NewRustBinary(android.HostAndDeviceSupported)
43 return module.Init()
44}
45
46func RustBinaryHostFactory() android.Module {
47 module, _ := NewRustBinary(android.HostSupported)
48 return module.Init()
49}
50
51func NewRustBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
52 module := newModule(hod, android.MultilibFirst)
53
54 binary := &binaryDecorator{
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080055 baseCompiler: NewBaseCompiler("bin", "", InstallInSystem),
Ivan Lozanoffee3342019-08-27 12:03:00 -070056 }
57
58 module.compiler = binary
59
60 return module, binary
61}
62
63func (binary *binaryDecorator) preferDynamic() bool {
64 return Bool(binary.Properties.Prefer_dynamic)
65}
66
67func (binary *binaryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
68 flags = binary.baseCompiler.compilerFlags(ctx, flags)
Ivan Lozanof1c84332019-09-20 11:00:37 -070069
70 if ctx.toolchain().Bionic() {
Ivan Lozanob2df9f82019-11-05 12:16:46 -080071 // no-undefined-version breaks dylib compilation since __rust_*alloc* functions aren't defined,
72 // but we can apply this to binaries.
Ivan Lozanof1c84332019-09-20 11:00:37 -070073 flags.LinkFlags = append(flags.LinkFlags,
74 "-Wl,--gc-sections",
75 "-Wl,-z,nocopyreloc",
76 "-Wl,--no-undefined-version")
77 }
78
Ivan Lozanoffee3342019-08-27 12:03:00 -070079 if binary.preferDynamic() {
80 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
81 }
82 return flags
83}
84
85func (binary *binaryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
86 deps = binary.baseCompiler.compilerDeps(ctx, deps)
87
Ivan Lozanof1c84332019-09-20 11:00:37 -070088 if ctx.toolchain().Bionic() {
Ivan Lozano45901ed2020-07-24 16:05:01 -040089 deps = bionicDeps(deps)
Ivan Lozanof1c84332019-09-20 11:00:37 -070090 deps.CrtBegin = "crtbegin_dynamic"
91 deps.CrtEnd = "crtend_android"
92 }
93
Ivan Lozanoffee3342019-08-27 12:03:00 -070094 return deps
95}
96
97func (binary *binaryDecorator) compilerProps() []interface{} {
98 return append(binary.baseCompiler.compilerProps(),
99 &binary.Properties)
100}
101
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400102func (binary *binaryDecorator) nativeCoverage() bool {
103 return true
104}
105
Ivan Lozanoffee3342019-08-27 12:03:00 -0700106func (binary *binaryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
107 fileName := binary.getStem(ctx) + ctx.toolchain().ExecutableSuffix()
108
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400109 srcPath, _ := srcPathFromModuleSrcs(ctx, binary.baseCompiler.Properties.Srcs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700110
111 outputFile := android.PathForModuleOut(ctx, fileName)
112 binary.unstrippedOutputFile = outputFile
113
114 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano2093af22020-08-25 12:48:19 -0400115 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700116
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400117 outputs := TransformSrcToBinary(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
118 binary.coverageFile = outputs.coverageFile
119
120 var coverageFiles android.Paths
121 if outputs.coverageFile != nil {
122 coverageFiles = append(coverageFiles, binary.coverageFile)
123 }
124 if len(deps.coverageFiles) > 0 {
125 coverageFiles = append(coverageFiles, deps.coverageFiles...)
126 }
127 binary.coverageOutputZipFile = TransformCoverageFilesToZip(ctx, coverageFiles, binary.getStem(ctx))
Ivan Lozanoffee3342019-08-27 12:03:00 -0700128
129 return outputFile
130}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400131
132func (binary *binaryDecorator) coverageOutputZipPath() android.OptionalPath {
133 return binary.coverageOutputZipFile
134}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700135
136func (binary *binaryDecorator) autoDep() autoDep {
137 if binary.preferDynamic() {
138 return dylibAutoDep
139 } else {
140 return rlibAutoDep
141 }
142}