blob: 8d0a0a7a081ce81f208f1f0715636d2dd03c4703 [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 Lozanobf63d002020-10-02 10:03:23 -040027 // Builds this binary as a static binary. Implies prefer_rlib true.
28 //
29 // Static executables currently only support for bionic targets. Non-bionic targets will not produce a fully static
30 // binary, but will still implicitly imply prefer_rlib true.
31 Static_executable *bool `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070032}
33
34type binaryDecorator struct {
35 *baseCompiler
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +020036 stripper Stripper
Ivan Lozanoffee3342019-08-27 12:03:00 -070037
Ivan Lozano8a23fa42020-06-16 10:26:57 -040038 Properties BinaryCompilerProperties
Ivan Lozanoffee3342019-08-27 12:03:00 -070039}
40
41var _ compiler = (*binaryDecorator)(nil)
42
43// rust_binary produces a binary that is runnable on a device.
44func RustBinaryFactory() android.Module {
45 module, _ := NewRustBinary(android.HostAndDeviceSupported)
46 return module.Init()
47}
48
49func RustBinaryHostFactory() android.Module {
50 module, _ := NewRustBinary(android.HostSupported)
51 return module.Init()
52}
53
54func NewRustBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
55 module := newModule(hod, android.MultilibFirst)
56
57 binary := &binaryDecorator{
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080058 baseCompiler: NewBaseCompiler("bin", "", InstallInSystem),
Ivan Lozanoffee3342019-08-27 12:03:00 -070059 }
60
61 module.compiler = binary
62
63 return module, binary
64}
65
Ivan Lozanoffee3342019-08-27 12:03:00 -070066func (binary *binaryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
67 flags = binary.baseCompiler.compilerFlags(ctx, flags)
Ivan Lozanof1c84332019-09-20 11:00:37 -070068
69 if ctx.toolchain().Bionic() {
Ivan Lozanob2df9f82019-11-05 12:16:46 -080070 // no-undefined-version breaks dylib compilation since __rust_*alloc* functions aren't defined,
71 // but we can apply this to binaries.
Ivan Lozanof1c84332019-09-20 11:00:37 -070072 flags.LinkFlags = append(flags.LinkFlags,
73 "-Wl,--gc-sections",
74 "-Wl,-z,nocopyreloc",
75 "-Wl,--no-undefined-version")
Ivan Lozanobf63d002020-10-02 10:03:23 -040076
77 if Bool(binary.Properties.Static_executable) {
78 flags.LinkFlags = append(flags.LinkFlags, "-static")
79 flags.RustFlags = append(flags.RustFlags, "-C relocation-model=static")
80 }
Ivan Lozanof1c84332019-09-20 11:00:37 -070081 }
82
Ivan Lozanoffee3342019-08-27 12:03:00 -070083 return flags
84}
85
86func (binary *binaryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
87 deps = binary.baseCompiler.compilerDeps(ctx, deps)
88
Ivan Lozanof1c84332019-09-20 11:00:37 -070089 if ctx.toolchain().Bionic() {
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +010090 deps = bionicDeps(ctx, deps, Bool(binary.Properties.Static_executable))
Ivan Lozanobf63d002020-10-02 10:03:23 -040091 if Bool(binary.Properties.Static_executable) {
92 deps.CrtBegin = "crtbegin_static"
93 } else {
94 deps.CrtBegin = "crtbegin_dynamic"
95 }
Ivan Lozanof1c84332019-09-20 11:00:37 -070096 deps.CrtEnd = "crtend_android"
97 }
98
Ivan Lozanoffee3342019-08-27 12:03:00 -070099 return deps
100}
101
102func (binary *binaryDecorator) compilerProps() []interface{} {
103 return append(binary.baseCompiler.compilerProps(),
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200104 &binary.Properties,
105 &binary.stripper.StripProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700106}
107
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400108func (binary *binaryDecorator) nativeCoverage() bool {
109 return true
110}
111
Ivan Lozanobf63d002020-10-02 10:03:23 -0400112func (binary *binaryDecorator) preferRlib() bool {
Ivan Lozanoea086132020-12-08 14:43:00 -0500113 return Bool(binary.baseCompiler.Properties.Prefer_rlib) || Bool(binary.Properties.Static_executable)
Ivan Lozanobf63d002020-10-02 10:03:23 -0400114}
115
Ivan Lozanoffee3342019-08-27 12:03:00 -0700116func (binary *binaryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
117 fileName := binary.getStem(ctx) + ctx.toolchain().ExecutableSuffix()
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400118 srcPath, _ := srcPathFromModuleSrcs(ctx, binary.baseCompiler.Properties.Srcs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700119 outputFile := android.PathForModuleOut(ctx, fileName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700120
121 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500122 flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...)
Ivan Lozano2093af22020-08-25 12:48:19 -0400123 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700124
Dan Albert06feee92021-03-19 15:06:02 -0700125 TransformSrcToBinary(ctx, srcPath, deps, flags, outputFile)
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200126
127 if binary.stripper.NeedsStrip(ctx) {
128 strippedOutputFile := android.PathForModuleOut(ctx, "stripped", fileName)
129 binary.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
130 binary.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
131 }
132
Ivan Lozanoffee3342019-08-27 12:03:00 -0700133 return outputFile
134}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400135
Liz Kammer356f7d42021-01-26 09:18:53 -0500136func (binary *binaryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Ivan Lozano042504f2020-08-18 14:31:23 -0400137 // Binaries default to dylib dependencies for device, rlib for host.
Ivan Lozanobf63d002020-10-02 10:03:23 -0400138 if binary.preferRlib() {
Ivan Lozano11200872020-09-28 11:56:30 -0400139 return rlibAutoDep
Ivan Lozano1921e802021-05-20 13:39:16 -0400140 } else if mod, ok := ctx.Module().(*Module); ok && mod.InVendor() {
141 // Vendor Rust binaries should prefer rlibs.
142 return rlibAutoDep
Ivan Lozanoea086132020-12-08 14:43:00 -0500143 } else if ctx.Device() {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700144 return dylibAutoDep
145 } else {
146 return rlibAutoDep
147 }
148}
Ivan Lozano11200872020-09-28 11:56:30 -0400149
Ivan Lozanodd055472020-09-28 13:22:45 -0400150func (binary *binaryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozanobf63d002020-10-02 10:03:23 -0400151 if binary.preferRlib() {
Ivan Lozanodd055472020-09-28 13:22:45 -0400152 return RlibLinkage
Ivan Lozano1921e802021-05-20 13:39:16 -0400153 } else if ctx.RustModule().InVendor() {
154 return RlibLinkage
Ivan Lozanodd055472020-09-28 13:22:45 -0400155 }
156 return binary.baseCompiler.stdLinkage(ctx)
Ivan Lozano11200872020-09-28 11:56:30 -0400157}
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500158
159func (binary *binaryDecorator) isDependencyRoot() bool {
160 return true
161}