blob: 0cd491638665b0fe57cdd915bfb4f40c3130634a [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
23 "github.com/google/blueprint"
24 "github.com/google/blueprint/proptools"
Colin Cross3f40fa42015-01-30 17:27:36 -080025)
26
27var (
Dan Willemsenb1957a52016-06-23 23:44:54 -070028 archTypeList []ArchType
29
Colin Crossec193632015-07-06 17:49:43 -070030 Arm = newArch("arm", "lib32")
31 Arm64 = newArch("arm64", "lib64")
32 Mips = newArch("mips", "lib32")
33 Mips64 = newArch("mips64", "lib64")
34 X86 = newArch("x86", "lib32")
35 X86_64 = newArch("x86_64", "lib64")
Colin Cross2fe66872015-03-30 17:20:39 -070036
37 Common = ArchType{
38 Name: "common",
39 }
Colin Cross3f40fa42015-01-30 17:27:36 -080040)
41
Colin Cross4225f652015-09-17 14:33:42 -070042var archTypeMap = map[string]ArchType{
43 "arm": Arm,
44 "arm64": Arm64,
45 "mips": Mips,
Colin Cross3b336c22015-11-23 16:28:31 -080046 "mips64": Mips64,
Colin Cross4225f652015-09-17 14:33:42 -070047 "x86": X86,
48 "x86_64": X86_64,
49}
50
Colin Cross3f40fa42015-01-30 17:27:36 -080051/*
52Example blueprints file containing all variant property groups, with comment listing what type
53of variants get properties in that group:
54
55module {
56 arch: {
57 arm: {
58 // Host or device variants with arm architecture
59 },
60 arm64: {
61 // Host or device variants with arm64 architecture
62 },
63 mips: {
64 // Host or device variants with mips architecture
65 },
66 mips64: {
67 // Host or device variants with mips64 architecture
68 },
69 x86: {
70 // Host or device variants with x86 architecture
71 },
72 x86_64: {
73 // Host or device variants with x86_64 architecture
74 },
75 },
76 multilib: {
77 lib32: {
78 // Host or device variants for 32-bit architectures
79 },
80 lib64: {
81 // Host or device variants for 64-bit architectures
82 },
83 },
84 target: {
85 android: {
86 // Device variants
87 },
88 host: {
89 // Host variants
90 },
91 linux: {
92 // Linux host variants
93 },
94 darwin: {
95 // Darwin host variants
96 },
97 windows: {
98 // Windows host variants
99 },
100 not_windows: {
101 // Non-windows host variants
102 },
103 },
104}
105*/
Colin Cross7d5136f2015-05-11 13:39:40 -0700106
Dan Willemsenb1957a52016-06-23 23:44:54 -0700107var archVariants = map[ArchType][]string{}
108var archFeatures = map[ArchType][]string{}
Colin Crossc5c24ad2015-11-20 15:35:00 -0800109var archFeatureMap = map[ArchType]map[string][]string{}
110
Dan Willemsenb1957a52016-06-23 23:44:54 -0700111func RegisterArchVariants(arch ArchType, variants ...string) {
112 checkCalledFromInit()
113 archVariants[arch] = append(archVariants[arch], variants...)
114}
115
116func RegisterArchFeatures(arch ArchType, features ...string) {
117 checkCalledFromInit()
118 archFeatures[arch] = append(archFeatures[arch], features...)
119}
120
121func RegisterArchVariantFeatures(arch ArchType, variant string, features ...string) {
122 checkCalledFromInit()
123 if variant != "" && !inList(variant, archVariants[arch]) {
124 panic(fmt.Errorf("Invalid variant %q for arch %q", variant, arch))
Colin Crossc5c24ad2015-11-20 15:35:00 -0800125 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700126
Colin Crossc5c24ad2015-11-20 15:35:00 -0800127 for _, feature := range features {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700128 if !inList(feature, archFeatures[arch]) {
Colin Crossc5c24ad2015-11-20 15:35:00 -0800129 panic(fmt.Errorf("Invalid feature %q for arch %q variant %q", feature, arch, variant))
130 }
131 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700132
Colin Crossc5c24ad2015-11-20 15:35:00 -0800133 if archFeatureMap[arch] == nil {
134 archFeatureMap[arch] = make(map[string][]string)
135 }
136 archFeatureMap[arch][variant] = features
137}
138
Colin Cross3f40fa42015-01-30 17:27:36 -0800139// An Arch indicates a single CPU architecture.
140type Arch struct {
Colin Crossc5c24ad2015-11-20 15:35:00 -0800141 ArchType ArchType
142 ArchVariant string
143 CpuVariant string
144 Abi []string
145 ArchFeatures []string
Dan Willemsen17f05262016-05-31 16:27:00 -0700146 Native bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800147}
148
149func (a Arch) String() string {
Colin Crossd3ba0392015-05-07 14:11:29 -0700150 s := a.ArchType.String()
Colin Cross3f40fa42015-01-30 17:27:36 -0800151 if a.ArchVariant != "" {
152 s += "_" + a.ArchVariant
153 }
154 if a.CpuVariant != "" {
155 s += "_" + a.CpuVariant
156 }
157 return s
158}
159
160type ArchType struct {
Colin Crossec193632015-07-06 17:49:43 -0700161 Name string
Dan Willemsenb1957a52016-06-23 23:44:54 -0700162 Field string
Colin Crossec193632015-07-06 17:49:43 -0700163 Multilib string
Colin Cross3f40fa42015-01-30 17:27:36 -0800164}
165
Colin Crossec193632015-07-06 17:49:43 -0700166func newArch(name, multilib string) ArchType {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700167 archType := ArchType{
Colin Crossec193632015-07-06 17:49:43 -0700168 Name: name,
Dan Willemsenb1957a52016-06-23 23:44:54 -0700169 Field: proptools.FieldNameForProperty(name),
Colin Crossec193632015-07-06 17:49:43 -0700170 Multilib: multilib,
Colin Cross3f40fa42015-01-30 17:27:36 -0800171 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700172 archTypeList = append(archTypeList, archType)
173 return archType
Colin Cross3f40fa42015-01-30 17:27:36 -0800174}
175
176func (a ArchType) String() string {
177 return a.Name
178}
179
Colin Crossa1ad8d12016-06-01 17:09:44 -0700180var BuildOs = func() OsType {
Dan Willemsen490fd492015-11-24 17:53:15 -0800181 switch runtime.GOOS {
182 case "linux":
183 return Linux
184 case "darwin":
185 return Darwin
186 default:
187 panic(fmt.Sprintf("unsupported OS: %s", runtime.GOOS))
188 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700189}()
190
191var (
192 osTypeList []OsType
193
Dan Willemsen00fcbde2016-11-17 00:25:59 -0800194 NoOsType OsType
195 Linux = NewOsType("linux", Host, false)
196 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 (
220 Device OsClass = iota
221 Host
222 HostCross
223)
224
225func (os OsType) String() string {
226 return os.Name
Colin Cross54c71122016-06-01 17:09:44 -0700227}
228
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800229func NewOsType(name string, class OsClass, defDisabled bool) OsType {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700230 os := OsType{
231 Name: name,
232 Field: strings.Title(name),
233 Class: class,
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800234
235 DefaultDisabled: defDisabled,
Colin Cross54c71122016-06-01 17:09:44 -0700236 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700237 osTypeList = append(osTypeList, os)
238 return os
239}
240
241func osByName(name string) OsType {
242 for _, os := range osTypeList {
243 if os.Name == name {
244 return os
245 }
246 }
247
248 return NoOsType
Dan Willemsen490fd492015-11-24 17:53:15 -0800249}
250
Colin Cross3f40fa42015-01-30 17:27:36 -0800251var (
Colin Crossa1ad8d12016-06-01 17:09:44 -0700252 commonTarget = Target{
253 Os: Android,
254 Arch: Arch{
255 ArchType: Common,
256 },
Colin Cross2fe66872015-03-30 17:20:39 -0700257 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800258)
259
Colin Crossa1ad8d12016-06-01 17:09:44 -0700260type Target struct {
261 Os OsType
262 Arch Arch
Colin Crossd3ba0392015-05-07 14:11:29 -0700263}
264
Colin Crossa1ad8d12016-06-01 17:09:44 -0700265func (target Target) String() string {
266 return target.Os.String() + "_" + target.Arch.String()
Dan Willemsen490fd492015-11-24 17:53:15 -0800267}
268
Colin Cross1e676be2016-10-12 14:38:15 -0700269func archMutator(mctx BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700270 var module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800271 var ok bool
Colin Cross635c3b02016-05-18 15:37:25 -0700272 if module, ok = mctx.Module().(Module); !ok {
Colin Cross3f40fa42015-01-30 17:27:36 -0800273 return
274 }
275
Dan Willemsen0b24c742016-10-04 15:13:37 -0700276 if !module.base().ArchSpecific() {
Colin Crossb9db4802016-06-03 01:50:47 +0000277 return
278 }
279
Dan Willemsen0b24c742016-10-04 15:13:37 -0700280 osClasses := module.base().OsClassSupported()
281
Colin Crossa1ad8d12016-06-01 17:09:44 -0700282 var moduleTargets []Target
Colin Cross8b74d172016-09-13 09:59:14 -0700283 primaryModules := make(map[int]bool)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700284
285 for _, class := range osClasses {
286 targets := mctx.AConfig().Targets[class]
287 if len(targets) == 0 {
288 continue
289 }
Colin Cross69617d32016-09-06 10:39:07 -0700290 var multilib string
291 switch class {
292 case Device:
293 multilib = module.base().commonProperties.Target.Android.Compile_multilib
294 case Host, HostCross:
295 multilib = module.base().commonProperties.Target.Host.Compile_multilib
296 }
297 if multilib == "" {
298 multilib = module.base().commonProperties.Compile_multilib
299 }
300 if multilib == "" {
301 multilib = module.base().commonProperties.Default_multilib
302 }
Colin Cross628a88c2016-09-13 15:15:51 -0700303 var prefer32 bool
304 switch class {
305 case Device:
Colin Cross8b74d172016-09-13 09:59:14 -0700306 prefer32 = mctx.AConfig().DevicePrefer32BitExecutables()
Colin Cross628a88c2016-09-13 15:15:51 -0700307 case HostCross:
308 // Windows builds always prefer 32-bit
309 prefer32 = true
Colin Cross8b74d172016-09-13 09:59:14 -0700310 }
311 targets, err := decodeMultilib(multilib, targets, prefer32)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700312 if err != nil {
313 mctx.ModuleErrorf("%s", err.Error())
314 }
Colin Cross8b74d172016-09-13 09:59:14 -0700315 if len(targets) > 0 {
316 primaryModules[len(moduleTargets)] = true
317 moduleTargets = append(moduleTargets, targets...)
318 }
Colin Crossb9db4802016-06-03 01:50:47 +0000319 }
320
Dan Willemsen3f32f032016-07-11 14:36:48 -0700321 if len(moduleTargets) == 0 {
322 module.base().commonProperties.Enabled = boolPtr(false)
323 return
324 }
325
Colin Crossa1ad8d12016-06-01 17:09:44 -0700326 targetNames := make([]string, len(moduleTargets))
Colin Crossb9db4802016-06-03 01:50:47 +0000327
Colin Crossa1ad8d12016-06-01 17:09:44 -0700328 for i, target := range moduleTargets {
329 targetNames[i] = target.String()
330 }
331
332 modules := mctx.CreateVariations(targetNames...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800333 for i, m := range modules {
Colin Cross8b74d172016-09-13 09:59:14 -0700334 m.(Module).base().SetTarget(moduleTargets[i], primaryModules[i])
Colin Cross635c3b02016-05-18 15:37:25 -0700335 m.(Module).base().setArchProperties(mctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800336 }
337}
338
Dan Willemsenb1957a52016-06-23 23:44:54 -0700339func filterArchStruct(prop reflect.Type) (reflect.Type, bool) {
340 var fields []reflect.StructField
341
342 ptr := prop.Kind() == reflect.Ptr
343 if ptr {
344 prop = prop.Elem()
345 }
346
347 for i := 0; i < prop.NumField(); i++ {
348 field := prop.Field(i)
349 if !proptools.HasTag(field, "android", "arch_variant") {
350 continue
351 }
352
353 // The arch_variant field isn't necessary past this point
354 // Instead of wasting space, just remove it. Go also has a
355 // 16-bit limit on structure name length. The name is constructed
356 // based on the Go source representation of the structure, so
357 // the tag names count towards that length.
358 //
359 // TODO: handle the uncommon case of other tags being involved
360 if field.Tag == `android:"arch_variant"` {
361 field.Tag = ""
362 }
363
364 // Recurse into structs
365 switch field.Type.Kind() {
366 case reflect.Struct:
367 var ok bool
368 field.Type, ok = filterArchStruct(field.Type)
369 if !ok {
370 continue
371 }
372 case reflect.Ptr:
373 if field.Type.Elem().Kind() == reflect.Struct {
374 nestedType, ok := filterArchStruct(field.Type.Elem())
375 if !ok {
376 continue
377 }
378 field.Type = reflect.PtrTo(nestedType)
379 }
380 case reflect.Interface:
381 panic("Interfaces are not supported in arch_variant properties")
382 }
383
384 fields = append(fields, field)
385 }
386 if len(fields) == 0 {
387 return nil, false
388 }
389
390 ret := reflect.StructOf(fields)
391 if ptr {
392 ret = reflect.PtrTo(ret)
393 }
394 return ret, true
395}
396
397func createArchType(props reflect.Type) reflect.Type {
398 props, ok := filterArchStruct(props)
399 if !ok {
400 return nil
401 }
402
403 variantFields := func(names []string) []reflect.StructField {
404 ret := make([]reflect.StructField, len(names))
405
406 for i, name := range names {
407 ret[i].Name = name
408 ret[i].Type = props
409 }
410
411 return ret
412 }
413
414 archFields := make([]reflect.StructField, len(archTypeList))
415 for i, arch := range archTypeList {
416 variants := []string{}
417
418 for _, archVariant := range archVariants[arch] {
419 variants = append(variants, proptools.FieldNameForProperty(archVariant))
420 }
421 for _, feature := range archFeatures[arch] {
422 variants = append(variants, proptools.FieldNameForProperty(feature))
423 }
424
425 fields := variantFields(variants)
426
427 fields = append([]reflect.StructField{reflect.StructField{
428 Name: "BlueprintEmbed",
429 Type: props,
430 Anonymous: true,
431 }}, fields...)
432
433 archFields[i] = reflect.StructField{
434 Name: arch.Field,
435 Type: reflect.StructOf(fields),
436 }
437 }
438 archType := reflect.StructOf(archFields)
439
440 multilibType := reflect.StructOf(variantFields([]string{"Lib32", "Lib64"}))
441
442 targets := []string{
443 "Host",
444 "Android64",
445 "Android32",
446 "Not_windows",
447 }
448 for _, os := range osTypeList {
449 targets = append(targets, os.Field)
450
451 for _, archType := range osArchTypeMap[os] {
452 targets = append(targets, os.Field+"_"+archType.Name)
453 }
454 }
455
456 targetType := reflect.StructOf(variantFields(targets))
457 return reflect.StructOf([]reflect.StructField{
458 reflect.StructField{
459 Name: "Arch",
460 Type: archType,
461 },
462 reflect.StructField{
463 Name: "Multilib",
464 Type: multilibType,
465 },
466 reflect.StructField{
467 Name: "Target",
468 Type: targetType,
469 },
470 })
471}
472
473var archPropTypeMap OncePer
474
Colin Cross635c3b02016-05-18 15:37:25 -0700475func InitArchModule(m Module,
Colin Cross3f40fa42015-01-30 17:27:36 -0800476 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
477
478 base := m.base()
479
Colin Cross3f40fa42015-01-30 17:27:36 -0800480 base.generalProperties = append(base.generalProperties,
Colin Cross3f40fa42015-01-30 17:27:36 -0800481 propertyStructs...)
482
483 for _, properties := range base.generalProperties {
484 propertiesValue := reflect.ValueOf(properties)
Colin Cross62496a02016-08-08 15:49:17 -0700485 t := propertiesValue.Type()
Colin Cross3f40fa42015-01-30 17:27:36 -0800486 if propertiesValue.Kind() != reflect.Ptr {
Colin Crossca860ac2016-01-04 14:34:37 -0800487 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
488 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800489 }
490
491 propertiesValue = propertiesValue.Elem()
492 if propertiesValue.Kind() != reflect.Struct {
Colin Crossca860ac2016-01-04 14:34:37 -0800493 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
494 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800495 }
496
Dan Willemsenb1957a52016-06-23 23:44:54 -0700497 archPropType := archPropTypeMap.Once(t, func() interface{} {
498 return createArchType(t)
Colin Cross3f40fa42015-01-30 17:27:36 -0800499 })
500
Dan Willemsenb1957a52016-06-23 23:44:54 -0700501 if archPropType != nil {
502 base.archProperties = append(base.archProperties, reflect.New(archPropType.(reflect.Type)).Interface())
503 } else {
504 base.archProperties = append(base.archProperties, nil)
505 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800506 }
507
508 var allProperties []interface{}
509 allProperties = append(allProperties, base.generalProperties...)
510 for _, asp := range base.archProperties {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700511 if asp != nil {
512 allProperties = append(allProperties, asp)
513 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800514 }
515
Colin Crossa120ec12016-08-19 16:07:38 -0700516 base.customizableProperties = allProperties
517
Colin Cross3f40fa42015-01-30 17:27:36 -0800518 return m, allProperties
519}
520
Colin Crossa716add2015-12-16 11:07:39 -0800521var variantReplacer = strings.NewReplacer("-", "_", ".", "_")
Colin Crossec193632015-07-06 17:49:43 -0700522
Colin Cross635c3b02016-05-18 15:37:25 -0700523func (a *ModuleBase) appendProperties(ctx BottomUpMutatorContext,
Dan Willemsenb1957a52016-06-23 23:44:54 -0700524 dst interface{}, src reflect.Value, field, srcPrefix string) reflect.Value {
Colin Cross06a931b2015-10-28 17:23:31 -0700525
Dan Willemsenb1957a52016-06-23 23:44:54 -0700526 src = src.FieldByName(field)
527 if !src.IsValid() {
Colin Crosseeabb892015-11-20 13:07:51 -0800528 ctx.ModuleErrorf("field %q does not exist", srcPrefix)
Dan Willemsenb1957a52016-06-23 23:44:54 -0700529 return src
Colin Cross85a88972015-11-23 13:29:51 -0800530 }
531
Dan Willemsenb1957a52016-06-23 23:44:54 -0700532 ret := src
Colin Cross85a88972015-11-23 13:29:51 -0800533
Dan Willemsenb1957a52016-06-23 23:44:54 -0700534 if src.Kind() == reflect.Struct {
535 src = src.FieldByName("BlueprintEmbed")
Colin Cross06a931b2015-10-28 17:23:31 -0700536 }
537
Colin Cross6ee75b62016-05-05 15:57:15 -0700538 order := func(property string,
539 dstField, srcField reflect.StructField,
540 dstValue, srcValue interface{}) (proptools.Order, error) {
541 if proptools.HasTag(dstField, "android", "variant_prepend") {
542 return proptools.Prepend, nil
543 } else {
544 return proptools.Append, nil
545 }
546 }
547
Dan Willemsenb1957a52016-06-23 23:44:54 -0700548 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, order)
Colin Cross06a931b2015-10-28 17:23:31 -0700549 if err != nil {
550 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
551 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
552 } else {
553 panic(err)
554 }
555 }
Colin Cross85a88972015-11-23 13:29:51 -0800556
Dan Willemsenb1957a52016-06-23 23:44:54 -0700557 return ret
Colin Cross06a931b2015-10-28 17:23:31 -0700558}
559
Colin Cross3f40fa42015-01-30 17:27:36 -0800560// Rewrite the module's properties structs to contain arch-specific values.
Colin Cross635c3b02016-05-18 15:37:25 -0700561func (a *ModuleBase) setArchProperties(ctx BottomUpMutatorContext) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700562 arch := a.Arch()
563 os := a.Os()
Colin Crossd3ba0392015-05-07 14:11:29 -0700564
Colin Cross2fe66872015-03-30 17:20:39 -0700565 if arch.ArchType == Common {
566 return
567 }
568
Colin Cross3f40fa42015-01-30 17:27:36 -0800569 for i := range a.generalProperties {
Colin Cross06a931b2015-10-28 17:23:31 -0700570 genProps := a.generalProperties[i]
Dan Willemsenb1957a52016-06-23 23:44:54 -0700571 if a.archProperties[i] == nil {
572 continue
573 }
574 archProps := reflect.ValueOf(a.archProperties[i]).Elem()
575
576 archProp := archProps.FieldByName("Arch")
577 multilibProp := archProps.FieldByName("Multilib")
578 targetProp := archProps.FieldByName("Target")
579
Colin Cross3f40fa42015-01-30 17:27:36 -0800580 // Handle arch-specific properties in the form:
Colin Crossb05bff22015-04-30 15:08:04 -0700581 // arch: {
582 // arm64: {
Colin Cross3f40fa42015-01-30 17:27:36 -0800583 // key: value,
584 // },
585 // },
586 t := arch.ArchType
Colin Cross06a931b2015-10-28 17:23:31 -0700587
Colin Crossec193632015-07-06 17:49:43 -0700588 field := proptools.FieldNameForProperty(t.Name)
Colin Cross06a931b2015-10-28 17:23:31 -0700589 prefix := "arch." + t.Name
Dan Willemsenb1957a52016-06-23 23:44:54 -0700590 archStruct := a.appendProperties(ctx, genProps, archProp, field, prefix)
Colin Crossec193632015-07-06 17:49:43 -0700591
592 // Handle arch-variant-specific properties in the form:
593 // arch: {
594 // variant: {
595 // key: value,
596 // },
597 // },
Colin Crossa716add2015-12-16 11:07:39 -0800598 v := variantReplacer.Replace(arch.ArchVariant)
Colin Crossec193632015-07-06 17:49:43 -0700599 if v != "" {
600 field := proptools.FieldNameForProperty(v)
Colin Cross85a88972015-11-23 13:29:51 -0800601 prefix := "arch." + t.Name + "." + v
602 a.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossec193632015-07-06 17:49:43 -0700603 }
604
605 // Handle cpu-variant-specific properties in the form:
606 // arch: {
607 // variant: {
608 // key: value,
609 // },
610 // },
Colin Crossa716add2015-12-16 11:07:39 -0800611 c := variantReplacer.Replace(arch.CpuVariant)
Colin Crossec193632015-07-06 17:49:43 -0700612 if c != "" {
613 field := proptools.FieldNameForProperty(c)
Colin Cross85a88972015-11-23 13:29:51 -0800614 prefix := "arch." + t.Name + "." + c
615 a.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossec193632015-07-06 17:49:43 -0700616 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800617
Colin Crossc5c24ad2015-11-20 15:35:00 -0800618 // Handle arch-feature-specific properties in the form:
619 // arch: {
620 // feature: {
621 // key: value,
622 // },
623 // },
624 for _, feature := range arch.ArchFeatures {
625 field := proptools.FieldNameForProperty(feature)
Colin Cross85a88972015-11-23 13:29:51 -0800626 prefix := "arch." + t.Name + "." + feature
627 a.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossc5c24ad2015-11-20 15:35:00 -0800628 }
629
Colin Cross3f40fa42015-01-30 17:27:36 -0800630 // Handle multilib-specific properties in the form:
Colin Crossb05bff22015-04-30 15:08:04 -0700631 // multilib: {
632 // lib32: {
Colin Cross3f40fa42015-01-30 17:27:36 -0800633 // key: value,
634 // },
635 // },
Colin Cross06a931b2015-10-28 17:23:31 -0700636 field = proptools.FieldNameForProperty(t.Multilib)
637 prefix = "multilib." + t.Multilib
Dan Willemsenb1957a52016-06-23 23:44:54 -0700638 a.appendProperties(ctx, genProps, multilibProp, field, prefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800639
Colin Crossa1ad8d12016-06-01 17:09:44 -0700640 // Handle host-specific properties in the form:
Colin Crossb05bff22015-04-30 15:08:04 -0700641 // target: {
642 // host: {
Colin Cross3f40fa42015-01-30 17:27:36 -0800643 // key: value,
644 // },
645 // },
Colin Crossa1ad8d12016-06-01 17:09:44 -0700646 if os.Class == Host || os.Class == HostCross {
647 field = "Host"
648 prefix = "target.host"
Dan Willemsenb1957a52016-06-23 23:44:54 -0700649 a.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700650 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800651
Colin Crossa1ad8d12016-06-01 17:09:44 -0700652 // Handle target OS properties in the form:
Colin Crossb05bff22015-04-30 15:08:04 -0700653 // target: {
654 // linux: {
Colin Cross3f40fa42015-01-30 17:27:36 -0800655 // key: value,
656 // },
Colin Crossb05bff22015-04-30 15:08:04 -0700657 // not_windows: {
658 // key: value,
659 // },
660 // linux_x86: {
661 // key: value,
662 // },
663 // linux_arm: {
Colin Cross3f40fa42015-01-30 17:27:36 -0800664 // key: value,
665 // },
Colin Crossa1ad8d12016-06-01 17:09:44 -0700666 // android {
667 // key: value,
668 // },
669 // android_arm {
670 // key: value,
671 // },
672 // android_x86 {
673 // key: value,
674 // },
Colin Cross3f40fa42015-01-30 17:27:36 -0800675 // },
Colin Crossa1ad8d12016-06-01 17:09:44 -0700676 // },
677 field = os.Field
678 prefix = "target." + os.Name
Dan Willemsenb1957a52016-06-23 23:44:54 -0700679 a.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossb9db4802016-06-03 01:50:47 +0000680
Colin Crossa1ad8d12016-06-01 17:09:44 -0700681 field = os.Field + "_" + t.Name
682 prefix = "target." + os.Name + "_" + t.Name
Dan Willemsenb1957a52016-06-23 23:44:54 -0700683 a.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossb9db4802016-06-03 01:50:47 +0000684
Colin Crossa1ad8d12016-06-01 17:09:44 -0700685 if (os.Class == Host || os.Class == HostCross) && os != Windows {
686 field := "Not_windows"
687 prefix := "target.not_windows"
Dan Willemsenb1957a52016-06-23 23:44:54 -0700688 a.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800689 }
690
Colin Crossf8209412015-03-26 14:44:26 -0700691 // Handle 64-bit device properties in the form:
692 // target {
693 // android64 {
694 // key: value,
695 // },
696 // android32 {
697 // key: value,
698 // },
699 // },
700 // WARNING: this is probably not what you want to use in your blueprints file, it selects
701 // options for all targets on a device that supports 64-bit binaries, not just the targets
702 // that are being compiled for 64-bit. Its expected use case is binaries like linker and
703 // 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 -0700704 if os.Class == Device {
705 if ctx.AConfig().Android64() {
Colin Cross06a931b2015-10-28 17:23:31 -0700706 field := "Android64"
707 prefix := "target.android64"
Dan Willemsenb1957a52016-06-23 23:44:54 -0700708 a.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossf8209412015-03-26 14:44:26 -0700709 } else {
Colin Cross06a931b2015-10-28 17:23:31 -0700710 field := "Android32"
711 prefix := "target.android32"
Dan Willemsenb1957a52016-06-23 23:44:54 -0700712 a.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossf8209412015-03-26 14:44:26 -0700713 }
714 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800715 }
716}
717
718func forEachInterface(v reflect.Value, f func(reflect.Value)) {
719 switch v.Kind() {
720 case reflect.Interface:
721 f(v)
722 case reflect.Struct:
723 for i := 0; i < v.NumField(); i++ {
724 forEachInterface(v.Field(i), f)
725 }
726 case reflect.Ptr:
727 forEachInterface(v.Elem(), f)
728 default:
729 panic(fmt.Errorf("Unsupported kind %s", v.Kind()))
730 }
731}
Colin Cross4225f652015-09-17 14:33:42 -0700732
Colin Crossa1ad8d12016-06-01 17:09:44 -0700733// Convert the arch product variables into a list of targets for each os class structs
Colin Cross9272ade2016-08-17 15:24:12 -0700734func decodeTargetProductVariables(config *config) (map[OsClass][]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700735 variables := config.ProductVariables
Dan Willemsen490fd492015-11-24 17:53:15 -0800736
Colin Crossa1ad8d12016-06-01 17:09:44 -0700737 targets := make(map[OsClass][]Target)
738 var targetErr error
739
740 addTarget := func(os OsType, archName string, archVariant, cpuVariant *string, abi *[]string) {
741 if targetErr != nil {
742 return
Dan Willemsen490fd492015-11-24 17:53:15 -0800743 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700744
745 arch, err := decodeArch(archName, archVariant, cpuVariant, abi)
746 if err != nil {
747 targetErr = err
748 return
749 }
750
751 targets[os.Class] = append(targets[os.Class],
752 Target{
753 Os: os,
754 Arch: arch,
755 })
Dan Willemsen490fd492015-11-24 17:53:15 -0800756 }
757
Colin Cross4225f652015-09-17 14:33:42 -0700758 if variables.HostArch == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700759 return nil, fmt.Errorf("No host primary architecture set")
Colin Cross4225f652015-09-17 14:33:42 -0700760 }
761
Colin Crossa1ad8d12016-06-01 17:09:44 -0700762 addTarget(BuildOs, *variables.HostArch, nil, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -0700763
Colin Crosseeabb892015-11-20 13:07:51 -0800764 if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700765 addTarget(BuildOs, *variables.HostSecondaryArch, nil, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -0800766 }
767
768 if variables.CrossHost != nil && *variables.CrossHost != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700769 crossHostOs := osByName(*variables.CrossHost)
770 if crossHostOs == NoOsType {
771 return nil, fmt.Errorf("Unknown cross host OS %q", *variables.CrossHost)
772 }
773
Dan Willemsen490fd492015-11-24 17:53:15 -0800774 if variables.CrossHostArch == nil || *variables.CrossHostArch == "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700775 return nil, fmt.Errorf("No cross-host primary architecture set")
Dan Willemsen490fd492015-11-24 17:53:15 -0800776 }
777
Colin Crossa1ad8d12016-06-01 17:09:44 -0700778 addTarget(crossHostOs, *variables.CrossHostArch, nil, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -0800779
780 if variables.CrossHostSecondaryArch != nil && *variables.CrossHostSecondaryArch != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700781 addTarget(crossHostOs, *variables.CrossHostSecondaryArch, nil, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -0800782 }
783 }
784
Dan Willemsen3f32f032016-07-11 14:36:48 -0700785 if variables.DeviceArch != nil && *variables.DeviceArch != "" {
786 addTarget(Android, *variables.DeviceArch, variables.DeviceArchVariant,
787 variables.DeviceCpuVariant, variables.DeviceAbi)
Colin Cross4225f652015-09-17 14:33:42 -0700788
Dan Willemsen3f32f032016-07-11 14:36:48 -0700789 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" {
790 addTarget(Android, *variables.DeviceSecondaryArch,
791 variables.DeviceSecondaryArchVariant, variables.DeviceSecondaryCpuVariant,
792 variables.DeviceSecondaryAbi)
Colin Cross4225f652015-09-17 14:33:42 -0700793
Dan Willemsen3f32f032016-07-11 14:36:48 -0700794 deviceArches := targets[Device]
795 if deviceArches[0].Arch.ArchType.Multilib == deviceArches[1].Arch.ArchType.Multilib {
796 deviceArches[1].Arch.Native = false
797 }
Colin Cross4225f652015-09-17 14:33:42 -0700798 }
Colin Cross4225f652015-09-17 14:33:42 -0700799 }
800
Colin Crossa1ad8d12016-06-01 17:09:44 -0700801 if targetErr != nil {
802 return nil, targetErr
803 }
804
805 return targets, nil
Colin Cross4225f652015-09-17 14:33:42 -0700806}
807
Dan Albert4098deb2016-10-19 14:04:41 -0700808type archConfig struct {
809 arch string
810 archVariant string
811 cpuVariant string
812 abi []string
813}
814
815func getMegaDeviceConfig() []archConfig {
816 return []archConfig{
Dan Willemsen110a89d2016-01-14 15:17:19 -0800817 // armv5 is only used for unbundled apps
818 //{"arm", "armv5te", "", []string{"armeabi"}},
819 {"arm", "armv7-a", "generic", []string{"armeabi-v7a"}},
820 {"arm", "armv7-a-neon", "generic", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -0800821 {"arm", "armv7-a-neon", "cortex-a7", []string{"armeabi-v7a"}},
822 {"arm", "armv7-a-neon", "cortex-a8", []string{"armeabi-v7a"}},
Dan Willemsen110a89d2016-01-14 15:17:19 -0800823 {"arm", "armv7-a-neon", "cortex-a9", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -0800824 {"arm", "armv7-a-neon", "cortex-a15", []string{"armeabi-v7a"}},
825 {"arm", "armv7-a-neon", "cortex-a53", []string{"armeabi-v7a"}},
826 {"arm", "armv7-a-neon", "cortex-a53.a57", []string{"armeabi-v7a"}},
827 {"arm", "armv7-a-neon", "denver", []string{"armeabi-v7a"}},
828 {"arm", "armv7-a-neon", "krait", []string{"armeabi-v7a"}},
Dan Willemsen110a89d2016-01-14 15:17:19 -0800829 {"arm64", "armv8-a", "cortex-a53", []string{"arm64-v8a"}},
830 {"arm64", "armv8-a", "denver64", []string{"arm64-v8a"}},
Dan Willemsen468cc312016-01-13 23:25:19 -0800831 {"mips", "mips32-fp", "", []string{"mips"}},
832 {"mips", "mips32r2-fp", "", []string{"mips"}},
833 {"mips", "mips32r2-fp-xburst", "", []string{"mips"}},
Dan Willemsen65fb9812016-07-19 21:37:28 -0700834 //{"mips", "mips32r6", "", []string{"mips"}},
Dan Willemsen468cc312016-01-13 23:25:19 -0800835 // mips32r2dsp[r2]-fp fails in the assembler for divdf3.c in compiler-rt:
836 // (same errors in make and soong)
837 // Error: invalid operands `mtlo $ac0,$11'
838 // Error: invalid operands `mthi $ac0,$12'
Dan Willemsen322acaf2016-01-12 23:07:05 -0800839 //{"mips", "mips32r2dsp-fp", "", []string{"mips"}},
840 //{"mips", "mips32r2dspr2-fp", "", []string{"mips"}},
841 // mips64r2 is mismatching 64r2 and 64r6 libraries during linking to libgcc
842 //{"mips64", "mips64r2", "", []string{"mips64"}},
843 {"mips64", "mips64r6", "", []string{"mips64"}},
844 {"x86", "", "", []string{"x86"}},
845 {"x86", "atom", "", []string{"x86"}},
846 {"x86", "haswell", "", []string{"x86"}},
847 {"x86", "ivybridge", "", []string{"x86"}},
848 {"x86", "sandybridge", "", []string{"x86"}},
849 {"x86", "silvermont", "", []string{"x86"}},
Dan Willemsen8a354052016-05-10 14:30:51 -0700850 {"x86", "x86_64", "", []string{"x86"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -0800851 {"x86_64", "", "", []string{"x86_64"}},
852 {"x86_64", "haswell", "", []string{"x86_64"}},
853 {"x86_64", "ivybridge", "", []string{"x86_64"}},
854 {"x86_64", "sandybridge", "", []string{"x86_64"}},
855 {"x86_64", "silvermont", "", []string{"x86_64"}},
856 }
Dan Albert4098deb2016-10-19 14:04:41 -0700857}
Dan Willemsen322acaf2016-01-12 23:07:05 -0800858
Dan Albert4098deb2016-10-19 14:04:41 -0700859func getNdkAbisConfig() []archConfig {
860 return []archConfig{
861 {"arm", "armv5te", "", []string{"armeabi"}},
862 {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
863 {"mips", "mips32-fp", "", []string{"mips"}},
864 {"mips64", "mips64r6", "", []string{"mips64"}},
865 {"x86", "", "", []string{"x86"}},
866 {"x86_64", "", "", []string{"x86_64"}},
867 }
868}
869
870func decodeArchSettings(archConfigs []archConfig) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700871 var ret []Target
Dan Willemsen322acaf2016-01-12 23:07:05 -0800872
Dan Albert4098deb2016-10-19 14:04:41 -0700873 for _, config := range archConfigs {
Dan Willemsen322acaf2016-01-12 23:07:05 -0800874 arch, err := decodeArch(config.arch, &config.archVariant,
875 &config.cpuVariant, &config.abi)
876 if err != nil {
877 return nil, err
878 }
Dan Willemsen17f05262016-05-31 16:27:00 -0700879 arch.Native = false
Colin Crossa1ad8d12016-06-01 17:09:44 -0700880 ret = append(ret, Target{
881 Os: Android,
882 Arch: arch,
883 })
Dan Willemsen322acaf2016-01-12 23:07:05 -0800884 }
885
886 return ret, nil
887}
888
Colin Cross4225f652015-09-17 14:33:42 -0700889// Convert a set of strings from product variables into a single Arch struct
890func decodeArch(arch string, archVariant, cpuVariant *string, abi *[]string) (Arch, error) {
891 stringPtr := func(p *string) string {
892 if p != nil {
893 return *p
894 }
895 return ""
896 }
897
898 slicePtr := func(p *[]string) []string {
899 if p != nil {
900 return *p
901 }
902 return nil
903 }
904
Colin Crosseeabb892015-11-20 13:07:51 -0800905 archType, ok := archTypeMap[arch]
906 if !ok {
907 return Arch{}, fmt.Errorf("unknown arch %q", arch)
908 }
Colin Cross4225f652015-09-17 14:33:42 -0700909
Colin Crosseeabb892015-11-20 13:07:51 -0800910 a := Arch{
Colin Cross4225f652015-09-17 14:33:42 -0700911 ArchType: archType,
912 ArchVariant: stringPtr(archVariant),
913 CpuVariant: stringPtr(cpuVariant),
914 Abi: slicePtr(abi),
Dan Willemsen17f05262016-05-31 16:27:00 -0700915 Native: true,
Colin Crosseeabb892015-11-20 13:07:51 -0800916 }
917
918 if a.ArchVariant == a.ArchType.Name || a.ArchVariant == "generic" {
919 a.ArchVariant = ""
920 }
921
922 if a.CpuVariant == a.ArchType.Name || a.CpuVariant == "generic" {
923 a.CpuVariant = ""
924 }
925
926 for i := 0; i < len(a.Abi); i++ {
927 if a.Abi[i] == "" {
928 a.Abi = append(a.Abi[:i], a.Abi[i+1:]...)
929 i--
930 }
931 }
932
Colin Crossc5c24ad2015-11-20 15:35:00 -0800933 if featureMap, ok := archFeatureMap[archType]; ok {
Dan Willemsenb4850992016-05-06 17:21:20 -0700934 a.ArchFeatures = featureMap[a.ArchVariant]
Colin Crossc5c24ad2015-11-20 15:35:00 -0800935 }
936
Colin Crosseeabb892015-11-20 13:07:51 -0800937 return a, nil
Colin Cross4225f652015-09-17 14:33:42 -0700938}
939
Colin Cross69617d32016-09-06 10:39:07 -0700940func filterMultilibTargets(targets []Target, multilib string) []Target {
941 var ret []Target
942 for _, t := range targets {
943 if t.Arch.ArchType.Multilib == multilib {
944 ret = append(ret, t)
945 }
946 }
947 return ret
948}
949
Colin Crossa1ad8d12016-06-01 17:09:44 -0700950// Use the module multilib setting to select one or more targets from a target list
Colin Cross8b74d172016-09-13 09:59:14 -0700951func decodeMultilib(multilib string, targets []Target, prefer32 bool) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700952 buildTargets := []Target{}
Colin Cross8b74d172016-09-13 09:59:14 -0700953 if multilib == "first" {
954 if prefer32 {
955 multilib = "prefer32"
956 } else {
957 multilib = "prefer64"
958 }
959 }
Colin Cross4225f652015-09-17 14:33:42 -0700960 switch multilib {
961 case "common":
Colin Crossa1ad8d12016-06-01 17:09:44 -0700962 buildTargets = append(buildTargets, commonTarget)
Colin Cross4225f652015-09-17 14:33:42 -0700963 case "both":
Colin Cross8b74d172016-09-13 09:59:14 -0700964 if prefer32 {
965 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
966 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
967 } else {
968 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
969 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
970 }
Colin Cross4225f652015-09-17 14:33:42 -0700971 case "32":
Colin Cross69617d32016-09-06 10:39:07 -0700972 buildTargets = filterMultilibTargets(targets, "lib32")
Colin Cross4225f652015-09-17 14:33:42 -0700973 case "64":
Colin Cross69617d32016-09-06 10:39:07 -0700974 buildTargets = filterMultilibTargets(targets, "lib64")
975 case "prefer32":
976 buildTargets = filterMultilibTargets(targets, "lib32")
977 if len(buildTargets) == 0 {
978 buildTargets = filterMultilibTargets(targets, "lib64")
Colin Cross4225f652015-09-17 14:33:42 -0700979 }
Colin Cross8b74d172016-09-13 09:59:14 -0700980 case "prefer64":
981 buildTargets = filterMultilibTargets(targets, "lib64")
982 if len(buildTargets) == 0 {
983 buildTargets = filterMultilibTargets(targets, "lib32")
984 }
Colin Cross4225f652015-09-17 14:33:42 -0700985 default:
Colin Cross69617d32016-09-06 10:39:07 -0700986 return nil, fmt.Errorf(`compile_multilib must be "both", "first", "32", "64", or "prefer32" found %q`,
Colin Cross4225f652015-09-17 14:33:42 -0700987 multilib)
Colin Cross4225f652015-09-17 14:33:42 -0700988 }
989
Colin Crossa1ad8d12016-06-01 17:09:44 -0700990 return buildTargets, nil
Colin Cross4225f652015-09-17 14:33:42 -0700991}