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 "android/soong/snapshot" 23) 24 25func init() { 26 android.RegisterModuleType("cc_genrule", GenRuleFactory) 27} 28 29type GenruleExtraProperties struct { 30 Vendor_available *bool 31 Odm_available *bool 32 Product_available *bool 33 Ramdisk_available *bool 34 Vendor_ramdisk_available *bool 35 Recovery_available *bool 36 Sdk_version *string 37} 38 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 41// variations. The following environment variables will be set when the command 42// execute: 43// 44// CC_ARCH the name of the architecture the command is being executed for 45// 46// CC_MULTILIB "lib32" if the architecture the command is being executed for is 32-bit, 47// "lib64" if it is 64-bit. 48// 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. 51// 52// CC_OS the name of the OS the command is being executed for. 53func GenRuleFactory() android.Module { 54 module := genrule.NewGenRule() 55 56 extra := &GenruleExtraProperties{} 57 module.Extra = extra 58 module.ImageInterface = extra 59 module.CmdModifier = genruleCmdModifier 60 module.AddProperties(module.Extra) 61 62 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibBoth) 63 64 android.InitApexModule(module) 65 android.InitBazelModule(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.BaseModuleContext) {} 81 82func (g *GenruleExtraProperties) CoreVariantNeeded(ctx android.BaseModuleContext) bool { 83 if ctx.DeviceConfig().VndkVersion() == "" { 84 return true 85 } 86 87 if ctx.DeviceConfig().ProductVndkVersion() != "" && ctx.ProductSpecific() { 88 return false 89 } 90 91 return !(ctx.SocSpecific() || ctx.DeviceSpecific()) 92} 93 94func (g *GenruleExtraProperties) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool { 95 return Bool(g.Ramdisk_available) 96} 97 98func (g *GenruleExtraProperties) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool { 99 return Bool(g.Vendor_ramdisk_available) 100} 101 102func (g *GenruleExtraProperties) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool { 103 return false 104} 105 106func (g *GenruleExtraProperties) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool { 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 != "" && 111 !snapshot.IsRecoveryProprietaryModule(ctx) { 112 return false 113 } else { 114 return Bool(g.Recovery_available) 115 } 116} 117 118func (g *GenruleExtraProperties) ExtraImageVariations(ctx android.BaseModuleContext) []string { 119 if ctx.DeviceConfig().VndkVersion() == "" { 120 return nil 121 } 122 123 var variants []string 124 if Bool(g.Vendor_available) || Bool(g.Odm_available) || ctx.SocSpecific() || ctx.DeviceSpecific() { 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. 130 if vndkVersion == "current" || !snapshot.IsVendorProprietaryModule(ctx) { 131 variants = append(variants, VendorVariationPrefix+ctx.DeviceConfig().PlatformVndkVersion()) 132 } else { 133 variants = append(variants, VendorVariationPrefix+vndkVersion) 134 } 135 } 136 137 if ctx.DeviceConfig().ProductVndkVersion() == "" { 138 return variants 139 } 140 141 if Bool(g.Product_available) || ctx.ProductSpecific() { 142 variants = append(variants, ProductVariationPrefix+ctx.DeviceConfig().PlatformVndkVersion()) 143 if vndkVersion := ctx.DeviceConfig().ProductVndkVersion(); vndkVersion != "current" { 144 variants = append(variants, ProductVariationPrefix+vndkVersion) 145 } 146 } 147 148 return variants 149} 150 151func (g *GenruleExtraProperties) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) { 152} 153