blob: d0db78e57163c5b345c980ff593850cd7e6a25ba [file] [log] [blame]
Colin Cross1f7f3bd2016-07-27 10:12:38 -07001// Copyright (C) 2016 The Android Open Source Project
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 art
16
17// This file implements the "codegen" property to apply different properties based on the currently
18// selected codegen arches, which defaults to all arches on the host and the primary and secondary
19// arches on the device.
20
21import (
22 "android/soong/android"
23 "sort"
24 "strings"
25)
26
Colin Cross6e95dd52016-09-12 15:37:10 -070027func codegen(ctx android.LoadHookContext, c *codegenProperties, library bool) {
Colin Cross1f7f3bd2016-07-27 10:12:38 -070028 var hostArches, deviceArches []string
29
30 e := envDefault(ctx, "ART_HOST_CODEGEN_ARCHS", "")
31 if e == "" {
32 hostArches = supportedArches
33 } else {
34 hostArches = strings.Split(e, " ")
35 }
36
37 e = envDefault(ctx, "ART_TARGET_CODEGEN_ARCHS", "")
38 if e == "" {
39 deviceArches = defaultDeviceCodegenArches(ctx)
40 } else {
41 deviceArches = strings.Split(e, " ")
42 }
43
Colin Cross6e95dd52016-09-12 15:37:10 -070044 addCodegenArchProperties := func(host bool, archName string) {
45 type props struct {
46 Target struct {
47 Android *CodegenCommonArchProperties
48 Host *CodegenCommonArchProperties
49 }
Colin Cross1f7f3bd2016-07-27 10:12:38 -070050 }
Colin Cross1f7f3bd2016-07-27 10:12:38 -070051
Colin Cross6e95dd52016-09-12 15:37:10 -070052 type libraryProps struct {
53 Target struct {
54 Android *CodegenLibraryArchProperties
55 Host *CodegenLibraryArchProperties
56 }
57 }
58
59 var arch *codegenArchProperties
60 switch archName {
Colin Cross1f7f3bd2016-07-27 10:12:38 -070061 case "arm":
Colin Cross6e95dd52016-09-12 15:37:10 -070062 arch = &c.Codegen.Arm
Colin Cross1f7f3bd2016-07-27 10:12:38 -070063 case "arm64":
Colin Cross6e95dd52016-09-12 15:37:10 -070064 arch = &c.Codegen.Arm64
Colin Cross1f7f3bd2016-07-27 10:12:38 -070065 case "mips":
Colin Cross6e95dd52016-09-12 15:37:10 -070066 arch = &c.Codegen.Mips
Colin Cross1f7f3bd2016-07-27 10:12:38 -070067 case "mips64":
Colin Cross6e95dd52016-09-12 15:37:10 -070068 arch = &c.Codegen.Mips64
Colin Cross1f7f3bd2016-07-27 10:12:38 -070069 case "x86":
Colin Cross6e95dd52016-09-12 15:37:10 -070070 arch = &c.Codegen.X86
Colin Cross1f7f3bd2016-07-27 10:12:38 -070071 case "x86_64":
Colin Cross6e95dd52016-09-12 15:37:10 -070072 arch = &c.Codegen.X86_64
Colin Cross1f7f3bd2016-07-27 10:12:38 -070073 default:
Colin Cross6e95dd52016-09-12 15:37:10 -070074 ctx.ModuleErrorf("Unknown codegen architecture %q", archName)
Colin Cross1f7f3bd2016-07-27 10:12:38 -070075 return
76 }
Colin Cross6e95dd52016-09-12 15:37:10 -070077
78 p := &props{}
79 l := &libraryProps{}
80 if host {
81 p.Target.Host = &arch.CodegenCommonArchProperties
82 l.Target.Host = &arch.CodegenLibraryArchProperties
83 } else {
84 p.Target.Android = &arch.CodegenCommonArchProperties
85 l.Target.Android = &arch.CodegenLibraryArchProperties
86 }
87
Colin Cross1f7f3bd2016-07-27 10:12:38 -070088 ctx.AppendProperties(p)
Colin Cross6e95dd52016-09-12 15:37:10 -070089 if library {
90 ctx.AppendProperties(l)
91 }
Colin Cross1f7f3bd2016-07-27 10:12:38 -070092 }
93
Colin Cross6e95dd52016-09-12 15:37:10 -070094 for _, arch := range deviceArches {
95 addCodegenArchProperties(false, arch)
Colin Cross1f7f3bd2016-07-27 10:12:38 -070096 if ctx.Failed() {
97 return
98 }
99 }
100
Colin Cross6e95dd52016-09-12 15:37:10 -0700101 for _, arch := range hostArches {
102 addCodegenArchProperties(true, arch)
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700103 if ctx.Failed() {
104 return
105 }
106 }
107}
108
Colin Cross6e95dd52016-09-12 15:37:10 -0700109type CodegenCommonArchProperties struct {
Roland Levillain12dd9ae2018-11-06 13:32:06 +0000110 Srcs []string
111 Cflags []string
112 Cppflags []string
Colin Cross6e95dd52016-09-12 15:37:10 -0700113}
114
115type CodegenLibraryArchProperties struct {
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700116 Static struct {
117 Whole_static_libs []string
118 }
119 Shared struct {
120 Shared_libs []string
121 }
122}
123
Colin Cross6e95dd52016-09-12 15:37:10 -0700124type codegenArchProperties struct {
125 CodegenCommonArchProperties
126 CodegenLibraryArchProperties
127}
128
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700129type codegenProperties struct {
130 Codegen struct {
131 Arm, Arm64, Mips, Mips64, X86, X86_64 codegenArchProperties
132 }
133}
134
135type codegenCustomizer struct {
Colin Cross6e95dd52016-09-12 15:37:10 -0700136 library bool
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700137 codegenProperties codegenProperties
138}
139
Colin Cross6e511782016-09-13 13:41:03 -0700140func defaultDeviceCodegenArches(ctx android.LoadHookContext) []string {
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700141 arches := make(map[string]bool)
142 for _, a := range ctx.DeviceConfig().Arches() {
Nicolas Geoffraye52ac152016-09-09 15:48:19 +0100143 s := a.ArchType.String()
144 arches[s] = true
145 if s == "arm64" {
146 arches["arm"] = true
147 } else if s == "mips64" {
148 arches["mips"] = true
149 } else if s == "x86_64" {
150 arches["x86"] = true
151 }
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700152 }
153 ret := make([]string, 0, len(arches))
154 for a := range arches {
155 ret = append(ret, a)
156 }
157 sort.Strings(ret)
158 return ret
159}
Colin Cross6e95dd52016-09-12 15:37:10 -0700160
Colin Crossca06ea32017-06-27 10:38:55 -0700161func installCodegenCustomizer(module android.Module, library bool) {
Colin Cross6e95dd52016-09-12 15:37:10 -0700162 c := &codegenProperties{}
163 android.AddLoadHook(module, func(ctx android.LoadHookContext) { codegen(ctx, c, library) })
Colin Crossca06ea32017-06-27 10:38:55 -0700164 module.AddProperties(c)
Colin Cross6e95dd52016-09-12 15:37:10 -0700165}