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 | |
| 22 | // sdkMutator sets a 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. |
| 27 | func sdkMutator(ctx android.BottomUpMutatorContext) { |
| 28 | if ctx.Os() != android.Android { |
| 29 | return |
| 30 | } |
| 31 | |
| 32 | switch m := ctx.Module().(type) { |
| 33 | case LinkableInterface: |
| 34 | if m.AlwaysSdk() { |
Colin Cross | 1348ce3 | 2020-10-01 13:37:16 -0700 | [diff] [blame] | 35 | if !m.UseSdk() && !m.SplitPerApiLevel() { |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 36 | ctx.ModuleErrorf("UseSdk() must return true when AlwaysSdk is set, did the factory forget to set Sdk_version?") |
| 37 | } |
Lukacs T. Berki | 2063a0d | 2021-06-17 09:32:36 +0200 | [diff] [blame] | 38 | modules := ctx.CreateVariations("sdk") |
| 39 | modules[0].(*Module).Properties.IsSdkVariant = true |
Colin Cross | 1348ce3 | 2020-10-01 13:37:16 -0700 | [diff] [blame] | 40 | } else if m.UseSdk() || m.SplitPerApiLevel() { |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 41 | modules := ctx.CreateVariations("", "sdk") |
Colin Cross | 94e347e | 2021-01-19 14:56:07 -0800 | [diff] [blame] | 42 | |
| 43 | // Clear the sdk_version property for the platform (non-SDK) variant so later code |
| 44 | // doesn't get confused by it. |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 45 | modules[0].(*Module).Properties.Sdk_version = nil |
Colin Cross | 94e347e | 2021-01-19 14:56:07 -0800 | [diff] [blame] | 46 | |
| 47 | // Mark the SDK variant. |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 48 | modules[1].(*Module).Properties.IsSdkVariant = true |
| 49 | |
Martin Stjernholm | fd9eb4b | 2020-06-17 01:13:15 +0100 | [diff] [blame] | 50 | if ctx.Config().UnbundledBuildApps() { |
Colin Cross | 94e347e | 2021-01-19 14:56:07 -0800 | [diff] [blame] | 51 | // For an unbundled apps build, hide the platform variant from Make. |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 52 | modules[0].(*Module).Properties.HideFromMake = true |
Martin Stjernholm | 02229a2 | 2020-06-02 18:57:05 +0100 | [diff] [blame] | 53 | modules[0].(*Module).Properties.PreventInstall = true |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 54 | } else { |
Colin Cross | 94e347e | 2021-01-19 14:56:07 -0800 | [diff] [blame] | 55 | // For a platform build, mark the SDK variant so that it gets a ".sdk" suffix when |
| 56 | // exposed to Make. |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 57 | modules[1].(*Module).Properties.SdkAndPlatformVariantVisibleToMake = true |
| 58 | modules[1].(*Module).Properties.PreventInstall = true |
| 59 | } |
| 60 | ctx.AliasVariation("") |
| 61 | } else { |
Colin Cross | 94e347e | 2021-01-19 14:56:07 -0800 | [diff] [blame] | 62 | if m, ok := ctx.Module().(*Module); ok { |
| 63 | // Clear the sdk_version property for modules that don't have an SDK variant so |
| 64 | // later code doesn't get confused by it. |
| 65 | m.Properties.Sdk_version = nil |
| 66 | } |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 67 | ctx.CreateVariations("") |
| 68 | ctx.AliasVariation("") |
| 69 | } |
| 70 | case *genrule.Module: |
| 71 | if p, ok := m.Extra.(*GenruleExtraProperties); ok { |
| 72 | if String(p.Sdk_version) != "" { |
| 73 | ctx.CreateVariations("", "sdk") |
| 74 | } else { |
| 75 | ctx.CreateVariations("") |
| 76 | } |
| 77 | ctx.AliasVariation("") |
| 78 | } |
Kiyoung Kim | 48f3778 | 2021-07-07 12:42:39 +0900 | [diff] [blame] | 79 | case *snapshotModule: |
Colin Cross | e0edaf9 | 2021-01-11 17:31:17 -0800 | [diff] [blame] | 80 | ctx.CreateVariations("") |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 81 | } |
| 82 | } |