blob: 1d02453db01c6276e66ee440c8b0e069523b046f [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 Lozanoffee3342019-08-27 12:03:00 -070027}
28
29type binaryDecorator struct {
30 *baseCompiler
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +020031 stripper Stripper
Ivan Lozanoffee3342019-08-27 12:03:00 -070032
Ivan Lozano8a23fa42020-06-16 10:26:57 -040033 Properties BinaryCompilerProperties
Ivan Lozanoffee3342019-08-27 12:03:00 -070034}
35
36var _ compiler = (*binaryDecorator)(nil)
37
38// rust_binary produces a binary that is runnable on a device.
39func RustBinaryFactory() android.Module {
40 module, _ := NewRustBinary(android.HostAndDeviceSupported)
41 return module.Init()
42}
43
44func RustBinaryHostFactory() android.Module {
45 module, _ := NewRustBinary(android.HostSupported)
46 return module.Init()
47}
48
49func NewRustBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
50 module := newModule(hod, android.MultilibFirst)
51
52 binary := &binaryDecorator{
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080053 baseCompiler: NewBaseCompiler("bin", "", InstallInSystem),
Ivan Lozanoffee3342019-08-27 12:03:00 -070054 }
55
56 module.compiler = binary
57
58 return module, binary
59}
60
Ivan Lozanoffee3342019-08-27 12:03:00 -070061func (binary *binaryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
62 flags = binary.baseCompiler.compilerFlags(ctx, flags)
Ivan Lozanof1c84332019-09-20 11:00:37 -070063
64 if ctx.toolchain().Bionic() {
Ivan Lozanob2df9f82019-11-05 12:16:46 -080065 // no-undefined-version breaks dylib compilation since __rust_*alloc* functions aren't defined,
66 // but we can apply this to binaries.
Ivan Lozanof1c84332019-09-20 11:00:37 -070067 flags.LinkFlags = append(flags.LinkFlags,
68 "-Wl,--gc-sections",
69 "-Wl,-z,nocopyreloc",
70 "-Wl,--no-undefined-version")
71 }
72
Ivan Lozanoffee3342019-08-27 12:03:00 -070073 return flags
74}
75
76func (binary *binaryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
77 deps = binary.baseCompiler.compilerDeps(ctx, deps)
78
Ivan Lozanof1c84332019-09-20 11:00:37 -070079 if ctx.toolchain().Bionic() {
Ivan Lozano45901ed2020-07-24 16:05:01 -040080 deps = bionicDeps(deps)
Ivan Lozanof1c84332019-09-20 11:00:37 -070081 deps.CrtBegin = "crtbegin_dynamic"
82 deps.CrtEnd = "crtend_android"
83 }
84
Ivan Lozanoffee3342019-08-27 12:03:00 -070085 return deps
86}
87
88func (binary *binaryDecorator) compilerProps() []interface{} {
89 return append(binary.baseCompiler.compilerProps(),
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +020090 &binary.Properties,
91 &binary.stripper.StripProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -070092}
93
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040094func (binary *binaryDecorator) nativeCoverage() bool {
95 return true
96}
97
Ivan Lozanoffee3342019-08-27 12:03:00 -070098func (binary *binaryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
99 fileName := binary.getStem(ctx) + ctx.toolchain().ExecutableSuffix()
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400100 srcPath, _ := srcPathFromModuleSrcs(ctx, binary.baseCompiler.Properties.Srcs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700101 outputFile := android.PathForModuleOut(ctx, fileName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700102
103 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano2093af22020-08-25 12:48:19 -0400104 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700105
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400106 outputs := TransformSrcToBinary(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200107
108 if binary.stripper.NeedsStrip(ctx) {
109 strippedOutputFile := android.PathForModuleOut(ctx, "stripped", fileName)
110 binary.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
111 binary.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
112 }
113
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400114 binary.coverageFile = outputs.coverageFile
115
116 var coverageFiles android.Paths
117 if outputs.coverageFile != nil {
118 coverageFiles = append(coverageFiles, binary.coverageFile)
119 }
120 if len(deps.coverageFiles) > 0 {
121 coverageFiles = append(coverageFiles, deps.coverageFiles...)
122 }
123 binary.coverageOutputZipFile = TransformCoverageFilesToZip(ctx, coverageFiles, binary.getStem(ctx))
Ivan Lozanoffee3342019-08-27 12:03:00 -0700124
125 return outputFile
126}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400127
128func (binary *binaryDecorator) coverageOutputZipPath() android.OptionalPath {
129 return binary.coverageOutputZipFile
130}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700131
Ivan Lozano042504f2020-08-18 14:31:23 -0400132func (binary *binaryDecorator) autoDep(ctx BaseModuleContext) autoDep {
133 // Binaries default to dylib dependencies for device, rlib for host.
134 if ctx.Device() {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700135 return dylibAutoDep
136 } else {
137 return rlibAutoDep
138 }
139}