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