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 15package rust 16 17import ( 18 "android/soong/android" 19) 20 21func init() { 22 android.RegisterModuleType("rust_binary", RustBinaryFactory) 23 android.RegisterModuleType("rust_binary_host", RustBinaryHostFactory) 24} 25 26type BinaryCompilerProperties struct { 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"` 32} 33 34type binaryInterface interface { 35 binary() bool 36 staticallyLinked() bool 37 testBinary() bool 38} 39 40type binaryDecorator struct { 41 *baseCompiler 42 stripper Stripper 43 44 Properties BinaryCompilerProperties 45} 46 47var _ compiler = (*binaryDecorator)(nil) 48 49// rust_binary produces a binary that is runnable on a device. 50func RustBinaryFactory() android.Module { 51 module, _ := NewRustBinary(android.HostAndDeviceSupported) 52 return module.Init() 53} 54 55func RustBinaryHostFactory() android.Module { 56 module, _ := NewRustBinary(android.HostSupported) 57 return module.Init() 58} 59 60func NewRustBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) { 61 module := newModule(hod, android.MultilibFirst) 62 63 binary := &binaryDecorator{ 64 baseCompiler: NewBaseCompiler("bin", "", InstallInSystem), 65 } 66 67 module.compiler = binary 68 69 return module, binary 70} 71 72func (binary *binaryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags { 73 flags = binary.baseCompiler.compilerFlags(ctx, flags) 74 75 if ctx.Os().Linux() { 76 flags.LinkFlags = append(flags.LinkFlags, "-Wl,--gc-sections") 77 } 78 79 if ctx.toolchain().Bionic() { 80 // no-undefined-version breaks dylib compilation since __rust_*alloc* functions aren't defined, 81 // but we can apply this to binaries. 82 flags.LinkFlags = append(flags.LinkFlags, 83 "-Wl,-z,nocopyreloc", 84 "-Wl,--no-undefined-version") 85 86 if Bool(binary.Properties.Static_executable) { 87 flags.LinkFlags = append(flags.LinkFlags, "-static") 88 flags.RustFlags = append(flags.RustFlags, "-C relocation-model=static") 89 } 90 } 91 92 return flags 93} 94 95func (binary *binaryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps { 96 deps = binary.baseCompiler.compilerDeps(ctx, deps) 97 98 static := Bool(binary.Properties.Static_executable) 99 if ctx.toolchain().Bionic() { 100 deps = bionicDeps(ctx, deps, static) 101 if static { 102 deps.CrtBegin = []string{"crtbegin_static"} 103 } else { 104 deps.CrtBegin = []string{"crtbegin_dynamic"} 105 } 106 deps.CrtEnd = []string{"crtend_android"} 107 } else if ctx.Os() == android.LinuxMusl { 108 deps = muslDeps(ctx, deps, static) 109 if static { 110 deps.CrtBegin = []string{"libc_musl_crtbegin_static"} 111 } else { 112 deps.CrtBegin = []string{"libc_musl_crtbegin_dynamic"} 113 } 114 deps.CrtEnd = []string{"libc_musl_crtend"} 115 } 116 117 return deps 118} 119 120func (binary *binaryDecorator) compilerProps() []interface{} { 121 return append(binary.baseCompiler.compilerProps(), 122 &binary.Properties, 123 &binary.stripper.StripProperties) 124} 125 126func (binary *binaryDecorator) nativeCoverage() bool { 127 return true 128} 129 130func (binary *binaryDecorator) preferRlib() bool { 131 return Bool(binary.baseCompiler.Properties.Prefer_rlib) || Bool(binary.Properties.Static_executable) 132} 133 134func (binary *binaryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput { 135 fileName := binary.getStem(ctx) + ctx.toolchain().ExecutableSuffix() 136 srcPath, _ := srcPathFromModuleSrcs(ctx, binary.baseCompiler.Properties.Srcs) 137 outputFile := android.PathForModuleOut(ctx, fileName) 138 ret := buildOutput{outputFile: outputFile} 139 140 flags.RustFlags = append(flags.RustFlags, deps.depFlags...) 141 flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...) 142 flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects.Strings()...) 143 144 if binary.stripper.NeedsStrip(ctx) { 145 strippedOutputFile := outputFile 146 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName) 147 binary.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile) 148 149 binary.baseCompiler.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile) 150 } 151 binary.baseCompiler.unstrippedOutputFile = outputFile 152 153 ret.kytheFile = TransformSrcToBinary(ctx, srcPath, deps, flags, outputFile).kytheFile 154 return ret 155} 156 157func (binary *binaryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep { 158 // Binaries default to dylib dependencies for device, rlib for host. 159 if binary.preferRlib() { 160 return rlibAutoDep 161 } else if mod, ok := ctx.Module().(*Module); ok && mod.InVendor() { 162 // Vendor Rust binaries should prefer rlibs. 163 return rlibAutoDep 164 } else if ctx.Device() { 165 return dylibAutoDep 166 } else { 167 return rlibAutoDep 168 } 169} 170 171func (binary *binaryDecorator) stdLinkage(ctx *depsContext) RustLinkage { 172 if binary.preferRlib() { 173 return RlibLinkage 174 } else if ctx.RustModule().InVendor() { 175 return RlibLinkage 176 } 177 return binary.baseCompiler.stdLinkage(ctx) 178} 179 180func (binary *binaryDecorator) binary() bool { 181 return true 182} 183 184func (binary *binaryDecorator) staticallyLinked() bool { 185 return Bool(binary.Properties.Static_executable) 186} 187 188func (binary *binaryDecorator) testBinary() bool { 189 return false 190} 191