1// 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"
19)
20
21func init() {
22	android.RegisterModuleType("rust_binary", RustBinaryFactory)
23	android.RegisterModuleType("rust_binary_host", RustBinaryHostFactory)
24}
25
26type BinaryCompilerProperties struct {
27	// 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"`
32}
33
34type binaryInterface interface {
35	binary() bool
36	staticallyLinked() bool
37	testBinary() bool
38}
39
40type binaryDecorator struct {
41	*baseCompiler
42	stripper Stripper
43
44	Properties BinaryCompilerProperties
45}
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{
64		baseCompiler: NewBaseCompiler("bin", "", InstallInSystem),
65	}
66
67	module.compiler = binary
68
69	return module, binary
70}
71
72func (binary *binaryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
73	flags = binary.baseCompiler.compilerFlags(ctx, flags)
74
75	if ctx.toolchain().Bionic() {
76		// no-undefined-version breaks dylib compilation since __rust_*alloc* functions aren't defined,
77		// but we can apply this to binaries.
78		flags.LinkFlags = append(flags.LinkFlags,
79			"-Wl,--gc-sections",
80			"-Wl,-z,nocopyreloc",
81			"-Wl,--no-undefined-version")
82
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		}
87	}
88
89	return flags
90}
91
92func (binary *binaryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
93	deps = binary.baseCompiler.compilerDeps(ctx, deps)
94
95	static := Bool(binary.Properties.Static_executable)
96	if ctx.toolchain().Bionic() {
97		deps = bionicDeps(ctx, deps, static)
98		if static {
99			deps.CrtBegin = []string{"crtbegin_static"}
100		} else {
101			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 {
109			deps.CrtBegin = []string{"libc_musl_crtbegin_dynamic"}
110		}
111		deps.CrtEnd = []string{"libc_musl_crtend"}
112	}
113
114	return deps
115}
116
117func (binary *binaryDecorator) compilerProps() []interface{} {
118	return append(binary.baseCompiler.compilerProps(),
119		&binary.Properties,
120		&binary.stripper.StripProperties)
121}
122
123func (binary *binaryDecorator) nativeCoverage() bool {
124	return true
125}
126
127func (binary *binaryDecorator) preferRlib() bool {
128	return Bool(binary.baseCompiler.Properties.Prefer_rlib) || Bool(binary.Properties.Static_executable)
129}
130
131func (binary *binaryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
132	fileName := binary.getStem(ctx) + ctx.toolchain().ExecutableSuffix()
133	outputFile := android.PathForModuleOut(ctx, fileName)
134	ret := buildOutput{outputFile: outputFile}
135	crateRootPath := crateRootPath(ctx, binary)
136
137	flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
138	flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...)
139	flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...)
140
141	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
150	ret.kytheFile = TransformSrcToBinary(ctx, crateRootPath, deps, flags, outputFile).kytheFile
151	return ret
152}
153
154func (binary *binaryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
155	// Binaries default to dylib dependencies for device, rlib for host.
156	if binary.preferRlib() {
157		return rlibAutoDep
158	} else if ctx.Device() {
159		return dylibAutoDep
160	} else {
161		return rlibAutoDep
162	}
163}
164
165func (binary *binaryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
166	if binary.preferRlib() {
167		return RlibLinkage
168	}
169	return binary.baseCompiler.stdLinkage(ctx)
170}
171
172func (binary *binaryDecorator) binary() bool {
173	return true
174}
175
176func (binary *binaryDecorator) staticallyLinked() bool {
177	return Bool(binary.Properties.Static_executable)
178}
179
180func (binary *binaryDecorator) testBinary() bool {
181	return false
182}
183