| 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 | } | 
| Colin Cross | ac57a6c | 2024-06-26 13:09:53 -0700 | [diff] [blame] | 54 | } | 
|  | 55 |  | 
|  | 56 | return []string{""} | 
|  | 57 | } | 
|  | 58 |  | 
|  | 59 | func (sdkTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string { | 
|  | 60 | return sourceVariation | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 | func (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 | } | 
| Colin Cross | ac57a6c | 2024-06-26 13:09:53 -0700 | [diff] [blame] | 80 | } | 
|  | 81 |  | 
|  | 82 | if ctx.IsAddingDependency() { | 
|  | 83 | return incomingVariation | 
|  | 84 | } else { | 
|  | 85 | return "" | 
|  | 86 | } | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | func (sdkTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) { | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 90 | if ctx.Os() != android.Android { | 
|  | 91 | return | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | switch m := ctx.Module().(type) { | 
|  | 95 | case LinkableInterface: | 
| Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 96 | ccModule, isCcModule := ctx.Module().(*Module) | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 97 | if m.AlwaysSdk() { | 
| Colin Cross | ac57a6c | 2024-06-26 13:09:53 -0700 | [diff] [blame] | 98 | if variation != "sdk" { | 
|  | 99 | 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] | 100 | } | 
| Colin Cross | ac57a6c | 2024-06-26 13:09:53 -0700 | [diff] [blame] | 101 |  | 
|  | 102 | ccModule.Properties.IsSdkVariant = true | 
| Colin Cross | 1348ce3 | 2020-10-01 13:37:16 -0700 | [diff] [blame] | 103 | } else if m.UseSdk() || m.SplitPerApiLevel() { | 
| Colin Cross | ac57a6c | 2024-06-26 13:09:53 -0700 | [diff] [blame] | 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 | 
| Colin Cross | 94e347e | 2021-01-19 14:56:07 -0800 | [diff] [blame] | 111 |  | 
| Colin Cross | ac57a6c | 2024-06-26 13:09:53 -0700 | [diff] [blame] | 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 | } | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 116 |  | 
| Martin Stjernholm | fd9eb4b | 2020-06-17 01:13:15 +0100 | [diff] [blame] | 117 | if ctx.Config().UnbundledBuildApps() { | 
| Colin Cross | ac57a6c | 2024-06-26 13:09:53 -0700 | [diff] [blame] | 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 | } | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 124 | } else { | 
| Colin Cross | ac57a6c | 2024-06-26 13:09:53 -0700 | [diff] [blame] | 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 | } | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 130 | } | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 131 | } else { | 
| Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 132 | if isCcModule { | 
| Colin Cross | 94e347e | 2021-01-19 14:56:07 -0800 | [diff] [blame] | 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. | 
| Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 135 | ccModule.Properties.Sdk_version = nil | 
| Colin Cross | 94e347e | 2021-01-19 14:56:07 -0800 | [diff] [blame] | 136 | } | 
| Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 137 | } | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 138 | } | 
|  | 139 | } |