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