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