blob: 1408ff17be502b0c186585f2b25823fb962447fc [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 (
Chris Parsons637458d2023-09-19 20:09:00 +000018 "fmt"
19
Ivan Lozanoffee3342019-08-27 12:03:00 -070020 "android/soong/android"
Vinh Tran093a57e2023-08-24 12:10:49 -040021 "android/soong/bazel"
Ivan Lozanoffee3342019-08-27 12:03:00 -070022)
23
24func init() {
25 android.RegisterModuleType("rust_binary", RustBinaryFactory)
26 android.RegisterModuleType("rust_binary_host", RustBinaryHostFactory)
27}
28
29type BinaryCompilerProperties struct {
Ivan Lozanobf63d002020-10-02 10:03:23 -040030 // Builds this binary as a static binary. Implies prefer_rlib true.
31 //
32 // Static executables currently only support for bionic targets. Non-bionic targets will not produce a fully static
33 // binary, but will still implicitly imply prefer_rlib true.
34 Static_executable *bool `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070035}
36
Ivan Lozano21fa0a52021-11-01 09:19:45 -040037type binaryInterface interface {
38 binary() bool
39 staticallyLinked() bool
Ivan Lozano62cd0382021-11-01 10:27:54 -040040 testBinary() bool
Ivan Lozano21fa0a52021-11-01 09:19:45 -040041}
42
Ivan Lozanoffee3342019-08-27 12:03:00 -070043type binaryDecorator struct {
44 *baseCompiler
ThiƩbaud Weksteenfabaff62020-08-27 13:48:36 +020045 stripper Stripper
Ivan Lozanoffee3342019-08-27 12:03:00 -070046
Ivan Lozano8a23fa42020-06-16 10:26:57 -040047 Properties BinaryCompilerProperties
Ivan Lozanoffee3342019-08-27 12:03:00 -070048}
49
50var _ compiler = (*binaryDecorator)(nil)
51
52// rust_binary produces a binary that is runnable on a device.
53func RustBinaryFactory() android.Module {
54 module, _ := NewRustBinary(android.HostAndDeviceSupported)
55 return module.Init()
56}
57
58func RustBinaryHostFactory() android.Module {
59 module, _ := NewRustBinary(android.HostSupported)
60 return module.Init()
61}
62
63func NewRustBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
64 module := newModule(hod, android.MultilibFirst)
65
Vinh Tran093a57e2023-08-24 12:10:49 -040066 android.InitBazelModule(module)
67
Ivan Lozanoffee3342019-08-27 12:03:00 -070068 binary := &binaryDecorator{
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080069 baseCompiler: NewBaseCompiler("bin", "", InstallInSystem),
Ivan Lozanoffee3342019-08-27 12:03:00 -070070 }
71
72 module.compiler = binary
73
74 return module, binary
75}
76
Ivan Lozanoffee3342019-08-27 12:03:00 -070077func (binary *binaryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
78 flags = binary.baseCompiler.compilerFlags(ctx, flags)
Ivan Lozanof1c84332019-09-20 11:00:37 -070079
Peter Collingbournee7c71c32023-03-31 20:21:19 -070080 if ctx.Os().Linux() {
81 flags.LinkFlags = append(flags.LinkFlags, "-Wl,--gc-sections")
82 }
83
Ivan Lozanof1c84332019-09-20 11:00:37 -070084 if ctx.toolchain().Bionic() {
Ivan Lozanob2df9f82019-11-05 12:16:46 -080085 // no-undefined-version breaks dylib compilation since __rust_*alloc* functions aren't defined,
86 // but we can apply this to binaries.
Ivan Lozanof1c84332019-09-20 11:00:37 -070087 flags.LinkFlags = append(flags.LinkFlags,
Ivan Lozanof1c84332019-09-20 11:00:37 -070088 "-Wl,-z,nocopyreloc",
89 "-Wl,--no-undefined-version")
Ivan Lozanobf63d002020-10-02 10:03:23 -040090
91 if Bool(binary.Properties.Static_executable) {
92 flags.LinkFlags = append(flags.LinkFlags, "-static")
93 flags.RustFlags = append(flags.RustFlags, "-C relocation-model=static")
94 }
Ivan Lozanof1c84332019-09-20 11:00:37 -070095 }
96
Ivan Lozanoffee3342019-08-27 12:03:00 -070097 return flags
98}
99
100func (binary *binaryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
101 deps = binary.baseCompiler.compilerDeps(ctx, deps)
102
Colin Crosse32f0932022-01-23 20:48:36 -0800103 static := Bool(binary.Properties.Static_executable)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700104 if ctx.toolchain().Bionic() {
Colin Crosse32f0932022-01-23 20:48:36 -0800105 deps = bionicDeps(ctx, deps, static)
106 if static {
Colin Crossfe605e12022-01-23 20:46:16 -0800107 deps.CrtBegin = []string{"crtbegin_static"}
Ivan Lozanobf63d002020-10-02 10:03:23 -0400108 } else {
Colin Crosse32f0932022-01-23 20:48:36 -0800109 deps.CrtBegin = []string{"crtbegin_dynamic"}
110 }
111 deps.CrtEnd = []string{"crtend_android"}
112 } else if ctx.Os() == android.LinuxMusl {
113 deps = muslDeps(ctx, deps, static)
114 if static {
115 deps.CrtBegin = []string{"libc_musl_crtbegin_static"}
116 } else {
Colin Cross9382c062022-09-30 15:11:47 -0700117 deps.CrtBegin = []string{"libc_musl_crtbegin_dynamic"}
Ivan Lozanobf63d002020-10-02 10:03:23 -0400118 }
Colin Crossfe605e12022-01-23 20:46:16 -0800119 deps.CrtEnd = []string{"libc_musl_crtend"}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700120 }
121
Ivan Lozanoffee3342019-08-27 12:03:00 -0700122 return deps
123}
124
125func (binary *binaryDecorator) compilerProps() []interface{} {
126 return append(binary.baseCompiler.compilerProps(),
ThiƩbaud Weksteenfabaff62020-08-27 13:48:36 +0200127 &binary.Properties,
128 &binary.stripper.StripProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700129}
130
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400131func (binary *binaryDecorator) nativeCoverage() bool {
132 return true
133}
134
Ivan Lozanobf63d002020-10-02 10:03:23 -0400135func (binary *binaryDecorator) preferRlib() bool {
Ivan Lozanoea086132020-12-08 14:43:00 -0500136 return Bool(binary.baseCompiler.Properties.Prefer_rlib) || Bool(binary.Properties.Static_executable)
Ivan Lozanobf63d002020-10-02 10:03:23 -0400137}
138
Sasha Smundaka76acba2022-04-18 20:12:56 -0700139func (binary *binaryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700140 fileName := binary.getStem(ctx) + ctx.toolchain().ExecutableSuffix()
Wen-yi Chu9c642742023-09-22 03:58:59 +0000141 srcPath, _ := srcPathFromModuleSrcs(ctx, binary.baseCompiler.Properties.Srcs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700142 outputFile := android.PathForModuleOut(ctx, fileName)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700143 ret := buildOutput{outputFile: outputFile}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700144
145 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500146 flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...)
Wen-yi Chu41326c12023-09-22 03:58:59 +0000147 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects.Strings()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700148
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400149 if binary.stripper.NeedsStrip(ctx) {
150 strippedOutputFile := outputFile
151 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
152 binary.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
153
154 binary.baseCompiler.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
155 }
156 binary.baseCompiler.unstrippedOutputFile = outputFile
157
Wen-yi Chu9c642742023-09-22 03:58:59 +0000158 ret.kytheFile = TransformSrcToBinary(ctx, srcPath, deps, flags, outputFile).kytheFile
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400159 return ret
Ivan Lozanoffee3342019-08-27 12:03:00 -0700160}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400161
Liz Kammer356f7d42021-01-26 09:18:53 -0500162func (binary *binaryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Ivan Lozano042504f2020-08-18 14:31:23 -0400163 // Binaries default to dylib dependencies for device, rlib for host.
Ivan Lozanobf63d002020-10-02 10:03:23 -0400164 if binary.preferRlib() {
Ivan Lozano11200872020-09-28 11:56:30 -0400165 return rlibAutoDep
Ivan Lozanoea086132020-12-08 14:43:00 -0500166 } else if ctx.Device() {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700167 return dylibAutoDep
168 } else {
169 return rlibAutoDep
170 }
171}
Ivan Lozano11200872020-09-28 11:56:30 -0400172
Ivan Lozanodd055472020-09-28 13:22:45 -0400173func (binary *binaryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozanobf63d002020-10-02 10:03:23 -0400174 if binary.preferRlib() {
Ivan Lozanodd055472020-09-28 13:22:45 -0400175 return RlibLinkage
176 }
177 return binary.baseCompiler.stdLinkage(ctx)
Ivan Lozano11200872020-09-28 11:56:30 -0400178}
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400179
180func (binary *binaryDecorator) binary() bool {
181 return true
182}
183
184func (binary *binaryDecorator) staticallyLinked() bool {
185 return Bool(binary.Properties.Static_executable)
186}
Ivan Lozano62cd0382021-11-01 10:27:54 -0400187
188func (binary *binaryDecorator) testBinary() bool {
189 return false
190}
Vinh Tran093a57e2023-08-24 12:10:49 -0400191
192type rustBinaryLibraryAttributes struct {
193 Srcs bazel.LabelListAttribute
194 Compile_data bazel.LabelListAttribute
195 Crate_name bazel.StringAttribute
196 Edition bazel.StringAttribute
197 Crate_features bazel.StringListAttribute
198 Deps bazel.LabelListAttribute
199 Proc_macro_deps bazel.LabelListAttribute
200 Rustc_flags bazel.StringListAttribute
201}
202
Chris Parsons637458d2023-09-19 20:09:00 +0000203func binaryBp2build(ctx android.Bp2buildMutatorContext, m *Module) {
Vinh Tran093a57e2023-08-24 12:10:49 -0400204 binary := m.compiler.(*binaryDecorator)
205
206 var srcs bazel.LabelList
207 var compileData bazel.LabelList
208
209 if binary.baseCompiler.Properties.Srcs[0] == "src/main.rs" {
210 srcs = android.BazelLabelForModuleSrc(ctx, []string{"src/**/*.rs"})
211 compileData = android.BazelLabelForModuleSrc(
212 ctx,
213 []string{
214 "src/**/*.proto",
215 "examples/**/*.rs",
216 "**/*.md",
217 "templates/**/*.template",
218 },
219 )
220 } else {
221 srcs = android.BazelLabelForModuleSrc(ctx, binary.baseCompiler.Properties.Srcs)
222 }
223
224 deps := android.BazelLabelForModuleDeps(ctx, append(
225 binary.baseCompiler.Properties.Rustlibs,
226 ))
227
228 procMacroDeps := android.BazelLabelForModuleDeps(ctx, binary.baseCompiler.Properties.Proc_macros)
229
230 var rustcFLags []string
231 for _, cfg := range binary.baseCompiler.Properties.Cfgs {
232 rustcFLags = append(rustcFLags, fmt.Sprintf("--cfg=%s", cfg))
233 }
234
235 attrs := &rustBinaryLibraryAttributes{
236 Srcs: bazel.MakeLabelListAttribute(
237 srcs,
238 ),
239 Compile_data: bazel.MakeLabelListAttribute(
240 compileData,
241 ),
242 Crate_name: bazel.StringAttribute{
243 Value: &binary.baseCompiler.Properties.Crate_name,
244 },
245 Edition: bazel.StringAttribute{
246 Value: binary.baseCompiler.Properties.Edition,
247 },
248 Crate_features: bazel.StringListAttribute{
249 Value: binary.baseCompiler.Properties.Features,
250 },
251 Deps: bazel.MakeLabelListAttribute(
252 deps,
253 ),
254 Proc_macro_deps: bazel.MakeLabelListAttribute(
255 procMacroDeps,
256 ),
257 Rustc_flags: bazel.StringListAttribute{
258 Value: append(
259 rustcFLags,
260 binary.baseCompiler.Properties.Flags...,
261 ),
262 },
263 }
264
265 ctx.CreateBazelTargetModule(
266 bazel.BazelTargetModuleProperties{
267 Rule_class: "rust_binary",
268 Bzl_load_location: "@rules_rust//rust:defs.bzl",
269 },
270 android.CommonAttributes{
271 Name: m.Name(),
272 },
273 attrs,
274 )
275}