binary.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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.Os().Linux() {
  60. flags.LinkFlags = append(flags.LinkFlags, "-Wl,--gc-sections")
  61. }
  62. if ctx.toolchain().Bionic() {
  63. // no-undefined-version breaks dylib compilation since __rust_*alloc* functions aren't defined,
  64. // but we can apply this to binaries.
  65. flags.LinkFlags = append(flags.LinkFlags,
  66. "-Wl,-z,nocopyreloc",
  67. "-Wl,--no-undefined-version")
  68. if Bool(binary.Properties.Static_executable) {
  69. flags.LinkFlags = append(flags.LinkFlags, "-static")
  70. flags.RustFlags = append(flags.RustFlags, "-C relocation-model=static")
  71. }
  72. }
  73. return flags
  74. }
  75. func (binary *binaryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
  76. deps = binary.baseCompiler.compilerDeps(ctx, deps)
  77. static := Bool(binary.Properties.Static_executable)
  78. if ctx.toolchain().Bionic() {
  79. deps = bionicDeps(ctx, deps, static)
  80. if static {
  81. deps.CrtBegin = []string{"crtbegin_static"}
  82. } else {
  83. deps.CrtBegin = []string{"crtbegin_dynamic"}
  84. }
  85. deps.CrtEnd = []string{"crtend_android"}
  86. } else if ctx.Os() == android.LinuxMusl {
  87. deps = muslDeps(ctx, deps, static)
  88. if static {
  89. deps.CrtBegin = []string{"libc_musl_crtbegin_static"}
  90. } else {
  91. deps.CrtBegin = []string{"libc_musl_crtbegin_dynamic"}
  92. }
  93. deps.CrtEnd = []string{"libc_musl_crtend"}
  94. }
  95. return deps
  96. }
  97. func (binary *binaryDecorator) compilerProps() []interface{} {
  98. return append(binary.baseCompiler.compilerProps(),
  99. &binary.Properties,
  100. &binary.stripper.StripProperties)
  101. }
  102. func (binary *binaryDecorator) nativeCoverage() bool {
  103. return true
  104. }
  105. func (binary *binaryDecorator) preferRlib() bool {
  106. return Bool(binary.baseCompiler.Properties.Prefer_rlib) || Bool(binary.Properties.Static_executable)
  107. }
  108. func (binary *binaryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
  109. fileName := binary.getStem(ctx) + ctx.toolchain().ExecutableSuffix()
  110. srcPath, _ := srcPathFromModuleSrcs(ctx, binary.baseCompiler.Properties.Srcs)
  111. outputFile := android.PathForModuleOut(ctx, fileName)
  112. ret := buildOutput{outputFile: outputFile}
  113. flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
  114. flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...)
  115. flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects.Strings()...)
  116. if binary.stripper.NeedsStrip(ctx) {
  117. strippedOutputFile := outputFile
  118. outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
  119. binary.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile)
  120. binary.baseCompiler.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
  121. }
  122. binary.baseCompiler.unstrippedOutputFile = outputFile
  123. ret.kytheFile = TransformSrcToBinary(ctx, srcPath, deps, flags, outputFile).kytheFile
  124. return ret
  125. }
  126. func (binary *binaryDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
  127. // Binaries default to dylib dependencies for device, rlib for host.
  128. if binary.preferRlib() {
  129. return rlibAutoDep
  130. } else if mod, ok := ctx.Module().(*Module); ok && mod.InVendor() {
  131. // Vendor Rust binaries should prefer rlibs.
  132. return rlibAutoDep
  133. } else if ctx.Device() {
  134. return dylibAutoDep
  135. } else {
  136. return rlibAutoDep
  137. }
  138. }
  139. func (binary *binaryDecorator) stdLinkage(ctx *depsContext) RustLinkage {
  140. if binary.preferRlib() {
  141. return RlibLinkage
  142. } else if ctx.RustModule().InVendor() {
  143. return RlibLinkage
  144. }
  145. return binary.baseCompiler.stdLinkage(ctx)
  146. }
  147. func (binary *binaryDecorator) binary() bool {
  148. return true
  149. }
  150. func (binary *binaryDecorator) staticallyLinked() bool {
  151. return Bool(binary.Properties.Static_executable)
  152. }
  153. func (binary *binaryDecorator) testBinary() bool {
  154. return false
  155. }