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
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:
34		ccModule, isCcModule := ctx.Module().(*Module)
35		if m.AlwaysSdk() {
36			if !m.UseSdk() && !m.SplitPerApiLevel() {
37				ctx.ModuleErrorf("UseSdk() must return true when AlwaysSdk is set, did the factory forget to set Sdk_version?")
38			}
39			modules := ctx.CreateVariations("sdk")
40			modules[0].(*Module).Properties.IsSdkVariant = true
41		} else if m.UseSdk() || m.SplitPerApiLevel() {
42			modules := ctx.CreateVariations("", "sdk")
43
44			// Clear the sdk_version property for the platform (non-SDK) variant so later code
45			// doesn't get confused by it.
46			modules[0].(*Module).Properties.Sdk_version = nil
47
48			// Mark the SDK variant.
49			modules[1].(*Module).Properties.IsSdkVariant = true
50
51			if ctx.Config().UnbundledBuildApps() {
52				// For an unbundled apps build, hide the platform variant from Make
53				// so that other Make modules don't link against it, but against the
54				// SDK variant.
55				modules[0].(*Module).Properties.HideFromMake = true
56			} else {
57				// For a platform build, mark the SDK variant so that it gets a ".sdk" suffix when
58				// exposed to Make.
59				modules[1].(*Module).Properties.SdkAndPlatformVariantVisibleToMake = true
60			}
61			// SDK variant never gets installed because the variant is to be embedded in
62			// APKs, not to be installed to the platform.
63			modules[1].(*Module).Properties.PreventInstall = true
64			ctx.AliasVariation("")
65		} else {
66			if isCcModule {
67				// Clear the sdk_version property for modules that don't have an SDK variant so
68				// later code doesn't get confused by it.
69				ccModule.Properties.Sdk_version = nil
70			}
71			ctx.CreateVariations("")
72			ctx.AliasVariation("")
73		}
74	case *genrule.Module:
75		if p, ok := m.Extra.(*GenruleExtraProperties); ok {
76			if String(p.Sdk_version) != "" {
77				ctx.CreateVariations("", "sdk")
78			} else {
79				ctx.CreateVariations("")
80			}
81			ctx.AliasVariation("")
82		}
83	case *CcApiVariant:
84		ccApiVariant, _ := ctx.Module().(*CcApiVariant)
85		if String(ccApiVariant.properties.Variant) == "ndk" {
86			ctx.CreateVariations("sdk")
87		} else {
88			ctx.CreateVariations("")
89		}
90	}
91}
92