blob: bd6c5f1c74ccc4e7b4bdac33e72d4b98dede9c43 [file] [log] [blame]
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001// 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 (
Colin Crossf3bfd022021-09-27 15:15:06 -070018 "fmt"
19
Dan Willemsen3e5bdf22017-09-13 18:37:08 -070020 "android/soong/android"
21 "android/soong/genrule"
22)
23
24func init() {
Wei Libcd39942021-09-16 23:57:28 +000025 android.RegisterModuleType("cc_genrule", GenRuleFactory)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -070026}
27
Jiyong Park3f736c92018-05-24 13:36:56 +090028type GenruleExtraProperties struct {
Yifan Hong60e0cfb2020-10-21 15:17:56 -070029 Vendor_available *bool
Justin Yunebcf0c52021-01-08 18:00:19 +090030 Odm_available *bool
Justin Yun63e9ec72020-10-29 16:49:43 +090031 Product_available *bool
Yifan Hong60e0cfb2020-10-21 15:17:56 -070032 Ramdisk_available *bool
33 Vendor_ramdisk_available *bool
34 Recovery_available *bool
35 Sdk_version *string
Jiyong Park3f736c92018-05-24 13:36:56 +090036}
37
Dan Willemsen3e5bdf22017-09-13 18:37:08 -070038// 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
Colin Crossf3bfd022021-09-27 15:15:06 -070040// variations. The following environment variables will be set when the command
41// execute:
42//
Colin Crossd079e0b2022-08-16 10:27:33 -070043// CC_ARCH the name of the architecture the command is being executed for
Colin Crossf3bfd022021-09-27 15:15:06 -070044//
Colin Crossd079e0b2022-08-16 10:27:33 -070045// CC_MULTILIB "lib32" if the architecture the command is being executed for is 32-bit,
46// "lib64" if it is 64-bit.
Colin Crossf3bfd022021-09-27 15:15:06 -070047//
Colin Crossd079e0b2022-08-16 10:27:33 -070048// 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.
Patrick Rohr5307b3c2022-12-01 19:03:05 +000050//
51// CC_OS the name of the OS the command is being executed for.
Wei Libcd39942021-09-16 23:57:28 +000052func GenRuleFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -070053 module := genrule.NewGenRule()
54
Colin Cross7228ecd2019-11-18 16:00:16 -080055 extra := &GenruleExtraProperties{}
56 module.Extra = extra
57 module.ImageInterface = extra
Colin Crossf3bfd022021-09-27 15:15:06 -070058 module.CmdModifier = genruleCmdModifier
Dan Willemsen3e5bdf22017-09-13 18:37:08 -070059 module.AddProperties(module.Extra)
60
61 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibBoth)
62
Jiyong Parkfc752ca2019-06-12 13:27:29 +090063 android.InitApexModule(module)
64
Aleks Todorovec2cf1e2024-06-13 17:31:33 +010065 android.InitDefaultableModule(module)
66
Dan Willemsen3e5bdf22017-09-13 18:37:08 -070067 return module
68}
Colin Cross7228ecd2019-11-18 16:00:16 -080069
Colin Crossf3bfd022021-09-27 15:15:06 -070070func genruleCmdModifier(ctx android.ModuleContext, cmd string) string {
71 target := ctx.Target()
72 arch := target.Arch.ArchType
Patrick Rohr5307b3c2022-12-01 19:03:05 +000073 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)
Colin Crossf3bfd022021-09-27 15:15:06 -070076}
77
Colin Cross7228ecd2019-11-18 16:00:16 -080078var _ android.ImageInterface = (*GenruleExtraProperties)(nil)
79
Cole Faustfa6e0fd2024-10-15 15:22:57 -070080func (g *GenruleExtraProperties) ImageMutatorBegin(ctx android.ImageInterfaceContext) {}
Colin Cross7228ecd2019-11-18 16:00:16 -080081
Cole Faustfa6e0fd2024-10-15 15:22:57 -070082func (g *GenruleExtraProperties) VendorVariantNeeded(ctx android.ImageInterfaceContext) bool {
Jihoon Kang47e91842024-06-19 00:51:16 +000083 return Bool(g.Vendor_available) || Bool(g.Odm_available) || ctx.SocSpecific() || ctx.DeviceSpecific()
84}
85
Cole Faustfa6e0fd2024-10-15 15:22:57 -070086func (g *GenruleExtraProperties) ProductVariantNeeded(ctx android.ImageInterfaceContext) bool {
Jihoon Kang47e91842024-06-19 00:51:16 +000087 return Bool(g.Product_available) || ctx.ProductSpecific()
88}
89
Cole Faustfa6e0fd2024-10-15 15:22:57 -070090func (g *GenruleExtraProperties) CoreVariantNeeded(ctx android.ImageInterfaceContext) bool {
Kiyoung Kim853e3912024-01-09 09:51:00 +090091 return !(ctx.SocSpecific() || ctx.DeviceSpecific() || ctx.ProductSpecific())
Colin Cross7228ecd2019-11-18 16:00:16 -080092}
93
Cole Faustfa6e0fd2024-10-15 15:22:57 -070094func (g *GenruleExtraProperties) RamdiskVariantNeeded(ctx android.ImageInterfaceContext) bool {
Yifan Hong1b3348d2020-01-21 15:53:22 -080095 return Bool(g.Ramdisk_available)
96}
97
Cole Faustfa6e0fd2024-10-15 15:22:57 -070098func (g *GenruleExtraProperties) VendorRamdiskVariantNeeded(ctx android.ImageInterfaceContext) bool {
Yifan Hong60e0cfb2020-10-21 15:17:56 -070099 return Bool(g.Vendor_ramdisk_available)
100}
101
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700102func (g *GenruleExtraProperties) DebugRamdiskVariantNeeded(ctx android.ImageInterfaceContext) bool {
Inseob Kim08758f02021-04-08 21:13:22 +0900103 return false
104}
105
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700106func (g *GenruleExtraProperties) RecoveryVariantNeeded(ctx android.ImageInterfaceContext) bool {
Jose Galmes6f843bc2020-12-11 13:36:29 -0800107 // If the build is using a snapshot, the recovery variant under AOSP directories
108 // is not needed.
Kiyoung Kim37693d02024-04-04 09:56:15 +0900109 return Bool(g.Recovery_available)
Colin Cross7228ecd2019-11-18 16:00:16 -0800110}
111
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700112func (g *GenruleExtraProperties) ExtraImageVariations(ctx android.ImageInterfaceContext) []string {
Jihoon Kang47e91842024-06-19 00:51:16 +0000113 return nil
Colin Cross7228ecd2019-11-18 16:00:16 -0800114}
115
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700116func (g *GenruleExtraProperties) SetImageVariation(ctx android.ImageInterfaceContext, variation string) {
Colin Cross7228ecd2019-11-18 16:00:16 -0800117}