blob: 5dd44d8b864c6b0cb6be49dfbeb2446bbdc36012 [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
Colin Crossac57a6c2024-06-26 13:09:53 -070022// sdkTransitionMutator creates a platform and an SDK variant for modules
Colin Crossc511bc52020-04-07 16:50:32 +000023// 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 Crossac57a6c2024-06-26 13:09:53 -070027type sdkTransitionMutator struct{}
28
29func (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 Crossac57a6c2024-06-26 13:09:53 -070054 }
55
56 return []string{""}
57}
58
59func (sdkTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
60 return sourceVariation
61}
62
63func (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 Crossac57a6c2024-06-26 13:09:53 -070080 }
81
82 if ctx.IsAddingDependency() {
83 return incomingVariation
84 } else {
85 return ""
86 }
87}
88
89func (sdkTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) {
Colin Crossc511bc52020-04-07 16:50:32 +000090 if ctx.Os() != android.Android {
91 return
92 }
93
94 switch m := ctx.Module().(type) {
95 case LinkableInterface:
Kiyoung Kimd5d1ab12022-11-28 16:47:10 +090096 ccModule, isCcModule := ctx.Module().(*Module)
Colin Crossc511bc52020-04-07 16:50:32 +000097 if m.AlwaysSdk() {
Colin Crossac57a6c2024-06-26 13:09:53 -070098 if variation != "sdk" {
99 ctx.ModuleErrorf("tried to create variation %q for module with AlwaysSdk set, expected \"sdk\"", variation)
Colin Crossc511bc52020-04-07 16:50:32 +0000100 }
Colin Crossac57a6c2024-06-26 13:09:53 -0700101
102 ccModule.Properties.IsSdkVariant = true
Colin Cross1348ce32020-10-01 13:37:16 -0700103 } else if m.UseSdk() || m.SplitPerApiLevel() {
Colin Crossac57a6c2024-06-26 13:09:53 -0700104 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 Cross94e347e2021-01-19 14:56:07 -0800111
Colin Crossac57a6c2024-06-26 13:09:53 -0700112 // 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 Crossc511bc52020-04-07 16:50:32 +0000116
Martin Stjernholmfd9eb4b2020-06-17 01:13:15 +0100117 if ctx.Config().UnbundledBuildApps() {
Colin Crossac57a6c2024-06-26 13:09:53 -0700118 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 Crossc511bc52020-04-07 16:50:32 +0000124 } else {
Colin Crossac57a6c2024-06-26 13:09:53 -0700125 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 Crossc511bc52020-04-07 16:50:32 +0000130 }
Colin Crossc511bc52020-04-07 16:50:32 +0000131 } else {
Kiyoung Kimd5d1ab12022-11-28 16:47:10 +0900132 if isCcModule {
Colin Cross94e347e2021-01-19 14:56:07 -0800133 // 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 Kimd5d1ab12022-11-28 16:47:10 +0900135 ccModule.Properties.Sdk_version = nil
Colin Cross94e347e2021-01-19 14:56:07 -0800136 }
Kiyoung Kimd5d1ab12022-11-28 16:47:10 +0900137 }
Colin Crossc511bc52020-04-07 16:50:32 +0000138 }
139}