blob: 0c06ae6dde2e848535cf6fc12c75556ddf7516a6 [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"
Kiyoung Kim48f37782021-07-07 12:42:39 +090022 "android/soong/snapshot"
Dan Willemsen3e5bdf22017-09-13 18:37:08 -070023)
24
25func init() {
Wei Libcd39942021-09-16 23:57:28 +000026 android.RegisterModuleType("cc_genrule", GenRuleFactory)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -070027}
28
Jiyong Park3f736c92018-05-24 13:36:56 +090029type GenruleExtraProperties struct {
Yifan Hong60e0cfb2020-10-21 15:17:56 -070030 Vendor_available *bool
Justin Yunebcf0c52021-01-08 18:00:19 +090031 Odm_available *bool
Justin Yun63e9ec72020-10-29 16:49:43 +090032 Product_available *bool
Yifan Hong60e0cfb2020-10-21 15:17:56 -070033 Ramdisk_available *bool
34 Vendor_ramdisk_available *bool
35 Recovery_available *bool
36 Sdk_version *string
Jiyong Park3f736c92018-05-24 13:36:56 +090037}
38
Dan Willemsen3e5bdf22017-09-13 18:37:08 -070039// cc_genrule is a genrule that can depend on other cc_* objects.
40// The cmd may be run multiple times, once for each of the different arch/etc
Colin Crossf3bfd022021-09-27 15:15:06 -070041// variations. The following environment variables will be set when the command
42// execute:
43//
Colin Crossd079e0b2022-08-16 10:27:33 -070044// CC_ARCH the name of the architecture the command is being executed for
Colin Crossf3bfd022021-09-27 15:15:06 -070045//
Colin Crossd079e0b2022-08-16 10:27:33 -070046// CC_MULTILIB "lib32" if the architecture the command is being executed for is 32-bit,
47// "lib64" if it is 64-bit.
Colin Crossf3bfd022021-09-27 15:15:06 -070048//
Colin Crossd079e0b2022-08-16 10:27:33 -070049// CC_NATIVE_BRIDGE the name of the subdirectory that native bridge libraries are stored in if
50// the architecture has native bridge enabled, empty if it is disabled.
Patrick Rohr5307b3c2022-12-01 19:03:05 +000051//
52// CC_OS the name of the OS the command is being executed for.
Wei Libcd39942021-09-16 23:57:28 +000053func GenRuleFactory() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -070054 module := genrule.NewGenRule()
55
Colin Cross7228ecd2019-11-18 16:00:16 -080056 extra := &GenruleExtraProperties{}
57 module.Extra = extra
58 module.ImageInterface = extra
Colin Crossf3bfd022021-09-27 15:15:06 -070059 module.CmdModifier = genruleCmdModifier
Dan Willemsen3e5bdf22017-09-13 18:37:08 -070060 module.AddProperties(module.Extra)
61
62 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibBoth)
63
Jiyong Parkfc752ca2019-06-12 13:27:29 +090064 android.InitApexModule(module)
65
Dan Willemsen3e5bdf22017-09-13 18:37:08 -070066 return module
67}
Colin Cross7228ecd2019-11-18 16:00:16 -080068
Colin Crossf3bfd022021-09-27 15:15:06 -070069func genruleCmdModifier(ctx android.ModuleContext, cmd string) string {
70 target := ctx.Target()
71 arch := target.Arch.ArchType
Patrick Rohr5307b3c2022-12-01 19:03:05 +000072 osName := target.Os.Name
73 return fmt.Sprintf("CC_ARCH=%s CC_NATIVE_BRIDGE=%s CC_MULTILIB=%s CC_OS=%s && %s",
74 arch.Name, target.NativeBridgeRelativePath, arch.Multilib, osName, cmd)
Colin Crossf3bfd022021-09-27 15:15:06 -070075}
76
Colin Cross7228ecd2019-11-18 16:00:16 -080077var _ android.ImageInterface = (*GenruleExtraProperties)(nil)
78
79func (g *GenruleExtraProperties) ImageMutatorBegin(ctx android.BaseModuleContext) {}
80
81func (g *GenruleExtraProperties) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
82 if ctx.DeviceConfig().VndkVersion() == "" {
83 return true
84 }
85
Justin Yunaf1fde42023-09-27 16:22:10 +090086 if ctx.ProductSpecific() {
Justin Yun5f7f7e82019-11-18 19:52:14 +090087 return false
88 }
89
Justin Yunebcf0c52021-01-08 18:00:19 +090090 return !(ctx.SocSpecific() || ctx.DeviceSpecific())
Colin Cross7228ecd2019-11-18 16:00:16 -080091}
92
Yifan Hong1b3348d2020-01-21 15:53:22 -080093func (g *GenruleExtraProperties) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
94 return Bool(g.Ramdisk_available)
95}
96
Yifan Hong60e0cfb2020-10-21 15:17:56 -070097func (g *GenruleExtraProperties) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
98 return Bool(g.Vendor_ramdisk_available)
99}
100
Inseob Kim08758f02021-04-08 21:13:22 +0900101func (g *GenruleExtraProperties) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
102 return false
103}
104
Colin Cross7228ecd2019-11-18 16:00:16 -0800105func (g *GenruleExtraProperties) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
Jose Galmes6f843bc2020-12-11 13:36:29 -0800106 // If the build is using a snapshot, the recovery variant under AOSP directories
107 // is not needed.
108 recoverySnapshotVersion := ctx.DeviceConfig().RecoverySnapshotVersion()
109 if recoverySnapshotVersion != "current" && recoverySnapshotVersion != "" &&
Kiyoung Kim48f37782021-07-07 12:42:39 +0900110 !snapshot.IsRecoveryProprietaryModule(ctx) {
Jose Galmes6f843bc2020-12-11 13:36:29 -0800111 return false
112 } else {
113 return Bool(g.Recovery_available)
114 }
Colin Cross7228ecd2019-11-18 16:00:16 -0800115}
116
117func (g *GenruleExtraProperties) ExtraImageVariations(ctx android.BaseModuleContext) []string {
118 if ctx.DeviceConfig().VndkVersion() == "" {
119 return nil
120 }
121
Justin Yun5f7f7e82019-11-18 19:52:14 +0900122 var variants []string
Justin Yunebcf0c52021-01-08 18:00:19 +0900123 if Bool(g.Vendor_available) || Bool(g.Odm_available) || ctx.SocSpecific() || ctx.DeviceSpecific() {
Inseob Kim85708802020-06-02 00:06:15 +0900124 vndkVersion := ctx.DeviceConfig().VndkVersion()
125 // If vndkVersion is current, we can always use PlatformVndkVersion.
126 // If not, we assume modules under proprietary paths are compatible for
127 // BOARD_VNDK_VERSION. The other modules are regarded as AOSP, that is
128 // PLATFORM_VNDK_VERSION.
Kiyoung Kim48f37782021-07-07 12:42:39 +0900129 if vndkVersion == "current" || !snapshot.IsVendorProprietaryModule(ctx) {
Inseob Kim85708802020-06-02 00:06:15 +0900130 variants = append(variants, VendorVariationPrefix+ctx.DeviceConfig().PlatformVndkVersion())
131 } else {
Colin Cross7228ecd2019-11-18 16:00:16 -0800132 variants = append(variants, VendorVariationPrefix+vndkVersion)
133 }
Justin Yun5f7f7e82019-11-18 19:52:14 +0900134 }
135
Justin Yun6977e8a2020-10-29 18:24:11 +0900136 if Bool(g.Product_available) || ctx.ProductSpecific() {
Justin Yun5f7f7e82019-11-18 19:52:14 +0900137 variants = append(variants, ProductVariationPrefix+ctx.DeviceConfig().PlatformVndkVersion())
Justin Yun5f7f7e82019-11-18 19:52:14 +0900138 }
139
140 return variants
Colin Cross7228ecd2019-11-18 16:00:16 -0800141}
142
143func (g *GenruleExtraProperties) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
144}