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