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