binary.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. package rust
  15. import (
  16. "android/soong/android"
  17. )
  18. func init() {
  19. android.RegisterModuleType("rust_binary", RustBinaryFactory)
  20. android.RegisterModuleType("rust_binary_host", RustBinaryHostFactory)
  21. }
  22. type BinaryCompilerProperties struct {
  23. // Builds this binary as a static binary. Implies prefer_rlib true.
  24. //
  25. // Static executables currently only support for bionic targets. Non-bionic targets will not produce a fully static
  26. // binary, but will still implicitly imply prefer_rlib true.
  27. Static_executable *bool `android:"arch_variant"`
  28. }
  29. type binaryInterface interface {
  30. binary() bool
  31. staticallyLinked() bool
  32. testBinary() bool
  33. }
  34. type binaryDecorator struct {
  35. *baseCompiler
  36. stripper Stripper
  37. Properties BinaryCompilerProperties
  38. }
  39. var _ compiler = (*binaryDecorator)(nil)
  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. func RustBinaryHostFactory() android.Module {
  46. module, _ := NewRustBinary(android.HostSupported)
  47. return module.Init()
  48. }
  49. func NewRustBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
  50. module := newModule(hod, android.MultilibFirst)
  51. binary := &binaryDecorator{
  52. baseCompiler: NewBaseCompiler("bin", "", InstallInSystem),
  53. }
  54. module.compiler = binary
  55. return module, binary
  56. }
  57. func (binary *binaryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
  58. flags = binary.baseCompiler.compilerFlags(ctx, flags)
  59. if ctx.toolchain().Bionic() {
  60. // no-undefined-version breaks dylib compilation since __rust_*alloc* functions aren't defined,
  61. // but we can apply this to binaries.
  62. flags.LinkFlags = append(flags.LinkFlags,
  63. "-Wl,--gc-sections",
  64. "-Wl,-z,nocopyreloc",
  65. "-Wl,--no-undefined-version")
  66. if Bool(binary.Properties.Static_executable) {
  67. flags.LinkFlags = append(flags.LinkFlags, "-static")
  68. flags.RustFlags = append(flags.RustFlags, "-C relocation-model=static")
  69. }
  70. }
  71. return flags
  72. }
  73. func (binary *binaryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
  74. deps = binary.baseCompiler.compilerDeps(ctx, deps)
  75. static := Bool(binary.Properties.Static_executable)
  76. if ctx.toolchain().Bionic() {
  77. deps = bionicDeps(ctx, deps, static)
  78. if static {
  79. deps.CrtBegin = []string{"crtbegin_static"}
  80. } else {
  81. deps.CrtBegin = []string{"crtbegin_dynamic"}
  82. }
  83. deps.CrtEnd = []string{"crtend_android"}
  84. } else if ctx.Os() == android.LinuxMusl {
  85. deps = muslDeps(ctx, deps, static)
  86. if static {
  87. deps.CrtBegin = []string{"libc_musl_crtbegin_static"}
  88. } else {
  89. deps.CrtBegin = []string{"libc_musl_crtbegin_dynamic", "musl_linker_script"}
  90. }
  91. deps.CrtEnd = []string{"libc_musl_crtend"}
  92. }
  93. return deps
  94. }
  95. func (binary *binaryDecorator) compilerProps() []interface{} {
  96. return append(binary.baseCompiler.compilerProps(),
  97. &binary.Properties,
  98. &binary.stripper.StripProperties)
  99. }
  100. func (binary *binaryDecorator) nativeCoverage() bool {
  101. return true
  102. }
  103. func (binary *binaryDecorator) preferRlib() bool {
  104. return Bool(binary.baseCompiler.Properties.Prefer_rlib) || Bool(binary.Properties.Static_executable)
  105. }
  106. func (binary *binaryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
  107. fileName := binary.getStem(ctx) + ctx.toolchain().ExecutableSuffix()
  108. srcPath, _ := srcPathFromModuleSrcs(ctx, binary.baseCompiler.Properties.Srcs)
  109. outputFile := android.PathForModuleOut(ctx, fileName)
  110. ret := buildOutput{outputFile: outputFile}
  111. flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
  112. flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...)
  113. flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...)
  114. if binary.stripper.NeedsStrip(ctx) {
  115. strippedOutputFile := outputFile
  116. outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
  117. binary.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
  118. binary.baseCompiler.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
  119. }
  120. binary.baseCompiler.unstrippedOutputFile = outputFile
  121. ret.kytheFile = TransformSrcToBinary(ctx, srcPath, deps, flags, outputFile).kytheFile
  122. return ret
  123. }
  124. func (binary *binaryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
  125. // Binaries default to dylib dependencies for device, rlib for host.
  126. if binary.preferRlib() {
  127. return rlibAutoDep
  128. } else if mod, ok := ctx.Module().(*Module); ok && mod.InVendor() {
  129. // Vendor Rust binaries should prefer rlibs.
  130. return rlibAutoDep
  131. } else if ctx.Device() {
  132. return dylibAutoDep
  133. } else {
  134. return rlibAutoDep
  135. }
  136. }
  137. func (binary *binaryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
  138. if binary.preferRlib() {
  139. return RlibLinkage
  140. } else if ctx.RustModule().InVendor() {
  141. return RlibLinkage
  142. }
  143. return binary.baseCompiler.stdLinkage(ctx)
  144. }
  145. func (binary *binaryDecorator) binary() bool {
  146. return true
  147. }
  148. func (binary *binaryDecorator) staticallyLinked() bool {
  149. return Bool(binary.Properties.Static_executable)
  150. }
  151. func (binary *binaryDecorator) testBinary() bool {
  152. return false
  153. }