blob: 8e5ef1a609d3e1527c6765bf59766cebd13a175a [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 Crossf383ed82019-09-24 15:02:23 -070027type moduleType struct {
28 library bool
29 static bool
30 shared bool
31}
32
33var (
34 staticLibrary = moduleType{true, true, false}
35 sharedLibrary = moduleType{true, false, true}
36 staticAndSharedLibrary = moduleType{true, true, true}
37 binary = moduleType{false, false, false}
38)
39
40func codegen(ctx android.LoadHookContext, c *codegenProperties, t moduleType) {
Colin Cross1f7f3bd2016-07-27 10:12:38 -070041 var hostArches, deviceArches []string
42
43 e := envDefault(ctx, "ART_HOST_CODEGEN_ARCHS", "")
44 if e == "" {
45 hostArches = supportedArches
46 } else {
47 hostArches = strings.Split(e, " ")
48 }
49
50 e = envDefault(ctx, "ART_TARGET_CODEGEN_ARCHS", "")
51 if e == "" {
52 deviceArches = defaultDeviceCodegenArches(ctx)
53 } else {
54 deviceArches = strings.Split(e, " ")
55 }
56
Roland Levillain118ce362019-08-05 18:06:00 +010057 getCodegenArchProperties := func(archName string) *codegenArchProperties {
Colin Cross6e95dd52016-09-12 15:37:10 -070058 var arch *codegenArchProperties
59 switch archName {
Colin Cross1f7f3bd2016-07-27 10:12:38 -070060 case "arm":
Colin Cross6e95dd52016-09-12 15:37:10 -070061 arch = &c.Codegen.Arm
Colin Cross1f7f3bd2016-07-27 10:12:38 -070062 case "arm64":
Colin Cross6e95dd52016-09-12 15:37:10 -070063 arch = &c.Codegen.Arm64
Colin Cross1f7f3bd2016-07-27 10:12:38 -070064 case "mips":
Colin Cross6e95dd52016-09-12 15:37:10 -070065 arch = &c.Codegen.Mips
Colin Cross1f7f3bd2016-07-27 10:12:38 -070066 case "mips64":
Colin Cross6e95dd52016-09-12 15:37:10 -070067 arch = &c.Codegen.Mips64
Colin Cross1f7f3bd2016-07-27 10:12:38 -070068 case "x86":
Colin Cross6e95dd52016-09-12 15:37:10 -070069 arch = &c.Codegen.X86
Colin Cross1f7f3bd2016-07-27 10:12:38 -070070 case "x86_64":
Colin Cross6e95dd52016-09-12 15:37:10 -070071 arch = &c.Codegen.X86_64
Colin Cross1f7f3bd2016-07-27 10:12:38 -070072 default:
Colin Cross6e95dd52016-09-12 15:37:10 -070073 ctx.ModuleErrorf("Unknown codegen architecture %q", archName)
Roland Levillain118ce362019-08-05 18:06:00 +010074 }
75 return arch
76 }
77
78 appendCodegenSourceArchProperties := func(p *CodegenSourceArchProperties, archName string) {
79 arch := getCodegenArchProperties(archName)
80 p.Srcs = append(p.Srcs, arch.CodegenSourceArchProperties.Srcs...)
81 }
82
83 addCodegenSourceArchProperties := func(host bool, p *CodegenSourceArchProperties) {
84 type sourceProps struct {
85 Target struct {
86 Android *CodegenSourceArchProperties
87 Host *CodegenSourceArchProperties
88 }
Colin Cross1f7f3bd2016-07-27 10:12:38 -070089 }
Colin Cross6e95dd52016-09-12 15:37:10 -070090
Roland Levillain118ce362019-08-05 18:06:00 +010091 sp := &sourceProps{}
Colin Cross6e95dd52016-09-12 15:37:10 -070092 if host {
Roland Levillain118ce362019-08-05 18:06:00 +010093 sp.Target.Host = p
Colin Cross6e95dd52016-09-12 15:37:10 -070094 } else {
Roland Levillain118ce362019-08-05 18:06:00 +010095 sp.Target.Android = p
96 }
97 ctx.AppendProperties(sp)
98 }
99
100 addCodegenArchProperties := func(host bool, archName string) {
101 type commonProps struct {
102 Target struct {
103 Android *CodegenCommonArchProperties
104 Host *CodegenCommonArchProperties
105 }
Colin Cross6e95dd52016-09-12 15:37:10 -0700106 }
107
Colin Crossf383ed82019-09-24 15:02:23 -0700108 type sharedLibraryProps struct {
Roland Levillain118ce362019-08-05 18:06:00 +0100109 Target struct {
Colin Crossf383ed82019-09-24 15:02:23 -0700110 Android *CodegenLibraryArchSharedProperties
111 Host *CodegenLibraryArchSharedProperties
112 }
113 }
114
115 type staticLibraryProps struct {
116 Target struct {
117 Android *CodegenLibraryArchStaticProperties
118 Host *CodegenLibraryArchStaticProperties
Roland Levillain118ce362019-08-05 18:06:00 +0100119 }
120 }
121
122 arch := getCodegenArchProperties(archName)
123
124 cp := &commonProps{}
Colin Crossf383ed82019-09-24 15:02:23 -0700125 sharedLP := &sharedLibraryProps{}
126 staticLP := &staticLibraryProps{}
Roland Levillain118ce362019-08-05 18:06:00 +0100127 if host {
128 cp.Target.Host = &arch.CodegenCommonArchProperties
Colin Crossf383ed82019-09-24 15:02:23 -0700129 sharedLP.Target.Host = &arch.CodegenLibraryArchSharedProperties
130 staticLP.Target.Host = &arch.CodegenLibraryArchStaticProperties
Roland Levillain118ce362019-08-05 18:06:00 +0100131 } else {
132 cp.Target.Android = &arch.CodegenCommonArchProperties
Colin Crossf383ed82019-09-24 15:02:23 -0700133 sharedLP.Target.Android = &arch.CodegenLibraryArchSharedProperties
134 staticLP.Target.Android = &arch.CodegenLibraryArchStaticProperties
Roland Levillain118ce362019-08-05 18:06:00 +0100135 }
136
137 ctx.AppendProperties(cp)
Colin Crossf383ed82019-09-24 15:02:23 -0700138 if t.library {
139 if t.static {
140 ctx.AppendProperties(staticLP)
141 }
142 if t.shared {
143 ctx.AppendProperties(sharedLP)
144 }
Colin Cross6e95dd52016-09-12 15:37:10 -0700145 }
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700146 }
147
Roland Levillain118ce362019-08-05 18:06:00 +0100148 addCodegenProperties := func(host bool, arches []string) {
149 sourceProps := &CodegenSourceArchProperties{}
150 for _, arch := range arches {
151 appendCodegenSourceArchProperties(sourceProps, arch)
152 addCodegenArchProperties(host, arch)
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700153 }
Roland Levillain118ce362019-08-05 18:06:00 +0100154 sourceProps.Srcs = android.FirstUniqueStrings(sourceProps.Srcs)
155 addCodegenSourceArchProperties(host, sourceProps)
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700156 }
157
Roland Levillain118ce362019-08-05 18:06:00 +0100158 addCodegenProperties(false /* host */, deviceArches)
159 addCodegenProperties(true /* host */, hostArches)
160}
161
162// These properties are allowed to contain the same source file name in different architectures.
163// They we will be deduplicated automatically.
164type CodegenSourceArchProperties struct {
165 Srcs []string
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700166}
167
Colin Cross6e95dd52016-09-12 15:37:10 -0700168type CodegenCommonArchProperties struct {
Roland Levillain12dd9ae2018-11-06 13:32:06 +0000169 Cflags []string
170 Cppflags []string
Colin Cross6e95dd52016-09-12 15:37:10 -0700171}
172
Colin Crossf383ed82019-09-24 15:02:23 -0700173type CodegenLibraryArchStaticProperties struct {
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700174 Static struct {
Paul Duffin74f89af2019-07-12 15:27:50 +0100175 Whole_static_libs []string
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700176 }
Colin Crossf383ed82019-09-24 15:02:23 -0700177}
178type CodegenLibraryArchSharedProperties struct {
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700179 Shared struct {
Paul Duffin15332432019-07-12 15:27:50 +0100180 Shared_libs []string
181 Export_shared_lib_headers []string
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700182 }
183}
184
Colin Cross6e95dd52016-09-12 15:37:10 -0700185type codegenArchProperties struct {
Roland Levillain118ce362019-08-05 18:06:00 +0100186 CodegenSourceArchProperties
Colin Cross6e95dd52016-09-12 15:37:10 -0700187 CodegenCommonArchProperties
Colin Crossf383ed82019-09-24 15:02:23 -0700188 CodegenLibraryArchStaticProperties
189 CodegenLibraryArchSharedProperties
Colin Cross6e95dd52016-09-12 15:37:10 -0700190}
191
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700192type codegenProperties struct {
193 Codegen struct {
194 Arm, Arm64, Mips, Mips64, X86, X86_64 codegenArchProperties
195 }
196}
197
Colin Cross6e511782016-09-13 13:41:03 -0700198func defaultDeviceCodegenArches(ctx android.LoadHookContext) []string {
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700199 arches := make(map[string]bool)
200 for _, a := range ctx.DeviceConfig().Arches() {
Nicolas Geoffraye52ac152016-09-09 15:48:19 +0100201 s := a.ArchType.String()
202 arches[s] = true
203 if s == "arm64" {
204 arches["arm"] = true
205 } else if s == "mips64" {
206 arches["mips"] = true
207 } else if s == "x86_64" {
208 arches["x86"] = true
209 }
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700210 }
211 ret := make([]string, 0, len(arches))
212 for a := range arches {
213 ret = append(ret, a)
214 }
215 sort.Strings(ret)
216 return ret
217}
Colin Cross6e95dd52016-09-12 15:37:10 -0700218
Colin Crossf383ed82019-09-24 15:02:23 -0700219func installCodegenCustomizer(module android.Module, t moduleType) {
Colin Cross6e95dd52016-09-12 15:37:10 -0700220 c := &codegenProperties{}
Colin Crossf383ed82019-09-24 15:02:23 -0700221 android.AddLoadHook(module, func(ctx android.LoadHookContext) { codegen(ctx, c, t) })
Colin Crossca06ea32017-06-27 10:38:55 -0700222 module.AddProperties(c)
Colin Cross6e95dd52016-09-12 15:37:10 -0700223}