blob: 6854e62dedc8a0a0a83546a073e363636caa3311 [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
Ivan Lozano21fa0a52021-11-01 09:19:45 -040034type binaryInterface interface {
35 binary() bool
36 staticallyLinked() bool
Ivan Lozano62cd0382021-11-01 10:27:54 -040037 testBinary() bool
Ivan Lozano21fa0a52021-11-01 09:19:45 -040038}
39
Ivan Lozanoffee3342019-08-27 12:03:00 -070040type binaryDecorator struct {
41 *baseCompiler
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +020042 stripper Stripper
Ivan Lozanoffee3342019-08-27 12:03:00 -070043
Ivan Lozano8a23fa42020-06-16 10:26:57 -040044 Properties BinaryCompilerProperties
Ivan Lozanoffee3342019-08-27 12:03:00 -070045}
46
47var _ compiler = (*binaryDecorator)(nil)
48
49// rust_binary produces a binary that is runnable on a device.
50func RustBinaryFactory() android.Module {
51 module, _ := NewRustBinary(android.HostAndDeviceSupported)
52 return module.Init()
53}
54
55func RustBinaryHostFactory() android.Module {
56 module, _ := NewRustBinary(android.HostSupported)
57 return module.Init()
58}
59
60func NewRustBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
61 module := newModule(hod, android.MultilibFirst)
62
63 binary := &binaryDecorator{
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080064 baseCompiler: NewBaseCompiler("bin", "", InstallInSystem),
Ivan Lozanoffee3342019-08-27 12:03:00 -070065 }
66
67 module.compiler = binary
68
69 return module, binary
70}
71
Ivan Lozanoffee3342019-08-27 12:03:00 -070072func (binary *binaryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
73 flags = binary.baseCompiler.compilerFlags(ctx, flags)
Ivan Lozanof1c84332019-09-20 11:00:37 -070074
75 if ctx.toolchain().Bionic() {
Ivan Lozanob2df9f82019-11-05 12:16:46 -080076 // no-undefined-version breaks dylib compilation since __rust_*alloc* functions aren't defined,
77 // but we can apply this to binaries.
Ivan Lozanof1c84332019-09-20 11:00:37 -070078 flags.LinkFlags = append(flags.LinkFlags,
79 "-Wl,--gc-sections",
80 "-Wl,-z,nocopyreloc",
81 "-Wl,--no-undefined-version")
Ivan Lozanobf63d002020-10-02 10:03:23 -040082
83 if Bool(binary.Properties.Static_executable) {
84 flags.LinkFlags = append(flags.LinkFlags, "-static")
85 flags.RustFlags = append(flags.RustFlags, "-C relocation-model=static")
86 }
Ivan Lozanof1c84332019-09-20 11:00:37 -070087 }
88
Ivan Lozanoffee3342019-08-27 12:03:00 -070089 return flags
90}
91
92func (binary *binaryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
93 deps = binary.baseCompiler.compilerDeps(ctx, deps)
94
Ivan Lozanof1c84332019-09-20 11:00:37 -070095 if ctx.toolchain().Bionic() {
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +010096 deps = bionicDeps(ctx, deps, Bool(binary.Properties.Static_executable))
Ivan Lozanobf63d002020-10-02 10:03:23 -040097 if Bool(binary.Properties.Static_executable) {
98 deps.CrtBegin = "crtbegin_static"
99 } else {
100 deps.CrtBegin = "crtbegin_dynamic"
101 }
Ivan Lozanof1c84332019-09-20 11:00:37 -0700102 deps.CrtEnd = "crtend_android"
103 }
104
Ivan Lozanoffee3342019-08-27 12:03:00 -0700105 return deps
106}
107
108func (binary *binaryDecorator) compilerProps() []interface{} {
109 return append(binary.baseCompiler.compilerProps(),
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200110 &binary.Properties,
111 &binary.stripper.StripProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700112}
113
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400114func (binary *binaryDecorator) nativeCoverage() bool {
115 return true
116}
117
Ivan Lozanobf63d002020-10-02 10:03:23 -0400118func (binary *binaryDecorator) preferRlib() bool {
Ivan Lozanoea086132020-12-08 14:43:00 -0500119 return Bool(binary.baseCompiler.Properties.Prefer_rlib) || Bool(binary.Properties.Static_executable)
Ivan Lozanobf63d002020-10-02 10:03:23 -0400120}
121
Ivan Lozanoffee3342019-08-27 12:03:00 -0700122func (binary *binaryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
123 fileName := binary.getStem(ctx) + ctx.toolchain().ExecutableSuffix()
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400124 srcPath, _ := srcPathFromModuleSrcs(ctx, binary.baseCompiler.Properties.Srcs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700125 outputFile := android.PathForModuleOut(ctx, fileName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700126
127 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500128 flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...)
Ivan Lozano2093af22020-08-25 12:48:19 -0400129 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700130
Dan Albert06feee92021-03-19 15:06:02 -0700131 TransformSrcToBinary(ctx, srcPath, deps, flags, outputFile)
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200132
133 if binary.stripper.NeedsStrip(ctx) {
134 strippedOutputFile := android.PathForModuleOut(ctx, "stripped", fileName)
135 binary.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
136 binary.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
137 }
138
Ivan Lozanoffee3342019-08-27 12:03:00 -0700139 return outputFile
140}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400141
Liz Kammer356f7d42021-01-26 09:18:53 -0500142func (binary *binaryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Ivan Lozano042504f2020-08-18 14:31:23 -0400143 // Binaries default to dylib dependencies for device, rlib for host.
Ivan Lozanobf63d002020-10-02 10:03:23 -0400144 if binary.preferRlib() {
Ivan Lozano11200872020-09-28 11:56:30 -0400145 return rlibAutoDep
Ivan Lozano1921e802021-05-20 13:39:16 -0400146 } else if mod, ok := ctx.Module().(*Module); ok && mod.InVendor() {
147 // Vendor Rust binaries should prefer rlibs.
148 return rlibAutoDep
Ivan Lozanoea086132020-12-08 14:43:00 -0500149 } else if ctx.Device() {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700150 return dylibAutoDep
151 } else {
152 return rlibAutoDep
153 }
154}
Ivan Lozano11200872020-09-28 11:56:30 -0400155
Ivan Lozanodd055472020-09-28 13:22:45 -0400156func (binary *binaryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozanobf63d002020-10-02 10:03:23 -0400157 if binary.preferRlib() {
Ivan Lozanodd055472020-09-28 13:22:45 -0400158 return RlibLinkage
Ivan Lozano1921e802021-05-20 13:39:16 -0400159 } else if ctx.RustModule().InVendor() {
160 return RlibLinkage
Ivan Lozanodd055472020-09-28 13:22:45 -0400161 }
162 return binary.baseCompiler.stdLinkage(ctx)
Ivan Lozano11200872020-09-28 11:56:30 -0400163}
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400164
165func (binary *binaryDecorator) binary() bool {
166 return true
167}
168
169func (binary *binaryDecorator) staticallyLinked() bool {
170 return Bool(binary.Properties.Static_executable)
171}
Ivan Lozano62cd0382021-11-01 10:27:54 -0400172
173func (binary *binaryDecorator) testBinary() bool {
174 return false
175}