blob: 056888ef243d6f1d851fec24adb0ec052b86bff4 [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
Colin Crosse32f0932022-01-23 20:48:36 -080095 static := Bool(binary.Properties.Static_executable)
Ivan Lozanof1c84332019-09-20 11:00:37 -070096 if ctx.toolchain().Bionic() {
Colin Crosse32f0932022-01-23 20:48:36 -080097 deps = bionicDeps(ctx, deps, static)
98 if static {
Colin Crossfe605e12022-01-23 20:46:16 -080099 deps.CrtBegin = []string{"crtbegin_static"}
Ivan Lozanobf63d002020-10-02 10:03:23 -0400100 } else {
Colin Crosse32f0932022-01-23 20:48:36 -0800101 deps.CrtBegin = []string{"crtbegin_dynamic"}
102 }
103 deps.CrtEnd = []string{"crtend_android"}
104 } else if ctx.Os() == android.LinuxMusl {
105 deps = muslDeps(ctx, deps, static)
106 if static {
107 deps.CrtBegin = []string{"libc_musl_crtbegin_static"}
108 } else {
Colin Cross9382c062022-09-30 15:11:47 -0700109 deps.CrtBegin = []string{"libc_musl_crtbegin_dynamic"}
Ivan Lozanobf63d002020-10-02 10:03:23 -0400110 }
Colin Crossfe605e12022-01-23 20:46:16 -0800111 deps.CrtEnd = []string{"libc_musl_crtend"}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700112 }
113
Ivan Lozanoffee3342019-08-27 12:03:00 -0700114 return deps
115}
116
117func (binary *binaryDecorator) compilerProps() []interface{} {
118 return append(binary.baseCompiler.compilerProps(),
ThiƩbaud Weksteenfabaff62020-08-27 13:48:36 +0200119 &binary.Properties,
120 &binary.stripper.StripProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700121}
122
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400123func (binary *binaryDecorator) nativeCoverage() bool {
124 return true
125}
126
Ivan Lozanobf63d002020-10-02 10:03:23 -0400127func (binary *binaryDecorator) preferRlib() bool {
Ivan Lozanoea086132020-12-08 14:43:00 -0500128 return Bool(binary.baseCompiler.Properties.Prefer_rlib) || Bool(binary.Properties.Static_executable)
Ivan Lozanobf63d002020-10-02 10:03:23 -0400129}
130
Sasha Smundaka76acba2022-04-18 20:12:56 -0700131func (binary *binaryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700132 fileName := binary.getStem(ctx) + ctx.toolchain().ExecutableSuffix()
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400133 srcPath, _ := srcPathFromModuleSrcs(ctx, binary.baseCompiler.Properties.Srcs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700134 outputFile := android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700135 ret := buildOutput{outputFile: outputFile}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700136
137 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500138 flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...)
Ivan Lozano2093af22020-08-25 12:48:19 -0400139 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700140
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400141 if binary.stripper.NeedsStrip(ctx) {
142 strippedOutputFile := outputFile
143 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
144 binary.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
145
146 binary.baseCompiler.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
147 }
148 binary.baseCompiler.unstrippedOutputFile = outputFile
149
Sasha Smundaka76acba2022-04-18 20:12:56 -0700150 ret.kytheFile = TransformSrcToBinary(ctx, srcPath, deps, flags, outputFile).kytheFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400151 return ret
Ivan Lozanoffee3342019-08-27 12:03:00 -0700152}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400153
Liz Kammer356f7d42021-01-26 09:18:53 -0500154func (binary *binaryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Ivan Lozano042504f2020-08-18 14:31:23 -0400155 // Binaries default to dylib dependencies for device, rlib for host.
Ivan Lozanobf63d002020-10-02 10:03:23 -0400156 if binary.preferRlib() {
Ivan Lozano11200872020-09-28 11:56:30 -0400157 return rlibAutoDep
Ivan Lozano1921e802021-05-20 13:39:16 -0400158 } else if mod, ok := ctx.Module().(*Module); ok && mod.InVendor() {
159 // Vendor Rust binaries should prefer rlibs.
160 return rlibAutoDep
Ivan Lozanoea086132020-12-08 14:43:00 -0500161 } else if ctx.Device() {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700162 return dylibAutoDep
163 } else {
164 return rlibAutoDep
165 }
166}
Ivan Lozano11200872020-09-28 11:56:30 -0400167
Ivan Lozanodd055472020-09-28 13:22:45 -0400168func (binary *binaryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozanobf63d002020-10-02 10:03:23 -0400169 if binary.preferRlib() {
Ivan Lozanodd055472020-09-28 13:22:45 -0400170 return RlibLinkage
Ivan Lozano1921e802021-05-20 13:39:16 -0400171 } else if ctx.RustModule().InVendor() {
172 return RlibLinkage
Ivan Lozanodd055472020-09-28 13:22:45 -0400173 }
174 return binary.baseCompiler.stdLinkage(ctx)
Ivan Lozano11200872020-09-28 11:56:30 -0400175}
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400176
177func (binary *binaryDecorator) binary() bool {
178 return true
179}
180
181func (binary *binaryDecorator) staticallyLinked() bool {
182 return Bool(binary.Properties.Static_executable)
183}
Ivan Lozano62cd0382021-11-01 10:27:54 -0400184
185func (binary *binaryDecorator) testBinary() bool {
186 return false
187}