blob: 3fe03457900aae8bd7b6f4f34563ae50dad96979 [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 },
90 linux: {
91 // 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
Dan Willemsenb1957a52016-06-23 23:44:54 -0700106var archVariants = map[ArchType][]string{}
107var archFeatures = map[ArchType][]string{}
Colin Crossc5c24ad2015-11-20 15:35:00 -0800108var archFeatureMap = map[ArchType]map[string][]string{}
109
Dan Willemsenb1957a52016-06-23 23:44:54 -0700110func RegisterArchVariants(arch ArchType, variants ...string) {
111 checkCalledFromInit()
112 archVariants[arch] = append(archVariants[arch], variants...)
113}
114
115func RegisterArchFeatures(arch ArchType, features ...string) {
116 checkCalledFromInit()
117 archFeatures[arch] = append(archFeatures[arch], features...)
118}
119
120func RegisterArchVariantFeatures(arch ArchType, variant string, features ...string) {
121 checkCalledFromInit()
122 if variant != "" && !inList(variant, archVariants[arch]) {
123 panic(fmt.Errorf("Invalid variant %q for arch %q", variant, arch))
Colin Crossc5c24ad2015-11-20 15:35:00 -0800124 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700125
Colin Crossc5c24ad2015-11-20 15:35:00 -0800126 for _, feature := range features {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700127 if !inList(feature, archFeatures[arch]) {
Colin Crossc5c24ad2015-11-20 15:35:00 -0800128 panic(fmt.Errorf("Invalid feature %q for arch %q variant %q", feature, arch, variant))
129 }
130 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700131
Colin Crossc5c24ad2015-11-20 15:35:00 -0800132 if archFeatureMap[arch] == nil {
133 archFeatureMap[arch] = make(map[string][]string)
134 }
135 archFeatureMap[arch][variant] = features
136}
137
Colin Cross3f40fa42015-01-30 17:27:36 -0800138// An Arch indicates a single CPU architecture.
139type Arch struct {
Colin Crossc5c24ad2015-11-20 15:35:00 -0800140 ArchType ArchType
141 ArchVariant string
142 CpuVariant string
143 Abi []string
144 ArchFeatures []string
Dan Willemsen17f05262016-05-31 16:27:00 -0700145 Native bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800146}
147
148func (a Arch) String() string {
Colin Crossd3ba0392015-05-07 14:11:29 -0700149 s := a.ArchType.String()
Colin Cross3f40fa42015-01-30 17:27:36 -0800150 if a.ArchVariant != "" {
151 s += "_" + a.ArchVariant
152 }
153 if a.CpuVariant != "" {
154 s += "_" + a.CpuVariant
155 }
156 return s
157}
158
159type ArchType struct {
Colin Crossec193632015-07-06 17:49:43 -0700160 Name string
Dan Willemsenb1957a52016-06-23 23:44:54 -0700161 Field string
Colin Crossec193632015-07-06 17:49:43 -0700162 Multilib string
Colin Cross3f40fa42015-01-30 17:27:36 -0800163}
164
Colin Crossec193632015-07-06 17:49:43 -0700165func newArch(name, multilib string) ArchType {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700166 archType := ArchType{
Colin Crossec193632015-07-06 17:49:43 -0700167 Name: name,
Dan Willemsenb1957a52016-06-23 23:44:54 -0700168 Field: proptools.FieldNameForProperty(name),
Colin Crossec193632015-07-06 17:49:43 -0700169 Multilib: multilib,
Colin Cross3f40fa42015-01-30 17:27:36 -0800170 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700171 archTypeList = append(archTypeList, archType)
172 return archType
Colin Cross3f40fa42015-01-30 17:27:36 -0800173}
174
175func (a ArchType) String() string {
176 return a.Name
177}
178
Colin Crossa1ad8d12016-06-01 17:09:44 -0700179var BuildOs = func() OsType {
Dan Willemsen490fd492015-11-24 17:53:15 -0800180 switch runtime.GOOS {
181 case "linux":
182 return Linux
183 case "darwin":
184 return Darwin
185 default:
186 panic(fmt.Sprintf("unsupported OS: %s", runtime.GOOS))
187 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700188}()
189
190var (
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800191 osTypeList []OsType
192 commonTargetMap = make(map[string]Target)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700193
Dan Willemsen00fcbde2016-11-17 00:25:59 -0800194 NoOsType OsType
Dan Willemsen866b5632017-09-22 12:28:24 -0700195 Linux = NewOsType("linux_glibc", Host, false)
Dan Willemsen00fcbde2016-11-17 00:25:59 -0800196 Darwin = NewOsType("darwin", Host, false)
197 LinuxBionic = NewOsType("linux_bionic", Host, true)
198 Windows = NewOsType("windows", HostCross, true)
199 Android = NewOsType("android", Device, false)
Dan Willemsenb1957a52016-06-23 23:44:54 -0700200
201 osArchTypeMap = map[OsType][]ArchType{
Dan Willemsen00fcbde2016-11-17 00:25:59 -0800202 Linux: []ArchType{X86, X86_64},
203 LinuxBionic: []ArchType{X86_64},
204 Darwin: []ArchType{X86, X86_64},
205 Windows: []ArchType{X86, X86_64},
206 Android: []ArchType{Arm, Arm64, Mips, Mips64, X86, X86_64},
Dan Willemsenb1957a52016-06-23 23:44:54 -0700207 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700208)
209
210type OsType struct {
211 Name, Field string
212 Class OsClass
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800213
214 DefaultDisabled bool
Dan Willemsen490fd492015-11-24 17:53:15 -0800215}
216
Colin Crossa1ad8d12016-06-01 17:09:44 -0700217type OsClass int
218
219const (
Dan Willemsen0e2d97b2016-11-28 17:50:06 -0800220 Generic OsClass = iota
221 Device
Colin Crossa1ad8d12016-06-01 17:09:44 -0700222 Host
223 HostCross
224)
225
Colin Cross67a5c132017-05-09 13:45:28 -0700226func (class OsClass) String() string {
227 switch class {
228 case Generic:
229 return "generic"
230 case Device:
231 return "device"
232 case Host:
233 return "host"
234 case HostCross:
235 return "host cross"
236 default:
237 panic(fmt.Errorf("unknown class %d", class))
238 }
239}
240
Colin Crossa1ad8d12016-06-01 17:09:44 -0700241func (os OsType) String() string {
242 return os.Name
Colin Cross54c71122016-06-01 17:09:44 -0700243}
244
Dan Willemsen866b5632017-09-22 12:28:24 -0700245func (os OsType) Bionic() bool {
246 return os == Android || os == LinuxBionic
247}
248
249func (os OsType) Linux() bool {
250 return os == Android || os == Linux || os == LinuxBionic
251}
252
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800253func NewOsType(name string, class OsClass, defDisabled bool) OsType {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700254 os := OsType{
255 Name: name,
256 Field: strings.Title(name),
257 Class: class,
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800258
259 DefaultDisabled: defDisabled,
Colin Cross54c71122016-06-01 17:09:44 -0700260 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700261 osTypeList = append(osTypeList, os)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800262
263 if _, found := commonTargetMap[name]; found {
264 panic(fmt.Errorf("Found Os type duplicate during OsType registration: %q", name))
265 } else {
266 commonTargetMap[name] = Target{Os: os, Arch: Arch{ArchType: Common}}
267 }
268
Colin Crossa1ad8d12016-06-01 17:09:44 -0700269 return os
270}
271
272func osByName(name string) OsType {
273 for _, os := range osTypeList {
274 if os.Name == name {
275 return os
276 }
277 }
278
279 return NoOsType
Dan Willemsen490fd492015-11-24 17:53:15 -0800280}
281
Colin Crossa1ad8d12016-06-01 17:09:44 -0700282type Target struct {
283 Os OsType
284 Arch Arch
Colin Crossd3ba0392015-05-07 14:11:29 -0700285}
286
Colin Crossa1ad8d12016-06-01 17:09:44 -0700287func (target Target) String() string {
288 return target.Os.String() + "_" + target.Arch.String()
Dan Willemsen490fd492015-11-24 17:53:15 -0800289}
290
Colin Cross1e676be2016-10-12 14:38:15 -0700291func archMutator(mctx BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700292 var module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800293 var ok bool
Colin Cross635c3b02016-05-18 15:37:25 -0700294 if module, ok = mctx.Module().(Module); !ok {
Colin Cross3f40fa42015-01-30 17:27:36 -0800295 return
296 }
297
Dan Willemsen0b24c742016-10-04 15:13:37 -0700298 if !module.base().ArchSpecific() {
Colin Crossb9db4802016-06-03 01:50:47 +0000299 return
300 }
301
Dan Willemsen0b24c742016-10-04 15:13:37 -0700302 osClasses := module.base().OsClassSupported()
303
Colin Crossa1ad8d12016-06-01 17:09:44 -0700304 var moduleTargets []Target
Colin Cross8b74d172016-09-13 09:59:14 -0700305 primaryModules := make(map[int]bool)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700306
307 for _, class := range osClasses {
308 targets := mctx.AConfig().Targets[class]
309 if len(targets) == 0 {
310 continue
311 }
Colin Cross69617d32016-09-06 10:39:07 -0700312 var multilib string
313 switch class {
314 case Device:
315 multilib = module.base().commonProperties.Target.Android.Compile_multilib
316 case Host, HostCross:
317 multilib = module.base().commonProperties.Target.Host.Compile_multilib
318 }
319 if multilib == "" {
320 multilib = module.base().commonProperties.Compile_multilib
321 }
322 if multilib == "" {
323 multilib = module.base().commonProperties.Default_multilib
324 }
Colin Cross628a88c2016-09-13 15:15:51 -0700325 var prefer32 bool
326 switch class {
327 case Device:
Colin Cross8b74d172016-09-13 09:59:14 -0700328 prefer32 = mctx.AConfig().DevicePrefer32BitExecutables()
Colin Cross628a88c2016-09-13 15:15:51 -0700329 case HostCross:
330 // Windows builds always prefer 32-bit
331 prefer32 = true
Colin Cross8b74d172016-09-13 09:59:14 -0700332 }
333 targets, err := decodeMultilib(multilib, targets, prefer32)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700334 if err != nil {
335 mctx.ModuleErrorf("%s", err.Error())
336 }
Colin Cross8b74d172016-09-13 09:59:14 -0700337 if len(targets) > 0 {
338 primaryModules[len(moduleTargets)] = true
339 moduleTargets = append(moduleTargets, targets...)
340 }
Colin Crossb9db4802016-06-03 01:50:47 +0000341 }
342
Dan Willemsen3f32f032016-07-11 14:36:48 -0700343 if len(moduleTargets) == 0 {
344 module.base().commonProperties.Enabled = boolPtr(false)
345 return
346 }
347
Colin Crossa1ad8d12016-06-01 17:09:44 -0700348 targetNames := make([]string, len(moduleTargets))
Colin Crossb9db4802016-06-03 01:50:47 +0000349
Colin Crossa1ad8d12016-06-01 17:09:44 -0700350 for i, target := range moduleTargets {
351 targetNames[i] = target.String()
352 }
353
354 modules := mctx.CreateVariations(targetNames...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800355 for i, m := range modules {
Colin Cross8b74d172016-09-13 09:59:14 -0700356 m.(Module).base().SetTarget(moduleTargets[i], primaryModules[i])
Colin Cross635c3b02016-05-18 15:37:25 -0700357 m.(Module).base().setArchProperties(mctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800358 }
359}
360
Dan Willemsenb1957a52016-06-23 23:44:54 -0700361func filterArchStruct(prop reflect.Type) (reflect.Type, bool) {
362 var fields []reflect.StructField
363
364 ptr := prop.Kind() == reflect.Ptr
365 if ptr {
366 prop = prop.Elem()
367 }
368
369 for i := 0; i < prop.NumField(); i++ {
370 field := prop.Field(i)
371 if !proptools.HasTag(field, "android", "arch_variant") {
372 continue
373 }
374
375 // The arch_variant field isn't necessary past this point
376 // Instead of wasting space, just remove it. Go also has a
377 // 16-bit limit on structure name length. The name is constructed
378 // based on the Go source representation of the structure, so
379 // the tag names count towards that length.
380 //
381 // TODO: handle the uncommon case of other tags being involved
382 if field.Tag == `android:"arch_variant"` {
383 field.Tag = ""
384 }
385
386 // Recurse into structs
387 switch field.Type.Kind() {
388 case reflect.Struct:
389 var ok bool
390 field.Type, ok = filterArchStruct(field.Type)
391 if !ok {
392 continue
393 }
394 case reflect.Ptr:
395 if field.Type.Elem().Kind() == reflect.Struct {
396 nestedType, ok := filterArchStruct(field.Type.Elem())
397 if !ok {
398 continue
399 }
400 field.Type = reflect.PtrTo(nestedType)
401 }
402 case reflect.Interface:
403 panic("Interfaces are not supported in arch_variant properties")
404 }
405
406 fields = append(fields, field)
407 }
408 if len(fields) == 0 {
409 return nil, false
410 }
411
412 ret := reflect.StructOf(fields)
413 if ptr {
414 ret = reflect.PtrTo(ret)
415 }
416 return ret, true
417}
418
419func createArchType(props reflect.Type) reflect.Type {
420 props, ok := filterArchStruct(props)
421 if !ok {
422 return nil
423 }
424
425 variantFields := func(names []string) []reflect.StructField {
426 ret := make([]reflect.StructField, len(names))
427
428 for i, name := range names {
429 ret[i].Name = name
430 ret[i].Type = props
431 }
432
433 return ret
434 }
435
436 archFields := make([]reflect.StructField, len(archTypeList))
437 for i, arch := range archTypeList {
438 variants := []string{}
439
440 for _, archVariant := range archVariants[arch] {
Colin Cross0906f172017-04-26 14:00:43 -0700441 archVariant := variantReplacer.Replace(archVariant)
Dan Willemsenb1957a52016-06-23 23:44:54 -0700442 variants = append(variants, proptools.FieldNameForProperty(archVariant))
443 }
444 for _, feature := range archFeatures[arch] {
Colin Cross0906f172017-04-26 14:00:43 -0700445 feature := variantReplacer.Replace(feature)
Dan Willemsenb1957a52016-06-23 23:44:54 -0700446 variants = append(variants, proptools.FieldNameForProperty(feature))
447 }
448
449 fields := variantFields(variants)
450
451 fields = append([]reflect.StructField{reflect.StructField{
452 Name: "BlueprintEmbed",
453 Type: props,
454 Anonymous: true,
455 }}, fields...)
456
457 archFields[i] = reflect.StructField{
458 Name: arch.Field,
459 Type: reflect.StructOf(fields),
460 }
461 }
462 archType := reflect.StructOf(archFields)
463
464 multilibType := reflect.StructOf(variantFields([]string{"Lib32", "Lib64"}))
465
466 targets := []string{
467 "Host",
468 "Android64",
469 "Android32",
Dan Willemsen866b5632017-09-22 12:28:24 -0700470 "Bionic",
471 "Linux",
Dan Willemsenb1957a52016-06-23 23:44:54 -0700472 "Not_windows",
Colin Crossbb2e2b72016-12-08 17:23:53 -0800473 "Arm_on_x86",
474 "Arm_on_x86_64",
Dan Willemsenb1957a52016-06-23 23:44:54 -0700475 }
476 for _, os := range osTypeList {
477 targets = append(targets, os.Field)
478
479 for _, archType := range osArchTypeMap[os] {
480 targets = append(targets, os.Field+"_"+archType.Name)
Dan Willemsen866b5632017-09-22 12:28:24 -0700481
482 if os == Linux { // TODO(dwillemsen): os.Linux()
483 target := "Linux_" + archType.Name
484 if !inList(target, targets) {
485 targets = append(targets, target)
486 }
487 }
488 if os.Bionic() {
489 target := "Bionic_" + archType.Name
490 if !inList(target, targets) {
491 targets = append(targets, target)
492 }
493 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700494 }
495 }
496
497 targetType := reflect.StructOf(variantFields(targets))
498 return reflect.StructOf([]reflect.StructField{
499 reflect.StructField{
500 Name: "Arch",
501 Type: archType,
502 },
503 reflect.StructField{
504 Name: "Multilib",
505 Type: multilibType,
506 },
507 reflect.StructField{
508 Name: "Target",
509 Type: targetType,
510 },
511 })
512}
513
514var archPropTypeMap OncePer
515
Colin Cross36242852017-06-23 15:06:31 -0700516func InitArchModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800517
518 base := m.base()
519
Colin Cross36242852017-06-23 15:06:31 -0700520 base.generalProperties = m.GetProperties()
Colin Cross3f40fa42015-01-30 17:27:36 -0800521
522 for _, properties := range base.generalProperties {
523 propertiesValue := reflect.ValueOf(properties)
Colin Cross62496a02016-08-08 15:49:17 -0700524 t := propertiesValue.Type()
Colin Cross3f40fa42015-01-30 17:27:36 -0800525 if propertiesValue.Kind() != reflect.Ptr {
Colin Crossca860ac2016-01-04 14:34:37 -0800526 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
527 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800528 }
529
530 propertiesValue = propertiesValue.Elem()
531 if propertiesValue.Kind() != reflect.Struct {
Colin Crossca860ac2016-01-04 14:34:37 -0800532 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
533 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800534 }
535
Dan Willemsenb1957a52016-06-23 23:44:54 -0700536 archPropType := archPropTypeMap.Once(t, func() interface{} {
537 return createArchType(t)
Colin Cross3f40fa42015-01-30 17:27:36 -0800538 })
539
Dan Willemsenb1957a52016-06-23 23:44:54 -0700540 if archPropType != nil {
541 base.archProperties = append(base.archProperties, reflect.New(archPropType.(reflect.Type)).Interface())
542 } else {
543 base.archProperties = append(base.archProperties, nil)
544 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800545 }
546
Colin Cross3f40fa42015-01-30 17:27:36 -0800547 for _, asp := range base.archProperties {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700548 if asp != nil {
Colin Cross36242852017-06-23 15:06:31 -0700549 m.AddProperties(asp)
Dan Willemsenb1957a52016-06-23 23:44:54 -0700550 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800551 }
552
Colin Cross36242852017-06-23 15:06:31 -0700553 base.customizableProperties = m.GetProperties()
Colin Cross3f40fa42015-01-30 17:27:36 -0800554}
555
Colin Crossa716add2015-12-16 11:07:39 -0800556var variantReplacer = strings.NewReplacer("-", "_", ".", "_")
Colin Crossec193632015-07-06 17:49:43 -0700557
Colin Cross635c3b02016-05-18 15:37:25 -0700558func (a *ModuleBase) appendProperties(ctx BottomUpMutatorContext,
Dan Willemsenb1957a52016-06-23 23:44:54 -0700559 dst interface{}, src reflect.Value, field, srcPrefix string) reflect.Value {
Colin Cross06a931b2015-10-28 17:23:31 -0700560
Dan Willemsenb1957a52016-06-23 23:44:54 -0700561 src = src.FieldByName(field)
562 if !src.IsValid() {
Colin Crosseeabb892015-11-20 13:07:51 -0800563 ctx.ModuleErrorf("field %q does not exist", srcPrefix)
Dan Willemsenb1957a52016-06-23 23:44:54 -0700564 return src
Colin Cross85a88972015-11-23 13:29:51 -0800565 }
566
Dan Willemsenb1957a52016-06-23 23:44:54 -0700567 ret := src
Colin Cross85a88972015-11-23 13:29:51 -0800568
Dan Willemsenb1957a52016-06-23 23:44:54 -0700569 if src.Kind() == reflect.Struct {
570 src = src.FieldByName("BlueprintEmbed")
Colin Cross06a931b2015-10-28 17:23:31 -0700571 }
572
Colin Cross6ee75b62016-05-05 15:57:15 -0700573 order := func(property string,
574 dstField, srcField reflect.StructField,
575 dstValue, srcValue interface{}) (proptools.Order, error) {
576 if proptools.HasTag(dstField, "android", "variant_prepend") {
577 return proptools.Prepend, nil
578 } else {
579 return proptools.Append, nil
580 }
581 }
582
Dan Willemsenb1957a52016-06-23 23:44:54 -0700583 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, order)
Colin Cross06a931b2015-10-28 17:23:31 -0700584 if err != nil {
585 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
586 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
587 } else {
588 panic(err)
589 }
590 }
Colin Cross85a88972015-11-23 13:29:51 -0800591
Dan Willemsenb1957a52016-06-23 23:44:54 -0700592 return ret
Colin Cross06a931b2015-10-28 17:23:31 -0700593}
594
Colin Cross3f40fa42015-01-30 17:27:36 -0800595// Rewrite the module's properties structs to contain arch-specific values.
Colin Cross635c3b02016-05-18 15:37:25 -0700596func (a *ModuleBase) setArchProperties(ctx BottomUpMutatorContext) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700597 arch := a.Arch()
598 os := a.Os()
Colin Crossd3ba0392015-05-07 14:11:29 -0700599
Colin Cross2fe66872015-03-30 17:20:39 -0700600 if arch.ArchType == Common {
601 return
602 }
603
Colin Cross3f40fa42015-01-30 17:27:36 -0800604 for i := range a.generalProperties {
Colin Cross06a931b2015-10-28 17:23:31 -0700605 genProps := a.generalProperties[i]
Dan Willemsenb1957a52016-06-23 23:44:54 -0700606 if a.archProperties[i] == nil {
607 continue
608 }
609 archProps := reflect.ValueOf(a.archProperties[i]).Elem()
610
611 archProp := archProps.FieldByName("Arch")
612 multilibProp := archProps.FieldByName("Multilib")
613 targetProp := archProps.FieldByName("Target")
614
Colin Cross3f40fa42015-01-30 17:27:36 -0800615 // Handle arch-specific properties in the form:
Colin Crossb05bff22015-04-30 15:08:04 -0700616 // arch: {
617 // arm64: {
Colin Cross3f40fa42015-01-30 17:27:36 -0800618 // key: value,
619 // },
620 // },
621 t := arch.ArchType
Colin Cross06a931b2015-10-28 17:23:31 -0700622
Colin Crossec193632015-07-06 17:49:43 -0700623 field := proptools.FieldNameForProperty(t.Name)
Colin Cross06a931b2015-10-28 17:23:31 -0700624 prefix := "arch." + t.Name
Dan Willemsenb1957a52016-06-23 23:44:54 -0700625 archStruct := a.appendProperties(ctx, genProps, archProp, field, prefix)
Colin Crossec193632015-07-06 17:49:43 -0700626
627 // Handle arch-variant-specific properties in the form:
628 // arch: {
629 // variant: {
630 // key: value,
631 // },
632 // },
Colin Crossa716add2015-12-16 11:07:39 -0800633 v := variantReplacer.Replace(arch.ArchVariant)
Colin Crossec193632015-07-06 17:49:43 -0700634 if v != "" {
635 field := proptools.FieldNameForProperty(v)
Colin Cross85a88972015-11-23 13:29:51 -0800636 prefix := "arch." + t.Name + "." + v
637 a.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossec193632015-07-06 17:49:43 -0700638 }
639
640 // Handle cpu-variant-specific properties in the form:
641 // arch: {
642 // variant: {
643 // key: value,
644 // },
645 // },
Colin Cross08016332016-12-20 09:53:14 -0800646 if arch.CpuVariant != arch.ArchVariant {
647 c := variantReplacer.Replace(arch.CpuVariant)
648 if c != "" {
649 field := proptools.FieldNameForProperty(c)
650 prefix := "arch." + t.Name + "." + c
651 a.appendProperties(ctx, genProps, archStruct, field, prefix)
652 }
Colin Crossec193632015-07-06 17:49:43 -0700653 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800654
Colin Crossc5c24ad2015-11-20 15:35:00 -0800655 // Handle arch-feature-specific properties in the form:
656 // arch: {
657 // feature: {
658 // key: value,
659 // },
660 // },
661 for _, feature := range arch.ArchFeatures {
662 field := proptools.FieldNameForProperty(feature)
Colin Cross85a88972015-11-23 13:29:51 -0800663 prefix := "arch." + t.Name + "." + feature
664 a.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossc5c24ad2015-11-20 15:35:00 -0800665 }
666
Colin Cross3f40fa42015-01-30 17:27:36 -0800667 // Handle multilib-specific properties in the form:
Colin Crossb05bff22015-04-30 15:08:04 -0700668 // multilib: {
669 // lib32: {
Colin Cross3f40fa42015-01-30 17:27:36 -0800670 // key: value,
671 // },
672 // },
Colin Cross06a931b2015-10-28 17:23:31 -0700673 field = proptools.FieldNameForProperty(t.Multilib)
674 prefix = "multilib." + t.Multilib
Dan Willemsenb1957a52016-06-23 23:44:54 -0700675 a.appendProperties(ctx, genProps, multilibProp, field, prefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800676
Colin Crossa1ad8d12016-06-01 17:09:44 -0700677 // Handle host-specific properties in the form:
Colin Crossb05bff22015-04-30 15:08:04 -0700678 // target: {
679 // host: {
Colin Cross3f40fa42015-01-30 17:27:36 -0800680 // key: value,
681 // },
682 // },
Colin Crossa1ad8d12016-06-01 17:09:44 -0700683 if os.Class == Host || os.Class == HostCross {
684 field = "Host"
685 prefix = "target.host"
Dan Willemsenb1957a52016-06-23 23:44:54 -0700686 a.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700687 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800688
Dan Willemsen866b5632017-09-22 12:28:24 -0700689 // Handle target OS generalities of the form:
690 // target: {
691 // bionic: {
692 // key: value,
693 // },
694 // bionic_x86: {
695 // key: value,
696 // },
697 // }
698 if os == Linux { // TODO(dwillemsen): os.Linux()
699 field = "Linux"
700 prefix = "target.linux"
701 a.appendProperties(ctx, genProps, targetProp, field, prefix)
702
703 field = "Linux_" + t.Name
704 prefix = "target.linux_" + t.Name
705 a.appendProperties(ctx, genProps, targetProp, field, prefix)
706 }
707
708 if os.Bionic() {
709 field = "Bionic"
710 prefix = "target.bionic"
711 a.appendProperties(ctx, genProps, targetProp, field, prefix)
712
713 field = "Bionic_" + t.Name
714 prefix = "target.bionic_" + t.Name
715 a.appendProperties(ctx, genProps, targetProp, field, prefix)
716 }
717
Colin Crossa1ad8d12016-06-01 17:09:44 -0700718 // Handle target OS properties in the form:
Colin Crossb05bff22015-04-30 15:08:04 -0700719 // target: {
Dan Willemsen866b5632017-09-22 12:28:24 -0700720 // linux_glibc: {
Colin Cross3f40fa42015-01-30 17:27:36 -0800721 // key: value,
722 // },
Colin Crossb05bff22015-04-30 15:08:04 -0700723 // not_windows: {
724 // key: value,
725 // },
Dan Willemsen866b5632017-09-22 12:28:24 -0700726 // linux_glibc_x86: {
Colin Crossb05bff22015-04-30 15:08:04 -0700727 // key: value,
728 // },
Dan Willemsen866b5632017-09-22 12:28:24 -0700729 // linux_glibc_arm: {
Colin Cross3f40fa42015-01-30 17:27:36 -0800730 // key: value,
731 // },
Colin Crossa1ad8d12016-06-01 17:09:44 -0700732 // android {
733 // key: value,
734 // },
735 // android_arm {
736 // key: value,
737 // },
738 // android_x86 {
739 // key: value,
740 // },
Colin Cross3f40fa42015-01-30 17:27:36 -0800741 // },
Colin Crossa1ad8d12016-06-01 17:09:44 -0700742 field = os.Field
743 prefix = "target." + os.Name
Dan Willemsenb1957a52016-06-23 23:44:54 -0700744 a.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossb9db4802016-06-03 01:50:47 +0000745
Colin Crossa1ad8d12016-06-01 17:09:44 -0700746 field = os.Field + "_" + t.Name
747 prefix = "target." + os.Name + "_" + t.Name
Dan Willemsenb1957a52016-06-23 23:44:54 -0700748 a.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossb9db4802016-06-03 01:50:47 +0000749
Colin Crossa1ad8d12016-06-01 17:09:44 -0700750 if (os.Class == Host || os.Class == HostCross) && os != Windows {
751 field := "Not_windows"
752 prefix := "target.not_windows"
Dan Willemsenb1957a52016-06-23 23:44:54 -0700753 a.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800754 }
755
Colin Crossf8209412015-03-26 14:44:26 -0700756 // Handle 64-bit device properties in the form:
757 // target {
758 // android64 {
759 // key: value,
760 // },
761 // android32 {
762 // key: value,
763 // },
764 // },
765 // WARNING: this is probably not what you want to use in your blueprints file, it selects
766 // options for all targets on a device that supports 64-bit binaries, not just the targets
767 // that are being compiled for 64-bit. Its expected use case is binaries like linker and
768 // debuggerd that need to know when they are a 32-bit process running on a 64-bit device
Colin Crossa1ad8d12016-06-01 17:09:44 -0700769 if os.Class == Device {
770 if ctx.AConfig().Android64() {
Colin Cross06a931b2015-10-28 17:23:31 -0700771 field := "Android64"
772 prefix := "target.android64"
Dan Willemsenb1957a52016-06-23 23:44:54 -0700773 a.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossf8209412015-03-26 14:44:26 -0700774 } else {
Colin Cross06a931b2015-10-28 17:23:31 -0700775 field := "Android32"
776 prefix := "target.android32"
Dan Willemsenb1957a52016-06-23 23:44:54 -0700777 a.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossf8209412015-03-26 14:44:26 -0700778 }
Colin Crossbb2e2b72016-12-08 17:23:53 -0800779
Colin Cross4247f0d2017-04-13 16:56:14 -0700780 if arch.ArchType == X86 && (hasArmAbi(arch) ||
781 hasArmAndroidArch(ctx.AConfig().Targets[Device])) {
782 field := "Arm_on_x86"
783 prefix := "target.arm_on_x86"
784 a.appendProperties(ctx, genProps, targetProp, field, prefix)
785 }
786 if arch.ArchType == X86_64 && (hasArmAbi(arch) ||
787 hasArmAndroidArch(ctx.AConfig().Targets[Device])) {
788 field := "Arm_on_x86_64"
789 prefix := "target.arm_on_x86_64"
790 a.appendProperties(ctx, genProps, targetProp, field, prefix)
791 }
Colin Crossbb2e2b72016-12-08 17:23:53 -0800792 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800793 }
794}
795
796func forEachInterface(v reflect.Value, f func(reflect.Value)) {
797 switch v.Kind() {
798 case reflect.Interface:
799 f(v)
800 case reflect.Struct:
801 for i := 0; i < v.NumField(); i++ {
802 forEachInterface(v.Field(i), f)
803 }
804 case reflect.Ptr:
805 forEachInterface(v.Elem(), f)
806 default:
807 panic(fmt.Errorf("Unsupported kind %s", v.Kind()))
808 }
809}
Colin Cross4225f652015-09-17 14:33:42 -0700810
Colin Crossa1ad8d12016-06-01 17:09:44 -0700811// Convert the arch product variables into a list of targets for each os class structs
Colin Cross9272ade2016-08-17 15:24:12 -0700812func decodeTargetProductVariables(config *config) (map[OsClass][]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700813 variables := config.ProductVariables
Dan Willemsen490fd492015-11-24 17:53:15 -0800814
Colin Crossa1ad8d12016-06-01 17:09:44 -0700815 targets := make(map[OsClass][]Target)
816 var targetErr error
817
818 addTarget := func(os OsType, archName string, archVariant, cpuVariant *string, abi *[]string) {
819 if targetErr != nil {
820 return
Dan Willemsen490fd492015-11-24 17:53:15 -0800821 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700822
823 arch, err := decodeArch(archName, archVariant, cpuVariant, abi)
824 if err != nil {
825 targetErr = err
826 return
827 }
828
829 targets[os.Class] = append(targets[os.Class],
830 Target{
831 Os: os,
832 Arch: arch,
833 })
Dan Willemsen490fd492015-11-24 17:53:15 -0800834 }
835
Colin Cross4225f652015-09-17 14:33:42 -0700836 if variables.HostArch == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700837 return nil, fmt.Errorf("No host primary architecture set")
Colin Cross4225f652015-09-17 14:33:42 -0700838 }
839
Colin Crossa1ad8d12016-06-01 17:09:44 -0700840 addTarget(BuildOs, *variables.HostArch, nil, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -0700841
Colin Crosseeabb892015-11-20 13:07:51 -0800842 if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700843 addTarget(BuildOs, *variables.HostSecondaryArch, nil, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -0800844 }
845
Dan Willemsen01a405a2016-06-13 17:19:03 -0700846 if config.Host_bionic != nil && *config.Host_bionic {
847 addTarget(LinuxBionic, "x86_64", nil, nil, nil)
848 }
849
Dan Willemsen490fd492015-11-24 17:53:15 -0800850 if variables.CrossHost != nil && *variables.CrossHost != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700851 crossHostOs := osByName(*variables.CrossHost)
852 if crossHostOs == NoOsType {
853 return nil, fmt.Errorf("Unknown cross host OS %q", *variables.CrossHost)
854 }
855
Dan Willemsen490fd492015-11-24 17:53:15 -0800856 if variables.CrossHostArch == nil || *variables.CrossHostArch == "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700857 return nil, fmt.Errorf("No cross-host primary architecture set")
Dan Willemsen490fd492015-11-24 17:53:15 -0800858 }
859
Colin Crossa1ad8d12016-06-01 17:09:44 -0700860 addTarget(crossHostOs, *variables.CrossHostArch, nil, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -0800861
862 if variables.CrossHostSecondaryArch != nil && *variables.CrossHostSecondaryArch != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700863 addTarget(crossHostOs, *variables.CrossHostSecondaryArch, nil, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -0800864 }
865 }
866
Dan Willemsen3f32f032016-07-11 14:36:48 -0700867 if variables.DeviceArch != nil && *variables.DeviceArch != "" {
868 addTarget(Android, *variables.DeviceArch, variables.DeviceArchVariant,
869 variables.DeviceCpuVariant, variables.DeviceAbi)
Colin Cross4225f652015-09-17 14:33:42 -0700870
Dan Willemsen3f32f032016-07-11 14:36:48 -0700871 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" {
872 addTarget(Android, *variables.DeviceSecondaryArch,
873 variables.DeviceSecondaryArchVariant, variables.DeviceSecondaryCpuVariant,
874 variables.DeviceSecondaryAbi)
Colin Cross4225f652015-09-17 14:33:42 -0700875
Dan Willemsen3f32f032016-07-11 14:36:48 -0700876 deviceArches := targets[Device]
877 if deviceArches[0].Arch.ArchType.Multilib == deviceArches[1].Arch.ArchType.Multilib {
878 deviceArches[1].Arch.Native = false
879 }
Colin Cross4225f652015-09-17 14:33:42 -0700880 }
Colin Cross4225f652015-09-17 14:33:42 -0700881 }
882
Colin Crossa1ad8d12016-06-01 17:09:44 -0700883 if targetErr != nil {
884 return nil, targetErr
885 }
886
887 return targets, nil
Colin Cross4225f652015-09-17 14:33:42 -0700888}
889
Colin Crossbb2e2b72016-12-08 17:23:53 -0800890// hasArmAbi returns true if arch has at least one arm ABI
891func hasArmAbi(arch Arch) bool {
892 for _, abi := range arch.Abi {
893 if strings.HasPrefix(abi, "arm") {
894 return true
895 }
896 }
897 return false
898}
899
Colin Cross4247f0d2017-04-13 16:56:14 -0700900// hasArmArch returns true if targets has at least arm Android arch
901func hasArmAndroidArch(targets []Target) bool {
902 for _, target := range targets {
903 if target.Os == Android && target.Arch.ArchType == Arm {
904 return true
905 }
906 }
907 return false
908}
909
Dan Albert4098deb2016-10-19 14:04:41 -0700910type archConfig struct {
911 arch string
912 archVariant string
913 cpuVariant string
914 abi []string
915}
916
917func getMegaDeviceConfig() []archConfig {
918 return []archConfig{
Dan Willemsen110a89d2016-01-14 15:17:19 -0800919 // armv5 is only used for unbundled apps
920 //{"arm", "armv5te", "", []string{"armeabi"}},
921 {"arm", "armv7-a", "generic", []string{"armeabi-v7a"}},
922 {"arm", "armv7-a-neon", "generic", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -0800923 {"arm", "armv7-a-neon", "cortex-a7", []string{"armeabi-v7a"}},
924 {"arm", "armv7-a-neon", "cortex-a8", []string{"armeabi-v7a"}},
Dan Willemsen110a89d2016-01-14 15:17:19 -0800925 {"arm", "armv7-a-neon", "cortex-a9", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -0800926 {"arm", "armv7-a-neon", "cortex-a15", []string{"armeabi-v7a"}},
927 {"arm", "armv7-a-neon", "cortex-a53", []string{"armeabi-v7a"}},
928 {"arm", "armv7-a-neon", "cortex-a53.a57", []string{"armeabi-v7a"}},
Jake Weinstein6600a442017-05-15 18:27:12 -0400929 {"arm", "armv7-a-neon", "cortex-a73", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -0800930 {"arm", "armv7-a-neon", "denver", []string{"armeabi-v7a"}},
931 {"arm", "armv7-a-neon", "krait", []string{"armeabi-v7a"}},
Alex Naidisae4fc182016-08-20 00:14:56 +0200932 {"arm", "armv7-a-neon", "kryo", []string{"armeabi-v7a"}},
Junmo Park8ea49592017-07-24 07:14:55 +0900933 {"arm", "armv7-a-neon", "exynos-m1", []string{"armeabi-v7a"}},
Junmo Parkd86c9022017-07-21 09:07:47 +0900934 {"arm", "armv7-a-neon", "exynos-m2", []string{"armeabi-v7a"}},
Dan Willemsen110a89d2016-01-14 15:17:19 -0800935 {"arm64", "armv8-a", "cortex-a53", []string{"arm64-v8a"}},
Jake Weinstein6600a442017-05-15 18:27:12 -0400936 {"arm64", "armv8-a", "cortex-a73", []string{"arm64-v8a"}},
Dan Willemsen110a89d2016-01-14 15:17:19 -0800937 {"arm64", "armv8-a", "denver64", []string{"arm64-v8a"}},
Alex Naidisac01ff52016-08-30 15:56:33 +0200938 {"arm64", "armv8-a", "kryo", []string{"arm64-v8a"}},
Junmo Park8ea49592017-07-24 07:14:55 +0900939 {"arm64", "armv8-a", "exynos-m1", []string{"arm64-v8a"}},
Junmo Parkd86c9022017-07-21 09:07:47 +0900940 {"arm64", "armv8-a", "exynos-m2", []string{"arm64-v8a"}},
Dan Willemsen468cc312016-01-13 23:25:19 -0800941 {"mips", "mips32-fp", "", []string{"mips"}},
942 {"mips", "mips32r2-fp", "", []string{"mips"}},
943 {"mips", "mips32r2-fp-xburst", "", []string{"mips"}},
Dan Willemsen65fb9812016-07-19 21:37:28 -0700944 //{"mips", "mips32r6", "", []string{"mips"}},
Colin Cross1837b802017-04-26 19:10:34 -0700945 {"mips", "mips32r2dsp-fp", "", []string{"mips"}},
946 {"mips", "mips32r2dspr2-fp", "", []string{"mips"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -0800947 // mips64r2 is mismatching 64r2 and 64r6 libraries during linking to libgcc
948 //{"mips64", "mips64r2", "", []string{"mips64"}},
949 {"mips64", "mips64r6", "", []string{"mips64"}},
950 {"x86", "", "", []string{"x86"}},
951 {"x86", "atom", "", []string{"x86"}},
952 {"x86", "haswell", "", []string{"x86"}},
953 {"x86", "ivybridge", "", []string{"x86"}},
954 {"x86", "sandybridge", "", []string{"x86"}},
955 {"x86", "silvermont", "", []string{"x86"}},
Dan Willemsen8a354052016-05-10 14:30:51 -0700956 {"x86", "x86_64", "", []string{"x86"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -0800957 {"x86_64", "", "", []string{"x86_64"}},
958 {"x86_64", "haswell", "", []string{"x86_64"}},
959 {"x86_64", "ivybridge", "", []string{"x86_64"}},
960 {"x86_64", "sandybridge", "", []string{"x86_64"}},
961 {"x86_64", "silvermont", "", []string{"x86_64"}},
962 }
Dan Albert4098deb2016-10-19 14:04:41 -0700963}
Dan Willemsen322acaf2016-01-12 23:07:05 -0800964
Dan Albert4098deb2016-10-19 14:04:41 -0700965func getNdkAbisConfig() []archConfig {
966 return []archConfig{
967 {"arm", "armv5te", "", []string{"armeabi"}},
968 {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
969 {"mips", "mips32-fp", "", []string{"mips"}},
970 {"mips64", "mips64r6", "", []string{"mips64"}},
971 {"x86", "", "", []string{"x86"}},
972 {"x86_64", "", "", []string{"x86_64"}},
973 }
974}
975
976func decodeArchSettings(archConfigs []archConfig) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700977 var ret []Target
Dan Willemsen322acaf2016-01-12 23:07:05 -0800978
Dan Albert4098deb2016-10-19 14:04:41 -0700979 for _, config := range archConfigs {
Dan Willemsen322acaf2016-01-12 23:07:05 -0800980 arch, err := decodeArch(config.arch, &config.archVariant,
981 &config.cpuVariant, &config.abi)
982 if err != nil {
983 return nil, err
984 }
Dan Willemsen17f05262016-05-31 16:27:00 -0700985 arch.Native = false
Colin Crossa1ad8d12016-06-01 17:09:44 -0700986 ret = append(ret, Target{
987 Os: Android,
988 Arch: arch,
989 })
Dan Willemsen322acaf2016-01-12 23:07:05 -0800990 }
991
992 return ret, nil
993}
994
Colin Cross4225f652015-09-17 14:33:42 -0700995// Convert a set of strings from product variables into a single Arch struct
996func decodeArch(arch string, archVariant, cpuVariant *string, abi *[]string) (Arch, error) {
997 stringPtr := func(p *string) string {
998 if p != nil {
999 return *p
1000 }
1001 return ""
1002 }
1003
1004 slicePtr := func(p *[]string) []string {
1005 if p != nil {
1006 return *p
1007 }
1008 return nil
1009 }
1010
Colin Crosseeabb892015-11-20 13:07:51 -08001011 archType, ok := archTypeMap[arch]
1012 if !ok {
1013 return Arch{}, fmt.Errorf("unknown arch %q", arch)
1014 }
Colin Cross4225f652015-09-17 14:33:42 -07001015
Colin Crosseeabb892015-11-20 13:07:51 -08001016 a := Arch{
Colin Cross4225f652015-09-17 14:33:42 -07001017 ArchType: archType,
1018 ArchVariant: stringPtr(archVariant),
1019 CpuVariant: stringPtr(cpuVariant),
1020 Abi: slicePtr(abi),
Dan Willemsen17f05262016-05-31 16:27:00 -07001021 Native: true,
Colin Crosseeabb892015-11-20 13:07:51 -08001022 }
1023
1024 if a.ArchVariant == a.ArchType.Name || a.ArchVariant == "generic" {
1025 a.ArchVariant = ""
1026 }
1027
1028 if a.CpuVariant == a.ArchType.Name || a.CpuVariant == "generic" {
1029 a.CpuVariant = ""
1030 }
1031
1032 for i := 0; i < len(a.Abi); i++ {
1033 if a.Abi[i] == "" {
1034 a.Abi = append(a.Abi[:i], a.Abi[i+1:]...)
1035 i--
1036 }
1037 }
1038
Colin Crossc5c24ad2015-11-20 15:35:00 -08001039 if featureMap, ok := archFeatureMap[archType]; ok {
Dan Willemsenb4850992016-05-06 17:21:20 -07001040 a.ArchFeatures = featureMap[a.ArchVariant]
Colin Crossc5c24ad2015-11-20 15:35:00 -08001041 }
1042
Colin Crosseeabb892015-11-20 13:07:51 -08001043 return a, nil
Colin Cross4225f652015-09-17 14:33:42 -07001044}
1045
Colin Cross69617d32016-09-06 10:39:07 -07001046func filterMultilibTargets(targets []Target, multilib string) []Target {
1047 var ret []Target
1048 for _, t := range targets {
1049 if t.Arch.ArchType.Multilib == multilib {
1050 ret = append(ret, t)
1051 }
1052 }
1053 return ret
1054}
1055
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001056func getCommonTargets(targets []Target) []Target {
1057 var ret []Target
1058 set := make(map[string]bool)
1059
1060 for _, t := range targets {
1061 if _, found := set[t.Os.String()]; !found {
1062 set[t.Os.String()] = true
1063 ret = append(ret, commonTargetMap[t.Os.String()])
1064 }
1065 }
1066
1067 return ret
1068}
1069
Colin Crossa1ad8d12016-06-01 17:09:44 -07001070// Use the module multilib setting to select one or more targets from a target list
Colin Cross8b74d172016-09-13 09:59:14 -07001071func decodeMultilib(multilib string, targets []Target, prefer32 bool) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001072 buildTargets := []Target{}
Colin Cross8b74d172016-09-13 09:59:14 -07001073 if multilib == "first" {
1074 if prefer32 {
1075 multilib = "prefer32"
1076 } else {
1077 multilib = "prefer64"
1078 }
1079 }
Colin Cross4225f652015-09-17 14:33:42 -07001080 switch multilib {
1081 case "common":
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001082 buildTargets = append(buildTargets, getCommonTargets(targets)...)
Colin Cross4225f652015-09-17 14:33:42 -07001083 case "both":
Colin Cross8b74d172016-09-13 09:59:14 -07001084 if prefer32 {
1085 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1086 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1087 } else {
1088 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1089 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1090 }
Colin Cross4225f652015-09-17 14:33:42 -07001091 case "32":
Colin Cross69617d32016-09-06 10:39:07 -07001092 buildTargets = filterMultilibTargets(targets, "lib32")
Colin Cross4225f652015-09-17 14:33:42 -07001093 case "64":
Colin Cross69617d32016-09-06 10:39:07 -07001094 buildTargets = filterMultilibTargets(targets, "lib64")
1095 case "prefer32":
1096 buildTargets = filterMultilibTargets(targets, "lib32")
1097 if len(buildTargets) == 0 {
1098 buildTargets = filterMultilibTargets(targets, "lib64")
Colin Cross4225f652015-09-17 14:33:42 -07001099 }
Colin Cross8b74d172016-09-13 09:59:14 -07001100 case "prefer64":
1101 buildTargets = filterMultilibTargets(targets, "lib64")
1102 if len(buildTargets) == 0 {
1103 buildTargets = filterMultilibTargets(targets, "lib32")
1104 }
Colin Cross4225f652015-09-17 14:33:42 -07001105 default:
Colin Cross69617d32016-09-06 10:39:07 -07001106 return nil, fmt.Errorf(`compile_multilib must be "both", "first", "32", "64", or "prefer32" found %q`,
Colin Cross4225f652015-09-17 14:33:42 -07001107 multilib)
Colin Cross4225f652015-09-17 14:33:42 -07001108 }
1109
Colin Crossa1ad8d12016-06-01 17:09:44 -07001110 return buildTargets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001111}