blob: 6552570765465af7a04f4cf622395b1d3ec4932a [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// 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 Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross3f40fa42015-01-30 17:27:36 -080016
17import (
Colin Cross74ba9622019-02-11 15:11:14 -080018 "encoding"
Colin Cross3f40fa42015-01-30 17:27:36 -080019 "fmt"
20 "reflect"
21 "runtime"
Colin Cross74ba9622019-02-11 15:11:14 -080022 "strconv"
Colin Cross3f40fa42015-01-30 17:27:36 -080023 "strings"
Colin Crossf6566ed2015-03-24 11:13:38 -070024
Colin Cross0f7d2ef2019-10-16 11:03:10 -070025 "github.com/google/blueprint"
Colin Cross617b88a2020-08-24 18:04:09 -070026 "github.com/google/blueprint/bootstrap"
Colin Crossf6566ed2015-03-24 11:13:38 -070027 "github.com/google/blueprint/proptools"
Colin Cross3f40fa42015-01-30 17:27:36 -080028)
29
Colin Cross0f7d2ef2019-10-16 11:03:10 -070030const COMMON_VARIANT = "common"
31
Colin Cross3f40fa42015-01-30 17:27:36 -080032var (
Dan Willemsenb1957a52016-06-23 23:44:54 -070033 archTypeList []ArchType
34
Colin Crossec193632015-07-06 17:49:43 -070035 Arm = newArch("arm", "lib32")
36 Arm64 = newArch("arm64", "lib64")
Colin Crossec193632015-07-06 17:49:43 -070037 X86 = newArch("x86", "lib32")
38 X86_64 = newArch("x86_64", "lib64")
Colin Cross2fe66872015-03-30 17:20:39 -070039
40 Common = ArchType{
Colin Cross0f7d2ef2019-10-16 11:03:10 -070041 Name: COMMON_VARIANT,
Colin Cross2fe66872015-03-30 17:20:39 -070042 }
Colin Cross3f40fa42015-01-30 17:27:36 -080043)
44
Colin Cross4225f652015-09-17 14:33:42 -070045var archTypeMap = map[string]ArchType{
46 "arm": Arm,
47 "arm64": Arm64,
Colin Cross4225f652015-09-17 14:33:42 -070048 "x86": X86,
49 "x86_64": X86_64,
50}
51
Colin Cross3f40fa42015-01-30 17:27:36 -080052/*
53Example blueprints file containing all variant property groups, with comment listing what type
54of variants get properties in that group:
55
56module {
57 arch: {
58 arm: {
59 // Host or device variants with arm architecture
60 },
61 arm64: {
62 // Host or device variants with arm64 architecture
63 },
Colin Cross3f40fa42015-01-30 17:27:36 -080064 x86: {
65 // Host or device variants with x86 architecture
66 },
67 x86_64: {
68 // Host or device variants with x86_64 architecture
69 },
70 },
71 multilib: {
72 lib32: {
73 // Host or device variants for 32-bit architectures
74 },
75 lib64: {
76 // Host or device variants for 64-bit architectures
77 },
78 },
79 target: {
80 android: {
81 // Device variants
82 },
83 host: {
84 // Host variants
85 },
Dan Willemsen5746bd42017-10-02 19:42:01 -070086 linux_glibc: {
Colin Cross3f40fa42015-01-30 17:27:36 -080087 // Linux host variants
88 },
89 darwin: {
90 // Darwin host variants
91 },
92 windows: {
93 // Windows host variants
94 },
95 not_windows: {
96 // Non-windows host variants
97 },
98 },
99}
100*/
Colin Cross7d5136f2015-05-11 13:39:40 -0700101
Jaewoong Junge46114c2019-01-16 14:33:13 -0800102var archVariants = map[ArchType][]string{
103 Arm: {
Dan Albert8818f492019-02-19 13:53:01 -0800104 "armv7-a",
Jaewoong Junge46114c2019-01-16 14:33:13 -0800105 "armv7-a-neon",
106 "armv8-a",
107 "armv8-2a",
108 "cortex-a7",
109 "cortex-a8",
110 "cortex-a9",
111 "cortex-a15",
112 "cortex-a53",
113 "cortex-a53-a57",
114 "cortex-a55",
115 "cortex-a72",
116 "cortex-a73",
117 "cortex-a75",
118 "cortex-a76",
119 "krait",
120 "kryo",
121 "kryo385",
122 "exynos-m1",
123 "exynos-m2",
124 },
125 Arm64: {
126 "armv8_a",
127 "armv8_2a",
Raphael Gault70b96b02020-06-18 09:56:53 +0000128 "armv8-2a-dotprod",
Jaewoong Junge46114c2019-01-16 14:33:13 -0800129 "cortex-a53",
130 "cortex-a55",
131 "cortex-a72",
132 "cortex-a73",
133 "cortex-a75",
134 "cortex-a76",
135 "kryo",
136 "kryo385",
137 "exynos-m1",
138 "exynos-m2",
139 },
Jaewoong Junge46114c2019-01-16 14:33:13 -0800140 X86: {
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530141 "amberlake",
Jaewoong Junge46114c2019-01-16 14:33:13 -0800142 "atom",
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530143 "broadwell",
Jaewoong Junge46114c2019-01-16 14:33:13 -0800144 "haswell",
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530145 "icelake",
Jaewoong Junge46114c2019-01-16 14:33:13 -0800146 "ivybridge",
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530147 "kabylake",
Jaewoong Junge46114c2019-01-16 14:33:13 -0800148 "sandybridge",
149 "silvermont",
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530150 "skylake",
Benjamin Gordon87e7f2f2019-02-14 10:59:48 -0700151 "stoneyridge",
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530152 "tigerlake",
153 "whiskeylake",
Jaewoong Junge46114c2019-01-16 14:33:13 -0800154 "x86_64",
155 },
156 X86_64: {
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530157 "amberlake",
158 "broadwell",
Jaewoong Junge46114c2019-01-16 14:33:13 -0800159 "haswell",
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530160 "icelake",
Jaewoong Junge46114c2019-01-16 14:33:13 -0800161 "ivybridge",
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530162 "kabylake",
Jaewoong Junge46114c2019-01-16 14:33:13 -0800163 "sandybridge",
164 "silvermont",
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530165 "skylake",
Benjamin Gordon87e7f2f2019-02-14 10:59:48 -0700166 "stoneyridge",
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530167 "tigerlake",
168 "whiskeylake",
Jaewoong Junge46114c2019-01-16 14:33:13 -0800169 },
170}
171
172var archFeatures = map[ArchType][]string{
173 Arm: {
174 "neon",
175 },
Raphael Gault70b96b02020-06-18 09:56:53 +0000176 Arm64: {
177 "dotprod",
178 },
Jaewoong Junge46114c2019-01-16 14:33:13 -0800179 X86: {
180 "ssse3",
181 "sse4",
182 "sse4_1",
183 "sse4_2",
184 "aes_ni",
185 "avx",
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530186 "avx2",
187 "avx512",
Jaewoong Junge46114c2019-01-16 14:33:13 -0800188 "popcnt",
189 "movbe",
190 },
191 X86_64: {
192 "ssse3",
193 "sse4",
194 "sse4_1",
195 "sse4_2",
196 "aes_ni",
197 "avx",
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530198 "avx2",
199 "avx512",
Jaewoong Junge46114c2019-01-16 14:33:13 -0800200 "popcnt",
201 },
202}
203
204var archFeatureMap = map[ArchType]map[string][]string{
205 Arm: {
206 "armv7-a-neon": {
207 "neon",
208 },
209 "armv8-a": {
210 "neon",
211 },
212 "armv8-2a": {
213 "neon",
214 },
215 },
Raphael Gault70b96b02020-06-18 09:56:53 +0000216 Arm64: {
217 "armv8-2a-dotprod": {
218 "dotprod",
219 },
220 },
Jaewoong Junge46114c2019-01-16 14:33:13 -0800221 X86: {
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530222 "amberlake": {
223 "ssse3",
224 "sse4",
225 "sse4_1",
226 "sse4_2",
227 "avx",
228 "avx2",
229 "aes_ni",
230 "popcnt",
231 },
Jaewoong Junge46114c2019-01-16 14:33:13 -0800232 "atom": {
233 "ssse3",
234 "movbe",
235 },
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530236 "broadwell": {
237 "ssse3",
238 "sse4",
239 "sse4_1",
240 "sse4_2",
241 "avx",
242 "avx2",
243 "aes_ni",
244 "popcnt",
245 },
Jaewoong Junge46114c2019-01-16 14:33:13 -0800246 "haswell": {
247 "ssse3",
248 "sse4",
249 "sse4_1",
250 "sse4_2",
251 "aes_ni",
252 "avx",
253 "popcnt",
254 "movbe",
255 },
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530256 "icelake": {
257 "ssse3",
258 "sse4",
259 "sse4_1",
260 "sse4_2",
261 "avx",
262 "avx2",
263 "avx512",
264 "aes_ni",
265 "popcnt",
266 },
Jaewoong Junge46114c2019-01-16 14:33:13 -0800267 "ivybridge": {
268 "ssse3",
269 "sse4",
270 "sse4_1",
271 "sse4_2",
272 "aes_ni",
273 "avx",
274 "popcnt",
275 },
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530276 "kabylake": {
277 "ssse3",
278 "sse4",
279 "sse4_1",
280 "sse4_2",
281 "avx",
282 "avx2",
283 "aes_ni",
284 "popcnt",
285 },
Jaewoong Junge46114c2019-01-16 14:33:13 -0800286 "sandybridge": {
287 "ssse3",
288 "sse4",
289 "sse4_1",
290 "sse4_2",
291 "popcnt",
292 },
293 "silvermont": {
294 "ssse3",
295 "sse4",
296 "sse4_1",
297 "sse4_2",
298 "aes_ni",
299 "popcnt",
300 "movbe",
301 },
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530302 "skylake": {
303 "ssse3",
304 "sse4",
305 "sse4_1",
306 "sse4_2",
307 "avx",
308 "avx2",
309 "avx512",
310 "aes_ni",
311 "popcnt",
312 },
Benjamin Gordon87e7f2f2019-02-14 10:59:48 -0700313 "stoneyridge": {
314 "ssse3",
315 "sse4",
316 "sse4_1",
317 "sse4_2",
318 "aes_ni",
319 "avx",
320 "avx2",
321 "popcnt",
322 "movbe",
323 },
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530324 "tigerlake": {
325 "ssse3",
326 "sse4",
327 "sse4_1",
328 "sse4_2",
329 "avx",
330 "avx2",
331 "avx512",
332 "aes_ni",
333 "popcnt",
334 },
335 "whiskeylake": {
336 "ssse3",
337 "sse4",
338 "sse4_1",
339 "sse4_2",
340 "avx",
341 "avx2",
342 "avx512",
343 "aes_ni",
344 "popcnt",
345 },
Jaewoong Junge46114c2019-01-16 14:33:13 -0800346 "x86_64": {
347 "ssse3",
348 "sse4",
349 "sse4_1",
350 "sse4_2",
351 "popcnt",
352 },
353 },
354 X86_64: {
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530355 "amberlake": {
356 "ssse3",
357 "sse4",
358 "sse4_1",
359 "sse4_2",
360 "avx",
361 "avx2",
362 "aes_ni",
363 "popcnt",
364 },
365 "broadwell": {
366 "ssse3",
367 "sse4",
368 "sse4_1",
369 "sse4_2",
370 "avx",
371 "avx2",
372 "aes_ni",
373 "popcnt",
374 },
Jaewoong Junge46114c2019-01-16 14:33:13 -0800375 "haswell": {
376 "ssse3",
377 "sse4",
378 "sse4_1",
379 "sse4_2",
380 "aes_ni",
381 "avx",
382 "popcnt",
383 },
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530384 "icelake": {
385 "ssse3",
386 "sse4",
387 "sse4_1",
388 "sse4_2",
389 "avx",
390 "avx2",
391 "avx512",
392 "aes_ni",
393 "popcnt",
394 },
Jaewoong Junge46114c2019-01-16 14:33:13 -0800395 "ivybridge": {
396 "ssse3",
397 "sse4",
398 "sse4_1",
399 "sse4_2",
400 "aes_ni",
401 "avx",
402 "popcnt",
403 },
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530404 "kabylake": {
405 "ssse3",
406 "sse4",
407 "sse4_1",
408 "sse4_2",
409 "avx",
410 "avx2",
411 "aes_ni",
412 "popcnt",
413 },
Jaewoong Junge46114c2019-01-16 14:33:13 -0800414 "sandybridge": {
415 "ssse3",
416 "sse4",
417 "sse4_1",
418 "sse4_2",
419 "popcnt",
420 },
421 "silvermont": {
422 "ssse3",
423 "sse4",
424 "sse4_1",
425 "sse4_2",
426 "aes_ni",
427 "popcnt",
428 },
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530429 "skylake": {
430 "ssse3",
431 "sse4",
432 "sse4_1",
433 "sse4_2",
434 "avx",
435 "avx2",
436 "avx512",
437 "aes_ni",
438 "popcnt",
439 },
Benjamin Gordon87e7f2f2019-02-14 10:59:48 -0700440 "stoneyridge": {
441 "ssse3",
442 "sse4",
443 "sse4_1",
444 "sse4_2",
445 "aes_ni",
446 "avx",
447 "avx2",
448 "popcnt",
449 },
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530450 "tigerlake": {
451 "ssse3",
452 "sse4",
453 "sse4_1",
454 "sse4_2",
455 "avx",
456 "avx2",
457 "avx512",
458 "aes_ni",
459 "popcnt",
460 },
461 "whiskeylake": {
462 "ssse3",
463 "sse4",
464 "sse4_1",
465 "sse4_2",
466 "avx",
467 "avx2",
468 "avx512",
469 "aes_ni",
470 "popcnt",
471 },
Jaewoong Junge46114c2019-01-16 14:33:13 -0800472 },
473}
474
Dan Willemsen01a3c252019-01-11 19:02:16 -0800475var defaultArchFeatureMap = map[OsType]map[ArchType][]string{}
Colin Crossc5c24ad2015-11-20 15:35:00 -0800476
Dan Willemsen01a3c252019-01-11 19:02:16 -0800477func RegisterDefaultArchVariantFeatures(os OsType, arch ArchType, features ...string) {
478 checkCalledFromInit()
479
480 for _, feature := range features {
481 if !InList(feature, archFeatures[arch]) {
482 panic(fmt.Errorf("Invalid feature %q for arch %q variant \"\"", feature, arch))
483 }
484 }
485
486 if defaultArchFeatureMap[os] == nil {
487 defaultArchFeatureMap[os] = make(map[ArchType][]string)
488 }
489 defaultArchFeatureMap[os][arch] = features
490}
491
Colin Cross3f40fa42015-01-30 17:27:36 -0800492// An Arch indicates a single CPU architecture.
493type Arch struct {
Colin Crossc5c24ad2015-11-20 15:35:00 -0800494 ArchType ArchType
495 ArchVariant string
496 CpuVariant string
497 Abi []string
498 ArchFeatures []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800499}
500
501func (a Arch) String() string {
Colin Crossd3ba0392015-05-07 14:11:29 -0700502 s := a.ArchType.String()
Colin Cross3f40fa42015-01-30 17:27:36 -0800503 if a.ArchVariant != "" {
504 s += "_" + a.ArchVariant
505 }
506 if a.CpuVariant != "" {
507 s += "_" + a.CpuVariant
508 }
509 return s
510}
511
512type ArchType struct {
Colin Crossec193632015-07-06 17:49:43 -0700513 Name string
Dan Willemsenb1957a52016-06-23 23:44:54 -0700514 Field string
Colin Crossec193632015-07-06 17:49:43 -0700515 Multilib string
Colin Cross3f40fa42015-01-30 17:27:36 -0800516}
517
Colin Crossec193632015-07-06 17:49:43 -0700518func newArch(name, multilib string) ArchType {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700519 archType := ArchType{
Colin Crossec193632015-07-06 17:49:43 -0700520 Name: name,
Dan Willemsenb1957a52016-06-23 23:44:54 -0700521 Field: proptools.FieldNameForProperty(name),
Colin Crossec193632015-07-06 17:49:43 -0700522 Multilib: multilib,
Colin Cross3f40fa42015-01-30 17:27:36 -0800523 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700524 archTypeList = append(archTypeList, archType)
525 return archType
Colin Cross3f40fa42015-01-30 17:27:36 -0800526}
527
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -0700528func ArchTypeList() []ArchType {
529 return append([]ArchType(nil), archTypeList...)
530}
531
Colin Cross3f40fa42015-01-30 17:27:36 -0800532func (a ArchType) String() string {
533 return a.Name
534}
535
Colin Cross74ba9622019-02-11 15:11:14 -0800536var _ encoding.TextMarshaler = ArchType{}
537
538func (a ArchType) MarshalText() ([]byte, error) {
539 return []byte(strconv.Quote(a.String())), nil
540}
541
542var _ encoding.TextUnmarshaler = &ArchType{}
543
544func (a *ArchType) UnmarshalText(text []byte) error {
545 if u, ok := archTypeMap[string(text)]; ok {
546 *a = u
547 return nil
548 }
549
550 return fmt.Errorf("unknown ArchType %q", text)
551}
552
Colin Crossa1ad8d12016-06-01 17:09:44 -0700553var BuildOs = func() OsType {
Dan Willemsen490fd492015-11-24 17:53:15 -0800554 switch runtime.GOOS {
555 case "linux":
556 return Linux
557 case "darwin":
558 return Darwin
559 default:
560 panic(fmt.Sprintf("unsupported OS: %s", runtime.GOOS))
561 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700562}()
563
Jiyong Park87788b52020-09-01 12:37:45 +0900564var BuildArch = func() ArchType {
565 switch runtime.GOARCH {
566 case "amd64":
567 return X86_64
568 default:
569 panic(fmt.Sprintf("unsupported Arch: %s", runtime.GOARCH))
570 }
571}()
572
Colin Crossa1ad8d12016-06-01 17:09:44 -0700573var (
Paul Duffina04c1072020-03-02 10:16:35 +0000574 OsTypeList []OsType
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800575 commonTargetMap = make(map[string]Target)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700576
Dan Willemsen00fcbde2016-11-17 00:25:59 -0800577 NoOsType OsType
Dan Willemsen866b5632017-09-22 12:28:24 -0700578 Linux = NewOsType("linux_glibc", Host, false)
Dan Willemsen00fcbde2016-11-17 00:25:59 -0800579 Darwin = NewOsType("darwin", Host, false)
Dan Willemsen9ff34c02018-10-10 17:58:19 -0700580 LinuxBionic = NewOsType("linux_bionic", Host, false)
Jiyong Park1613e552020-09-14 19:43:17 +0900581 Windows = NewOsType("windows", Host, true)
Dan Willemsen00fcbde2016-11-17 00:25:59 -0800582 Android = NewOsType("android", Device, false)
Doug Horn21b94272019-01-16 12:06:11 -0800583 Fuchsia = NewOsType("fuchsia", Device, false)
Dan Willemsenb1957a52016-06-23 23:44:54 -0700584
Paul Duffin1356d8c2020-02-25 19:26:33 +0000585 // A pseudo OSType for a common os variant, which is OSType agnostic and which
586 // has dependencies on all the OS variants.
587 CommonOS = NewOsType("common_os", Generic, false)
588
Dan Willemsenb1957a52016-06-23 23:44:54 -0700589 osArchTypeMap = map[OsType][]ArchType{
Dan Willemsen00fcbde2016-11-17 00:25:59 -0800590 Linux: []ArchType{X86, X86_64},
Jiyong Park4afa2e22020-07-13 15:43:45 +0900591 LinuxBionic: []ArchType{Arm64, X86_64},
Dan Willemsene97e68a2018-08-28 17:12:56 -0700592 Darwin: []ArchType{X86_64},
Dan Willemsen00fcbde2016-11-17 00:25:59 -0800593 Windows: []ArchType{X86, X86_64},
Elliott Hughesda3a0712020-03-06 16:55:28 -0800594 Android: []ArchType{Arm, Arm64, X86, X86_64},
Doug Horn21b94272019-01-16 12:06:11 -0800595 Fuchsia: []ArchType{Arm64, X86_64},
Dan Willemsenb1957a52016-06-23 23:44:54 -0700596 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700597)
598
599type OsType struct {
600 Name, Field string
601 Class OsClass
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800602
603 DefaultDisabled bool
Dan Willemsen490fd492015-11-24 17:53:15 -0800604}
605
Colin Crossa1ad8d12016-06-01 17:09:44 -0700606type OsClass int
607
608const (
Dan Willemsen0e2d97b2016-11-28 17:50:06 -0800609 Generic OsClass = iota
610 Device
Colin Crossa1ad8d12016-06-01 17:09:44 -0700611 Host
Colin Crossa1ad8d12016-06-01 17:09:44 -0700612)
613
Colin Cross67a5c132017-05-09 13:45:28 -0700614func (class OsClass) String() string {
615 switch class {
616 case Generic:
617 return "generic"
618 case Device:
619 return "device"
620 case Host:
621 return "host"
Colin Cross67a5c132017-05-09 13:45:28 -0700622 default:
623 panic(fmt.Errorf("unknown class %d", class))
624 }
625}
626
Colin Crossa1ad8d12016-06-01 17:09:44 -0700627func (os OsType) String() string {
628 return os.Name
Colin Cross54c71122016-06-01 17:09:44 -0700629}
630
Dan Willemsen866b5632017-09-22 12:28:24 -0700631func (os OsType) Bionic() bool {
632 return os == Android || os == LinuxBionic
633}
634
635func (os OsType) Linux() bool {
636 return os == Android || os == Linux || os == LinuxBionic
637}
638
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800639func NewOsType(name string, class OsClass, defDisabled bool) OsType {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700640 os := OsType{
641 Name: name,
642 Field: strings.Title(name),
643 Class: class,
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800644
645 DefaultDisabled: defDisabled,
Colin Cross54c71122016-06-01 17:09:44 -0700646 }
Paul Duffina04c1072020-03-02 10:16:35 +0000647 OsTypeList = append(OsTypeList, os)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800648
649 if _, found := commonTargetMap[name]; found {
650 panic(fmt.Errorf("Found Os type duplicate during OsType registration: %q", name))
651 } else {
652 commonTargetMap[name] = Target{Os: os, Arch: Arch{ArchType: Common}}
653 }
654
Colin Crossa1ad8d12016-06-01 17:09:44 -0700655 return os
656}
657
658func osByName(name string) OsType {
Paul Duffina04c1072020-03-02 10:16:35 +0000659 for _, os := range OsTypeList {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700660 if os.Name == name {
661 return os
662 }
663 }
664
665 return NoOsType
Dan Willemsen490fd492015-11-24 17:53:15 -0800666}
667
dimitry1f33e402019-03-26 12:39:31 +0100668type NativeBridgeSupport bool
669
670const (
671 NativeBridgeDisabled NativeBridgeSupport = false
672 NativeBridgeEnabled NativeBridgeSupport = true
673)
674
Colin Crossa1ad8d12016-06-01 17:09:44 -0700675type Target struct {
dimitry8d6dde82019-07-11 10:23:53 +0200676 Os OsType
677 Arch Arch
678 NativeBridge NativeBridgeSupport
679 NativeBridgeHostArchName string
680 NativeBridgeRelativePath string
Jiyong Park1613e552020-09-14 19:43:17 +0900681
682 // HostCross is true when the target cannot run natively on the current build host.
683 // For example, linux_glibc_x86 returns true on a regular x86/i686/Linux machines, but returns false
684 // on Mac (different OS), or on 64-bit only i686/Linux machines (unsupported arch).
685 HostCross bool
Colin Crossd3ba0392015-05-07 14:11:29 -0700686}
687
Colin Crossa1ad8d12016-06-01 17:09:44 -0700688func (target Target) String() string {
Colin Crossa195f912019-10-16 11:07:20 -0700689 return target.OsVariation() + "_" + target.ArchVariation()
690}
691
692func (target Target) OsVariation() string {
693 return target.Os.String()
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700694}
695
696func (target Target) ArchVariation() string {
697 var variation string
dimitry1f33e402019-03-26 12:39:31 +0100698 if target.NativeBridge {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700699 variation = "native_bridge_"
dimitry1f33e402019-03-26 12:39:31 +0100700 }
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700701 variation += target.Arch.String()
702
Colin Crossa195f912019-10-16 11:07:20 -0700703 return variation
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700704}
705
706func (target Target) Variations() []blueprint.Variation {
707 return []blueprint.Variation{
Colin Crossa195f912019-10-16 11:07:20 -0700708 {Mutator: "os", Variation: target.OsVariation()},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700709 {Mutator: "arch", Variation: target.ArchVariation()},
710 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800711}
712
Colin Cross617b88a2020-08-24 18:04:09 -0700713func osMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Crossa195f912019-10-16 11:07:20 -0700714 var module Module
715 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700716 if module, ok = bpctx.Module().(Module); !ok {
717 if bootstrap.IsBootstrapModule(bpctx.Module()) {
718 // Bootstrap Go modules are always the build OS or linux bionic.
719 config := bpctx.Config().(Config)
720 osNames := []string{config.BuildOSTarget.OsVariation()}
721 for _, hostCrossTarget := range config.Targets[LinuxBionic] {
722 if hostCrossTarget.Arch.ArchType == config.BuildOSTarget.Arch.ArchType {
723 osNames = append(osNames, hostCrossTarget.OsVariation())
724 }
725 }
726 osNames = FirstUniqueStrings(osNames)
727 bpctx.CreateVariations(osNames...)
728 }
Colin Crossa195f912019-10-16 11:07:20 -0700729 return
730 }
731
Colin Cross617b88a2020-08-24 18:04:09 -0700732 // Bootstrap Go module support above requires this mutator to be a
733 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
734 // filters out non-Soong modules. Now that we've handled them, create a
735 // normal android.BottomUpMutatorContext.
736 mctx := bottomUpMutatorContextFactory(bpctx, module, false)
737
Colin Crossa195f912019-10-16 11:07:20 -0700738 base := module.base()
739
740 if !base.ArchSpecific() {
741 return
742 }
743
Colin Crossa195f912019-10-16 11:07:20 -0700744 var moduleOSList []OsType
745
Paul Duffina04c1072020-03-02 10:16:35 +0000746 for _, os := range OsTypeList {
Jiyong Park1613e552020-09-14 19:43:17 +0900747 for _, t := range mctx.Config().Targets[os] {
748 if base.supportsTarget(t, mctx.Config()) {
749 moduleOSList = append(moduleOSList, os)
750 break
Colin Crossa195f912019-10-16 11:07:20 -0700751 }
752 }
Colin Crossa195f912019-10-16 11:07:20 -0700753 }
754
755 if len(moduleOSList) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900756 base.Disable()
Colin Crossa195f912019-10-16 11:07:20 -0700757 return
758 }
759
760 osNames := make([]string, len(moduleOSList))
761
762 for i, os := range moduleOSList {
763 osNames[i] = os.String()
764 }
765
Paul Duffin1356d8c2020-02-25 19:26:33 +0000766 createCommonOSVariant := base.commonProperties.CreateCommonOSVariant
767 if createCommonOSVariant {
768 // A CommonOS variant was requested so add it to the list of OS's variants to
769 // create. It needs to be added to the end because it needs to depend on the
770 // the other variants in the list returned by CreateVariations(...) and inter
771 // variant dependencies can only be created from a later variant in that list to
772 // an earlier one. That is because variants are always processed in the order in
773 // which they are returned from CreateVariations(...).
774 osNames = append(osNames, CommonOS.Name)
775 moduleOSList = append(moduleOSList, CommonOS)
Colin Crossa195f912019-10-16 11:07:20 -0700776 }
777
Paul Duffin1356d8c2020-02-25 19:26:33 +0000778 modules := mctx.CreateVariations(osNames...)
779 for i, m := range modules {
780 m.base().commonProperties.CompileOS = moduleOSList[i]
781 m.base().setOSProperties(mctx)
782 }
783
784 if createCommonOSVariant {
785 // A CommonOS variant was requested so add dependencies from it (the last one in
786 // the list) to the OS type specific variants.
787 last := len(modules) - 1
788 commonOSVariant := modules[last]
789 commonOSVariant.base().commonProperties.CommonOSVariant = true
790 for _, module := range modules[0:last] {
791 // Ignore modules that are enabled. Note, this will only avoid adding
792 // dependencies on OsType variants that are explicitly disabled in their
793 // properties. The CommonOS variant will still depend on disabled variants
794 // if they are disabled afterwards, e.g. in archMutator if
795 if module.Enabled() {
796 mctx.AddInterVariantDependency(commonOsToOsSpecificVariantTag, commonOSVariant, module)
797 }
798 }
799 }
800}
801
802// Identifies the dependency from CommonOS variant to the os specific variants.
803type commonOSTag struct{ blueprint.BaseDependencyTag }
804
805var commonOsToOsSpecificVariantTag = commonOSTag{}
806
807// Get the OsType specific variants for the current CommonOS variant.
808//
809// The returned list will only contain enabled OsType specific variants of the
810// module referenced in the supplied context. An empty list is returned if there
811// are no enabled variants or the supplied context is not for an CommonOS
812// variant.
813func GetOsSpecificVariantsOfCommonOSVariant(mctx BaseModuleContext) []Module {
814 var variants []Module
815 mctx.VisitDirectDeps(func(m Module) {
816 if mctx.OtherModuleDependencyTag(m) == commonOsToOsSpecificVariantTag {
817 if m.Enabled() {
818 variants = append(variants, m)
819 }
820 }
821 })
822
823 return variants
Colin Crossa195f912019-10-16 11:07:20 -0700824}
825
Colin Crossee0bc3b2018-10-02 22:01:37 -0700826// archMutator splits a module into a variant for each Target requested by the module. Target selection
827// for a module is in three levels, OsClass, mulitlib, and then Target.
828// OsClass selection is determined by:
829// - The HostOrDeviceSupported value passed in to InitAndroidArchModule by the module type factory, which selects
830// whether the module type can compile for host, device or both.
831// - The host_supported and device_supported properties on the module.
Roland Levillainf5b635d2019-06-05 14:42:57 +0100832// If host is supported for the module, the Host and HostCross OsClasses are selected. If device is supported
Colin Crossee0bc3b2018-10-02 22:01:37 -0700833// for the module, the Device OsClass is selected.
834// Within each selected OsClass, the multilib selection is determined by:
Jaewoong Jung02b2d4d2019-06-06 15:19:57 -0700835// - The compile_multilib property if it set (which may be overridden by target.android.compile_multilib or
Colin Crossee0bc3b2018-10-02 22:01:37 -0700836// target.host.compile_multilib).
837// - The default multilib passed to InitAndroidArchModule if compile_multilib was not set.
838// Valid multilib values include:
839// "both": compile for all Targets supported by the OsClass (generally x86_64 and x86, or arm64 and arm).
840// "first": compile for only a single preferred Target supported by the OsClass. This is generally x86_64 or arm64,
Elliott Hughes79ae3412020-04-17 15:49:49 -0700841// but may be arm for a 32-bit only build.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700842// "32": compile for only a single 32-bit Target supported by the OsClass.
843// "64": compile for only a single 64-bit Target supported by the OsClass.
844// "common": compile a for a single Target that will work on all Targets suported by the OsClass (for example Java).
845//
846// Once the list of Targets is determined, the module is split into a variant for each Target.
847//
848// Modules can be initialized with InitAndroidMultiTargetsArchModule, in which case they will be split by OsClass,
849// but will have a common Target that is expected to handle all other selected Targets via ctx.MultiTargets().
Colin Cross617b88a2020-08-24 18:04:09 -0700850func archMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700851 var module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800852 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700853 if module, ok = bpctx.Module().(Module); !ok {
854 if bootstrap.IsBootstrapModule(bpctx.Module()) {
855 // Bootstrap Go modules are always the build architecture.
856 bpctx.CreateVariations(bpctx.Config().(Config).BuildOSTarget.ArchVariation())
857 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800858 return
859 }
860
Colin Cross617b88a2020-08-24 18:04:09 -0700861 // Bootstrap Go module support above requires this mutator to be a
862 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
863 // filters out non-Soong modules. Now that we've handled them, create a
864 // normal android.BottomUpMutatorContext.
865 mctx := bottomUpMutatorContextFactory(bpctx, module, false)
866
Colin Cross5eca7cb2018-10-02 14:02:10 -0700867 base := module.base()
868
869 if !base.ArchSpecific() {
Colin Crossb9db4802016-06-03 01:50:47 +0000870 return
871 }
872
Colin Crossa195f912019-10-16 11:07:20 -0700873 os := base.commonProperties.CompileOS
Paul Duffin1356d8c2020-02-25 19:26:33 +0000874 if os == CommonOS {
875 // Make sure that the target related properties are initialized for the
876 // CommonOS variant.
877 addTargetProperties(module, commonTargetMap[os.Name], nil, true)
878
879 // Do not create arch specific variants for the CommonOS variant.
880 return
881 }
882
Colin Crossa195f912019-10-16 11:07:20 -0700883 osTargets := mctx.Config().Targets[os]
Colin Crossfb0c16e2019-11-20 17:12:35 -0800884 image := base.commonProperties.ImageVariation
Colin Crossa195f912019-10-16 11:07:20 -0700885 // Filter NativeBridge targets unless they are explicitly supported
Colin Cross83bead42019-12-18 10:45:46 -0800886 // Skip creating native bridge variants for vendor modules
887 if os == Android &&
888 !(Bool(base.commonProperties.Native_bridge_supported) && image == CoreVariation) {
889
Colin Crossa195f912019-10-16 11:07:20 -0700890 var targets []Target
891 for _, t := range osTargets {
892 if !t.NativeBridge {
893 targets = append(targets, t)
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700894 }
895 }
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700896
Colin Crossa195f912019-10-16 11:07:20 -0700897 osTargets = targets
898 }
Colin Crossee0bc3b2018-10-02 22:01:37 -0700899
Yifan Hong1b3348d2020-01-21 15:53:22 -0800900 // only the primary arch in the ramdisk / recovery partition
901 if os == Android && (module.InstallInRecovery() || module.InstallInRamdisk()) {
Colin Crossa195f912019-10-16 11:07:20 -0700902 osTargets = []Target{osTargets[0]}
903 }
dimitry1f33e402019-03-26 12:39:31 +0100904
Colin Crossa195f912019-10-16 11:07:20 -0700905 prefer32 := false
906 if base.prefer32 != nil {
Jiyong Park1613e552020-09-14 19:43:17 +0900907 prefer32 = base.prefer32(mctx, base, os)
Colin Crossa195f912019-10-16 11:07:20 -0700908 }
dimitry1f33e402019-03-26 12:39:31 +0100909
Colin Crossa195f912019-10-16 11:07:20 -0700910 multilib, extraMultilib := decodeMultilib(base, os.Class)
911 targets, err := decodeMultilibTargets(multilib, osTargets, prefer32)
912 if err != nil {
913 mctx.ModuleErrorf("%s", err.Error())
914 }
Colin Cross5eca7cb2018-10-02 14:02:10 -0700915
Colin Crossa195f912019-10-16 11:07:20 -0700916 var multiTargets []Target
917 if extraMultilib != "" {
918 multiTargets, err = decodeMultilibTargets(extraMultilib, osTargets, prefer32)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700919 if err != nil {
920 mctx.ModuleErrorf("%s", err.Error())
921 }
Colin Crossb9db4802016-06-03 01:50:47 +0000922 }
923
Colin Crossfb0c16e2019-11-20 17:12:35 -0800924 if image == RecoveryVariation {
925 primaryArch := mctx.Config().DevicePrimaryArchType()
926 targets = filterToArch(targets, primaryArch)
927 multiTargets = filterToArch(multiTargets, primaryArch)
928 }
929
Colin Crossa195f912019-10-16 11:07:20 -0700930 if len(targets) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900931 base.Disable()
Dan Willemsen3f32f032016-07-11 14:36:48 -0700932 return
933 }
934
Colin Crossa195f912019-10-16 11:07:20 -0700935 targetNames := make([]string, len(targets))
Colin Crossb9db4802016-06-03 01:50:47 +0000936
Colin Crossa195f912019-10-16 11:07:20 -0700937 for i, target := range targets {
938 targetNames[i] = target.ArchVariation()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700939 }
940
941 modules := mctx.CreateVariations(targetNames...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800942 for i, m := range modules {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000943 addTargetProperties(m, targets[i], multiTargets, i == 0)
Colin Cross617b88a2020-08-24 18:04:09 -0700944 m.base().setArchProperties(mctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800945 }
946}
947
Paul Duffin1356d8c2020-02-25 19:26:33 +0000948func addTargetProperties(m Module, target Target, multiTargets []Target, primaryTarget bool) {
949 m.base().commonProperties.CompileTarget = target
950 m.base().commonProperties.CompileMultiTargets = multiTargets
951 m.base().commonProperties.CompilePrimary = primaryTarget
952}
953
Colin Crossee0bc3b2018-10-02 22:01:37 -0700954func decodeMultilib(base *ModuleBase, class OsClass) (multilib, extraMultilib string) {
955 switch class {
956 case Device:
957 multilib = String(base.commonProperties.Target.Android.Compile_multilib)
Jiyong Park1613e552020-09-14 19:43:17 +0900958 case Host:
Colin Crossee0bc3b2018-10-02 22:01:37 -0700959 multilib = String(base.commonProperties.Target.Host.Compile_multilib)
960 }
961 if multilib == "" {
962 multilib = String(base.commonProperties.Compile_multilib)
963 }
964 if multilib == "" {
965 multilib = base.commonProperties.Default_multilib
966 }
967
968 if base.commonProperties.UseTargetVariants {
969 return multilib, ""
970 } else {
971 // For app modules a single arch variant will be created per OS class which is expected to handle all the
972 // selected arches. Return the common-type as multilib and any Android.bp provided multilib as extraMultilib
973 if multilib == base.commonProperties.Default_multilib {
974 multilib = "first"
975 }
976 return base.commonProperties.Default_multilib, multilib
977 }
978}
979
Colin Crossfb0c16e2019-11-20 17:12:35 -0800980func filterToArch(targets []Target, arch ArchType) []Target {
981 for i := 0; i < len(targets); i++ {
982 if targets[i].Arch.ArchType != arch {
983 targets = append(targets[:i], targets[i+1:]...)
984 i--
985 }
986 }
987 return targets
988}
989
Colin Crosscbbd13f2020-01-17 14:08:22 -0800990type archPropTypeDesc struct {
991 arch, multilib, target reflect.Type
992}
993
994type archPropRoot struct {
995 Arch, Multilib, Target interface{}
996}
997
998// createArchPropTypeDesc takes a reflect.Type that is either a struct or a pointer to a struct, and
999// returns lists of reflect.Types that contains the arch-variant properties inside structs for each
1000// arch, multilib and target property.
1001func createArchPropTypeDesc(props reflect.Type) []archPropTypeDesc {
Colin Crossb1d8c992020-01-21 11:43:29 -08001002 // Each property struct shard will be nested many times under the runtime generated arch struct,
1003 // which can hit the limit of 64kB for the name of runtime generated structs. They are nested
1004 // 97 times now, which may grow in the future, plus there is some overhead for the containing
1005 // type. This number may need to be reduced if too many are added, but reducing it too far
1006 // could cause problems if a single deeply nested property no longer fits in the name.
1007 const maxArchTypeNameSize = 500
1008
1009 propShards, _ := proptools.FilterPropertyStructSharded(props, maxArchTypeNameSize, filterArchStruct)
Colin Crosscb988072019-01-24 14:58:11 -08001010 if len(propShards) == 0 {
Dan Willemsenb1957a52016-06-23 23:44:54 -07001011 return nil
1012 }
1013
Colin Crosscbbd13f2020-01-17 14:08:22 -08001014 var ret []archPropTypeDesc
Colin Crossc17727d2018-10-24 12:42:09 -07001015 for _, props := range propShards {
Dan Willemsenb1957a52016-06-23 23:44:54 -07001016
Colin Crossc17727d2018-10-24 12:42:09 -07001017 variantFields := func(names []string) []reflect.StructField {
1018 ret := make([]reflect.StructField, len(names))
Dan Willemsenb1957a52016-06-23 23:44:54 -07001019
Colin Crossc17727d2018-10-24 12:42:09 -07001020 for i, name := range names {
1021 ret[i].Name = name
1022 ret[i].Type = props
Dan Willemsen866b5632017-09-22 12:28:24 -07001023 }
Colin Crossc17727d2018-10-24 12:42:09 -07001024
1025 return ret
1026 }
1027
1028 archFields := make([]reflect.StructField, len(archTypeList))
1029 for i, arch := range archTypeList {
1030 variants := []string{}
1031
1032 for _, archVariant := range archVariants[arch] {
1033 archVariant := variantReplacer.Replace(archVariant)
1034 variants = append(variants, proptools.FieldNameForProperty(archVariant))
1035 }
1036 for _, feature := range archFeatures[arch] {
1037 feature := variantReplacer.Replace(feature)
1038 variants = append(variants, proptools.FieldNameForProperty(feature))
1039 }
1040
1041 fields := variantFields(variants)
1042
1043 fields = append([]reflect.StructField{{
1044 Name: "BlueprintEmbed",
1045 Type: props,
1046 Anonymous: true,
1047 }}, fields...)
1048
1049 archFields[i] = reflect.StructField{
1050 Name: arch.Field,
1051 Type: reflect.StructOf(fields),
1052 }
1053 }
1054 archType := reflect.StructOf(archFields)
1055
1056 multilibType := reflect.StructOf(variantFields([]string{"Lib32", "Lib64"}))
1057
1058 targets := []string{
1059 "Host",
1060 "Android64",
1061 "Android32",
1062 "Bionic",
1063 "Linux",
1064 "Not_windows",
1065 "Arm_on_x86",
1066 "Arm_on_x86_64",
Victor Khimenkoc26fcf42020-05-07 22:16:33 +02001067 "Native_bridge",
Colin Crossc17727d2018-10-24 12:42:09 -07001068 }
Paul Duffina04c1072020-03-02 10:16:35 +00001069 for _, os := range OsTypeList {
Colin Crossc17727d2018-10-24 12:42:09 -07001070 targets = append(targets, os.Field)
1071
1072 for _, archType := range osArchTypeMap[os] {
1073 targets = append(targets, os.Field+"_"+archType.Name)
1074
1075 if os.Linux() {
1076 target := "Linux_" + archType.Name
1077 if !InList(target, targets) {
1078 targets = append(targets, target)
1079 }
1080 }
1081 if os.Bionic() {
1082 target := "Bionic_" + archType.Name
1083 if !InList(target, targets) {
1084 targets = append(targets, target)
1085 }
Dan Willemsen866b5632017-09-22 12:28:24 -07001086 }
1087 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001088 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001089
Colin Crossc17727d2018-10-24 12:42:09 -07001090 targetType := reflect.StructOf(variantFields(targets))
Colin Crosscbbd13f2020-01-17 14:08:22 -08001091
1092 ret = append(ret, archPropTypeDesc{
1093 arch: reflect.PtrTo(archType),
1094 multilib: reflect.PtrTo(multilibType),
1095 target: reflect.PtrTo(targetType),
1096 })
Colin Crossc17727d2018-10-24 12:42:09 -07001097 }
1098 return ret
Dan Willemsenb1957a52016-06-23 23:44:54 -07001099}
1100
Colin Cross74449102019-09-25 11:26:40 -07001101func filterArchStruct(field reflect.StructField, prefix string) (bool, reflect.StructField) {
1102 if proptools.HasTag(field, "android", "arch_variant") {
1103 // The arch_variant field isn't necessary past this point
1104 // Instead of wasting space, just remove it. Go also has a
1105 // 16-bit limit on structure name length. The name is constructed
1106 // based on the Go source representation of the structure, so
1107 // the tag names count towards that length.
Colin Crossb4fecbf2020-01-21 11:38:47 -08001108
1109 androidTag := field.Tag.Get("android")
1110 values := strings.Split(androidTag, ",")
1111
1112 if string(field.Tag) != `android:"`+strings.Join(values, ",")+`"` {
1113 panic(fmt.Errorf("unexpected tag format %q", field.Tag))
Colin Cross74449102019-09-25 11:26:40 -07001114 }
Colin Crossb4fecbf2020-01-21 11:38:47 -08001115 // these tags don't need to be present in the runtime generated struct type.
1116 values = RemoveListFromList(values, []string{"arch_variant", "variant_prepend", "path"})
1117 if len(values) > 0 {
1118 panic(fmt.Errorf("unknown tags %q in field %q", values, prefix+field.Name))
1119 }
1120
1121 field.Tag = ""
Colin Cross74449102019-09-25 11:26:40 -07001122 return true, field
1123 }
1124 return false, field
1125}
1126
Dan Willemsenb1957a52016-06-23 23:44:54 -07001127var archPropTypeMap OncePer
1128
Colin Cross36242852017-06-23 15:06:31 -07001129func InitArchModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001130
1131 base := m.base()
1132
Colin Cross36242852017-06-23 15:06:31 -07001133 base.generalProperties = m.GetProperties()
Colin Cross3f40fa42015-01-30 17:27:36 -08001134
1135 for _, properties := range base.generalProperties {
1136 propertiesValue := reflect.ValueOf(properties)
Colin Cross62496a02016-08-08 15:49:17 -07001137 t := propertiesValue.Type()
Colin Cross3f40fa42015-01-30 17:27:36 -08001138 if propertiesValue.Kind() != reflect.Ptr {
Colin Crossca860ac2016-01-04 14:34:37 -08001139 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
1140 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001141 }
1142
1143 propertiesValue = propertiesValue.Elem()
1144 if propertiesValue.Kind() != reflect.Struct {
Colin Crossca860ac2016-01-04 14:34:37 -08001145 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
1146 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001147 }
1148
Colin Cross571cccf2019-02-04 11:22:08 -08001149 archPropTypes := archPropTypeMap.Once(NewCustomOnceKey(t), func() interface{} {
Colin Crosscbbd13f2020-01-17 14:08:22 -08001150 return createArchPropTypeDesc(t)
1151 }).([]archPropTypeDesc)
Colin Cross3f40fa42015-01-30 17:27:36 -08001152
Colin Crossc17727d2018-10-24 12:42:09 -07001153 var archProperties []interface{}
1154 for _, t := range archPropTypes {
Colin Crosscbbd13f2020-01-17 14:08:22 -08001155 archProperties = append(archProperties, &archPropRoot{
1156 Arch: reflect.Zero(t.arch).Interface(),
1157 Multilib: reflect.Zero(t.multilib).Interface(),
1158 Target: reflect.Zero(t.target).Interface(),
1159 })
Dan Willemsenb1957a52016-06-23 23:44:54 -07001160 }
Colin Crossc17727d2018-10-24 12:42:09 -07001161 base.archProperties = append(base.archProperties, archProperties)
1162 m.AddProperties(archProperties...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001163 }
1164
Colin Cross36242852017-06-23 15:06:31 -07001165 base.customizableProperties = m.GetProperties()
Colin Cross3f40fa42015-01-30 17:27:36 -08001166}
1167
Colin Crossa716add2015-12-16 11:07:39 -08001168var variantReplacer = strings.NewReplacer("-", "_", ".", "_")
Colin Crossec193632015-07-06 17:49:43 -07001169
Colin Cross4157e882019-06-06 16:57:04 -07001170func (m *ModuleBase) appendProperties(ctx BottomUpMutatorContext,
Dan Willemsenb1957a52016-06-23 23:44:54 -07001171 dst interface{}, src reflect.Value, field, srcPrefix string) reflect.Value {
Colin Cross06a931b2015-10-28 17:23:31 -07001172
Colin Crosscbbd13f2020-01-17 14:08:22 -08001173 if src.Kind() == reflect.Ptr {
1174 if src.IsNil() {
1175 return src
1176 }
1177 src = src.Elem()
1178 }
1179
Dan Willemsenb1957a52016-06-23 23:44:54 -07001180 src = src.FieldByName(field)
1181 if !src.IsValid() {
Colin Crosseeabb892015-11-20 13:07:51 -08001182 ctx.ModuleErrorf("field %q does not exist", srcPrefix)
Dan Willemsenb1957a52016-06-23 23:44:54 -07001183 return src
Colin Cross85a88972015-11-23 13:29:51 -08001184 }
1185
Dan Willemsenb1957a52016-06-23 23:44:54 -07001186 ret := src
Colin Cross85a88972015-11-23 13:29:51 -08001187
Dan Willemsenb1957a52016-06-23 23:44:54 -07001188 if src.Kind() == reflect.Struct {
1189 src = src.FieldByName("BlueprintEmbed")
Colin Cross06a931b2015-10-28 17:23:31 -07001190 }
1191
Colin Cross6ee75b62016-05-05 15:57:15 -07001192 order := func(property string,
1193 dstField, srcField reflect.StructField,
1194 dstValue, srcValue interface{}) (proptools.Order, error) {
1195 if proptools.HasTag(dstField, "android", "variant_prepend") {
1196 return proptools.Prepend, nil
1197 } else {
1198 return proptools.Append, nil
1199 }
1200 }
1201
Dan Willemsenb1957a52016-06-23 23:44:54 -07001202 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, order)
Colin Cross06a931b2015-10-28 17:23:31 -07001203 if err != nil {
1204 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
1205 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
1206 } else {
1207 panic(err)
1208 }
1209 }
Colin Cross85a88972015-11-23 13:29:51 -08001210
Dan Willemsenb1957a52016-06-23 23:44:54 -07001211 return ret
Colin Cross06a931b2015-10-28 17:23:31 -07001212}
1213
Colin Crossa195f912019-10-16 11:07:20 -07001214// Rewrite the module's properties structs to contain os-specific values.
1215func (m *ModuleBase) setOSProperties(ctx BottomUpMutatorContext) {
1216 os := m.commonProperties.CompileOS
1217
1218 for i := range m.generalProperties {
1219 genProps := m.generalProperties[i]
1220 if m.archProperties[i] == nil {
1221 continue
1222 }
1223 for _, archProperties := range m.archProperties[i] {
1224 archPropValues := reflect.ValueOf(archProperties).Elem()
1225
Colin Crosscbbd13f2020-01-17 14:08:22 -08001226 targetProp := archPropValues.FieldByName("Target").Elem()
Colin Crossa195f912019-10-16 11:07:20 -07001227
1228 // Handle host-specific properties in the form:
1229 // target: {
1230 // host: {
1231 // key: value,
1232 // },
1233 // },
Jiyong Park1613e552020-09-14 19:43:17 +09001234 if os.Class == Host {
Colin Crossa195f912019-10-16 11:07:20 -07001235 field := "Host"
1236 prefix := "target.host"
1237 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1238 }
1239
1240 // Handle target OS generalities of the form:
1241 // target: {
1242 // bionic: {
1243 // key: value,
1244 // },
1245 // }
1246 if os.Linux() {
1247 field := "Linux"
1248 prefix := "target.linux"
1249 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1250 }
1251
1252 if os.Bionic() {
1253 field := "Bionic"
1254 prefix := "target.bionic"
1255 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1256 }
1257
1258 // Handle target OS properties in the form:
1259 // target: {
1260 // linux_glibc: {
1261 // key: value,
1262 // },
1263 // not_windows: {
1264 // key: value,
1265 // },
1266 // android {
1267 // key: value,
1268 // },
1269 // },
1270 field := os.Field
1271 prefix := "target." + os.Name
1272 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1273
Jiyong Park1613e552020-09-14 19:43:17 +09001274 if os.Class == Host && os != Windows {
Colin Crossa195f912019-10-16 11:07:20 -07001275 field := "Not_windows"
1276 prefix := "target.not_windows"
1277 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1278 }
1279
1280 // Handle 64-bit device properties in the form:
1281 // target {
1282 // android64 {
1283 // key: value,
1284 // },
1285 // android32 {
1286 // key: value,
1287 // },
1288 // },
1289 // WARNING: this is probably not what you want to use in your blueprints file, it selects
1290 // options for all targets on a device that supports 64-bit binaries, not just the targets
1291 // that are being compiled for 64-bit. Its expected use case is binaries like linker and
1292 // debuggerd that need to know when they are a 32-bit process running on a 64-bit device
1293 if os.Class == Device {
1294 if ctx.Config().Android64() {
1295 field := "Android64"
1296 prefix := "target.android64"
1297 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1298 } else {
1299 field := "Android32"
1300 prefix := "target.android32"
1301 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1302 }
1303 }
1304 }
1305 }
1306}
1307
Colin Cross3f40fa42015-01-30 17:27:36 -08001308// Rewrite the module's properties structs to contain arch-specific values.
Colin Cross4157e882019-06-06 16:57:04 -07001309func (m *ModuleBase) setArchProperties(ctx BottomUpMutatorContext) {
1310 arch := m.Arch()
1311 os := m.Os()
Colin Crossd3ba0392015-05-07 14:11:29 -07001312
Colin Cross4157e882019-06-06 16:57:04 -07001313 for i := range m.generalProperties {
1314 genProps := m.generalProperties[i]
1315 if m.archProperties[i] == nil {
Dan Willemsenb1957a52016-06-23 23:44:54 -07001316 continue
1317 }
Colin Cross4157e882019-06-06 16:57:04 -07001318 for _, archProperties := range m.archProperties[i] {
Colin Crossc17727d2018-10-24 12:42:09 -07001319 archPropValues := reflect.ValueOf(archProperties).Elem()
Dan Willemsenb1957a52016-06-23 23:44:54 -07001320
Colin Crosscbbd13f2020-01-17 14:08:22 -08001321 archProp := archPropValues.FieldByName("Arch").Elem()
1322 multilibProp := archPropValues.FieldByName("Multilib").Elem()
1323 targetProp := archPropValues.FieldByName("Target").Elem()
Dan Willemsenb1957a52016-06-23 23:44:54 -07001324
Colin Crossc17727d2018-10-24 12:42:09 -07001325 // Handle arch-specific properties in the form:
Colin Crossd5934c82017-10-02 13:55:26 -07001326 // arch: {
Colin Crossc17727d2018-10-24 12:42:09 -07001327 // arm64: {
Colin Crossd5934c82017-10-02 13:55:26 -07001328 // key: value,
1329 // },
1330 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001331 t := arch.ArchType
1332
1333 if arch.ArchType != Common {
1334 field := proptools.FieldNameForProperty(t.Name)
1335 prefix := "arch." + t.Name
Colin Cross4157e882019-06-06 16:57:04 -07001336 archStruct := m.appendProperties(ctx, genProps, archProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001337
1338 // Handle arch-variant-specific properties in the form:
1339 // arch: {
1340 // variant: {
1341 // key: value,
1342 // },
1343 // },
1344 v := variantReplacer.Replace(arch.ArchVariant)
1345 if v != "" {
1346 field := proptools.FieldNameForProperty(v)
1347 prefix := "arch." + t.Name + "." + v
Colin Cross4157e882019-06-06 16:57:04 -07001348 m.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001349 }
1350
1351 // Handle cpu-variant-specific properties in the form:
1352 // arch: {
1353 // variant: {
1354 // key: value,
1355 // },
1356 // },
1357 if arch.CpuVariant != arch.ArchVariant {
1358 c := variantReplacer.Replace(arch.CpuVariant)
1359 if c != "" {
1360 field := proptools.FieldNameForProperty(c)
1361 prefix := "arch." + t.Name + "." + c
Colin Cross4157e882019-06-06 16:57:04 -07001362 m.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001363 }
1364 }
1365
1366 // Handle arch-feature-specific properties in the form:
1367 // arch: {
1368 // feature: {
1369 // key: value,
1370 // },
1371 // },
1372 for _, feature := range arch.ArchFeatures {
1373 field := proptools.FieldNameForProperty(feature)
1374 prefix := "arch." + t.Name + "." + feature
Colin Cross4157e882019-06-06 16:57:04 -07001375 m.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001376 }
1377
1378 // Handle multilib-specific properties in the form:
1379 // multilib: {
1380 // lib32: {
1381 // key: value,
1382 // },
1383 // },
1384 field = proptools.FieldNameForProperty(t.Multilib)
1385 prefix = "multilib." + t.Multilib
Colin Cross4157e882019-06-06 16:57:04 -07001386 m.appendProperties(ctx, genProps, multilibProp, field, prefix)
Colin Cross08016332016-12-20 09:53:14 -08001387 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001388
Colin Crossa195f912019-10-16 11:07:20 -07001389 // Handle combined OS-feature and arch specific properties in the form:
Colin Crossc17727d2018-10-24 12:42:09 -07001390 // target: {
Colin Crossc17727d2018-10-24 12:42:09 -07001391 // bionic_x86: {
1392 // key: value,
1393 // },
1394 // }
Colin Crossa195f912019-10-16 11:07:20 -07001395 if os.Linux() && arch.ArchType != Common {
1396 field := "Linux_" + arch.ArchType.Name
1397 prefix := "target.linux_" + arch.ArchType.Name
Colin Cross4157e882019-06-06 16:57:04 -07001398 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossd5934c82017-10-02 13:55:26 -07001399 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001400
Colin Crossa195f912019-10-16 11:07:20 -07001401 if os.Bionic() && arch.ArchType != Common {
1402 field := "Bionic_" + t.Name
1403 prefix := "target.bionic_" + t.Name
Colin Cross4157e882019-06-06 16:57:04 -07001404 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001405 }
1406
Colin Crossa195f912019-10-16 11:07:20 -07001407 // Handle combined OS and arch specific properties in the form:
Colin Crossc17727d2018-10-24 12:42:09 -07001408 // target: {
Colin Crossc17727d2018-10-24 12:42:09 -07001409 // linux_glibc_x86: {
1410 // key: value,
1411 // },
1412 // linux_glibc_arm: {
1413 // key: value,
1414 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001415 // android_arm {
1416 // key: value,
1417 // },
1418 // android_x86 {
Colin Crossd5934c82017-10-02 13:55:26 -07001419 // key: value,
1420 // },
1421 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001422 if arch.ArchType != Common {
Colin Crossa195f912019-10-16 11:07:20 -07001423 field := os.Field + "_" + t.Name
1424 prefix := "target." + os.Name + "_" + t.Name
Colin Cross4157e882019-06-06 16:57:04 -07001425 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossd5934c82017-10-02 13:55:26 -07001426 }
1427
Colin Crossa195f912019-10-16 11:07:20 -07001428 // Handle arm on x86 properties in the form:
Colin Crossc17727d2018-10-24 12:42:09 -07001429 // target {
Colin Crossa195f912019-10-16 11:07:20 -07001430 // arm_on_x86 {
Colin Crossc17727d2018-10-24 12:42:09 -07001431 // key: value,
1432 // },
Colin Crossa195f912019-10-16 11:07:20 -07001433 // arm_on_x86_64 {
Colin Crossd5934c82017-10-02 13:55:26 -07001434 // key: value,
1435 // },
1436 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001437 if os.Class == Device {
Victor Khimenko1a31f802020-09-17 03:07:31 +02001438 if arch.ArchType == X86 && (hasArmAbi(arch) ||
1439 hasArmAndroidArch(ctx.Config().Targets[Android])) {
Colin Crossc17727d2018-10-24 12:42:09 -07001440 field := "Arm_on_x86"
1441 prefix := "target.arm_on_x86"
Colin Cross4157e882019-06-06 16:57:04 -07001442 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001443 }
Victor Khimenko1a31f802020-09-17 03:07:31 +02001444 if arch.ArchType == X86_64 && (hasArmAbi(arch) ||
1445 hasArmAndroidArch(ctx.Config().Targets[Android])) {
Colin Crossc17727d2018-10-24 12:42:09 -07001446 field := "Arm_on_x86_64"
1447 prefix := "target.arm_on_x86_64"
Colin Cross4157e882019-06-06 16:57:04 -07001448 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001449 }
Victor Khimenkoc26fcf42020-05-07 22:16:33 +02001450 if os == Android && m.Target().NativeBridge == NativeBridgeEnabled {
1451 field := "Native_bridge"
1452 prefix := "target.native_bridge"
1453 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1454 }
Colin Cross4247f0d2017-04-13 16:56:14 -07001455 }
Colin Crossbb2e2b72016-12-08 17:23:53 -08001456 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001457 }
1458}
1459
1460func forEachInterface(v reflect.Value, f func(reflect.Value)) {
1461 switch v.Kind() {
1462 case reflect.Interface:
1463 f(v)
1464 case reflect.Struct:
1465 for i := 0; i < v.NumField(); i++ {
1466 forEachInterface(v.Field(i), f)
1467 }
1468 case reflect.Ptr:
1469 forEachInterface(v.Elem(), f)
1470 default:
1471 panic(fmt.Errorf("Unsupported kind %s", v.Kind()))
1472 }
1473}
Colin Cross4225f652015-09-17 14:33:42 -07001474
Colin Crossa1ad8d12016-06-01 17:09:44 -07001475// Convert the arch product variables into a list of targets for each os class structs
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001476func decodeTargetProductVariables(config *config) (map[OsType][]Target, error) {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001477 variables := config.productVariables
Dan Willemsen490fd492015-11-24 17:53:15 -08001478
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001479 targets := make(map[OsType][]Target)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001480 var targetErr error
1481
dimitry1f33e402019-03-26 12:39:31 +01001482 addTarget := func(os OsType, archName string, archVariant, cpuVariant *string, abi []string,
dimitry8d6dde82019-07-11 10:23:53 +02001483 nativeBridgeEnabled NativeBridgeSupport, nativeBridgeHostArchName *string,
1484 nativeBridgeRelativePath *string) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001485 if targetErr != nil {
1486 return
Dan Willemsen490fd492015-11-24 17:53:15 -08001487 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001488
Dan Willemsen01a3c252019-01-11 19:02:16 -08001489 arch, err := decodeArch(os, archName, archVariant, cpuVariant, abi)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001490 if err != nil {
1491 targetErr = err
1492 return
1493 }
dimitry8d6dde82019-07-11 10:23:53 +02001494 nativeBridgeRelativePathStr := String(nativeBridgeRelativePath)
1495 nativeBridgeHostArchNameStr := String(nativeBridgeHostArchName)
1496
1497 // Use guest arch as relative install path by default
1498 if nativeBridgeEnabled && nativeBridgeRelativePathStr == "" {
1499 nativeBridgeRelativePathStr = arch.ArchType.String()
1500 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001501
Jiyong Park1613e552020-09-14 19:43:17 +09001502 // A target is considered as HostCross if it's a host target which can't run natively on
1503 // the currently configured build machine (either because the OS is different or because of
1504 // the unsupported arch)
1505 hostCross := false
1506 if os.Class == Host {
1507 var osSupported bool
1508 if os == BuildOs {
1509 osSupported = true
1510 } else if BuildOs.Linux() && os.Linux() {
1511 // LinuxBionic and Linux are compatible
1512 osSupported = true
1513 } else {
1514 osSupported = false
1515 }
1516
1517 var archSupported bool
1518 if arch.ArchType == Common {
1519 archSupported = true
1520 } else if arch.ArchType.Name == *variables.HostArch {
1521 archSupported = true
1522 } else if variables.HostSecondaryArch != nil && arch.ArchType.Name == *variables.HostSecondaryArch {
1523 archSupported = true
1524 } else {
1525 archSupported = false
1526 }
1527 if !osSupported || !archSupported {
1528 hostCross = true
1529 }
1530 }
1531
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001532 targets[os] = append(targets[os],
Colin Crossa1ad8d12016-06-01 17:09:44 -07001533 Target{
dimitry8d6dde82019-07-11 10:23:53 +02001534 Os: os,
1535 Arch: arch,
1536 NativeBridge: nativeBridgeEnabled,
1537 NativeBridgeHostArchName: nativeBridgeHostArchNameStr,
1538 NativeBridgeRelativePath: nativeBridgeRelativePathStr,
Jiyong Park1613e552020-09-14 19:43:17 +09001539 HostCross: hostCross,
Colin Crossa1ad8d12016-06-01 17:09:44 -07001540 })
Dan Willemsen490fd492015-11-24 17:53:15 -08001541 }
1542
Colin Cross4225f652015-09-17 14:33:42 -07001543 if variables.HostArch == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001544 return nil, fmt.Errorf("No host primary architecture set")
Colin Cross4225f652015-09-17 14:33:42 -07001545 }
1546
dimitry8d6dde82019-07-11 10:23:53 +02001547 addTarget(BuildOs, *variables.HostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001548
Colin Crosseeabb892015-11-20 13:07:51 -08001549 if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" {
dimitry8d6dde82019-07-11 10:23:53 +02001550 addTarget(BuildOs, *variables.HostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001551 }
1552
Colin Crossff3ae9d2018-04-10 16:15:18 -07001553 if Bool(config.Host_bionic) {
dimitry8d6dde82019-07-11 10:23:53 +02001554 addTarget(LinuxBionic, "x86_64", nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen01a405a2016-06-13 17:19:03 -07001555 }
1556
Colin Crossff3ae9d2018-04-10 16:15:18 -07001557 if String(variables.CrossHost) != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001558 crossHostOs := osByName(*variables.CrossHost)
1559 if crossHostOs == NoOsType {
1560 return nil, fmt.Errorf("Unknown cross host OS %q", *variables.CrossHost)
1561 }
1562
Colin Crossff3ae9d2018-04-10 16:15:18 -07001563 if String(variables.CrossHostArch) == "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001564 return nil, fmt.Errorf("No cross-host primary architecture set")
Dan Willemsen490fd492015-11-24 17:53:15 -08001565 }
1566
dimitry8d6dde82019-07-11 10:23:53 +02001567 addTarget(crossHostOs, *variables.CrossHostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001568
1569 if variables.CrossHostSecondaryArch != nil && *variables.CrossHostSecondaryArch != "" {
dimitry8d6dde82019-07-11 10:23:53 +02001570 addTarget(crossHostOs, *variables.CrossHostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001571 }
1572 }
1573
Dan Willemsen3f32f032016-07-11 14:36:48 -07001574 if variables.DeviceArch != nil && *variables.DeviceArch != "" {
Doug Horn21b94272019-01-16 12:06:11 -08001575 var target = Android
1576 if Bool(variables.Fuchsia) {
1577 target = Fuchsia
1578 }
1579
1580 addTarget(target, *variables.DeviceArch, variables.DeviceArchVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001581 variables.DeviceCpuVariant, variables.DeviceAbi, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001582
Dan Willemsen3f32f032016-07-11 14:36:48 -07001583 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" {
1584 addTarget(Android, *variables.DeviceSecondaryArch,
1585 variables.DeviceSecondaryArchVariant, variables.DeviceSecondaryCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001586 variables.DeviceSecondaryAbi, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001587 }
dimitry1f33e402019-03-26 12:39:31 +01001588
1589 if variables.NativeBridgeArch != nil && *variables.NativeBridgeArch != "" {
1590 addTarget(Android, *variables.NativeBridgeArch,
1591 variables.NativeBridgeArchVariant, variables.NativeBridgeCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001592 variables.NativeBridgeAbi, NativeBridgeEnabled, variables.DeviceArch,
1593 variables.NativeBridgeRelativePath)
dimitry1f33e402019-03-26 12:39:31 +01001594 }
1595
1596 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" &&
1597 variables.NativeBridgeSecondaryArch != nil && *variables.NativeBridgeSecondaryArch != "" {
1598 addTarget(Android, *variables.NativeBridgeSecondaryArch,
1599 variables.NativeBridgeSecondaryArchVariant,
1600 variables.NativeBridgeSecondaryCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001601 variables.NativeBridgeSecondaryAbi,
1602 NativeBridgeEnabled,
1603 variables.DeviceSecondaryArch,
1604 variables.NativeBridgeSecondaryRelativePath)
dimitry1f33e402019-03-26 12:39:31 +01001605 }
Colin Cross4225f652015-09-17 14:33:42 -07001606 }
1607
Colin Crossa1ad8d12016-06-01 17:09:44 -07001608 if targetErr != nil {
1609 return nil, targetErr
1610 }
1611
1612 return targets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001613}
1614
Colin Crossbb2e2b72016-12-08 17:23:53 -08001615// hasArmAbi returns true if arch has at least one arm ABI
1616func hasArmAbi(arch Arch) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -08001617 return PrefixInList(arch.Abi, "arm")
Colin Crossbb2e2b72016-12-08 17:23:53 -08001618}
1619
dimitry628db6f2019-05-22 17:16:21 +02001620// hasArmArch returns true if targets has at least non-native_bridge arm Android arch
Colin Cross4247f0d2017-04-13 16:56:14 -07001621func hasArmAndroidArch(targets []Target) bool {
1622 for _, target := range targets {
Victor Khimenko1a31f802020-09-17 03:07:31 +02001623 if target.Os == Android && target.Arch.ArchType == Arm {
Victor Khimenko5eb8ec12018-03-21 20:30:54 +01001624 return true
1625 }
1626 }
1627 return false
1628}
1629
Dan Albert4098deb2016-10-19 14:04:41 -07001630type archConfig struct {
1631 arch string
1632 archVariant string
1633 cpuVariant string
1634 abi []string
1635}
1636
1637func getMegaDeviceConfig() []archConfig {
1638 return []archConfig{
Dan Albert8818f492019-02-19 13:53:01 -08001639 {"arm", "armv7-a", "generic", []string{"armeabi-v7a"}},
Dan Willemsen110a89d2016-01-14 15:17:19 -08001640 {"arm", "armv7-a-neon", "generic", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001641 {"arm", "armv7-a-neon", "cortex-a7", []string{"armeabi-v7a"}},
1642 {"arm", "armv7-a-neon", "cortex-a8", []string{"armeabi-v7a"}},
Dan Willemsen110a89d2016-01-14 15:17:19 -08001643 {"arm", "armv7-a-neon", "cortex-a9", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001644 {"arm", "armv7-a-neon", "cortex-a15", []string{"armeabi-v7a"}},
1645 {"arm", "armv7-a-neon", "cortex-a53", []string{"armeabi-v7a"}},
1646 {"arm", "armv7-a-neon", "cortex-a53.a57", []string{"armeabi-v7a"}},
Richard Fungeb37ed32018-09-24 16:33:45 -07001647 {"arm", "armv7-a-neon", "cortex-a72", []string{"armeabi-v7a"}},
Jake Weinstein6600a442017-05-15 18:27:12 -04001648 {"arm", "armv7-a-neon", "cortex-a73", []string{"armeabi-v7a"}},
Christopher Ferrisba14a8f2018-04-23 18:15:25 -07001649 {"arm", "armv7-a-neon", "cortex-a75", []string{"armeabi-v7a"}},
Haibo Huanga31e2bd2018-10-09 14:27:28 -07001650 {"arm", "armv7-a-neon", "cortex-a76", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001651 {"arm", "armv7-a-neon", "krait", []string{"armeabi-v7a"}},
Alex Naidisae4fc182016-08-20 00:14:56 +02001652 {"arm", "armv7-a-neon", "kryo", []string{"armeabi-v7a"}},
Artem Serovd3072b02018-11-15 15:21:51 +00001653 {"arm", "armv7-a-neon", "kryo385", []string{"armeabi-v7a"}},
Junmo Park8ea49592017-07-24 07:14:55 +09001654 {"arm", "armv7-a-neon", "exynos-m1", []string{"armeabi-v7a"}},
Junmo Parkd86c9022017-07-21 09:07:47 +09001655 {"arm", "armv7-a-neon", "exynos-m2", []string{"armeabi-v7a"}},
Dan Willemsen110a89d2016-01-14 15:17:19 -08001656 {"arm64", "armv8-a", "cortex-a53", []string{"arm64-v8a"}},
Richard Fungeb37ed32018-09-24 16:33:45 -07001657 {"arm64", "armv8-a", "cortex-a72", []string{"arm64-v8a"}},
Jake Weinstein6600a442017-05-15 18:27:12 -04001658 {"arm64", "armv8-a", "cortex-a73", []string{"arm64-v8a"}},
Alex Naidisac01ff52016-08-30 15:56:33 +02001659 {"arm64", "armv8-a", "kryo", []string{"arm64-v8a"}},
Junmo Park8ea49592017-07-24 07:14:55 +09001660 {"arm64", "armv8-a", "exynos-m1", []string{"arm64-v8a"}},
Junmo Parkd86c9022017-07-21 09:07:47 +09001661 {"arm64", "armv8-a", "exynos-m2", []string{"arm64-v8a"}},
Artem Serovd3072b02018-11-15 15:21:51 +00001662 {"arm64", "armv8-2a", "kryo385", []string{"arm64-v8a"}},
Raphael Gault70b96b02020-06-18 09:56:53 +00001663 {"arm64", "armv8-2a-dotprod", "cortex-a55", []string{"arm64-v8a"}},
1664 {"arm64", "armv8-2a-dotprod", "cortex-a75", []string{"arm64-v8a"}},
1665 {"arm64", "armv8-2a-dotprod", "cortex-a76", []string{"arm64-v8a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001666 {"x86", "", "", []string{"x86"}},
1667 {"x86", "atom", "", []string{"x86"}},
1668 {"x86", "haswell", "", []string{"x86"}},
1669 {"x86", "ivybridge", "", []string{"x86"}},
1670 {"x86", "sandybridge", "", []string{"x86"}},
1671 {"x86", "silvermont", "", []string{"x86"}},
Benjamin Gordon87e7f2f2019-02-14 10:59:48 -07001672 {"x86", "stoneyridge", "", []string{"x86"}},
Dan Willemsen8a354052016-05-10 14:30:51 -07001673 {"x86", "x86_64", "", []string{"x86"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001674 {"x86_64", "", "", []string{"x86_64"}},
1675 {"x86_64", "haswell", "", []string{"x86_64"}},
1676 {"x86_64", "ivybridge", "", []string{"x86_64"}},
1677 {"x86_64", "sandybridge", "", []string{"x86_64"}},
1678 {"x86_64", "silvermont", "", []string{"x86_64"}},
Benjamin Gordon87e7f2f2019-02-14 10:59:48 -07001679 {"x86_64", "stoneyridge", "", []string{"x86_64"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001680 }
Dan Albert4098deb2016-10-19 14:04:41 -07001681}
Dan Willemsen322acaf2016-01-12 23:07:05 -08001682
Dan Albert4098deb2016-10-19 14:04:41 -07001683func getNdkAbisConfig() []archConfig {
1684 return []archConfig{
Dan Albert6bba6442020-01-30 15:16:49 -08001685 {"arm", "armv7-a", "", []string{"armeabi-v7a"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001686 {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001687 {"x86", "", "", []string{"x86"}},
1688 {"x86_64", "", "", []string{"x86_64"}},
1689 }
1690}
1691
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001692func getAmlAbisConfig() []archConfig {
1693 return []archConfig{
1694 {"arm", "armv7-a", "", []string{"armeabi-v7a"}},
1695 {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
1696 {"x86", "", "", []string{"x86"}},
1697 {"x86_64", "", "", []string{"x86_64"}},
1698 }
1699}
1700
Dan Willemsen01a3c252019-01-11 19:02:16 -08001701func decodeArchSettings(os OsType, archConfigs []archConfig) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001702 var ret []Target
Dan Willemsen322acaf2016-01-12 23:07:05 -08001703
Dan Albert4098deb2016-10-19 14:04:41 -07001704 for _, config := range archConfigs {
Dan Willemsen01a3c252019-01-11 19:02:16 -08001705 arch, err := decodeArch(os, config.arch, &config.archVariant,
Colin Crossa74ca042019-01-31 14:31:51 -08001706 &config.cpuVariant, config.abi)
Dan Willemsen322acaf2016-01-12 23:07:05 -08001707 if err != nil {
1708 return nil, err
1709 }
Colin Cross3b19f5d2019-09-17 14:45:31 -07001710
Colin Crossa1ad8d12016-06-01 17:09:44 -07001711 ret = append(ret, Target{
1712 Os: Android,
1713 Arch: arch,
1714 })
Dan Willemsen322acaf2016-01-12 23:07:05 -08001715 }
1716
1717 return ret, nil
1718}
1719
Colin Cross4225f652015-09-17 14:33:42 -07001720// Convert a set of strings from product variables into a single Arch struct
Colin Crossa74ca042019-01-31 14:31:51 -08001721func decodeArch(os OsType, arch string, archVariant, cpuVariant *string, abi []string) (Arch, error) {
Colin Cross4225f652015-09-17 14:33:42 -07001722 stringPtr := func(p *string) string {
1723 if p != nil {
1724 return *p
1725 }
1726 return ""
1727 }
1728
Colin Crosseeabb892015-11-20 13:07:51 -08001729 archType, ok := archTypeMap[arch]
1730 if !ok {
1731 return Arch{}, fmt.Errorf("unknown arch %q", arch)
1732 }
Colin Cross4225f652015-09-17 14:33:42 -07001733
Colin Crosseeabb892015-11-20 13:07:51 -08001734 a := Arch{
Colin Cross4225f652015-09-17 14:33:42 -07001735 ArchType: archType,
1736 ArchVariant: stringPtr(archVariant),
1737 CpuVariant: stringPtr(cpuVariant),
Colin Crossa74ca042019-01-31 14:31:51 -08001738 Abi: abi,
Colin Crosseeabb892015-11-20 13:07:51 -08001739 }
1740
1741 if a.ArchVariant == a.ArchType.Name || a.ArchVariant == "generic" {
1742 a.ArchVariant = ""
1743 }
1744
1745 if a.CpuVariant == a.ArchType.Name || a.CpuVariant == "generic" {
1746 a.CpuVariant = ""
1747 }
1748
1749 for i := 0; i < len(a.Abi); i++ {
1750 if a.Abi[i] == "" {
1751 a.Abi = append(a.Abi[:i], a.Abi[i+1:]...)
1752 i--
1753 }
1754 }
1755
Dan Willemsen01a3c252019-01-11 19:02:16 -08001756 if a.ArchVariant == "" {
1757 if featureMap, ok := defaultArchFeatureMap[os]; ok {
1758 a.ArchFeatures = featureMap[archType]
1759 }
1760 } else {
1761 if featureMap, ok := archFeatureMap[archType]; ok {
1762 a.ArchFeatures = featureMap[a.ArchVariant]
1763 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001764 }
1765
Colin Crosseeabb892015-11-20 13:07:51 -08001766 return a, nil
Colin Cross4225f652015-09-17 14:33:42 -07001767}
1768
Colin Cross69617d32016-09-06 10:39:07 -07001769func filterMultilibTargets(targets []Target, multilib string) []Target {
1770 var ret []Target
1771 for _, t := range targets {
1772 if t.Arch.ArchType.Multilib == multilib {
1773 ret = append(ret, t)
1774 }
1775 }
1776 return ret
1777}
1778
Paul Duffinca7f0ef2020-02-25 15:50:49 +00001779// Return the set of Os specific common architecture targets for each Os in a list of
1780// targets.
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001781func getCommonTargets(targets []Target) []Target {
1782 var ret []Target
1783 set := make(map[string]bool)
1784
1785 for _, t := range targets {
1786 if _, found := set[t.Os.String()]; !found {
1787 set[t.Os.String()] = true
1788 ret = append(ret, commonTargetMap[t.Os.String()])
1789 }
1790 }
1791
1792 return ret
1793}
1794
Colin Cross3dceee32018-09-06 10:19:57 -07001795func firstTarget(targets []Target, filters ...string) []Target {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001796 for _, filter := range filters {
1797 buildTargets := filterMultilibTargets(targets, filter)
1798 if len(buildTargets) > 0 {
Colin Cross3dceee32018-09-06 10:19:57 -07001799 return buildTargets[:1]
Colin Cross6b4a32d2017-12-05 13:42:45 -08001800 }
1801 }
1802 return nil
1803}
1804
Colin Crossa1ad8d12016-06-01 17:09:44 -07001805// Use the module multilib setting to select one or more targets from a target list
Colin Crossee0bc3b2018-10-02 22:01:37 -07001806func decodeMultilibTargets(multilib string, targets []Target, prefer32 bool) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001807 buildTargets := []Target{}
Colin Cross6b4a32d2017-12-05 13:42:45 -08001808
Colin Cross4225f652015-09-17 14:33:42 -07001809 switch multilib {
1810 case "common":
Colin Cross6b4a32d2017-12-05 13:42:45 -08001811 buildTargets = getCommonTargets(targets)
1812 case "common_first":
1813 buildTargets = getCommonTargets(targets)
1814 if prefer32 {
Colin Cross3dceee32018-09-06 10:19:57 -07001815 buildTargets = append(buildTargets, firstTarget(targets, "lib32", "lib64")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001816 } else {
Colin Cross3dceee32018-09-06 10:19:57 -07001817 buildTargets = append(buildTargets, firstTarget(targets, "lib64", "lib32")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001818 }
Colin Cross4225f652015-09-17 14:33:42 -07001819 case "both":
Colin Cross8b74d172016-09-13 09:59:14 -07001820 if prefer32 {
1821 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1822 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1823 } else {
1824 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1825 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1826 }
Colin Cross4225f652015-09-17 14:33:42 -07001827 case "32":
Colin Cross69617d32016-09-06 10:39:07 -07001828 buildTargets = filterMultilibTargets(targets, "lib32")
Colin Cross4225f652015-09-17 14:33:42 -07001829 case "64":
Colin Cross69617d32016-09-06 10:39:07 -07001830 buildTargets = filterMultilibTargets(targets, "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001831 case "first":
1832 if prefer32 {
Colin Cross3dceee32018-09-06 10:19:57 -07001833 buildTargets = firstTarget(targets, "lib32", "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001834 } else {
Colin Cross3dceee32018-09-06 10:19:57 -07001835 buildTargets = firstTarget(targets, "lib64", "lib32")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001836 }
Colin Cross69617d32016-09-06 10:39:07 -07001837 case "prefer32":
Colin Cross3dceee32018-09-06 10:19:57 -07001838 buildTargets = filterMultilibTargets(targets, "lib32")
1839 if len(buildTargets) == 0 {
1840 buildTargets = filterMultilibTargets(targets, "lib64")
1841 }
Colin Cross4225f652015-09-17 14:33:42 -07001842 default:
Colin Cross69617d32016-09-06 10:39:07 -07001843 return nil, fmt.Errorf(`compile_multilib must be "both", "first", "32", "64", or "prefer32" found %q`,
Colin Cross4225f652015-09-17 14:33:42 -07001844 multilib)
Colin Cross4225f652015-09-17 14:33:42 -07001845 }
1846
Colin Crossa1ad8d12016-06-01 17:09:44 -07001847 return buildTargets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001848}