blob: 1e24bebab9c6ccaad1eec8017f00f15199a1d037 [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"
Vinh Tran093a57e2023-08-24 12:10:49 -040019 "android/soong/bazel"
20 "fmt"
Ivan Lozanoffee3342019-08-27 12:03:00 -070021)
22
23func init() {
24 android.RegisterModuleType("rust_binary", RustBinaryFactory)
25 android.RegisterModuleType("rust_binary_host", RustBinaryHostFactory)
26}
27
28type BinaryCompilerProperties struct {
Ivan Lozanobf63d002020-10-02 10:03:23 -040029 // Builds this binary as a static binary. Implies prefer_rlib true.
30 //
31 // Static executables currently only support for bionic targets. Non-bionic targets will not produce a fully static
32 // binary, but will still implicitly imply prefer_rlib true.
33 Static_executable *bool `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070034}
35
Ivan Lozano21fa0a52021-11-01 09:19:45 -040036type binaryInterface interface {
37 binary() bool
38 staticallyLinked() bool
Ivan Lozano62cd0382021-11-01 10:27:54 -040039 testBinary() bool
Ivan Lozano21fa0a52021-11-01 09:19:45 -040040}
41
Ivan Lozanoffee3342019-08-27 12:03:00 -070042type binaryDecorator struct {
43 *baseCompiler
ThiƩbaud Weksteenfabaff62020-08-27 13:48:36 +020044 stripper Stripper
Ivan Lozanoffee3342019-08-27 12:03:00 -070045
Ivan Lozano8a23fa42020-06-16 10:26:57 -040046 Properties BinaryCompilerProperties
Ivan Lozanoffee3342019-08-27 12:03:00 -070047}
48
49var _ compiler = (*binaryDecorator)(nil)
50
51// rust_binary produces a binary that is runnable on a device.
52func RustBinaryFactory() android.Module {
53 module, _ := NewRustBinary(android.HostAndDeviceSupported)
54 return module.Init()
55}
56
57func RustBinaryHostFactory() android.Module {
58 module, _ := NewRustBinary(android.HostSupported)
59 return module.Init()
60}
61
62func NewRustBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
63 module := newModule(hod, android.MultilibFirst)
64
Vinh Tran093a57e2023-08-24 12:10:49 -040065 android.InitBazelModule(module)
66
Ivan Lozanoffee3342019-08-27 12:03:00 -070067 binary := &binaryDecorator{
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080068 baseCompiler: NewBaseCompiler("bin", "", InstallInSystem),
Ivan Lozanoffee3342019-08-27 12:03:00 -070069 }
70
71 module.compiler = binary
72
73 return module, binary
74}
75
Ivan Lozanoffee3342019-08-27 12:03:00 -070076func (binary *binaryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
77 flags = binary.baseCompiler.compilerFlags(ctx, flags)
Ivan Lozanof1c84332019-09-20 11:00:37 -070078
Peter Collingbournee7c71c32023-03-31 20:21:19 -070079 if ctx.Os().Linux() {
80 flags.LinkFlags = append(flags.LinkFlags, "-Wl,--gc-sections")
81 }
82
Ivan Lozanof1c84332019-09-20 11:00:37 -070083 if ctx.toolchain().Bionic() {
Ivan Lozanob2df9f82019-11-05 12:16:46 -080084 // no-undefined-version breaks dylib compilation since __rust_*alloc* functions aren't defined,
85 // but we can apply this to binaries.
Ivan Lozanof1c84332019-09-20 11:00:37 -070086 flags.LinkFlags = append(flags.LinkFlags,
Ivan Lozanof1c84332019-09-20 11:00:37 -070087 "-Wl,-z,nocopyreloc",
88 "-Wl,--no-undefined-version")
Ivan Lozanobf63d002020-10-02 10:03:23 -040089
90 if Bool(binary.Properties.Static_executable) {
91 flags.LinkFlags = append(flags.LinkFlags, "-static")
92 flags.RustFlags = append(flags.RustFlags, "-C relocation-model=static")
93 }
Ivan Lozanof1c84332019-09-20 11:00:37 -070094 }
95
Ivan Lozanoffee3342019-08-27 12:03:00 -070096 return flags
97}
98
99func (binary *binaryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
100 deps = binary.baseCompiler.compilerDeps(ctx, deps)
101
Colin Crosse32f0932022-01-23 20:48:36 -0800102 static := Bool(binary.Properties.Static_executable)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700103 if ctx.toolchain().Bionic() {
Colin Crosse32f0932022-01-23 20:48:36 -0800104 deps = bionicDeps(ctx, deps, static)
105 if static {
Colin Crossfe605e12022-01-23 20:46:16 -0800106 deps.CrtBegin = []string{"crtbegin_static"}
Ivan Lozanobf63d002020-10-02 10:03:23 -0400107 } else {
Colin Crosse32f0932022-01-23 20:48:36 -0800108 deps.CrtBegin = []string{"crtbegin_dynamic"}
109 }
110 deps.CrtEnd = []string{"crtend_android"}
111 } else if ctx.Os() == android.LinuxMusl {
112 deps = muslDeps(ctx, deps, static)
113 if static {
114 deps.CrtBegin = []string{"libc_musl_crtbegin_static"}
115 } else {
Colin Cross9382c062022-09-30 15:11:47 -0700116 deps.CrtBegin = []string{"libc_musl_crtbegin_dynamic"}
Ivan Lozanobf63d002020-10-02 10:03:23 -0400117 }
Colin Crossfe605e12022-01-23 20:46:16 -0800118 deps.CrtEnd = []string{"libc_musl_crtend"}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700119 }
120
Ivan Lozanoffee3342019-08-27 12:03:00 -0700121 return deps
122}
123
124func (binary *binaryDecorator) compilerProps() []interface{} {
125 return append(binary.baseCompiler.compilerProps(),
ThiƩbaud Weksteenfabaff62020-08-27 13:48:36 +0200126 &binary.Properties,
127 &binary.stripper.StripProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700128}
129
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400130func (binary *binaryDecorator) nativeCoverage() bool {
131 return true
132}
133
Ivan Lozanobf63d002020-10-02 10:03:23 -0400134func (binary *binaryDecorator) preferRlib() bool {
Ivan Lozanoea086132020-12-08 14:43:00 -0500135 return Bool(binary.baseCompiler.Properties.Prefer_rlib) || Bool(binary.Properties.Static_executable)
Ivan Lozanobf63d002020-10-02 10:03:23 -0400136}
137
Sasha Smundaka76acba2022-04-18 20:12:56 -0700138func (binary *binaryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700139 fileName := binary.getStem(ctx) + ctx.toolchain().ExecutableSuffix()
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400140 srcPath, _ := srcPathFromModuleSrcs(ctx, binary.baseCompiler.Properties.Srcs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700141 outputFile := android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700142 ret := buildOutput{outputFile: outputFile}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700143
144 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500145 flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...)
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700146 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects.Strings()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700147
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400148 if binary.stripper.NeedsStrip(ctx) {
149 strippedOutputFile := outputFile
150 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
151 binary.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
152
153 binary.baseCompiler.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
154 }
155 binary.baseCompiler.unstrippedOutputFile = outputFile
156
Sasha Smundaka76acba2022-04-18 20:12:56 -0700157 ret.kytheFile = TransformSrcToBinary(ctx, srcPath, deps, flags, outputFile).kytheFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400158 return ret
Ivan Lozanoffee3342019-08-27 12:03:00 -0700159}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400160
Liz Kammer356f7d42021-01-26 09:18:53 -0500161func (binary *binaryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Ivan Lozano042504f2020-08-18 14:31:23 -0400162 // Binaries default to dylib dependencies for device, rlib for host.
Ivan Lozanobf63d002020-10-02 10:03:23 -0400163 if binary.preferRlib() {
Ivan Lozano11200872020-09-28 11:56:30 -0400164 return rlibAutoDep
Ivan Lozanoea086132020-12-08 14:43:00 -0500165 } else if ctx.Device() {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700166 return dylibAutoDep
167 } else {
168 return rlibAutoDep
169 }
170}
Ivan Lozano11200872020-09-28 11:56:30 -0400171
Ivan Lozanodd055472020-09-28 13:22:45 -0400172func (binary *binaryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozanobf63d002020-10-02 10:03:23 -0400173 if binary.preferRlib() {
Ivan Lozanodd055472020-09-28 13:22:45 -0400174 return RlibLinkage
175 }
176 return binary.baseCompiler.stdLinkage(ctx)
Ivan Lozano11200872020-09-28 11:56:30 -0400177}
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400178
179func (binary *binaryDecorator) binary() bool {
180 return true
181}
182
183func (binary *binaryDecorator) staticallyLinked() bool {
184 return Bool(binary.Properties.Static_executable)
185}
Ivan Lozano62cd0382021-11-01 10:27:54 -0400186
187func (binary *binaryDecorator) testBinary() bool {
188 return false
189}
Vinh Tran093a57e2023-08-24 12:10:49 -0400190
191type rustBinaryLibraryAttributes struct {
192 Srcs bazel.LabelListAttribute
193 Compile_data bazel.LabelListAttribute
194 Crate_name bazel.StringAttribute
195 Edition bazel.StringAttribute
196 Crate_features bazel.StringListAttribute
197 Deps bazel.LabelListAttribute
198 Proc_macro_deps bazel.LabelListAttribute
199 Rustc_flags bazel.StringListAttribute
200}
201
202func binaryBp2build(ctx android.TopDownMutatorContext, m *Module) {
203 binary := m.compiler.(*binaryDecorator)
204
205 var srcs bazel.LabelList
206 var compileData bazel.LabelList
207
208 if binary.baseCompiler.Properties.Srcs[0] == "src/main.rs" {
209 srcs = android.BazelLabelForModuleSrc(ctx, []string{"src/**/*.rs"})
210 compileData = android.BazelLabelForModuleSrc(
211 ctx,
212 []string{
213 "src/**/*.proto",
214 "examples/**/*.rs",
215 "**/*.md",
216 "templates/**/*.template",
217 },
218 )
219 } else {
220 srcs = android.BazelLabelForModuleSrc(ctx, binary.baseCompiler.Properties.Srcs)
221 }
222
223 deps := android.BazelLabelForModuleDeps(ctx, append(
224 binary.baseCompiler.Properties.Rustlibs,
225 ))
226
227 procMacroDeps := android.BazelLabelForModuleDeps(ctx, binary.baseCompiler.Properties.Proc_macros)
228
229 var rustcFLags []string
230 for _, cfg := range binary.baseCompiler.Properties.Cfgs {
231 rustcFLags = append(rustcFLags, fmt.Sprintf("--cfg=%s", cfg))
232 }
233
234 attrs := &rustBinaryLibraryAttributes{
235 Srcs: bazel.MakeLabelListAttribute(
236 srcs,
237 ),
238 Compile_data: bazel.MakeLabelListAttribute(
239 compileData,
240 ),
241 Crate_name: bazel.StringAttribute{
242 Value: &binary.baseCompiler.Properties.Crate_name,
243 },
244 Edition: bazel.StringAttribute{
245 Value: binary.baseCompiler.Properties.Edition,
246 },
247 Crate_features: bazel.StringListAttribute{
248 Value: binary.baseCompiler.Properties.Features,
249 },
250 Deps: bazel.MakeLabelListAttribute(
251 deps,
252 ),
253 Proc_macro_deps: bazel.MakeLabelListAttribute(
254 procMacroDeps,
255 ),
256 Rustc_flags: bazel.StringListAttribute{
257 Value: append(
258 rustcFLags,
259 binary.baseCompiler.Properties.Flags...,
260 ),
261 },
262 }
263
264 ctx.CreateBazelTargetModule(
265 bazel.BazelTargetModuleProperties{
266 Rule_class: "rust_binary",
267 Bzl_load_location: "@rules_rust//rust:defs.bzl",
268 },
269 android.CommonAttributes{
270 Name: m.Name(),
271 },
272 attrs,
273 )
274}