• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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	// path to the main source file that contains the program entry point (e.g. src/main.rs)
28	Srcs []string `android:"path,arch_variant"`
29
30	// passes -C prefer-dynamic to rustc, which tells it to dynamically link the stdlib
31	// (assuming it has no dylib dependencies already)
32	Prefer_dynamic *bool
33}
34
35type binaryDecorator struct {
36	*baseCompiler
37
38	Properties           BinaryCompilerProperties
39	distFile             android.OptionalPath
40	unstrippedOutputFile android.Path
41}
42
43var _ compiler = (*binaryDecorator)(nil)
44
45// rust_binary produces a binary that is runnable on a device.
46func RustBinaryFactory() android.Module {
47	module, _ := NewRustBinary(android.HostAndDeviceSupported)
48	return module.Init()
49}
50
51func RustBinaryHostFactory() android.Module {
52	module, _ := NewRustBinary(android.HostSupported)
53	return module.Init()
54}
55
56func NewRustBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
57	module := newModule(hod, android.MultilibFirst)
58
59	binary := &binaryDecorator{
60		baseCompiler: NewBaseCompiler("bin", "", InstallInSystem),
61	}
62
63	module.compiler = binary
64
65	return module, binary
66}
67
68func (binary *binaryDecorator) preferDynamic() bool {
69	return Bool(binary.Properties.Prefer_dynamic)
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
84	if binary.preferDynamic() {
85		flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
86	}
87	return flags
88}
89
90func (binary *binaryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
91	deps = binary.baseCompiler.compilerDeps(ctx, deps)
92
93	if ctx.toolchain().Bionic() {
94		deps = binary.baseCompiler.bionicDeps(ctx, deps)
95		deps.CrtBegin = "crtbegin_dynamic"
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}
106
107func (binary *binaryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
108	fileName := binary.getStem(ctx) + ctx.toolchain().ExecutableSuffix()
109
110	srcPath := srcPathFromModuleSrcs(ctx, binary.Properties.Srcs)
111
112	outputFile := android.PathForModuleOut(ctx, fileName)
113	binary.unstrippedOutputFile = outputFile
114
115	flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
116
117	TransformSrcToBinary(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
118
119	return outputFile
120}
121