blob: a909ea681f1580c6538343e46c1f2ce5fcc7258b [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"
19 "android/soong/rust/config"
20)
21
22func init() {
23 android.RegisterModuleType("rust_binary", RustBinaryFactory)
24 android.RegisterModuleType("rust_binary_host", RustBinaryHostFactory)
25}
26
27type BinaryCompilerProperties struct {
28 // path to the main source file that contains the program entry point (e.g. src/main.rs)
29 Srcs []string `android:"path,arch_variant"`
30
Ivan Lozanob2df9f82019-11-05 12:16:46 -080031 // passes -C prefer-dynamic to rustc, which tells it to dynamically link the stdlib
32 // (assuming it has no dylib dependencies already)
Ivan Lozanoffee3342019-08-27 12:03:00 -070033 Prefer_dynamic *bool
34}
35
36type binaryDecorator struct {
37 *baseCompiler
38
39 Properties BinaryCompilerProperties
40 distFile android.OptionalPath
41 unstrippedOutputFile android.Path
42}
43
44var _ compiler = (*binaryDecorator)(nil)
45
46// rust_binary produces a binary that is runnable on a device.
47func RustBinaryFactory() android.Module {
48 module, _ := NewRustBinary(android.HostAndDeviceSupported)
49 return module.Init()
50}
51
52func RustBinaryHostFactory() android.Module {
53 module, _ := NewRustBinary(android.HostSupported)
54 return module.Init()
55}
56
57func NewRustBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
58 module := newModule(hod, android.MultilibFirst)
59
60 binary := &binaryDecorator{
61 baseCompiler: NewBaseCompiler("bin", ""),
62 }
63
64 module.compiler = binary
65
66 return module, binary
67}
68
69func (binary *binaryDecorator) preferDynamic() bool {
70 return Bool(binary.Properties.Prefer_dynamic)
71}
72
73func (binary *binaryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
74 flags = binary.baseCompiler.compilerFlags(ctx, flags)
Ivan Lozanof1c84332019-09-20 11:00:37 -070075
76 if ctx.toolchain().Bionic() {
Ivan Lozanob2df9f82019-11-05 12:16:46 -080077 // no-undefined-version breaks dylib compilation since __rust_*alloc* functions aren't defined,
78 // but we can apply this to binaries.
Ivan Lozanof1c84332019-09-20 11:00:37 -070079 flags.LinkFlags = append(flags.LinkFlags,
80 "-Wl,--gc-sections",
81 "-Wl,-z,nocopyreloc",
82 "-Wl,--no-undefined-version")
83 }
84
Ivan Lozanoffee3342019-08-27 12:03:00 -070085 if binary.preferDynamic() {
86 flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic")
87 }
88 return flags
89}
90
91func (binary *binaryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
92 deps = binary.baseCompiler.compilerDeps(ctx, deps)
93
94 if binary.preferDynamic() || len(deps.Dylibs) > 0 {
95 for _, stdlib := range config.Stdlibs {
96 deps.Dylibs = append(deps.Dylibs, stdlib+"_"+ctx.toolchain().RustTriple())
97 }
98 }
99
Ivan Lozanof1c84332019-09-20 11:00:37 -0700100 if ctx.toolchain().Bionic() {
101 deps = binary.baseCompiler.bionicDeps(ctx, deps)
102 deps.CrtBegin = "crtbegin_dynamic"
103 deps.CrtEnd = "crtend_android"
104 }
105
Ivan Lozanoffee3342019-08-27 12:03:00 -0700106 return deps
107}
108
109func (binary *binaryDecorator) compilerProps() []interface{} {
110 return append(binary.baseCompiler.compilerProps(),
111 &binary.Properties)
112}
113
114func (binary *binaryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
115 fileName := binary.getStem(ctx) + ctx.toolchain().ExecutableSuffix()
116
117 srcPath := srcPathFromModuleSrcs(ctx, binary.Properties.Srcs)
118
119 outputFile := android.PathForModuleOut(ctx, fileName)
120 binary.unstrippedOutputFile = outputFile
121
122 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
123
124 TransformSrcToBinary(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
125
126 return outputFile
127}