blob: cba2f7232fb5368b97a700c0bbb8c4580c335447 [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
37}
38
Ivan Lozanoffee3342019-08-27 12:03:00 -070039type binaryDecorator struct {
40 *baseCompiler
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +020041 stripper Stripper
Ivan Lozanoffee3342019-08-27 12:03:00 -070042
Ivan Lozano8a23fa42020-06-16 10:26:57 -040043 Properties BinaryCompilerProperties
Ivan Lozanoffee3342019-08-27 12:03:00 -070044}
45
46var _ compiler = (*binaryDecorator)(nil)
47
48// rust_binary produces a binary that is runnable on a device.
49func RustBinaryFactory() android.Module {
50 module, _ := NewRustBinary(android.HostAndDeviceSupported)
51 return module.Init()
52}
53
54func RustBinaryHostFactory() android.Module {
55 module, _ := NewRustBinary(android.HostSupported)
56 return module.Init()
57}
58
59func NewRustBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
60 module := newModule(hod, android.MultilibFirst)
61
62 binary := &binaryDecorator{
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080063 baseCompiler: NewBaseCompiler("bin", "", InstallInSystem),
Ivan Lozanoffee3342019-08-27 12:03:00 -070064 }
65
66 module.compiler = binary
67
68 return module, binary
69}
70
Ivan Lozanoffee3342019-08-27 12:03:00 -070071func (binary *binaryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
72 flags = binary.baseCompiler.compilerFlags(ctx, flags)
Ivan Lozanof1c84332019-09-20 11:00:37 -070073
74 if ctx.toolchain().Bionic() {
Ivan Lozanob2df9f82019-11-05 12:16:46 -080075 // no-undefined-version breaks dylib compilation since __rust_*alloc* functions aren't defined,
76 // but we can apply this to binaries.
Ivan Lozanof1c84332019-09-20 11:00:37 -070077 flags.LinkFlags = append(flags.LinkFlags,
78 "-Wl,--gc-sections",
79 "-Wl,-z,nocopyreloc",
80 "-Wl,--no-undefined-version")
Ivan Lozanobf63d002020-10-02 10:03:23 -040081
82 if Bool(binary.Properties.Static_executable) {
83 flags.LinkFlags = append(flags.LinkFlags, "-static")
84 flags.RustFlags = append(flags.RustFlags, "-C relocation-model=static")
85 }
Ivan Lozanof1c84332019-09-20 11:00:37 -070086 }
87
Ivan Lozanoffee3342019-08-27 12:03:00 -070088 return flags
89}
90
91func (binary *binaryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
92 deps = binary.baseCompiler.compilerDeps(ctx, deps)
93
Ivan Lozanof1c84332019-09-20 11:00:37 -070094 if ctx.toolchain().Bionic() {
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +010095 deps = bionicDeps(ctx, deps, Bool(binary.Properties.Static_executable))
Ivan Lozanobf63d002020-10-02 10:03:23 -040096 if Bool(binary.Properties.Static_executable) {
97 deps.CrtBegin = "crtbegin_static"
98 } else {
99 deps.CrtBegin = "crtbegin_dynamic"
100 }
Ivan Lozanof1c84332019-09-20 11:00:37 -0700101 deps.CrtEnd = "crtend_android"
102 }
103
Ivan Lozanoffee3342019-08-27 12:03:00 -0700104 return deps
105}
106
107func (binary *binaryDecorator) compilerProps() []interface{} {
108 return append(binary.baseCompiler.compilerProps(),
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200109 &binary.Properties,
110 &binary.stripper.StripProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700111}
112
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400113func (binary *binaryDecorator) nativeCoverage() bool {
114 return true
115}
116
Ivan Lozanobf63d002020-10-02 10:03:23 -0400117func (binary *binaryDecorator) preferRlib() bool {
Ivan Lozanoea086132020-12-08 14:43:00 -0500118 return Bool(binary.baseCompiler.Properties.Prefer_rlib) || Bool(binary.Properties.Static_executable)
Ivan Lozanobf63d002020-10-02 10:03:23 -0400119}
120
Ivan Lozanoffee3342019-08-27 12:03:00 -0700121func (binary *binaryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
122 fileName := binary.getStem(ctx) + ctx.toolchain().ExecutableSuffix()
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400123 srcPath, _ := srcPathFromModuleSrcs(ctx, binary.baseCompiler.Properties.Srcs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700124 outputFile := android.PathForModuleOut(ctx, fileName)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400125 ret := outputFile
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
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400131 if binary.stripper.NeedsStrip(ctx) {
132 strippedOutputFile := outputFile
133 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
134 binary.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
135
136 binary.baseCompiler.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
137 }
138 binary.baseCompiler.unstrippedOutputFile = outputFile
139
Dan Albert06feee92021-03-19 15:06:02 -0700140 TransformSrcToBinary(ctx, srcPath, deps, flags, outputFile)
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200141
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400142 return ret
Ivan Lozanoffee3342019-08-27 12:03:00 -0700143}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400144
Liz Kammer356f7d42021-01-26 09:18:53 -0500145func (binary *binaryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Ivan Lozano042504f2020-08-18 14:31:23 -0400146 // Binaries default to dylib dependencies for device, rlib for host.
Ivan Lozanobf63d002020-10-02 10:03:23 -0400147 if binary.preferRlib() {
Ivan Lozano11200872020-09-28 11:56:30 -0400148 return rlibAutoDep
Ivan Lozano1921e802021-05-20 13:39:16 -0400149 } else if mod, ok := ctx.Module().(*Module); ok && mod.InVendor() {
150 // Vendor Rust binaries should prefer rlibs.
151 return rlibAutoDep
Ivan Lozanoea086132020-12-08 14:43:00 -0500152 } else if ctx.Device() {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700153 return dylibAutoDep
154 } else {
155 return rlibAutoDep
156 }
157}
Ivan Lozano11200872020-09-28 11:56:30 -0400158
Ivan Lozanodd055472020-09-28 13:22:45 -0400159func (binary *binaryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozanobf63d002020-10-02 10:03:23 -0400160 if binary.preferRlib() {
Ivan Lozanodd055472020-09-28 13:22:45 -0400161 return RlibLinkage
Ivan Lozano1921e802021-05-20 13:39:16 -0400162 } else if ctx.RustModule().InVendor() {
163 return RlibLinkage
Ivan Lozanodd055472020-09-28 13:22:45 -0400164 }
165 return binary.baseCompiler.stdLinkage(ctx)
Ivan Lozano11200872020-09-28 11:56:30 -0400166}
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400167
168func (binary *binaryDecorator) binary() bool {
169 return true
170}
171
172func (binary *binaryDecorator) staticallyLinked() bool {
173 return Bool(binary.Properties.Static_executable)
174}