Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 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 | |
| 15 | package rust |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 19 | ) |
| 20 | |
| 21 | func init() { |
| 22 | android.RegisterModuleType("rust_binary", RustBinaryFactory) |
| 23 | android.RegisterModuleType("rust_binary_host", RustBinaryHostFactory) |
| 24 | } |
| 25 | |
| 26 | type 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 | |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 30 | // passes -C prefer-dynamic to rustc, which tells it to dynamically link the stdlib |
| 31 | // (assuming it has no dylib dependencies already) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 32 | Prefer_dynamic *bool |
| 33 | } |
| 34 | |
| 35 | type binaryDecorator struct { |
| 36 | *baseCompiler |
| 37 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame^] | 38 | Properties BinaryCompilerProperties |
| 39 | distFile android.OptionalPath |
| 40 | coverageOutputZipFile android.OptionalPath |
| 41 | unstrippedOutputFile android.Path |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | var _ compiler = (*binaryDecorator)(nil) |
| 45 | |
| 46 | // rust_binary produces a binary that is runnable on a device. |
| 47 | func RustBinaryFactory() android.Module { |
| 48 | module, _ := NewRustBinary(android.HostAndDeviceSupported) |
| 49 | return module.Init() |
| 50 | } |
| 51 | |
| 52 | func RustBinaryHostFactory() android.Module { |
| 53 | module, _ := NewRustBinary(android.HostSupported) |
| 54 | return module.Init() |
| 55 | } |
| 56 | |
| 57 | func NewRustBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) { |
| 58 | module := newModule(hod, android.MultilibFirst) |
| 59 | |
| 60 | binary := &binaryDecorator{ |
Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 61 | baseCompiler: NewBaseCompiler("bin", "", InstallInSystem), |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | module.compiler = binary |
| 65 | |
| 66 | return module, binary |
| 67 | } |
| 68 | |
| 69 | func (binary *binaryDecorator) preferDynamic() bool { |
| 70 | return Bool(binary.Properties.Prefer_dynamic) |
| 71 | } |
| 72 | |
| 73 | func (binary *binaryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags { |
| 74 | flags = binary.baseCompiler.compilerFlags(ctx, flags) |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 75 | |
| 76 | if ctx.toolchain().Bionic() { |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 77 | // no-undefined-version breaks dylib compilation since __rust_*alloc* functions aren't defined, |
| 78 | // but we can apply this to binaries. |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 79 | flags.LinkFlags = append(flags.LinkFlags, |
| 80 | "-Wl,--gc-sections", |
| 81 | "-Wl,-z,nocopyreloc", |
| 82 | "-Wl,--no-undefined-version") |
| 83 | } |
| 84 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 85 | if binary.preferDynamic() { |
| 86 | flags.RustFlags = append(flags.RustFlags, "-C prefer-dynamic") |
| 87 | } |
| 88 | return flags |
| 89 | } |
| 90 | |
| 91 | func (binary *binaryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps { |
| 92 | deps = binary.baseCompiler.compilerDeps(ctx, deps) |
| 93 | |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 94 | if ctx.toolchain().Bionic() { |
| 95 | deps = binary.baseCompiler.bionicDeps(ctx, deps) |
| 96 | deps.CrtBegin = "crtbegin_dynamic" |
| 97 | deps.CrtEnd = "crtend_android" |
| 98 | } |
| 99 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 100 | return deps |
| 101 | } |
| 102 | |
| 103 | func (binary *binaryDecorator) compilerProps() []interface{} { |
| 104 | return append(binary.baseCompiler.compilerProps(), |
| 105 | &binary.Properties) |
| 106 | } |
| 107 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame^] | 108 | func (binary *binaryDecorator) nativeCoverage() bool { |
| 109 | return true |
| 110 | } |
| 111 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 112 | func (binary *binaryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path { |
| 113 | fileName := binary.getStem(ctx) + ctx.toolchain().ExecutableSuffix() |
| 114 | |
| 115 | srcPath := srcPathFromModuleSrcs(ctx, binary.Properties.Srcs) |
| 116 | |
| 117 | outputFile := android.PathForModuleOut(ctx, fileName) |
| 118 | binary.unstrippedOutputFile = outputFile |
| 119 | |
| 120 | flags.RustFlags = append(flags.RustFlags, deps.depFlags...) |
| 121 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame^] | 122 | outputs := TransformSrcToBinary(ctx, srcPath, deps, flags, outputFile, deps.linkDirs) |
| 123 | binary.coverageFile = outputs.coverageFile |
| 124 | |
| 125 | var coverageFiles android.Paths |
| 126 | if outputs.coverageFile != nil { |
| 127 | coverageFiles = append(coverageFiles, binary.coverageFile) |
| 128 | } |
| 129 | if len(deps.coverageFiles) > 0 { |
| 130 | coverageFiles = append(coverageFiles, deps.coverageFiles...) |
| 131 | } |
| 132 | binary.coverageOutputZipFile = TransformCoverageFilesToZip(ctx, coverageFiles, binary.getStem(ctx)) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 133 | |
| 134 | return outputFile |
| 135 | } |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame^] | 136 | |
| 137 | func (binary *binaryDecorator) coverageOutputZipPath() android.OptionalPath { |
| 138 | return binary.coverageOutputZipFile |
| 139 | } |