1// Copyright 2017 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
22func init() {
23	android.RegisterModuleType("cc_genrule", genRuleFactory)
24}
25
26type GenruleExtraProperties struct {
27	Vendor_available         *bool
28	Odm_available            *bool
29	Product_available        *bool
30	Ramdisk_available        *bool
31	Vendor_ramdisk_available *bool
32	Recovery_available       *bool
33	Sdk_version              *string
34}
35
36// cc_genrule is a genrule that can depend on other cc_* objects.
37// The cmd may be run multiple times, once for each of the different arch/etc
38// variations.
39func genRuleFactory() android.Module {
40	module := genrule.NewGenRule()
41
42	extra := &GenruleExtraProperties{}
43	module.Extra = extra
44	module.ImageInterface = extra
45	module.AddProperties(module.Extra)
46
47	android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibBoth)
48
49	android.InitApexModule(module)
50
51	return module
52}
53
54var _ android.ImageInterface = (*GenruleExtraProperties)(nil)
55
56func (g *GenruleExtraProperties) ImageMutatorBegin(ctx android.BaseModuleContext) {}
57
58func (g *GenruleExtraProperties) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
59	if ctx.DeviceConfig().VndkVersion() == "" {
60		return true
61	}
62
63	if ctx.DeviceConfig().ProductVndkVersion() != "" && ctx.ProductSpecific() {
64		return false
65	}
66
67	return !(ctx.SocSpecific() || ctx.DeviceSpecific())
68}
69
70func (g *GenruleExtraProperties) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
71	return Bool(g.Ramdisk_available)
72}
73
74func (g *GenruleExtraProperties) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
75	return Bool(g.Vendor_ramdisk_available)
76}
77
78func (g *GenruleExtraProperties) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
79	return false
80}
81
82func (g *GenruleExtraProperties) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
83	// If the build is using a snapshot, the recovery variant under AOSP directories
84	// is not needed.
85	recoverySnapshotVersion := ctx.DeviceConfig().RecoverySnapshotVersion()
86	if recoverySnapshotVersion != "current" && recoverySnapshotVersion != "" &&
87		!isRecoveryProprietaryModule(ctx) {
88		return false
89	} else {
90		return Bool(g.Recovery_available)
91	}
92}
93
94func (g *GenruleExtraProperties) ExtraImageVariations(ctx android.BaseModuleContext) []string {
95	if ctx.DeviceConfig().VndkVersion() == "" {
96		return nil
97	}
98
99	var variants []string
100	if Bool(g.Vendor_available) || Bool(g.Odm_available) || ctx.SocSpecific() || ctx.DeviceSpecific() {
101		vndkVersion := ctx.DeviceConfig().VndkVersion()
102		// If vndkVersion is current, we can always use PlatformVndkVersion.
103		// If not, we assume modules under proprietary paths are compatible for
104		// BOARD_VNDK_VERSION. The other modules are regarded as AOSP, that is
105		// PLATFORM_VNDK_VERSION.
106		if vndkVersion == "current" || !isVendorProprietaryModule(ctx) {
107			variants = append(variants, VendorVariationPrefix+ctx.DeviceConfig().PlatformVndkVersion())
108		} else {
109			variants = append(variants, VendorVariationPrefix+vndkVersion)
110		}
111	}
112
113	if ctx.DeviceConfig().ProductVndkVersion() == "" {
114		return variants
115	}
116
117	if Bool(g.Product_available) || ctx.ProductSpecific() {
118		variants = append(variants, ProductVariationPrefix+ctx.DeviceConfig().PlatformVndkVersion())
119		if vndkVersion := ctx.DeviceConfig().ProductVndkVersion(); vndkVersion != "current" {
120			variants = append(variants, ProductVariationPrefix+vndkVersion)
121		}
122	}
123
124	return variants
125}
126
127func (g *GenruleExtraProperties) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
128}
129