blob: af3919cfa01306fea0a32c5e4b1823d54a5017b0 [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
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 {
Colin Cross6510f912017-11-29 00:27:14 -0800308 targets := mctx.Config().Targets[class]
Colin Crossa1ad8d12016-06-01 17:09:44 -0700309 if len(targets) == 0 {
310 continue
311 }
Colin Cross69617d32016-09-06 10:39:07 -0700312 var multilib string
313 switch class {
314 case Device:
Colin Cross7d716ba2017-11-01 10:38:29 -0700315 multilib = String(module.base().commonProperties.Target.Android.Compile_multilib)
Colin Cross69617d32016-09-06 10:39:07 -0700316 case Host, HostCross:
Colin Cross7d716ba2017-11-01 10:38:29 -0700317 multilib = String(module.base().commonProperties.Target.Host.Compile_multilib)
Colin Cross69617d32016-09-06 10:39:07 -0700318 }
319 if multilib == "" {
Colin Cross7d716ba2017-11-01 10:38:29 -0700320 multilib = String(module.base().commonProperties.Compile_multilib)
Colin Cross69617d32016-09-06 10:39:07 -0700321 }
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 Cross6510f912017-11-29 00:27:14 -0800328 prefer32 = mctx.Config().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",
Dan Willemsen86fc1eb2017-10-02 20:32:35 -0700471 "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
Dan Willemsen86fc1eb2017-10-02 20:32:35 -0700482 if os.Linux() {
Dan Willemsen866b5632017-09-22 12:28:24 -0700483 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 Cross3f40fa42015-01-30 17:27:36 -0800600 for i := range a.generalProperties {
Colin Cross06a931b2015-10-28 17:23:31 -0700601 genProps := a.generalProperties[i]
Dan Willemsenb1957a52016-06-23 23:44:54 -0700602 if a.archProperties[i] == nil {
603 continue
604 }
605 archProps := reflect.ValueOf(a.archProperties[i]).Elem()
606
607 archProp := archProps.FieldByName("Arch")
608 multilibProp := archProps.FieldByName("Multilib")
609 targetProp := archProps.FieldByName("Target")
610
Colin Crossd5934c82017-10-02 13:55:26 -0700611 var field string
612 var prefix string
613
Colin Cross3f40fa42015-01-30 17:27:36 -0800614 // Handle arch-specific properties in the form:
Colin Crossb05bff22015-04-30 15:08:04 -0700615 // arch: {
616 // arm64: {
Colin Cross3f40fa42015-01-30 17:27:36 -0800617 // key: value,
618 // },
619 // },
620 t := arch.ArchType
Colin Cross06a931b2015-10-28 17:23:31 -0700621
Colin Crossd5934c82017-10-02 13:55:26 -0700622 if arch.ArchType != Common {
623 field := proptools.FieldNameForProperty(t.Name)
624 prefix := "arch." + t.Name
625 archStruct := a.appendProperties(ctx, genProps, archProp, field, prefix)
Colin Crossec193632015-07-06 17:49:43 -0700626
Colin Crossd5934c82017-10-02 13:55:26 -0700627 // Handle arch-variant-specific properties in the form:
628 // arch: {
629 // variant: {
630 // key: value,
631 // },
632 // },
633 v := variantReplacer.Replace(arch.ArchVariant)
634 if v != "" {
635 field := proptools.FieldNameForProperty(v)
636 prefix := "arch." + t.Name + "." + v
Colin Cross08016332016-12-20 09:53:14 -0800637 a.appendProperties(ctx, genProps, archStruct, field, prefix)
638 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800639
Colin Crossd5934c82017-10-02 13:55:26 -0700640 // Handle cpu-variant-specific properties in the form:
641 // arch: {
642 // variant: {
643 // key: value,
644 // },
645 // },
646 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 }
653 }
Colin Crossc5c24ad2015-11-20 15:35:00 -0800654
Colin Crossd5934c82017-10-02 13:55:26 -0700655 // 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)
663 prefix := "arch." + t.Name + "." + feature
664 a.appendProperties(ctx, genProps, archStruct, field, prefix)
665 }
666
667 // Handle multilib-specific properties in the form:
668 // multilib: {
669 // lib32: {
670 // key: value,
671 // },
672 // },
673 field = proptools.FieldNameForProperty(t.Multilib)
674 prefix = "multilib." + t.Multilib
675 a.appendProperties(ctx, genProps, multilibProp, field, prefix)
676 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800677
Colin Crossa1ad8d12016-06-01 17:09:44 -0700678 // Handle host-specific properties in the form:
Colin Crossb05bff22015-04-30 15:08:04 -0700679 // target: {
680 // host: {
Colin Cross3f40fa42015-01-30 17:27:36 -0800681 // key: value,
682 // },
683 // },
Colin Crossa1ad8d12016-06-01 17:09:44 -0700684 if os.Class == Host || os.Class == HostCross {
685 field = "Host"
686 prefix = "target.host"
Dan Willemsenb1957a52016-06-23 23:44:54 -0700687 a.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700688 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800689
Dan Willemsen866b5632017-09-22 12:28:24 -0700690 // Handle target OS generalities of the form:
691 // target: {
692 // bionic: {
693 // key: value,
694 // },
695 // bionic_x86: {
696 // key: value,
697 // },
698 // }
Dan Willemsen86fc1eb2017-10-02 20:32:35 -0700699 if os.Linux() {
Dan Willemsen866b5632017-09-22 12:28:24 -0700700 field = "Linux"
701 prefix = "target.linux"
702 a.appendProperties(ctx, genProps, targetProp, field, prefix)
703
Colin Crossd5934c82017-10-02 13:55:26 -0700704 if arch.ArchType != Common {
705 field = "Linux_" + arch.ArchType.Name
706 prefix = "target.linux_" + arch.ArchType.Name
707 a.appendProperties(ctx, genProps, targetProp, field, prefix)
708 }
Dan Willemsen866b5632017-09-22 12:28:24 -0700709 }
710
711 if os.Bionic() {
712 field = "Bionic"
713 prefix = "target.bionic"
714 a.appendProperties(ctx, genProps, targetProp, field, prefix)
715
Colin Crossd5934c82017-10-02 13:55:26 -0700716 if arch.ArchType != Common {
717 field = "Bionic_" + t.Name
718 prefix = "target.bionic_" + t.Name
719 a.appendProperties(ctx, genProps, targetProp, field, prefix)
720 }
Dan Willemsen866b5632017-09-22 12:28:24 -0700721 }
722
Colin Crossa1ad8d12016-06-01 17:09:44 -0700723 // Handle target OS properties in the form:
Colin Crossb05bff22015-04-30 15:08:04 -0700724 // target: {
Dan Willemsen866b5632017-09-22 12:28:24 -0700725 // linux_glibc: {
Colin Cross3f40fa42015-01-30 17:27:36 -0800726 // key: value,
727 // },
Colin Crossb05bff22015-04-30 15:08:04 -0700728 // not_windows: {
729 // key: value,
730 // },
Dan Willemsen866b5632017-09-22 12:28:24 -0700731 // linux_glibc_x86: {
Colin Crossb05bff22015-04-30 15:08:04 -0700732 // key: value,
733 // },
Dan Willemsen866b5632017-09-22 12:28:24 -0700734 // linux_glibc_arm: {
Colin Cross3f40fa42015-01-30 17:27:36 -0800735 // key: value,
736 // },
Colin Crossa1ad8d12016-06-01 17:09:44 -0700737 // android {
738 // key: value,
739 // },
740 // android_arm {
741 // key: value,
742 // },
743 // android_x86 {
744 // key: value,
745 // },
Colin Cross3f40fa42015-01-30 17:27:36 -0800746 // },
Colin Crossa1ad8d12016-06-01 17:09:44 -0700747 field = os.Field
748 prefix = "target." + os.Name
Dan Willemsenb1957a52016-06-23 23:44:54 -0700749 a.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossb9db4802016-06-03 01:50:47 +0000750
Colin Crossd5934c82017-10-02 13:55:26 -0700751 if arch.ArchType != Common {
752 field = os.Field + "_" + t.Name
753 prefix = "target." + os.Name + "_" + t.Name
754 a.appendProperties(ctx, genProps, targetProp, field, prefix)
755 }
Colin Crossb9db4802016-06-03 01:50:47 +0000756
Colin Crossa1ad8d12016-06-01 17:09:44 -0700757 if (os.Class == Host || os.Class == HostCross) && os != Windows {
758 field := "Not_windows"
759 prefix := "target.not_windows"
Dan Willemsenb1957a52016-06-23 23:44:54 -0700760 a.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800761 }
762
Colin Crossf8209412015-03-26 14:44:26 -0700763 // Handle 64-bit device properties in the form:
764 // target {
765 // android64 {
766 // key: value,
767 // },
768 // android32 {
769 // key: value,
770 // },
771 // },
772 // WARNING: this is probably not what you want to use in your blueprints file, it selects
773 // options for all targets on a device that supports 64-bit binaries, not just the targets
774 // that are being compiled for 64-bit. Its expected use case is binaries like linker and
775 // 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 -0700776 if os.Class == Device {
Colin Cross6510f912017-11-29 00:27:14 -0800777 if ctx.Config().Android64() {
Colin Cross06a931b2015-10-28 17:23:31 -0700778 field := "Android64"
779 prefix := "target.android64"
Dan Willemsenb1957a52016-06-23 23:44:54 -0700780 a.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossf8209412015-03-26 14:44:26 -0700781 } else {
Colin Cross06a931b2015-10-28 17:23:31 -0700782 field := "Android32"
783 prefix := "target.android32"
Dan Willemsenb1957a52016-06-23 23:44:54 -0700784 a.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossf8209412015-03-26 14:44:26 -0700785 }
Colin Crossbb2e2b72016-12-08 17:23:53 -0800786
Colin Cross4247f0d2017-04-13 16:56:14 -0700787 if arch.ArchType == X86 && (hasArmAbi(arch) ||
Colin Cross6510f912017-11-29 00:27:14 -0800788 hasArmAndroidArch(ctx.Config().Targets[Device])) {
Colin Cross4247f0d2017-04-13 16:56:14 -0700789 field := "Arm_on_x86"
790 prefix := "target.arm_on_x86"
791 a.appendProperties(ctx, genProps, targetProp, field, prefix)
792 }
793 if arch.ArchType == X86_64 && (hasArmAbi(arch) ||
Colin Cross6510f912017-11-29 00:27:14 -0800794 hasArmAndroidArch(ctx.Config().Targets[Device])) {
Colin Cross4247f0d2017-04-13 16:56:14 -0700795 field := "Arm_on_x86_64"
796 prefix := "target.arm_on_x86_64"
797 a.appendProperties(ctx, genProps, targetProp, field, prefix)
798 }
Colin Crossbb2e2b72016-12-08 17:23:53 -0800799 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800800 }
801}
802
803func forEachInterface(v reflect.Value, f func(reflect.Value)) {
804 switch v.Kind() {
805 case reflect.Interface:
806 f(v)
807 case reflect.Struct:
808 for i := 0; i < v.NumField(); i++ {
809 forEachInterface(v.Field(i), f)
810 }
811 case reflect.Ptr:
812 forEachInterface(v.Elem(), f)
813 default:
814 panic(fmt.Errorf("Unsupported kind %s", v.Kind()))
815 }
816}
Colin Cross4225f652015-09-17 14:33:42 -0700817
Colin Crossa1ad8d12016-06-01 17:09:44 -0700818// Convert the arch product variables into a list of targets for each os class structs
Colin Cross9272ade2016-08-17 15:24:12 -0700819func decodeTargetProductVariables(config *config) (map[OsClass][]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700820 variables := config.ProductVariables
Dan Willemsen490fd492015-11-24 17:53:15 -0800821
Colin Crossa1ad8d12016-06-01 17:09:44 -0700822 targets := make(map[OsClass][]Target)
823 var targetErr error
824
825 addTarget := func(os OsType, archName string, archVariant, cpuVariant *string, abi *[]string) {
826 if targetErr != nil {
827 return
Dan Willemsen490fd492015-11-24 17:53:15 -0800828 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700829
830 arch, err := decodeArch(archName, archVariant, cpuVariant, abi)
831 if err != nil {
832 targetErr = err
833 return
834 }
835
836 targets[os.Class] = append(targets[os.Class],
837 Target{
838 Os: os,
839 Arch: arch,
840 })
Dan Willemsen490fd492015-11-24 17:53:15 -0800841 }
842
Colin Cross4225f652015-09-17 14:33:42 -0700843 if variables.HostArch == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700844 return nil, fmt.Errorf("No host primary architecture set")
Colin Cross4225f652015-09-17 14:33:42 -0700845 }
846
Colin Crossa1ad8d12016-06-01 17:09:44 -0700847 addTarget(BuildOs, *variables.HostArch, nil, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -0700848
Colin Crosseeabb892015-11-20 13:07:51 -0800849 if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700850 addTarget(BuildOs, *variables.HostSecondaryArch, nil, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -0800851 }
852
Dan Willemsen01a405a2016-06-13 17:19:03 -0700853 if config.Host_bionic != nil && *config.Host_bionic {
854 addTarget(LinuxBionic, "x86_64", nil, nil, nil)
855 }
856
Dan Willemsen490fd492015-11-24 17:53:15 -0800857 if variables.CrossHost != nil && *variables.CrossHost != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700858 crossHostOs := osByName(*variables.CrossHost)
859 if crossHostOs == NoOsType {
860 return nil, fmt.Errorf("Unknown cross host OS %q", *variables.CrossHost)
861 }
862
Dan Willemsen490fd492015-11-24 17:53:15 -0800863 if variables.CrossHostArch == nil || *variables.CrossHostArch == "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700864 return nil, fmt.Errorf("No cross-host primary architecture set")
Dan Willemsen490fd492015-11-24 17:53:15 -0800865 }
866
Colin Crossa1ad8d12016-06-01 17:09:44 -0700867 addTarget(crossHostOs, *variables.CrossHostArch, nil, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -0800868
869 if variables.CrossHostSecondaryArch != nil && *variables.CrossHostSecondaryArch != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700870 addTarget(crossHostOs, *variables.CrossHostSecondaryArch, nil, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -0800871 }
872 }
873
Dan Willemsen3f32f032016-07-11 14:36:48 -0700874 if variables.DeviceArch != nil && *variables.DeviceArch != "" {
875 addTarget(Android, *variables.DeviceArch, variables.DeviceArchVariant,
876 variables.DeviceCpuVariant, variables.DeviceAbi)
Colin Cross4225f652015-09-17 14:33:42 -0700877
Dan Willemsen3f32f032016-07-11 14:36:48 -0700878 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" {
879 addTarget(Android, *variables.DeviceSecondaryArch,
880 variables.DeviceSecondaryArchVariant, variables.DeviceSecondaryCpuVariant,
881 variables.DeviceSecondaryAbi)
Colin Cross4225f652015-09-17 14:33:42 -0700882
Dan Willemsen3f32f032016-07-11 14:36:48 -0700883 deviceArches := targets[Device]
884 if deviceArches[0].Arch.ArchType.Multilib == deviceArches[1].Arch.ArchType.Multilib {
885 deviceArches[1].Arch.Native = false
886 }
Colin Cross4225f652015-09-17 14:33:42 -0700887 }
Colin Cross4225f652015-09-17 14:33:42 -0700888 }
889
Colin Crossa1ad8d12016-06-01 17:09:44 -0700890 if targetErr != nil {
891 return nil, targetErr
892 }
893
894 return targets, nil
Colin Cross4225f652015-09-17 14:33:42 -0700895}
896
Colin Crossbb2e2b72016-12-08 17:23:53 -0800897// hasArmAbi returns true if arch has at least one arm ABI
898func hasArmAbi(arch Arch) bool {
899 for _, abi := range arch.Abi {
900 if strings.HasPrefix(abi, "arm") {
901 return true
902 }
903 }
904 return false
905}
906
Colin Cross4247f0d2017-04-13 16:56:14 -0700907// hasArmArch returns true if targets has at least arm Android arch
908func hasArmAndroidArch(targets []Target) bool {
909 for _, target := range targets {
910 if target.Os == Android && target.Arch.ArchType == Arm {
911 return true
912 }
913 }
914 return false
915}
916
Dan Albert4098deb2016-10-19 14:04:41 -0700917type archConfig struct {
918 arch string
919 archVariant string
920 cpuVariant string
921 abi []string
922}
923
924func getMegaDeviceConfig() []archConfig {
925 return []archConfig{
Dan Willemsen110a89d2016-01-14 15:17:19 -0800926 // armv5 is only used for unbundled apps
927 //{"arm", "armv5te", "", []string{"armeabi"}},
928 {"arm", "armv7-a", "generic", []string{"armeabi-v7a"}},
929 {"arm", "armv7-a-neon", "generic", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -0800930 {"arm", "armv7-a-neon", "cortex-a7", []string{"armeabi-v7a"}},
931 {"arm", "armv7-a-neon", "cortex-a8", []string{"armeabi-v7a"}},
Dan Willemsen110a89d2016-01-14 15:17:19 -0800932 {"arm", "armv7-a-neon", "cortex-a9", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -0800933 {"arm", "armv7-a-neon", "cortex-a15", []string{"armeabi-v7a"}},
934 {"arm", "armv7-a-neon", "cortex-a53", []string{"armeabi-v7a"}},
935 {"arm", "armv7-a-neon", "cortex-a53.a57", []string{"armeabi-v7a"}},
Jake Weinstein6600a442017-05-15 18:27:12 -0400936 {"arm", "armv7-a-neon", "cortex-a73", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -0800937 {"arm", "armv7-a-neon", "denver", []string{"armeabi-v7a"}},
938 {"arm", "armv7-a-neon", "krait", []string{"armeabi-v7a"}},
Alex Naidisae4fc182016-08-20 00:14:56 +0200939 {"arm", "armv7-a-neon", "kryo", []string{"armeabi-v7a"}},
Junmo Park8ea49592017-07-24 07:14:55 +0900940 {"arm", "armv7-a-neon", "exynos-m1", []string{"armeabi-v7a"}},
Junmo Parkd86c9022017-07-21 09:07:47 +0900941 {"arm", "armv7-a-neon", "exynos-m2", []string{"armeabi-v7a"}},
Dan Willemsen110a89d2016-01-14 15:17:19 -0800942 {"arm64", "armv8-a", "cortex-a53", []string{"arm64-v8a"}},
Jake Weinstein6600a442017-05-15 18:27:12 -0400943 {"arm64", "armv8-a", "cortex-a73", []string{"arm64-v8a"}},
Dan Willemsen110a89d2016-01-14 15:17:19 -0800944 {"arm64", "armv8-a", "denver64", []string{"arm64-v8a"}},
Alex Naidisac01ff52016-08-30 15:56:33 +0200945 {"arm64", "armv8-a", "kryo", []string{"arm64-v8a"}},
Junmo Park8ea49592017-07-24 07:14:55 +0900946 {"arm64", "armv8-a", "exynos-m1", []string{"arm64-v8a"}},
Junmo Parkd86c9022017-07-21 09:07:47 +0900947 {"arm64", "armv8-a", "exynos-m2", []string{"arm64-v8a"}},
Dan Willemsen468cc312016-01-13 23:25:19 -0800948 {"mips", "mips32-fp", "", []string{"mips"}},
949 {"mips", "mips32r2-fp", "", []string{"mips"}},
950 {"mips", "mips32r2-fp-xburst", "", []string{"mips"}},
Dan Willemsen65fb9812016-07-19 21:37:28 -0700951 //{"mips", "mips32r6", "", []string{"mips"}},
Colin Cross1837b802017-04-26 19:10:34 -0700952 {"mips", "mips32r2dsp-fp", "", []string{"mips"}},
953 {"mips", "mips32r2dspr2-fp", "", []string{"mips"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -0800954 // mips64r2 is mismatching 64r2 and 64r6 libraries during linking to libgcc
955 //{"mips64", "mips64r2", "", []string{"mips64"}},
956 {"mips64", "mips64r6", "", []string{"mips64"}},
957 {"x86", "", "", []string{"x86"}},
958 {"x86", "atom", "", []string{"x86"}},
959 {"x86", "haswell", "", []string{"x86"}},
960 {"x86", "ivybridge", "", []string{"x86"}},
961 {"x86", "sandybridge", "", []string{"x86"}},
962 {"x86", "silvermont", "", []string{"x86"}},
Dan Willemsen8a354052016-05-10 14:30:51 -0700963 {"x86", "x86_64", "", []string{"x86"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -0800964 {"x86_64", "", "", []string{"x86_64"}},
965 {"x86_64", "haswell", "", []string{"x86_64"}},
966 {"x86_64", "ivybridge", "", []string{"x86_64"}},
967 {"x86_64", "sandybridge", "", []string{"x86_64"}},
968 {"x86_64", "silvermont", "", []string{"x86_64"}},
969 }
Dan Albert4098deb2016-10-19 14:04:41 -0700970}
Dan Willemsen322acaf2016-01-12 23:07:05 -0800971
Dan Albert4098deb2016-10-19 14:04:41 -0700972func getNdkAbisConfig() []archConfig {
973 return []archConfig{
974 {"arm", "armv5te", "", []string{"armeabi"}},
975 {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
Dan Albert4098deb2016-10-19 14:04:41 -0700976 {"x86", "", "", []string{"x86"}},
977 {"x86_64", "", "", []string{"x86_64"}},
978 }
979}
980
981func decodeArchSettings(archConfigs []archConfig) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700982 var ret []Target
Dan Willemsen322acaf2016-01-12 23:07:05 -0800983
Dan Albert4098deb2016-10-19 14:04:41 -0700984 for _, config := range archConfigs {
Dan Willemsen322acaf2016-01-12 23:07:05 -0800985 arch, err := decodeArch(config.arch, &config.archVariant,
986 &config.cpuVariant, &config.abi)
987 if err != nil {
988 return nil, err
989 }
Dan Willemsen17f05262016-05-31 16:27:00 -0700990 arch.Native = false
Colin Crossa1ad8d12016-06-01 17:09:44 -0700991 ret = append(ret, Target{
992 Os: Android,
993 Arch: arch,
994 })
Dan Willemsen322acaf2016-01-12 23:07:05 -0800995 }
996
997 return ret, nil
998}
999
Colin Cross4225f652015-09-17 14:33:42 -07001000// Convert a set of strings from product variables into a single Arch struct
1001func decodeArch(arch string, archVariant, cpuVariant *string, abi *[]string) (Arch, error) {
1002 stringPtr := func(p *string) string {
1003 if p != nil {
1004 return *p
1005 }
1006 return ""
1007 }
1008
1009 slicePtr := func(p *[]string) []string {
1010 if p != nil {
1011 return *p
1012 }
1013 return nil
1014 }
1015
Colin Crosseeabb892015-11-20 13:07:51 -08001016 archType, ok := archTypeMap[arch]
1017 if !ok {
1018 return Arch{}, fmt.Errorf("unknown arch %q", arch)
1019 }
Colin Cross4225f652015-09-17 14:33:42 -07001020
Colin Crosseeabb892015-11-20 13:07:51 -08001021 a := Arch{
Colin Cross4225f652015-09-17 14:33:42 -07001022 ArchType: archType,
1023 ArchVariant: stringPtr(archVariant),
1024 CpuVariant: stringPtr(cpuVariant),
1025 Abi: slicePtr(abi),
Dan Willemsen17f05262016-05-31 16:27:00 -07001026 Native: true,
Colin Crosseeabb892015-11-20 13:07:51 -08001027 }
1028
1029 if a.ArchVariant == a.ArchType.Name || a.ArchVariant == "generic" {
1030 a.ArchVariant = ""
1031 }
1032
1033 if a.CpuVariant == a.ArchType.Name || a.CpuVariant == "generic" {
1034 a.CpuVariant = ""
1035 }
1036
1037 for i := 0; i < len(a.Abi); i++ {
1038 if a.Abi[i] == "" {
1039 a.Abi = append(a.Abi[:i], a.Abi[i+1:]...)
1040 i--
1041 }
1042 }
1043
Colin Crossc5c24ad2015-11-20 15:35:00 -08001044 if featureMap, ok := archFeatureMap[archType]; ok {
Dan Willemsenb4850992016-05-06 17:21:20 -07001045 a.ArchFeatures = featureMap[a.ArchVariant]
Colin Crossc5c24ad2015-11-20 15:35:00 -08001046 }
1047
Colin Crosseeabb892015-11-20 13:07:51 -08001048 return a, nil
Colin Cross4225f652015-09-17 14:33:42 -07001049}
1050
Colin Cross69617d32016-09-06 10:39:07 -07001051func filterMultilibTargets(targets []Target, multilib string) []Target {
1052 var ret []Target
1053 for _, t := range targets {
1054 if t.Arch.ArchType.Multilib == multilib {
1055 ret = append(ret, t)
1056 }
1057 }
1058 return ret
1059}
1060
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001061func getCommonTargets(targets []Target) []Target {
1062 var ret []Target
1063 set := make(map[string]bool)
1064
1065 for _, t := range targets {
1066 if _, found := set[t.Os.String()]; !found {
1067 set[t.Os.String()] = true
1068 ret = append(ret, commonTargetMap[t.Os.String()])
1069 }
1070 }
1071
1072 return ret
1073}
1074
Colin Cross6b4a32d2017-12-05 13:42:45 -08001075func preferTargets(targets []Target, filters ...string) []Target {
1076 for _, filter := range filters {
1077 buildTargets := filterMultilibTargets(targets, filter)
1078 if len(buildTargets) > 0 {
1079 return buildTargets
1080 }
1081 }
1082 return nil
1083}
1084
Colin Crossa1ad8d12016-06-01 17:09:44 -07001085// Use the module multilib setting to select one or more targets from a target list
Colin Cross8b74d172016-09-13 09:59:14 -07001086func decodeMultilib(multilib string, targets []Target, prefer32 bool) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001087 buildTargets := []Target{}
Colin Cross6b4a32d2017-12-05 13:42:45 -08001088
Colin Cross4225f652015-09-17 14:33:42 -07001089 switch multilib {
1090 case "common":
Colin Cross6b4a32d2017-12-05 13:42:45 -08001091 buildTargets = getCommonTargets(targets)
1092 case "common_first":
1093 buildTargets = getCommonTargets(targets)
1094 if prefer32 {
1095 buildTargets = append(buildTargets, preferTargets(targets, "lib32", "lib64")...)
1096 } else {
1097 buildTargets = append(buildTargets, preferTargets(targets, "lib64", "lib32")...)
1098 }
Colin Cross4225f652015-09-17 14:33:42 -07001099 case "both":
Colin Cross8b74d172016-09-13 09:59:14 -07001100 if prefer32 {
1101 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1102 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1103 } else {
1104 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1105 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1106 }
Colin Cross4225f652015-09-17 14:33:42 -07001107 case "32":
Colin Cross69617d32016-09-06 10:39:07 -07001108 buildTargets = filterMultilibTargets(targets, "lib32")
Colin Cross4225f652015-09-17 14:33:42 -07001109 case "64":
Colin Cross69617d32016-09-06 10:39:07 -07001110 buildTargets = filterMultilibTargets(targets, "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001111 case "first":
1112 if prefer32 {
1113 buildTargets = preferTargets(targets, "lib32", "lib64")
1114 } else {
1115 buildTargets = preferTargets(targets, "lib64", "lib32")
1116 }
Colin Cross69617d32016-09-06 10:39:07 -07001117 case "prefer32":
Colin Cross6b4a32d2017-12-05 13:42:45 -08001118 buildTargets = preferTargets(targets, "lib32", "lib64")
Colin Cross4225f652015-09-17 14:33:42 -07001119 default:
Colin Cross69617d32016-09-06 10:39:07 -07001120 return nil, fmt.Errorf(`compile_multilib must be "both", "first", "32", "64", or "prefer32" found %q`,
Colin Cross4225f652015-09-17 14:33:42 -07001121 multilib)
Colin Cross4225f652015-09-17 14:33:42 -07001122 }
1123
Colin Crossa1ad8d12016-06-01 17:09:44 -07001124 return buildTargets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001125}