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