blob: 66569becf9e06f0ab298f7a7b6d834acfe6ec602 [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 (
Colin Cross1f7f3bd2016-07-27 10:12:38 -070022 "sort"
23 "strings"
Colin Crossdd3b7aa2019-07-31 13:47:39 -070024
25 "android/soong/android"
Colin Cross1f7f3bd2016-07-27 10:12:38 -070026)
27
Colin Crossf383ed82019-09-24 15:02:23 -070028type moduleType struct {
29 library bool
30 static bool
31 shared bool
32}
33
34var (
35 staticLibrary = moduleType{true, true, false}
36 sharedLibrary = moduleType{true, false, true}
37 staticAndSharedLibrary = moduleType{true, true, true}
38 binary = moduleType{false, false, false}
39)
40
41func codegen(ctx android.LoadHookContext, c *codegenProperties, t moduleType) {
Colin Cross1f7f3bd2016-07-27 10:12:38 -070042 var hostArches, deviceArches []string
43
Colin Crossdd3b7aa2019-07-31 13:47:39 -070044 e := ctx.Config().Getenv("ART_HOST_CODEGEN_ARCHS")
Colin Cross1f7f3bd2016-07-27 10:12:38 -070045 if e == "" {
46 hostArches = supportedArches
47 } else {
48 hostArches = strings.Split(e, " ")
49 }
50
Colin Crossdd3b7aa2019-07-31 13:47:39 -070051 e = ctx.Config().Getenv("ART_TARGET_CODEGEN_ARCHS")
Colin Cross1f7f3bd2016-07-27 10:12:38 -070052 if e == "" {
53 deviceArches = defaultDeviceCodegenArches(ctx)
54 } else {
55 deviceArches = strings.Split(e, " ")
56 }
57
Roland Levillain118ce362019-08-05 18:06:00 +010058 getCodegenArchProperties := func(archName string) *codegenArchProperties {
Colin Cross6e95dd52016-09-12 15:37:10 -070059 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 Cross0b325ab2022-10-03 12:50:25 -070065 case "riscv64":
66 arch = &c.Codegen.Riscv64
Colin Cross1f7f3bd2016-07-27 10:12:38 -070067 case "x86":
Colin Cross6e95dd52016-09-12 15:37:10 -070068 arch = &c.Codegen.X86
Colin Cross1f7f3bd2016-07-27 10:12:38 -070069 case "x86_64":
Colin Cross6e95dd52016-09-12 15:37:10 -070070 arch = &c.Codegen.X86_64
Colin Cross1f7f3bd2016-07-27 10:12:38 -070071 default:
Colin Cross6e95dd52016-09-12 15:37:10 -070072 ctx.ModuleErrorf("Unknown codegen architecture %q", archName)
Roland Levillain118ce362019-08-05 18:06:00 +010073 }
74 return arch
75 }
76
77 appendCodegenSourceArchProperties := func(p *CodegenSourceArchProperties, archName string) {
78 arch := getCodegenArchProperties(archName)
79 p.Srcs = append(p.Srcs, arch.CodegenSourceArchProperties.Srcs...)
80 }
81
82 addCodegenSourceArchProperties := func(host bool, p *CodegenSourceArchProperties) {
83 type sourceProps struct {
84 Target struct {
85 Android *CodegenSourceArchProperties
86 Host *CodegenSourceArchProperties
87 }
Colin Cross1f7f3bd2016-07-27 10:12:38 -070088 }
Colin Cross6e95dd52016-09-12 15:37:10 -070089
Roland Levillain118ce362019-08-05 18:06:00 +010090 sp := &sourceProps{}
Colin Cross6e95dd52016-09-12 15:37:10 -070091 if host {
Roland Levillain118ce362019-08-05 18:06:00 +010092 sp.Target.Host = p
Colin Cross6e95dd52016-09-12 15:37:10 -070093 } else {
Roland Levillain118ce362019-08-05 18:06:00 +010094 sp.Target.Android = p
95 }
96 ctx.AppendProperties(sp)
97 }
98
99 addCodegenArchProperties := func(host bool, archName string) {
100 type commonProps struct {
101 Target struct {
102 Android *CodegenCommonArchProperties
103 Host *CodegenCommonArchProperties
104 }
Colin Cross6e95dd52016-09-12 15:37:10 -0700105 }
106
Vladimir Marko053e1382021-05-05 16:07:27 +0000107 type libraryProps struct {
108 Target struct {
109 Android *CodegenLibraryArchProperties
110 Host *CodegenLibraryArchProperties
111 }
112 }
113
Colin Crossf383ed82019-09-24 15:02:23 -0700114 type sharedLibraryProps struct {
Roland Levillain118ce362019-08-05 18:06:00 +0100115 Target struct {
Colin Crossf383ed82019-09-24 15:02:23 -0700116 Android *CodegenLibraryArchSharedProperties
117 Host *CodegenLibraryArchSharedProperties
118 }
119 }
120
121 type staticLibraryProps struct {
122 Target struct {
123 Android *CodegenLibraryArchStaticProperties
124 Host *CodegenLibraryArchStaticProperties
Roland Levillain118ce362019-08-05 18:06:00 +0100125 }
126 }
127
128 arch := getCodegenArchProperties(archName)
129
130 cp := &commonProps{}
Vladimir Marko053e1382021-05-05 16:07:27 +0000131 lp := &libraryProps{}
Colin Crossf383ed82019-09-24 15:02:23 -0700132 sharedLP := &sharedLibraryProps{}
133 staticLP := &staticLibraryProps{}
Roland Levillain118ce362019-08-05 18:06:00 +0100134 if host {
135 cp.Target.Host = &arch.CodegenCommonArchProperties
Vladimir Marko053e1382021-05-05 16:07:27 +0000136 lp.Target.Host = &arch.CodegenLibraryArchProperties
Colin Crossf383ed82019-09-24 15:02:23 -0700137 sharedLP.Target.Host = &arch.CodegenLibraryArchSharedProperties
138 staticLP.Target.Host = &arch.CodegenLibraryArchStaticProperties
Roland Levillain118ce362019-08-05 18:06:00 +0100139 } else {
140 cp.Target.Android = &arch.CodegenCommonArchProperties
Vladimir Marko053e1382021-05-05 16:07:27 +0000141 lp.Target.Android = &arch.CodegenLibraryArchProperties
Colin Crossf383ed82019-09-24 15:02:23 -0700142 sharedLP.Target.Android = &arch.CodegenLibraryArchSharedProperties
143 staticLP.Target.Android = &arch.CodegenLibraryArchStaticProperties
Roland Levillain118ce362019-08-05 18:06:00 +0100144 }
145
146 ctx.AppendProperties(cp)
Colin Crossf383ed82019-09-24 15:02:23 -0700147 if t.library {
Vladimir Marko053e1382021-05-05 16:07:27 +0000148 ctx.AppendProperties(lp)
Colin Crossf383ed82019-09-24 15:02:23 -0700149 if t.static {
150 ctx.AppendProperties(staticLP)
151 }
152 if t.shared {
153 ctx.AppendProperties(sharedLP)
154 }
Colin Cross6e95dd52016-09-12 15:37:10 -0700155 }
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700156 }
157
Roland Levillain118ce362019-08-05 18:06:00 +0100158 addCodegenProperties := func(host bool, arches []string) {
159 sourceProps := &CodegenSourceArchProperties{}
160 for _, arch := range arches {
161 appendCodegenSourceArchProperties(sourceProps, arch)
162 addCodegenArchProperties(host, arch)
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700163 }
Roland Levillain118ce362019-08-05 18:06:00 +0100164 sourceProps.Srcs = android.FirstUniqueStrings(sourceProps.Srcs)
165 addCodegenSourceArchProperties(host, sourceProps)
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700166 }
167
Roland Levillain118ce362019-08-05 18:06:00 +0100168 addCodegenProperties(false /* host */, deviceArches)
169 addCodegenProperties(true /* host */, hostArches)
170}
171
172// These properties are allowed to contain the same source file name in different architectures.
173// They we will be deduplicated automatically.
174type CodegenSourceArchProperties struct {
175 Srcs []string
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700176}
177
Colin Cross6e95dd52016-09-12 15:37:10 -0700178type CodegenCommonArchProperties struct {
Roland Levillain12dd9ae2018-11-06 13:32:06 +0000179 Cflags []string
180 Cppflags []string
Colin Cross6e95dd52016-09-12 15:37:10 -0700181}
182
Vladimir Marko053e1382021-05-05 16:07:27 +0000183type CodegenLibraryArchProperties struct {
184 Static_libs []string
185 Export_static_lib_headers []string
186}
187
Colin Crossf383ed82019-09-24 15:02:23 -0700188type CodegenLibraryArchStaticProperties struct {
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700189 Static struct {
Paul Duffin74f89af2019-07-12 15:27:50 +0100190 Whole_static_libs []string
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700191 }
Colin Crossf383ed82019-09-24 15:02:23 -0700192}
193type CodegenLibraryArchSharedProperties struct {
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700194 Shared struct {
Paul Duffin15332432019-07-12 15:27:50 +0100195 Shared_libs []string
196 Export_shared_lib_headers []string
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700197 }
198}
199
Colin Cross6e95dd52016-09-12 15:37:10 -0700200type codegenArchProperties struct {
Roland Levillain118ce362019-08-05 18:06:00 +0100201 CodegenSourceArchProperties
Colin Cross6e95dd52016-09-12 15:37:10 -0700202 CodegenCommonArchProperties
Vladimir Marko053e1382021-05-05 16:07:27 +0000203 CodegenLibraryArchProperties
Colin Crossf383ed82019-09-24 15:02:23 -0700204 CodegenLibraryArchStaticProperties
205 CodegenLibraryArchSharedProperties
Colin Cross6e95dd52016-09-12 15:37:10 -0700206}
207
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700208type codegenProperties struct {
209 Codegen struct {
Colin Cross0b325ab2022-10-03 12:50:25 -0700210 Arm, Arm64, Riscv64, X86, X86_64 codegenArchProperties
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700211 }
212}
213
Colin Cross6e511782016-09-13 13:41:03 -0700214func defaultDeviceCodegenArches(ctx android.LoadHookContext) []string {
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700215 arches := make(map[string]bool)
216 for _, a := range ctx.DeviceConfig().Arches() {
Nicolas Geoffraye52ac152016-09-09 15:48:19 +0100217 s := a.ArchType.String()
218 arches[s] = true
219 if s == "arm64" {
220 arches["arm"] = true
Nicolas Geoffraye52ac152016-09-09 15:48:19 +0100221 } else if s == "x86_64" {
222 arches["x86"] = true
223 }
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700224 }
225 ret := make([]string, 0, len(arches))
226 for a := range arches {
227 ret = append(ret, a)
228 }
229 sort.Strings(ret)
230 return ret
231}
Colin Cross6e95dd52016-09-12 15:37:10 -0700232
Colin Crossf383ed82019-09-24 15:02:23 -0700233func installCodegenCustomizer(module android.Module, t moduleType) {
Colin Cross6e95dd52016-09-12 15:37:10 -0700234 c := &codegenProperties{}
Colin Crossf383ed82019-09-24 15:02:23 -0700235 android.AddLoadHook(module, func(ctx android.LoadHookContext) { codegen(ctx, c, t) })
Colin Crossca06ea32017-06-27 10:38:55 -0700236 module.AddProperties(c)
Colin Cross6e95dd52016-09-12 15:37:10 -0700237}