blob: 3f43da07690e817ad490055bff0a8342500f5ffb [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 {
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 (assuming it has no dylib dependencies already)
31 Prefer_dynamic *bool
32}
33
34type binaryDecorator struct {
35 *baseCompiler
36
37 Properties BinaryCompilerProperties
38 distFile android.OptionalPath
39 unstrippedOutputFile android.Path
40}
41
42var _ compiler = (*binaryDecorator)(nil)
43
44// rust_binary produces a binary that is runnable on a device.
45func RustBinaryFactory() android.Module {
46 module, _ := NewRustBinary(android.HostAndDeviceSupported)
47 return module.Init()
48}
49
50func RustBinaryHostFactory() android.Module {
51 module, _ := NewRustBinary(android.HostSupported)
52 return module.Init()
53}
54
55func NewRustBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
56 module := newModule(hod, android.MultilibFirst)
57
58 binary := &binaryDecorator{
59 baseCompiler: NewBaseCompiler("bin", ""),
60 }
61
62 module.compiler = binary
63
64 return module, binary
65}
66
67func (binary *binaryDecorator) preferDynamic() bool {
68 return Bool(binary.Properties.Prefer_dynamic)
69}
70
71func (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() {
75 // no-undefined-version breaks dylib compilation since __rust_*alloc* functions aren't defined, but we can apply this to binaries.
76 flags.LinkFlags = append(flags.LinkFlags,
77 "-Wl,--gc-sections",
78 "-Wl,-z,nocopyreloc",
79 "-Wl,--no-undefined-version")
80 }
81
Ivan Lozanoffee3342019-08-27 12:03:00 -070082 if binary.preferDynamic() {
83 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
84 }
85 return flags
86}
87
88func (binary *binaryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
89 deps = binary.baseCompiler.compilerDeps(ctx, deps)
90
Ivan Lozanof1c84332019-09-20 11:00:37 -070091 if ctx.toolchain().Bionic() {
92 deps = binary.baseCompiler.bionicDeps(ctx, deps)
93 deps.CrtBegin = "crtbegin_dynamic"
94 deps.CrtEnd = "crtend_android"
95 }
96
Ivan Lozanoffee3342019-08-27 12:03:00 -070097 return deps
98}
99
100func (binary *binaryDecorator) compilerProps() []interface{} {
101 return append(binary.baseCompiler.compilerProps(),
102 &binary.Properties)
103}
104
105func (binary *binaryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
106 fileName := binary.getStem(ctx) + ctx.toolchain().ExecutableSuffix()
107
108 srcPath := srcPathFromModuleSrcs(ctx, binary.Properties.Srcs)
109
110 outputFile := android.PathForModuleOut(ctx, fileName)
111 binary.unstrippedOutputFile = outputFile
112
113 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
114
115 TransformSrcToBinary(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
116
117 return outputFile
118}