blob: 52c834762515f37155352f24986ac79e8a04d103 [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
15package common
16
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
Colin Cross463a90e2015-06-17 14:20:06 -070027func init() {
Colin Crosscfad1192015-11-02 16:43:11 -080028 RegisterBottomUpMutator("defaults_deps", defaultsDepsMutator)
29 RegisterTopDownMutator("defaults", defaultsMutator)
30
Colin Cross6362e272015-10-29 15:25:03 -070031 RegisterBottomUpMutator("host_or_device", HostOrDeviceMutator)
32 RegisterBottomUpMutator("arch", ArchMutator)
Colin Cross463a90e2015-06-17 14:20:06 -070033}
34
Colin Cross3f40fa42015-01-30 17:27:36 -080035var (
Colin Crossec193632015-07-06 17:49:43 -070036 Arm = newArch("arm", "lib32")
37 Arm64 = newArch("arm64", "lib64")
38 Mips = newArch("mips", "lib32")
39 Mips64 = newArch("mips64", "lib64")
40 X86 = newArch("x86", "lib32")
41 X86_64 = newArch("x86_64", "lib64")
Colin Cross2fe66872015-03-30 17:20:39 -070042
43 Common = ArchType{
44 Name: "common",
45 }
Colin Cross3f40fa42015-01-30 17:27:36 -080046)
47
Colin Cross4225f652015-09-17 14:33:42 -070048var archTypeMap = map[string]ArchType{
49 "arm": Arm,
50 "arm64": Arm64,
51 "mips": Mips,
52 "misp64": Mips64,
53 "x86": X86,
54 "x86_64": X86_64,
55}
56
Colin Cross3f40fa42015-01-30 17:27:36 -080057/*
58Example blueprints file containing all variant property groups, with comment listing what type
59of variants get properties in that group:
60
61module {
62 arch: {
63 arm: {
64 // Host or device variants with arm architecture
65 },
66 arm64: {
67 // Host or device variants with arm64 architecture
68 },
69 mips: {
70 // Host or device variants with mips architecture
71 },
72 mips64: {
73 // Host or device variants with mips64 architecture
74 },
75 x86: {
76 // Host or device variants with x86 architecture
77 },
78 x86_64: {
79 // Host or device variants with x86_64 architecture
80 },
81 },
82 multilib: {
83 lib32: {
84 // Host or device variants for 32-bit architectures
85 },
86 lib64: {
87 // Host or device variants for 64-bit architectures
88 },
89 },
90 target: {
91 android: {
92 // Device variants
93 },
94 host: {
95 // Host variants
96 },
97 linux: {
98 // Linux host variants
99 },
100 darwin: {
101 // Darwin host variants
102 },
103 windows: {
104 // Windows host variants
105 },
106 not_windows: {
107 // Non-windows host variants
108 },
109 },
110}
111*/
Colin Cross7d5136f2015-05-11 13:39:40 -0700112
Colin Cross3f40fa42015-01-30 17:27:36 -0800113type archProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700114 // Properties to vary by target architecture
Colin Cross3f40fa42015-01-30 17:27:36 -0800115 Arch struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700116 // Properties for module variants being built to run on arm (host or device)
117 Arm interface{} `blueprint:"filter(android:\"arch_variant\")"`
118 // Properties for module variants being built to run on arm64 (host or device)
119 Arm64 interface{} `blueprint:"filter(android:\"arch_variant\")"`
120 // Properties for module variants being built to run on mips (host or device)
121 Mips interface{} `blueprint:"filter(android:\"arch_variant\")"`
122 // Properties for module variants being built to run on mips64 (host or device)
123 Mips64 interface{} `blueprint:"filter(android:\"arch_variant\")"`
124 // Properties for module variants being built to run on x86 (host or device)
125 X86 interface{} `blueprint:"filter(android:\"arch_variant\")"`
126 // Properties for module variants being built to run on x86_64 (host or device)
127 X86_64 interface{} `blueprint:"filter(android:\"arch_variant\")"`
Colin Crossec193632015-07-06 17:49:43 -0700128
129 // Arm arch variants
130 Armv5te interface{} `blueprint:"filter(android:\"arch_variant\")"`
131 Armv7_a interface{} `blueprint:"filter(android:\"arch_variant\")"`
132 Armv7_a_neon interface{} `blueprint:"filter(android:\"arch_variant\")"`
133
134 // Arm cpu variants
Dan Willemsen60c3dfb2015-10-16 17:29:12 -0700135 Cortex_a7 interface{} `blueprint:"filter(android:\"arch_variant\")"`
136 Cortex_a8 interface{} `blueprint:"filter(android:\"arch_variant\")"`
137 Cortex_a9 interface{} `blueprint:"filter(android:\"arch_variant\")"`
138 Cortex_a15 interface{} `blueprint:"filter(android:\"arch_variant\")"`
139 Cortex_a53 interface{} `blueprint:"filter(android:\"arch_variant\")"`
140 Cortex_a53_a57 interface{} `blueprint:"filter(android:\"arch_variant\")"`
141 Krait interface{} `blueprint:"filter(android:\"arch_variant\")"`
142 Denver interface{} `blueprint:"filter(android:\"arch_variant\")"`
Colin Crossec193632015-07-06 17:49:43 -0700143
Colin Cross5602b582015-11-10 16:18:47 -0800144 // Arm64 arch variants
145 Armv8_a interface{} `blueprint:"filter(android:\"arch_variant\")"`
146
Colin Crossec193632015-07-06 17:49:43 -0700147 // Arm64 cpu variants
Dan Willemsen60c3dfb2015-10-16 17:29:12 -0700148 Cortex_a53_64 interface{} `blueprint:"filter(android:\"arch_variant\")"`
149 Denver64 interface{} `blueprint:"filter(android:\"arch_variant\")"`
Colin Crossec193632015-07-06 17:49:43 -0700150
151 // Mips arch variants
152 Mips_rev6 interface{} `blueprint:"filter(android:\"arch_variant\")"`
153
Colin Cross01432f62015-07-09 17:56:26 -0700154 // X86 arch variants
Dan Willemsen96dc9f32015-10-16 16:31:15 -0700155 X86_ssse3 interface{} `blueprint:"filter(android:\"arch_variant\")"`
Colin Cross6d27f342015-10-28 17:23:16 -0700156 X86_sse4 interface{} `blueprint:"filter(android:\"arch_variant\")"`
Colin Cross01432f62015-07-09 17:56:26 -0700157
Colin Crossec193632015-07-06 17:49:43 -0700158 // X86 cpu variants
159 Atom interface{} `blueprint:"filter(android:\"arch_variant\")"`
160 Silvermont interface{} `blueprint:"filter(android:\"arch_variant\")"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800161 }
Colin Crossec193632015-07-06 17:49:43 -0700162
Colin Cross7d5136f2015-05-11 13:39:40 -0700163 // Properties to vary by 32-bit or 64-bit
Colin Cross3f40fa42015-01-30 17:27:36 -0800164 Multilib struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700165 // Properties for module variants being built to run on 32-bit devices
166 Lib32 interface{} `blueprint:"filter(android:\"arch_variant\")"`
167 // Properties for module variants being built to run on 64-bit devices
168 Lib64 interface{} `blueprint:"filter(android:\"arch_variant\")"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800169 }
Colin Cross7d5136f2015-05-11 13:39:40 -0700170 // Properties to vary by build target (host or device, os, os+archictecture)
Colin Cross3f40fa42015-01-30 17:27:36 -0800171 Target struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700172 // Properties for module variants being built to run on the host
173 Host interface{} `blueprint:"filter(android:\"arch_variant\")"`
174 // Properties for module variants being built to run on the device
175 Android interface{} `blueprint:"filter(android:\"arch_variant\")"`
176 // Properties for module variants being built to run on arm devices
177 Android_arm interface{} `blueprint:"filter(android:\"arch_variant\")"`
178 // Properties for module variants being built to run on arm64 devices
179 Android_arm64 interface{} `blueprint:"filter(android:\"arch_variant\")"`
180 // Properties for module variants being built to run on mips devices
181 Android_mips interface{} `blueprint:"filter(android:\"arch_variant\")"`
182 // Properties for module variants being built to run on mips64 devices
183 Android_mips64 interface{} `blueprint:"filter(android:\"arch_variant\")"`
184 // Properties for module variants being built to run on x86 devices
185 Android_x86 interface{} `blueprint:"filter(android:\"arch_variant\")"`
186 // Properties for module variants being built to run on x86_64 devices
187 Android_x86_64 interface{} `blueprint:"filter(android:\"arch_variant\")"`
188 // Properties for module variants being built to run on devices that support 64-bit
189 Android64 interface{} `blueprint:"filter(android:\"arch_variant\")"`
190 // Properties for module variants being built to run on devices that do not support 64-bit
191 Android32 interface{} `blueprint:"filter(android:\"arch_variant\")"`
192 // Properties for module variants being built to run on linux hosts
193 Linux interface{} `blueprint:"filter(android:\"arch_variant\")"`
194 // Properties for module variants being built to run on linux x86 hosts
195 Linux_x86 interface{} `blueprint:"filter(android:\"arch_variant\")"`
196 // Properties for module variants being built to run on linux x86_64 hosts
197 Linux_x86_64 interface{} `blueprint:"filter(android:\"arch_variant\")"`
198 // Properties for module variants being built to run on darwin hosts
199 Darwin interface{} `blueprint:"filter(android:\"arch_variant\")"`
200 // Properties for module variants being built to run on darwin x86 hosts
201 Darwin_x86 interface{} `blueprint:"filter(android:\"arch_variant\")"`
202 // Properties for module variants being built to run on darwin x86_64 hosts
203 Darwin_x86_64 interface{} `blueprint:"filter(android:\"arch_variant\")"`
204 // Properties for module variants being built to run on windows hosts
205 Windows interface{} `blueprint:"filter(android:\"arch_variant\")"`
206 // Properties for module variants being built to run on linux or darwin hosts
207 Not_windows interface{} `blueprint:"filter(android:\"arch_variant\")"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800208 }
209}
210
211// An Arch indicates a single CPU architecture.
212type Arch struct {
Colin Crossd3ba0392015-05-07 14:11:29 -0700213 ArchType ArchType
214 ArchVariant string
215 CpuVariant string
Colin Cross4225f652015-09-17 14:33:42 -0700216 Abi []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800217}
218
219func (a Arch) String() string {
Colin Crossd3ba0392015-05-07 14:11:29 -0700220 s := a.ArchType.String()
Colin Cross3f40fa42015-01-30 17:27:36 -0800221 if a.ArchVariant != "" {
222 s += "_" + a.ArchVariant
223 }
224 if a.CpuVariant != "" {
225 s += "_" + a.CpuVariant
226 }
227 return s
228}
229
230type ArchType struct {
Colin Crossec193632015-07-06 17:49:43 -0700231 Name string
232 Multilib string
Colin Cross3f40fa42015-01-30 17:27:36 -0800233}
234
Colin Crossec193632015-07-06 17:49:43 -0700235func newArch(name, multilib string) ArchType {
Colin Cross3f40fa42015-01-30 17:27:36 -0800236 return ArchType{
Colin Crossec193632015-07-06 17:49:43 -0700237 Name: name,
238 Multilib: multilib,
Colin Cross3f40fa42015-01-30 17:27:36 -0800239 }
240}
241
242func (a ArchType) String() string {
243 return a.Name
244}
245
246type HostOrDeviceSupported int
247
248const (
249 _ HostOrDeviceSupported = iota
250 HostSupported
251 DeviceSupported
252 HostAndDeviceSupported
253)
254
255type HostOrDevice int
256
257const (
258 _ HostOrDevice = iota
259 Host
260 Device
261)
262
263func (hod HostOrDevice) String() string {
264 switch hod {
265 case Device:
266 return "device"
267 case Host:
268 return "host"
269 default:
270 panic(fmt.Sprintf("unexpected HostOrDevice value %d", hod))
271 }
272}
273
Colin Crossec193632015-07-06 17:49:43 -0700274func (hod HostOrDevice) Property() string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800275 switch hod {
276 case Device:
277 return "android"
278 case Host:
279 return "host"
280 default:
281 panic(fmt.Sprintf("unexpected HostOrDevice value %d", hod))
282 }
283}
284
Colin Cross3f40fa42015-01-30 17:27:36 -0800285func (hod HostOrDevice) Host() bool {
286 if hod == 0 {
287 panic("HostOrDevice unset")
288 }
289 return hod == Host
290}
291
292func (hod HostOrDevice) Device() bool {
293 if hod == 0 {
294 panic("HostOrDevice unset")
295 }
296 return hod == Device
297}
298
299var hostOrDeviceName = map[HostOrDevice]string{
300 Device: "device",
301 Host: "host",
302}
303
304var (
Colin Crossd3ba0392015-05-07 14:11:29 -0700305 commonArch = Arch{
306 ArchType: Common,
Colin Cross2fe66872015-03-30 17:20:39 -0700307 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800308)
309
Colin Cross6362e272015-10-29 15:25:03 -0700310func HostOrDeviceMutator(mctx AndroidBottomUpMutatorContext) {
Colin Crossd3ba0392015-05-07 14:11:29 -0700311 var module AndroidModule
312 var ok bool
313 if module, ok = mctx.Module().(AndroidModule); !ok {
314 return
315 }
316
317 hods := []HostOrDevice{}
318
319 if module.base().HostSupported() {
320 hods = append(hods, Host)
321 }
322
323 if module.base().DeviceSupported() {
324 hods = append(hods, Device)
325 }
326
327 if len(hods) == 0 {
328 return
329 }
330
331 hodNames := []string{}
332 for _, hod := range hods {
333 hodNames = append(hodNames, hod.String())
334 }
335
336 modules := mctx.CreateVariations(hodNames...)
337 for i, m := range modules {
338 m.(AndroidModule).base().SetHostOrDevice(hods[i])
339 }
340}
341
Colin Cross6362e272015-10-29 15:25:03 -0700342func ArchMutator(mctx AndroidBottomUpMutatorContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800343 var module AndroidModule
344 var ok bool
345 if module, ok = mctx.Module().(AndroidModule); !ok {
346 return
347 }
348
Colin Cross4225f652015-09-17 14:33:42 -0700349 hostArches, deviceArches, err := decodeArchProductVariables(mctx.Config().(Config).ProductVariables)
350 if err != nil {
351 mctx.ModuleErrorf("%s", err.Error())
352 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800353
Colin Cross4225f652015-09-17 14:33:42 -0700354 moduleArches := []Arch{}
355 multilib := module.base().commonProperties.Compile_multilib
Colin Cross3f40fa42015-01-30 17:27:36 -0800356
Colin Crossd3ba0392015-05-07 14:11:29 -0700357 if module.base().HostSupported() && module.base().HostOrDevice().Host() {
Colin Cross4225f652015-09-17 14:33:42 -0700358 hostModuleArches, err := decodeMultilib(multilib, hostArches)
359 if err != nil {
360 mctx.ModuleErrorf("%s", err.Error())
Colin Cross2fe66872015-03-30 17:20:39 -0700361 }
Colin Cross4225f652015-09-17 14:33:42 -0700362
363 moduleArches = append(moduleArches, hostModuleArches...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800364 }
365
Colin Crossd3ba0392015-05-07 14:11:29 -0700366 if module.base().DeviceSupported() && module.base().HostOrDevice().Device() {
Colin Cross4225f652015-09-17 14:33:42 -0700367 deviceModuleArches, err := decodeMultilib(multilib, deviceArches)
368 if err != nil {
369 mctx.ModuleErrorf("%s", err.Error())
Colin Cross3f40fa42015-01-30 17:27:36 -0800370 }
Colin Cross4225f652015-09-17 14:33:42 -0700371
372 moduleArches = append(moduleArches, deviceModuleArches...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800373 }
374
Colin Cross4225f652015-09-17 14:33:42 -0700375 if len(moduleArches) == 0 {
Colin Cross5049f022015-03-18 13:28:46 -0700376 return
377 }
378
Colin Cross3f40fa42015-01-30 17:27:36 -0800379 archNames := []string{}
Colin Cross4225f652015-09-17 14:33:42 -0700380 for _, arch := range moduleArches {
Colin Cross3f40fa42015-01-30 17:27:36 -0800381 archNames = append(archNames, arch.String())
382 }
383
384 modules := mctx.CreateVariations(archNames...)
385
386 for i, m := range modules {
Colin Cross4225f652015-09-17 14:33:42 -0700387 m.(AndroidModule).base().SetArch(moduleArches[i])
Colin Crossd3ba0392015-05-07 14:11:29 -0700388 m.(AndroidModule).base().setArchProperties(mctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800389 }
390}
391
Colin Crosscfad1192015-11-02 16:43:11 -0800392func InitArchModule(m AndroidModule,
Colin Cross3f40fa42015-01-30 17:27:36 -0800393 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
394
395 base := m.base()
396
Colin Cross3f40fa42015-01-30 17:27:36 -0800397 base.generalProperties = append(base.generalProperties,
Colin Cross3f40fa42015-01-30 17:27:36 -0800398 propertyStructs...)
399
400 for _, properties := range base.generalProperties {
401 propertiesValue := reflect.ValueOf(properties)
402 if propertiesValue.Kind() != reflect.Ptr {
403 panic("properties must be a pointer to a struct")
404 }
405
406 propertiesValue = propertiesValue.Elem()
407 if propertiesValue.Kind() != reflect.Struct {
408 panic("properties must be a pointer to a struct")
409 }
410
411 archProperties := &archProperties{}
412 forEachInterface(reflect.ValueOf(archProperties), func(v reflect.Value) {
Colin Cross3ab7d882015-05-19 13:03:01 -0700413 newValue := proptools.CloneEmptyProperties(propertiesValue)
Colin Cross3f40fa42015-01-30 17:27:36 -0800414 v.Set(newValue)
415 })
416
417 base.archProperties = append(base.archProperties, archProperties)
418 }
419
420 var allProperties []interface{}
421 allProperties = append(allProperties, base.generalProperties...)
422 for _, asp := range base.archProperties {
423 allProperties = append(allProperties, asp)
424 }
425
426 return m, allProperties
427}
428
Colin Crossec193632015-07-06 17:49:43 -0700429var dashToUnderscoreReplacer = strings.NewReplacer("-", "_")
430
Colin Cross6362e272015-10-29 15:25:03 -0700431func (a *AndroidModuleBase) appendProperties(ctx AndroidBottomUpMutatorContext,
Colin Cross06a931b2015-10-28 17:23:31 -0700432 dst, src interface{}, field, srcPrefix string) {
433
434 src = reflect.ValueOf(src).FieldByName(field).Elem().Interface()
435
436 filter := func(property string,
437 dstField, srcField reflect.StructField,
438 dstValue, srcValue interface{}) (bool, error) {
439
440 srcProperty := srcPrefix + "." + property
441
442 if !proptools.HasTag(dstField, "android", "arch_variant") {
443 if ctx.ContainsProperty(srcProperty) {
444 return false, fmt.Errorf("can't be specific to a build variant")
445 } else {
446 return false, nil
447 }
448 }
449
450 return true, nil
451 }
452
453 err := proptools.AppendProperties(dst, src, filter)
454 if err != nil {
455 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
456 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
457 } else {
458 panic(err)
459 }
460 }
461}
462
Colin Cross3f40fa42015-01-30 17:27:36 -0800463// Rewrite the module's properties structs to contain arch-specific values.
Colin Cross6362e272015-10-29 15:25:03 -0700464func (a *AndroidModuleBase) setArchProperties(ctx AndroidBottomUpMutatorContext) {
Colin Crossd3ba0392015-05-07 14:11:29 -0700465 arch := a.commonProperties.CompileArch
466 hod := a.commonProperties.CompileHostOrDevice
467
Colin Cross2fe66872015-03-30 17:20:39 -0700468 if arch.ArchType == Common {
469 return
470 }
471
Colin Cross3f40fa42015-01-30 17:27:36 -0800472 for i := range a.generalProperties {
Colin Cross06a931b2015-10-28 17:23:31 -0700473 genProps := a.generalProperties[i]
474 archProps := a.archProperties[i]
Colin Cross3f40fa42015-01-30 17:27:36 -0800475 // Handle arch-specific properties in the form:
Colin Crossb05bff22015-04-30 15:08:04 -0700476 // arch: {
477 // arm64: {
Colin Cross3f40fa42015-01-30 17:27:36 -0800478 // key: value,
479 // },
480 // },
481 t := arch.ArchType
Colin Cross06a931b2015-10-28 17:23:31 -0700482
Colin Crossec193632015-07-06 17:49:43 -0700483 field := proptools.FieldNameForProperty(t.Name)
Colin Cross06a931b2015-10-28 17:23:31 -0700484 prefix := "arch." + t.Name
485 a.appendProperties(ctx, genProps, archProps.Arch, field, prefix)
Colin Crossec193632015-07-06 17:49:43 -0700486
487 // Handle arch-variant-specific properties in the form:
488 // arch: {
489 // variant: {
490 // key: value,
491 // },
492 // },
493 v := dashToUnderscoreReplacer.Replace(arch.ArchVariant)
494 if v != "" {
495 field := proptools.FieldNameForProperty(v)
Colin Cross06a931b2015-10-28 17:23:31 -0700496 prefix := "arch." + v
497 a.appendProperties(ctx, genProps, archProps.Arch, field, prefix)
Colin Crossec193632015-07-06 17:49:43 -0700498 }
499
500 // Handle cpu-variant-specific properties in the form:
501 // arch: {
502 // variant: {
503 // key: value,
504 // },
505 // },
506 c := dashToUnderscoreReplacer.Replace(arch.CpuVariant)
507 if c != "" {
508 field := proptools.FieldNameForProperty(c)
Colin Cross06a931b2015-10-28 17:23:31 -0700509 prefix := "arch." + c
510 a.appendProperties(ctx, genProps, archProps.Arch, field, prefix)
Colin Crossec193632015-07-06 17:49:43 -0700511 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800512
513 // Handle multilib-specific properties in the form:
Colin Crossb05bff22015-04-30 15:08:04 -0700514 // multilib: {
515 // lib32: {
Colin Cross3f40fa42015-01-30 17:27:36 -0800516 // key: value,
517 // },
518 // },
Colin Cross06a931b2015-10-28 17:23:31 -0700519 field = proptools.FieldNameForProperty(t.Multilib)
520 prefix = "multilib." + t.Multilib
521 a.appendProperties(ctx, genProps, archProps.Multilib, field, prefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800522
523 // Handle host-or-device-specific properties in the form:
Colin Crossb05bff22015-04-30 15:08:04 -0700524 // target: {
525 // host: {
Colin Cross3f40fa42015-01-30 17:27:36 -0800526 // key: value,
527 // },
528 // },
Colin Crossec193632015-07-06 17:49:43 -0700529 hodProperty := hod.Property()
Colin Cross06a931b2015-10-28 17:23:31 -0700530 field = proptools.FieldNameForProperty(hodProperty)
531 prefix = "target." + hodProperty
532 a.appendProperties(ctx, genProps, archProps.Target, field, prefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800533
534 // Handle host target properties in the form:
Colin Crossb05bff22015-04-30 15:08:04 -0700535 // target: {
536 // linux: {
Colin Cross3f40fa42015-01-30 17:27:36 -0800537 // key: value,
538 // },
Colin Crossb05bff22015-04-30 15:08:04 -0700539 // not_windows: {
540 // key: value,
541 // },
542 // linux_x86: {
543 // key: value,
544 // },
545 // linux_arm: {
Colin Cross3f40fa42015-01-30 17:27:36 -0800546 // key: value,
547 // },
548 // },
549 var osList = []struct {
550 goos string
551 field string
552 }{
553 {"darwin", "Darwin"},
554 {"linux", "Linux"},
555 {"windows", "Windows"},
556 }
557
558 if hod.Host() {
559 for _, v := range osList {
560 if v.goos == runtime.GOOS {
Colin Cross06a931b2015-10-28 17:23:31 -0700561 field := v.field
562 prefix := "target." + v.goos
563 a.appendProperties(ctx, genProps, archProps.Target, field, prefix)
Colin Crossb05bff22015-04-30 15:08:04 -0700564 t := arch.ArchType
Colin Cross06a931b2015-10-28 17:23:31 -0700565 field = v.field + "_" + t.Name
566 prefix = "target." + v.goos + "_" + t.Name
567 a.appendProperties(ctx, genProps, archProps.Target, field, prefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800568 }
569 }
Colin Cross06a931b2015-10-28 17:23:31 -0700570 field := "Not_windows"
571 prefix := "target.not_windows"
572 a.appendProperties(ctx, genProps, archProps.Target, field, prefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800573 }
574
Colin Crossf8209412015-03-26 14:44:26 -0700575 // Handle 64-bit device properties in the form:
576 // target {
577 // android64 {
578 // key: value,
579 // },
580 // android32 {
581 // key: value,
582 // },
583 // },
584 // WARNING: this is probably not what you want to use in your blueprints file, it selects
585 // options for all targets on a device that supports 64-bit binaries, not just the targets
586 // that are being compiled for 64-bit. Its expected use case is binaries like linker and
587 // debuggerd that need to know when they are a 32-bit process running on a 64-bit device
588 if hod.Device() {
589 if true /* && target_is_64_bit */ {
Colin Cross06a931b2015-10-28 17:23:31 -0700590 field := "Android64"
591 prefix := "target.android64"
592 a.appendProperties(ctx, genProps, archProps.Target, field, prefix)
Colin Crossf8209412015-03-26 14:44:26 -0700593 } else {
Colin Cross06a931b2015-10-28 17:23:31 -0700594 field := "Android32"
595 prefix := "target.android32"
596 a.appendProperties(ctx, genProps, archProps.Target, field, prefix)
Colin Crossf8209412015-03-26 14:44:26 -0700597 }
598 }
Colin Crossb05bff22015-04-30 15:08:04 -0700599
600 // Handle device architecture properties in the form:
601 // target {
602 // android_arm {
603 // key: value,
604 // },
605 // android_x86 {
606 // key: value,
607 // },
608 // },
609 if hod.Device() {
610 t := arch.ArchType
Colin Cross06a931b2015-10-28 17:23:31 -0700611 field := "Android_" + t.Name
612 prefix := "target.android_" + t.Name
613 a.appendProperties(ctx, genProps, archProps.Target, field, prefix)
Colin Crossb05bff22015-04-30 15:08:04 -0700614 }
615
Colin Cross3f40fa42015-01-30 17:27:36 -0800616 if ctx.Failed() {
617 return
618 }
619 }
620}
621
622func forEachInterface(v reflect.Value, f func(reflect.Value)) {
623 switch v.Kind() {
624 case reflect.Interface:
625 f(v)
626 case reflect.Struct:
627 for i := 0; i < v.NumField(); i++ {
628 forEachInterface(v.Field(i), f)
629 }
630 case reflect.Ptr:
631 forEachInterface(v.Elem(), f)
632 default:
633 panic(fmt.Errorf("Unsupported kind %s", v.Kind()))
634 }
635}
Colin Cross4225f652015-09-17 14:33:42 -0700636
637// Convert the arch product variables into a list of host and device Arch structs
638func decodeArchProductVariables(variables productVariables) ([]Arch, []Arch, error) {
639 if variables.HostArch == nil {
640 return nil, nil, fmt.Errorf("No host primary architecture set")
641 }
642
643 hostArch, err := decodeArch(*variables.HostArch, nil, nil, nil)
644 if err != nil {
645 return nil, nil, err
646 }
647
648 hostArches := []Arch{hostArch}
649
650 if variables.HostSecondaryArch != nil {
651 hostSecondaryArch, err := decodeArch(*variables.HostSecondaryArch, nil, nil, nil)
652 if err != nil {
653 return nil, nil, err
654 }
655 hostArches = append(hostArches, hostSecondaryArch)
656 }
657
658 if variables.DeviceArch == nil {
659 return nil, nil, fmt.Errorf("No device primary architecture set")
660 }
661
662 deviceArch, err := decodeArch(*variables.DeviceArch, variables.DeviceArchVariant,
663 variables.DeviceCpuVariant, variables.DeviceAbi)
664 if err != nil {
665 return nil, nil, err
666 }
667
668 deviceArches := []Arch{deviceArch}
669
670 if variables.DeviceSecondaryArch != nil {
671 deviceSecondaryArch, err := decodeArch(*variables.DeviceSecondaryArch,
672 variables.DeviceSecondaryArchVariant, variables.DeviceSecondaryCpuVariant,
673 variables.DeviceSecondaryAbi)
674 if err != nil {
675 return nil, nil, err
676 }
677 deviceArches = append(deviceArches, deviceSecondaryArch)
678 }
679
680 return hostArches, deviceArches, nil
681}
682
683// Convert a set of strings from product variables into a single Arch struct
684func decodeArch(arch string, archVariant, cpuVariant *string, abi *[]string) (Arch, error) {
685 stringPtr := func(p *string) string {
686 if p != nil {
687 return *p
688 }
689 return ""
690 }
691
692 slicePtr := func(p *[]string) []string {
693 if p != nil {
694 return *p
695 }
696 return nil
697 }
698
699 archType := archTypeMap[arch]
700
701 return Arch{
702 ArchType: archType,
703 ArchVariant: stringPtr(archVariant),
704 CpuVariant: stringPtr(cpuVariant),
705 Abi: slicePtr(abi),
706 }, nil
707}
708
709// Use the module multilib setting to select one or more arches from an arch list
710func decodeMultilib(multilib string, arches []Arch) ([]Arch, error) {
711 buildArches := []Arch{}
712 switch multilib {
713 case "common":
714 buildArches = append(buildArches, commonArch)
715 case "both":
716 buildArches = append(buildArches, arches...)
717 case "first":
718 buildArches = append(buildArches, arches[0])
719 case "32":
720 for _, a := range arches {
721 if a.ArchType.Multilib == "lib32" {
722 buildArches = append(buildArches, a)
723 }
724 }
725 case "64":
726 for _, a := range arches {
727 if a.ArchType.Multilib == "lib64" {
728 buildArches = append(buildArches, a)
729 }
730 }
731 default:
732 return nil, fmt.Errorf(`compile_multilib must be "both", "first", "32", or "64", found %q`,
733 multilib)
734 //buildArches = append(buildArches, arches[0])
735 }
736
737 return buildArches, nil
738}