xref: /aosp_15_r20/build/soong/cc/genrule.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
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	"fmt"
19
20	"android/soong/android"
21	"android/soong/genrule"
22)
23
24func init() {
25	android.RegisterModuleType("cc_genrule", GenRuleFactory)
26}
27
28type GenruleExtraProperties struct {
29	Vendor_available         *bool
30	Odm_available            *bool
31	Product_available        *bool
32	Ramdisk_available        *bool
33	Vendor_ramdisk_available *bool
34	Recovery_available       *bool
35	Sdk_version              *string
36}
37
38// cc_genrule is a genrule that can depend on other cc_* objects.
39// The cmd may be run multiple times, once for each of the different arch/etc
40// variations.  The following environment variables will be set when the command
41// execute:
42//
43//	CC_ARCH           the name of the architecture the command is being executed for
44//
45//	CC_MULTILIB       "lib32" if the architecture the command is being executed for is 32-bit,
46//	                  "lib64" if it is 64-bit.
47//
48//	CC_NATIVE_BRIDGE  the name of the subdirectory that native bridge libraries are stored in if
49//	                  the architecture has native bridge enabled, empty if it is disabled.
50//
51//	CC_OS             the name of the OS the command is being executed for.
52func GenRuleFactory() android.Module {
53	module := genrule.NewGenRule()
54
55	extra := &GenruleExtraProperties{}
56	module.Extra = extra
57	module.ImageInterface = extra
58	module.CmdModifier = genruleCmdModifier
59	module.AddProperties(module.Extra)
60
61	android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibBoth)
62
63	android.InitApexModule(module)
64
65	android.InitDefaultableModule(module)
66
67	return module
68}
69
70func genruleCmdModifier(ctx android.ModuleContext, cmd string) string {
71	target := ctx.Target()
72	arch := target.Arch.ArchType
73	osName := target.Os.Name
74	return fmt.Sprintf("CC_ARCH=%s CC_NATIVE_BRIDGE=%s CC_MULTILIB=%s CC_OS=%s && %s",
75		arch.Name, target.NativeBridgeRelativePath, arch.Multilib, osName, cmd)
76}
77
78var _ android.ImageInterface = (*GenruleExtraProperties)(nil)
79
80func (g *GenruleExtraProperties) ImageMutatorBegin(ctx android.ImageInterfaceContext) {}
81
82func (g *GenruleExtraProperties) VendorVariantNeeded(ctx android.ImageInterfaceContext) bool {
83	return Bool(g.Vendor_available) || Bool(g.Odm_available) || ctx.SocSpecific() || ctx.DeviceSpecific()
84}
85
86func (g *GenruleExtraProperties) ProductVariantNeeded(ctx android.ImageInterfaceContext) bool {
87	return Bool(g.Product_available) || ctx.ProductSpecific()
88}
89
90func (g *GenruleExtraProperties) CoreVariantNeeded(ctx android.ImageInterfaceContext) bool {
91	return !(ctx.SocSpecific() || ctx.DeviceSpecific() || ctx.ProductSpecific())
92}
93
94func (g *GenruleExtraProperties) RamdiskVariantNeeded(ctx android.ImageInterfaceContext) bool {
95	return Bool(g.Ramdisk_available)
96}
97
98func (g *GenruleExtraProperties) VendorRamdiskVariantNeeded(ctx android.ImageInterfaceContext) bool {
99	return Bool(g.Vendor_ramdisk_available)
100}
101
102func (g *GenruleExtraProperties) DebugRamdiskVariantNeeded(ctx android.ImageInterfaceContext) bool {
103	return false
104}
105
106func (g *GenruleExtraProperties) RecoveryVariantNeeded(ctx android.ImageInterfaceContext) bool {
107	// If the build is using a snapshot, the recovery variant under AOSP directories
108	// is not needed.
109	return Bool(g.Recovery_available)
110}
111
112func (g *GenruleExtraProperties) ExtraImageVariations(ctx android.ImageInterfaceContext) []string {
113	return nil
114}
115
116func (g *GenruleExtraProperties) SetImageVariation(ctx android.ImageInterfaceContext, variation string) {
117}
118