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 | bf63d00 | 2020-10-02 10:03:23 -0400 | [diff] [blame] | 27 | // Builds this binary as a static binary. Implies prefer_rlib true. |
| 28 | // |
| 29 | // Static executables currently only support for bionic targets. Non-bionic targets will not produce a fully static |
| 30 | // binary, but will still implicitly imply prefer_rlib true. |
| 31 | Static_executable *bool `android:"arch_variant"` |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Ivan Lozano | 21fa0a5 | 2021-11-01 09:19:45 -0400 | [diff] [blame] | 34 | type binaryInterface interface { |
| 35 | binary() bool |
| 36 | staticallyLinked() bool |
| 37 | } |
| 38 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 39 | type binaryDecorator struct { |
| 40 | *baseCompiler |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 41 | stripper Stripper |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 42 | |
Ivan Lozano | 8a23fa4 | 2020-06-16 10:26:57 -0400 | [diff] [blame] | 43 | Properties BinaryCompilerProperties |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | var _ compiler = (*binaryDecorator)(nil) |
| 47 | |
| 48 | // rust_binary produces a binary that is runnable on a device. |
| 49 | func RustBinaryFactory() android.Module { |
| 50 | module, _ := NewRustBinary(android.HostAndDeviceSupported) |
| 51 | return module.Init() |
| 52 | } |
| 53 | |
| 54 | func RustBinaryHostFactory() android.Module { |
| 55 | module, _ := NewRustBinary(android.HostSupported) |
| 56 | return module.Init() |
| 57 | } |
| 58 | |
| 59 | func NewRustBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) { |
| 60 | module := newModule(hod, android.MultilibFirst) |
| 61 | |
| 62 | binary := &binaryDecorator{ |
Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 63 | baseCompiler: NewBaseCompiler("bin", "", InstallInSystem), |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | module.compiler = binary |
| 67 | |
| 68 | return module, binary |
| 69 | } |
| 70 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 71 | func (binary *binaryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags { |
| 72 | flags = binary.baseCompiler.compilerFlags(ctx, flags) |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 73 | |
| 74 | if ctx.toolchain().Bionic() { |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 75 | // no-undefined-version breaks dylib compilation since __rust_*alloc* functions aren't defined, |
| 76 | // but we can apply this to binaries. |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 77 | flags.LinkFlags = append(flags.LinkFlags, |
| 78 | "-Wl,--gc-sections", |
| 79 | "-Wl,-z,nocopyreloc", |
| 80 | "-Wl,--no-undefined-version") |
Ivan Lozano | bf63d00 | 2020-10-02 10:03:23 -0400 | [diff] [blame] | 81 | |
| 82 | if Bool(binary.Properties.Static_executable) { |
| 83 | flags.LinkFlags = append(flags.LinkFlags, "-static") |
| 84 | flags.RustFlags = append(flags.RustFlags, "-C relocation-model=static") |
| 85 | } |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 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() { |
Thiébaud Weksteen | f1ff54a | 2021-03-22 14:24:54 +0100 | [diff] [blame] | 95 | deps = bionicDeps(ctx, deps, Bool(binary.Properties.Static_executable)) |
Ivan Lozano | bf63d00 | 2020-10-02 10:03:23 -0400 | [diff] [blame] | 96 | if Bool(binary.Properties.Static_executable) { |
| 97 | deps.CrtBegin = "crtbegin_static" |
| 98 | } else { |
| 99 | deps.CrtBegin = "crtbegin_dynamic" |
| 100 | } |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 101 | deps.CrtEnd = "crtend_android" |
| 102 | } |
| 103 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 104 | return deps |
| 105 | } |
| 106 | |
| 107 | func (binary *binaryDecorator) compilerProps() []interface{} { |
| 108 | return append(binary.baseCompiler.compilerProps(), |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 109 | &binary.Properties, |
| 110 | &binary.stripper.StripProperties) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 113 | func (binary *binaryDecorator) nativeCoverage() bool { |
| 114 | return true |
| 115 | } |
| 116 | |
Ivan Lozano | bf63d00 | 2020-10-02 10:03:23 -0400 | [diff] [blame] | 117 | func (binary *binaryDecorator) preferRlib() bool { |
Ivan Lozano | ea08613 | 2020-12-08 14:43:00 -0500 | [diff] [blame] | 118 | return Bool(binary.baseCompiler.Properties.Prefer_rlib) || Bool(binary.Properties.Static_executable) |
Ivan Lozano | bf63d00 | 2020-10-02 10:03:23 -0400 | [diff] [blame] | 119 | } |
| 120 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 121 | func (binary *binaryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path { |
| 122 | fileName := binary.getStem(ctx) + ctx.toolchain().ExecutableSuffix() |
Ivan Lozano | 07cbaf4 | 2020-07-22 16:09:13 -0400 | [diff] [blame] | 123 | srcPath, _ := srcPathFromModuleSrcs(ctx, binary.baseCompiler.Properties.Srcs) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 124 | outputFile := android.PathForModuleOut(ctx, fileName) |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame^] | 125 | ret := outputFile |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 126 | |
| 127 | flags.RustFlags = append(flags.RustFlags, deps.depFlags...) |
Ivan Lozano | 3dfa12d | 2021-02-04 11:29:41 -0500 | [diff] [blame] | 128 | flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...) |
Ivan Lozano | 2093af2 | 2020-08-25 12:48:19 -0400 | [diff] [blame] | 129 | flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 130 | |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame^] | 131 | if binary.stripper.NeedsStrip(ctx) { |
| 132 | strippedOutputFile := outputFile |
| 133 | outputFile = android.PathForModuleOut(ctx, "unstripped", fileName) |
| 134 | binary.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile) |
| 135 | |
| 136 | binary.baseCompiler.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile) |
| 137 | } |
| 138 | binary.baseCompiler.unstrippedOutputFile = outputFile |
| 139 | |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 140 | TransformSrcToBinary(ctx, srcPath, deps, flags, outputFile) |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 141 | |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame^] | 142 | return ret |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 143 | } |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 144 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 145 | func (binary *binaryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep { |
Ivan Lozano | 042504f | 2020-08-18 14:31:23 -0400 | [diff] [blame] | 146 | // Binaries default to dylib dependencies for device, rlib for host. |
Ivan Lozano | bf63d00 | 2020-10-02 10:03:23 -0400 | [diff] [blame] | 147 | if binary.preferRlib() { |
Ivan Lozano | 1120087 | 2020-09-28 11:56:30 -0400 | [diff] [blame] | 148 | return rlibAutoDep |
Ivan Lozano | 1921e80 | 2021-05-20 13:39:16 -0400 | [diff] [blame] | 149 | } else if mod, ok := ctx.Module().(*Module); ok && mod.InVendor() { |
| 150 | // Vendor Rust binaries should prefer rlibs. |
| 151 | return rlibAutoDep |
Ivan Lozano | ea08613 | 2020-12-08 14:43:00 -0500 | [diff] [blame] | 152 | } else if ctx.Device() { |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 153 | return dylibAutoDep |
| 154 | } else { |
| 155 | return rlibAutoDep |
| 156 | } |
| 157 | } |
Ivan Lozano | 1120087 | 2020-09-28 11:56:30 -0400 | [diff] [blame] | 158 | |
Ivan Lozano | dd05547 | 2020-09-28 13:22:45 -0400 | [diff] [blame] | 159 | func (binary *binaryDecorator) stdLinkage(ctx *depsContext) RustLinkage { |
Ivan Lozano | bf63d00 | 2020-10-02 10:03:23 -0400 | [diff] [blame] | 160 | if binary.preferRlib() { |
Ivan Lozano | dd05547 | 2020-09-28 13:22:45 -0400 | [diff] [blame] | 161 | return RlibLinkage |
Ivan Lozano | 1921e80 | 2021-05-20 13:39:16 -0400 | [diff] [blame] | 162 | } else if ctx.RustModule().InVendor() { |
| 163 | return RlibLinkage |
Ivan Lozano | dd05547 | 2020-09-28 13:22:45 -0400 | [diff] [blame] | 164 | } |
| 165 | return binary.baseCompiler.stdLinkage(ctx) |
Ivan Lozano | 1120087 | 2020-09-28 11:56:30 -0400 | [diff] [blame] | 166 | } |
Ivan Lozano | 21fa0a5 | 2021-11-01 09:19:45 -0400 | [diff] [blame] | 167 | |
| 168 | func (binary *binaryDecorator) binary() bool { |
| 169 | return true |
| 170 | } |
| 171 | |
| 172 | func (binary *binaryDecorator) staticallyLinked() bool { |
| 173 | return Bool(binary.Properties.Static_executable) |
| 174 | } |