blob: ce0fdc2e35a70ff7032886a481f096eb454f4755 [file] [log] [blame]
Colin Crossc511bc52020-04-07 16:50:32 +00001// 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// 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.
27func sdkMutator(ctx android.BottomUpMutatorContext) {
28 if ctx.Os() != android.Android {
29 return
30 }
31
32 switch m := ctx.Module().(type) {
33 case LinkableInterface:
Kiyoung Kimd5d1ab12022-11-28 16:47:10 +090034 ccModule, isCcModule := ctx.Module().(*Module)
Colin Crossc511bc52020-04-07 16:50:32 +000035 if m.AlwaysSdk() {
Colin Cross1348ce32020-10-01 13:37:16 -070036 if !m.UseSdk() && !m.SplitPerApiLevel() {
Colin Crossc511bc52020-04-07 16:50:32 +000037 ctx.ModuleErrorf("UseSdk() must return true when AlwaysSdk is set, did the factory forget to set Sdk_version?")
38 }
Lukacs T. Berki2063a0d2021-06-17 09:32:36 +020039 modules := ctx.CreateVariations("sdk")
40 modules[0].(*Module).Properties.IsSdkVariant = true
Colin Cross1348ce32020-10-01 13:37:16 -070041 } else if m.UseSdk() || m.SplitPerApiLevel() {
Colin Crossc511bc52020-04-07 16:50:32 +000042 modules := ctx.CreateVariations("", "sdk")
Colin Cross94e347e2021-01-19 14:56:07 -080043
44 // Clear the sdk_version property for the platform (non-SDK) variant so later code
45 // doesn't get confused by it.
Colin Crossc511bc52020-04-07 16:50:32 +000046 modules[0].(*Module).Properties.Sdk_version = nil
Colin Cross94e347e2021-01-19 14:56:07 -080047
48 // Mark the SDK variant.
Colin Crossc511bc52020-04-07 16:50:32 +000049 modules[1].(*Module).Properties.IsSdkVariant = true
50
Martin Stjernholmfd9eb4b2020-06-17 01:13:15 +010051 if ctx.Config().UnbundledBuildApps() {
Colin Cross94e347e2021-01-19 14:56:07 -080052 // For an unbundled apps build, hide the platform variant from Make.
Colin Crossc511bc52020-04-07 16:50:32 +000053 modules[0].(*Module).Properties.HideFromMake = true
Colin Crossbd3a16b2023-04-25 11:30:51 -070054 modules[0].(*Module).Properties.PreventInstall = true
Colin Crossc511bc52020-04-07 16:50:32 +000055 } else {
Colin Cross94e347e2021-01-19 14:56:07 -080056 // For a platform build, mark the SDK variant so that it gets a ".sdk" suffix when
57 // exposed to Make.
Colin Crossc511bc52020-04-07 16:50:32 +000058 modules[1].(*Module).Properties.SdkAndPlatformVariantVisibleToMake = true
Colin Crossbd3a16b2023-04-25 11:30:51 -070059 modules[1].(*Module).Properties.PreventInstall = true
Colin Crossc511bc52020-04-07 16:50:32 +000060 }
61 ctx.AliasVariation("")
62 } else {
Kiyoung Kimd5d1ab12022-11-28 16:47:10 +090063 if isCcModule {
Colin Cross94e347e2021-01-19 14:56:07 -080064 // Clear the sdk_version property for modules that don't have an SDK variant so
65 // later code doesn't get confused by it.
Kiyoung Kimd5d1ab12022-11-28 16:47:10 +090066 ccModule.Properties.Sdk_version = nil
Colin Cross94e347e2021-01-19 14:56:07 -080067 }
Colin Crossc511bc52020-04-07 16:50:32 +000068 ctx.CreateVariations("")
69 ctx.AliasVariation("")
70 }
71 case *genrule.Module:
72 if p, ok := m.Extra.(*GenruleExtraProperties); ok {
73 if String(p.Sdk_version) != "" {
74 ctx.CreateVariations("", "sdk")
75 } else {
76 ctx.CreateVariations("")
77 }
78 ctx.AliasVariation("")
79 }
Kiyoung Kimee58c932022-10-25 22:59:41 +090080 case *CcApiVariant:
Kiyoung Kimd5d1ab12022-11-28 16:47:10 +090081 ccApiVariant, _ := ctx.Module().(*CcApiVariant)
82 if String(ccApiVariant.properties.Variant) == "ndk" {
83 ctx.CreateVariations("sdk")
84 } else {
85 ctx.CreateVariations("")
86 }
Colin Crossc511bc52020-04-07 16:50:32 +000087 }
88}