blob: dc1261d683176675b058b67d2ed4b1b8cc3cf4c6 [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 }
54 case *CcApiVariant:
55 ccApiVariant, _ := ctx.Module().(*CcApiVariant)
56 if String(ccApiVariant.properties.Variant) == "ndk" {
57 return []string{"sdk"}
58 } else {
59 return []string{""}
60 }
61 }
62
63 return []string{""}
64}
65
66func (sdkTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
67 return sourceVariation
68}
69
70func (sdkTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
71 if ctx.Os() != android.Android {
72 return ""
73 }
74 switch m := ctx.Module().(type) {
75 case LinkableInterface:
76 if m.AlwaysSdk() {
77 return "sdk"
78 } else if m.UseSdk() || m.SplitPerApiLevel() {
79 return incomingVariation
80 }
81 case *genrule.Module:
82 if p, ok := m.Extra.(*GenruleExtraProperties); ok {
83 if String(p.Sdk_version) != "" {
84 return incomingVariation
85 }
86 }
87 case *CcApiVariant:
88 ccApiVariant, _ := ctx.Module().(*CcApiVariant)
89 if String(ccApiVariant.properties.Variant) == "ndk" {
90 return "sdk"
91 }
92 }
93
94 if ctx.IsAddingDependency() {
95 return incomingVariation
96 } else {
97 return ""
98 }
99}
100
101func (sdkTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) {
Colin Crossc511bc52020-04-07 16:50:32 +0000102 if ctx.Os() != android.Android {
103 return
104 }
105
106 switch m := ctx.Module().(type) {
107 case LinkableInterface:
Kiyoung Kimd5d1ab12022-11-28 16:47:10 +0900108 ccModule, isCcModule := ctx.Module().(*Module)
Colin Crossc511bc52020-04-07 16:50:32 +0000109 if m.AlwaysSdk() {
Colin Crossac57a6c2024-06-26 13:09:53 -0700110 if variation != "sdk" {
111 ctx.ModuleErrorf("tried to create variation %q for module with AlwaysSdk set, expected \"sdk\"", variation)
Colin Crossc511bc52020-04-07 16:50:32 +0000112 }
Colin Crossac57a6c2024-06-26 13:09:53 -0700113
114 ccModule.Properties.IsSdkVariant = true
Colin Cross1348ce32020-10-01 13:37:16 -0700115 } else if m.UseSdk() || m.SplitPerApiLevel() {
Colin Crossac57a6c2024-06-26 13:09:53 -0700116 if variation == "" {
117 // Clear the sdk_version property for the platform (non-SDK) variant so later code
118 // doesn't get confused by it.
119 ccModule.Properties.Sdk_version = nil
120 } else {
121 // Mark the SDK variant.
122 ccModule.Properties.IsSdkVariant = true
Colin Cross94e347e2021-01-19 14:56:07 -0800123
Colin Crossac57a6c2024-06-26 13:09:53 -0700124 // SDK variant never gets installed because the variant is to be embedded in
125 // APKs, not to be installed to the platform.
126 ccModule.Properties.PreventInstall = true
127 }
Colin Crossc511bc52020-04-07 16:50:32 +0000128
Martin Stjernholmfd9eb4b2020-06-17 01:13:15 +0100129 if ctx.Config().UnbundledBuildApps() {
Colin Crossac57a6c2024-06-26 13:09:53 -0700130 if variation == "" {
131 // For an unbundled apps build, hide the platform variant from Make
132 // so that other Make modules don't link against it, but against the
133 // SDK variant.
134 ccModule.Properties.HideFromMake = true
135 }
Colin Crossc511bc52020-04-07 16:50:32 +0000136 } else {
Colin Crossac57a6c2024-06-26 13:09:53 -0700137 if variation == "sdk" {
138 // For a platform build, mark the SDK variant so that it gets a ".sdk" suffix when
139 // exposed to Make.
140 ccModule.Properties.SdkAndPlatformVariantVisibleToMake = true
141 }
Colin Crossc511bc52020-04-07 16:50:32 +0000142 }
Colin Crossc511bc52020-04-07 16:50:32 +0000143 } else {
Kiyoung Kimd5d1ab12022-11-28 16:47:10 +0900144 if isCcModule {
Colin Cross94e347e2021-01-19 14:56:07 -0800145 // Clear the sdk_version property for modules that don't have an SDK variant so
146 // later code doesn't get confused by it.
Kiyoung Kimd5d1ab12022-11-28 16:47:10 +0900147 ccModule.Properties.Sdk_version = nil
Colin Cross94e347e2021-01-19 14:56:07 -0800148 }
Kiyoung Kimd5d1ab12022-11-28 16:47:10 +0900149 }
Colin Crossc511bc52020-04-07 16:50:32 +0000150 }
151}