blob: bc7dc4253d3dca1dde3ab3d34d0c5d576b9b50cd [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 Cross1f7f3bd2016-07-27 10:12:38 -070065 case "x86":
Colin Cross6e95dd52016-09-12 15:37:10 -070066 arch = &c.Codegen.X86
Colin Cross1f7f3bd2016-07-27 10:12:38 -070067 case "x86_64":
Colin Cross6e95dd52016-09-12 15:37:10 -070068 arch = &c.Codegen.X86_64
Colin Cross1f7f3bd2016-07-27 10:12:38 -070069 default:
Colin Cross6e95dd52016-09-12 15:37:10 -070070 ctx.ModuleErrorf("Unknown codegen architecture %q", archName)
Roland Levillain118ce362019-08-05 18:06:00 +010071 }
72 return arch
73 }
74
75 appendCodegenSourceArchProperties := func(p *CodegenSourceArchProperties, archName string) {
76 arch := getCodegenArchProperties(archName)
77 p.Srcs = append(p.Srcs, arch.CodegenSourceArchProperties.Srcs...)
78 }
79
80 addCodegenSourceArchProperties := func(host bool, p *CodegenSourceArchProperties) {
81 type sourceProps struct {
82 Target struct {
83 Android *CodegenSourceArchProperties
84 Host *CodegenSourceArchProperties
85 }
Colin Cross1f7f3bd2016-07-27 10:12:38 -070086 }
Colin Cross6e95dd52016-09-12 15:37:10 -070087
Roland Levillain118ce362019-08-05 18:06:00 +010088 sp := &sourceProps{}
Colin Cross6e95dd52016-09-12 15:37:10 -070089 if host {
Roland Levillain118ce362019-08-05 18:06:00 +010090 sp.Target.Host = p
Colin Cross6e95dd52016-09-12 15:37:10 -070091 } else {
Roland Levillain118ce362019-08-05 18:06:00 +010092 sp.Target.Android = p
93 }
94 ctx.AppendProperties(sp)
95 }
96
97 addCodegenArchProperties := func(host bool, archName string) {
98 type commonProps struct {
99 Target struct {
100 Android *CodegenCommonArchProperties
101 Host *CodegenCommonArchProperties
102 }
Colin Cross6e95dd52016-09-12 15:37:10 -0700103 }
104
Colin Crossf383ed82019-09-24 15:02:23 -0700105 type sharedLibraryProps struct {
Roland Levillain118ce362019-08-05 18:06:00 +0100106 Target struct {
Colin Crossf383ed82019-09-24 15:02:23 -0700107 Android *CodegenLibraryArchSharedProperties
108 Host *CodegenLibraryArchSharedProperties
109 }
110 }
111
112 type staticLibraryProps struct {
113 Target struct {
114 Android *CodegenLibraryArchStaticProperties
115 Host *CodegenLibraryArchStaticProperties
Roland Levillain118ce362019-08-05 18:06:00 +0100116 }
117 }
118
119 arch := getCodegenArchProperties(archName)
120
121 cp := &commonProps{}
Colin Crossf383ed82019-09-24 15:02:23 -0700122 sharedLP := &sharedLibraryProps{}
123 staticLP := &staticLibraryProps{}
Roland Levillain118ce362019-08-05 18:06:00 +0100124 if host {
125 cp.Target.Host = &arch.CodegenCommonArchProperties
Colin Crossf383ed82019-09-24 15:02:23 -0700126 sharedLP.Target.Host = &arch.CodegenLibraryArchSharedProperties
127 staticLP.Target.Host = &arch.CodegenLibraryArchStaticProperties
Roland Levillain118ce362019-08-05 18:06:00 +0100128 } else {
129 cp.Target.Android = &arch.CodegenCommonArchProperties
Colin Crossf383ed82019-09-24 15:02:23 -0700130 sharedLP.Target.Android = &arch.CodegenLibraryArchSharedProperties
131 staticLP.Target.Android = &arch.CodegenLibraryArchStaticProperties
Roland Levillain118ce362019-08-05 18:06:00 +0100132 }
133
134 ctx.AppendProperties(cp)
Colin Crossf383ed82019-09-24 15:02:23 -0700135 if t.library {
136 if t.static {
137 ctx.AppendProperties(staticLP)
138 }
139 if t.shared {
140 ctx.AppendProperties(sharedLP)
141 }
Colin Cross6e95dd52016-09-12 15:37:10 -0700142 }
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700143 }
144
Roland Levillain118ce362019-08-05 18:06:00 +0100145 addCodegenProperties := func(host bool, arches []string) {
146 sourceProps := &CodegenSourceArchProperties{}
147 for _, arch := range arches {
148 appendCodegenSourceArchProperties(sourceProps, arch)
149 addCodegenArchProperties(host, arch)
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700150 }
Roland Levillain118ce362019-08-05 18:06:00 +0100151 sourceProps.Srcs = android.FirstUniqueStrings(sourceProps.Srcs)
152 addCodegenSourceArchProperties(host, sourceProps)
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700153 }
154
Roland Levillain118ce362019-08-05 18:06:00 +0100155 addCodegenProperties(false /* host */, deviceArches)
156 addCodegenProperties(true /* host */, hostArches)
157}
158
159// These properties are allowed to contain the same source file name in different architectures.
160// They we will be deduplicated automatically.
161type CodegenSourceArchProperties struct {
162 Srcs []string
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700163}
164
Colin Cross6e95dd52016-09-12 15:37:10 -0700165type CodegenCommonArchProperties struct {
Roland Levillain12dd9ae2018-11-06 13:32:06 +0000166 Cflags []string
167 Cppflags []string
Colin Cross6e95dd52016-09-12 15:37:10 -0700168}
169
Colin Crossf383ed82019-09-24 15:02:23 -0700170type CodegenLibraryArchStaticProperties struct {
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700171 Static struct {
Paul Duffin74f89af2019-07-12 15:27:50 +0100172 Whole_static_libs []string
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700173 }
Colin Crossf383ed82019-09-24 15:02:23 -0700174}
175type CodegenLibraryArchSharedProperties struct {
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700176 Shared struct {
Paul Duffin15332432019-07-12 15:27:50 +0100177 Shared_libs []string
178 Export_shared_lib_headers []string
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700179 }
180}
181
Colin Cross6e95dd52016-09-12 15:37:10 -0700182type codegenArchProperties struct {
Roland Levillain118ce362019-08-05 18:06:00 +0100183 CodegenSourceArchProperties
Colin Cross6e95dd52016-09-12 15:37:10 -0700184 CodegenCommonArchProperties
Colin Crossf383ed82019-09-24 15:02:23 -0700185 CodegenLibraryArchStaticProperties
186 CodegenLibraryArchSharedProperties
Colin Cross6e95dd52016-09-12 15:37:10 -0700187}
188
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700189type codegenProperties struct {
190 Codegen struct {
Vladimir Markobe0d3cf2020-02-12 10:52:22 +0000191 Arm, Arm64, X86, X86_64 codegenArchProperties
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700192 }
193}
194
Colin Cross6e511782016-09-13 13:41:03 -0700195func defaultDeviceCodegenArches(ctx android.LoadHookContext) []string {
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700196 arches := make(map[string]bool)
197 for _, a := range ctx.DeviceConfig().Arches() {
Nicolas Geoffraye52ac152016-09-09 15:48:19 +0100198 s := a.ArchType.String()
199 arches[s] = true
200 if s == "arm64" {
201 arches["arm"] = true
Nicolas Geoffraye52ac152016-09-09 15:48:19 +0100202 } else if s == "x86_64" {
203 arches["x86"] = true
204 }
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700205 }
206 ret := make([]string, 0, len(arches))
207 for a := range arches {
208 ret = append(ret, a)
209 }
210 sort.Strings(ret)
211 return ret
212}
Colin Cross6e95dd52016-09-12 15:37:10 -0700213
Colin Crossf383ed82019-09-24 15:02:23 -0700214func installCodegenCustomizer(module android.Module, t moduleType) {
Colin Cross6e95dd52016-09-12 15:37:10 -0700215 c := &codegenProperties{}
Colin Crossf383ed82019-09-24 15:02:23 -0700216 android.AddLoadHook(module, func(ctx android.LoadHookContext) { codegen(ctx, c, t) })
Colin Crossca06ea32017-06-27 10:38:55 -0700217 module.AddProperties(c)
Colin Cross6e95dd52016-09-12 15:37:10 -0700218}