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 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 15 | package android |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 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 ( |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 28 | archTypeList []ArchType |
| 29 | |
Colin Cross | ec19363 | 2015-07-06 17:49:43 -0700 | [diff] [blame] | 30 | Arm = newArch("arm", "lib32") |
| 31 | Arm64 = newArch("arm64", "lib64") |
| 32 | Mips = newArch("mips", "lib32") |
| 33 | Mips64 = newArch("mips64", "lib64") |
| 34 | X86 = newArch("x86", "lib32") |
| 35 | X86_64 = newArch("x86_64", "lib64") |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 36 | |
| 37 | Common = ArchType{ |
| 38 | Name: "common", |
| 39 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 40 | ) |
| 41 | |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 42 | var archTypeMap = map[string]ArchType{ |
| 43 | "arm": Arm, |
| 44 | "arm64": Arm64, |
| 45 | "mips": Mips, |
Colin Cross | 3b336c2 | 2015-11-23 16:28:31 -0800 | [diff] [blame] | 46 | "mips64": Mips64, |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 47 | "x86": X86, |
| 48 | "x86_64": X86_64, |
| 49 | } |
| 50 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 51 | /* |
| 52 | Example blueprints file containing all variant property groups, with comment listing what type |
| 53 | of variants get properties in that group: |
| 54 | |
| 55 | module { |
| 56 | arch: { |
| 57 | arm: { |
| 58 | // Host or device variants with arm architecture |
| 59 | }, |
| 60 | arm64: { |
| 61 | // Host or device variants with arm64 architecture |
| 62 | }, |
| 63 | mips: { |
| 64 | // Host or device variants with mips architecture |
| 65 | }, |
| 66 | mips64: { |
| 67 | // Host or device variants with mips64 architecture |
| 68 | }, |
| 69 | x86: { |
| 70 | // Host or device variants with x86 architecture |
| 71 | }, |
| 72 | x86_64: { |
| 73 | // Host or device variants with x86_64 architecture |
| 74 | }, |
| 75 | }, |
| 76 | multilib: { |
| 77 | lib32: { |
| 78 | // Host or device variants for 32-bit architectures |
| 79 | }, |
| 80 | lib64: { |
| 81 | // Host or device variants for 64-bit architectures |
| 82 | }, |
| 83 | }, |
| 84 | target: { |
| 85 | android: { |
| 86 | // Device variants |
| 87 | }, |
| 88 | host: { |
| 89 | // Host variants |
| 90 | }, |
| 91 | linux: { |
| 92 | // Linux host variants |
| 93 | }, |
| 94 | darwin: { |
| 95 | // Darwin host variants |
| 96 | }, |
| 97 | windows: { |
| 98 | // Windows host variants |
| 99 | }, |
| 100 | not_windows: { |
| 101 | // Non-windows host variants |
| 102 | }, |
| 103 | }, |
| 104 | } |
| 105 | */ |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 106 | |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 107 | var archVariants = map[ArchType][]string{} |
| 108 | var archFeatures = map[ArchType][]string{} |
Colin Cross | c5c24ad | 2015-11-20 15:35:00 -0800 | [diff] [blame] | 109 | var archFeatureMap = map[ArchType]map[string][]string{} |
| 110 | |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 111 | func RegisterArchVariants(arch ArchType, variants ...string) { |
| 112 | checkCalledFromInit() |
| 113 | archVariants[arch] = append(archVariants[arch], variants...) |
| 114 | } |
| 115 | |
| 116 | func RegisterArchFeatures(arch ArchType, features ...string) { |
| 117 | checkCalledFromInit() |
| 118 | archFeatures[arch] = append(archFeatures[arch], features...) |
| 119 | } |
| 120 | |
| 121 | func RegisterArchVariantFeatures(arch ArchType, variant string, features ...string) { |
| 122 | checkCalledFromInit() |
| 123 | if variant != "" && !inList(variant, archVariants[arch]) { |
| 124 | panic(fmt.Errorf("Invalid variant %q for arch %q", variant, arch)) |
Colin Cross | c5c24ad | 2015-11-20 15:35:00 -0800 | [diff] [blame] | 125 | } |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 126 | |
Colin Cross | c5c24ad | 2015-11-20 15:35:00 -0800 | [diff] [blame] | 127 | for _, feature := range features { |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 128 | if !inList(feature, archFeatures[arch]) { |
Colin Cross | c5c24ad | 2015-11-20 15:35:00 -0800 | [diff] [blame] | 129 | panic(fmt.Errorf("Invalid feature %q for arch %q variant %q", feature, arch, variant)) |
| 130 | } |
| 131 | } |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 132 | |
Colin Cross | c5c24ad | 2015-11-20 15:35:00 -0800 | [diff] [blame] | 133 | if archFeatureMap[arch] == nil { |
| 134 | archFeatureMap[arch] = make(map[string][]string) |
| 135 | } |
| 136 | archFeatureMap[arch][variant] = features |
| 137 | } |
| 138 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 139 | // An Arch indicates a single CPU architecture. |
| 140 | type Arch struct { |
Colin Cross | c5c24ad | 2015-11-20 15:35:00 -0800 | [diff] [blame] | 141 | ArchType ArchType |
| 142 | ArchVariant string |
| 143 | CpuVariant string |
| 144 | Abi []string |
| 145 | ArchFeatures []string |
Dan Willemsen | 17f0526 | 2016-05-31 16:27:00 -0700 | [diff] [blame] | 146 | Native bool |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | func (a Arch) String() string { |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame] | 150 | s := a.ArchType.String() |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 151 | if a.ArchVariant != "" { |
| 152 | s += "_" + a.ArchVariant |
| 153 | } |
| 154 | if a.CpuVariant != "" { |
| 155 | s += "_" + a.CpuVariant |
| 156 | } |
| 157 | return s |
| 158 | } |
| 159 | |
| 160 | type ArchType struct { |
Colin Cross | ec19363 | 2015-07-06 17:49:43 -0700 | [diff] [blame] | 161 | Name string |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 162 | Field string |
Colin Cross | ec19363 | 2015-07-06 17:49:43 -0700 | [diff] [blame] | 163 | Multilib string |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Colin Cross | ec19363 | 2015-07-06 17:49:43 -0700 | [diff] [blame] | 166 | func newArch(name, multilib string) ArchType { |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 167 | archType := ArchType{ |
Colin Cross | ec19363 | 2015-07-06 17:49:43 -0700 | [diff] [blame] | 168 | Name: name, |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 169 | Field: proptools.FieldNameForProperty(name), |
Colin Cross | ec19363 | 2015-07-06 17:49:43 -0700 | [diff] [blame] | 170 | Multilib: multilib, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 171 | } |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 172 | archTypeList = append(archTypeList, archType) |
| 173 | return archType |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | func (a ArchType) String() string { |
| 177 | return a.Name |
| 178 | } |
| 179 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 180 | var BuildOs = func() OsType { |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 181 | switch runtime.GOOS { |
| 182 | case "linux": |
| 183 | return Linux |
| 184 | case "darwin": |
| 185 | return Darwin |
| 186 | default: |
| 187 | panic(fmt.Sprintf("unsupported OS: %s", runtime.GOOS)) |
| 188 | } |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 189 | }() |
| 190 | |
| 191 | var ( |
| 192 | osTypeList []OsType |
| 193 | |
| 194 | NoOsType OsType |
Dan Willemsen | 0a37a2a | 2016-11-13 10:16:05 -0800 | [diff] [blame^] | 195 | Linux = NewOsType("linux", Host, false) |
| 196 | Darwin = NewOsType("darwin", Host, false) |
| 197 | Windows = NewOsType("windows", HostCross, true) |
| 198 | Android = NewOsType("android", Device, false) |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 199 | |
| 200 | osArchTypeMap = map[OsType][]ArchType{ |
| 201 | Linux: []ArchType{X86, X86_64}, |
| 202 | Darwin: []ArchType{X86, X86_64}, |
| 203 | Windows: []ArchType{X86, X86_64}, |
| 204 | Android: []ArchType{Arm, Arm64, Mips, Mips64, X86, X86_64}, |
| 205 | } |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 206 | ) |
| 207 | |
| 208 | type OsType struct { |
| 209 | Name, Field string |
| 210 | Class OsClass |
Dan Willemsen | 0a37a2a | 2016-11-13 10:16:05 -0800 | [diff] [blame^] | 211 | |
| 212 | DefaultDisabled bool |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 215 | type OsClass int |
| 216 | |
| 217 | const ( |
| 218 | Device OsClass = iota |
| 219 | Host |
| 220 | HostCross |
| 221 | ) |
| 222 | |
| 223 | func (os OsType) String() string { |
| 224 | return os.Name |
Colin Cross | 54c7112 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Dan Willemsen | 0a37a2a | 2016-11-13 10:16:05 -0800 | [diff] [blame^] | 227 | func NewOsType(name string, class OsClass, defDisabled bool) OsType { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 228 | os := OsType{ |
| 229 | Name: name, |
| 230 | Field: strings.Title(name), |
| 231 | Class: class, |
Dan Willemsen | 0a37a2a | 2016-11-13 10:16:05 -0800 | [diff] [blame^] | 232 | |
| 233 | DefaultDisabled: defDisabled, |
Colin Cross | 54c7112 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 234 | } |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 235 | osTypeList = append(osTypeList, os) |
| 236 | return os |
| 237 | } |
| 238 | |
| 239 | func osByName(name string) OsType { |
| 240 | for _, os := range osTypeList { |
| 241 | if os.Name == name { |
| 242 | return os |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | return NoOsType |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 249 | var ( |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 250 | commonTarget = Target{ |
| 251 | Os: Android, |
| 252 | Arch: Arch{ |
| 253 | ArchType: Common, |
| 254 | }, |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 255 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 256 | ) |
| 257 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 258 | type Target struct { |
| 259 | Os OsType |
| 260 | Arch Arch |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 263 | func (target Target) String() string { |
| 264 | return target.Os.String() + "_" + target.Arch.String() |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 265 | } |
| 266 | |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 267 | func archMutator(mctx BottomUpMutatorContext) { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 268 | var module Module |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 269 | var ok bool |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 270 | if module, ok = mctx.Module().(Module); !ok { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 271 | return |
| 272 | } |
| 273 | |
Dan Willemsen | 0b24c74 | 2016-10-04 15:13:37 -0700 | [diff] [blame] | 274 | if !module.base().ArchSpecific() { |
Colin Cross | b9db480 | 2016-06-03 01:50:47 +0000 | [diff] [blame] | 275 | return |
| 276 | } |
| 277 | |
Dan Willemsen | 0b24c74 | 2016-10-04 15:13:37 -0700 | [diff] [blame] | 278 | osClasses := module.base().OsClassSupported() |
| 279 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 280 | var moduleTargets []Target |
Colin Cross | 8b74d17 | 2016-09-13 09:59:14 -0700 | [diff] [blame] | 281 | primaryModules := make(map[int]bool) |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 282 | |
| 283 | for _, class := range osClasses { |
| 284 | targets := mctx.AConfig().Targets[class] |
| 285 | if len(targets) == 0 { |
| 286 | continue |
| 287 | } |
Colin Cross | 69617d3 | 2016-09-06 10:39:07 -0700 | [diff] [blame] | 288 | var multilib string |
| 289 | switch class { |
| 290 | case Device: |
| 291 | multilib = module.base().commonProperties.Target.Android.Compile_multilib |
| 292 | case Host, HostCross: |
| 293 | multilib = module.base().commonProperties.Target.Host.Compile_multilib |
| 294 | } |
| 295 | if multilib == "" { |
| 296 | multilib = module.base().commonProperties.Compile_multilib |
| 297 | } |
| 298 | if multilib == "" { |
| 299 | multilib = module.base().commonProperties.Default_multilib |
| 300 | } |
Colin Cross | 628a88c | 2016-09-13 15:15:51 -0700 | [diff] [blame] | 301 | var prefer32 bool |
| 302 | switch class { |
| 303 | case Device: |
Colin Cross | 8b74d17 | 2016-09-13 09:59:14 -0700 | [diff] [blame] | 304 | prefer32 = mctx.AConfig().DevicePrefer32BitExecutables() |
Colin Cross | 628a88c | 2016-09-13 15:15:51 -0700 | [diff] [blame] | 305 | case HostCross: |
| 306 | // Windows builds always prefer 32-bit |
| 307 | prefer32 = true |
Colin Cross | 8b74d17 | 2016-09-13 09:59:14 -0700 | [diff] [blame] | 308 | } |
| 309 | targets, err := decodeMultilib(multilib, targets, prefer32) |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 310 | if err != nil { |
| 311 | mctx.ModuleErrorf("%s", err.Error()) |
| 312 | } |
Colin Cross | 8b74d17 | 2016-09-13 09:59:14 -0700 | [diff] [blame] | 313 | if len(targets) > 0 { |
| 314 | primaryModules[len(moduleTargets)] = true |
| 315 | moduleTargets = append(moduleTargets, targets...) |
| 316 | } |
Colin Cross | b9db480 | 2016-06-03 01:50:47 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Dan Willemsen | 3f32f03 | 2016-07-11 14:36:48 -0700 | [diff] [blame] | 319 | if len(moduleTargets) == 0 { |
| 320 | module.base().commonProperties.Enabled = boolPtr(false) |
| 321 | return |
| 322 | } |
| 323 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 324 | targetNames := make([]string, len(moduleTargets)) |
Colin Cross | b9db480 | 2016-06-03 01:50:47 +0000 | [diff] [blame] | 325 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 326 | for i, target := range moduleTargets { |
| 327 | targetNames[i] = target.String() |
| 328 | } |
| 329 | |
| 330 | modules := mctx.CreateVariations(targetNames...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 331 | for i, m := range modules { |
Colin Cross | 8b74d17 | 2016-09-13 09:59:14 -0700 | [diff] [blame] | 332 | m.(Module).base().SetTarget(moduleTargets[i], primaryModules[i]) |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 333 | m.(Module).base().setArchProperties(mctx) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 337 | func filterArchStruct(prop reflect.Type) (reflect.Type, bool) { |
| 338 | var fields []reflect.StructField |
| 339 | |
| 340 | ptr := prop.Kind() == reflect.Ptr |
| 341 | if ptr { |
| 342 | prop = prop.Elem() |
| 343 | } |
| 344 | |
| 345 | for i := 0; i < prop.NumField(); i++ { |
| 346 | field := prop.Field(i) |
| 347 | if !proptools.HasTag(field, "android", "arch_variant") { |
| 348 | continue |
| 349 | } |
| 350 | |
| 351 | // The arch_variant field isn't necessary past this point |
| 352 | // Instead of wasting space, just remove it. Go also has a |
| 353 | // 16-bit limit on structure name length. The name is constructed |
| 354 | // based on the Go source representation of the structure, so |
| 355 | // the tag names count towards that length. |
| 356 | // |
| 357 | // TODO: handle the uncommon case of other tags being involved |
| 358 | if field.Tag == `android:"arch_variant"` { |
| 359 | field.Tag = "" |
| 360 | } |
| 361 | |
| 362 | // Recurse into structs |
| 363 | switch field.Type.Kind() { |
| 364 | case reflect.Struct: |
| 365 | var ok bool |
| 366 | field.Type, ok = filterArchStruct(field.Type) |
| 367 | if !ok { |
| 368 | continue |
| 369 | } |
| 370 | case reflect.Ptr: |
| 371 | if field.Type.Elem().Kind() == reflect.Struct { |
| 372 | nestedType, ok := filterArchStruct(field.Type.Elem()) |
| 373 | if !ok { |
| 374 | continue |
| 375 | } |
| 376 | field.Type = reflect.PtrTo(nestedType) |
| 377 | } |
| 378 | case reflect.Interface: |
| 379 | panic("Interfaces are not supported in arch_variant properties") |
| 380 | } |
| 381 | |
| 382 | fields = append(fields, field) |
| 383 | } |
| 384 | if len(fields) == 0 { |
| 385 | return nil, false |
| 386 | } |
| 387 | |
| 388 | ret := reflect.StructOf(fields) |
| 389 | if ptr { |
| 390 | ret = reflect.PtrTo(ret) |
| 391 | } |
| 392 | return ret, true |
| 393 | } |
| 394 | |
| 395 | func createArchType(props reflect.Type) reflect.Type { |
| 396 | props, ok := filterArchStruct(props) |
| 397 | if !ok { |
| 398 | return nil |
| 399 | } |
| 400 | |
| 401 | variantFields := func(names []string) []reflect.StructField { |
| 402 | ret := make([]reflect.StructField, len(names)) |
| 403 | |
| 404 | for i, name := range names { |
| 405 | ret[i].Name = name |
| 406 | ret[i].Type = props |
| 407 | } |
| 408 | |
| 409 | return ret |
| 410 | } |
| 411 | |
| 412 | archFields := make([]reflect.StructField, len(archTypeList)) |
| 413 | for i, arch := range archTypeList { |
| 414 | variants := []string{} |
| 415 | |
| 416 | for _, archVariant := range archVariants[arch] { |
| 417 | variants = append(variants, proptools.FieldNameForProperty(archVariant)) |
| 418 | } |
| 419 | for _, feature := range archFeatures[arch] { |
| 420 | variants = append(variants, proptools.FieldNameForProperty(feature)) |
| 421 | } |
| 422 | |
| 423 | fields := variantFields(variants) |
| 424 | |
| 425 | fields = append([]reflect.StructField{reflect.StructField{ |
| 426 | Name: "BlueprintEmbed", |
| 427 | Type: props, |
| 428 | Anonymous: true, |
| 429 | }}, fields...) |
| 430 | |
| 431 | archFields[i] = reflect.StructField{ |
| 432 | Name: arch.Field, |
| 433 | Type: reflect.StructOf(fields), |
| 434 | } |
| 435 | } |
| 436 | archType := reflect.StructOf(archFields) |
| 437 | |
| 438 | multilibType := reflect.StructOf(variantFields([]string{"Lib32", "Lib64"})) |
| 439 | |
| 440 | targets := []string{ |
| 441 | "Host", |
| 442 | "Android64", |
| 443 | "Android32", |
| 444 | "Not_windows", |
| 445 | } |
| 446 | for _, os := range osTypeList { |
| 447 | targets = append(targets, os.Field) |
| 448 | |
| 449 | for _, archType := range osArchTypeMap[os] { |
| 450 | targets = append(targets, os.Field+"_"+archType.Name) |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | targetType := reflect.StructOf(variantFields(targets)) |
| 455 | return reflect.StructOf([]reflect.StructField{ |
| 456 | reflect.StructField{ |
| 457 | Name: "Arch", |
| 458 | Type: archType, |
| 459 | }, |
| 460 | reflect.StructField{ |
| 461 | Name: "Multilib", |
| 462 | Type: multilibType, |
| 463 | }, |
| 464 | reflect.StructField{ |
| 465 | Name: "Target", |
| 466 | Type: targetType, |
| 467 | }, |
| 468 | }) |
| 469 | } |
| 470 | |
| 471 | var archPropTypeMap OncePer |
| 472 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 473 | func InitArchModule(m Module, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 474 | propertyStructs ...interface{}) (blueprint.Module, []interface{}) { |
| 475 | |
| 476 | base := m.base() |
| 477 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 478 | base.generalProperties = append(base.generalProperties, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 479 | propertyStructs...) |
| 480 | |
| 481 | for _, properties := range base.generalProperties { |
| 482 | propertiesValue := reflect.ValueOf(properties) |
Colin Cross | 62496a0 | 2016-08-08 15:49:17 -0700 | [diff] [blame] | 483 | t := propertiesValue.Type() |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 484 | if propertiesValue.Kind() != reflect.Ptr { |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 485 | panic(fmt.Errorf("properties must be a pointer to a struct, got %T", |
| 486 | propertiesValue.Interface())) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | propertiesValue = propertiesValue.Elem() |
| 490 | if propertiesValue.Kind() != reflect.Struct { |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 491 | panic(fmt.Errorf("properties must be a pointer to a struct, got %T", |
| 492 | propertiesValue.Interface())) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 493 | } |
| 494 | |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 495 | archPropType := archPropTypeMap.Once(t, func() interface{} { |
| 496 | return createArchType(t) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 497 | }) |
| 498 | |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 499 | if archPropType != nil { |
| 500 | base.archProperties = append(base.archProperties, reflect.New(archPropType.(reflect.Type)).Interface()) |
| 501 | } else { |
| 502 | base.archProperties = append(base.archProperties, nil) |
| 503 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | var allProperties []interface{} |
| 507 | allProperties = append(allProperties, base.generalProperties...) |
| 508 | for _, asp := range base.archProperties { |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 509 | if asp != nil { |
| 510 | allProperties = append(allProperties, asp) |
| 511 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 512 | } |
| 513 | |
Colin Cross | a120ec1 | 2016-08-19 16:07:38 -0700 | [diff] [blame] | 514 | base.customizableProperties = allProperties |
| 515 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 516 | return m, allProperties |
| 517 | } |
| 518 | |
Colin Cross | a716add | 2015-12-16 11:07:39 -0800 | [diff] [blame] | 519 | var variantReplacer = strings.NewReplacer("-", "_", ".", "_") |
Colin Cross | ec19363 | 2015-07-06 17:49:43 -0700 | [diff] [blame] | 520 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 521 | func (a *ModuleBase) appendProperties(ctx BottomUpMutatorContext, |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 522 | dst interface{}, src reflect.Value, field, srcPrefix string) reflect.Value { |
Colin Cross | 06a931b | 2015-10-28 17:23:31 -0700 | [diff] [blame] | 523 | |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 524 | src = src.FieldByName(field) |
| 525 | if !src.IsValid() { |
Colin Cross | eeabb89 | 2015-11-20 13:07:51 -0800 | [diff] [blame] | 526 | ctx.ModuleErrorf("field %q does not exist", srcPrefix) |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 527 | return src |
Colin Cross | 85a8897 | 2015-11-23 13:29:51 -0800 | [diff] [blame] | 528 | } |
| 529 | |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 530 | ret := src |
Colin Cross | 85a8897 | 2015-11-23 13:29:51 -0800 | [diff] [blame] | 531 | |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 532 | if src.Kind() == reflect.Struct { |
| 533 | src = src.FieldByName("BlueprintEmbed") |
Colin Cross | 06a931b | 2015-10-28 17:23:31 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Colin Cross | 6ee75b6 | 2016-05-05 15:57:15 -0700 | [diff] [blame] | 536 | order := func(property string, |
| 537 | dstField, srcField reflect.StructField, |
| 538 | dstValue, srcValue interface{}) (proptools.Order, error) { |
| 539 | if proptools.HasTag(dstField, "android", "variant_prepend") { |
| 540 | return proptools.Prepend, nil |
| 541 | } else { |
| 542 | return proptools.Append, nil |
| 543 | } |
| 544 | } |
| 545 | |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 546 | err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, order) |
Colin Cross | 06a931b | 2015-10-28 17:23:31 -0700 | [diff] [blame] | 547 | if err != nil { |
| 548 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { |
| 549 | ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) |
| 550 | } else { |
| 551 | panic(err) |
| 552 | } |
| 553 | } |
Colin Cross | 85a8897 | 2015-11-23 13:29:51 -0800 | [diff] [blame] | 554 | |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 555 | return ret |
Colin Cross | 06a931b | 2015-10-28 17:23:31 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 558 | // Rewrite the module's properties structs to contain arch-specific values. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 559 | func (a *ModuleBase) setArchProperties(ctx BottomUpMutatorContext) { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 560 | arch := a.Arch() |
| 561 | os := a.Os() |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame] | 562 | |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 563 | if arch.ArchType == Common { |
| 564 | return |
| 565 | } |
| 566 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 567 | for i := range a.generalProperties { |
Colin Cross | 06a931b | 2015-10-28 17:23:31 -0700 | [diff] [blame] | 568 | genProps := a.generalProperties[i] |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 569 | if a.archProperties[i] == nil { |
| 570 | continue |
| 571 | } |
| 572 | archProps := reflect.ValueOf(a.archProperties[i]).Elem() |
| 573 | |
| 574 | archProp := archProps.FieldByName("Arch") |
| 575 | multilibProp := archProps.FieldByName("Multilib") |
| 576 | targetProp := archProps.FieldByName("Target") |
| 577 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 578 | // Handle arch-specific properties in the form: |
Colin Cross | b05bff2 | 2015-04-30 15:08:04 -0700 | [diff] [blame] | 579 | // arch: { |
| 580 | // arm64: { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 581 | // key: value, |
| 582 | // }, |
| 583 | // }, |
| 584 | t := arch.ArchType |
Colin Cross | 06a931b | 2015-10-28 17:23:31 -0700 | [diff] [blame] | 585 | |
Colin Cross | ec19363 | 2015-07-06 17:49:43 -0700 | [diff] [blame] | 586 | field := proptools.FieldNameForProperty(t.Name) |
Colin Cross | 06a931b | 2015-10-28 17:23:31 -0700 | [diff] [blame] | 587 | prefix := "arch." + t.Name |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 588 | archStruct := a.appendProperties(ctx, genProps, archProp, field, prefix) |
Colin Cross | ec19363 | 2015-07-06 17:49:43 -0700 | [diff] [blame] | 589 | |
| 590 | // Handle arch-variant-specific properties in the form: |
| 591 | // arch: { |
| 592 | // variant: { |
| 593 | // key: value, |
| 594 | // }, |
| 595 | // }, |
Colin Cross | a716add | 2015-12-16 11:07:39 -0800 | [diff] [blame] | 596 | v := variantReplacer.Replace(arch.ArchVariant) |
Colin Cross | ec19363 | 2015-07-06 17:49:43 -0700 | [diff] [blame] | 597 | if v != "" { |
| 598 | field := proptools.FieldNameForProperty(v) |
Colin Cross | 85a8897 | 2015-11-23 13:29:51 -0800 | [diff] [blame] | 599 | prefix := "arch." + t.Name + "." + v |
| 600 | a.appendProperties(ctx, genProps, archStruct, field, prefix) |
Colin Cross | ec19363 | 2015-07-06 17:49:43 -0700 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | // Handle cpu-variant-specific properties in the form: |
| 604 | // arch: { |
| 605 | // variant: { |
| 606 | // key: value, |
| 607 | // }, |
| 608 | // }, |
Colin Cross | a716add | 2015-12-16 11:07:39 -0800 | [diff] [blame] | 609 | c := variantReplacer.Replace(arch.CpuVariant) |
Colin Cross | ec19363 | 2015-07-06 17:49:43 -0700 | [diff] [blame] | 610 | if c != "" { |
| 611 | field := proptools.FieldNameForProperty(c) |
Colin Cross | 85a8897 | 2015-11-23 13:29:51 -0800 | [diff] [blame] | 612 | prefix := "arch." + t.Name + "." + c |
| 613 | a.appendProperties(ctx, genProps, archStruct, field, prefix) |
Colin Cross | ec19363 | 2015-07-06 17:49:43 -0700 | [diff] [blame] | 614 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 615 | |
Colin Cross | c5c24ad | 2015-11-20 15:35:00 -0800 | [diff] [blame] | 616 | // Handle arch-feature-specific properties in the form: |
| 617 | // arch: { |
| 618 | // feature: { |
| 619 | // key: value, |
| 620 | // }, |
| 621 | // }, |
| 622 | for _, feature := range arch.ArchFeatures { |
| 623 | field := proptools.FieldNameForProperty(feature) |
Colin Cross | 85a8897 | 2015-11-23 13:29:51 -0800 | [diff] [blame] | 624 | prefix := "arch." + t.Name + "." + feature |
| 625 | a.appendProperties(ctx, genProps, archStruct, field, prefix) |
Colin Cross | c5c24ad | 2015-11-20 15:35:00 -0800 | [diff] [blame] | 626 | } |
| 627 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 628 | // Handle multilib-specific properties in the form: |
Colin Cross | b05bff2 | 2015-04-30 15:08:04 -0700 | [diff] [blame] | 629 | // multilib: { |
| 630 | // lib32: { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 631 | // key: value, |
| 632 | // }, |
| 633 | // }, |
Colin Cross | 06a931b | 2015-10-28 17:23:31 -0700 | [diff] [blame] | 634 | field = proptools.FieldNameForProperty(t.Multilib) |
| 635 | prefix = "multilib." + t.Multilib |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 636 | a.appendProperties(ctx, genProps, multilibProp, field, prefix) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 637 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 638 | // Handle host-specific properties in the form: |
Colin Cross | b05bff2 | 2015-04-30 15:08:04 -0700 | [diff] [blame] | 639 | // target: { |
| 640 | // host: { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 641 | // key: value, |
| 642 | // }, |
| 643 | // }, |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 644 | if os.Class == Host || os.Class == HostCross { |
| 645 | field = "Host" |
| 646 | prefix = "target.host" |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 647 | a.appendProperties(ctx, genProps, targetProp, field, prefix) |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 648 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 649 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 650 | // Handle target OS properties in the form: |
Colin Cross | b05bff2 | 2015-04-30 15:08:04 -0700 | [diff] [blame] | 651 | // target: { |
| 652 | // linux: { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 653 | // key: value, |
| 654 | // }, |
Colin Cross | b05bff2 | 2015-04-30 15:08:04 -0700 | [diff] [blame] | 655 | // not_windows: { |
| 656 | // key: value, |
| 657 | // }, |
| 658 | // linux_x86: { |
| 659 | // key: value, |
| 660 | // }, |
| 661 | // linux_arm: { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 662 | // key: value, |
| 663 | // }, |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 664 | // android { |
| 665 | // key: value, |
| 666 | // }, |
| 667 | // android_arm { |
| 668 | // key: value, |
| 669 | // }, |
| 670 | // android_x86 { |
| 671 | // key: value, |
| 672 | // }, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 673 | // }, |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 674 | // }, |
| 675 | field = os.Field |
| 676 | prefix = "target." + os.Name |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 677 | a.appendProperties(ctx, genProps, targetProp, field, prefix) |
Colin Cross | b9db480 | 2016-06-03 01:50:47 +0000 | [diff] [blame] | 678 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 679 | field = os.Field + "_" + t.Name |
| 680 | prefix = "target." + os.Name + "_" + t.Name |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 681 | a.appendProperties(ctx, genProps, targetProp, field, prefix) |
Colin Cross | b9db480 | 2016-06-03 01:50:47 +0000 | [diff] [blame] | 682 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 683 | if (os.Class == Host || os.Class == HostCross) && os != Windows { |
| 684 | field := "Not_windows" |
| 685 | prefix := "target.not_windows" |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 686 | a.appendProperties(ctx, genProps, targetProp, field, prefix) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 687 | } |
| 688 | |
Colin Cross | f820941 | 2015-03-26 14:44:26 -0700 | [diff] [blame] | 689 | // Handle 64-bit device properties in the form: |
| 690 | // target { |
| 691 | // android64 { |
| 692 | // key: value, |
| 693 | // }, |
| 694 | // android32 { |
| 695 | // key: value, |
| 696 | // }, |
| 697 | // }, |
| 698 | // WARNING: this is probably not what you want to use in your blueprints file, it selects |
| 699 | // options for all targets on a device that supports 64-bit binaries, not just the targets |
| 700 | // that are being compiled for 64-bit. Its expected use case is binaries like linker and |
| 701 | // debuggerd that need to know when they are a 32-bit process running on a 64-bit device |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 702 | if os.Class == Device { |
| 703 | if ctx.AConfig().Android64() { |
Colin Cross | 06a931b | 2015-10-28 17:23:31 -0700 | [diff] [blame] | 704 | field := "Android64" |
| 705 | prefix := "target.android64" |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 706 | a.appendProperties(ctx, genProps, targetProp, field, prefix) |
Colin Cross | f820941 | 2015-03-26 14:44:26 -0700 | [diff] [blame] | 707 | } else { |
Colin Cross | 06a931b | 2015-10-28 17:23:31 -0700 | [diff] [blame] | 708 | field := "Android32" |
| 709 | prefix := "target.android32" |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 710 | a.appendProperties(ctx, genProps, targetProp, field, prefix) |
Colin Cross | f820941 | 2015-03-26 14:44:26 -0700 | [diff] [blame] | 711 | } |
| 712 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 713 | } |
| 714 | } |
| 715 | |
| 716 | func forEachInterface(v reflect.Value, f func(reflect.Value)) { |
| 717 | switch v.Kind() { |
| 718 | case reflect.Interface: |
| 719 | f(v) |
| 720 | case reflect.Struct: |
| 721 | for i := 0; i < v.NumField(); i++ { |
| 722 | forEachInterface(v.Field(i), f) |
| 723 | } |
| 724 | case reflect.Ptr: |
| 725 | forEachInterface(v.Elem(), f) |
| 726 | default: |
| 727 | panic(fmt.Errorf("Unsupported kind %s", v.Kind())) |
| 728 | } |
| 729 | } |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 730 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 731 | // Convert the arch product variables into a list of targets for each os class structs |
Colin Cross | 9272ade | 2016-08-17 15:24:12 -0700 | [diff] [blame] | 732 | func decodeTargetProductVariables(config *config) (map[OsClass][]Target, error) { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 733 | variables := config.ProductVariables |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 734 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 735 | targets := make(map[OsClass][]Target) |
| 736 | var targetErr error |
| 737 | |
| 738 | addTarget := func(os OsType, archName string, archVariant, cpuVariant *string, abi *[]string) { |
| 739 | if targetErr != nil { |
| 740 | return |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 741 | } |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 742 | |
| 743 | arch, err := decodeArch(archName, archVariant, cpuVariant, abi) |
| 744 | if err != nil { |
| 745 | targetErr = err |
| 746 | return |
| 747 | } |
| 748 | |
| 749 | targets[os.Class] = append(targets[os.Class], |
| 750 | Target{ |
| 751 | Os: os, |
| 752 | Arch: arch, |
| 753 | }) |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 754 | } |
| 755 | |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 756 | if variables.HostArch == nil { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 757 | return nil, fmt.Errorf("No host primary architecture set") |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 758 | } |
| 759 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 760 | addTarget(BuildOs, *variables.HostArch, nil, nil, nil) |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 761 | |
Colin Cross | eeabb89 | 2015-11-20 13:07:51 -0800 | [diff] [blame] | 762 | if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 763 | addTarget(BuildOs, *variables.HostSecondaryArch, nil, nil, nil) |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | if variables.CrossHost != nil && *variables.CrossHost != "" { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 767 | crossHostOs := osByName(*variables.CrossHost) |
| 768 | if crossHostOs == NoOsType { |
| 769 | return nil, fmt.Errorf("Unknown cross host OS %q", *variables.CrossHost) |
| 770 | } |
| 771 | |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 772 | if variables.CrossHostArch == nil || *variables.CrossHostArch == "" { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 773 | return nil, fmt.Errorf("No cross-host primary architecture set") |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 774 | } |
| 775 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 776 | addTarget(crossHostOs, *variables.CrossHostArch, nil, nil, nil) |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 777 | |
| 778 | if variables.CrossHostSecondaryArch != nil && *variables.CrossHostSecondaryArch != "" { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 779 | addTarget(crossHostOs, *variables.CrossHostSecondaryArch, nil, nil, nil) |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | |
Dan Willemsen | 3f32f03 | 2016-07-11 14:36:48 -0700 | [diff] [blame] | 783 | if variables.DeviceArch != nil && *variables.DeviceArch != "" { |
| 784 | addTarget(Android, *variables.DeviceArch, variables.DeviceArchVariant, |
| 785 | variables.DeviceCpuVariant, variables.DeviceAbi) |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 786 | |
Dan Willemsen | 3f32f03 | 2016-07-11 14:36:48 -0700 | [diff] [blame] | 787 | if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" { |
| 788 | addTarget(Android, *variables.DeviceSecondaryArch, |
| 789 | variables.DeviceSecondaryArchVariant, variables.DeviceSecondaryCpuVariant, |
| 790 | variables.DeviceSecondaryAbi) |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 791 | |
Dan Willemsen | 3f32f03 | 2016-07-11 14:36:48 -0700 | [diff] [blame] | 792 | deviceArches := targets[Device] |
| 793 | if deviceArches[0].Arch.ArchType.Multilib == deviceArches[1].Arch.ArchType.Multilib { |
| 794 | deviceArches[1].Arch.Native = false |
| 795 | } |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 796 | } |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 797 | } |
| 798 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 799 | if targetErr != nil { |
| 800 | return nil, targetErr |
| 801 | } |
| 802 | |
| 803 | return targets, nil |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 804 | } |
| 805 | |
Dan Albert | 4098deb | 2016-10-19 14:04:41 -0700 | [diff] [blame] | 806 | type archConfig struct { |
| 807 | arch string |
| 808 | archVariant string |
| 809 | cpuVariant string |
| 810 | abi []string |
| 811 | } |
| 812 | |
| 813 | func getMegaDeviceConfig() []archConfig { |
| 814 | return []archConfig{ |
Dan Willemsen | 110a89d | 2016-01-14 15:17:19 -0800 | [diff] [blame] | 815 | // armv5 is only used for unbundled apps |
| 816 | //{"arm", "armv5te", "", []string{"armeabi"}}, |
| 817 | {"arm", "armv7-a", "generic", []string{"armeabi-v7a"}}, |
| 818 | {"arm", "armv7-a-neon", "generic", []string{"armeabi-v7a"}}, |
Dan Willemsen | 322acaf | 2016-01-12 23:07:05 -0800 | [diff] [blame] | 819 | {"arm", "armv7-a-neon", "cortex-a7", []string{"armeabi-v7a"}}, |
| 820 | {"arm", "armv7-a-neon", "cortex-a8", []string{"armeabi-v7a"}}, |
Dan Willemsen | 110a89d | 2016-01-14 15:17:19 -0800 | [diff] [blame] | 821 | {"arm", "armv7-a-neon", "cortex-a9", []string{"armeabi-v7a"}}, |
Dan Willemsen | 322acaf | 2016-01-12 23:07:05 -0800 | [diff] [blame] | 822 | {"arm", "armv7-a-neon", "cortex-a15", []string{"armeabi-v7a"}}, |
| 823 | {"arm", "armv7-a-neon", "cortex-a53", []string{"armeabi-v7a"}}, |
| 824 | {"arm", "armv7-a-neon", "cortex-a53.a57", []string{"armeabi-v7a"}}, |
| 825 | {"arm", "armv7-a-neon", "denver", []string{"armeabi-v7a"}}, |
| 826 | {"arm", "armv7-a-neon", "krait", []string{"armeabi-v7a"}}, |
Dan Willemsen | 110a89d | 2016-01-14 15:17:19 -0800 | [diff] [blame] | 827 | {"arm64", "armv8-a", "cortex-a53", []string{"arm64-v8a"}}, |
| 828 | {"arm64", "armv8-a", "denver64", []string{"arm64-v8a"}}, |
Dan Willemsen | 468cc31 | 2016-01-13 23:25:19 -0800 | [diff] [blame] | 829 | {"mips", "mips32-fp", "", []string{"mips"}}, |
| 830 | {"mips", "mips32r2-fp", "", []string{"mips"}}, |
| 831 | {"mips", "mips32r2-fp-xburst", "", []string{"mips"}}, |
Dan Willemsen | 65fb981 | 2016-07-19 21:37:28 -0700 | [diff] [blame] | 832 | //{"mips", "mips32r6", "", []string{"mips"}}, |
Dan Willemsen | 468cc31 | 2016-01-13 23:25:19 -0800 | [diff] [blame] | 833 | // mips32r2dsp[r2]-fp fails in the assembler for divdf3.c in compiler-rt: |
| 834 | // (same errors in make and soong) |
| 835 | // Error: invalid operands `mtlo $ac0,$11' |
| 836 | // Error: invalid operands `mthi $ac0,$12' |
Dan Willemsen | 322acaf | 2016-01-12 23:07:05 -0800 | [diff] [blame] | 837 | //{"mips", "mips32r2dsp-fp", "", []string{"mips"}}, |
| 838 | //{"mips", "mips32r2dspr2-fp", "", []string{"mips"}}, |
| 839 | // mips64r2 is mismatching 64r2 and 64r6 libraries during linking to libgcc |
| 840 | //{"mips64", "mips64r2", "", []string{"mips64"}}, |
| 841 | {"mips64", "mips64r6", "", []string{"mips64"}}, |
| 842 | {"x86", "", "", []string{"x86"}}, |
| 843 | {"x86", "atom", "", []string{"x86"}}, |
| 844 | {"x86", "haswell", "", []string{"x86"}}, |
| 845 | {"x86", "ivybridge", "", []string{"x86"}}, |
| 846 | {"x86", "sandybridge", "", []string{"x86"}}, |
| 847 | {"x86", "silvermont", "", []string{"x86"}}, |
Dan Willemsen | 8a35405 | 2016-05-10 14:30:51 -0700 | [diff] [blame] | 848 | {"x86", "x86_64", "", []string{"x86"}}, |
Dan Willemsen | 322acaf | 2016-01-12 23:07:05 -0800 | [diff] [blame] | 849 | {"x86_64", "", "", []string{"x86_64"}}, |
| 850 | {"x86_64", "haswell", "", []string{"x86_64"}}, |
| 851 | {"x86_64", "ivybridge", "", []string{"x86_64"}}, |
| 852 | {"x86_64", "sandybridge", "", []string{"x86_64"}}, |
| 853 | {"x86_64", "silvermont", "", []string{"x86_64"}}, |
| 854 | } |
Dan Albert | 4098deb | 2016-10-19 14:04:41 -0700 | [diff] [blame] | 855 | } |
Dan Willemsen | 322acaf | 2016-01-12 23:07:05 -0800 | [diff] [blame] | 856 | |
Dan Albert | 4098deb | 2016-10-19 14:04:41 -0700 | [diff] [blame] | 857 | func getNdkAbisConfig() []archConfig { |
| 858 | return []archConfig{ |
| 859 | {"arm", "armv5te", "", []string{"armeabi"}}, |
| 860 | {"arm64", "armv8-a", "", []string{"arm64-v8a"}}, |
| 861 | {"mips", "mips32-fp", "", []string{"mips"}}, |
| 862 | {"mips64", "mips64r6", "", []string{"mips64"}}, |
| 863 | {"x86", "", "", []string{"x86"}}, |
| 864 | {"x86_64", "", "", []string{"x86_64"}}, |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | func decodeArchSettings(archConfigs []archConfig) ([]Target, error) { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 869 | var ret []Target |
Dan Willemsen | 322acaf | 2016-01-12 23:07:05 -0800 | [diff] [blame] | 870 | |
Dan Albert | 4098deb | 2016-10-19 14:04:41 -0700 | [diff] [blame] | 871 | for _, config := range archConfigs { |
Dan Willemsen | 322acaf | 2016-01-12 23:07:05 -0800 | [diff] [blame] | 872 | arch, err := decodeArch(config.arch, &config.archVariant, |
| 873 | &config.cpuVariant, &config.abi) |
| 874 | if err != nil { |
| 875 | return nil, err |
| 876 | } |
Dan Willemsen | 17f0526 | 2016-05-31 16:27:00 -0700 | [diff] [blame] | 877 | arch.Native = false |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 878 | ret = append(ret, Target{ |
| 879 | Os: Android, |
| 880 | Arch: arch, |
| 881 | }) |
Dan Willemsen | 322acaf | 2016-01-12 23:07:05 -0800 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | return ret, nil |
| 885 | } |
| 886 | |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 887 | // Convert a set of strings from product variables into a single Arch struct |
| 888 | func decodeArch(arch string, archVariant, cpuVariant *string, abi *[]string) (Arch, error) { |
| 889 | stringPtr := func(p *string) string { |
| 890 | if p != nil { |
| 891 | return *p |
| 892 | } |
| 893 | return "" |
| 894 | } |
| 895 | |
| 896 | slicePtr := func(p *[]string) []string { |
| 897 | if p != nil { |
| 898 | return *p |
| 899 | } |
| 900 | return nil |
| 901 | } |
| 902 | |
Colin Cross | eeabb89 | 2015-11-20 13:07:51 -0800 | [diff] [blame] | 903 | archType, ok := archTypeMap[arch] |
| 904 | if !ok { |
| 905 | return Arch{}, fmt.Errorf("unknown arch %q", arch) |
| 906 | } |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 907 | |
Colin Cross | eeabb89 | 2015-11-20 13:07:51 -0800 | [diff] [blame] | 908 | a := Arch{ |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 909 | ArchType: archType, |
| 910 | ArchVariant: stringPtr(archVariant), |
| 911 | CpuVariant: stringPtr(cpuVariant), |
| 912 | Abi: slicePtr(abi), |
Dan Willemsen | 17f0526 | 2016-05-31 16:27:00 -0700 | [diff] [blame] | 913 | Native: true, |
Colin Cross | eeabb89 | 2015-11-20 13:07:51 -0800 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | if a.ArchVariant == a.ArchType.Name || a.ArchVariant == "generic" { |
| 917 | a.ArchVariant = "" |
| 918 | } |
| 919 | |
| 920 | if a.CpuVariant == a.ArchType.Name || a.CpuVariant == "generic" { |
| 921 | a.CpuVariant = "" |
| 922 | } |
| 923 | |
| 924 | for i := 0; i < len(a.Abi); i++ { |
| 925 | if a.Abi[i] == "" { |
| 926 | a.Abi = append(a.Abi[:i], a.Abi[i+1:]...) |
| 927 | i-- |
| 928 | } |
| 929 | } |
| 930 | |
Colin Cross | c5c24ad | 2015-11-20 15:35:00 -0800 | [diff] [blame] | 931 | if featureMap, ok := archFeatureMap[archType]; ok { |
Dan Willemsen | b485099 | 2016-05-06 17:21:20 -0700 | [diff] [blame] | 932 | a.ArchFeatures = featureMap[a.ArchVariant] |
Colin Cross | c5c24ad | 2015-11-20 15:35:00 -0800 | [diff] [blame] | 933 | } |
| 934 | |
Colin Cross | eeabb89 | 2015-11-20 13:07:51 -0800 | [diff] [blame] | 935 | return a, nil |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 936 | } |
| 937 | |
Colin Cross | 69617d3 | 2016-09-06 10:39:07 -0700 | [diff] [blame] | 938 | func filterMultilibTargets(targets []Target, multilib string) []Target { |
| 939 | var ret []Target |
| 940 | for _, t := range targets { |
| 941 | if t.Arch.ArchType.Multilib == multilib { |
| 942 | ret = append(ret, t) |
| 943 | } |
| 944 | } |
| 945 | return ret |
| 946 | } |
| 947 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 948 | // Use the module multilib setting to select one or more targets from a target list |
Colin Cross | 8b74d17 | 2016-09-13 09:59:14 -0700 | [diff] [blame] | 949 | func decodeMultilib(multilib string, targets []Target, prefer32 bool) ([]Target, error) { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 950 | buildTargets := []Target{} |
Colin Cross | 8b74d17 | 2016-09-13 09:59:14 -0700 | [diff] [blame] | 951 | if multilib == "first" { |
| 952 | if prefer32 { |
| 953 | multilib = "prefer32" |
| 954 | } else { |
| 955 | multilib = "prefer64" |
| 956 | } |
| 957 | } |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 958 | switch multilib { |
| 959 | case "common": |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 960 | buildTargets = append(buildTargets, commonTarget) |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 961 | case "both": |
Colin Cross | 8b74d17 | 2016-09-13 09:59:14 -0700 | [diff] [blame] | 962 | if prefer32 { |
| 963 | buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...) |
| 964 | buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...) |
| 965 | } else { |
| 966 | buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...) |
| 967 | buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...) |
| 968 | } |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 969 | case "32": |
Colin Cross | 69617d3 | 2016-09-06 10:39:07 -0700 | [diff] [blame] | 970 | buildTargets = filterMultilibTargets(targets, "lib32") |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 971 | case "64": |
Colin Cross | 69617d3 | 2016-09-06 10:39:07 -0700 | [diff] [blame] | 972 | buildTargets = filterMultilibTargets(targets, "lib64") |
| 973 | case "prefer32": |
| 974 | buildTargets = filterMultilibTargets(targets, "lib32") |
| 975 | if len(buildTargets) == 0 { |
| 976 | buildTargets = filterMultilibTargets(targets, "lib64") |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 977 | } |
Colin Cross | 8b74d17 | 2016-09-13 09:59:14 -0700 | [diff] [blame] | 978 | case "prefer64": |
| 979 | buildTargets = filterMultilibTargets(targets, "lib64") |
| 980 | if len(buildTargets) == 0 { |
| 981 | buildTargets = filterMultilibTargets(targets, "lib32") |
| 982 | } |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 983 | default: |
Colin Cross | 69617d3 | 2016-09-06 10:39:07 -0700 | [diff] [blame] | 984 | return nil, fmt.Errorf(`compile_multilib must be "both", "first", "32", "64", or "prefer32" found %q`, |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 985 | multilib) |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 986 | } |
| 987 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 988 | return buildTargets, nil |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 989 | } |