Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package common |
| 16 | |
| 17 | import ( |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 18 | "fmt" |
| 19 | "reflect" |
| 20 | "runtime" |
| 21 | "strings" |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 22 | |
| 23 | "github.com/google/blueprint" |
| 24 | "github.com/google/blueprint/proptools" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | var ( |
| 28 | Arm = newArch32("Arm") |
| 29 | Arm64 = newArch64("Arm64") |
| 30 | Mips = newArch32("Mips") |
| 31 | Mips64 = newArch64("Mips64") |
| 32 | X86 = newArch32("X86") |
| 33 | X86_64 = newArch64("X86_64") |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 34 | |
| 35 | Common = ArchType{ |
| 36 | Name: "common", |
| 37 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 38 | ) |
| 39 | |
| 40 | /* |
| 41 | Example blueprints file containing all variant property groups, with comment listing what type |
| 42 | of variants get properties in that group: |
| 43 | |
| 44 | module { |
| 45 | arch: { |
| 46 | arm: { |
| 47 | // Host or device variants with arm architecture |
| 48 | }, |
| 49 | arm64: { |
| 50 | // Host or device variants with arm64 architecture |
| 51 | }, |
| 52 | mips: { |
| 53 | // Host or device variants with mips architecture |
| 54 | }, |
| 55 | mips64: { |
| 56 | // Host or device variants with mips64 architecture |
| 57 | }, |
| 58 | x86: { |
| 59 | // Host or device variants with x86 architecture |
| 60 | }, |
| 61 | x86_64: { |
| 62 | // Host or device variants with x86_64 architecture |
| 63 | }, |
| 64 | }, |
| 65 | multilib: { |
| 66 | lib32: { |
| 67 | // Host or device variants for 32-bit architectures |
| 68 | }, |
| 69 | lib64: { |
| 70 | // Host or device variants for 64-bit architectures |
| 71 | }, |
| 72 | }, |
| 73 | target: { |
| 74 | android: { |
| 75 | // Device variants |
| 76 | }, |
| 77 | host: { |
| 78 | // Host variants |
| 79 | }, |
| 80 | linux: { |
| 81 | // Linux host variants |
| 82 | }, |
| 83 | darwin: { |
| 84 | // Darwin host variants |
| 85 | }, |
| 86 | windows: { |
| 87 | // Windows host variants |
| 88 | }, |
| 89 | not_windows: { |
| 90 | // Non-windows host variants |
| 91 | }, |
| 92 | }, |
| 93 | } |
| 94 | */ |
| 95 | type archProperties struct { |
| 96 | Arch struct { |
| 97 | Arm interface{} |
| 98 | Arm64 interface{} |
| 99 | Mips interface{} |
| 100 | Mips64 interface{} |
| 101 | X86 interface{} |
| 102 | X86_64 interface{} |
| 103 | } |
| 104 | Multilib struct { |
| 105 | Lib32 interface{} |
| 106 | Lib64 interface{} |
| 107 | } |
| 108 | Target struct { |
Colin Cross | b05bff2 | 2015-04-30 15:08:04 -0700 | [diff] [blame] | 109 | Host interface{} |
| 110 | Android interface{} |
| 111 | Android_arm interface{} |
| 112 | Android_arm64 interface{} |
| 113 | Android_mips interface{} |
| 114 | Android_mips64 interface{} |
| 115 | Android_x86 interface{} |
| 116 | Android_x86_64 interface{} |
| 117 | Android64 interface{} |
| 118 | Android32 interface{} |
| 119 | Linux interface{} |
| 120 | Linux_x86 interface{} |
| 121 | Linux_x86_64 interface{} |
| 122 | Darwin interface{} |
| 123 | Darwin_x86 interface{} |
| 124 | Darwin_x86_64 interface{} |
| 125 | Windows interface{} |
| 126 | Not_windows interface{} |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
| 130 | // An Arch indicates a single CPU architecture. |
| 131 | type Arch struct { |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame^] | 132 | ArchType ArchType |
| 133 | ArchVariant string |
| 134 | CpuVariant string |
| 135 | Abi string |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | func (a Arch) String() string { |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame^] | 139 | s := a.ArchType.String() |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 140 | if a.ArchVariant != "" { |
| 141 | s += "_" + a.ArchVariant |
| 142 | } |
| 143 | if a.CpuVariant != "" { |
| 144 | s += "_" + a.CpuVariant |
| 145 | } |
| 146 | return s |
| 147 | } |
| 148 | |
| 149 | type ArchType struct { |
| 150 | Name string |
| 151 | Field string |
| 152 | Multilib string |
| 153 | MultilibField string |
| 154 | } |
| 155 | |
| 156 | func newArch32(field string) ArchType { |
| 157 | return ArchType{ |
| 158 | Name: strings.ToLower(field), |
| 159 | Field: field, |
| 160 | Multilib: "lib32", |
| 161 | MultilibField: "Lib32", |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | func newArch64(field string) ArchType { |
| 166 | return ArchType{ |
| 167 | Name: strings.ToLower(field), |
| 168 | Field: field, |
| 169 | Multilib: "lib64", |
| 170 | MultilibField: "Lib64", |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | func (a ArchType) String() string { |
| 175 | return a.Name |
| 176 | } |
| 177 | |
| 178 | type HostOrDeviceSupported int |
| 179 | |
| 180 | const ( |
| 181 | _ HostOrDeviceSupported = iota |
| 182 | HostSupported |
| 183 | DeviceSupported |
| 184 | HostAndDeviceSupported |
| 185 | ) |
| 186 | |
| 187 | type HostOrDevice int |
| 188 | |
| 189 | const ( |
| 190 | _ HostOrDevice = iota |
| 191 | Host |
| 192 | Device |
| 193 | ) |
| 194 | |
| 195 | func (hod HostOrDevice) String() string { |
| 196 | switch hod { |
| 197 | case Device: |
| 198 | return "device" |
| 199 | case Host: |
| 200 | return "host" |
| 201 | default: |
| 202 | panic(fmt.Sprintf("unexpected HostOrDevice value %d", hod)) |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | func (hod HostOrDevice) FieldLower() string { |
| 207 | switch hod { |
| 208 | case Device: |
| 209 | return "android" |
| 210 | case Host: |
| 211 | return "host" |
| 212 | default: |
| 213 | panic(fmt.Sprintf("unexpected HostOrDevice value %d", hod)) |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | func (hod HostOrDevice) Field() string { |
| 218 | switch hod { |
| 219 | case Device: |
| 220 | return "Android" |
| 221 | case Host: |
| 222 | return "Host" |
| 223 | default: |
| 224 | panic(fmt.Sprintf("unexpected HostOrDevice value %d", hod)) |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | func (hod HostOrDevice) Host() bool { |
| 229 | if hod == 0 { |
| 230 | panic("HostOrDevice unset") |
| 231 | } |
| 232 | return hod == Host |
| 233 | } |
| 234 | |
| 235 | func (hod HostOrDevice) Device() bool { |
| 236 | if hod == 0 { |
| 237 | panic("HostOrDevice unset") |
| 238 | } |
| 239 | return hod == Device |
| 240 | } |
| 241 | |
| 242 | var hostOrDeviceName = map[HostOrDevice]string{ |
| 243 | Device: "device", |
| 244 | Host: "host", |
| 245 | } |
| 246 | |
| 247 | var ( |
| 248 | armArch = Arch{ |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame^] | 249 | ArchType: Arm, |
| 250 | ArchVariant: "armv7-a-neon", |
| 251 | CpuVariant: "cortex-a15", |
| 252 | Abi: "armeabi-v7a", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 253 | } |
| 254 | arm64Arch = Arch{ |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame^] | 255 | ArchType: Arm64, |
| 256 | ArchVariant: "armv8-a", |
| 257 | CpuVariant: "denver", |
| 258 | Abi: "arm64-v8a", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 259 | } |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame^] | 260 | x86Arch = Arch{ |
| 261 | ArchType: X86, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 262 | } |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame^] | 263 | x8664Arch = Arch{ |
| 264 | ArchType: X86_64, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 265 | } |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame^] | 266 | commonArch = Arch{ |
| 267 | ArchType: Common, |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 268 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 269 | ) |
| 270 | |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame^] | 271 | func HostOrDeviceMutator(mctx blueprint.EarlyMutatorContext) { |
| 272 | var module AndroidModule |
| 273 | var ok bool |
| 274 | if module, ok = mctx.Module().(AndroidModule); !ok { |
| 275 | return |
| 276 | } |
| 277 | |
| 278 | hods := []HostOrDevice{} |
| 279 | |
| 280 | if module.base().HostSupported() { |
| 281 | hods = append(hods, Host) |
| 282 | } |
| 283 | |
| 284 | if module.base().DeviceSupported() { |
| 285 | hods = append(hods, Device) |
| 286 | } |
| 287 | |
| 288 | if len(hods) == 0 { |
| 289 | return |
| 290 | } |
| 291 | |
| 292 | hodNames := []string{} |
| 293 | for _, hod := range hods { |
| 294 | hodNames = append(hodNames, hod.String()) |
| 295 | } |
| 296 | |
| 297 | modules := mctx.CreateVariations(hodNames...) |
| 298 | for i, m := range modules { |
| 299 | m.(AndroidModule).base().SetHostOrDevice(hods[i]) |
| 300 | } |
| 301 | } |
| 302 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 303 | func ArchMutator(mctx blueprint.EarlyMutatorContext) { |
| 304 | var module AndroidModule |
| 305 | var ok bool |
| 306 | if module, ok = mctx.Module().(AndroidModule); !ok { |
| 307 | return |
| 308 | } |
| 309 | |
| 310 | // TODO: this is all hardcoded for arm64 primary, arm secondary for now |
| 311 | // Replace with a configuration file written by lunch or bootstrap |
| 312 | |
| 313 | arches := []Arch{} |
| 314 | |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame^] | 315 | if module.base().HostSupported() && module.base().HostOrDevice().Host() { |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 316 | switch module.base().commonProperties.Compile_multilib { |
| 317 | case "common": |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame^] | 318 | arches = append(arches, commonArch) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 319 | default: |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame^] | 320 | arches = append(arches, x8664Arch) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 321 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame^] | 324 | if module.base().DeviceSupported() && module.base().HostOrDevice().Device() { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 325 | switch module.base().commonProperties.Compile_multilib { |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 326 | case "common": |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame^] | 327 | arches = append(arches, commonArch) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 328 | case "both": |
| 329 | arches = append(arches, arm64Arch, armArch) |
| 330 | case "first", "64": |
| 331 | arches = append(arches, arm64Arch) |
| 332 | case "32": |
| 333 | arches = append(arches, armArch) |
| 334 | default: |
| 335 | mctx.ModuleErrorf(`compile_multilib must be "both", "first", "32", or "64", found %q`, |
| 336 | module.base().commonProperties.Compile_multilib) |
| 337 | } |
| 338 | } |
| 339 | |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 340 | if len(arches) == 0 { |
| 341 | return |
| 342 | } |
| 343 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 344 | archNames := []string{} |
| 345 | for _, arch := range arches { |
| 346 | archNames = append(archNames, arch.String()) |
| 347 | } |
| 348 | |
| 349 | modules := mctx.CreateVariations(archNames...) |
| 350 | |
| 351 | for i, m := range modules { |
| 352 | m.(AndroidModule).base().SetArch(arches[i]) |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame^] | 353 | m.(AndroidModule).base().setArchProperties(mctx) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 357 | func InitArchModule(m AndroidModule, defaultMultilib Multilib, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 358 | propertyStructs ...interface{}) (blueprint.Module, []interface{}) { |
| 359 | |
| 360 | base := m.base() |
| 361 | |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 362 | base.commonProperties.Compile_multilib = string(defaultMultilib) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 363 | |
| 364 | base.generalProperties = append(base.generalProperties, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 365 | propertyStructs...) |
| 366 | |
| 367 | for _, properties := range base.generalProperties { |
| 368 | propertiesValue := reflect.ValueOf(properties) |
| 369 | if propertiesValue.Kind() != reflect.Ptr { |
| 370 | panic("properties must be a pointer to a struct") |
| 371 | } |
| 372 | |
| 373 | propertiesValue = propertiesValue.Elem() |
| 374 | if propertiesValue.Kind() != reflect.Struct { |
| 375 | panic("properties must be a pointer to a struct") |
| 376 | } |
| 377 | |
| 378 | archProperties := &archProperties{} |
| 379 | forEachInterface(reflect.ValueOf(archProperties), func(v reflect.Value) { |
| 380 | newValue := proptools.CloneProperties(propertiesValue) |
| 381 | proptools.ZeroProperties(newValue.Elem()) |
| 382 | v.Set(newValue) |
| 383 | }) |
| 384 | |
| 385 | base.archProperties = append(base.archProperties, archProperties) |
| 386 | } |
| 387 | |
| 388 | var allProperties []interface{} |
| 389 | allProperties = append(allProperties, base.generalProperties...) |
| 390 | for _, asp := range base.archProperties { |
| 391 | allProperties = append(allProperties, asp) |
| 392 | } |
| 393 | |
| 394 | return m, allProperties |
| 395 | } |
| 396 | |
| 397 | // Rewrite the module's properties structs to contain arch-specific values. |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame^] | 398 | func (a *AndroidModuleBase) setArchProperties(ctx blueprint.EarlyMutatorContext) { |
| 399 | arch := a.commonProperties.CompileArch |
| 400 | hod := a.commonProperties.CompileHostOrDevice |
| 401 | |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 402 | if arch.ArchType == Common { |
| 403 | return |
| 404 | } |
| 405 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 406 | for i := range a.generalProperties { |
| 407 | generalPropsValue := reflect.ValueOf(a.generalProperties[i]).Elem() |
| 408 | |
| 409 | // Handle arch-specific properties in the form: |
Colin Cross | b05bff2 | 2015-04-30 15:08:04 -0700 | [diff] [blame] | 410 | // arch: { |
| 411 | // arm64: { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 412 | // key: value, |
| 413 | // }, |
| 414 | // }, |
| 415 | t := arch.ArchType |
Colin Cross | 28d7659 | 2015-03-26 16:14:04 -0700 | [diff] [blame] | 416 | a.extendProperties(ctx, "arch", t.Name, generalPropsValue, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 417 | reflect.ValueOf(a.archProperties[i].Arch).FieldByName(t.Field).Elem().Elem()) |
| 418 | |
| 419 | // Handle multilib-specific properties in the form: |
Colin Cross | b05bff2 | 2015-04-30 15:08:04 -0700 | [diff] [blame] | 420 | // multilib: { |
| 421 | // lib32: { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 422 | // key: value, |
| 423 | // }, |
| 424 | // }, |
Colin Cross | 28d7659 | 2015-03-26 16:14:04 -0700 | [diff] [blame] | 425 | a.extendProperties(ctx, "multilib", t.Multilib, generalPropsValue, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 426 | reflect.ValueOf(a.archProperties[i].Multilib).FieldByName(t.MultilibField).Elem().Elem()) |
| 427 | |
| 428 | // Handle host-or-device-specific properties in the form: |
Colin Cross | b05bff2 | 2015-04-30 15:08:04 -0700 | [diff] [blame] | 429 | // target: { |
| 430 | // host: { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 431 | // key: value, |
| 432 | // }, |
| 433 | // }, |
Colin Cross | 28d7659 | 2015-03-26 16:14:04 -0700 | [diff] [blame] | 434 | a.extendProperties(ctx, "target", hod.FieldLower(), generalPropsValue, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 435 | reflect.ValueOf(a.archProperties[i].Target).FieldByName(hod.Field()).Elem().Elem()) |
| 436 | |
| 437 | // Handle host target properties in the form: |
Colin Cross | b05bff2 | 2015-04-30 15:08:04 -0700 | [diff] [blame] | 438 | // target: { |
| 439 | // linux: { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 440 | // key: value, |
| 441 | // }, |
Colin Cross | b05bff2 | 2015-04-30 15:08:04 -0700 | [diff] [blame] | 442 | // not_windows: { |
| 443 | // key: value, |
| 444 | // }, |
| 445 | // linux_x86: { |
| 446 | // key: value, |
| 447 | // }, |
| 448 | // linux_arm: { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 449 | // key: value, |
| 450 | // }, |
| 451 | // }, |
| 452 | var osList = []struct { |
| 453 | goos string |
| 454 | field string |
| 455 | }{ |
| 456 | {"darwin", "Darwin"}, |
| 457 | {"linux", "Linux"}, |
| 458 | {"windows", "Windows"}, |
| 459 | } |
| 460 | |
| 461 | if hod.Host() { |
| 462 | for _, v := range osList { |
| 463 | if v.goos == runtime.GOOS { |
Colin Cross | 28d7659 | 2015-03-26 16:14:04 -0700 | [diff] [blame] | 464 | a.extendProperties(ctx, "target", v.goos, generalPropsValue, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 465 | reflect.ValueOf(a.archProperties[i].Target).FieldByName(v.field).Elem().Elem()) |
Colin Cross | b05bff2 | 2015-04-30 15:08:04 -0700 | [diff] [blame] | 466 | t := arch.ArchType |
| 467 | a.extendProperties(ctx, "target", v.goos+"_"+t.Name, generalPropsValue, |
| 468 | reflect.ValueOf(a.archProperties[i].Target).FieldByName(v.field+"_"+t.Name).Elem().Elem()) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 469 | } |
| 470 | } |
Colin Cross | 28d7659 | 2015-03-26 16:14:04 -0700 | [diff] [blame] | 471 | a.extendProperties(ctx, "target", "not_windows", generalPropsValue, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 472 | reflect.ValueOf(a.archProperties[i].Target).FieldByName("Not_windows").Elem().Elem()) |
| 473 | } |
| 474 | |
Colin Cross | f820941 | 2015-03-26 14:44:26 -0700 | [diff] [blame] | 475 | // Handle 64-bit device properties in the form: |
| 476 | // target { |
| 477 | // android64 { |
| 478 | // key: value, |
| 479 | // }, |
| 480 | // android32 { |
| 481 | // key: value, |
| 482 | // }, |
| 483 | // }, |
| 484 | // WARNING: this is probably not what you want to use in your blueprints file, it selects |
| 485 | // options for all targets on a device that supports 64-bit binaries, not just the targets |
| 486 | // that are being compiled for 64-bit. Its expected use case is binaries like linker and |
| 487 | // debuggerd that need to know when they are a 32-bit process running on a 64-bit device |
| 488 | if hod.Device() { |
| 489 | if true /* && target_is_64_bit */ { |
Colin Cross | 28d7659 | 2015-03-26 16:14:04 -0700 | [diff] [blame] | 490 | a.extendProperties(ctx, "target", "android64", generalPropsValue, |
Colin Cross | f820941 | 2015-03-26 14:44:26 -0700 | [diff] [blame] | 491 | reflect.ValueOf(a.archProperties[i].Target).FieldByName("Android64").Elem().Elem()) |
| 492 | } else { |
Colin Cross | 28d7659 | 2015-03-26 16:14:04 -0700 | [diff] [blame] | 493 | a.extendProperties(ctx, "target", "android32", generalPropsValue, |
Colin Cross | f820941 | 2015-03-26 14:44:26 -0700 | [diff] [blame] | 494 | reflect.ValueOf(a.archProperties[i].Target).FieldByName("Android32").Elem().Elem()) |
| 495 | } |
| 496 | } |
Colin Cross | b05bff2 | 2015-04-30 15:08:04 -0700 | [diff] [blame] | 497 | |
| 498 | // Handle device architecture properties in the form: |
| 499 | // target { |
| 500 | // android_arm { |
| 501 | // key: value, |
| 502 | // }, |
| 503 | // android_x86 { |
| 504 | // key: value, |
| 505 | // }, |
| 506 | // }, |
| 507 | if hod.Device() { |
| 508 | t := arch.ArchType |
| 509 | a.extendProperties(ctx, "target", "android_"+t.Name, generalPropsValue, |
| 510 | reflect.ValueOf(a.archProperties[i].Target).FieldByName("Android_"+t.Name).Elem().Elem()) |
| 511 | } |
| 512 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 513 | if ctx.Failed() { |
| 514 | return |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | func forEachInterface(v reflect.Value, f func(reflect.Value)) { |
| 520 | switch v.Kind() { |
| 521 | case reflect.Interface: |
| 522 | f(v) |
| 523 | case reflect.Struct: |
| 524 | for i := 0; i < v.NumField(); i++ { |
| 525 | forEachInterface(v.Field(i), f) |
| 526 | } |
| 527 | case reflect.Ptr: |
| 528 | forEachInterface(v.Elem(), f) |
| 529 | default: |
| 530 | panic(fmt.Errorf("Unsupported kind %s", v.Kind())) |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | // TODO: move this to proptools |
Colin Cross | 28d7659 | 2015-03-26 16:14:04 -0700 | [diff] [blame] | 535 | func (a *AndroidModuleBase) extendProperties(ctx blueprint.EarlyMutatorContext, variationType, variationName string, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 536 | dstValue, srcValue reflect.Value) { |
Colin Cross | 28d7659 | 2015-03-26 16:14:04 -0700 | [diff] [blame] | 537 | a.extendPropertiesRecursive(ctx, variationType, variationName, dstValue, srcValue, "") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 538 | } |
| 539 | |
Colin Cross | 28d7659 | 2015-03-26 16:14:04 -0700 | [diff] [blame] | 540 | func (a *AndroidModuleBase) extendPropertiesRecursive(ctx blueprint.EarlyMutatorContext, variationType, variationName string, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 541 | dstValue, srcValue reflect.Value, recursePrefix string) { |
| 542 | |
| 543 | typ := dstValue.Type() |
| 544 | if srcValue.Type() != typ { |
| 545 | panic(fmt.Errorf("can't extend mismatching types (%s <- %s)", |
| 546 | dstValue.Kind(), srcValue.Kind())) |
| 547 | } |
| 548 | |
| 549 | for i := 0; i < srcValue.NumField(); i++ { |
| 550 | field := typ.Field(i) |
| 551 | if field.PkgPath != "" { |
| 552 | // The field is not exported so just skip it. |
| 553 | continue |
| 554 | } |
| 555 | |
| 556 | srcFieldValue := srcValue.Field(i) |
| 557 | dstFieldValue := dstValue.Field(i) |
| 558 | |
| 559 | localPropertyName := proptools.PropertyNameForField(field.Name) |
| 560 | propertyName := fmt.Sprintf("%s.%s.%s%s", variationType, variationName, |
| 561 | recursePrefix, localPropertyName) |
| 562 | propertyPresentInVariation := ctx.ContainsProperty(propertyName) |
| 563 | |
| 564 | if !propertyPresentInVariation { |
| 565 | continue |
| 566 | } |
| 567 | |
| 568 | tag := field.Tag.Get("android") |
| 569 | tags := map[string]bool{} |
| 570 | for _, entry := range strings.Split(tag, ",") { |
| 571 | if entry != "" { |
| 572 | tags[entry] = true |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | if !tags["arch_variant"] { |
| 577 | ctx.PropertyErrorf(propertyName, "property %q can't be specific to a build variant", |
| 578 | recursePrefix+proptools.PropertyNameForField(field.Name)) |
| 579 | continue |
| 580 | } |
| 581 | |
Colin Cross | 28d7659 | 2015-03-26 16:14:04 -0700 | [diff] [blame] | 582 | if !ctx.ContainsProperty(propertyName) { |
| 583 | continue |
| 584 | } |
| 585 | a.extendedProperties[localPropertyName] = struct{}{} |
| 586 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 587 | switch srcFieldValue.Kind() { |
| 588 | case reflect.Bool: |
| 589 | // Replace the original value. |
| 590 | dstFieldValue.Set(srcFieldValue) |
| 591 | case reflect.String: |
| 592 | // Append the extension string. |
| 593 | dstFieldValue.SetString(dstFieldValue.String() + |
| 594 | srcFieldValue.String()) |
| 595 | case reflect.Struct: |
| 596 | // Recursively extend the struct's fields. |
| 597 | newRecursePrefix := fmt.Sprintf("%s%s.", recursePrefix, strings.ToLower(field.Name)) |
Colin Cross | 28d7659 | 2015-03-26 16:14:04 -0700 | [diff] [blame] | 598 | a.extendPropertiesRecursive(ctx, variationType, variationName, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 599 | dstFieldValue, srcFieldValue, |
| 600 | newRecursePrefix) |
| 601 | case reflect.Slice: |
| 602 | val, err := archCombineSlices(dstFieldValue, srcFieldValue, tags["arch_subtract"]) |
| 603 | if err != nil { |
| 604 | ctx.PropertyErrorf(propertyName, err.Error()) |
| 605 | continue |
| 606 | } |
| 607 | dstFieldValue.Set(val) |
| 608 | case reflect.Ptr, reflect.Interface: |
| 609 | // Recursively extend the pointed-to struct's fields. |
| 610 | if dstFieldValue.IsNil() != srcFieldValue.IsNil() { |
| 611 | panic(fmt.Errorf("can't extend field %q: nilitude mismatch")) |
| 612 | } |
| 613 | if dstFieldValue.Type() != srcFieldValue.Type() { |
| 614 | panic(fmt.Errorf("can't extend field %q: type mismatch")) |
| 615 | } |
| 616 | if !dstFieldValue.IsNil() { |
| 617 | newRecursePrefix := fmt.Sprintf("%s.%s", recursePrefix, field.Name) |
Colin Cross | 28d7659 | 2015-03-26 16:14:04 -0700 | [diff] [blame] | 618 | a.extendPropertiesRecursive(ctx, variationType, variationName, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 619 | dstFieldValue.Elem(), srcFieldValue.Elem(), |
| 620 | newRecursePrefix) |
| 621 | } |
| 622 | default: |
| 623 | panic(fmt.Errorf("unexpected kind for property struct field %q: %s", |
| 624 | field.Name, srcFieldValue.Kind())) |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | func archCombineSlices(general, arch reflect.Value, canSubtract bool) (reflect.Value, error) { |
| 630 | if !canSubtract { |
| 631 | // Append the extension slice. |
| 632 | return reflect.AppendSlice(general, arch), nil |
| 633 | } |
| 634 | |
| 635 | // Support -val in arch list to subtract a value from original list |
| 636 | l := general.Interface().([]string) |
| 637 | for archIndex := 0; archIndex < arch.Len(); archIndex++ { |
| 638 | archString := arch.Index(archIndex).String() |
| 639 | if strings.HasPrefix(archString, "-") { |
| 640 | generalIndex := findStringInSlice(archString[1:], l) |
| 641 | if generalIndex == -1 { |
| 642 | return reflect.Value{}, |
| 643 | fmt.Errorf("can't find %q to subtract from general properties", archString[1:]) |
| 644 | } |
| 645 | l = append(l[:generalIndex], l[generalIndex+1:]...) |
| 646 | } else { |
| 647 | l = append(l, archString) |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | return reflect.ValueOf(l), nil |
| 652 | } |
| 653 | |
| 654 | func findStringInSlice(str string, slice []string) int { |
| 655 | for i, s := range slice { |
| 656 | if s == str { |
| 657 | return i |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | return -1 |
| 662 | } |