sabi.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. "sync"
  17. "android/soong/android"
  18. "android/soong/cc/config"
  19. )
  20. var (
  21. lsdumpPaths []string
  22. lsdumpPathsLock sync.Mutex
  23. )
  24. // Properties for ABI compatibility checker in Android.bp.
  25. type headerAbiCheckerProperties struct {
  26. // Enable ABI checks (even if this is not an LLNDK/VNDK lib)
  27. Enabled *bool
  28. // Path to a symbol file that specifies the symbols to be included in the generated
  29. // ABI dump file
  30. Symbol_file *string `android:"path"`
  31. // Symbol versions that should be ignored from the symbol file
  32. Exclude_symbol_versions []string
  33. // Symbol tags that should be ignored from the symbol file
  34. Exclude_symbol_tags []string
  35. // Run checks on all APIs (in addition to the ones referred by
  36. // one of exported ELF symbols.)
  37. Check_all_apis *bool
  38. // Extra flags passed to header-abi-diff
  39. Diff_flags []string
  40. // Opt-in reference dump directories
  41. Ref_dump_dirs []string
  42. }
  43. func (props *headerAbiCheckerProperties) enabled() bool {
  44. return Bool(props.Enabled)
  45. }
  46. func (props *headerAbiCheckerProperties) explicitlyDisabled() bool {
  47. return !BoolDefault(props.Enabled, true)
  48. }
  49. type SAbiProperties struct {
  50. // Whether ABI dump should be created for this module.
  51. // Set by `sabiDepsMutator` if this module is a shared library that needs ABI check, or a static
  52. // library that is depended on by an ABI checked library.
  53. ShouldCreateSourceAbiDump bool `blueprint:"mutated"`
  54. // Include directories that may contain ABI information exported by a library.
  55. // These directories are passed to the header-abi-dumper.
  56. ReexportedIncludes []string `blueprint:"mutated"`
  57. }
  58. type sabi struct {
  59. Properties SAbiProperties
  60. }
  61. func (sabi *sabi) props() []interface{} {
  62. return []interface{}{&sabi.Properties}
  63. }
  64. func (sabi *sabi) flags(ctx ModuleContext, flags Flags) Flags {
  65. // Filter out flags which libTooling don't understand.
  66. // This is here for legacy reasons and future-proof, in case the version of libTooling and clang
  67. // diverge.
  68. flags.Local.ToolingCFlags = config.ClangLibToolingFilterUnknownCflags(flags.Local.CFlags)
  69. flags.Global.ToolingCFlags = config.ClangLibToolingFilterUnknownCflags(flags.Global.CFlags)
  70. flags.Local.ToolingCppFlags = config.ClangLibToolingFilterUnknownCflags(flags.Local.CppFlags)
  71. flags.Global.ToolingCppFlags = config.ClangLibToolingFilterUnknownCflags(flags.Global.CppFlags)
  72. return flags
  73. }
  74. // Returns true if ABI dump should be created for this library, either because library is ABI
  75. // checked or is depended on by an ABI checked library.
  76. // Could be called as a nil receiver.
  77. func (sabi *sabi) shouldCreateSourceAbiDump() bool {
  78. return sabi != nil && sabi.Properties.ShouldCreateSourceAbiDump
  79. }
  80. // Returns a string that represents the class of the ABI dump.
  81. // Returns an empty string if ABI check is disabled for this library.
  82. func classifySourceAbiDump(ctx android.BaseModuleContext) string {
  83. m := ctx.Module().(*Module)
  84. headerAbiChecker := m.library.getHeaderAbiCheckerProperties(ctx)
  85. if headerAbiChecker.explicitlyDisabled() {
  86. return ""
  87. }
  88. // Return NDK if the library is both NDK and LLNDK.
  89. if m.IsNdk(ctx.Config()) {
  90. return "NDK"
  91. }
  92. if m.isImplementationForLLNDKPublic() {
  93. return "LLNDK"
  94. }
  95. if m.UseVndk() && m.IsVndk() && !m.IsVndkPrivate() {
  96. if m.IsVndkSp() {
  97. if m.IsVndkExt() {
  98. return "VNDK-SP-ext"
  99. } else {
  100. return "VNDK-SP"
  101. }
  102. } else {
  103. if m.IsVndkExt() {
  104. return "VNDK-ext"
  105. } else {
  106. return "VNDK-core"
  107. }
  108. }
  109. }
  110. if m.library.hasStubsVariants() && !m.InProduct() && !m.InVendor() {
  111. return "PLATFORM"
  112. }
  113. if headerAbiChecker.enabled() {
  114. if m.InProduct() {
  115. return "PRODUCT"
  116. }
  117. if m.InVendor() {
  118. return "VENDOR"
  119. }
  120. return "PLATFORM"
  121. }
  122. return ""
  123. }
  124. // Called from sabiDepsMutator to check whether ABI dumps should be created for this module.
  125. // ctx should be wrapping a native library type module.
  126. func shouldCreateSourceAbiDumpForLibrary(ctx android.BaseModuleContext) bool {
  127. // Only generate ABI dump for device modules.
  128. if !ctx.Device() {
  129. return false
  130. }
  131. m := ctx.Module().(*Module)
  132. // Only create ABI dump for native library module types.
  133. if m.library == nil {
  134. return false
  135. }
  136. // Create ABI dump for static libraries only if they are dependencies of ABI checked libraries.
  137. if m.library.static() {
  138. return m.sabi.shouldCreateSourceAbiDump()
  139. }
  140. // Module is shared library type.
  141. // Don't check uninstallable modules.
  142. if m.IsHideFromMake() {
  143. return false
  144. }
  145. // Don't check ramdisk or recovery variants. Only check core, vendor or product variants.
  146. if m.InRamdisk() || m.InVendorRamdisk() || m.InRecovery() {
  147. return false
  148. }
  149. // Don't create ABI dump for prebuilts.
  150. if m.Prebuilt() != nil || m.IsSnapshotPrebuilt() {
  151. return false
  152. }
  153. // Coverage builds have extra symbols.
  154. if m.isCoverageVariant() {
  155. return false
  156. }
  157. // Some sanitizer variants may have different ABI.
  158. if m.sanitize != nil && !m.sanitize.isVariantOnProductionDevice() {
  159. return false
  160. }
  161. // Don't create ABI dump for stubs.
  162. if m.isNDKStubLibrary() || m.IsLlndk() || m.IsStubs() {
  163. return false
  164. }
  165. isPlatformVariant := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
  166. if isPlatformVariant {
  167. // Bionic libraries that are installed to the bootstrap directory are not ABI checked.
  168. // Only the runtime APEX variants, which are the implementation libraries of bionic NDK stubs,
  169. // are checked.
  170. if InstallToBootstrap(m.BaseModuleName(), ctx.Config()) {
  171. return false
  172. }
  173. } else {
  174. // Don't create ABI dump if this library is for APEX but isn't exported.
  175. if !m.HasStubsVariants() {
  176. return false
  177. }
  178. }
  179. return classifySourceAbiDump(ctx) != ""
  180. }
  181. // Mark the direct and transitive dependencies of libraries that need ABI check, so that ABI dumps
  182. // of their dependencies would be generated.
  183. func sabiDepsMutator(mctx android.TopDownMutatorContext) {
  184. // Escape hatch to not check any ABI dump.
  185. if mctx.Config().IsEnvTrue("SKIP_ABI_CHECKS") {
  186. return
  187. }
  188. // Only create ABI dump for native shared libraries and their static library dependencies.
  189. if m, ok := mctx.Module().(*Module); ok && m.sabi != nil {
  190. if shouldCreateSourceAbiDumpForLibrary(mctx) {
  191. // Mark this module so that .sdump / .lsdump for this library can be generated.
  192. m.sabi.Properties.ShouldCreateSourceAbiDump = true
  193. // Mark all of its static library dependencies.
  194. mctx.VisitDirectDeps(func(child android.Module) {
  195. depTag := mctx.OtherModuleDependencyTag(child)
  196. if IsStaticDepTag(depTag) || depTag == reuseObjTag {
  197. if c, ok := child.(*Module); ok && c.sabi != nil {
  198. // Mark this module so that .sdump for this static library can be generated.
  199. c.sabi.Properties.ShouldCreateSourceAbiDump = true
  200. }
  201. }
  202. })
  203. }
  204. }
  205. }
  206. // Add an entry to the global list of lsdump. The list is exported to a Make variable by
  207. // `cc.makeVarsProvider`.
  208. func addLsdumpPath(lsdumpPath string) {
  209. lsdumpPathsLock.Lock()
  210. defer lsdumpPathsLock.Unlock()
  211. lsdumpPaths = append(lsdumpPaths, lsdumpPath)
  212. }