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: |
Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 34 | ccModule, isCcModule := ctx.Module().(*Module) |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 35 | if m.AlwaysSdk() { |
Colin Cross | 1348ce3 | 2020-10-01 13:37:16 -0700 | [diff] [blame] | 36 | if !m.UseSdk() && !m.SplitPerApiLevel() { |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 37 | ctx.ModuleErrorf("UseSdk() must return true when AlwaysSdk is set, did the factory forget to set Sdk_version?") |
| 38 | } |
Lukacs T. Berki | 2063a0d | 2021-06-17 09:32:36 +0200 | [diff] [blame] | 39 | modules := ctx.CreateVariations("sdk") |
| 40 | modules[0].(*Module).Properties.IsSdkVariant = true |
Colin Cross | 1348ce3 | 2020-10-01 13:37:16 -0700 | [diff] [blame] | 41 | } else if m.UseSdk() || m.SplitPerApiLevel() { |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 42 | modules := ctx.CreateVariations("", "sdk") |
Colin Cross | 94e347e | 2021-01-19 14:56:07 -0800 | [diff] [blame] | 43 | |
| 44 | // Clear the sdk_version property for the platform (non-SDK) variant so later code |
| 45 | // doesn't get confused by it. |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 46 | modules[0].(*Module).Properties.Sdk_version = nil |
Colin Cross | 94e347e | 2021-01-19 14:56:07 -0800 | [diff] [blame] | 47 | |
| 48 | // Mark the SDK variant. |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 49 | modules[1].(*Module).Properties.IsSdkVariant = true |
| 50 | |
Martin Stjernholm | fd9eb4b | 2020-06-17 01:13:15 +0100 | [diff] [blame] | 51 | if ctx.Config().UnbundledBuildApps() { |
Colin Cross | 94e347e | 2021-01-19 14:56:07 -0800 | [diff] [blame] | 52 | // For an unbundled apps build, hide the platform variant from Make. |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 53 | modules[0].(*Module).Properties.HideFromMake = true |
Colin Cross | bd3a16b | 2023-04-25 11:30:51 -0700 | [diff] [blame] | 54 | modules[0].(*Module).Properties.PreventInstall = true |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 55 | } else { |
Colin Cross | 94e347e | 2021-01-19 14:56:07 -0800 | [diff] [blame] | 56 | // For a platform build, mark the SDK variant so that it gets a ".sdk" suffix when |
| 57 | // exposed to Make. |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 58 | modules[1].(*Module).Properties.SdkAndPlatformVariantVisibleToMake = true |
Colin Cross | bd3a16b | 2023-04-25 11:30:51 -0700 | [diff] [blame] | 59 | modules[1].(*Module).Properties.PreventInstall = true |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 60 | } |
| 61 | ctx.AliasVariation("") |
Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 62 | } else if isCcModule && ccModule.isImportedApiLibrary() { |
| 63 | apiLibrary, _ := ccModule.linker.(*apiLibraryDecorator) |
| 64 | if apiLibrary.hasNDKStubs() && ccModule.canUseSdk() { |
Kiyoung Kim | 76b06f3 | 2023-02-06 22:08:13 +0900 | [diff] [blame] | 65 | variations := []string{"sdk"} |
| 66 | if apiLibrary.hasApexStubs() { |
| 67 | variations = append(variations, "") |
| 68 | } |
Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 69 | // Handle cc_api_library module with NDK stubs and variants only which can use SDK |
Kiyoung Kim | 76b06f3 | 2023-02-06 22:08:13 +0900 | [diff] [blame] | 70 | modules := ctx.CreateVariations(variations...) |
Jooyung Han | 54f7805 | 2023-02-20 18:17:47 +0900 | [diff] [blame] | 71 | // Mark the SDK variant. |
Kiyoung Kim | 76b06f3 | 2023-02-06 22:08:13 +0900 | [diff] [blame] | 72 | modules[0].(*Module).Properties.IsSdkVariant = true |
Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 73 | if ctx.Config().UnbundledBuildApps() { |
Kiyoung Kim | 76b06f3 | 2023-02-06 22:08:13 +0900 | [diff] [blame] | 74 | if apiLibrary.hasApexStubs() { |
| 75 | // For an unbundled apps build, hide the platform variant from Make. |
| 76 | modules[1].(*Module).Properties.HideFromMake = true |
Kiyoung Kim | 76b06f3 | 2023-02-06 22:08:13 +0900 | [diff] [blame] | 77 | } |
Colin Cross | bd3a16b | 2023-04-25 11:30:51 -0700 | [diff] [blame] | 78 | modules[1].(*Module).Properties.PreventInstall = true |
Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 79 | } else { |
| 80 | // For a platform build, mark the SDK variant so that it gets a ".sdk" suffix when |
| 81 | // exposed to Make. |
Kiyoung Kim | 76b06f3 | 2023-02-06 22:08:13 +0900 | [diff] [blame] | 82 | modules[0].(*Module).Properties.SdkAndPlatformVariantVisibleToMake = true |
| 83 | // SDK variant is not supposed to be installed |
| 84 | modules[0].(*Module).Properties.PreventInstall = true |
Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 85 | } |
| 86 | } else { |
| 87 | ccModule.Properties.Sdk_version = nil |
| 88 | ctx.CreateVariations("") |
| 89 | ctx.AliasVariation("") |
| 90 | } |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 91 | } else { |
Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 92 | if isCcModule { |
Colin Cross | 94e347e | 2021-01-19 14:56:07 -0800 | [diff] [blame] | 93 | // Clear the sdk_version property for modules that don't have an SDK variant so |
| 94 | // later code doesn't get confused by it. |
Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 95 | ccModule.Properties.Sdk_version = nil |
Colin Cross | 94e347e | 2021-01-19 14:56:07 -0800 | [diff] [blame] | 96 | } |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 97 | ctx.CreateVariations("") |
| 98 | ctx.AliasVariation("") |
| 99 | } |
| 100 | case *genrule.Module: |
| 101 | if p, ok := m.Extra.(*GenruleExtraProperties); ok { |
| 102 | if String(p.Sdk_version) != "" { |
| 103 | ctx.CreateVariations("", "sdk") |
| 104 | } else { |
| 105 | ctx.CreateVariations("") |
| 106 | } |
| 107 | ctx.AliasVariation("") |
| 108 | } |
Kiyoung Kim | 48f3778 | 2021-07-07 12:42:39 +0900 | [diff] [blame] | 109 | case *snapshotModule: |
Colin Cross | e0edaf9 | 2021-01-11 17:31:17 -0800 | [diff] [blame] | 110 | ctx.CreateVariations("") |
Kiyoung Kim | ee58c93 | 2022-10-25 22:59:41 +0900 | [diff] [blame] | 111 | case *CcApiVariant: |
Kiyoung Kim | d5d1ab1 | 2022-11-28 16:47:10 +0900 | [diff] [blame] | 112 | ccApiVariant, _ := ctx.Module().(*CcApiVariant) |
| 113 | if String(ccApiVariant.properties.Variant) == "ndk" { |
| 114 | ctx.CreateVariations("sdk") |
| 115 | } else { |
| 116 | ctx.CreateVariations("") |
| 117 | } |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 118 | } |
| 119 | } |