| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 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 |  | 
|  | 15 | package cc | 
|  | 16 |  | 
|  | 17 | import ( | 
|  | 18 | "android/soong/android" | 
|  | 19 | "android/soong/genrule" | 
|  | 20 | ) | 
|  | 21 |  | 
| Colin Cross | ac57a6c | 2024-06-26 13:09:53 -0700 | [diff] [blame] | 22 | // sdkTransitionMutator creates a platform and an SDK variant for modules | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 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. | 
| Colin Cross | ac57a6c | 2024-06-26 13:09:53 -0700 | [diff] [blame] | 27 | type sdkTransitionMutator struct{} | 
|  | 28 |  | 
|  | 29 | func (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 | case *CcApiVariant: | 
|  | 55 | ccApiVariant, _ := ctx.Module().(*CcApiVariant) | 
|  | 56 | if String(ccApiVariant.properties.Variant) == "ndk" { | 
|  | 57 | return []string{"sdk"} | 
|  | 58 | } else { | 
|  | 59 | return []string{""} | 
|  | 60 | } | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 | return []string{""} | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | func (sdkTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string { | 
|  | 67 | return sourceVariation | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | func (sdkTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string { | 
|  | 71 | if ctx.Os() != android.Android { | 
|  | 72 | return "" | 
|  | 73 | } | 
|  | 74 | switch m := ctx.Module().(type) { | 
|  | 75 | case LinkableInterface: | 
|  | 76 | if m.AlwaysSdk() { | 
|  | 77 | return "sdk" | 
|  | 78 | } else if m.UseSdk() || m.SplitPerApiLevel() { | 
|  | 79 | return incomingVariation | 
|  | 80 | } | 
|  | 81 | case *genrule.Module: | 
|  | 82 | if p, ok := m.Extra.(*GenruleExtraProperties); ok { | 
|  | 83 | if String(p.Sdk_version) != "" { | 
|  | 84 | return incomingVariation | 
|  | 85 | } | 
|  | 86 | } | 
|  | 87 | case *CcApiVariant: | 
|  | 88 | ccApiVariant, _ := ctx.Module().(*CcApiVariant) | 
|  | 89 | if String(ccApiVariant.properties.Variant) == "ndk" { | 
|  | 90 | return "sdk" | 
|  | 91 | } | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | if ctx.IsAddingDependency() { | 
|  | 95 | return incomingVariation | 
|  | 96 | } else { | 
|  | 97 | return "" | 
|  | 98 | } | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | func (sdkTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) { | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 102 | if ctx.Os() != android.Android { | 
|  | 103 | return | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | switch m := ctx.Module().(type) { | 
|  | 107 | case LinkableInterface: | 
| Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 108 | ccModule, isCcModule := ctx.Module().(*Module) | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 109 | if m.AlwaysSdk() { | 
| Colin Cross | ac57a6c | 2024-06-26 13:09:53 -0700 | [diff] [blame] | 110 | if variation != "sdk" { | 
|  | 111 | ctx.ModuleErrorf("tried to create variation %q for module with AlwaysSdk set, expected \"sdk\"", variation) | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 112 | } | 
| Colin Cross | ac57a6c | 2024-06-26 13:09:53 -0700 | [diff] [blame] | 113 |  | 
|  | 114 | ccModule.Properties.IsSdkVariant = true | 
| Colin Cross | 1348ce3 | 2020-10-01 13:37:16 -0700 | [diff] [blame] | 115 | } else if m.UseSdk() || m.SplitPerApiLevel() { | 
| Colin Cross | ac57a6c | 2024-06-26 13:09:53 -0700 | [diff] [blame] | 116 | if variation == "" { | 
|  | 117 | // Clear the sdk_version property for the platform (non-SDK) variant so later code | 
|  | 118 | // doesn't get confused by it. | 
|  | 119 | ccModule.Properties.Sdk_version = nil | 
|  | 120 | } else { | 
|  | 121 | // Mark the SDK variant. | 
|  | 122 | ccModule.Properties.IsSdkVariant = true | 
| Colin Cross | 94e347e | 2021-01-19 14:56:07 -0800 | [diff] [blame] | 123 |  | 
| Colin Cross | ac57a6c | 2024-06-26 13:09:53 -0700 | [diff] [blame] | 124 | // SDK variant never gets installed because the variant is to be embedded in | 
|  | 125 | // APKs, not to be installed to the platform. | 
|  | 126 | ccModule.Properties.PreventInstall = true | 
|  | 127 | } | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 128 |  | 
| Martin Stjernholm | fd9eb4b | 2020-06-17 01:13:15 +0100 | [diff] [blame] | 129 | if ctx.Config().UnbundledBuildApps() { | 
| Colin Cross | ac57a6c | 2024-06-26 13:09:53 -0700 | [diff] [blame] | 130 | if variation == "" { | 
|  | 131 | // For an unbundled apps build, hide the platform variant from Make | 
|  | 132 | // so that other Make modules don't link against it, but against the | 
|  | 133 | // SDK variant. | 
|  | 134 | ccModule.Properties.HideFromMake = true | 
|  | 135 | } | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 136 | } else { | 
| Colin Cross | ac57a6c | 2024-06-26 13:09:53 -0700 | [diff] [blame] | 137 | if variation == "sdk" { | 
|  | 138 | // For a platform build, mark the SDK variant so that it gets a ".sdk" suffix when | 
|  | 139 | // exposed to Make. | 
|  | 140 | ccModule.Properties.SdkAndPlatformVariantVisibleToMake = true | 
|  | 141 | } | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 142 | } | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 143 | } else { | 
| Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 144 | if isCcModule { | 
| Colin Cross | 94e347e | 2021-01-19 14:56:07 -0800 | [diff] [blame] | 145 | // Clear the sdk_version property for modules that don't have an SDK variant so | 
|  | 146 | // later code doesn't get confused by it. | 
| Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 147 | ccModule.Properties.Sdk_version = nil | 
| Colin Cross | 94e347e | 2021-01-19 14:56:07 -0800 | [diff] [blame] | 148 | } | 
| Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 149 | } | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 150 | } | 
|  | 151 | } |