blob: 953e6cfc7b697f81ba13111e149f6093a04b5244 [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// Copyright 2015 Google Inc. All rights reserved.
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
Colin Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross3f40fa42015-01-30 17:27:36 -080016
17import (
Colin Cross3f40fa42015-01-30 17:27:36 -080018 "fmt"
19 "reflect"
20 "runtime"
21 "strings"
Colin Crossf6566ed2015-03-24 11:13:38 -070022
Colin Crossf6566ed2015-03-24 11:13:38 -070023 "github.com/google/blueprint/proptools"
Colin Cross3f40fa42015-01-30 17:27:36 -080024)
25
26var (
Dan Willemsenb1957a52016-06-23 23:44:54 -070027 archTypeList []ArchType
28
Colin Crossec193632015-07-06 17:49:43 -070029 Arm = newArch("arm", "lib32")
30 Arm64 = newArch("arm64", "lib64")
31 Mips = newArch("mips", "lib32")
32 Mips64 = newArch("mips64", "lib64")
33 X86 = newArch("x86", "lib32")
34 X86_64 = newArch("x86_64", "lib64")
Colin Cross2fe66872015-03-30 17:20:39 -070035
36 Common = ArchType{
37 Name: "common",
38 }
Colin Cross3f40fa42015-01-30 17:27:36 -080039)
40
Colin Cross4225f652015-09-17 14:33:42 -070041var archTypeMap = map[string]ArchType{
42 "arm": Arm,
43 "arm64": Arm64,
44 "mips": Mips,
Colin Cross3b336c22015-11-23 16:28:31 -080045 "mips64": Mips64,
Colin Cross4225f652015-09-17 14:33:42 -070046 "x86": X86,
47 "x86_64": X86_64,
48}
49
Colin Cross3f40fa42015-01-30 17:27:36 -080050/*
51Example blueprints file containing all variant property groups, with comment listing what type
52of variants get properties in that group:
53
54module {
55 arch: {
56 arm: {
57 // Host or device variants with arm architecture
58 },
59 arm64: {
60 // Host or device variants with arm64 architecture
61 },
62 mips: {
63 // Host or device variants with mips architecture
64 },
65 mips64: {
66 // Host or device variants with mips64 architecture
67 },
68 x86: {
69 // Host or device variants with x86 architecture
70 },
71 x86_64: {
72 // Host or device variants with x86_64 architecture
73 },
74 },
75 multilib: {
76 lib32: {
77 // Host or device variants for 32-bit architectures
78 },
79 lib64: {
80 // Host or device variants for 64-bit architectures
81 },
82 },
83 target: {
84 android: {
85 // Device variants
86 },
87 host: {
88 // Host variants
89 },
Dan Willemsen5746bd42017-10-02 19:42:01 -070090 linux_glibc: {
Colin Cross3f40fa42015-01-30 17:27:36 -080091 // Linux host variants
92 },
93 darwin: {
94 // Darwin host variants
95 },
96 windows: {
97 // Windows host variants
98 },
99 not_windows: {
100 // Non-windows host variants
101 },
102 },
103}
104*/
Colin Cross7d5136f2015-05-11 13:39:40 -0700105
Jaewoong Junge46114c2019-01-16 14:33:13 -0800106var archVariants = map[ArchType][]string{
107 Arm: {
Jaewoong Junge46114c2019-01-16 14:33:13 -0800108 "armv7-a-neon",
109 "armv8-a",
110 "armv8-2a",
111 "cortex-a7",
112 "cortex-a8",
113 "cortex-a9",
114 "cortex-a15",
115 "cortex-a53",
116 "cortex-a53-a57",
117 "cortex-a55",
118 "cortex-a72",
119 "cortex-a73",
120 "cortex-a75",
121 "cortex-a76",
122 "krait",
123 "kryo",
124 "kryo385",
125 "exynos-m1",
126 "exynos-m2",
127 },
128 Arm64: {
129 "armv8_a",
130 "armv8_2a",
131 "cortex-a53",
132 "cortex-a55",
133 "cortex-a72",
134 "cortex-a73",
135 "cortex-a75",
136 "cortex-a76",
137 "kryo",
138 "kryo385",
139 "exynos-m1",
140 "exynos-m2",
141 },
142 Mips: {
143 "mips32_fp",
144 "mips32r2_fp",
145 "mips32r2_fp_xburst",
146 "mips32r2dsp_fp",
147 "mips32r2dspr2_fp",
148 "mips32r6",
149 },
150 Mips64: {
151 "mips64r2",
152 "mips64r6",
153 },
154 X86: {
155 "atom",
156 "haswell",
157 "ivybridge",
158 "sandybridge",
159 "silvermont",
160 "x86_64",
161 },
162 X86_64: {
163 "haswell",
164 "ivybridge",
165 "sandybridge",
166 "silvermont",
167 },
168}
169
170var archFeatures = map[ArchType][]string{
171 Arm: {
172 "neon",
173 },
174 Mips: {
175 "dspr2",
176 "rev6",
177 "msa",
178 },
179 Mips64: {
180 "rev6",
181 "msa",
182 },
183 X86: {
184 "ssse3",
185 "sse4",
186 "sse4_1",
187 "sse4_2",
188 "aes_ni",
189 "avx",
190 "popcnt",
191 "movbe",
192 },
193 X86_64: {
194 "ssse3",
195 "sse4",
196 "sse4_1",
197 "sse4_2",
198 "aes_ni",
199 "avx",
200 "popcnt",
201 },
202}
203
204var archFeatureMap = map[ArchType]map[string][]string{
205 Arm: {
206 "armv7-a-neon": {
207 "neon",
208 },
209 "armv8-a": {
210 "neon",
211 },
212 "armv8-2a": {
213 "neon",
214 },
215 },
216 Mips: {
217 "mips32r2dspr2_fp": {
218 "dspr2",
219 },
220 "mips32r6": {
221 "rev6",
222 },
223 },
224 Mips64: {
225 "mips64r6": {
226 "rev6",
227 },
228 },
229 X86: {
230 "atom": {
231 "ssse3",
232 "movbe",
233 },
234 "haswell": {
235 "ssse3",
236 "sse4",
237 "sse4_1",
238 "sse4_2",
239 "aes_ni",
240 "avx",
241 "popcnt",
242 "movbe",
243 },
244 "ivybridge": {
245 "ssse3",
246 "sse4",
247 "sse4_1",
248 "sse4_2",
249 "aes_ni",
250 "avx",
251 "popcnt",
252 },
253 "sandybridge": {
254 "ssse3",
255 "sse4",
256 "sse4_1",
257 "sse4_2",
258 "popcnt",
259 },
260 "silvermont": {
261 "ssse3",
262 "sse4",
263 "sse4_1",
264 "sse4_2",
265 "aes_ni",
266 "popcnt",
267 "movbe",
268 },
269 "x86_64": {
270 "ssse3",
271 "sse4",
272 "sse4_1",
273 "sse4_2",
274 "popcnt",
275 },
276 },
277 X86_64: {
278 "haswell": {
279 "ssse3",
280 "sse4",
281 "sse4_1",
282 "sse4_2",
283 "aes_ni",
284 "avx",
285 "popcnt",
286 },
287 "ivybridge": {
288 "ssse3",
289 "sse4",
290 "sse4_1",
291 "sse4_2",
292 "aes_ni",
293 "avx",
294 "popcnt",
295 },
296 "sandybridge": {
297 "ssse3",
298 "sse4",
299 "sse4_1",
300 "sse4_2",
301 "popcnt",
302 },
303 "silvermont": {
304 "ssse3",
305 "sse4",
306 "sse4_1",
307 "sse4_2",
308 "aes_ni",
309 "popcnt",
310 },
311 },
312}
313
Dan Willemsen01a3c252019-01-11 19:02:16 -0800314var defaultArchFeatureMap = map[OsType]map[ArchType][]string{}
Colin Crossc5c24ad2015-11-20 15:35:00 -0800315
Dan Willemsen01a3c252019-01-11 19:02:16 -0800316func RegisterDefaultArchVariantFeatures(os OsType, arch ArchType, features ...string) {
317 checkCalledFromInit()
318
319 for _, feature := range features {
320 if !InList(feature, archFeatures[arch]) {
321 panic(fmt.Errorf("Invalid feature %q for arch %q variant \"\"", feature, arch))
322 }
323 }
324
325 if defaultArchFeatureMap[os] == nil {
326 defaultArchFeatureMap[os] = make(map[ArchType][]string)
327 }
328 defaultArchFeatureMap[os][arch] = features
329}
330
Colin Cross3f40fa42015-01-30 17:27:36 -0800331// An Arch indicates a single CPU architecture.
332type Arch struct {
Colin Crossc5c24ad2015-11-20 15:35:00 -0800333 ArchType ArchType
334 ArchVariant string
335 CpuVariant string
336 Abi []string
337 ArchFeatures []string
Dan Willemsen17f05262016-05-31 16:27:00 -0700338 Native bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800339}
340
341func (a Arch) String() string {
Colin Crossd3ba0392015-05-07 14:11:29 -0700342 s := a.ArchType.String()
Colin Cross3f40fa42015-01-30 17:27:36 -0800343 if a.ArchVariant != "" {
344 s += "_" + a.ArchVariant
345 }
346 if a.CpuVariant != "" {
347 s += "_" + a.CpuVariant
348 }
349 return s
350}
351
352type ArchType struct {
Colin Crossec193632015-07-06 17:49:43 -0700353 Name string
Dan Willemsenb1957a52016-06-23 23:44:54 -0700354 Field string
Colin Crossec193632015-07-06 17:49:43 -0700355 Multilib string
Colin Cross3f40fa42015-01-30 17:27:36 -0800356}
357
Colin Crossec193632015-07-06 17:49:43 -0700358func newArch(name, multilib string) ArchType {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700359 archType := ArchType{
Colin Crossec193632015-07-06 17:49:43 -0700360 Name: name,
Dan Willemsenb1957a52016-06-23 23:44:54 -0700361 Field: proptools.FieldNameForProperty(name),
Colin Crossec193632015-07-06 17:49:43 -0700362 Multilib: multilib,
Colin Cross3f40fa42015-01-30 17:27:36 -0800363 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700364 archTypeList = append(archTypeList, archType)
365 return archType
Colin Cross3f40fa42015-01-30 17:27:36 -0800366}
367
368func (a ArchType) String() string {
369 return a.Name
370}
371
Colin Crossa1ad8d12016-06-01 17:09:44 -0700372var BuildOs = func() OsType {
Dan Willemsen490fd492015-11-24 17:53:15 -0800373 switch runtime.GOOS {
374 case "linux":
375 return Linux
376 case "darwin":
377 return Darwin
378 default:
379 panic(fmt.Sprintf("unsupported OS: %s", runtime.GOOS))
380 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700381}()
382
383var (
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800384 osTypeList []OsType
385 commonTargetMap = make(map[string]Target)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700386
Dan Willemsen00fcbde2016-11-17 00:25:59 -0800387 NoOsType OsType
Dan Willemsen866b5632017-09-22 12:28:24 -0700388 Linux = NewOsType("linux_glibc", Host, false)
Dan Willemsen00fcbde2016-11-17 00:25:59 -0800389 Darwin = NewOsType("darwin", Host, false)
Dan Willemsen9ff34c02018-10-10 17:58:19 -0700390 LinuxBionic = NewOsType("linux_bionic", Host, false)
Dan Willemsen00fcbde2016-11-17 00:25:59 -0800391 Windows = NewOsType("windows", HostCross, true)
392 Android = NewOsType("android", Device, false)
Doug Horn21b94272019-01-16 12:06:11 -0800393 Fuchsia = NewOsType("fuchsia", Device, false)
Dan Willemsenb1957a52016-06-23 23:44:54 -0700394
395 osArchTypeMap = map[OsType][]ArchType{
Dan Willemsen00fcbde2016-11-17 00:25:59 -0800396 Linux: []ArchType{X86, X86_64},
397 LinuxBionic: []ArchType{X86_64},
Dan Willemsene97e68a2018-08-28 17:12:56 -0700398 Darwin: []ArchType{X86_64},
Dan Willemsen00fcbde2016-11-17 00:25:59 -0800399 Windows: []ArchType{X86, X86_64},
400 Android: []ArchType{Arm, Arm64, Mips, Mips64, X86, X86_64},
Doug Horn21b94272019-01-16 12:06:11 -0800401 Fuchsia: []ArchType{Arm64, X86_64},
Dan Willemsenb1957a52016-06-23 23:44:54 -0700402 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700403)
404
405type OsType struct {
406 Name, Field string
407 Class OsClass
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800408
409 DefaultDisabled bool
Dan Willemsen490fd492015-11-24 17:53:15 -0800410}
411
Colin Crossa1ad8d12016-06-01 17:09:44 -0700412type OsClass int
413
414const (
Dan Willemsen0e2d97b2016-11-28 17:50:06 -0800415 Generic OsClass = iota
416 Device
Colin Crossa1ad8d12016-06-01 17:09:44 -0700417 Host
418 HostCross
419)
420
Colin Cross67a5c132017-05-09 13:45:28 -0700421func (class OsClass) String() string {
422 switch class {
423 case Generic:
424 return "generic"
425 case Device:
426 return "device"
427 case Host:
428 return "host"
429 case HostCross:
430 return "host cross"
431 default:
432 panic(fmt.Errorf("unknown class %d", class))
433 }
434}
435
Colin Crossa1ad8d12016-06-01 17:09:44 -0700436func (os OsType) String() string {
437 return os.Name
Colin Cross54c71122016-06-01 17:09:44 -0700438}
439
Dan Willemsen866b5632017-09-22 12:28:24 -0700440func (os OsType) Bionic() bool {
441 return os == Android || os == LinuxBionic
442}
443
444func (os OsType) Linux() bool {
445 return os == Android || os == Linux || os == LinuxBionic
446}
447
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800448func NewOsType(name string, class OsClass, defDisabled bool) OsType {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700449 os := OsType{
450 Name: name,
451 Field: strings.Title(name),
452 Class: class,
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800453
454 DefaultDisabled: defDisabled,
Colin Cross54c71122016-06-01 17:09:44 -0700455 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700456 osTypeList = append(osTypeList, os)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800457
458 if _, found := commonTargetMap[name]; found {
459 panic(fmt.Errorf("Found Os type duplicate during OsType registration: %q", name))
460 } else {
461 commonTargetMap[name] = Target{Os: os, Arch: Arch{ArchType: Common}}
462 }
463
Colin Crossa1ad8d12016-06-01 17:09:44 -0700464 return os
465}
466
467func osByName(name string) OsType {
468 for _, os := range osTypeList {
469 if os.Name == name {
470 return os
471 }
472 }
473
474 return NoOsType
Dan Willemsen490fd492015-11-24 17:53:15 -0800475}
476
Colin Crossa1ad8d12016-06-01 17:09:44 -0700477type Target struct {
478 Os OsType
479 Arch Arch
Colin Crossd3ba0392015-05-07 14:11:29 -0700480}
481
Colin Crossa1ad8d12016-06-01 17:09:44 -0700482func (target Target) String() string {
483 return target.Os.String() + "_" + target.Arch.String()
Dan Willemsen490fd492015-11-24 17:53:15 -0800484}
485
Colin Crossee0bc3b2018-10-02 22:01:37 -0700486// archMutator splits a module into a variant for each Target requested by the module. Target selection
487// for a module is in three levels, OsClass, mulitlib, and then Target.
488// OsClass selection is determined by:
489// - The HostOrDeviceSupported value passed in to InitAndroidArchModule by the module type factory, which selects
490// whether the module type can compile for host, device or both.
491// - The host_supported and device_supported properties on the module.
492// If host is supported for the module, the Host and HostCross OsClasses are are selected. If device is supported
493// for the module, the Device OsClass is selected.
494// Within each selected OsClass, the multilib selection is determined by:
495// - The compile_multilib property if it set (which may be overriden by target.android.compile_multlib or
496// target.host.compile_multilib).
497// - The default multilib passed to InitAndroidArchModule if compile_multilib was not set.
498// Valid multilib values include:
499// "both": compile for all Targets supported by the OsClass (generally x86_64 and x86, or arm64 and arm).
500// "first": compile for only a single preferred Target supported by the OsClass. This is generally x86_64 or arm64,
501// but may be arm for a 32-bit only build or a build with TARGET_PREFER_32_BIT=true set.
502// "32": compile for only a single 32-bit Target supported by the OsClass.
503// "64": compile for only a single 64-bit Target supported by the OsClass.
504// "common": compile a for a single Target that will work on all Targets suported by the OsClass (for example Java).
505//
506// Once the list of Targets is determined, the module is split into a variant for each Target.
507//
508// Modules can be initialized with InitAndroidMultiTargetsArchModule, in which case they will be split by OsClass,
509// but will have a common Target that is expected to handle all other selected Targets via ctx.MultiTargets().
Colin Cross1e676be2016-10-12 14:38:15 -0700510func archMutator(mctx BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700511 var module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800512 var ok bool
Colin Cross635c3b02016-05-18 15:37:25 -0700513 if module, ok = mctx.Module().(Module); !ok {
Colin Cross3f40fa42015-01-30 17:27:36 -0800514 return
515 }
516
Colin Cross5eca7cb2018-10-02 14:02:10 -0700517 base := module.base()
518
519 if !base.ArchSpecific() {
Colin Crossb9db4802016-06-03 01:50:47 +0000520 return
521 }
522
Colin Crossa1ad8d12016-06-01 17:09:44 -0700523 var moduleTargets []Target
Colin Crossee0bc3b2018-10-02 22:01:37 -0700524 moduleMultiTargets := make(map[int][]Target)
Colin Cross8b74d172016-09-13 09:59:14 -0700525 primaryModules := make(map[int]bool)
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700526 osClasses := base.OsClassSupported()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700527
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700528 for _, os := range osTypeList {
529 supportedClass := false
530 for _, osClass := range osClasses {
531 if os.Class == osClass {
532 supportedClass = true
533 }
534 }
535 if !supportedClass {
536 continue
537 }
538
539 osTargets := mctx.Config().Targets[os]
540 if len(osTargets) == 0 {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700541 continue
542 }
Colin Crossee0bc3b2018-10-02 22:01:37 -0700543
Colin Cross5eca7cb2018-10-02 14:02:10 -0700544 // only the primary arch in the recovery partition
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700545 if os == Android && module.InstallInRecovery() {
546 osTargets = []Target{osTargets[0]}
Colin Cross5eca7cb2018-10-02 14:02:10 -0700547 }
548
Colin Crossa9d8bee2018-10-02 13:59:46 -0700549 prefer32 := false
550 if base.prefer32 != nil {
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700551 prefer32 = base.prefer32(mctx, base, os.Class)
Colin Cross8b74d172016-09-13 09:59:14 -0700552 }
Colin Crossa9d8bee2018-10-02 13:59:46 -0700553
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700554 multilib, extraMultilib := decodeMultilib(base, os.Class)
555 targets, err := decodeMultilibTargets(multilib, osTargets, prefer32)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700556 if err != nil {
557 mctx.ModuleErrorf("%s", err.Error())
558 }
Colin Crossee0bc3b2018-10-02 22:01:37 -0700559
560 var multiTargets []Target
561 if extraMultilib != "" {
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700562 multiTargets, err = decodeMultilibTargets(extraMultilib, osTargets, prefer32)
Colin Crossee0bc3b2018-10-02 22:01:37 -0700563 if err != nil {
564 mctx.ModuleErrorf("%s", err.Error())
565 }
566 }
567
Colin Cross8b74d172016-09-13 09:59:14 -0700568 if len(targets) > 0 {
569 primaryModules[len(moduleTargets)] = true
Colin Crossee0bc3b2018-10-02 22:01:37 -0700570 moduleMultiTargets[len(moduleTargets)] = multiTargets
Colin Cross8b74d172016-09-13 09:59:14 -0700571 moduleTargets = append(moduleTargets, targets...)
572 }
Colin Crossb9db4802016-06-03 01:50:47 +0000573 }
574
Dan Willemsen3f32f032016-07-11 14:36:48 -0700575 if len(moduleTargets) == 0 {
Colin Cross5eca7cb2018-10-02 14:02:10 -0700576 base.commonProperties.Enabled = boolPtr(false)
Dan Willemsen3f32f032016-07-11 14:36:48 -0700577 return
578 }
579
Colin Crossa1ad8d12016-06-01 17:09:44 -0700580 targetNames := make([]string, len(moduleTargets))
Colin Crossb9db4802016-06-03 01:50:47 +0000581
Colin Crossa1ad8d12016-06-01 17:09:44 -0700582 for i, target := range moduleTargets {
583 targetNames[i] = target.String()
584 }
585
586 modules := mctx.CreateVariations(targetNames...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800587 for i, m := range modules {
Colin Crossee0bc3b2018-10-02 22:01:37 -0700588 m.(Module).base().SetTarget(moduleTargets[i], moduleMultiTargets[i], primaryModules[i])
Colin Cross635c3b02016-05-18 15:37:25 -0700589 m.(Module).base().setArchProperties(mctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800590 }
591}
592
Colin Crossee0bc3b2018-10-02 22:01:37 -0700593func decodeMultilib(base *ModuleBase, class OsClass) (multilib, extraMultilib string) {
594 switch class {
595 case Device:
596 multilib = String(base.commonProperties.Target.Android.Compile_multilib)
597 case Host, HostCross:
598 multilib = String(base.commonProperties.Target.Host.Compile_multilib)
599 }
600 if multilib == "" {
601 multilib = String(base.commonProperties.Compile_multilib)
602 }
603 if multilib == "" {
604 multilib = base.commonProperties.Default_multilib
605 }
606
607 if base.commonProperties.UseTargetVariants {
608 return multilib, ""
609 } else {
610 // For app modules a single arch variant will be created per OS class which is expected to handle all the
611 // selected arches. Return the common-type as multilib and any Android.bp provided multilib as extraMultilib
612 if multilib == base.commonProperties.Default_multilib {
613 multilib = "first"
614 }
615 return base.commonProperties.Default_multilib, multilib
616 }
617}
618
Colin Crosscb988072019-01-24 14:58:11 -0800619func filterArchStructFields(fields []reflect.StructField) (filteredFields []reflect.StructField, filtered bool) {
Colin Crossc17727d2018-10-24 12:42:09 -0700620 for _, field := range fields {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700621 if !proptools.HasTag(field, "android", "arch_variant") {
Colin Crosscb988072019-01-24 14:58:11 -0800622 filtered = true
Dan Willemsenb1957a52016-06-23 23:44:54 -0700623 continue
624 }
625
626 // The arch_variant field isn't necessary past this point
627 // Instead of wasting space, just remove it. Go also has a
628 // 16-bit limit on structure name length. The name is constructed
629 // based on the Go source representation of the structure, so
630 // the tag names count towards that length.
631 //
632 // TODO: handle the uncommon case of other tags being involved
633 if field.Tag == `android:"arch_variant"` {
634 field.Tag = ""
635 }
636
637 // Recurse into structs
638 switch field.Type.Kind() {
639 case reflect.Struct:
Colin Crosscb988072019-01-24 14:58:11 -0800640 var subFiltered bool
641 field.Type, subFiltered = filterArchStruct(field.Type)
642 filtered = filtered || subFiltered
643 if field.Type == nil {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700644 continue
645 }
646 case reflect.Ptr:
647 if field.Type.Elem().Kind() == reflect.Struct {
Colin Crosscb988072019-01-24 14:58:11 -0800648 nestedType, subFiltered := filterArchStruct(field.Type.Elem())
649 filtered = filtered || subFiltered
650 if nestedType == nil {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700651 continue
652 }
653 field.Type = reflect.PtrTo(nestedType)
654 }
655 case reflect.Interface:
656 panic("Interfaces are not supported in arch_variant properties")
657 }
658
Colin Crosscb988072019-01-24 14:58:11 -0800659 filteredFields = append(filteredFields, field)
Dan Willemsenb1957a52016-06-23 23:44:54 -0700660 }
Colin Crossc17727d2018-10-24 12:42:09 -0700661
Colin Crosscb988072019-01-24 14:58:11 -0800662 return filteredFields, filtered
Colin Crossc17727d2018-10-24 12:42:09 -0700663}
664
Colin Crosscb988072019-01-24 14:58:11 -0800665// filterArchStruct takes a reflect.Type that is either a sturct or a pointer to a struct, and returns a reflect.Type
666// that only contains the fields in the original type that have an `android:"arch_variant"` struct tag, and a bool
667// that is true if the new struct type has fewer fields than the original type. If there are no fields in the
668// original type with the struct tag it returns nil and true.
669func filterArchStruct(prop reflect.Type) (filteredProp reflect.Type, filtered bool) {
Colin Crossc17727d2018-10-24 12:42:09 -0700670 var fields []reflect.StructField
671
672 ptr := prop.Kind() == reflect.Ptr
673 if ptr {
674 prop = prop.Elem()
675 }
676
677 for i := 0; i < prop.NumField(); i++ {
678 fields = append(fields, prop.Field(i))
679 }
680
Colin Crosscb988072019-01-24 14:58:11 -0800681 filteredFields, filtered := filterArchStructFields(fields)
Colin Crossc17727d2018-10-24 12:42:09 -0700682
Colin Crosscb988072019-01-24 14:58:11 -0800683 if len(filteredFields) == 0 {
684 return nil, true
Dan Willemsenb1957a52016-06-23 23:44:54 -0700685 }
686
Colin Crosscb988072019-01-24 14:58:11 -0800687 if !filtered {
688 if ptr {
689 return reflect.PtrTo(prop), false
690 }
691 return prop, false
692 }
693
694 ret := reflect.StructOf(filteredFields)
Dan Willemsenb1957a52016-06-23 23:44:54 -0700695 if ptr {
696 ret = reflect.PtrTo(ret)
697 }
Colin Crossc17727d2018-10-24 12:42:09 -0700698
Dan Willemsenb1957a52016-06-23 23:44:54 -0700699 return ret, true
700}
701
Colin Crosscb988072019-01-24 14:58:11 -0800702// filterArchStruct takes a reflect.Type that is either a sturct or a pointer to a struct, and returns a list of
703// reflect.Type that only contains the fields in the original type that have an `android:"arch_variant"` struct tag,
704// and a bool that is true if the new struct type has fewer fields than the original type. If there are no fields in
705// the original type with the struct tag it returns nil and true. Each returned struct type will have a maximum of
706// 10 top level fields in it to attempt to avoid hitting the reflect.StructOf name length limit, although the limit
707// can still be reached with a single struct field with many fields in it.
708func filterArchStructSharded(prop reflect.Type) (filteredProp []reflect.Type, filtered bool) {
Colin Crossc17727d2018-10-24 12:42:09 -0700709 var fields []reflect.StructField
710
711 ptr := prop.Kind() == reflect.Ptr
712 if ptr {
713 prop = prop.Elem()
714 }
715
716 for i := 0; i < prop.NumField(); i++ {
717 fields = append(fields, prop.Field(i))
718 }
719
Colin Crosscb988072019-01-24 14:58:11 -0800720 fields, filtered = filterArchStructFields(fields)
721 if !filtered {
722 if ptr {
723 return []reflect.Type{reflect.PtrTo(prop)}, false
724 }
725 return []reflect.Type{prop}, false
726 }
Colin Crossc17727d2018-10-24 12:42:09 -0700727
728 if len(fields) == 0 {
Colin Crosscb988072019-01-24 14:58:11 -0800729 return nil, true
Colin Crossc17727d2018-10-24 12:42:09 -0700730 }
731
732 shards := shardFields(fields, 10)
733
Colin Crossc17727d2018-10-24 12:42:09 -0700734 for _, shard := range shards {
735 s := reflect.StructOf(shard)
736 if ptr {
737 s = reflect.PtrTo(s)
738 }
Colin Crosscb988072019-01-24 14:58:11 -0800739 filteredProp = append(filteredProp, s)
Colin Crossc17727d2018-10-24 12:42:09 -0700740 }
741
Colin Crosscb988072019-01-24 14:58:11 -0800742 return filteredProp, true
Colin Crossc17727d2018-10-24 12:42:09 -0700743}
744
745func shardFields(fields []reflect.StructField, shardSize int) [][]reflect.StructField {
746 ret := make([][]reflect.StructField, 0, (len(fields)+shardSize-1)/shardSize)
747 for len(fields) > shardSize {
748 ret = append(ret, fields[0:shardSize])
749 fields = fields[shardSize:]
750 }
751 if len(fields) > 0 {
752 ret = append(ret, fields)
753 }
754 return ret
755}
756
Colin Crosscb988072019-01-24 14:58:11 -0800757// createArchType takes a reflect.Type that is either a struct or a pointer to a struct, and returns a list of
758// reflect.Type that contains the arch-variant properties inside structs for each architecture, os, target, multilib,
759// etc.
Colin Crossc17727d2018-10-24 12:42:09 -0700760func createArchType(props reflect.Type) []reflect.Type {
Colin Crosscb988072019-01-24 14:58:11 -0800761 propShards, _ := filterArchStructSharded(props)
762 if len(propShards) == 0 {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700763 return nil
764 }
765
Colin Crossc17727d2018-10-24 12:42:09 -0700766 var ret []reflect.Type
767 for _, props := range propShards {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700768
Colin Crossc17727d2018-10-24 12:42:09 -0700769 variantFields := func(names []string) []reflect.StructField {
770 ret := make([]reflect.StructField, len(names))
Dan Willemsenb1957a52016-06-23 23:44:54 -0700771
Colin Crossc17727d2018-10-24 12:42:09 -0700772 for i, name := range names {
773 ret[i].Name = name
774 ret[i].Type = props
Dan Willemsen866b5632017-09-22 12:28:24 -0700775 }
Colin Crossc17727d2018-10-24 12:42:09 -0700776
777 return ret
778 }
779
780 archFields := make([]reflect.StructField, len(archTypeList))
781 for i, arch := range archTypeList {
782 variants := []string{}
783
784 for _, archVariant := range archVariants[arch] {
785 archVariant := variantReplacer.Replace(archVariant)
786 variants = append(variants, proptools.FieldNameForProperty(archVariant))
787 }
788 for _, feature := range archFeatures[arch] {
789 feature := variantReplacer.Replace(feature)
790 variants = append(variants, proptools.FieldNameForProperty(feature))
791 }
792
793 fields := variantFields(variants)
794
795 fields = append([]reflect.StructField{{
796 Name: "BlueprintEmbed",
797 Type: props,
798 Anonymous: true,
799 }}, fields...)
800
801 archFields[i] = reflect.StructField{
802 Name: arch.Field,
803 Type: reflect.StructOf(fields),
804 }
805 }
806 archType := reflect.StructOf(archFields)
807
808 multilibType := reflect.StructOf(variantFields([]string{"Lib32", "Lib64"}))
809
810 targets := []string{
811 "Host",
812 "Android64",
813 "Android32",
814 "Bionic",
815 "Linux",
816 "Not_windows",
817 "Arm_on_x86",
818 "Arm_on_x86_64",
819 }
820 for _, os := range osTypeList {
821 targets = append(targets, os.Field)
822
823 for _, archType := range osArchTypeMap[os] {
824 targets = append(targets, os.Field+"_"+archType.Name)
825
826 if os.Linux() {
827 target := "Linux_" + archType.Name
828 if !InList(target, targets) {
829 targets = append(targets, target)
830 }
831 }
832 if os.Bionic() {
833 target := "Bionic_" + archType.Name
834 if !InList(target, targets) {
835 targets = append(targets, target)
836 }
Dan Willemsen866b5632017-09-22 12:28:24 -0700837 }
838 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700839 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700840
Colin Crossc17727d2018-10-24 12:42:09 -0700841 targetType := reflect.StructOf(variantFields(targets))
842 ret = append(ret, reflect.StructOf([]reflect.StructField{
843 {
844 Name: "Arch",
845 Type: archType,
846 },
847 {
848 Name: "Multilib",
849 Type: multilibType,
850 },
851 {
852 Name: "Target",
853 Type: targetType,
854 },
855 }))
856 }
857 return ret
Dan Willemsenb1957a52016-06-23 23:44:54 -0700858}
859
860var archPropTypeMap OncePer
861
Colin Cross36242852017-06-23 15:06:31 -0700862func InitArchModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800863
864 base := m.base()
865
Colin Cross36242852017-06-23 15:06:31 -0700866 base.generalProperties = m.GetProperties()
Colin Cross3f40fa42015-01-30 17:27:36 -0800867
868 for _, properties := range base.generalProperties {
869 propertiesValue := reflect.ValueOf(properties)
Colin Cross62496a02016-08-08 15:49:17 -0700870 t := propertiesValue.Type()
Colin Cross3f40fa42015-01-30 17:27:36 -0800871 if propertiesValue.Kind() != reflect.Ptr {
Colin Crossca860ac2016-01-04 14:34:37 -0800872 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
873 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800874 }
875
876 propertiesValue = propertiesValue.Elem()
877 if propertiesValue.Kind() != reflect.Struct {
Colin Crossca860ac2016-01-04 14:34:37 -0800878 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
879 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800880 }
881
Colin Crossc17727d2018-10-24 12:42:09 -0700882 archPropTypes := archPropTypeMap.Once(t, func() interface{} {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700883 return createArchType(t)
Colin Crossc17727d2018-10-24 12:42:09 -0700884 }).([]reflect.Type)
Colin Cross3f40fa42015-01-30 17:27:36 -0800885
Colin Crossc17727d2018-10-24 12:42:09 -0700886 var archProperties []interface{}
887 for _, t := range archPropTypes {
888 archProperties = append(archProperties, reflect.New(t).Interface())
Dan Willemsenb1957a52016-06-23 23:44:54 -0700889 }
Colin Crossc17727d2018-10-24 12:42:09 -0700890 base.archProperties = append(base.archProperties, archProperties)
891 m.AddProperties(archProperties...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800892 }
893
Colin Cross36242852017-06-23 15:06:31 -0700894 base.customizableProperties = m.GetProperties()
Colin Cross3f40fa42015-01-30 17:27:36 -0800895}
896
Colin Crossa716add2015-12-16 11:07:39 -0800897var variantReplacer = strings.NewReplacer("-", "_", ".", "_")
Colin Crossec193632015-07-06 17:49:43 -0700898
Colin Cross635c3b02016-05-18 15:37:25 -0700899func (a *ModuleBase) appendProperties(ctx BottomUpMutatorContext,
Dan Willemsenb1957a52016-06-23 23:44:54 -0700900 dst interface{}, src reflect.Value, field, srcPrefix string) reflect.Value {
Colin Cross06a931b2015-10-28 17:23:31 -0700901
Dan Willemsenb1957a52016-06-23 23:44:54 -0700902 src = src.FieldByName(field)
903 if !src.IsValid() {
Colin Crosseeabb892015-11-20 13:07:51 -0800904 ctx.ModuleErrorf("field %q does not exist", srcPrefix)
Dan Willemsenb1957a52016-06-23 23:44:54 -0700905 return src
Colin Cross85a88972015-11-23 13:29:51 -0800906 }
907
Dan Willemsenb1957a52016-06-23 23:44:54 -0700908 ret := src
Colin Cross85a88972015-11-23 13:29:51 -0800909
Dan Willemsenb1957a52016-06-23 23:44:54 -0700910 if src.Kind() == reflect.Struct {
911 src = src.FieldByName("BlueprintEmbed")
Colin Cross06a931b2015-10-28 17:23:31 -0700912 }
913
Colin Cross6ee75b62016-05-05 15:57:15 -0700914 order := func(property string,
915 dstField, srcField reflect.StructField,
916 dstValue, srcValue interface{}) (proptools.Order, error) {
917 if proptools.HasTag(dstField, "android", "variant_prepend") {
918 return proptools.Prepend, nil
919 } else {
920 return proptools.Append, nil
921 }
922 }
923
Dan Willemsenb1957a52016-06-23 23:44:54 -0700924 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, order)
Colin Cross06a931b2015-10-28 17:23:31 -0700925 if err != nil {
926 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
927 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
928 } else {
929 panic(err)
930 }
931 }
Colin Cross85a88972015-11-23 13:29:51 -0800932
Dan Willemsenb1957a52016-06-23 23:44:54 -0700933 return ret
Colin Cross06a931b2015-10-28 17:23:31 -0700934}
935
Colin Cross3f40fa42015-01-30 17:27:36 -0800936// Rewrite the module's properties structs to contain arch-specific values.
Colin Cross635c3b02016-05-18 15:37:25 -0700937func (a *ModuleBase) setArchProperties(ctx BottomUpMutatorContext) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700938 arch := a.Arch()
939 os := a.Os()
Colin Crossd3ba0392015-05-07 14:11:29 -0700940
Colin Cross3f40fa42015-01-30 17:27:36 -0800941 for i := range a.generalProperties {
Colin Cross06a931b2015-10-28 17:23:31 -0700942 genProps := a.generalProperties[i]
Dan Willemsenb1957a52016-06-23 23:44:54 -0700943 if a.archProperties[i] == nil {
944 continue
945 }
Colin Crossc17727d2018-10-24 12:42:09 -0700946 for _, archProperties := range a.archProperties[i] {
947 archPropValues := reflect.ValueOf(archProperties).Elem()
Dan Willemsenb1957a52016-06-23 23:44:54 -0700948
Colin Crossc17727d2018-10-24 12:42:09 -0700949 archProp := archPropValues.FieldByName("Arch")
950 multilibProp := archPropValues.FieldByName("Multilib")
951 targetProp := archPropValues.FieldByName("Target")
Dan Willemsenb1957a52016-06-23 23:44:54 -0700952
Colin Crossc17727d2018-10-24 12:42:09 -0700953 var field string
954 var prefix string
Colin Crossd5934c82017-10-02 13:55:26 -0700955
Colin Crossc17727d2018-10-24 12:42:09 -0700956 // Handle arch-specific properties in the form:
Colin Crossd5934c82017-10-02 13:55:26 -0700957 // arch: {
Colin Crossc17727d2018-10-24 12:42:09 -0700958 // arm64: {
Colin Crossd5934c82017-10-02 13:55:26 -0700959 // key: value,
960 // },
961 // },
Colin Crossc17727d2018-10-24 12:42:09 -0700962 t := arch.ArchType
963
964 if arch.ArchType != Common {
965 field := proptools.FieldNameForProperty(t.Name)
966 prefix := "arch." + t.Name
967 archStruct := a.appendProperties(ctx, genProps, archProp, field, prefix)
968
969 // Handle arch-variant-specific properties in the form:
970 // arch: {
971 // variant: {
972 // key: value,
973 // },
974 // },
975 v := variantReplacer.Replace(arch.ArchVariant)
976 if v != "" {
977 field := proptools.FieldNameForProperty(v)
978 prefix := "arch." + t.Name + "." + v
979 a.appendProperties(ctx, genProps, archStruct, field, prefix)
980 }
981
982 // Handle cpu-variant-specific properties in the form:
983 // arch: {
984 // variant: {
985 // key: value,
986 // },
987 // },
988 if arch.CpuVariant != arch.ArchVariant {
989 c := variantReplacer.Replace(arch.CpuVariant)
990 if c != "" {
991 field := proptools.FieldNameForProperty(c)
992 prefix := "arch." + t.Name + "." + c
993 a.appendProperties(ctx, genProps, archStruct, field, prefix)
994 }
995 }
996
997 // Handle arch-feature-specific properties in the form:
998 // arch: {
999 // feature: {
1000 // key: value,
1001 // },
1002 // },
1003 for _, feature := range arch.ArchFeatures {
1004 field := proptools.FieldNameForProperty(feature)
1005 prefix := "arch." + t.Name + "." + feature
1006 a.appendProperties(ctx, genProps, archStruct, field, prefix)
1007 }
1008
1009 // Handle multilib-specific properties in the form:
1010 // multilib: {
1011 // lib32: {
1012 // key: value,
1013 // },
1014 // },
1015 field = proptools.FieldNameForProperty(t.Multilib)
1016 prefix = "multilib." + t.Multilib
1017 a.appendProperties(ctx, genProps, multilibProp, field, prefix)
Colin Cross08016332016-12-20 09:53:14 -08001018 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001019
Colin Crossc17727d2018-10-24 12:42:09 -07001020 // Handle host-specific properties in the form:
1021 // target: {
1022 // host: {
Colin Crossd5934c82017-10-02 13:55:26 -07001023 // key: value,
1024 // },
1025 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001026 if os.Class == Host || os.Class == HostCross {
1027 field = "Host"
1028 prefix = "target.host"
1029 a.appendProperties(ctx, genProps, targetProp, field, prefix)
1030 }
1031
1032 // Handle target OS generalities of the form:
1033 // target: {
1034 // bionic: {
1035 // key: value,
1036 // },
1037 // bionic_x86: {
1038 // key: value,
1039 // },
1040 // }
1041 if os.Linux() {
1042 field = "Linux"
1043 prefix = "target.linux"
1044 a.appendProperties(ctx, genProps, targetProp, field, prefix)
1045
1046 if arch.ArchType != Common {
1047 field = "Linux_" + arch.ArchType.Name
1048 prefix = "target.linux_" + arch.ArchType.Name
1049 a.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossd5934c82017-10-02 13:55:26 -07001050 }
1051 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001052
Colin Crossc17727d2018-10-24 12:42:09 -07001053 if os.Bionic() {
1054 field = "Bionic"
1055 prefix = "target.bionic"
1056 a.appendProperties(ctx, genProps, targetProp, field, prefix)
1057
1058 if arch.ArchType != Common {
1059 field = "Bionic_" + t.Name
1060 prefix = "target.bionic_" + t.Name
1061 a.appendProperties(ctx, genProps, targetProp, field, prefix)
1062 }
1063 }
1064
1065 // Handle target OS properties in the form:
1066 // target: {
1067 // linux_glibc: {
1068 // key: value,
1069 // },
1070 // not_windows: {
1071 // key: value,
1072 // },
1073 // linux_glibc_x86: {
1074 // key: value,
1075 // },
1076 // linux_glibc_arm: {
1077 // key: value,
1078 // },
1079 // android {
1080 // key: value,
1081 // },
1082 // android_arm {
1083 // key: value,
1084 // },
1085 // android_x86 {
Colin Crossd5934c82017-10-02 13:55:26 -07001086 // key: value,
1087 // },
1088 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001089 field = os.Field
1090 prefix = "target." + os.Name
1091 a.appendProperties(ctx, genProps, targetProp, field, prefix)
1092
1093 if arch.ArchType != Common {
1094 field = os.Field + "_" + t.Name
1095 prefix = "target." + os.Name + "_" + t.Name
1096 a.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossd5934c82017-10-02 13:55:26 -07001097 }
1098
Colin Crossc17727d2018-10-24 12:42:09 -07001099 if (os.Class == Host || os.Class == HostCross) && os != Windows {
1100 field := "Not_windows"
1101 prefix := "target.not_windows"
1102 a.appendProperties(ctx, genProps, targetProp, field, prefix)
1103 }
1104
1105 // Handle 64-bit device properties in the form:
1106 // target {
1107 // android64 {
1108 // key: value,
1109 // },
1110 // android32 {
Colin Crossd5934c82017-10-02 13:55:26 -07001111 // key: value,
1112 // },
1113 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001114 // WARNING: this is probably not what you want to use in your blueprints file, it selects
1115 // options for all targets on a device that supports 64-bit binaries, not just the targets
1116 // that are being compiled for 64-bit. Its expected use case is binaries like linker and
1117 // debuggerd that need to know when they are a 32-bit process running on a 64-bit device
1118 if os.Class == Device {
1119 if ctx.Config().Android64() {
1120 field := "Android64"
1121 prefix := "target.android64"
1122 a.appendProperties(ctx, genProps, targetProp, field, prefix)
1123 } else {
1124 field := "Android32"
1125 prefix := "target.android32"
1126 a.appendProperties(ctx, genProps, targetProp, field, prefix)
1127 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001128
Colin Crossc17727d2018-10-24 12:42:09 -07001129 if (arch.ArchType == X86 && (hasArmAbi(arch) ||
1130 hasArmAndroidArch(ctx.Config().Targets[Android]))) ||
1131 (arch.ArchType == Arm &&
1132 hasX86AndroidArch(ctx.Config().Targets[Android])) {
1133 field := "Arm_on_x86"
1134 prefix := "target.arm_on_x86"
1135 a.appendProperties(ctx, genProps, targetProp, field, prefix)
1136 }
1137 if (arch.ArchType == X86_64 && (hasArmAbi(arch) ||
1138 hasArmAndroidArch(ctx.Config().Targets[Android]))) ||
1139 (arch.ArchType == Arm &&
1140 hasX8664AndroidArch(ctx.Config().Targets[Android])) {
1141 field := "Arm_on_x86_64"
1142 prefix := "target.arm_on_x86_64"
1143 a.appendProperties(ctx, genProps, targetProp, field, prefix)
1144 }
Colin Cross4247f0d2017-04-13 16:56:14 -07001145 }
Colin Crossbb2e2b72016-12-08 17:23:53 -08001146 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001147 }
1148}
1149
1150func forEachInterface(v reflect.Value, f func(reflect.Value)) {
1151 switch v.Kind() {
1152 case reflect.Interface:
1153 f(v)
1154 case reflect.Struct:
1155 for i := 0; i < v.NumField(); i++ {
1156 forEachInterface(v.Field(i), f)
1157 }
1158 case reflect.Ptr:
1159 forEachInterface(v.Elem(), f)
1160 default:
1161 panic(fmt.Errorf("Unsupported kind %s", v.Kind()))
1162 }
1163}
Colin Cross4225f652015-09-17 14:33:42 -07001164
Colin Crossa1ad8d12016-06-01 17:09:44 -07001165// Convert the arch product variables into a list of targets for each os class structs
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001166func decodeTargetProductVariables(config *config) (map[OsType][]Target, error) {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001167 variables := config.productVariables
Dan Willemsen490fd492015-11-24 17:53:15 -08001168
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001169 targets := make(map[OsType][]Target)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001170 var targetErr error
1171
Colin Crossa74ca042019-01-31 14:31:51 -08001172 addTarget := func(os OsType, archName string, archVariant, cpuVariant *string, abi []string) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001173 if targetErr != nil {
1174 return
Dan Willemsen490fd492015-11-24 17:53:15 -08001175 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001176
Dan Willemsen01a3c252019-01-11 19:02:16 -08001177 arch, err := decodeArch(os, archName, archVariant, cpuVariant, abi)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001178 if err != nil {
1179 targetErr = err
1180 return
1181 }
1182
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001183 targets[os] = append(targets[os],
Colin Crossa1ad8d12016-06-01 17:09:44 -07001184 Target{
1185 Os: os,
1186 Arch: arch,
1187 })
Dan Willemsen490fd492015-11-24 17:53:15 -08001188 }
1189
Colin Cross4225f652015-09-17 14:33:42 -07001190 if variables.HostArch == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001191 return nil, fmt.Errorf("No host primary architecture set")
Colin Cross4225f652015-09-17 14:33:42 -07001192 }
1193
Colin Crossa1ad8d12016-06-01 17:09:44 -07001194 addTarget(BuildOs, *variables.HostArch, nil, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001195
Colin Crosseeabb892015-11-20 13:07:51 -08001196 if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001197 addTarget(BuildOs, *variables.HostSecondaryArch, nil, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001198 }
1199
Colin Crossff3ae9d2018-04-10 16:15:18 -07001200 if Bool(config.Host_bionic) {
Dan Willemsen01a405a2016-06-13 17:19:03 -07001201 addTarget(LinuxBionic, "x86_64", nil, nil, nil)
1202 }
1203
Colin Crossff3ae9d2018-04-10 16:15:18 -07001204 if String(variables.CrossHost) != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001205 crossHostOs := osByName(*variables.CrossHost)
1206 if crossHostOs == NoOsType {
1207 return nil, fmt.Errorf("Unknown cross host OS %q", *variables.CrossHost)
1208 }
1209
Colin Crossff3ae9d2018-04-10 16:15:18 -07001210 if String(variables.CrossHostArch) == "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001211 return nil, fmt.Errorf("No cross-host primary architecture set")
Dan Willemsen490fd492015-11-24 17:53:15 -08001212 }
1213
Colin Crossa1ad8d12016-06-01 17:09:44 -07001214 addTarget(crossHostOs, *variables.CrossHostArch, nil, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001215
1216 if variables.CrossHostSecondaryArch != nil && *variables.CrossHostSecondaryArch != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001217 addTarget(crossHostOs, *variables.CrossHostSecondaryArch, nil, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001218 }
1219 }
1220
Dan Willemsen3f32f032016-07-11 14:36:48 -07001221 if variables.DeviceArch != nil && *variables.DeviceArch != "" {
Doug Horn21b94272019-01-16 12:06:11 -08001222 var target = Android
1223 if Bool(variables.Fuchsia) {
1224 target = Fuchsia
1225 }
1226
1227 addTarget(target, *variables.DeviceArch, variables.DeviceArchVariant,
Dan Willemsen3f32f032016-07-11 14:36:48 -07001228 variables.DeviceCpuVariant, variables.DeviceAbi)
Colin Cross4225f652015-09-17 14:33:42 -07001229
Dan Willemsen3f32f032016-07-11 14:36:48 -07001230 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" {
1231 addTarget(Android, *variables.DeviceSecondaryArch,
1232 variables.DeviceSecondaryArchVariant, variables.DeviceSecondaryCpuVariant,
1233 variables.DeviceSecondaryAbi)
Colin Cross4225f652015-09-17 14:33:42 -07001234
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001235 deviceArches := targets[Android]
Dan Willemsen3f32f032016-07-11 14:36:48 -07001236 if deviceArches[0].Arch.ArchType.Multilib == deviceArches[1].Arch.ArchType.Multilib {
1237 deviceArches[1].Arch.Native = false
1238 }
Colin Cross4225f652015-09-17 14:33:42 -07001239 }
Colin Cross4225f652015-09-17 14:33:42 -07001240 }
1241
Colin Crossa1ad8d12016-06-01 17:09:44 -07001242 if targetErr != nil {
1243 return nil, targetErr
1244 }
1245
1246 return targets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001247}
1248
Colin Crossbb2e2b72016-12-08 17:23:53 -08001249// hasArmAbi returns true if arch has at least one arm ABI
1250func hasArmAbi(arch Arch) bool {
1251 for _, abi := range arch.Abi {
1252 if strings.HasPrefix(abi, "arm") {
1253 return true
1254 }
1255 }
1256 return false
1257}
1258
Colin Cross4247f0d2017-04-13 16:56:14 -07001259// hasArmArch returns true if targets has at least arm Android arch
1260func hasArmAndroidArch(targets []Target) bool {
1261 for _, target := range targets {
1262 if target.Os == Android && target.Arch.ArchType == Arm {
1263 return true
1264 }
1265 }
1266 return false
1267}
1268
Victor Khimenko5eb8ec12018-03-21 20:30:54 +01001269// hasX86Arch returns true if targets has at least x86 Android arch
1270func hasX86AndroidArch(targets []Target) bool {
1271 for _, target := range targets {
1272 if target.Os == Android && target.Arch.ArchType == X86 {
1273 return true
1274 }
1275 }
1276 return false
1277}
1278
1279// hasX8664Arch returns true if targets has at least x86_64 Android arch
1280func hasX8664AndroidArch(targets []Target) bool {
1281 for _, target := range targets {
1282 if target.Os == Android && target.Arch.ArchType == X86_64 {
1283 return true
1284 }
1285 }
1286 return false
1287}
1288
Dan Albert4098deb2016-10-19 14:04:41 -07001289type archConfig struct {
1290 arch string
1291 archVariant string
1292 cpuVariant string
1293 abi []string
1294}
1295
1296func getMegaDeviceConfig() []archConfig {
1297 return []archConfig{
Dan Willemsen110a89d2016-01-14 15:17:19 -08001298 {"arm", "armv7-a-neon", "generic", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001299 {"arm", "armv7-a-neon", "cortex-a7", []string{"armeabi-v7a"}},
1300 {"arm", "armv7-a-neon", "cortex-a8", []string{"armeabi-v7a"}},
Dan Willemsen110a89d2016-01-14 15:17:19 -08001301 {"arm", "armv7-a-neon", "cortex-a9", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001302 {"arm", "armv7-a-neon", "cortex-a15", []string{"armeabi-v7a"}},
1303 {"arm", "armv7-a-neon", "cortex-a53", []string{"armeabi-v7a"}},
1304 {"arm", "armv7-a-neon", "cortex-a53.a57", []string{"armeabi-v7a"}},
Richard Fungeb37ed32018-09-24 16:33:45 -07001305 {"arm", "armv7-a-neon", "cortex-a72", []string{"armeabi-v7a"}},
Jake Weinstein6600a442017-05-15 18:27:12 -04001306 {"arm", "armv7-a-neon", "cortex-a73", []string{"armeabi-v7a"}},
Christopher Ferrisba14a8f2018-04-23 18:15:25 -07001307 {"arm", "armv7-a-neon", "cortex-a75", []string{"armeabi-v7a"}},
Haibo Huanga31e2bd2018-10-09 14:27:28 -07001308 {"arm", "armv7-a-neon", "cortex-a76", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001309 {"arm", "armv7-a-neon", "krait", []string{"armeabi-v7a"}},
Alex Naidisae4fc182016-08-20 00:14:56 +02001310 {"arm", "armv7-a-neon", "kryo", []string{"armeabi-v7a"}},
Artem Serovd3072b02018-11-15 15:21:51 +00001311 {"arm", "armv7-a-neon", "kryo385", []string{"armeabi-v7a"}},
Junmo Park8ea49592017-07-24 07:14:55 +09001312 {"arm", "armv7-a-neon", "exynos-m1", []string{"armeabi-v7a"}},
Junmo Parkd86c9022017-07-21 09:07:47 +09001313 {"arm", "armv7-a-neon", "exynos-m2", []string{"armeabi-v7a"}},
Dan Willemsen110a89d2016-01-14 15:17:19 -08001314 {"arm64", "armv8-a", "cortex-a53", []string{"arm64-v8a"}},
Richard Fungeb37ed32018-09-24 16:33:45 -07001315 {"arm64", "armv8-a", "cortex-a72", []string{"arm64-v8a"}},
Jake Weinstein6600a442017-05-15 18:27:12 -04001316 {"arm64", "armv8-a", "cortex-a73", []string{"arm64-v8a"}},
Alex Naidisac01ff52016-08-30 15:56:33 +02001317 {"arm64", "armv8-a", "kryo", []string{"arm64-v8a"}},
Junmo Park8ea49592017-07-24 07:14:55 +09001318 {"arm64", "armv8-a", "exynos-m1", []string{"arm64-v8a"}},
Junmo Parkd86c9022017-07-21 09:07:47 +09001319 {"arm64", "armv8-a", "exynos-m2", []string{"arm64-v8a"}},
Christopher Ferrisba14a8f2018-04-23 18:15:25 -07001320 {"arm64", "armv8-2a", "cortex-a75", []string{"arm64-v8a"}},
Haibo Huanga31e2bd2018-10-09 14:27:28 -07001321 {"arm64", "armv8-2a", "cortex-a76", []string{"arm64-v8a"}},
Artem Serovd3072b02018-11-15 15:21:51 +00001322 {"arm64", "armv8-2a", "kryo385", []string{"arm64-v8a"}},
Dan Willemsen468cc312016-01-13 23:25:19 -08001323 {"mips", "mips32-fp", "", []string{"mips"}},
1324 {"mips", "mips32r2-fp", "", []string{"mips"}},
1325 {"mips", "mips32r2-fp-xburst", "", []string{"mips"}},
Dan Willemsen65fb9812016-07-19 21:37:28 -07001326 //{"mips", "mips32r6", "", []string{"mips"}},
Colin Cross1837b802017-04-26 19:10:34 -07001327 {"mips", "mips32r2dsp-fp", "", []string{"mips"}},
1328 {"mips", "mips32r2dspr2-fp", "", []string{"mips"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001329 // mips64r2 is mismatching 64r2 and 64r6 libraries during linking to libgcc
1330 //{"mips64", "mips64r2", "", []string{"mips64"}},
1331 {"mips64", "mips64r6", "", []string{"mips64"}},
1332 {"x86", "", "", []string{"x86"}},
1333 {"x86", "atom", "", []string{"x86"}},
1334 {"x86", "haswell", "", []string{"x86"}},
1335 {"x86", "ivybridge", "", []string{"x86"}},
1336 {"x86", "sandybridge", "", []string{"x86"}},
1337 {"x86", "silvermont", "", []string{"x86"}},
Dan Willemsen8a354052016-05-10 14:30:51 -07001338 {"x86", "x86_64", "", []string{"x86"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001339 {"x86_64", "", "", []string{"x86_64"}},
1340 {"x86_64", "haswell", "", []string{"x86_64"}},
1341 {"x86_64", "ivybridge", "", []string{"x86_64"}},
1342 {"x86_64", "sandybridge", "", []string{"x86_64"}},
1343 {"x86_64", "silvermont", "", []string{"x86_64"}},
1344 }
Dan Albert4098deb2016-10-19 14:04:41 -07001345}
Dan Willemsen322acaf2016-01-12 23:07:05 -08001346
Dan Albert4098deb2016-10-19 14:04:41 -07001347func getNdkAbisConfig() []archConfig {
1348 return []archConfig{
Dan Willemsenf4e06012019-01-23 22:31:26 -08001349 {"arm", "armv7-a-neon", "", []string{"armeabi"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001350 {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001351 {"x86", "", "", []string{"x86"}},
1352 {"x86_64", "", "", []string{"x86_64"}},
1353 }
1354}
1355
Dan Willemsen01a3c252019-01-11 19:02:16 -08001356func decodeArchSettings(os OsType, archConfigs []archConfig) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001357 var ret []Target
Dan Willemsen322acaf2016-01-12 23:07:05 -08001358
Dan Albert4098deb2016-10-19 14:04:41 -07001359 for _, config := range archConfigs {
Dan Willemsen01a3c252019-01-11 19:02:16 -08001360 arch, err := decodeArch(os, config.arch, &config.archVariant,
Colin Crossa74ca042019-01-31 14:31:51 -08001361 &config.cpuVariant, config.abi)
Dan Willemsen322acaf2016-01-12 23:07:05 -08001362 if err != nil {
1363 return nil, err
1364 }
Dan Willemsen17f05262016-05-31 16:27:00 -07001365 arch.Native = false
Colin Crossa1ad8d12016-06-01 17:09:44 -07001366 ret = append(ret, Target{
1367 Os: Android,
1368 Arch: arch,
1369 })
Dan Willemsen322acaf2016-01-12 23:07:05 -08001370 }
1371
1372 return ret, nil
1373}
1374
Colin Cross4225f652015-09-17 14:33:42 -07001375// Convert a set of strings from product variables into a single Arch struct
Colin Crossa74ca042019-01-31 14:31:51 -08001376func decodeArch(os OsType, arch string, archVariant, cpuVariant *string, abi []string) (Arch, error) {
Colin Cross4225f652015-09-17 14:33:42 -07001377 stringPtr := func(p *string) string {
1378 if p != nil {
1379 return *p
1380 }
1381 return ""
1382 }
1383
Colin Crosseeabb892015-11-20 13:07:51 -08001384 archType, ok := archTypeMap[arch]
1385 if !ok {
1386 return Arch{}, fmt.Errorf("unknown arch %q", arch)
1387 }
Colin Cross4225f652015-09-17 14:33:42 -07001388
Colin Crosseeabb892015-11-20 13:07:51 -08001389 a := Arch{
Colin Cross4225f652015-09-17 14:33:42 -07001390 ArchType: archType,
1391 ArchVariant: stringPtr(archVariant),
1392 CpuVariant: stringPtr(cpuVariant),
Colin Crossa74ca042019-01-31 14:31:51 -08001393 Abi: abi,
Dan Willemsen17f05262016-05-31 16:27:00 -07001394 Native: true,
Colin Crosseeabb892015-11-20 13:07:51 -08001395 }
1396
1397 if a.ArchVariant == a.ArchType.Name || a.ArchVariant == "generic" {
1398 a.ArchVariant = ""
1399 }
1400
1401 if a.CpuVariant == a.ArchType.Name || a.CpuVariant == "generic" {
1402 a.CpuVariant = ""
1403 }
1404
1405 for i := 0; i < len(a.Abi); i++ {
1406 if a.Abi[i] == "" {
1407 a.Abi = append(a.Abi[:i], a.Abi[i+1:]...)
1408 i--
1409 }
1410 }
1411
Dan Willemsen01a3c252019-01-11 19:02:16 -08001412 if a.ArchVariant == "" {
1413 if featureMap, ok := defaultArchFeatureMap[os]; ok {
1414 a.ArchFeatures = featureMap[archType]
1415 }
1416 } else {
1417 if featureMap, ok := archFeatureMap[archType]; ok {
1418 a.ArchFeatures = featureMap[a.ArchVariant]
1419 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001420 }
1421
Colin Crosseeabb892015-11-20 13:07:51 -08001422 return a, nil
Colin Cross4225f652015-09-17 14:33:42 -07001423}
1424
Colin Cross69617d32016-09-06 10:39:07 -07001425func filterMultilibTargets(targets []Target, multilib string) []Target {
1426 var ret []Target
1427 for _, t := range targets {
1428 if t.Arch.ArchType.Multilib == multilib {
1429 ret = append(ret, t)
1430 }
1431 }
1432 return ret
1433}
1434
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001435func getCommonTargets(targets []Target) []Target {
1436 var ret []Target
1437 set := make(map[string]bool)
1438
1439 for _, t := range targets {
1440 if _, found := set[t.Os.String()]; !found {
1441 set[t.Os.String()] = true
1442 ret = append(ret, commonTargetMap[t.Os.String()])
1443 }
1444 }
1445
1446 return ret
1447}
1448
Colin Cross3dceee32018-09-06 10:19:57 -07001449func firstTarget(targets []Target, filters ...string) []Target {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001450 for _, filter := range filters {
1451 buildTargets := filterMultilibTargets(targets, filter)
1452 if len(buildTargets) > 0 {
Colin Cross3dceee32018-09-06 10:19:57 -07001453 return buildTargets[:1]
Colin Cross6b4a32d2017-12-05 13:42:45 -08001454 }
1455 }
1456 return nil
1457}
1458
Colin Crossa1ad8d12016-06-01 17:09:44 -07001459// Use the module multilib setting to select one or more targets from a target list
Colin Crossee0bc3b2018-10-02 22:01:37 -07001460func decodeMultilibTargets(multilib string, targets []Target, prefer32 bool) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001461 buildTargets := []Target{}
Colin Cross6b4a32d2017-12-05 13:42:45 -08001462
Colin Cross4225f652015-09-17 14:33:42 -07001463 switch multilib {
1464 case "common":
Colin Cross6b4a32d2017-12-05 13:42:45 -08001465 buildTargets = getCommonTargets(targets)
1466 case "common_first":
1467 buildTargets = getCommonTargets(targets)
1468 if prefer32 {
Colin Cross3dceee32018-09-06 10:19:57 -07001469 buildTargets = append(buildTargets, firstTarget(targets, "lib32", "lib64")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001470 } else {
Colin Cross3dceee32018-09-06 10:19:57 -07001471 buildTargets = append(buildTargets, firstTarget(targets, "lib64", "lib32")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001472 }
Colin Cross4225f652015-09-17 14:33:42 -07001473 case "both":
Colin Cross8b74d172016-09-13 09:59:14 -07001474 if prefer32 {
1475 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1476 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1477 } else {
1478 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1479 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1480 }
Colin Cross4225f652015-09-17 14:33:42 -07001481 case "32":
Colin Cross69617d32016-09-06 10:39:07 -07001482 buildTargets = filterMultilibTargets(targets, "lib32")
Colin Cross4225f652015-09-17 14:33:42 -07001483 case "64":
Colin Cross69617d32016-09-06 10:39:07 -07001484 buildTargets = filterMultilibTargets(targets, "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001485 case "first":
1486 if prefer32 {
Colin Cross3dceee32018-09-06 10:19:57 -07001487 buildTargets = firstTarget(targets, "lib32", "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001488 } else {
Colin Cross3dceee32018-09-06 10:19:57 -07001489 buildTargets = firstTarget(targets, "lib64", "lib32")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001490 }
Colin Cross69617d32016-09-06 10:39:07 -07001491 case "prefer32":
Colin Cross3dceee32018-09-06 10:19:57 -07001492 buildTargets = filterMultilibTargets(targets, "lib32")
1493 if len(buildTargets) == 0 {
1494 buildTargets = filterMultilibTargets(targets, "lib64")
1495 }
Colin Cross4225f652015-09-17 14:33:42 -07001496 default:
Colin Cross69617d32016-09-06 10:39:07 -07001497 return nil, fmt.Errorf(`compile_multilib must be "both", "first", "32", "64", or "prefer32" found %q`,
Colin Cross4225f652015-09-17 14:33:42 -07001498 multilib)
Colin Cross4225f652015-09-17 14:33:42 -07001499 }
1500
Colin Crossa1ad8d12016-06-01 17:09:44 -07001501 return buildTargets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001502}