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 15package cc 16 17import ( 18 "android/soong/android" 19 "android/soong/genrule" 20) 21 22// sdkTransitionMutator creates a platform and an SDK variant for modules 23// that set sdk_version, and ignores sdk_version for the platform 24// variant. The SDK variant will be used for embedding in APKs 25// that may be installed on older platforms. Apexes use their own 26// variants that enforce backwards compatibility. 27type sdkTransitionMutator struct{} 28 29func (sdkTransitionMutator) Split(ctx android.BaseModuleContext) []string { 30 if ctx.Os() != android.Android { 31 return []string{""} 32 } 33 34 switch m := ctx.Module().(type) { 35 case LinkableInterface: 36 if m.AlwaysSdk() { 37 if !m.UseSdk() && !m.SplitPerApiLevel() { 38 ctx.ModuleErrorf("UseSdk() must return true when AlwaysSdk is set, did the factory forget to set Sdk_version?") 39 } 40 return []string{"sdk"} 41 } else if m.UseSdk() || m.SplitPerApiLevel() { 42 return []string{"", "sdk"} 43 } else { 44 return []string{""} 45 } 46 case *genrule.Module: 47 if p, ok := m.Extra.(*GenruleExtraProperties); ok { 48 if String(p.Sdk_version) != "" { 49 return []string{"", "sdk"} 50 } else { 51 return []string{""} 52 } 53 } 54 } 55 56 return []string{""} 57} 58 59func (sdkTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string { 60 return sourceVariation 61} 62 63func (sdkTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string { 64 if ctx.Os() != android.Android { 65 return "" 66 } 67 switch m := ctx.Module().(type) { 68 case LinkableInterface: 69 if m.AlwaysSdk() { 70 return "sdk" 71 } else if m.UseSdk() || m.SplitPerApiLevel() { 72 return incomingVariation 73 } 74 case *genrule.Module: 75 if p, ok := m.Extra.(*GenruleExtraProperties); ok { 76 if String(p.Sdk_version) != "" { 77 return incomingVariation 78 } 79 } 80 } 81 82 if ctx.IsAddingDependency() { 83 return incomingVariation 84 } else { 85 return "" 86 } 87} 88 89func (sdkTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) { 90 if ctx.Os() != android.Android { 91 return 92 } 93 94 switch m := ctx.Module().(type) { 95 case LinkableInterface: 96 ccModule, isCcModule := ctx.Module().(*Module) 97 if m.AlwaysSdk() { 98 if variation != "sdk" { 99 ctx.ModuleErrorf("tried to create variation %q for module with AlwaysSdk set, expected \"sdk\"", variation) 100 } 101 102 ccModule.Properties.IsSdkVariant = true 103 } else if m.UseSdk() || m.SplitPerApiLevel() { 104 if variation == "" { 105 // Clear the sdk_version property for the platform (non-SDK) variant so later code 106 // doesn't get confused by it. 107 ccModule.Properties.Sdk_version = nil 108 } else { 109 // Mark the SDK variant. 110 ccModule.Properties.IsSdkVariant = true 111 112 // SDK variant never gets installed because the variant is to be embedded in 113 // APKs, not to be installed to the platform. 114 ccModule.Properties.PreventInstall = true 115 } 116 117 if ctx.Config().UnbundledBuildApps() { 118 if variation == "" { 119 // For an unbundled apps build, hide the platform variant from Make 120 // so that other Make modules don't link against it, but against the 121 // SDK variant. 122 ccModule.Properties.HideFromMake = true 123 } 124 } else { 125 if variation == "sdk" { 126 // For a platform build, mark the SDK variant so that it gets a ".sdk" suffix when 127 // exposed to Make. 128 ccModule.Properties.SdkAndPlatformVariantVisibleToMake = true 129 } 130 } 131 } else { 132 if isCcModule { 133 // Clear the sdk_version property for modules that don't have an SDK variant so 134 // later code doesn't get confused by it. 135 ccModule.Properties.Sdk_version = nil 136 } 137 } 138 } 139} 140