| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 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 |  | 
|  | 15 | package cc | 
|  | 16 |  | 
|  | 17 | import ( | 
| Colin Cross | f3bfd02 | 2021-09-27 15:15:06 -0700 | [diff] [blame] | 18 | "fmt" | 
|  | 19 |  | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 20 | "android/soong/android" | 
|  | 21 | "android/soong/genrule" | 
| Kiyoung Kim | 48f3778 | 2021-07-07 12:42:39 +0900 | [diff] [blame] | 22 | "android/soong/snapshot" | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 23 | ) | 
|  | 24 |  | 
|  | 25 | func init() { | 
| Wei Li | bcd3994 | 2021-09-16 23:57:28 +0000 | [diff] [blame] | 26 | android.RegisterModuleType("cc_genrule", GenRuleFactory) | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 27 | } | 
|  | 28 |  | 
| Jiyong Park | 3f736c9 | 2018-05-24 13:36:56 +0900 | [diff] [blame] | 29 | type GenruleExtraProperties struct { | 
| Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 30 | Vendor_available         *bool | 
| Justin Yun | ebcf0c5 | 2021-01-08 18:00:19 +0900 | [diff] [blame] | 31 | Odm_available            *bool | 
| Justin Yun | 63e9ec7 | 2020-10-29 16:49:43 +0900 | [diff] [blame] | 32 | Product_available        *bool | 
| Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 33 | Ramdisk_available        *bool | 
|  | 34 | Vendor_ramdisk_available *bool | 
|  | 35 | Recovery_available       *bool | 
|  | 36 | Sdk_version              *string | 
| Jiyong Park | 3f736c9 | 2018-05-24 13:36:56 +0900 | [diff] [blame] | 37 | } | 
|  | 38 |  | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 39 | // 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 Cross | f3bfd02 | 2021-09-27 15:15:06 -0700 | [diff] [blame] | 41 | // variations.  The following environment variables will be set when the command | 
|  | 42 | // execute: | 
|  | 43 | // | 
| Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 44 | //	CC_ARCH           the name of the architecture the command is being executed for | 
| Colin Cross | f3bfd02 | 2021-09-27 15:15:06 -0700 | [diff] [blame] | 45 | // | 
| Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 46 | //	CC_MULTILIB       "lib32" if the architecture the command is being executed for is 32-bit, | 
|  | 47 | //	                  "lib64" if it is 64-bit. | 
| Colin Cross | f3bfd02 | 2021-09-27 15:15:06 -0700 | [diff] [blame] | 48 | // | 
| Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 49 | //	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 Rohr | 5307b3c | 2022-12-01 19:03:05 +0000 | [diff] [blame] | 51 | // | 
|  | 52 | //	CC_OS             the name of the OS the command is being executed for. | 
| Wei Li | bcd3994 | 2021-09-16 23:57:28 +0000 | [diff] [blame] | 53 | func GenRuleFactory() android.Module { | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 54 | module := genrule.NewGenRule() | 
|  | 55 |  | 
| Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 56 | extra := &GenruleExtraProperties{} | 
|  | 57 | module.Extra = extra | 
|  | 58 | module.ImageInterface = extra | 
| Colin Cross | f3bfd02 | 2021-09-27 15:15:06 -0700 | [diff] [blame] | 59 | module.CmdModifier = genruleCmdModifier | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 60 | module.AddProperties(module.Extra) | 
|  | 61 |  | 
|  | 62 | android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibBoth) | 
|  | 63 |  | 
| Jiyong Park | fc752ca | 2019-06-12 13:27:29 +0900 | [diff] [blame] | 64 | android.InitApexModule(module) | 
| Wei Li | bcd3994 | 2021-09-16 23:57:28 +0000 | [diff] [blame] | 65 | android.InitBazelModule(module) | 
| Jiyong Park | fc752ca | 2019-06-12 13:27:29 +0900 | [diff] [blame] | 66 |  | 
| Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 67 | return module | 
|  | 68 | } | 
| Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 69 |  | 
| Colin Cross | f3bfd02 | 2021-09-27 15:15:06 -0700 | [diff] [blame] | 70 | func genruleCmdModifier(ctx android.ModuleContext, cmd string) string { | 
|  | 71 | target := ctx.Target() | 
|  | 72 | arch := target.Arch.ArchType | 
| Patrick Rohr | 5307b3c | 2022-12-01 19:03:05 +0000 | [diff] [blame] | 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) | 
| Colin Cross | f3bfd02 | 2021-09-27 15:15:06 -0700 | [diff] [blame] | 76 | } | 
|  | 77 |  | 
| Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 78 | var _ android.ImageInterface = (*GenruleExtraProperties)(nil) | 
|  | 79 |  | 
|  | 80 | func (g *GenruleExtraProperties) ImageMutatorBegin(ctx android.BaseModuleContext) {} | 
|  | 81 |  | 
|  | 82 | func (g *GenruleExtraProperties) CoreVariantNeeded(ctx android.BaseModuleContext) bool { | 
|  | 83 | if ctx.DeviceConfig().VndkVersion() == "" { | 
|  | 84 | return true | 
|  | 85 | } | 
|  | 86 |  | 
| Justin Yun | af1fde4 | 2023-09-27 16:22:10 +0900 | [diff] [blame] | 87 | if ctx.ProductSpecific() { | 
| Justin Yun | 5f7f7e8 | 2019-11-18 19:52:14 +0900 | [diff] [blame] | 88 | return false | 
|  | 89 | } | 
|  | 90 |  | 
| Justin Yun | ebcf0c5 | 2021-01-08 18:00:19 +0900 | [diff] [blame] | 91 | return !(ctx.SocSpecific() || ctx.DeviceSpecific()) | 
| Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 92 | } | 
|  | 93 |  | 
| Yifan Hong | 1b3348d | 2020-01-21 15:53:22 -0800 | [diff] [blame] | 94 | func (g *GenruleExtraProperties) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool { | 
|  | 95 | return Bool(g.Ramdisk_available) | 
|  | 96 | } | 
|  | 97 |  | 
| Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 98 | func (g *GenruleExtraProperties) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool { | 
|  | 99 | return Bool(g.Vendor_ramdisk_available) | 
|  | 100 | } | 
|  | 101 |  | 
| Inseob Kim | 08758f0 | 2021-04-08 21:13:22 +0900 | [diff] [blame] | 102 | func (g *GenruleExtraProperties) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool { | 
|  | 103 | return false | 
|  | 104 | } | 
|  | 105 |  | 
| Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 106 | func (g *GenruleExtraProperties) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool { | 
| Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 107 | // If the build is using a snapshot, the recovery variant under AOSP directories | 
|  | 108 | // is not needed. | 
|  | 109 | recoverySnapshotVersion := ctx.DeviceConfig().RecoverySnapshotVersion() | 
|  | 110 | if recoverySnapshotVersion != "current" && recoverySnapshotVersion != "" && | 
| Kiyoung Kim | 48f3778 | 2021-07-07 12:42:39 +0900 | [diff] [blame] | 111 | !snapshot.IsRecoveryProprietaryModule(ctx) { | 
| Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 112 | return false | 
|  | 113 | } else { | 
|  | 114 | return Bool(g.Recovery_available) | 
|  | 115 | } | 
| Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 116 | } | 
|  | 117 |  | 
|  | 118 | func (g *GenruleExtraProperties) ExtraImageVariations(ctx android.BaseModuleContext) []string { | 
|  | 119 | if ctx.DeviceConfig().VndkVersion() == "" { | 
|  | 120 | return nil | 
|  | 121 | } | 
|  | 122 |  | 
| Justin Yun | 5f7f7e8 | 2019-11-18 19:52:14 +0900 | [diff] [blame] | 123 | var variants []string | 
| Justin Yun | ebcf0c5 | 2021-01-08 18:00:19 +0900 | [diff] [blame] | 124 | if Bool(g.Vendor_available) || Bool(g.Odm_available) || ctx.SocSpecific() || ctx.DeviceSpecific() { | 
| Inseob Kim | 8570880 | 2020-06-02 00:06:15 +0900 | [diff] [blame] | 125 | vndkVersion := ctx.DeviceConfig().VndkVersion() | 
|  | 126 | // If vndkVersion is current, we can always use PlatformVndkVersion. | 
|  | 127 | // If not, we assume modules under proprietary paths are compatible for | 
|  | 128 | // BOARD_VNDK_VERSION. The other modules are regarded as AOSP, that is | 
|  | 129 | // PLATFORM_VNDK_VERSION. | 
| Kiyoung Kim | 48f3778 | 2021-07-07 12:42:39 +0900 | [diff] [blame] | 130 | if vndkVersion == "current" || !snapshot.IsVendorProprietaryModule(ctx) { | 
| Inseob Kim | 8570880 | 2020-06-02 00:06:15 +0900 | [diff] [blame] | 131 | variants = append(variants, VendorVariationPrefix+ctx.DeviceConfig().PlatformVndkVersion()) | 
|  | 132 | } else { | 
| Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 133 | variants = append(variants, VendorVariationPrefix+vndkVersion) | 
|  | 134 | } | 
| Justin Yun | 5f7f7e8 | 2019-11-18 19:52:14 +0900 | [diff] [blame] | 135 | } | 
|  | 136 |  | 
| Justin Yun | 6977e8a | 2020-10-29 18:24:11 +0900 | [diff] [blame] | 137 | if Bool(g.Product_available) || ctx.ProductSpecific() { | 
| Justin Yun | 5f7f7e8 | 2019-11-18 19:52:14 +0900 | [diff] [blame] | 138 | variants = append(variants, ProductVariationPrefix+ctx.DeviceConfig().PlatformVndkVersion()) | 
| Justin Yun | 5f7f7e8 | 2019-11-18 19:52:14 +0900 | [diff] [blame] | 139 | } | 
|  | 140 |  | 
|  | 141 | return variants | 
| Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 142 | } | 
|  | 143 |  | 
|  | 144 | func (g *GenruleExtraProperties) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) { | 
|  | 145 | } |