blob: b653ce6bc30b015ed8b5b872c347a57364073617 [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
194 NoOsType OsType
195 Linux = NewOsType("linux", Host)
196 Darwin = NewOsType("darwin", Host)
197 Windows = NewOsType("windows", HostCross)
198 Android = NewOsType("android", Device)
Dan Willemsenb1957a52016-06-23 23:44:54 -0700199
200 osArchTypeMap = map[OsType][]ArchType{
201 Linux: []ArchType{X86, X86_64},
202 Darwin: []ArchType{X86, X86_64},
203 Windows: []ArchType{X86, X86_64},
204 Android: []ArchType{Arm, Arm64, Mips, Mips64, X86, X86_64},
205 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700206)
207
208type OsType struct {
209 Name, Field string
210 Class OsClass
Dan Willemsen490fd492015-11-24 17:53:15 -0800211}
212
Colin Crossa1ad8d12016-06-01 17:09:44 -0700213type OsClass int
214
215const (
216 Device OsClass = iota
217 Host
218 HostCross
219)
220
221func (os OsType) String() string {
222 return os.Name
Colin Cross54c71122016-06-01 17:09:44 -0700223}
224
Colin Crossa1ad8d12016-06-01 17:09:44 -0700225func NewOsType(name string, class OsClass) OsType {
226 os := OsType{
227 Name: name,
228 Field: strings.Title(name),
229 Class: class,
Colin Cross54c71122016-06-01 17:09:44 -0700230 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700231 osTypeList = append(osTypeList, os)
232 return os
233}
234
235func osByName(name string) OsType {
236 for _, os := range osTypeList {
237 if os.Name == name {
238 return os
239 }
240 }
241
242 return NoOsType
Dan Willemsen490fd492015-11-24 17:53:15 -0800243}
244
Colin Cross3f40fa42015-01-30 17:27:36 -0800245var (
Colin Crossa1ad8d12016-06-01 17:09:44 -0700246 commonTarget = Target{
247 Os: Android,
248 Arch: Arch{
249 ArchType: Common,
250 },
Colin Cross2fe66872015-03-30 17:20:39 -0700251 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800252)
253
Colin Crossa1ad8d12016-06-01 17:09:44 -0700254type Target struct {
255 Os OsType
256 Arch Arch
Colin Crossd3ba0392015-05-07 14:11:29 -0700257}
258
Colin Crossa1ad8d12016-06-01 17:09:44 -0700259func (target Target) String() string {
260 return target.Os.String() + "_" + target.Arch.String()
Dan Willemsen490fd492015-11-24 17:53:15 -0800261}
262
Colin Cross635c3b02016-05-18 15:37:25 -0700263func ArchMutator(mctx BottomUpMutatorContext) {
264 var module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800265 var ok bool
Colin Cross635c3b02016-05-18 15:37:25 -0700266 if module, ok = mctx.Module().(Module); !ok {
Colin Cross3f40fa42015-01-30 17:27:36 -0800267 return
268 }
269
Colin Crossa1ad8d12016-06-01 17:09:44 -0700270 osClasses := module.base().OsClassSupported()
Colin Cross3f40fa42015-01-30 17:27:36 -0800271
Colin Crossa1ad8d12016-06-01 17:09:44 -0700272 if len(osClasses) == 0 {
Colin Crossb9db4802016-06-03 01:50:47 +0000273 return
274 }
275
Colin Crossa1ad8d12016-06-01 17:09:44 -0700276 var moduleTargets []Target
Colin Cross8b74d172016-09-13 09:59:14 -0700277 primaryModules := make(map[int]bool)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700278
279 for _, class := range osClasses {
280 targets := mctx.AConfig().Targets[class]
281 if len(targets) == 0 {
282 continue
283 }
Colin Cross69617d32016-09-06 10:39:07 -0700284 var multilib string
285 switch class {
286 case Device:
287 multilib = module.base().commonProperties.Target.Android.Compile_multilib
288 case Host, HostCross:
289 multilib = module.base().commonProperties.Target.Host.Compile_multilib
290 }
291 if multilib == "" {
292 multilib = module.base().commonProperties.Compile_multilib
293 }
294 if multilib == "" {
295 multilib = module.base().commonProperties.Default_multilib
296 }
Colin Cross8b74d172016-09-13 09:59:14 -0700297 prefer32 := false
298 if class == Device {
299 prefer32 = mctx.AConfig().DevicePrefer32BitExecutables()
300 }
301 targets, err := decodeMultilib(multilib, targets, prefer32)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700302 if err != nil {
303 mctx.ModuleErrorf("%s", err.Error())
304 }
Colin Cross8b74d172016-09-13 09:59:14 -0700305 if len(targets) > 0 {
306 primaryModules[len(moduleTargets)] = true
307 moduleTargets = append(moduleTargets, targets...)
308 }
Colin Crossb9db4802016-06-03 01:50:47 +0000309 }
310
Dan Willemsen3f32f032016-07-11 14:36:48 -0700311 if len(moduleTargets) == 0 {
312 module.base().commonProperties.Enabled = boolPtr(false)
313 return
314 }
315
Colin Crossa1ad8d12016-06-01 17:09:44 -0700316 targetNames := make([]string, len(moduleTargets))
Colin Crossb9db4802016-06-03 01:50:47 +0000317
Colin Crossa1ad8d12016-06-01 17:09:44 -0700318 for i, target := range moduleTargets {
319 targetNames[i] = target.String()
320 }
321
322 modules := mctx.CreateVariations(targetNames...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800323 for i, m := range modules {
Colin Cross8b74d172016-09-13 09:59:14 -0700324 m.(Module).base().SetTarget(moduleTargets[i], primaryModules[i])
Colin Cross635c3b02016-05-18 15:37:25 -0700325 m.(Module).base().setArchProperties(mctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800326 }
327}
328
Dan Willemsenb1957a52016-06-23 23:44:54 -0700329func filterArchStruct(prop reflect.Type) (reflect.Type, bool) {
330 var fields []reflect.StructField
331
332 ptr := prop.Kind() == reflect.Ptr
333 if ptr {
334 prop = prop.Elem()
335 }
336
337 for i := 0; i < prop.NumField(); i++ {
338 field := prop.Field(i)
339 if !proptools.HasTag(field, "android", "arch_variant") {
340 continue
341 }
342
343 // The arch_variant field isn't necessary past this point
344 // Instead of wasting space, just remove it. Go also has a
345 // 16-bit limit on structure name length. The name is constructed
346 // based on the Go source representation of the structure, so
347 // the tag names count towards that length.
348 //
349 // TODO: handle the uncommon case of other tags being involved
350 if field.Tag == `android:"arch_variant"` {
351 field.Tag = ""
352 }
353
354 // Recurse into structs
355 switch field.Type.Kind() {
356 case reflect.Struct:
357 var ok bool
358 field.Type, ok = filterArchStruct(field.Type)
359 if !ok {
360 continue
361 }
362 case reflect.Ptr:
363 if field.Type.Elem().Kind() == reflect.Struct {
364 nestedType, ok := filterArchStruct(field.Type.Elem())
365 if !ok {
366 continue
367 }
368 field.Type = reflect.PtrTo(nestedType)
369 }
370 case reflect.Interface:
371 panic("Interfaces are not supported in arch_variant properties")
372 }
373
374 fields = append(fields, field)
375 }
376 if len(fields) == 0 {
377 return nil, false
378 }
379
380 ret := reflect.StructOf(fields)
381 if ptr {
382 ret = reflect.PtrTo(ret)
383 }
384 return ret, true
385}
386
387func createArchType(props reflect.Type) reflect.Type {
388 props, ok := filterArchStruct(props)
389 if !ok {
390 return nil
391 }
392
393 variantFields := func(names []string) []reflect.StructField {
394 ret := make([]reflect.StructField, len(names))
395
396 for i, name := range names {
397 ret[i].Name = name
398 ret[i].Type = props
399 }
400
401 return ret
402 }
403
404 archFields := make([]reflect.StructField, len(archTypeList))
405 for i, arch := range archTypeList {
406 variants := []string{}
407
408 for _, archVariant := range archVariants[arch] {
409 variants = append(variants, proptools.FieldNameForProperty(archVariant))
410 }
411 for _, feature := range archFeatures[arch] {
412 variants = append(variants, proptools.FieldNameForProperty(feature))
413 }
414
415 fields := variantFields(variants)
416
417 fields = append([]reflect.StructField{reflect.StructField{
418 Name: "BlueprintEmbed",
419 Type: props,
420 Anonymous: true,
421 }}, fields...)
422
423 archFields[i] = reflect.StructField{
424 Name: arch.Field,
425 Type: reflect.StructOf(fields),
426 }
427 }
428 archType := reflect.StructOf(archFields)
429
430 multilibType := reflect.StructOf(variantFields([]string{"Lib32", "Lib64"}))
431
432 targets := []string{
433 "Host",
434 "Android64",
435 "Android32",
436 "Not_windows",
437 }
438 for _, os := range osTypeList {
439 targets = append(targets, os.Field)
440
441 for _, archType := range osArchTypeMap[os] {
442 targets = append(targets, os.Field+"_"+archType.Name)
443 }
444 }
445
446 targetType := reflect.StructOf(variantFields(targets))
447 return reflect.StructOf([]reflect.StructField{
448 reflect.StructField{
449 Name: "Arch",
450 Type: archType,
451 },
452 reflect.StructField{
453 Name: "Multilib",
454 Type: multilibType,
455 },
456 reflect.StructField{
457 Name: "Target",
458 Type: targetType,
459 },
460 })
461}
462
463var archPropTypeMap OncePer
464
Colin Cross635c3b02016-05-18 15:37:25 -0700465func InitArchModule(m Module,
Colin Cross3f40fa42015-01-30 17:27:36 -0800466 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
467
468 base := m.base()
469
Colin Cross3f40fa42015-01-30 17:27:36 -0800470 base.generalProperties = append(base.generalProperties,
Colin Cross3f40fa42015-01-30 17:27:36 -0800471 propertyStructs...)
472
473 for _, properties := range base.generalProperties {
474 propertiesValue := reflect.ValueOf(properties)
Colin Cross62496a02016-08-08 15:49:17 -0700475 t := propertiesValue.Type()
Colin Cross3f40fa42015-01-30 17:27:36 -0800476 if propertiesValue.Kind() != reflect.Ptr {
Colin Crossca860ac2016-01-04 14:34:37 -0800477 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
478 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800479 }
480
481 propertiesValue = propertiesValue.Elem()
482 if propertiesValue.Kind() != reflect.Struct {
Colin Crossca860ac2016-01-04 14:34:37 -0800483 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
484 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800485 }
486
Dan Willemsenb1957a52016-06-23 23:44:54 -0700487 archPropType := archPropTypeMap.Once(t, func() interface{} {
488 return createArchType(t)
Colin Cross3f40fa42015-01-30 17:27:36 -0800489 })
490
Dan Willemsenb1957a52016-06-23 23:44:54 -0700491 if archPropType != nil {
492 base.archProperties = append(base.archProperties, reflect.New(archPropType.(reflect.Type)).Interface())
493 } else {
494 base.archProperties = append(base.archProperties, nil)
495 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800496 }
497
498 var allProperties []interface{}
499 allProperties = append(allProperties, base.generalProperties...)
500 for _, asp := range base.archProperties {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700501 if asp != nil {
502 allProperties = append(allProperties, asp)
503 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800504 }
505
Colin Crossa120ec12016-08-19 16:07:38 -0700506 base.customizableProperties = allProperties
507
Colin Cross3f40fa42015-01-30 17:27:36 -0800508 return m, allProperties
509}
510
Colin Crossa716add2015-12-16 11:07:39 -0800511var variantReplacer = strings.NewReplacer("-", "_", ".", "_")
Colin Crossec193632015-07-06 17:49:43 -0700512
Colin Cross635c3b02016-05-18 15:37:25 -0700513func (a *ModuleBase) appendProperties(ctx BottomUpMutatorContext,
Dan Willemsenb1957a52016-06-23 23:44:54 -0700514 dst interface{}, src reflect.Value, field, srcPrefix string) reflect.Value {
Colin Cross06a931b2015-10-28 17:23:31 -0700515
Dan Willemsenb1957a52016-06-23 23:44:54 -0700516 src = src.FieldByName(field)
517 if !src.IsValid() {
Colin Crosseeabb892015-11-20 13:07:51 -0800518 ctx.ModuleErrorf("field %q does not exist", srcPrefix)
Dan Willemsenb1957a52016-06-23 23:44:54 -0700519 return src
Colin Cross85a88972015-11-23 13:29:51 -0800520 }
521
Dan Willemsenb1957a52016-06-23 23:44:54 -0700522 ret := src
Colin Cross85a88972015-11-23 13:29:51 -0800523
Dan Willemsenb1957a52016-06-23 23:44:54 -0700524 if src.Kind() == reflect.Struct {
525 src = src.FieldByName("BlueprintEmbed")
Colin Cross06a931b2015-10-28 17:23:31 -0700526 }
527
Colin Cross6ee75b62016-05-05 15:57:15 -0700528 order := func(property string,
529 dstField, srcField reflect.StructField,
530 dstValue, srcValue interface{}) (proptools.Order, error) {
531 if proptools.HasTag(dstField, "android", "variant_prepend") {
532 return proptools.Prepend, nil
533 } else {
534 return proptools.Append, nil
535 }
536 }
537
Dan Willemsenb1957a52016-06-23 23:44:54 -0700538 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, order)
Colin Cross06a931b2015-10-28 17:23:31 -0700539 if err != nil {
540 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
541 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
542 } else {
543 panic(err)
544 }
545 }
Colin Cross85a88972015-11-23 13:29:51 -0800546
Dan Willemsenb1957a52016-06-23 23:44:54 -0700547 return ret
Colin Cross06a931b2015-10-28 17:23:31 -0700548}
549
Colin Cross3f40fa42015-01-30 17:27:36 -0800550// Rewrite the module's properties structs to contain arch-specific values.
Colin Cross635c3b02016-05-18 15:37:25 -0700551func (a *ModuleBase) setArchProperties(ctx BottomUpMutatorContext) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700552 arch := a.Arch()
553 os := a.Os()
Colin Crossd3ba0392015-05-07 14:11:29 -0700554
Colin Cross2fe66872015-03-30 17:20:39 -0700555 if arch.ArchType == Common {
556 return
557 }
558
Colin Cross3f40fa42015-01-30 17:27:36 -0800559 for i := range a.generalProperties {
Colin Cross06a931b2015-10-28 17:23:31 -0700560 genProps := a.generalProperties[i]
Dan Willemsenb1957a52016-06-23 23:44:54 -0700561 if a.archProperties[i] == nil {
562 continue
563 }
564 archProps := reflect.ValueOf(a.archProperties[i]).Elem()
565
566 archProp := archProps.FieldByName("Arch")
567 multilibProp := archProps.FieldByName("Multilib")
568 targetProp := archProps.FieldByName("Target")
569
Colin Cross3f40fa42015-01-30 17:27:36 -0800570 // Handle arch-specific properties in the form:
Colin Crossb05bff22015-04-30 15:08:04 -0700571 // arch: {
572 // arm64: {
Colin Cross3f40fa42015-01-30 17:27:36 -0800573 // key: value,
574 // },
575 // },
576 t := arch.ArchType
Colin Cross06a931b2015-10-28 17:23:31 -0700577
Colin Crossec193632015-07-06 17:49:43 -0700578 field := proptools.FieldNameForProperty(t.Name)
Colin Cross06a931b2015-10-28 17:23:31 -0700579 prefix := "arch." + t.Name
Dan Willemsenb1957a52016-06-23 23:44:54 -0700580 archStruct := a.appendProperties(ctx, genProps, archProp, field, prefix)
Colin Crossec193632015-07-06 17:49:43 -0700581
582 // Handle arch-variant-specific properties in the form:
583 // arch: {
584 // variant: {
585 // key: value,
586 // },
587 // },
Colin Crossa716add2015-12-16 11:07:39 -0800588 v := variantReplacer.Replace(arch.ArchVariant)
Colin Crossec193632015-07-06 17:49:43 -0700589 if v != "" {
590 field := proptools.FieldNameForProperty(v)
Colin Cross85a88972015-11-23 13:29:51 -0800591 prefix := "arch." + t.Name + "." + v
592 a.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossec193632015-07-06 17:49:43 -0700593 }
594
595 // Handle cpu-variant-specific properties in the form:
596 // arch: {
597 // variant: {
598 // key: value,
599 // },
600 // },
Colin Crossa716add2015-12-16 11:07:39 -0800601 c := variantReplacer.Replace(arch.CpuVariant)
Colin Crossec193632015-07-06 17:49:43 -0700602 if c != "" {
603 field := proptools.FieldNameForProperty(c)
Colin Cross85a88972015-11-23 13:29:51 -0800604 prefix := "arch." + t.Name + "." + c
605 a.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossec193632015-07-06 17:49:43 -0700606 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800607
Colin Crossc5c24ad2015-11-20 15:35:00 -0800608 // Handle arch-feature-specific properties in the form:
609 // arch: {
610 // feature: {
611 // key: value,
612 // },
613 // },
614 for _, feature := range arch.ArchFeatures {
615 field := proptools.FieldNameForProperty(feature)
Colin Cross85a88972015-11-23 13:29:51 -0800616 prefix := "arch." + t.Name + "." + feature
617 a.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossc5c24ad2015-11-20 15:35:00 -0800618 }
619
Colin Cross3f40fa42015-01-30 17:27:36 -0800620 // Handle multilib-specific properties in the form:
Colin Crossb05bff22015-04-30 15:08:04 -0700621 // multilib: {
622 // lib32: {
Colin Cross3f40fa42015-01-30 17:27:36 -0800623 // key: value,
624 // },
625 // },
Colin Cross06a931b2015-10-28 17:23:31 -0700626 field = proptools.FieldNameForProperty(t.Multilib)
627 prefix = "multilib." + t.Multilib
Dan Willemsenb1957a52016-06-23 23:44:54 -0700628 a.appendProperties(ctx, genProps, multilibProp, field, prefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800629
Colin Crossa1ad8d12016-06-01 17:09:44 -0700630 // Handle host-specific properties in the form:
Colin Crossb05bff22015-04-30 15:08:04 -0700631 // target: {
632 // host: {
Colin Cross3f40fa42015-01-30 17:27:36 -0800633 // key: value,
634 // },
635 // },
Colin Crossa1ad8d12016-06-01 17:09:44 -0700636 if os.Class == Host || os.Class == HostCross {
637 field = "Host"
638 prefix = "target.host"
Dan Willemsenb1957a52016-06-23 23:44:54 -0700639 a.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700640 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800641
Colin Crossa1ad8d12016-06-01 17:09:44 -0700642 // Handle target OS properties in the form:
Colin Crossb05bff22015-04-30 15:08:04 -0700643 // target: {
644 // linux: {
Colin Cross3f40fa42015-01-30 17:27:36 -0800645 // key: value,
646 // },
Colin Crossb05bff22015-04-30 15:08:04 -0700647 // not_windows: {
648 // key: value,
649 // },
650 // linux_x86: {
651 // key: value,
652 // },
653 // linux_arm: {
Colin Cross3f40fa42015-01-30 17:27:36 -0800654 // key: value,
655 // },
Colin Crossa1ad8d12016-06-01 17:09:44 -0700656 // android {
657 // key: value,
658 // },
659 // android_arm {
660 // key: value,
661 // },
662 // android_x86 {
663 // key: value,
664 // },
Colin Cross3f40fa42015-01-30 17:27:36 -0800665 // },
Colin Crossa1ad8d12016-06-01 17:09:44 -0700666 // },
667 field = os.Field
668 prefix = "target." + os.Name
Dan Willemsenb1957a52016-06-23 23:44:54 -0700669 a.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossb9db4802016-06-03 01:50:47 +0000670
Colin Crossa1ad8d12016-06-01 17:09:44 -0700671 field = os.Field + "_" + t.Name
672 prefix = "target." + os.Name + "_" + t.Name
Dan Willemsenb1957a52016-06-23 23:44:54 -0700673 a.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossb9db4802016-06-03 01:50:47 +0000674
Colin Crossa1ad8d12016-06-01 17:09:44 -0700675 if (os.Class == Host || os.Class == HostCross) && os != Windows {
676 field := "Not_windows"
677 prefix := "target.not_windows"
Dan Willemsenb1957a52016-06-23 23:44:54 -0700678 a.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800679 }
680
Colin Crossf8209412015-03-26 14:44:26 -0700681 // Handle 64-bit device properties in the form:
682 // target {
683 // android64 {
684 // key: value,
685 // },
686 // android32 {
687 // key: value,
688 // },
689 // },
690 // WARNING: this is probably not what you want to use in your blueprints file, it selects
691 // options for all targets on a device that supports 64-bit binaries, not just the targets
692 // that are being compiled for 64-bit. Its expected use case is binaries like linker and
693 // 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 -0700694 if os.Class == Device {
695 if ctx.AConfig().Android64() {
Colin Cross06a931b2015-10-28 17:23:31 -0700696 field := "Android64"
697 prefix := "target.android64"
Dan Willemsenb1957a52016-06-23 23:44:54 -0700698 a.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossf8209412015-03-26 14:44:26 -0700699 } else {
Colin Cross06a931b2015-10-28 17:23:31 -0700700 field := "Android32"
701 prefix := "target.android32"
Dan Willemsenb1957a52016-06-23 23:44:54 -0700702 a.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossf8209412015-03-26 14:44:26 -0700703 }
704 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800705 }
706}
707
708func forEachInterface(v reflect.Value, f func(reflect.Value)) {
709 switch v.Kind() {
710 case reflect.Interface:
711 f(v)
712 case reflect.Struct:
713 for i := 0; i < v.NumField(); i++ {
714 forEachInterface(v.Field(i), f)
715 }
716 case reflect.Ptr:
717 forEachInterface(v.Elem(), f)
718 default:
719 panic(fmt.Errorf("Unsupported kind %s", v.Kind()))
720 }
721}
Colin Cross4225f652015-09-17 14:33:42 -0700722
Colin Crossa1ad8d12016-06-01 17:09:44 -0700723// Convert the arch product variables into a list of targets for each os class structs
Colin Cross9272ade2016-08-17 15:24:12 -0700724func decodeTargetProductVariables(config *config) (map[OsClass][]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700725 variables := config.ProductVariables
Dan Willemsen490fd492015-11-24 17:53:15 -0800726
Colin Crossa1ad8d12016-06-01 17:09:44 -0700727 targets := make(map[OsClass][]Target)
728 var targetErr error
729
730 addTarget := func(os OsType, archName string, archVariant, cpuVariant *string, abi *[]string) {
731 if targetErr != nil {
732 return
Dan Willemsen490fd492015-11-24 17:53:15 -0800733 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700734
735 arch, err := decodeArch(archName, archVariant, cpuVariant, abi)
736 if err != nil {
737 targetErr = err
738 return
739 }
740
741 targets[os.Class] = append(targets[os.Class],
742 Target{
743 Os: os,
744 Arch: arch,
745 })
Dan Willemsen490fd492015-11-24 17:53:15 -0800746 }
747
Colin Cross4225f652015-09-17 14:33:42 -0700748 if variables.HostArch == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700749 return nil, fmt.Errorf("No host primary architecture set")
Colin Cross4225f652015-09-17 14:33:42 -0700750 }
751
Colin Crossa1ad8d12016-06-01 17:09:44 -0700752 addTarget(BuildOs, *variables.HostArch, nil, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -0700753
Colin Crosseeabb892015-11-20 13:07:51 -0800754 if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700755 addTarget(BuildOs, *variables.HostSecondaryArch, nil, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -0800756 }
757
758 if variables.CrossHost != nil && *variables.CrossHost != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700759 crossHostOs := osByName(*variables.CrossHost)
760 if crossHostOs == NoOsType {
761 return nil, fmt.Errorf("Unknown cross host OS %q", *variables.CrossHost)
762 }
763
Dan Willemsen490fd492015-11-24 17:53:15 -0800764 if variables.CrossHostArch == nil || *variables.CrossHostArch == "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700765 return nil, fmt.Errorf("No cross-host primary architecture set")
Dan Willemsen490fd492015-11-24 17:53:15 -0800766 }
767
Colin Crossa1ad8d12016-06-01 17:09:44 -0700768 addTarget(crossHostOs, *variables.CrossHostArch, nil, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -0800769
770 if variables.CrossHostSecondaryArch != nil && *variables.CrossHostSecondaryArch != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700771 addTarget(crossHostOs, *variables.CrossHostSecondaryArch, nil, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -0800772 }
773 }
774
Dan Willemsen3f32f032016-07-11 14:36:48 -0700775 if variables.DeviceArch != nil && *variables.DeviceArch != "" {
776 addTarget(Android, *variables.DeviceArch, variables.DeviceArchVariant,
777 variables.DeviceCpuVariant, variables.DeviceAbi)
Colin Cross4225f652015-09-17 14:33:42 -0700778
Dan Willemsen3f32f032016-07-11 14:36:48 -0700779 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" {
780 addTarget(Android, *variables.DeviceSecondaryArch,
781 variables.DeviceSecondaryArchVariant, variables.DeviceSecondaryCpuVariant,
782 variables.DeviceSecondaryAbi)
Colin Cross4225f652015-09-17 14:33:42 -0700783
Dan Willemsen3f32f032016-07-11 14:36:48 -0700784 deviceArches := targets[Device]
785 if deviceArches[0].Arch.ArchType.Multilib == deviceArches[1].Arch.ArchType.Multilib {
786 deviceArches[1].Arch.Native = false
787 }
Colin Cross4225f652015-09-17 14:33:42 -0700788 }
Colin Cross4225f652015-09-17 14:33:42 -0700789 }
790
Colin Crossa1ad8d12016-06-01 17:09:44 -0700791 if targetErr != nil {
792 return nil, targetErr
793 }
794
795 return targets, nil
Colin Cross4225f652015-09-17 14:33:42 -0700796}
797
Colin Crossa1ad8d12016-06-01 17:09:44 -0700798func decodeMegaDevice() ([]Target, error) {
Dan Willemsen322acaf2016-01-12 23:07:05 -0800799 archSettings := []struct {
800 arch string
801 archVariant string
802 cpuVariant string
803 abi []string
804 }{
Dan Willemsen110a89d2016-01-14 15:17:19 -0800805 // armv5 is only used for unbundled apps
806 //{"arm", "armv5te", "", []string{"armeabi"}},
807 {"arm", "armv7-a", "generic", []string{"armeabi-v7a"}},
808 {"arm", "armv7-a-neon", "generic", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -0800809 {"arm", "armv7-a-neon", "cortex-a7", []string{"armeabi-v7a"}},
810 {"arm", "armv7-a-neon", "cortex-a8", []string{"armeabi-v7a"}},
Dan Willemsen110a89d2016-01-14 15:17:19 -0800811 {"arm", "armv7-a-neon", "cortex-a9", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -0800812 {"arm", "armv7-a-neon", "cortex-a15", []string{"armeabi-v7a"}},
813 {"arm", "armv7-a-neon", "cortex-a53", []string{"armeabi-v7a"}},
814 {"arm", "armv7-a-neon", "cortex-a53.a57", []string{"armeabi-v7a"}},
815 {"arm", "armv7-a-neon", "denver", []string{"armeabi-v7a"}},
816 {"arm", "armv7-a-neon", "krait", []string{"armeabi-v7a"}},
Dan Willemsen110a89d2016-01-14 15:17:19 -0800817 {"arm64", "armv8-a", "cortex-a53", []string{"arm64-v8a"}},
818 {"arm64", "armv8-a", "denver64", []string{"arm64-v8a"}},
Dan Willemsen468cc312016-01-13 23:25:19 -0800819 {"mips", "mips32-fp", "", []string{"mips"}},
820 {"mips", "mips32r2-fp", "", []string{"mips"}},
821 {"mips", "mips32r2-fp-xburst", "", []string{"mips"}},
Dan Willemsen65fb9812016-07-19 21:37:28 -0700822 //{"mips", "mips32r6", "", []string{"mips"}},
Dan Willemsen468cc312016-01-13 23:25:19 -0800823 // mips32r2dsp[r2]-fp fails in the assembler for divdf3.c in compiler-rt:
824 // (same errors in make and soong)
825 // Error: invalid operands `mtlo $ac0,$11'
826 // Error: invalid operands `mthi $ac0,$12'
Dan Willemsen322acaf2016-01-12 23:07:05 -0800827 //{"mips", "mips32r2dsp-fp", "", []string{"mips"}},
828 //{"mips", "mips32r2dspr2-fp", "", []string{"mips"}},
829 // mips64r2 is mismatching 64r2 and 64r6 libraries during linking to libgcc
830 //{"mips64", "mips64r2", "", []string{"mips64"}},
831 {"mips64", "mips64r6", "", []string{"mips64"}},
832 {"x86", "", "", []string{"x86"}},
833 {"x86", "atom", "", []string{"x86"}},
834 {"x86", "haswell", "", []string{"x86"}},
835 {"x86", "ivybridge", "", []string{"x86"}},
836 {"x86", "sandybridge", "", []string{"x86"}},
837 {"x86", "silvermont", "", []string{"x86"}},
Dan Willemsen8a354052016-05-10 14:30:51 -0700838 {"x86", "x86_64", "", []string{"x86"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -0800839 {"x86_64", "", "", []string{"x86_64"}},
840 {"x86_64", "haswell", "", []string{"x86_64"}},
841 {"x86_64", "ivybridge", "", []string{"x86_64"}},
842 {"x86_64", "sandybridge", "", []string{"x86_64"}},
843 {"x86_64", "silvermont", "", []string{"x86_64"}},
844 }
845
Colin Crossa1ad8d12016-06-01 17:09:44 -0700846 var ret []Target
Dan Willemsen322acaf2016-01-12 23:07:05 -0800847
848 for _, config := range archSettings {
849 arch, err := decodeArch(config.arch, &config.archVariant,
850 &config.cpuVariant, &config.abi)
851 if err != nil {
852 return nil, err
853 }
Dan Willemsen17f05262016-05-31 16:27:00 -0700854 arch.Native = false
Colin Crossa1ad8d12016-06-01 17:09:44 -0700855 ret = append(ret, Target{
856 Os: Android,
857 Arch: arch,
858 })
Dan Willemsen322acaf2016-01-12 23:07:05 -0800859 }
860
861 return ret, nil
862}
863
Colin Cross4225f652015-09-17 14:33:42 -0700864// Convert a set of strings from product variables into a single Arch struct
865func decodeArch(arch string, archVariant, cpuVariant *string, abi *[]string) (Arch, error) {
866 stringPtr := func(p *string) string {
867 if p != nil {
868 return *p
869 }
870 return ""
871 }
872
873 slicePtr := func(p *[]string) []string {
874 if p != nil {
875 return *p
876 }
877 return nil
878 }
879
Colin Crosseeabb892015-11-20 13:07:51 -0800880 archType, ok := archTypeMap[arch]
881 if !ok {
882 return Arch{}, fmt.Errorf("unknown arch %q", arch)
883 }
Colin Cross4225f652015-09-17 14:33:42 -0700884
Colin Crosseeabb892015-11-20 13:07:51 -0800885 a := Arch{
Colin Cross4225f652015-09-17 14:33:42 -0700886 ArchType: archType,
887 ArchVariant: stringPtr(archVariant),
888 CpuVariant: stringPtr(cpuVariant),
889 Abi: slicePtr(abi),
Dan Willemsen17f05262016-05-31 16:27:00 -0700890 Native: true,
Colin Crosseeabb892015-11-20 13:07:51 -0800891 }
892
893 if a.ArchVariant == a.ArchType.Name || a.ArchVariant == "generic" {
894 a.ArchVariant = ""
895 }
896
897 if a.CpuVariant == a.ArchType.Name || a.CpuVariant == "generic" {
898 a.CpuVariant = ""
899 }
900
901 for i := 0; i < len(a.Abi); i++ {
902 if a.Abi[i] == "" {
903 a.Abi = append(a.Abi[:i], a.Abi[i+1:]...)
904 i--
905 }
906 }
907
Colin Crossc5c24ad2015-11-20 15:35:00 -0800908 if featureMap, ok := archFeatureMap[archType]; ok {
Dan Willemsenb4850992016-05-06 17:21:20 -0700909 a.ArchFeatures = featureMap[a.ArchVariant]
Colin Crossc5c24ad2015-11-20 15:35:00 -0800910 }
911
Colin Crosseeabb892015-11-20 13:07:51 -0800912 return a, nil
Colin Cross4225f652015-09-17 14:33:42 -0700913}
914
Colin Cross69617d32016-09-06 10:39:07 -0700915func filterMultilibTargets(targets []Target, multilib string) []Target {
916 var ret []Target
917 for _, t := range targets {
918 if t.Arch.ArchType.Multilib == multilib {
919 ret = append(ret, t)
920 }
921 }
922 return ret
923}
924
Colin Crossa1ad8d12016-06-01 17:09:44 -0700925// Use the module multilib setting to select one or more targets from a target list
Colin Cross8b74d172016-09-13 09:59:14 -0700926func decodeMultilib(multilib string, targets []Target, prefer32 bool) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700927 buildTargets := []Target{}
Colin Cross8b74d172016-09-13 09:59:14 -0700928 if multilib == "first" {
929 if prefer32 {
930 multilib = "prefer32"
931 } else {
932 multilib = "prefer64"
933 }
934 }
Colin Cross4225f652015-09-17 14:33:42 -0700935 switch multilib {
936 case "common":
Colin Crossa1ad8d12016-06-01 17:09:44 -0700937 buildTargets = append(buildTargets, commonTarget)
Colin Cross4225f652015-09-17 14:33:42 -0700938 case "both":
Colin Cross8b74d172016-09-13 09:59:14 -0700939 if prefer32 {
940 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
941 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
942 } else {
943 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
944 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
945 }
Colin Cross4225f652015-09-17 14:33:42 -0700946 case "32":
Colin Cross69617d32016-09-06 10:39:07 -0700947 buildTargets = filterMultilibTargets(targets, "lib32")
Colin Cross4225f652015-09-17 14:33:42 -0700948 case "64":
Colin Cross69617d32016-09-06 10:39:07 -0700949 buildTargets = filterMultilibTargets(targets, "lib64")
950 case "prefer32":
951 buildTargets = filterMultilibTargets(targets, "lib32")
952 if len(buildTargets) == 0 {
953 buildTargets = filterMultilibTargets(targets, "lib64")
Colin Cross4225f652015-09-17 14:33:42 -0700954 }
Colin Cross8b74d172016-09-13 09:59:14 -0700955 case "prefer64":
956 buildTargets = filterMultilibTargets(targets, "lib64")
957 if len(buildTargets) == 0 {
958 buildTargets = filterMultilibTargets(targets, "lib32")
959 }
Colin Cross4225f652015-09-17 14:33:42 -0700960 default:
Colin Cross69617d32016-09-06 10:39:07 -0700961 return nil, fmt.Errorf(`compile_multilib must be "both", "first", "32", "64", or "prefer32" found %q`,
Colin Cross4225f652015-09-17 14:33:42 -0700962 multilib)
Colin Cross4225f652015-09-17 14:33:42 -0700963 }
964
Colin Crossa1ad8d12016-06-01 17:09:44 -0700965 return buildTargets, nil
Colin Cross4225f652015-09-17 14:33:42 -0700966}