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 ( |
Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 18 | "fmt" |
| 19 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 20 | "android/soong/android" |
Vinh Tran | 093a57e | 2023-08-24 12:10:49 -0400 | [diff] [blame] | 21 | "android/soong/bazel" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 22 | ) |
| 23 | |
| 24 | func init() { |
| 25 | android.RegisterModuleType("rust_binary", RustBinaryFactory) |
| 26 | android.RegisterModuleType("rust_binary_host", RustBinaryHostFactory) |
| 27 | } |
| 28 | |
| 29 | type BinaryCompilerProperties struct { |
Ivan Lozano | bf63d00 | 2020-10-02 10:03:23 -0400 | [diff] [blame] | 30 | // Builds this binary as a static binary. Implies prefer_rlib true. |
| 31 | // |
| 32 | // Static executables currently only support for bionic targets. Non-bionic targets will not produce a fully static |
| 33 | // binary, but will still implicitly imply prefer_rlib true. |
| 34 | Static_executable *bool `android:"arch_variant"` |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Ivan Lozano | 21fa0a5 | 2021-11-01 09:19:45 -0400 | [diff] [blame] | 37 | type binaryInterface interface { |
| 38 | binary() bool |
| 39 | staticallyLinked() bool |
Ivan Lozano | 62cd038 | 2021-11-01 10:27:54 -0400 | [diff] [blame] | 40 | testBinary() bool |
Ivan Lozano | 21fa0a5 | 2021-11-01 09:19:45 -0400 | [diff] [blame] | 41 | } |
| 42 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 43 | type binaryDecorator struct { |
| 44 | *baseCompiler |
ThiƩbaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 45 | stripper Stripper |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 46 | |
Ivan Lozano | 8a23fa4 | 2020-06-16 10:26:57 -0400 | [diff] [blame] | 47 | Properties BinaryCompilerProperties |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | var _ compiler = (*binaryDecorator)(nil) |
| 51 | |
| 52 | // rust_binary produces a binary that is runnable on a device. |
| 53 | func RustBinaryFactory() android.Module { |
| 54 | module, _ := NewRustBinary(android.HostAndDeviceSupported) |
| 55 | return module.Init() |
| 56 | } |
| 57 | |
| 58 | func RustBinaryHostFactory() android.Module { |
| 59 | module, _ := NewRustBinary(android.HostSupported) |
| 60 | return module.Init() |
| 61 | } |
| 62 | |
| 63 | func NewRustBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) { |
| 64 | module := newModule(hod, android.MultilibFirst) |
| 65 | |
Vinh Tran | 093a57e | 2023-08-24 12:10:49 -0400 | [diff] [blame] | 66 | android.InitBazelModule(module) |
| 67 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 68 | binary := &binaryDecorator{ |
Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 69 | baseCompiler: NewBaseCompiler("bin", "", InstallInSystem), |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | module.compiler = binary |
| 73 | |
| 74 | return module, binary |
| 75 | } |
| 76 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 77 | func (binary *binaryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags { |
| 78 | flags = binary.baseCompiler.compilerFlags(ctx, flags) |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 79 | |
Peter Collingbourne | e7c71c3 | 2023-03-31 20:21:19 -0700 | [diff] [blame] | 80 | if ctx.Os().Linux() { |
| 81 | flags.LinkFlags = append(flags.LinkFlags, "-Wl,--gc-sections") |
| 82 | } |
| 83 | |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 84 | if ctx.toolchain().Bionic() { |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 85 | // no-undefined-version breaks dylib compilation since __rust_*alloc* functions aren't defined, |
| 86 | // but we can apply this to binaries. |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 87 | flags.LinkFlags = append(flags.LinkFlags, |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 88 | "-Wl,-z,nocopyreloc", |
| 89 | "-Wl,--no-undefined-version") |
Ivan Lozano | bf63d00 | 2020-10-02 10:03:23 -0400 | [diff] [blame] | 90 | |
| 91 | if Bool(binary.Properties.Static_executable) { |
| 92 | flags.LinkFlags = append(flags.LinkFlags, "-static") |
| 93 | flags.RustFlags = append(flags.RustFlags, "-C relocation-model=static") |
| 94 | } |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 97 | return flags |
| 98 | } |
| 99 | |
| 100 | func (binary *binaryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps { |
| 101 | deps = binary.baseCompiler.compilerDeps(ctx, deps) |
| 102 | |
Colin Cross | e32f093 | 2022-01-23 20:48:36 -0800 | [diff] [blame] | 103 | static := Bool(binary.Properties.Static_executable) |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 104 | if ctx.toolchain().Bionic() { |
Colin Cross | e32f093 | 2022-01-23 20:48:36 -0800 | [diff] [blame] | 105 | deps = bionicDeps(ctx, deps, static) |
| 106 | if static { |
Colin Cross | fe605e1 | 2022-01-23 20:46:16 -0800 | [diff] [blame] | 107 | deps.CrtBegin = []string{"crtbegin_static"} |
Ivan Lozano | bf63d00 | 2020-10-02 10:03:23 -0400 | [diff] [blame] | 108 | } else { |
Colin Cross | e32f093 | 2022-01-23 20:48:36 -0800 | [diff] [blame] | 109 | deps.CrtBegin = []string{"crtbegin_dynamic"} |
| 110 | } |
| 111 | deps.CrtEnd = []string{"crtend_android"} |
| 112 | } else if ctx.Os() == android.LinuxMusl { |
| 113 | deps = muslDeps(ctx, deps, static) |
| 114 | if static { |
| 115 | deps.CrtBegin = []string{"libc_musl_crtbegin_static"} |
| 116 | } else { |
Colin Cross | 9382c06 | 2022-09-30 15:11:47 -0700 | [diff] [blame] | 117 | deps.CrtBegin = []string{"libc_musl_crtbegin_dynamic"} |
Ivan Lozano | bf63d00 | 2020-10-02 10:03:23 -0400 | [diff] [blame] | 118 | } |
Colin Cross | fe605e1 | 2022-01-23 20:46:16 -0800 | [diff] [blame] | 119 | deps.CrtEnd = []string{"libc_musl_crtend"} |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 122 | return deps |
| 123 | } |
| 124 | |
| 125 | func (binary *binaryDecorator) compilerProps() []interface{} { |
| 126 | return append(binary.baseCompiler.compilerProps(), |
ThiƩbaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 127 | &binary.Properties, |
| 128 | &binary.stripper.StripProperties) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 131 | func (binary *binaryDecorator) nativeCoverage() bool { |
| 132 | return true |
| 133 | } |
| 134 | |
Ivan Lozano | bf63d00 | 2020-10-02 10:03:23 -0400 | [diff] [blame] | 135 | func (binary *binaryDecorator) preferRlib() bool { |
Ivan Lozano | ea08613 | 2020-12-08 14:43:00 -0500 | [diff] [blame] | 136 | return Bool(binary.baseCompiler.Properties.Prefer_rlib) || Bool(binary.Properties.Static_executable) |
Ivan Lozano | bf63d00 | 2020-10-02 10:03:23 -0400 | [diff] [blame] | 137 | } |
| 138 | |
Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 139 | func (binary *binaryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 140 | fileName := binary.getStem(ctx) + ctx.toolchain().ExecutableSuffix() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 141 | outputFile := android.PathForModuleOut(ctx, fileName) |
Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 142 | ret := buildOutput{outputFile: outputFile} |
Sam Delmerico | 63ca14e | 2023-09-25 12:13:17 +0000 | [diff] [blame^] | 143 | var crateRootPath android.Path |
| 144 | if binary.baseCompiler.Properties.Crate_root == nil { |
| 145 | crateRootPath, _ = srcPathFromModuleSrcs(ctx, binary.baseCompiler.Properties.Srcs) |
| 146 | } else { |
| 147 | crateRootPath = android.PathForModuleSrc(ctx, *binary.baseCompiler.Properties.Crate_root) |
| 148 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 149 | |
| 150 | flags.RustFlags = append(flags.RustFlags, deps.depFlags...) |
Ivan Lozano | 3dfa12d | 2021-02-04 11:29:41 -0500 | [diff] [blame] | 151 | flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...) |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 152 | flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects.Strings()...) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 153 | |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 154 | if binary.stripper.NeedsStrip(ctx) { |
| 155 | strippedOutputFile := outputFile |
| 156 | outputFile = android.PathForModuleOut(ctx, "unstripped", fileName) |
| 157 | binary.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile) |
| 158 | |
| 159 | binary.baseCompiler.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile) |
| 160 | } |
| 161 | binary.baseCompiler.unstrippedOutputFile = outputFile |
| 162 | |
Sam Delmerico | 63ca14e | 2023-09-25 12:13:17 +0000 | [diff] [blame^] | 163 | ret.kytheFile = TransformSrcToBinary(ctx, crateRootPath, deps, flags, outputFile).kytheFile |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 164 | return ret |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 165 | } |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 166 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 167 | func (binary *binaryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep { |
Ivan Lozano | 042504f | 2020-08-18 14:31:23 -0400 | [diff] [blame] | 168 | // Binaries default to dylib dependencies for device, rlib for host. |
Ivan Lozano | bf63d00 | 2020-10-02 10:03:23 -0400 | [diff] [blame] | 169 | if binary.preferRlib() { |
Ivan Lozano | 1120087 | 2020-09-28 11:56:30 -0400 | [diff] [blame] | 170 | return rlibAutoDep |
Ivan Lozano | ea08613 | 2020-12-08 14:43:00 -0500 | [diff] [blame] | 171 | } else if ctx.Device() { |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 172 | return dylibAutoDep |
| 173 | } else { |
| 174 | return rlibAutoDep |
| 175 | } |
| 176 | } |
Ivan Lozano | 1120087 | 2020-09-28 11:56:30 -0400 | [diff] [blame] | 177 | |
Ivan Lozano | dd05547 | 2020-09-28 13:22:45 -0400 | [diff] [blame] | 178 | func (binary *binaryDecorator) stdLinkage(ctx *depsContext) RustLinkage { |
Ivan Lozano | bf63d00 | 2020-10-02 10:03:23 -0400 | [diff] [blame] | 179 | if binary.preferRlib() { |
Ivan Lozano | dd05547 | 2020-09-28 13:22:45 -0400 | [diff] [blame] | 180 | return RlibLinkage |
| 181 | } |
| 182 | return binary.baseCompiler.stdLinkage(ctx) |
Ivan Lozano | 1120087 | 2020-09-28 11:56:30 -0400 | [diff] [blame] | 183 | } |
Ivan Lozano | 21fa0a5 | 2021-11-01 09:19:45 -0400 | [diff] [blame] | 184 | |
| 185 | func (binary *binaryDecorator) binary() bool { |
| 186 | return true |
| 187 | } |
| 188 | |
| 189 | func (binary *binaryDecorator) staticallyLinked() bool { |
| 190 | return Bool(binary.Properties.Static_executable) |
| 191 | } |
Ivan Lozano | 62cd038 | 2021-11-01 10:27:54 -0400 | [diff] [blame] | 192 | |
| 193 | func (binary *binaryDecorator) testBinary() bool { |
| 194 | return false |
| 195 | } |
Vinh Tran | 093a57e | 2023-08-24 12:10:49 -0400 | [diff] [blame] | 196 | |
| 197 | type rustBinaryLibraryAttributes struct { |
| 198 | Srcs bazel.LabelListAttribute |
| 199 | Compile_data bazel.LabelListAttribute |
| 200 | Crate_name bazel.StringAttribute |
| 201 | Edition bazel.StringAttribute |
| 202 | Crate_features bazel.StringListAttribute |
| 203 | Deps bazel.LabelListAttribute |
| 204 | Proc_macro_deps bazel.LabelListAttribute |
| 205 | Rustc_flags bazel.StringListAttribute |
| 206 | } |
| 207 | |
Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 208 | func binaryBp2build(ctx android.Bp2buildMutatorContext, m *Module) { |
Vinh Tran | 093a57e | 2023-08-24 12:10:49 -0400 | [diff] [blame] | 209 | binary := m.compiler.(*binaryDecorator) |
| 210 | |
| 211 | var srcs bazel.LabelList |
| 212 | var compileData bazel.LabelList |
| 213 | |
| 214 | if binary.baseCompiler.Properties.Srcs[0] == "src/main.rs" { |
| 215 | srcs = android.BazelLabelForModuleSrc(ctx, []string{"src/**/*.rs"}) |
| 216 | compileData = android.BazelLabelForModuleSrc( |
| 217 | ctx, |
| 218 | []string{ |
| 219 | "src/**/*.proto", |
| 220 | "examples/**/*.rs", |
| 221 | "**/*.md", |
| 222 | "templates/**/*.template", |
| 223 | }, |
| 224 | ) |
| 225 | } else { |
| 226 | srcs = android.BazelLabelForModuleSrc(ctx, binary.baseCompiler.Properties.Srcs) |
| 227 | } |
| 228 | |
| 229 | deps := android.BazelLabelForModuleDeps(ctx, append( |
| 230 | binary.baseCompiler.Properties.Rustlibs, |
| 231 | )) |
| 232 | |
| 233 | procMacroDeps := android.BazelLabelForModuleDeps(ctx, binary.baseCompiler.Properties.Proc_macros) |
| 234 | |
| 235 | var rustcFLags []string |
| 236 | for _, cfg := range binary.baseCompiler.Properties.Cfgs { |
| 237 | rustcFLags = append(rustcFLags, fmt.Sprintf("--cfg=%s", cfg)) |
| 238 | } |
| 239 | |
| 240 | attrs := &rustBinaryLibraryAttributes{ |
| 241 | Srcs: bazel.MakeLabelListAttribute( |
| 242 | srcs, |
| 243 | ), |
| 244 | Compile_data: bazel.MakeLabelListAttribute( |
| 245 | compileData, |
| 246 | ), |
| 247 | Crate_name: bazel.StringAttribute{ |
| 248 | Value: &binary.baseCompiler.Properties.Crate_name, |
| 249 | }, |
| 250 | Edition: bazel.StringAttribute{ |
| 251 | Value: binary.baseCompiler.Properties.Edition, |
| 252 | }, |
| 253 | Crate_features: bazel.StringListAttribute{ |
| 254 | Value: binary.baseCompiler.Properties.Features, |
| 255 | }, |
| 256 | Deps: bazel.MakeLabelListAttribute( |
| 257 | deps, |
| 258 | ), |
| 259 | Proc_macro_deps: bazel.MakeLabelListAttribute( |
| 260 | procMacroDeps, |
| 261 | ), |
| 262 | Rustc_flags: bazel.StringListAttribute{ |
| 263 | Value: append( |
| 264 | rustcFLags, |
| 265 | binary.baseCompiler.Properties.Flags..., |
| 266 | ), |
| 267 | }, |
| 268 | } |
| 269 | |
| 270 | ctx.CreateBazelTargetModule( |
| 271 | bazel.BazelTargetModuleProperties{ |
| 272 | Rule_class: "rust_binary", |
| 273 | Bzl_load_location: "@rules_rust//rust:defs.bzl", |
| 274 | }, |
| 275 | android.CommonAttributes{ |
| 276 | Name: m.Name(), |
| 277 | }, |
| 278 | attrs, |
| 279 | ) |
| 280 | } |