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 binaryDecorator struct {
35	*baseCompiler
36	stripper Stripper
37
38	Properties BinaryCompilerProperties
39}
40
41var _ compiler = (*binaryDecorator)(nil)
42
43// rust_binary produces a binary that is runnable on a device.
44func RustBinaryFactory() android.Module {
45	module, _ := NewRustBinary(android.HostAndDeviceSupported)
46	return module.Init()
47}
48
49func RustBinaryHostFactory() android.Module {
50	module, _ := NewRustBinary(android.HostSupported)
51	return module.Init()
52}
53
54func NewRustBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
55	module := newModule(hod, android.MultilibFirst)
56
57	binary := &binaryDecorator{
58		baseCompiler: NewBaseCompiler("bin", "", InstallInSystem),
59	}
60
61	module.compiler = binary
62
63	return module, binary
64}
65
66func (binary *binaryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
67	flags = binary.baseCompiler.compilerFlags(ctx, flags)
68
69	if ctx.toolchain().Bionic() {
70		// no-undefined-version breaks dylib compilation since __rust_*alloc* functions aren't defined,
71		// but we can apply this to binaries.
72		flags.LinkFlags = append(flags.LinkFlags,
73			"-Wl,--gc-sections",
74			"-Wl,-z,nocopyreloc",
75			"-Wl,--no-undefined-version")
76
77		if Bool(binary.Properties.Static_executable) {
78			flags.LinkFlags = append(flags.LinkFlags, "-static")
79			flags.RustFlags = append(flags.RustFlags, "-C relocation-model=static")
80		}
81	}
82
83	return flags
84}
85
86func (binary *binaryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
87	deps = binary.baseCompiler.compilerDeps(ctx, deps)
88
89	if ctx.toolchain().Bionic() {
90		deps = bionicDeps(ctx, deps, Bool(binary.Properties.Static_executable))
91		if Bool(binary.Properties.Static_executable) {
92			deps.CrtBegin = "crtbegin_static"
93		} else {
94			deps.CrtBegin = "crtbegin_dynamic"
95		}
96		deps.CrtEnd = "crtend_android"
97	}
98
99	return deps
100}
101
102func (binary *binaryDecorator) compilerProps() []interface{} {
103	return append(binary.baseCompiler.compilerProps(),
104		&binary.Properties,
105		&binary.stripper.StripProperties)
106}
107
108func (binary *binaryDecorator) nativeCoverage() bool {
109	return true
110}
111
112func (binary *binaryDecorator) preferRlib() bool {
113	return Bool(binary.baseCompiler.Properties.Prefer_rlib) || Bool(binary.Properties.Static_executable)
114}
115
116func (binary *binaryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
117	fileName := binary.getStem(ctx) + ctx.toolchain().ExecutableSuffix()
118	srcPath, _ := srcPathFromModuleSrcs(ctx, binary.baseCompiler.Properties.Srcs)
119	outputFile := android.PathForModuleOut(ctx, fileName)
120
121	flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
122	flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...)
123	flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...)
124
125	TransformSrcToBinary(ctx, srcPath, deps, flags, outputFile)
126
127	if binary.stripper.NeedsStrip(ctx) {
128		strippedOutputFile := android.PathForModuleOut(ctx, "stripped", fileName)
129		binary.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
130		binary.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
131	}
132
133	return outputFile
134}
135
136func (binary *binaryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
137	// Binaries default to dylib dependencies for device, rlib for host.
138	if binary.preferRlib() {
139		return rlibAutoDep
140	} else if ctx.Device() {
141		return dylibAutoDep
142	} else {
143		return rlibAutoDep
144	}
145}
146
147func (binary *binaryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
148	if binary.preferRlib() {
149		return RlibLinkage
150	}
151	return binary.baseCompiler.stdLinkage(ctx)
152}
153
154func (binary *binaryDecorator) isDependencyRoot() bool {
155	return true
156}
157