blob: e95cb3afc1348bbaa33b6f43033325542da6e441 [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 Lozano11200872020-09-28 11:56:30 -040027 // Change the rustlibs linkage to select rlib linkage by default for device targets.
28 // Also link libstd as an rlib as well on device targets.
29 // Note: This is the default behavior for host targets.
30 Prefer_rlib *bool `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070031}
32
33type binaryDecorator struct {
34 *baseCompiler
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +020035 stripper Stripper
Ivan Lozanoffee3342019-08-27 12:03:00 -070036
Ivan Lozano8a23fa42020-06-16 10:26:57 -040037 Properties BinaryCompilerProperties
Ivan Lozanoffee3342019-08-27 12:03:00 -070038}
39
40var _ compiler = (*binaryDecorator)(nil)
41
42// rust_binary produces a binary that is runnable on a device.
43func RustBinaryFactory() android.Module {
44 module, _ := NewRustBinary(android.HostAndDeviceSupported)
45 return module.Init()
46}
47
48func RustBinaryHostFactory() android.Module {
49 module, _ := NewRustBinary(android.HostSupported)
50 return module.Init()
51}
52
53func NewRustBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
54 module := newModule(hod, android.MultilibFirst)
55
56 binary := &binaryDecorator{
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080057 baseCompiler: NewBaseCompiler("bin", "", InstallInSystem),
Ivan Lozanoffee3342019-08-27 12:03:00 -070058 }
59
60 module.compiler = binary
61
62 return module, binary
63}
64
Ivan Lozanoffee3342019-08-27 12:03:00 -070065func (binary *binaryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
66 flags = binary.baseCompiler.compilerFlags(ctx, flags)
Ivan Lozanof1c84332019-09-20 11:00:37 -070067
68 if ctx.toolchain().Bionic() {
Ivan Lozanob2df9f82019-11-05 12:16:46 -080069 // no-undefined-version breaks dylib compilation since __rust_*alloc* functions aren't defined,
70 // but we can apply this to binaries.
Ivan Lozanof1c84332019-09-20 11:00:37 -070071 flags.LinkFlags = append(flags.LinkFlags,
72 "-Wl,--gc-sections",
73 "-Wl,-z,nocopyreloc",
74 "-Wl,--no-undefined-version")
75 }
76
Ivan Lozanoffee3342019-08-27 12:03:00 -070077 return flags
78}
79
80func (binary *binaryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
81 deps = binary.baseCompiler.compilerDeps(ctx, deps)
82
Ivan Lozanof1c84332019-09-20 11:00:37 -070083 if ctx.toolchain().Bionic() {
Ivan Lozano45901ed2020-07-24 16:05:01 -040084 deps = bionicDeps(deps)
Ivan Lozanof1c84332019-09-20 11:00:37 -070085 deps.CrtBegin = "crtbegin_dynamic"
86 deps.CrtEnd = "crtend_android"
87 }
88
Ivan Lozanoffee3342019-08-27 12:03:00 -070089 return deps
90}
91
92func (binary *binaryDecorator) compilerProps() []interface{} {
93 return append(binary.baseCompiler.compilerProps(),
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +020094 &binary.Properties,
95 &binary.stripper.StripProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -070096}
97
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040098func (binary *binaryDecorator) nativeCoverage() bool {
99 return true
100}
101
Ivan Lozanoffee3342019-08-27 12:03:00 -0700102func (binary *binaryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
103 fileName := binary.getStem(ctx) + ctx.toolchain().ExecutableSuffix()
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400104 srcPath, _ := srcPathFromModuleSrcs(ctx, binary.baseCompiler.Properties.Srcs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700105 outputFile := android.PathForModuleOut(ctx, fileName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700106
107 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano2093af22020-08-25 12:48:19 -0400108 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700109
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400110 outputs := TransformSrcToBinary(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200111
112 if binary.stripper.NeedsStrip(ctx) {
113 strippedOutputFile := android.PathForModuleOut(ctx, "stripped", fileName)
114 binary.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
115 binary.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
116 }
117
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400118 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
Ivan Lozano042504f2020-08-18 14:31:23 -0400136func (binary *binaryDecorator) autoDep(ctx BaseModuleContext) autoDep {
137 // Binaries default to dylib dependencies for device, rlib for host.
Ivan Lozano11200872020-09-28 11:56:30 -0400138 if Bool(binary.Properties.Prefer_rlib) {
139 return rlibAutoDep
140 }
Ivan Lozano042504f2020-08-18 14:31:23 -0400141 if ctx.Device() {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700142 return dylibAutoDep
143 } else {
144 return rlibAutoDep
145 }
146}
Ivan Lozano11200872020-09-28 11:56:30 -0400147
148func (binary *binaryDecorator) staticStd(ctx *depsContext) bool {
149 return binary.baseCompiler.staticStd(ctx) || Bool(binary.Properties.Prefer_rlib)
150}