sdk.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2020 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. "android/soong/android"
  17. "android/soong/genrule"
  18. )
  19. // sdkMutator sets a creates a platform and an SDK variant for modules
  20. // that set sdk_version, and ignores sdk_version for the platform
  21. // variant. The SDK variant will be used for embedding in APKs
  22. // that may be installed on older platforms. Apexes use their own
  23. // variants that enforce backwards compatibility.
  24. func sdkMutator(ctx android.BottomUpMutatorContext) {
  25. if ctx.Os() != android.Android {
  26. return
  27. }
  28. switch m := ctx.Module().(type) {
  29. case LinkableInterface:
  30. ccModule, isCcModule := ctx.Module().(*Module)
  31. if m.AlwaysSdk() {
  32. if !m.UseSdk() && !m.SplitPerApiLevel() {
  33. ctx.ModuleErrorf("UseSdk() must return true when AlwaysSdk is set, did the factory forget to set Sdk_version?")
  34. }
  35. modules := ctx.CreateVariations("sdk")
  36. modules[0].(*Module).Properties.IsSdkVariant = true
  37. } else if m.UseSdk() || m.SplitPerApiLevel() {
  38. modules := ctx.CreateVariations("", "sdk")
  39. // Clear the sdk_version property for the platform (non-SDK) variant so later code
  40. // doesn't get confused by it.
  41. modules[0].(*Module).Properties.Sdk_version = nil
  42. // Mark the SDK variant.
  43. modules[1].(*Module).Properties.IsSdkVariant = true
  44. if ctx.Config().UnbundledBuildApps() {
  45. // For an unbundled apps build, hide the platform variant from Make.
  46. modules[0].(*Module).Properties.HideFromMake = true
  47. modules[0].(*Module).Properties.PreventInstall = true
  48. } else {
  49. // For a platform build, mark the SDK variant so that it gets a ".sdk" suffix when
  50. // exposed to Make.
  51. modules[1].(*Module).Properties.SdkAndPlatformVariantVisibleToMake = true
  52. modules[1].(*Module).Properties.PreventInstall = true
  53. }
  54. ctx.AliasVariation("")
  55. } else if isCcModule && ccModule.isImportedApiLibrary() {
  56. apiLibrary, _ := ccModule.linker.(*apiLibraryDecorator)
  57. if apiLibrary.hasNDKStubs() && ccModule.canUseSdk() {
  58. variations := []string{"sdk"}
  59. if apiLibrary.hasApexStubs() {
  60. variations = append(variations, "")
  61. }
  62. // Handle cc_api_library module with NDK stubs and variants only which can use SDK
  63. modules := ctx.CreateVariations(variations...)
  64. // Mark the SDK variant.
  65. modules[0].(*Module).Properties.IsSdkVariant = true
  66. if ctx.Config().UnbundledBuildApps() {
  67. if apiLibrary.hasApexStubs() {
  68. // For an unbundled apps build, hide the platform variant from Make.
  69. modules[1].(*Module).Properties.HideFromMake = true
  70. }
  71. modules[1].(*Module).Properties.PreventInstall = true
  72. } else {
  73. // For a platform build, mark the SDK variant so that it gets a ".sdk" suffix when
  74. // exposed to Make.
  75. modules[0].(*Module).Properties.SdkAndPlatformVariantVisibleToMake = true
  76. // SDK variant is not supposed to be installed
  77. modules[0].(*Module).Properties.PreventInstall = true
  78. }
  79. } else {
  80. ccModule.Properties.Sdk_version = nil
  81. ctx.CreateVariations("")
  82. ctx.AliasVariation("")
  83. }
  84. } else {
  85. if isCcModule {
  86. // Clear the sdk_version property for modules that don't have an SDK variant so
  87. // later code doesn't get confused by it.
  88. ccModule.Properties.Sdk_version = nil
  89. }
  90. ctx.CreateVariations("")
  91. ctx.AliasVariation("")
  92. }
  93. case *genrule.Module:
  94. if p, ok := m.Extra.(*GenruleExtraProperties); ok {
  95. if String(p.Sdk_version) != "" {
  96. ctx.CreateVariations("", "sdk")
  97. } else {
  98. ctx.CreateVariations("")
  99. }
  100. ctx.AliasVariation("")
  101. }
  102. case *snapshotModule:
  103. ctx.CreateVariations("")
  104. case *CcApiVariant:
  105. ccApiVariant, _ := ctx.Module().(*CcApiVariant)
  106. if String(ccApiVariant.properties.Variant) == "ndk" {
  107. ctx.CreateVariations("sdk")
  108. } else {
  109. ctx.CreateVariations("")
  110. }
  111. }
  112. }