1*795d594fSAndroid Build Coastguard Worker// Copyright (C) 2016 The Android Open Source Project 2*795d594fSAndroid Build Coastguard Worker// 3*795d594fSAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License"); 4*795d594fSAndroid Build Coastguard Worker// you may not use this file except in compliance with the License. 5*795d594fSAndroid Build Coastguard Worker// You may obtain a copy of the License at 6*795d594fSAndroid Build Coastguard Worker// 7*795d594fSAndroid Build Coastguard Worker// http://www.apache.org/licenses/LICENSE-2.0 8*795d594fSAndroid Build Coastguard Worker// 9*795d594fSAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software 10*795d594fSAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS, 11*795d594fSAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*795d594fSAndroid Build Coastguard Worker// See the License for the specific language governing permissions and 13*795d594fSAndroid Build Coastguard Worker// limitations under the License. 14*795d594fSAndroid Build Coastguard Worker 15*795d594fSAndroid Build Coastguard Workerpackage art 16*795d594fSAndroid Build Coastguard Worker 17*795d594fSAndroid Build Coastguard Worker// This file implements the "codegen" property to apply different properties based on the currently 18*795d594fSAndroid Build Coastguard Worker// selected codegen arches, which defaults to all arches on the host and the primary and secondary 19*795d594fSAndroid Build Coastguard Worker// arches on the device. 20*795d594fSAndroid Build Coastguard Worker 21*795d594fSAndroid Build Coastguard Workerimport ( 22*795d594fSAndroid Build Coastguard Worker "sort" 23*795d594fSAndroid Build Coastguard Worker "strings" 24*795d594fSAndroid Build Coastguard Worker 25*795d594fSAndroid Build Coastguard Worker "android/soong/android" 26*795d594fSAndroid Build Coastguard Worker) 27*795d594fSAndroid Build Coastguard Worker 28*795d594fSAndroid Build Coastguard Workertype moduleType struct { 29*795d594fSAndroid Build Coastguard Worker library bool 30*795d594fSAndroid Build Coastguard Worker static bool 31*795d594fSAndroid Build Coastguard Worker shared bool 32*795d594fSAndroid Build Coastguard Worker} 33*795d594fSAndroid Build Coastguard Worker 34*795d594fSAndroid Build Coastguard Workervar ( 35*795d594fSAndroid Build Coastguard Worker staticLibrary = moduleType{true, true, false} 36*795d594fSAndroid Build Coastguard Worker sharedLibrary = moduleType{true, false, true} 37*795d594fSAndroid Build Coastguard Worker staticAndSharedLibrary = moduleType{true, true, true} 38*795d594fSAndroid Build Coastguard Worker binary = moduleType{false, false, false} 39*795d594fSAndroid Build Coastguard Worker) 40*795d594fSAndroid Build Coastguard Worker 41*795d594fSAndroid Build Coastguard Workerfunc codegen(ctx android.LoadHookContext, c *codegenProperties, t moduleType) { 42*795d594fSAndroid Build Coastguard Worker var hostArches, deviceArches []string 43*795d594fSAndroid Build Coastguard Worker 44*795d594fSAndroid Build Coastguard Worker e := ctx.Config().Getenv("ART_HOST_CODEGEN_ARCHS") 45*795d594fSAndroid Build Coastguard Worker if e == "" { 46*795d594fSAndroid Build Coastguard Worker hostArches = supportedArches 47*795d594fSAndroid Build Coastguard Worker } else { 48*795d594fSAndroid Build Coastguard Worker hostArches = strings.Split(e, " ") 49*795d594fSAndroid Build Coastguard Worker } 50*795d594fSAndroid Build Coastguard Worker 51*795d594fSAndroid Build Coastguard Worker e = ctx.Config().Getenv("ART_TARGET_CODEGEN_ARCHS") 52*795d594fSAndroid Build Coastguard Worker if e == "" { 53*795d594fSAndroid Build Coastguard Worker deviceArches = defaultDeviceCodegenArches(ctx) 54*795d594fSAndroid Build Coastguard Worker } else { 55*795d594fSAndroid Build Coastguard Worker deviceArches = strings.Split(e, " ") 56*795d594fSAndroid Build Coastguard Worker } 57*795d594fSAndroid Build Coastguard Worker 58*795d594fSAndroid Build Coastguard Worker getCodegenArchProperties := func(archName string) *codegenArchProperties { 59*795d594fSAndroid Build Coastguard Worker var arch *codegenArchProperties 60*795d594fSAndroid Build Coastguard Worker switch archName { 61*795d594fSAndroid Build Coastguard Worker case "arm": 62*795d594fSAndroid Build Coastguard Worker arch = &c.Codegen.Arm 63*795d594fSAndroid Build Coastguard Worker case "arm64": 64*795d594fSAndroid Build Coastguard Worker arch = &c.Codegen.Arm64 65*795d594fSAndroid Build Coastguard Worker case "riscv64": 66*795d594fSAndroid Build Coastguard Worker arch = &c.Codegen.Riscv64 67*795d594fSAndroid Build Coastguard Worker case "x86": 68*795d594fSAndroid Build Coastguard Worker arch = &c.Codegen.X86 69*795d594fSAndroid Build Coastguard Worker case "x86_64": 70*795d594fSAndroid Build Coastguard Worker arch = &c.Codegen.X86_64 71*795d594fSAndroid Build Coastguard Worker default: 72*795d594fSAndroid Build Coastguard Worker ctx.ModuleErrorf("Unknown codegen architecture %q", archName) 73*795d594fSAndroid Build Coastguard Worker } 74*795d594fSAndroid Build Coastguard Worker return arch 75*795d594fSAndroid Build Coastguard Worker } 76*795d594fSAndroid Build Coastguard Worker 77*795d594fSAndroid Build Coastguard Worker appendCodegenSourceArchProperties := func(p *CodegenSourceArchProperties, archName string) { 78*795d594fSAndroid Build Coastguard Worker arch := getCodegenArchProperties(archName) 79*795d594fSAndroid Build Coastguard Worker p.Srcs = append(p.Srcs, arch.CodegenSourceArchProperties.Srcs...) 80*795d594fSAndroid Build Coastguard Worker } 81*795d594fSAndroid Build Coastguard Worker 82*795d594fSAndroid Build Coastguard Worker addCodegenSourceArchProperties := func(host bool, p *CodegenSourceArchProperties) { 83*795d594fSAndroid Build Coastguard Worker type sourceProps struct { 84*795d594fSAndroid Build Coastguard Worker Target struct { 85*795d594fSAndroid Build Coastguard Worker Android *CodegenSourceArchProperties 86*795d594fSAndroid Build Coastguard Worker Host *CodegenSourceArchProperties 87*795d594fSAndroid Build Coastguard Worker } 88*795d594fSAndroid Build Coastguard Worker } 89*795d594fSAndroid Build Coastguard Worker 90*795d594fSAndroid Build Coastguard Worker sp := &sourceProps{} 91*795d594fSAndroid Build Coastguard Worker if host { 92*795d594fSAndroid Build Coastguard Worker sp.Target.Host = p 93*795d594fSAndroid Build Coastguard Worker } else { 94*795d594fSAndroid Build Coastguard Worker sp.Target.Android = p 95*795d594fSAndroid Build Coastguard Worker } 96*795d594fSAndroid Build Coastguard Worker ctx.AppendProperties(sp) 97*795d594fSAndroid Build Coastguard Worker } 98*795d594fSAndroid Build Coastguard Worker 99*795d594fSAndroid Build Coastguard Worker addCodegenArchProperties := func(host bool, archName string) { 100*795d594fSAndroid Build Coastguard Worker type commonProps struct { 101*795d594fSAndroid Build Coastguard Worker Target struct { 102*795d594fSAndroid Build Coastguard Worker Android *CodegenCommonArchProperties 103*795d594fSAndroid Build Coastguard Worker Host *CodegenCommonArchProperties 104*795d594fSAndroid Build Coastguard Worker } 105*795d594fSAndroid Build Coastguard Worker } 106*795d594fSAndroid Build Coastguard Worker 107*795d594fSAndroid Build Coastguard Worker type libraryProps struct { 108*795d594fSAndroid Build Coastguard Worker Target struct { 109*795d594fSAndroid Build Coastguard Worker Android *CodegenLibraryArchProperties 110*795d594fSAndroid Build Coastguard Worker Host *CodegenLibraryArchProperties 111*795d594fSAndroid Build Coastguard Worker } 112*795d594fSAndroid Build Coastguard Worker } 113*795d594fSAndroid Build Coastguard Worker 114*795d594fSAndroid Build Coastguard Worker type sharedLibraryProps struct { 115*795d594fSAndroid Build Coastguard Worker Target struct { 116*795d594fSAndroid Build Coastguard Worker Android *CodegenLibraryArchSharedProperties 117*795d594fSAndroid Build Coastguard Worker Host *CodegenLibraryArchSharedProperties 118*795d594fSAndroid Build Coastguard Worker } 119*795d594fSAndroid Build Coastguard Worker } 120*795d594fSAndroid Build Coastguard Worker 121*795d594fSAndroid Build Coastguard Worker type staticLibraryProps struct { 122*795d594fSAndroid Build Coastguard Worker Target struct { 123*795d594fSAndroid Build Coastguard Worker Android *CodegenLibraryArchStaticProperties 124*795d594fSAndroid Build Coastguard Worker Host *CodegenLibraryArchStaticProperties 125*795d594fSAndroid Build Coastguard Worker } 126*795d594fSAndroid Build Coastguard Worker } 127*795d594fSAndroid Build Coastguard Worker 128*795d594fSAndroid Build Coastguard Worker arch := getCodegenArchProperties(archName) 129*795d594fSAndroid Build Coastguard Worker 130*795d594fSAndroid Build Coastguard Worker cp := &commonProps{} 131*795d594fSAndroid Build Coastguard Worker lp := &libraryProps{} 132*795d594fSAndroid Build Coastguard Worker sharedLP := &sharedLibraryProps{} 133*795d594fSAndroid Build Coastguard Worker staticLP := &staticLibraryProps{} 134*795d594fSAndroid Build Coastguard Worker if host { 135*795d594fSAndroid Build Coastguard Worker cp.Target.Host = &arch.CodegenCommonArchProperties 136*795d594fSAndroid Build Coastguard Worker lp.Target.Host = &arch.CodegenLibraryArchProperties 137*795d594fSAndroid Build Coastguard Worker sharedLP.Target.Host = &arch.CodegenLibraryArchSharedProperties 138*795d594fSAndroid Build Coastguard Worker staticLP.Target.Host = &arch.CodegenLibraryArchStaticProperties 139*795d594fSAndroid Build Coastguard Worker } else { 140*795d594fSAndroid Build Coastguard Worker cp.Target.Android = &arch.CodegenCommonArchProperties 141*795d594fSAndroid Build Coastguard Worker lp.Target.Android = &arch.CodegenLibraryArchProperties 142*795d594fSAndroid Build Coastguard Worker sharedLP.Target.Android = &arch.CodegenLibraryArchSharedProperties 143*795d594fSAndroid Build Coastguard Worker staticLP.Target.Android = &arch.CodegenLibraryArchStaticProperties 144*795d594fSAndroid Build Coastguard Worker } 145*795d594fSAndroid Build Coastguard Worker 146*795d594fSAndroid Build Coastguard Worker ctx.AppendProperties(cp) 147*795d594fSAndroid Build Coastguard Worker if t.library { 148*795d594fSAndroid Build Coastguard Worker ctx.AppendProperties(lp) 149*795d594fSAndroid Build Coastguard Worker if t.static { 150*795d594fSAndroid Build Coastguard Worker ctx.AppendProperties(staticLP) 151*795d594fSAndroid Build Coastguard Worker } 152*795d594fSAndroid Build Coastguard Worker if t.shared { 153*795d594fSAndroid Build Coastguard Worker ctx.AppendProperties(sharedLP) 154*795d594fSAndroid Build Coastguard Worker } 155*795d594fSAndroid Build Coastguard Worker } 156*795d594fSAndroid Build Coastguard Worker } 157*795d594fSAndroid Build Coastguard Worker 158*795d594fSAndroid Build Coastguard Worker addCodegenProperties := func(host bool, arches []string) { 159*795d594fSAndroid Build Coastguard Worker sourceProps := &CodegenSourceArchProperties{} 160*795d594fSAndroid Build Coastguard Worker for _, arch := range arches { 161*795d594fSAndroid Build Coastguard Worker appendCodegenSourceArchProperties(sourceProps, arch) 162*795d594fSAndroid Build Coastguard Worker addCodegenArchProperties(host, arch) 163*795d594fSAndroid Build Coastguard Worker } 164*795d594fSAndroid Build Coastguard Worker sourceProps.Srcs = android.FirstUniqueStrings(sourceProps.Srcs) 165*795d594fSAndroid Build Coastguard Worker addCodegenSourceArchProperties(host, sourceProps) 166*795d594fSAndroid Build Coastguard Worker } 167*795d594fSAndroid Build Coastguard Worker 168*795d594fSAndroid Build Coastguard Worker addCodegenProperties(false /* host */, deviceArches) 169*795d594fSAndroid Build Coastguard Worker addCodegenProperties(true /* host */, hostArches) 170*795d594fSAndroid Build Coastguard Worker} 171*795d594fSAndroid Build Coastguard Worker 172*795d594fSAndroid Build Coastguard Worker// These properties are allowed to contain the same source file name in different architectures. 173*795d594fSAndroid Build Coastguard Worker// They we will be deduplicated automatically. 174*795d594fSAndroid Build Coastguard Workertype CodegenSourceArchProperties struct { 175*795d594fSAndroid Build Coastguard Worker Srcs []string 176*795d594fSAndroid Build Coastguard Worker} 177*795d594fSAndroid Build Coastguard Worker 178*795d594fSAndroid Build Coastguard Workertype CodegenCommonArchProperties struct { 179*795d594fSAndroid Build Coastguard Worker Cflags []string 180*795d594fSAndroid Build Coastguard Worker Cppflags []string 181*795d594fSAndroid Build Coastguard Worker} 182*795d594fSAndroid Build Coastguard Worker 183*795d594fSAndroid Build Coastguard Workertype CodegenLibraryArchProperties struct { 184*795d594fSAndroid Build Coastguard Worker Static_libs []string 185*795d594fSAndroid Build Coastguard Worker Export_static_lib_headers []string 186*795d594fSAndroid Build Coastguard Worker} 187*795d594fSAndroid Build Coastguard Worker 188*795d594fSAndroid Build Coastguard Workertype CodegenLibraryArchStaticProperties struct { 189*795d594fSAndroid Build Coastguard Worker Static struct { 190*795d594fSAndroid Build Coastguard Worker Whole_static_libs []string 191*795d594fSAndroid Build Coastguard Worker } 192*795d594fSAndroid Build Coastguard Worker} 193*795d594fSAndroid Build Coastguard Workertype CodegenLibraryArchSharedProperties struct { 194*795d594fSAndroid Build Coastguard Worker Shared struct { 195*795d594fSAndroid Build Coastguard Worker Shared_libs []string 196*795d594fSAndroid Build Coastguard Worker Export_shared_lib_headers []string 197*795d594fSAndroid Build Coastguard Worker } 198*795d594fSAndroid Build Coastguard Worker} 199*795d594fSAndroid Build Coastguard Worker 200*795d594fSAndroid Build Coastguard Workertype codegenArchProperties struct { 201*795d594fSAndroid Build Coastguard Worker CodegenSourceArchProperties 202*795d594fSAndroid Build Coastguard Worker CodegenCommonArchProperties 203*795d594fSAndroid Build Coastguard Worker CodegenLibraryArchProperties 204*795d594fSAndroid Build Coastguard Worker CodegenLibraryArchStaticProperties 205*795d594fSAndroid Build Coastguard Worker CodegenLibraryArchSharedProperties 206*795d594fSAndroid Build Coastguard Worker} 207*795d594fSAndroid Build Coastguard Worker 208*795d594fSAndroid Build Coastguard Workertype codegenProperties struct { 209*795d594fSAndroid Build Coastguard Worker Codegen struct { 210*795d594fSAndroid Build Coastguard Worker Arm, Arm64, Riscv64, X86, X86_64 codegenArchProperties 211*795d594fSAndroid Build Coastguard Worker } 212*795d594fSAndroid Build Coastguard Worker} 213*795d594fSAndroid Build Coastguard Worker 214*795d594fSAndroid Build Coastguard Workerfunc defaultDeviceCodegenArches(ctx android.LoadHookContext) []string { 215*795d594fSAndroid Build Coastguard Worker arches := make(map[string]bool) 216*795d594fSAndroid Build Coastguard Worker for _, a := range ctx.DeviceConfig().Arches() { 217*795d594fSAndroid Build Coastguard Worker s := a.ArchType.String() 218*795d594fSAndroid Build Coastguard Worker arches[s] = true 219*795d594fSAndroid Build Coastguard Worker if s == "arm64" { 220*795d594fSAndroid Build Coastguard Worker arches["arm"] = true 221*795d594fSAndroid Build Coastguard Worker } else if s == "riscv64" { 222*795d594fSAndroid Build Coastguard Worker arches["riscv64"] = true 223*795d594fSAndroid Build Coastguard Worker } else if s == "x86_64" { 224*795d594fSAndroid Build Coastguard Worker arches["x86"] = true 225*795d594fSAndroid Build Coastguard Worker } 226*795d594fSAndroid Build Coastguard Worker } 227*795d594fSAndroid Build Coastguard Worker ret := make([]string, 0, len(arches)) 228*795d594fSAndroid Build Coastguard Worker for a := range arches { 229*795d594fSAndroid Build Coastguard Worker ret = append(ret, a) 230*795d594fSAndroid Build Coastguard Worker } 231*795d594fSAndroid Build Coastguard Worker sort.Strings(ret) 232*795d594fSAndroid Build Coastguard Worker return ret 233*795d594fSAndroid Build Coastguard Worker} 234*795d594fSAndroid Build Coastguard Worker 235*795d594fSAndroid Build Coastguard Workerfunc installCodegenCustomizer(module android.Module, t moduleType) { 236*795d594fSAndroid Build Coastguard Worker c := &codegenProperties{} 237*795d594fSAndroid Build Coastguard Worker android.AddLoadHook(module, func(ctx android.LoadHookContext) { codegen(ctx, c, t) }) 238*795d594fSAndroid Build Coastguard Worker module.AddProperties(c) 239*795d594fSAndroid Build Coastguard Worker} 240