1*333d2b36SAndroid Build Coastguard Worker// Copyright 2017 Google Inc. All rights reserved. 2*333d2b36SAndroid Build Coastguard Worker// 3*333d2b36SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License"); 4*333d2b36SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License. 5*333d2b36SAndroid Build Coastguard Worker// You may obtain a copy of the License at 6*333d2b36SAndroid Build Coastguard Worker// 7*333d2b36SAndroid Build Coastguard Worker// http://www.apache.org/licenses/LICENSE-2.0 8*333d2b36SAndroid Build Coastguard Worker// 9*333d2b36SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software 10*333d2b36SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS, 11*333d2b36SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*333d2b36SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and 13*333d2b36SAndroid Build Coastguard Worker// limitations under the License. 14*333d2b36SAndroid Build Coastguard Worker 15*333d2b36SAndroid Build Coastguard Workerpackage cc 16*333d2b36SAndroid Build Coastguard Worker 17*333d2b36SAndroid Build Coastguard Workerimport ( 18*333d2b36SAndroid Build Coastguard Worker "fmt" 19*333d2b36SAndroid Build Coastguard Worker 20*333d2b36SAndroid Build Coastguard Worker "android/soong/android" 21*333d2b36SAndroid Build Coastguard Worker "android/soong/genrule" 22*333d2b36SAndroid Build Coastguard Worker) 23*333d2b36SAndroid Build Coastguard Worker 24*333d2b36SAndroid Build Coastguard Workerfunc init() { 25*333d2b36SAndroid Build Coastguard Worker android.RegisterModuleType("cc_genrule", GenRuleFactory) 26*333d2b36SAndroid Build Coastguard Worker} 27*333d2b36SAndroid Build Coastguard Worker 28*333d2b36SAndroid Build Coastguard Workertype GenruleExtraProperties struct { 29*333d2b36SAndroid Build Coastguard Worker Vendor_available *bool 30*333d2b36SAndroid Build Coastguard Worker Odm_available *bool 31*333d2b36SAndroid Build Coastguard Worker Product_available *bool 32*333d2b36SAndroid Build Coastguard Worker Ramdisk_available *bool 33*333d2b36SAndroid Build Coastguard Worker Vendor_ramdisk_available *bool 34*333d2b36SAndroid Build Coastguard Worker Recovery_available *bool 35*333d2b36SAndroid Build Coastguard Worker Sdk_version *string 36*333d2b36SAndroid Build Coastguard Worker} 37*333d2b36SAndroid Build Coastguard Worker 38*333d2b36SAndroid Build Coastguard Worker// cc_genrule is a genrule that can depend on other cc_* objects. 39*333d2b36SAndroid Build Coastguard Worker// The cmd may be run multiple times, once for each of the different arch/etc 40*333d2b36SAndroid Build Coastguard Worker// variations. The following environment variables will be set when the command 41*333d2b36SAndroid Build Coastguard Worker// execute: 42*333d2b36SAndroid Build Coastguard Worker// 43*333d2b36SAndroid Build Coastguard Worker// CC_ARCH the name of the architecture the command is being executed for 44*333d2b36SAndroid Build Coastguard Worker// 45*333d2b36SAndroid Build Coastguard Worker// CC_MULTILIB "lib32" if the architecture the command is being executed for is 32-bit, 46*333d2b36SAndroid Build Coastguard Worker// "lib64" if it is 64-bit. 47*333d2b36SAndroid Build Coastguard Worker// 48*333d2b36SAndroid Build Coastguard Worker// CC_NATIVE_BRIDGE the name of the subdirectory that native bridge libraries are stored in if 49*333d2b36SAndroid Build Coastguard Worker// the architecture has native bridge enabled, empty if it is disabled. 50*333d2b36SAndroid Build Coastguard Worker// 51*333d2b36SAndroid Build Coastguard Worker// CC_OS the name of the OS the command is being executed for. 52*333d2b36SAndroid Build Coastguard Workerfunc GenRuleFactory() android.Module { 53*333d2b36SAndroid Build Coastguard Worker module := genrule.NewGenRule() 54*333d2b36SAndroid Build Coastguard Worker 55*333d2b36SAndroid Build Coastguard Worker extra := &GenruleExtraProperties{} 56*333d2b36SAndroid Build Coastguard Worker module.Extra = extra 57*333d2b36SAndroid Build Coastguard Worker module.ImageInterface = extra 58*333d2b36SAndroid Build Coastguard Worker module.CmdModifier = genruleCmdModifier 59*333d2b36SAndroid Build Coastguard Worker module.AddProperties(module.Extra) 60*333d2b36SAndroid Build Coastguard Worker 61*333d2b36SAndroid Build Coastguard Worker android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibBoth) 62*333d2b36SAndroid Build Coastguard Worker 63*333d2b36SAndroid Build Coastguard Worker android.InitApexModule(module) 64*333d2b36SAndroid Build Coastguard Worker 65*333d2b36SAndroid Build Coastguard Worker android.InitDefaultableModule(module) 66*333d2b36SAndroid Build Coastguard Worker 67*333d2b36SAndroid Build Coastguard Worker return module 68*333d2b36SAndroid Build Coastguard Worker} 69*333d2b36SAndroid Build Coastguard Worker 70*333d2b36SAndroid Build Coastguard Workerfunc genruleCmdModifier(ctx android.ModuleContext, cmd string) string { 71*333d2b36SAndroid Build Coastguard Worker target := ctx.Target() 72*333d2b36SAndroid Build Coastguard Worker arch := target.Arch.ArchType 73*333d2b36SAndroid Build Coastguard Worker osName := target.Os.Name 74*333d2b36SAndroid Build Coastguard Worker return fmt.Sprintf("CC_ARCH=%s CC_NATIVE_BRIDGE=%s CC_MULTILIB=%s CC_OS=%s && %s", 75*333d2b36SAndroid Build Coastguard Worker arch.Name, target.NativeBridgeRelativePath, arch.Multilib, osName, cmd) 76*333d2b36SAndroid Build Coastguard Worker} 77*333d2b36SAndroid Build Coastguard Worker 78*333d2b36SAndroid Build Coastguard Workervar _ android.ImageInterface = (*GenruleExtraProperties)(nil) 79*333d2b36SAndroid Build Coastguard Worker 80*333d2b36SAndroid Build Coastguard Workerfunc (g *GenruleExtraProperties) ImageMutatorBegin(ctx android.ImageInterfaceContext) {} 81*333d2b36SAndroid Build Coastguard Worker 82*333d2b36SAndroid Build Coastguard Workerfunc (g *GenruleExtraProperties) VendorVariantNeeded(ctx android.ImageInterfaceContext) bool { 83*333d2b36SAndroid Build Coastguard Worker return Bool(g.Vendor_available) || Bool(g.Odm_available) || ctx.SocSpecific() || ctx.DeviceSpecific() 84*333d2b36SAndroid Build Coastguard Worker} 85*333d2b36SAndroid Build Coastguard Worker 86*333d2b36SAndroid Build Coastguard Workerfunc (g *GenruleExtraProperties) ProductVariantNeeded(ctx android.ImageInterfaceContext) bool { 87*333d2b36SAndroid Build Coastguard Worker return Bool(g.Product_available) || ctx.ProductSpecific() 88*333d2b36SAndroid Build Coastguard Worker} 89*333d2b36SAndroid Build Coastguard Worker 90*333d2b36SAndroid Build Coastguard Workerfunc (g *GenruleExtraProperties) CoreVariantNeeded(ctx android.ImageInterfaceContext) bool { 91*333d2b36SAndroid Build Coastguard Worker return !(ctx.SocSpecific() || ctx.DeviceSpecific() || ctx.ProductSpecific()) 92*333d2b36SAndroid Build Coastguard Worker} 93*333d2b36SAndroid Build Coastguard Worker 94*333d2b36SAndroid Build Coastguard Workerfunc (g *GenruleExtraProperties) RamdiskVariantNeeded(ctx android.ImageInterfaceContext) bool { 95*333d2b36SAndroid Build Coastguard Worker return Bool(g.Ramdisk_available) 96*333d2b36SAndroid Build Coastguard Worker} 97*333d2b36SAndroid Build Coastguard Worker 98*333d2b36SAndroid Build Coastguard Workerfunc (g *GenruleExtraProperties) VendorRamdiskVariantNeeded(ctx android.ImageInterfaceContext) bool { 99*333d2b36SAndroid Build Coastguard Worker return Bool(g.Vendor_ramdisk_available) 100*333d2b36SAndroid Build Coastguard Worker} 101*333d2b36SAndroid Build Coastguard Worker 102*333d2b36SAndroid Build Coastguard Workerfunc (g *GenruleExtraProperties) DebugRamdiskVariantNeeded(ctx android.ImageInterfaceContext) bool { 103*333d2b36SAndroid Build Coastguard Worker return false 104*333d2b36SAndroid Build Coastguard Worker} 105*333d2b36SAndroid Build Coastguard Worker 106*333d2b36SAndroid Build Coastguard Workerfunc (g *GenruleExtraProperties) RecoveryVariantNeeded(ctx android.ImageInterfaceContext) bool { 107*333d2b36SAndroid Build Coastguard Worker // If the build is using a snapshot, the recovery variant under AOSP directories 108*333d2b36SAndroid Build Coastguard Worker // is not needed. 109*333d2b36SAndroid Build Coastguard Worker return Bool(g.Recovery_available) 110*333d2b36SAndroid Build Coastguard Worker} 111*333d2b36SAndroid Build Coastguard Worker 112*333d2b36SAndroid Build Coastguard Workerfunc (g *GenruleExtraProperties) ExtraImageVariations(ctx android.ImageInterfaceContext) []string { 113*333d2b36SAndroid Build Coastguard Worker return nil 114*333d2b36SAndroid Build Coastguard Worker} 115*333d2b36SAndroid Build Coastguard Worker 116*333d2b36SAndroid Build Coastguard Workerfunc (g *GenruleExtraProperties) SetImageVariation(ctx android.ImageInterfaceContext, variation string) { 117*333d2b36SAndroid Build Coastguard Worker} 118