blob: f75018626411ec432f00a07a49ffc694285d7503 [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 {
Ivan Lozanoffee3342019-08-27 12:03:00 -070027}
28
29type binaryDecorator struct {
30 *baseCompiler
31
Ivan Lozano8a23fa42020-06-16 10:26:57 -040032 Properties BinaryCompilerProperties
Ivan Lozanoffee3342019-08-27 12:03:00 -070033}
34
35var _ compiler = (*binaryDecorator)(nil)
36
37// rust_binary produces a binary that is runnable on a device.
38func RustBinaryFactory() android.Module {
39 module, _ := NewRustBinary(android.HostAndDeviceSupported)
40 return module.Init()
41}
42
43func RustBinaryHostFactory() android.Module {
44 module, _ := NewRustBinary(android.HostSupported)
45 return module.Init()
46}
47
48func NewRustBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
49 module := newModule(hod, android.MultilibFirst)
50
51 binary := &binaryDecorator{
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080052 baseCompiler: NewBaseCompiler("bin", "", InstallInSystem),
Ivan Lozanoffee3342019-08-27 12:03:00 -070053 }
54
55 module.compiler = binary
56
57 return module, binary
58}
59
Ivan Lozanoffee3342019-08-27 12:03:00 -070060func (binary *binaryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
61 flags = binary.baseCompiler.compilerFlags(ctx, flags)
Ivan Lozanof1c84332019-09-20 11:00:37 -070062
63 if ctx.toolchain().Bionic() {
Ivan Lozanob2df9f82019-11-05 12:16:46 -080064 // no-undefined-version breaks dylib compilation since __rust_*alloc* functions aren't defined,
65 // but we can apply this to binaries.
Ivan Lozanof1c84332019-09-20 11:00:37 -070066 flags.LinkFlags = append(flags.LinkFlags,
67 "-Wl,--gc-sections",
68 "-Wl,-z,nocopyreloc",
69 "-Wl,--no-undefined-version")
70 }
71
Ivan Lozanoffee3342019-08-27 12:03:00 -070072 return flags
73}
74
75func (binary *binaryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
76 deps = binary.baseCompiler.compilerDeps(ctx, deps)
77
Ivan Lozanof1c84332019-09-20 11:00:37 -070078 if ctx.toolchain().Bionic() {
Ivan Lozano45901ed2020-07-24 16:05:01 -040079 deps = bionicDeps(deps)
Ivan Lozanof1c84332019-09-20 11:00:37 -070080 deps.CrtBegin = "crtbegin_dynamic"
81 deps.CrtEnd = "crtend_android"
82 }
83
Ivan Lozanoffee3342019-08-27 12:03:00 -070084 return deps
85}
86
87func (binary *binaryDecorator) compilerProps() []interface{} {
88 return append(binary.baseCompiler.compilerProps(),
89 &binary.Properties)
90}
91
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040092func (binary *binaryDecorator) nativeCoverage() bool {
93 return true
94}
95
Ivan Lozanoffee3342019-08-27 12:03:00 -070096func (binary *binaryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
97 fileName := binary.getStem(ctx) + ctx.toolchain().ExecutableSuffix()
98
Ivan Lozano07cbaf42020-07-22 16:09:13 -040099 srcPath, _ := srcPathFromModuleSrcs(ctx, binary.baseCompiler.Properties.Srcs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700100
101 outputFile := android.PathForModuleOut(ctx, fileName)
102 binary.unstrippedOutputFile = outputFile
103
104 flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
105
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400106 outputs := TransformSrcToBinary(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
107 binary.coverageFile = outputs.coverageFile
108
109 var coverageFiles android.Paths
110 if outputs.coverageFile != nil {
111 coverageFiles = append(coverageFiles, binary.coverageFile)
112 }
113 if len(deps.coverageFiles) > 0 {
114 coverageFiles = append(coverageFiles, deps.coverageFiles...)
115 }
116 binary.coverageOutputZipFile = TransformCoverageFilesToZip(ctx, coverageFiles, binary.getStem(ctx))
Ivan Lozanoffee3342019-08-27 12:03:00 -0700117
118 return outputFile
119}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400120
121func (binary *binaryDecorator) coverageOutputZipPath() android.OptionalPath {
122 return binary.coverageOutputZipFile
123}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700124
Ivan Lozano042504f2020-08-18 14:31:23 -0400125func (binary *binaryDecorator) autoDep(ctx BaseModuleContext) autoDep {
126 // Binaries default to dylib dependencies for device, rlib for host.
127 if ctx.Device() {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700128 return dylibAutoDep
129 } else {
130 return rlibAutoDep
131 }
132}