vndk_prebuilt.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // Copyright 2017 Google Inc. All rights reserved.
  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 cc
  15. import (
  16. "strings"
  17. "android/soong/android"
  18. )
  19. var (
  20. vndkSuffix = ".vndk."
  21. binder32Suffix = ".binder32"
  22. )
  23. // Creates vndk prebuilts that include the VNDK version.
  24. //
  25. // Example:
  26. //
  27. // vndk_prebuilt_shared {
  28. // name: "libfoo",
  29. // version: "27",
  30. // target_arch: "arm64",
  31. // vendor_available: true,
  32. // product_available: true,
  33. // vndk: {
  34. // enabled: true,
  35. // },
  36. // export_include_dirs: ["include/external/libfoo/vndk_include"],
  37. // arch: {
  38. // arm64: {
  39. // srcs: ["arm/lib64/libfoo.so"],
  40. // },
  41. // arm: {
  42. // srcs: ["arm/lib/libfoo.so"],
  43. // },
  44. // },
  45. // }
  46. type vndkPrebuiltProperties struct {
  47. // VNDK snapshot version.
  48. Version *string
  49. // Target arch name of the snapshot (e.g. 'arm64' for variant 'aosp_arm64')
  50. Target_arch *string
  51. // If the prebuilt snapshot lib is built with 32 bit binder, this must be set to true.
  52. // The lib with 64 bit binder does not need to set this property.
  53. Binder32bit *bool
  54. // Prebuilt files for each arch.
  55. Srcs []string `android:"arch_variant"`
  56. // list of flags that will be used for any module that links against this module.
  57. Export_flags []string `android:"arch_variant"`
  58. // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined symbols,
  59. // etc).
  60. Check_elf_files *bool
  61. }
  62. type vndkPrebuiltLibraryDecorator struct {
  63. *libraryDecorator
  64. properties vndkPrebuiltProperties
  65. androidMkSuffix string
  66. }
  67. func (p *vndkPrebuiltLibraryDecorator) Name(name string) string {
  68. return name + p.NameSuffix()
  69. }
  70. func (p *vndkPrebuiltLibraryDecorator) NameSuffix() string {
  71. suffix := p.Version()
  72. if p.arch() != "" {
  73. suffix += "." + p.arch()
  74. }
  75. if Bool(p.properties.Binder32bit) {
  76. suffix += binder32Suffix
  77. }
  78. return vndkSuffix + suffix
  79. }
  80. func (p *vndkPrebuiltLibraryDecorator) Version() string {
  81. return String(p.properties.Version)
  82. }
  83. func (p *vndkPrebuiltLibraryDecorator) arch() string {
  84. return String(p.properties.Target_arch)
  85. }
  86. func (p *vndkPrebuiltLibraryDecorator) binderBit() string {
  87. if Bool(p.properties.Binder32bit) {
  88. return "32"
  89. }
  90. return "64"
  91. }
  92. func (p *vndkPrebuiltLibraryDecorator) SnapshotAndroidMkSuffix() string {
  93. return ".vendor"
  94. }
  95. func (p *vndkPrebuiltLibraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
  96. p.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), p.NameSuffix())
  97. return p.libraryDecorator.linkerFlags(ctx, flags)
  98. }
  99. func (p *vndkPrebuiltLibraryDecorator) singleSourcePath(ctx ModuleContext) android.Path {
  100. if len(p.properties.Srcs) == 0 {
  101. ctx.PropertyErrorf("srcs", "missing prebuilt source file")
  102. return nil
  103. }
  104. if len(p.properties.Srcs) > 1 {
  105. ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
  106. return nil
  107. }
  108. return android.PathForModuleSrc(ctx, p.properties.Srcs[0])
  109. }
  110. func (p *vndkPrebuiltLibraryDecorator) link(ctx ModuleContext,
  111. flags Flags, deps PathDeps, objs Objects) android.Path {
  112. if !p.MatchesWithDevice(ctx.DeviceConfig()) {
  113. ctx.Module().HideFromMake()
  114. return nil
  115. }
  116. if len(p.properties.Srcs) > 0 && p.shared() {
  117. p.libraryDecorator.exportIncludes(ctx)
  118. p.libraryDecorator.reexportFlags(p.properties.Export_flags...)
  119. // current VNDK prebuilts are only shared libs.
  120. in := p.singleSourcePath(ctx)
  121. p.unstrippedOutputFile = in
  122. libName := in.Base()
  123. if p.stripper.NeedsStrip(ctx) {
  124. stripFlags := flagsToStripFlags(flags)
  125. stripped := android.PathForModuleOut(ctx, "stripped", libName)
  126. p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, stripFlags)
  127. in = stripped
  128. }
  129. // Optimize out relinking against shared libraries whose interface hasn't changed by
  130. // depending on a table of contents file instead of the library itself.
  131. tocFile := android.PathForModuleOut(ctx, libName+".toc")
  132. p.tocFile = android.OptionalPathForPath(tocFile)
  133. TransformSharedObjectToToc(ctx, in, tocFile)
  134. p.androidMkSuffix = p.NameSuffix()
  135. vndkVersion := ctx.DeviceConfig().VndkVersion()
  136. if vndkVersion == p.Version() {
  137. p.androidMkSuffix = ""
  138. }
  139. ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
  140. SharedLibrary: in,
  141. Target: ctx.Target(),
  142. TableOfContents: p.tocFile,
  143. })
  144. p.libraryDecorator.flagExporter.setProvider(ctx)
  145. return in
  146. }
  147. ctx.Module().HideFromMake()
  148. return nil
  149. }
  150. func (p *vndkPrebuiltLibraryDecorator) MatchesWithDevice(config android.DeviceConfig) bool {
  151. arches := config.Arches()
  152. if len(arches) == 0 || arches[0].ArchType.String() != p.arch() {
  153. return false
  154. }
  155. if config.BinderBitness() != p.binderBit() {
  156. return false
  157. }
  158. if len(p.properties.Srcs) == 0 {
  159. return false
  160. }
  161. return true
  162. }
  163. func (p *vndkPrebuiltLibraryDecorator) nativeCoverage() bool {
  164. return false
  165. }
  166. func (p *vndkPrebuiltLibraryDecorator) IsSnapshotPrebuilt() bool {
  167. return true
  168. }
  169. func (p *vndkPrebuiltLibraryDecorator) install(ctx ModuleContext, file android.Path) {
  170. // do not install vndk libs
  171. }
  172. func vndkPrebuiltSharedLibrary() *Module {
  173. module, library := NewLibrary(android.DeviceSupported)
  174. library.BuildOnlyShared()
  175. module.stl = nil
  176. module.sanitize = nil
  177. library.disableStripping()
  178. prebuilt := &vndkPrebuiltLibraryDecorator{
  179. libraryDecorator: library,
  180. }
  181. prebuilt.properties.Check_elf_files = BoolPtr(false)
  182. prebuilt.baseLinker.Properties.No_libcrt = BoolPtr(true)
  183. prebuilt.baseLinker.Properties.Nocrt = BoolPtr(true)
  184. // Prevent default system libs (libc, libm, and libdl) from being linked
  185. if prebuilt.baseLinker.Properties.System_shared_libs == nil {
  186. prebuilt.baseLinker.Properties.System_shared_libs = []string{}
  187. }
  188. module.compiler = nil
  189. module.linker = prebuilt
  190. module.installer = prebuilt
  191. module.AddProperties(
  192. &prebuilt.properties,
  193. )
  194. android.AddLoadHook(module, func(ctx android.LoadHookContext) {
  195. // empty BOARD_VNDK_VERSION implies that the device won't support
  196. // system only OTA. In this case, VNDK snapshots aren't needed.
  197. if ctx.DeviceConfig().VndkVersion() == "" {
  198. ctx.Module().Disable()
  199. }
  200. })
  201. return module
  202. }
  203. // vndk_prebuilt_shared installs Vendor Native Development kit (VNDK) snapshot
  204. // shared libraries for system build. Example:
  205. //
  206. // vndk_prebuilt_shared {
  207. // name: "libfoo",
  208. // version: "27",
  209. // target_arch: "arm64",
  210. // vendor_available: true,
  211. // product_available: true,
  212. // vndk: {
  213. // enabled: true,
  214. // },
  215. // export_include_dirs: ["include/external/libfoo/vndk_include"],
  216. // arch: {
  217. // arm64: {
  218. // srcs: ["arm/lib64/libfoo.so"],
  219. // },
  220. // arm: {
  221. // srcs: ["arm/lib/libfoo.so"],
  222. // },
  223. // },
  224. // }
  225. func VndkPrebuiltSharedFactory() android.Module {
  226. module := vndkPrebuiltSharedLibrary()
  227. return module.Init()
  228. }
  229. func init() {
  230. android.RegisterModuleType("vndk_prebuilt_shared", VndkPrebuiltSharedFactory)
  231. }