blob: 414113893b5f42daa4e4aff0596aafaf34292fa9 [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)
Dan Willemsen00fcbde2016-11-17 00:25:59 -0800581 Windows = NewOsType("windows", HostCross, true)
582 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
612 HostCross
613)
614
Colin Cross67a5c132017-05-09 13:45:28 -0700615func (class OsClass) String() string {
616 switch class {
617 case Generic:
618 return "generic"
619 case Device:
620 return "device"
621 case Host:
622 return "host"
623 case HostCross:
624 return "host cross"
625 default:
626 panic(fmt.Errorf("unknown class %d", class))
627 }
628}
629
Colin Crossa1ad8d12016-06-01 17:09:44 -0700630func (os OsType) String() string {
631 return os.Name
Colin Cross54c71122016-06-01 17:09:44 -0700632}
633
Dan Willemsen866b5632017-09-22 12:28:24 -0700634func (os OsType) Bionic() bool {
635 return os == Android || os == LinuxBionic
636}
637
638func (os OsType) Linux() bool {
639 return os == Android || os == Linux || os == LinuxBionic
640}
641
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800642func NewOsType(name string, class OsClass, defDisabled bool) OsType {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700643 os := OsType{
644 Name: name,
645 Field: strings.Title(name),
646 Class: class,
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800647
648 DefaultDisabled: defDisabled,
Colin Cross54c71122016-06-01 17:09:44 -0700649 }
Paul Duffina04c1072020-03-02 10:16:35 +0000650 OsTypeList = append(OsTypeList, os)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800651
652 if _, found := commonTargetMap[name]; found {
653 panic(fmt.Errorf("Found Os type duplicate during OsType registration: %q", name))
654 } else {
655 commonTargetMap[name] = Target{Os: os, Arch: Arch{ArchType: Common}}
656 }
657
Colin Crossa1ad8d12016-06-01 17:09:44 -0700658 return os
659}
660
661func osByName(name string) OsType {
Paul Duffina04c1072020-03-02 10:16:35 +0000662 for _, os := range OsTypeList {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700663 if os.Name == name {
664 return os
665 }
666 }
667
668 return NoOsType
Dan Willemsen490fd492015-11-24 17:53:15 -0800669}
670
dimitry1f33e402019-03-26 12:39:31 +0100671type NativeBridgeSupport bool
672
673const (
674 NativeBridgeDisabled NativeBridgeSupport = false
675 NativeBridgeEnabled NativeBridgeSupport = true
676)
677
Colin Crossa1ad8d12016-06-01 17:09:44 -0700678type Target struct {
dimitry8d6dde82019-07-11 10:23:53 +0200679 Os OsType
680 Arch Arch
681 NativeBridge NativeBridgeSupport
682 NativeBridgeHostArchName string
683 NativeBridgeRelativePath string
Colin Crossd3ba0392015-05-07 14:11:29 -0700684}
685
Colin Crossa1ad8d12016-06-01 17:09:44 -0700686func (target Target) String() string {
Colin Crossa195f912019-10-16 11:07:20 -0700687 return target.OsVariation() + "_" + target.ArchVariation()
688}
689
690func (target Target) OsVariation() string {
691 return target.Os.String()
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700692}
693
694func (target Target) ArchVariation() string {
695 var variation string
dimitry1f33e402019-03-26 12:39:31 +0100696 if target.NativeBridge {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700697 variation = "native_bridge_"
dimitry1f33e402019-03-26 12:39:31 +0100698 }
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700699 variation += target.Arch.String()
700
Colin Crossa195f912019-10-16 11:07:20 -0700701 return variation
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700702}
703
704func (target Target) Variations() []blueprint.Variation {
705 return []blueprint.Variation{
Colin Crossa195f912019-10-16 11:07:20 -0700706 {Mutator: "os", Variation: target.OsVariation()},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700707 {Mutator: "arch", Variation: target.ArchVariation()},
708 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800709}
710
Colin Cross617b88a2020-08-24 18:04:09 -0700711func osMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Crossa195f912019-10-16 11:07:20 -0700712 var module Module
713 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700714 if module, ok = bpctx.Module().(Module); !ok {
715 if bootstrap.IsBootstrapModule(bpctx.Module()) {
716 // Bootstrap Go modules are always the build OS or linux bionic.
717 config := bpctx.Config().(Config)
718 osNames := []string{config.BuildOSTarget.OsVariation()}
719 for _, hostCrossTarget := range config.Targets[LinuxBionic] {
720 if hostCrossTarget.Arch.ArchType == config.BuildOSTarget.Arch.ArchType {
721 osNames = append(osNames, hostCrossTarget.OsVariation())
722 }
723 }
724 osNames = FirstUniqueStrings(osNames)
725 bpctx.CreateVariations(osNames...)
726 }
Colin Crossa195f912019-10-16 11:07:20 -0700727 return
728 }
729
Colin Cross617b88a2020-08-24 18:04:09 -0700730 // Bootstrap Go module support above requires this mutator to be a
731 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
732 // filters out non-Soong modules. Now that we've handled them, create a
733 // normal android.BottomUpMutatorContext.
734 mctx := bottomUpMutatorContextFactory(bpctx, module, false)
735
Colin Crossa195f912019-10-16 11:07:20 -0700736 base := module.base()
737
738 if !base.ArchSpecific() {
739 return
740 }
741
742 osClasses := base.OsClassSupported()
743
744 var moduleOSList []OsType
745
Paul Duffina04c1072020-03-02 10:16:35 +0000746 for _, os := range OsTypeList {
Colin Crossa195f912019-10-16 11:07:20 -0700747 supportedClass := false
748 for _, osClass := range osClasses {
749 if os.Class == osClass {
750 supportedClass = true
751 }
752 }
753 if !supportedClass {
754 continue
755 }
756
757 if len(mctx.Config().Targets[os]) == 0 {
758 continue
759 }
760
761 moduleOSList = append(moduleOSList, os)
762 }
763
764 if len(moduleOSList) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900765 base.Disable()
Colin Crossa195f912019-10-16 11:07:20 -0700766 return
767 }
768
769 osNames := make([]string, len(moduleOSList))
770
771 for i, os := range moduleOSList {
772 osNames[i] = os.String()
773 }
774
Paul Duffin1356d8c2020-02-25 19:26:33 +0000775 createCommonOSVariant := base.commonProperties.CreateCommonOSVariant
776 if createCommonOSVariant {
777 // A CommonOS variant was requested so add it to the list of OS's variants to
778 // create. It needs to be added to the end because it needs to depend on the
779 // the other variants in the list returned by CreateVariations(...) and inter
780 // variant dependencies can only be created from a later variant in that list to
781 // an earlier one. That is because variants are always processed in the order in
782 // which they are returned from CreateVariations(...).
783 osNames = append(osNames, CommonOS.Name)
784 moduleOSList = append(moduleOSList, CommonOS)
Colin Crossa195f912019-10-16 11:07:20 -0700785 }
786
Paul Duffin1356d8c2020-02-25 19:26:33 +0000787 modules := mctx.CreateVariations(osNames...)
788 for i, m := range modules {
789 m.base().commonProperties.CompileOS = moduleOSList[i]
790 m.base().setOSProperties(mctx)
791 }
792
793 if createCommonOSVariant {
794 // A CommonOS variant was requested so add dependencies from it (the last one in
795 // the list) to the OS type specific variants.
796 last := len(modules) - 1
797 commonOSVariant := modules[last]
798 commonOSVariant.base().commonProperties.CommonOSVariant = true
799 for _, module := range modules[0:last] {
800 // Ignore modules that are enabled. Note, this will only avoid adding
801 // dependencies on OsType variants that are explicitly disabled in their
802 // properties. The CommonOS variant will still depend on disabled variants
803 // if they are disabled afterwards, e.g. in archMutator if
804 if module.Enabled() {
805 mctx.AddInterVariantDependency(commonOsToOsSpecificVariantTag, commonOSVariant, module)
806 }
807 }
808 }
809}
810
811// Identifies the dependency from CommonOS variant to the os specific variants.
812type commonOSTag struct{ blueprint.BaseDependencyTag }
813
814var commonOsToOsSpecificVariantTag = commonOSTag{}
815
816// Get the OsType specific variants for the current CommonOS variant.
817//
818// The returned list will only contain enabled OsType specific variants of the
819// module referenced in the supplied context. An empty list is returned if there
820// are no enabled variants or the supplied context is not for an CommonOS
821// variant.
822func GetOsSpecificVariantsOfCommonOSVariant(mctx BaseModuleContext) []Module {
823 var variants []Module
824 mctx.VisitDirectDeps(func(m Module) {
825 if mctx.OtherModuleDependencyTag(m) == commonOsToOsSpecificVariantTag {
826 if m.Enabled() {
827 variants = append(variants, m)
828 }
829 }
830 })
831
832 return variants
Colin Crossa195f912019-10-16 11:07:20 -0700833}
834
Colin Crossee0bc3b2018-10-02 22:01:37 -0700835// archMutator splits a module into a variant for each Target requested by the module. Target selection
836// for a module is in three levels, OsClass, mulitlib, and then Target.
837// OsClass selection is determined by:
838// - The HostOrDeviceSupported value passed in to InitAndroidArchModule by the module type factory, which selects
839// whether the module type can compile for host, device or both.
840// - The host_supported and device_supported properties on the module.
Roland Levillainf5b635d2019-06-05 14:42:57 +0100841// 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 -0700842// for the module, the Device OsClass is selected.
843// Within each selected OsClass, the multilib selection is determined by:
Jaewoong Jung02b2d4d2019-06-06 15:19:57 -0700844// - The compile_multilib property if it set (which may be overridden by target.android.compile_multilib or
Colin Crossee0bc3b2018-10-02 22:01:37 -0700845// target.host.compile_multilib).
846// - The default multilib passed to InitAndroidArchModule if compile_multilib was not set.
847// Valid multilib values include:
848// "both": compile for all Targets supported by the OsClass (generally x86_64 and x86, or arm64 and arm).
849// "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 -0700850// but may be arm for a 32-bit only build.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700851// "32": compile for only a single 32-bit Target supported by the OsClass.
852// "64": compile for only a single 64-bit Target supported by the OsClass.
853// "common": compile a for a single Target that will work on all Targets suported by the OsClass (for example Java).
854//
855// Once the list of Targets is determined, the module is split into a variant for each Target.
856//
857// Modules can be initialized with InitAndroidMultiTargetsArchModule, in which case they will be split by OsClass,
858// 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 -0700859func archMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700860 var module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800861 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700862 if module, ok = bpctx.Module().(Module); !ok {
863 if bootstrap.IsBootstrapModule(bpctx.Module()) {
864 // Bootstrap Go modules are always the build architecture.
865 bpctx.CreateVariations(bpctx.Config().(Config).BuildOSTarget.ArchVariation())
866 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800867 return
868 }
869
Colin Cross617b88a2020-08-24 18:04:09 -0700870 // Bootstrap Go module support above requires this mutator to be a
871 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
872 // filters out non-Soong modules. Now that we've handled them, create a
873 // normal android.BottomUpMutatorContext.
874 mctx := bottomUpMutatorContextFactory(bpctx, module, false)
875
Colin Cross5eca7cb2018-10-02 14:02:10 -0700876 base := module.base()
877
878 if !base.ArchSpecific() {
Colin Crossb9db4802016-06-03 01:50:47 +0000879 return
880 }
881
Colin Crossa195f912019-10-16 11:07:20 -0700882 os := base.commonProperties.CompileOS
Paul Duffin1356d8c2020-02-25 19:26:33 +0000883 if os == CommonOS {
884 // Make sure that the target related properties are initialized for the
885 // CommonOS variant.
886 addTargetProperties(module, commonTargetMap[os.Name], nil, true)
887
888 // Do not create arch specific variants for the CommonOS variant.
889 return
890 }
891
Colin Crossa195f912019-10-16 11:07:20 -0700892 osTargets := mctx.Config().Targets[os]
Colin Crossfb0c16e2019-11-20 17:12:35 -0800893 image := base.commonProperties.ImageVariation
Colin Crossa195f912019-10-16 11:07:20 -0700894 // Filter NativeBridge targets unless they are explicitly supported
Colin Cross83bead42019-12-18 10:45:46 -0800895 // Skip creating native bridge variants for vendor modules
896 if os == Android &&
897 !(Bool(base.commonProperties.Native_bridge_supported) && image == CoreVariation) {
898
Colin Crossa195f912019-10-16 11:07:20 -0700899 var targets []Target
900 for _, t := range osTargets {
901 if !t.NativeBridge {
902 targets = append(targets, t)
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700903 }
904 }
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700905
Colin Crossa195f912019-10-16 11:07:20 -0700906 osTargets = targets
907 }
Colin Crossee0bc3b2018-10-02 22:01:37 -0700908
Yifan Hong1b3348d2020-01-21 15:53:22 -0800909 // only the primary arch in the ramdisk / recovery partition
910 if os == Android && (module.InstallInRecovery() || module.InstallInRamdisk()) {
Colin Crossa195f912019-10-16 11:07:20 -0700911 osTargets = []Target{osTargets[0]}
912 }
dimitry1f33e402019-03-26 12:39:31 +0100913
Colin Crossa195f912019-10-16 11:07:20 -0700914 prefer32 := false
915 if base.prefer32 != nil {
916 prefer32 = base.prefer32(mctx, base, os.Class)
917 }
dimitry1f33e402019-03-26 12:39:31 +0100918
Colin Crossa195f912019-10-16 11:07:20 -0700919 multilib, extraMultilib := decodeMultilib(base, os.Class)
920 targets, err := decodeMultilibTargets(multilib, osTargets, prefer32)
921 if err != nil {
922 mctx.ModuleErrorf("%s", err.Error())
923 }
Colin Cross5eca7cb2018-10-02 14:02:10 -0700924
Colin Crossa195f912019-10-16 11:07:20 -0700925 var multiTargets []Target
926 if extraMultilib != "" {
927 multiTargets, err = decodeMultilibTargets(extraMultilib, osTargets, prefer32)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700928 if err != nil {
929 mctx.ModuleErrorf("%s", err.Error())
930 }
Colin Crossb9db4802016-06-03 01:50:47 +0000931 }
932
Colin Crossfb0c16e2019-11-20 17:12:35 -0800933 if image == RecoveryVariation {
934 primaryArch := mctx.Config().DevicePrimaryArchType()
935 targets = filterToArch(targets, primaryArch)
936 multiTargets = filterToArch(multiTargets, primaryArch)
937 }
938
Colin Crossa195f912019-10-16 11:07:20 -0700939 if len(targets) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900940 base.Disable()
Dan Willemsen3f32f032016-07-11 14:36:48 -0700941 return
942 }
943
Colin Crossa195f912019-10-16 11:07:20 -0700944 targetNames := make([]string, len(targets))
Colin Crossb9db4802016-06-03 01:50:47 +0000945
Colin Crossa195f912019-10-16 11:07:20 -0700946 for i, target := range targets {
947 targetNames[i] = target.ArchVariation()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700948 }
949
950 modules := mctx.CreateVariations(targetNames...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800951 for i, m := range modules {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000952 addTargetProperties(m, targets[i], multiTargets, i == 0)
Colin Cross617b88a2020-08-24 18:04:09 -0700953 m.base().setArchProperties(mctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800954 }
955}
956
Paul Duffin1356d8c2020-02-25 19:26:33 +0000957func addTargetProperties(m Module, target Target, multiTargets []Target, primaryTarget bool) {
958 m.base().commonProperties.CompileTarget = target
959 m.base().commonProperties.CompileMultiTargets = multiTargets
960 m.base().commonProperties.CompilePrimary = primaryTarget
961}
962
Colin Crossee0bc3b2018-10-02 22:01:37 -0700963func decodeMultilib(base *ModuleBase, class OsClass) (multilib, extraMultilib string) {
964 switch class {
965 case Device:
966 multilib = String(base.commonProperties.Target.Android.Compile_multilib)
967 case Host, HostCross:
968 multilib = String(base.commonProperties.Target.Host.Compile_multilib)
969 }
970 if multilib == "" {
971 multilib = String(base.commonProperties.Compile_multilib)
972 }
973 if multilib == "" {
974 multilib = base.commonProperties.Default_multilib
975 }
976
977 if base.commonProperties.UseTargetVariants {
978 return multilib, ""
979 } else {
980 // For app modules a single arch variant will be created per OS class which is expected to handle all the
981 // selected arches. Return the common-type as multilib and any Android.bp provided multilib as extraMultilib
982 if multilib == base.commonProperties.Default_multilib {
983 multilib = "first"
984 }
985 return base.commonProperties.Default_multilib, multilib
986 }
987}
988
Colin Crossfb0c16e2019-11-20 17:12:35 -0800989func filterToArch(targets []Target, arch ArchType) []Target {
990 for i := 0; i < len(targets); i++ {
991 if targets[i].Arch.ArchType != arch {
992 targets = append(targets[:i], targets[i+1:]...)
993 i--
994 }
995 }
996 return targets
997}
998
Colin Crosscbbd13f2020-01-17 14:08:22 -0800999type archPropTypeDesc struct {
1000 arch, multilib, target reflect.Type
1001}
1002
1003type archPropRoot struct {
1004 Arch, Multilib, Target interface{}
1005}
1006
1007// createArchPropTypeDesc takes a reflect.Type that is either a struct or a pointer to a struct, and
1008// returns lists of reflect.Types that contains the arch-variant properties inside structs for each
1009// arch, multilib and target property.
1010func createArchPropTypeDesc(props reflect.Type) []archPropTypeDesc {
Colin Crossb1d8c992020-01-21 11:43:29 -08001011 // Each property struct shard will be nested many times under the runtime generated arch struct,
1012 // which can hit the limit of 64kB for the name of runtime generated structs. They are nested
1013 // 97 times now, which may grow in the future, plus there is some overhead for the containing
1014 // type. This number may need to be reduced if too many are added, but reducing it too far
1015 // could cause problems if a single deeply nested property no longer fits in the name.
1016 const maxArchTypeNameSize = 500
1017
1018 propShards, _ := proptools.FilterPropertyStructSharded(props, maxArchTypeNameSize, filterArchStruct)
Colin Crosscb988072019-01-24 14:58:11 -08001019 if len(propShards) == 0 {
Dan Willemsenb1957a52016-06-23 23:44:54 -07001020 return nil
1021 }
1022
Colin Crosscbbd13f2020-01-17 14:08:22 -08001023 var ret []archPropTypeDesc
Colin Crossc17727d2018-10-24 12:42:09 -07001024 for _, props := range propShards {
Dan Willemsenb1957a52016-06-23 23:44:54 -07001025
Colin Crossc17727d2018-10-24 12:42:09 -07001026 variantFields := func(names []string) []reflect.StructField {
1027 ret := make([]reflect.StructField, len(names))
Dan Willemsenb1957a52016-06-23 23:44:54 -07001028
Colin Crossc17727d2018-10-24 12:42:09 -07001029 for i, name := range names {
1030 ret[i].Name = name
1031 ret[i].Type = props
Dan Willemsen866b5632017-09-22 12:28:24 -07001032 }
Colin Crossc17727d2018-10-24 12:42:09 -07001033
1034 return ret
1035 }
1036
1037 archFields := make([]reflect.StructField, len(archTypeList))
1038 for i, arch := range archTypeList {
1039 variants := []string{}
1040
1041 for _, archVariant := range archVariants[arch] {
1042 archVariant := variantReplacer.Replace(archVariant)
1043 variants = append(variants, proptools.FieldNameForProperty(archVariant))
1044 }
1045 for _, feature := range archFeatures[arch] {
1046 feature := variantReplacer.Replace(feature)
1047 variants = append(variants, proptools.FieldNameForProperty(feature))
1048 }
1049
1050 fields := variantFields(variants)
1051
1052 fields = append([]reflect.StructField{{
1053 Name: "BlueprintEmbed",
1054 Type: props,
1055 Anonymous: true,
1056 }}, fields...)
1057
1058 archFields[i] = reflect.StructField{
1059 Name: arch.Field,
1060 Type: reflect.StructOf(fields),
1061 }
1062 }
1063 archType := reflect.StructOf(archFields)
1064
1065 multilibType := reflect.StructOf(variantFields([]string{"Lib32", "Lib64"}))
1066
1067 targets := []string{
1068 "Host",
1069 "Android64",
1070 "Android32",
1071 "Bionic",
1072 "Linux",
1073 "Not_windows",
1074 "Arm_on_x86",
1075 "Arm_on_x86_64",
Victor Khimenkoc26fcf42020-05-07 22:16:33 +02001076 "Native_bridge",
Colin Crossc17727d2018-10-24 12:42:09 -07001077 }
Paul Duffina04c1072020-03-02 10:16:35 +00001078 for _, os := range OsTypeList {
Colin Crossc17727d2018-10-24 12:42:09 -07001079 targets = append(targets, os.Field)
1080
1081 for _, archType := range osArchTypeMap[os] {
1082 targets = append(targets, os.Field+"_"+archType.Name)
1083
1084 if os.Linux() {
1085 target := "Linux_" + archType.Name
1086 if !InList(target, targets) {
1087 targets = append(targets, target)
1088 }
1089 }
1090 if os.Bionic() {
1091 target := "Bionic_" + archType.Name
1092 if !InList(target, targets) {
1093 targets = append(targets, target)
1094 }
Dan Willemsen866b5632017-09-22 12:28:24 -07001095 }
1096 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001097 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001098
Colin Crossc17727d2018-10-24 12:42:09 -07001099 targetType := reflect.StructOf(variantFields(targets))
Colin Crosscbbd13f2020-01-17 14:08:22 -08001100
1101 ret = append(ret, archPropTypeDesc{
1102 arch: reflect.PtrTo(archType),
1103 multilib: reflect.PtrTo(multilibType),
1104 target: reflect.PtrTo(targetType),
1105 })
Colin Crossc17727d2018-10-24 12:42:09 -07001106 }
1107 return ret
Dan Willemsenb1957a52016-06-23 23:44:54 -07001108}
1109
Colin Cross74449102019-09-25 11:26:40 -07001110func filterArchStruct(field reflect.StructField, prefix string) (bool, reflect.StructField) {
1111 if proptools.HasTag(field, "android", "arch_variant") {
1112 // The arch_variant field isn't necessary past this point
1113 // Instead of wasting space, just remove it. Go also has a
1114 // 16-bit limit on structure name length. The name is constructed
1115 // based on the Go source representation of the structure, so
1116 // the tag names count towards that length.
Colin Crossb4fecbf2020-01-21 11:38:47 -08001117
1118 androidTag := field.Tag.Get("android")
1119 values := strings.Split(androidTag, ",")
1120
1121 if string(field.Tag) != `android:"`+strings.Join(values, ",")+`"` {
1122 panic(fmt.Errorf("unexpected tag format %q", field.Tag))
Colin Cross74449102019-09-25 11:26:40 -07001123 }
Colin Crossb4fecbf2020-01-21 11:38:47 -08001124 // these tags don't need to be present in the runtime generated struct type.
1125 values = RemoveListFromList(values, []string{"arch_variant", "variant_prepend", "path"})
1126 if len(values) > 0 {
1127 panic(fmt.Errorf("unknown tags %q in field %q", values, prefix+field.Name))
1128 }
1129
1130 field.Tag = ""
Colin Cross74449102019-09-25 11:26:40 -07001131 return true, field
1132 }
1133 return false, field
1134}
1135
Dan Willemsenb1957a52016-06-23 23:44:54 -07001136var archPropTypeMap OncePer
1137
Colin Cross36242852017-06-23 15:06:31 -07001138func InitArchModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001139
1140 base := m.base()
1141
Colin Cross36242852017-06-23 15:06:31 -07001142 base.generalProperties = m.GetProperties()
Colin Cross3f40fa42015-01-30 17:27:36 -08001143
1144 for _, properties := range base.generalProperties {
1145 propertiesValue := reflect.ValueOf(properties)
Colin Cross62496a02016-08-08 15:49:17 -07001146 t := propertiesValue.Type()
Colin Cross3f40fa42015-01-30 17:27:36 -08001147 if propertiesValue.Kind() != reflect.Ptr {
Colin Crossca860ac2016-01-04 14:34:37 -08001148 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
1149 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001150 }
1151
1152 propertiesValue = propertiesValue.Elem()
1153 if propertiesValue.Kind() != reflect.Struct {
Colin Crossca860ac2016-01-04 14:34:37 -08001154 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
1155 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001156 }
1157
Colin Cross571cccf2019-02-04 11:22:08 -08001158 archPropTypes := archPropTypeMap.Once(NewCustomOnceKey(t), func() interface{} {
Colin Crosscbbd13f2020-01-17 14:08:22 -08001159 return createArchPropTypeDesc(t)
1160 }).([]archPropTypeDesc)
Colin Cross3f40fa42015-01-30 17:27:36 -08001161
Colin Crossc17727d2018-10-24 12:42:09 -07001162 var archProperties []interface{}
1163 for _, t := range archPropTypes {
Colin Crosscbbd13f2020-01-17 14:08:22 -08001164 archProperties = append(archProperties, &archPropRoot{
1165 Arch: reflect.Zero(t.arch).Interface(),
1166 Multilib: reflect.Zero(t.multilib).Interface(),
1167 Target: reflect.Zero(t.target).Interface(),
1168 })
Dan Willemsenb1957a52016-06-23 23:44:54 -07001169 }
Colin Crossc17727d2018-10-24 12:42:09 -07001170 base.archProperties = append(base.archProperties, archProperties)
1171 m.AddProperties(archProperties...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001172 }
1173
Colin Cross36242852017-06-23 15:06:31 -07001174 base.customizableProperties = m.GetProperties()
Colin Cross3f40fa42015-01-30 17:27:36 -08001175}
1176
Colin Crossa716add2015-12-16 11:07:39 -08001177var variantReplacer = strings.NewReplacer("-", "_", ".", "_")
Colin Crossec193632015-07-06 17:49:43 -07001178
Colin Cross4157e882019-06-06 16:57:04 -07001179func (m *ModuleBase) appendProperties(ctx BottomUpMutatorContext,
Dan Willemsenb1957a52016-06-23 23:44:54 -07001180 dst interface{}, src reflect.Value, field, srcPrefix string) reflect.Value {
Colin Cross06a931b2015-10-28 17:23:31 -07001181
Colin Crosscbbd13f2020-01-17 14:08:22 -08001182 if src.Kind() == reflect.Ptr {
1183 if src.IsNil() {
1184 return src
1185 }
1186 src = src.Elem()
1187 }
1188
Dan Willemsenb1957a52016-06-23 23:44:54 -07001189 src = src.FieldByName(field)
1190 if !src.IsValid() {
Colin Crosseeabb892015-11-20 13:07:51 -08001191 ctx.ModuleErrorf("field %q does not exist", srcPrefix)
Dan Willemsenb1957a52016-06-23 23:44:54 -07001192 return src
Colin Cross85a88972015-11-23 13:29:51 -08001193 }
1194
Dan Willemsenb1957a52016-06-23 23:44:54 -07001195 ret := src
Colin Cross85a88972015-11-23 13:29:51 -08001196
Dan Willemsenb1957a52016-06-23 23:44:54 -07001197 if src.Kind() == reflect.Struct {
1198 src = src.FieldByName("BlueprintEmbed")
Colin Cross06a931b2015-10-28 17:23:31 -07001199 }
1200
Colin Cross6ee75b62016-05-05 15:57:15 -07001201 order := func(property string,
1202 dstField, srcField reflect.StructField,
1203 dstValue, srcValue interface{}) (proptools.Order, error) {
1204 if proptools.HasTag(dstField, "android", "variant_prepend") {
1205 return proptools.Prepend, nil
1206 } else {
1207 return proptools.Append, nil
1208 }
1209 }
1210
Dan Willemsenb1957a52016-06-23 23:44:54 -07001211 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, order)
Colin Cross06a931b2015-10-28 17:23:31 -07001212 if err != nil {
1213 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
1214 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
1215 } else {
1216 panic(err)
1217 }
1218 }
Colin Cross85a88972015-11-23 13:29:51 -08001219
Dan Willemsenb1957a52016-06-23 23:44:54 -07001220 return ret
Colin Cross06a931b2015-10-28 17:23:31 -07001221}
1222
Colin Crossa195f912019-10-16 11:07:20 -07001223// Rewrite the module's properties structs to contain os-specific values.
1224func (m *ModuleBase) setOSProperties(ctx BottomUpMutatorContext) {
1225 os := m.commonProperties.CompileOS
1226
1227 for i := range m.generalProperties {
1228 genProps := m.generalProperties[i]
1229 if m.archProperties[i] == nil {
1230 continue
1231 }
1232 for _, archProperties := range m.archProperties[i] {
1233 archPropValues := reflect.ValueOf(archProperties).Elem()
1234
Colin Crosscbbd13f2020-01-17 14:08:22 -08001235 targetProp := archPropValues.FieldByName("Target").Elem()
Colin Crossa195f912019-10-16 11:07:20 -07001236
1237 // Handle host-specific properties in the form:
1238 // target: {
1239 // host: {
1240 // key: value,
1241 // },
1242 // },
1243 if os.Class == Host || os.Class == HostCross {
1244 field := "Host"
1245 prefix := "target.host"
1246 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1247 }
1248
1249 // Handle target OS generalities of the form:
1250 // target: {
1251 // bionic: {
1252 // key: value,
1253 // },
1254 // }
1255 if os.Linux() {
1256 field := "Linux"
1257 prefix := "target.linux"
1258 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1259 }
1260
1261 if os.Bionic() {
1262 field := "Bionic"
1263 prefix := "target.bionic"
1264 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1265 }
1266
1267 // Handle target OS properties in the form:
1268 // target: {
1269 // linux_glibc: {
1270 // key: value,
1271 // },
1272 // not_windows: {
1273 // key: value,
1274 // },
1275 // android {
1276 // key: value,
1277 // },
1278 // },
1279 field := os.Field
1280 prefix := "target." + os.Name
1281 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1282
1283 if (os.Class == Host || os.Class == HostCross) && os != Windows {
1284 field := "Not_windows"
1285 prefix := "target.not_windows"
1286 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1287 }
1288
1289 // Handle 64-bit device properties in the form:
1290 // target {
1291 // android64 {
1292 // key: value,
1293 // },
1294 // android32 {
1295 // key: value,
1296 // },
1297 // },
1298 // WARNING: this is probably not what you want to use in your blueprints file, it selects
1299 // options for all targets on a device that supports 64-bit binaries, not just the targets
1300 // that are being compiled for 64-bit. Its expected use case is binaries like linker and
1301 // debuggerd that need to know when they are a 32-bit process running on a 64-bit device
1302 if os.Class == Device {
1303 if ctx.Config().Android64() {
1304 field := "Android64"
1305 prefix := "target.android64"
1306 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1307 } else {
1308 field := "Android32"
1309 prefix := "target.android32"
1310 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1311 }
1312 }
1313 }
1314 }
1315}
1316
Colin Cross3f40fa42015-01-30 17:27:36 -08001317// Rewrite the module's properties structs to contain arch-specific values.
Colin Cross4157e882019-06-06 16:57:04 -07001318func (m *ModuleBase) setArchProperties(ctx BottomUpMutatorContext) {
1319 arch := m.Arch()
1320 os := m.Os()
Colin Crossd3ba0392015-05-07 14:11:29 -07001321
Colin Cross4157e882019-06-06 16:57:04 -07001322 for i := range m.generalProperties {
1323 genProps := m.generalProperties[i]
1324 if m.archProperties[i] == nil {
Dan Willemsenb1957a52016-06-23 23:44:54 -07001325 continue
1326 }
Colin Cross4157e882019-06-06 16:57:04 -07001327 for _, archProperties := range m.archProperties[i] {
Colin Crossc17727d2018-10-24 12:42:09 -07001328 archPropValues := reflect.ValueOf(archProperties).Elem()
Dan Willemsenb1957a52016-06-23 23:44:54 -07001329
Colin Crosscbbd13f2020-01-17 14:08:22 -08001330 archProp := archPropValues.FieldByName("Arch").Elem()
1331 multilibProp := archPropValues.FieldByName("Multilib").Elem()
1332 targetProp := archPropValues.FieldByName("Target").Elem()
Dan Willemsenb1957a52016-06-23 23:44:54 -07001333
Colin Crossc17727d2018-10-24 12:42:09 -07001334 // Handle arch-specific properties in the form:
Colin Crossd5934c82017-10-02 13:55:26 -07001335 // arch: {
Colin Crossc17727d2018-10-24 12:42:09 -07001336 // arm64: {
Colin Crossd5934c82017-10-02 13:55:26 -07001337 // key: value,
1338 // },
1339 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001340 t := arch.ArchType
1341
1342 if arch.ArchType != Common {
1343 field := proptools.FieldNameForProperty(t.Name)
1344 prefix := "arch." + t.Name
Colin Cross4157e882019-06-06 16:57:04 -07001345 archStruct := m.appendProperties(ctx, genProps, archProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001346
1347 // Handle arch-variant-specific properties in the form:
1348 // arch: {
1349 // variant: {
1350 // key: value,
1351 // },
1352 // },
1353 v := variantReplacer.Replace(arch.ArchVariant)
1354 if v != "" {
1355 field := proptools.FieldNameForProperty(v)
1356 prefix := "arch." + t.Name + "." + v
Colin Cross4157e882019-06-06 16:57:04 -07001357 m.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001358 }
1359
1360 // Handle cpu-variant-specific properties in the form:
1361 // arch: {
1362 // variant: {
1363 // key: value,
1364 // },
1365 // },
1366 if arch.CpuVariant != arch.ArchVariant {
1367 c := variantReplacer.Replace(arch.CpuVariant)
1368 if c != "" {
1369 field := proptools.FieldNameForProperty(c)
1370 prefix := "arch." + t.Name + "." + c
Colin Cross4157e882019-06-06 16:57:04 -07001371 m.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001372 }
1373 }
1374
1375 // Handle arch-feature-specific properties in the form:
1376 // arch: {
1377 // feature: {
1378 // key: value,
1379 // },
1380 // },
1381 for _, feature := range arch.ArchFeatures {
1382 field := proptools.FieldNameForProperty(feature)
1383 prefix := "arch." + t.Name + "." + feature
Colin Cross4157e882019-06-06 16:57:04 -07001384 m.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001385 }
1386
1387 // Handle multilib-specific properties in the form:
1388 // multilib: {
1389 // lib32: {
1390 // key: value,
1391 // },
1392 // },
1393 field = proptools.FieldNameForProperty(t.Multilib)
1394 prefix = "multilib." + t.Multilib
Colin Cross4157e882019-06-06 16:57:04 -07001395 m.appendProperties(ctx, genProps, multilibProp, field, prefix)
Colin Cross08016332016-12-20 09:53:14 -08001396 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001397
Colin Crossa195f912019-10-16 11:07:20 -07001398 // Handle combined OS-feature and arch specific properties in the form:
Colin Crossc17727d2018-10-24 12:42:09 -07001399 // target: {
Colin Crossc17727d2018-10-24 12:42:09 -07001400 // bionic_x86: {
1401 // key: value,
1402 // },
1403 // }
Colin Crossa195f912019-10-16 11:07:20 -07001404 if os.Linux() && arch.ArchType != Common {
1405 field := "Linux_" + arch.ArchType.Name
1406 prefix := "target.linux_" + arch.ArchType.Name
Colin Cross4157e882019-06-06 16:57:04 -07001407 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossd5934c82017-10-02 13:55:26 -07001408 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001409
Colin Crossa195f912019-10-16 11:07:20 -07001410 if os.Bionic() && arch.ArchType != Common {
1411 field := "Bionic_" + t.Name
1412 prefix := "target.bionic_" + t.Name
Colin Cross4157e882019-06-06 16:57:04 -07001413 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001414 }
1415
Colin Crossa195f912019-10-16 11:07:20 -07001416 // Handle combined OS and arch specific properties in the form:
Colin Crossc17727d2018-10-24 12:42:09 -07001417 // target: {
Colin Crossc17727d2018-10-24 12:42:09 -07001418 // linux_glibc_x86: {
1419 // key: value,
1420 // },
1421 // linux_glibc_arm: {
1422 // key: value,
1423 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001424 // android_arm {
1425 // key: value,
1426 // },
1427 // android_x86 {
Colin Crossd5934c82017-10-02 13:55:26 -07001428 // key: value,
1429 // },
1430 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001431 if arch.ArchType != Common {
Colin Crossa195f912019-10-16 11:07:20 -07001432 field := os.Field + "_" + t.Name
1433 prefix := "target." + os.Name + "_" + t.Name
Colin Cross4157e882019-06-06 16:57:04 -07001434 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossd5934c82017-10-02 13:55:26 -07001435 }
1436
Colin Crossa195f912019-10-16 11:07:20 -07001437 // Handle arm on x86 properties in the form:
Colin Crossc17727d2018-10-24 12:42:09 -07001438 // target {
Colin Crossa195f912019-10-16 11:07:20 -07001439 // arm_on_x86 {
Colin Crossc17727d2018-10-24 12:42:09 -07001440 // key: value,
1441 // },
Colin Crossa195f912019-10-16 11:07:20 -07001442 // arm_on_x86_64 {
Colin Crossd5934c82017-10-02 13:55:26 -07001443 // key: value,
1444 // },
1445 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001446 if os.Class == Device {
Victor Khimenko1a31f802020-09-17 03:07:31 +02001447 if arch.ArchType == X86 && (hasArmAbi(arch) ||
1448 hasArmAndroidArch(ctx.Config().Targets[Android])) {
Colin Crossc17727d2018-10-24 12:42:09 -07001449 field := "Arm_on_x86"
1450 prefix := "target.arm_on_x86"
Colin Cross4157e882019-06-06 16:57:04 -07001451 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001452 }
Victor Khimenko1a31f802020-09-17 03:07:31 +02001453 if arch.ArchType == X86_64 && (hasArmAbi(arch) ||
1454 hasArmAndroidArch(ctx.Config().Targets[Android])) {
Colin Crossc17727d2018-10-24 12:42:09 -07001455 field := "Arm_on_x86_64"
1456 prefix := "target.arm_on_x86_64"
Colin Cross4157e882019-06-06 16:57:04 -07001457 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001458 }
Victor Khimenkoc26fcf42020-05-07 22:16:33 +02001459 if os == Android && m.Target().NativeBridge == NativeBridgeEnabled {
1460 field := "Native_bridge"
1461 prefix := "target.native_bridge"
1462 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1463 }
Colin Cross4247f0d2017-04-13 16:56:14 -07001464 }
Colin Crossbb2e2b72016-12-08 17:23:53 -08001465 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001466 }
1467}
1468
1469func forEachInterface(v reflect.Value, f func(reflect.Value)) {
1470 switch v.Kind() {
1471 case reflect.Interface:
1472 f(v)
1473 case reflect.Struct:
1474 for i := 0; i < v.NumField(); i++ {
1475 forEachInterface(v.Field(i), f)
1476 }
1477 case reflect.Ptr:
1478 forEachInterface(v.Elem(), f)
1479 default:
1480 panic(fmt.Errorf("Unsupported kind %s", v.Kind()))
1481 }
1482}
Colin Cross4225f652015-09-17 14:33:42 -07001483
Colin Crossa1ad8d12016-06-01 17:09:44 -07001484// Convert the arch product variables into a list of targets for each os class structs
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001485func decodeTargetProductVariables(config *config) (map[OsType][]Target, error) {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001486 variables := config.productVariables
Dan Willemsen490fd492015-11-24 17:53:15 -08001487
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001488 targets := make(map[OsType][]Target)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001489 var targetErr error
1490
dimitry1f33e402019-03-26 12:39:31 +01001491 addTarget := func(os OsType, archName string, archVariant, cpuVariant *string, abi []string,
dimitry8d6dde82019-07-11 10:23:53 +02001492 nativeBridgeEnabled NativeBridgeSupport, nativeBridgeHostArchName *string,
1493 nativeBridgeRelativePath *string) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001494 if targetErr != nil {
1495 return
Dan Willemsen490fd492015-11-24 17:53:15 -08001496 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001497
Dan Willemsen01a3c252019-01-11 19:02:16 -08001498 arch, err := decodeArch(os, archName, archVariant, cpuVariant, abi)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001499 if err != nil {
1500 targetErr = err
1501 return
1502 }
dimitry8d6dde82019-07-11 10:23:53 +02001503 nativeBridgeRelativePathStr := String(nativeBridgeRelativePath)
1504 nativeBridgeHostArchNameStr := String(nativeBridgeHostArchName)
1505
1506 // Use guest arch as relative install path by default
1507 if nativeBridgeEnabled && nativeBridgeRelativePathStr == "" {
1508 nativeBridgeRelativePathStr = arch.ArchType.String()
1509 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001510
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001511 targets[os] = append(targets[os],
Colin Crossa1ad8d12016-06-01 17:09:44 -07001512 Target{
dimitry8d6dde82019-07-11 10:23:53 +02001513 Os: os,
1514 Arch: arch,
1515 NativeBridge: nativeBridgeEnabled,
1516 NativeBridgeHostArchName: nativeBridgeHostArchNameStr,
1517 NativeBridgeRelativePath: nativeBridgeRelativePathStr,
Colin Crossa1ad8d12016-06-01 17:09:44 -07001518 })
Dan Willemsen490fd492015-11-24 17:53:15 -08001519 }
1520
Colin Cross4225f652015-09-17 14:33:42 -07001521 if variables.HostArch == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001522 return nil, fmt.Errorf("No host primary architecture set")
Colin Cross4225f652015-09-17 14:33:42 -07001523 }
1524
dimitry8d6dde82019-07-11 10:23:53 +02001525 addTarget(BuildOs, *variables.HostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001526
Colin Crosseeabb892015-11-20 13:07:51 -08001527 if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" {
dimitry8d6dde82019-07-11 10:23:53 +02001528 addTarget(BuildOs, *variables.HostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001529 }
1530
Colin Crossff3ae9d2018-04-10 16:15:18 -07001531 if Bool(config.Host_bionic) {
dimitry8d6dde82019-07-11 10:23:53 +02001532 addTarget(LinuxBionic, "x86_64", nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen01a405a2016-06-13 17:19:03 -07001533 }
1534
Colin Crossff3ae9d2018-04-10 16:15:18 -07001535 if String(variables.CrossHost) != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001536 crossHostOs := osByName(*variables.CrossHost)
1537 if crossHostOs == NoOsType {
1538 return nil, fmt.Errorf("Unknown cross host OS %q", *variables.CrossHost)
1539 }
1540
Colin Crossff3ae9d2018-04-10 16:15:18 -07001541 if String(variables.CrossHostArch) == "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001542 return nil, fmt.Errorf("No cross-host primary architecture set")
Dan Willemsen490fd492015-11-24 17:53:15 -08001543 }
1544
dimitry8d6dde82019-07-11 10:23:53 +02001545 addTarget(crossHostOs, *variables.CrossHostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001546
1547 if variables.CrossHostSecondaryArch != nil && *variables.CrossHostSecondaryArch != "" {
dimitry8d6dde82019-07-11 10:23:53 +02001548 addTarget(crossHostOs, *variables.CrossHostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001549 }
1550 }
1551
Dan Willemsen3f32f032016-07-11 14:36:48 -07001552 if variables.DeviceArch != nil && *variables.DeviceArch != "" {
Doug Horn21b94272019-01-16 12:06:11 -08001553 var target = Android
1554 if Bool(variables.Fuchsia) {
1555 target = Fuchsia
1556 }
1557
1558 addTarget(target, *variables.DeviceArch, variables.DeviceArchVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001559 variables.DeviceCpuVariant, variables.DeviceAbi, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001560
Dan Willemsen3f32f032016-07-11 14:36:48 -07001561 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" {
1562 addTarget(Android, *variables.DeviceSecondaryArch,
1563 variables.DeviceSecondaryArchVariant, variables.DeviceSecondaryCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001564 variables.DeviceSecondaryAbi, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001565 }
dimitry1f33e402019-03-26 12:39:31 +01001566
1567 if variables.NativeBridgeArch != nil && *variables.NativeBridgeArch != "" {
1568 addTarget(Android, *variables.NativeBridgeArch,
1569 variables.NativeBridgeArchVariant, variables.NativeBridgeCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001570 variables.NativeBridgeAbi, NativeBridgeEnabled, variables.DeviceArch,
1571 variables.NativeBridgeRelativePath)
dimitry1f33e402019-03-26 12:39:31 +01001572 }
1573
1574 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" &&
1575 variables.NativeBridgeSecondaryArch != nil && *variables.NativeBridgeSecondaryArch != "" {
1576 addTarget(Android, *variables.NativeBridgeSecondaryArch,
1577 variables.NativeBridgeSecondaryArchVariant,
1578 variables.NativeBridgeSecondaryCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001579 variables.NativeBridgeSecondaryAbi,
1580 NativeBridgeEnabled,
1581 variables.DeviceSecondaryArch,
1582 variables.NativeBridgeSecondaryRelativePath)
dimitry1f33e402019-03-26 12:39:31 +01001583 }
Colin Cross4225f652015-09-17 14:33:42 -07001584 }
1585
Colin Crossa1ad8d12016-06-01 17:09:44 -07001586 if targetErr != nil {
1587 return nil, targetErr
1588 }
1589
1590 return targets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001591}
1592
Colin Crossbb2e2b72016-12-08 17:23:53 -08001593// hasArmAbi returns true if arch has at least one arm ABI
1594func hasArmAbi(arch Arch) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -08001595 return PrefixInList(arch.Abi, "arm")
Colin Crossbb2e2b72016-12-08 17:23:53 -08001596}
1597
dimitry628db6f2019-05-22 17:16:21 +02001598// hasArmArch returns true if targets has at least non-native_bridge arm Android arch
Colin Cross4247f0d2017-04-13 16:56:14 -07001599func hasArmAndroidArch(targets []Target) bool {
1600 for _, target := range targets {
Victor Khimenko1a31f802020-09-17 03:07:31 +02001601 if target.Os == Android && target.Arch.ArchType == Arm {
Victor Khimenko5eb8ec12018-03-21 20:30:54 +01001602 return true
1603 }
1604 }
1605 return false
1606}
1607
Dan Albert4098deb2016-10-19 14:04:41 -07001608type archConfig struct {
1609 arch string
1610 archVariant string
1611 cpuVariant string
1612 abi []string
1613}
1614
1615func getMegaDeviceConfig() []archConfig {
1616 return []archConfig{
Dan Albert8818f492019-02-19 13:53:01 -08001617 {"arm", "armv7-a", "generic", []string{"armeabi-v7a"}},
Dan Willemsen110a89d2016-01-14 15:17:19 -08001618 {"arm", "armv7-a-neon", "generic", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001619 {"arm", "armv7-a-neon", "cortex-a7", []string{"armeabi-v7a"}},
1620 {"arm", "armv7-a-neon", "cortex-a8", []string{"armeabi-v7a"}},
Dan Willemsen110a89d2016-01-14 15:17:19 -08001621 {"arm", "armv7-a-neon", "cortex-a9", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001622 {"arm", "armv7-a-neon", "cortex-a15", []string{"armeabi-v7a"}},
1623 {"arm", "armv7-a-neon", "cortex-a53", []string{"armeabi-v7a"}},
1624 {"arm", "armv7-a-neon", "cortex-a53.a57", []string{"armeabi-v7a"}},
Richard Fungeb37ed32018-09-24 16:33:45 -07001625 {"arm", "armv7-a-neon", "cortex-a72", []string{"armeabi-v7a"}},
Jake Weinstein6600a442017-05-15 18:27:12 -04001626 {"arm", "armv7-a-neon", "cortex-a73", []string{"armeabi-v7a"}},
Christopher Ferrisba14a8f2018-04-23 18:15:25 -07001627 {"arm", "armv7-a-neon", "cortex-a75", []string{"armeabi-v7a"}},
Haibo Huanga31e2bd2018-10-09 14:27:28 -07001628 {"arm", "armv7-a-neon", "cortex-a76", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001629 {"arm", "armv7-a-neon", "krait", []string{"armeabi-v7a"}},
Alex Naidisae4fc182016-08-20 00:14:56 +02001630 {"arm", "armv7-a-neon", "kryo", []string{"armeabi-v7a"}},
Artem Serovd3072b02018-11-15 15:21:51 +00001631 {"arm", "armv7-a-neon", "kryo385", []string{"armeabi-v7a"}},
Junmo Park8ea49592017-07-24 07:14:55 +09001632 {"arm", "armv7-a-neon", "exynos-m1", []string{"armeabi-v7a"}},
Junmo Parkd86c9022017-07-21 09:07:47 +09001633 {"arm", "armv7-a-neon", "exynos-m2", []string{"armeabi-v7a"}},
Dan Willemsen110a89d2016-01-14 15:17:19 -08001634 {"arm64", "armv8-a", "cortex-a53", []string{"arm64-v8a"}},
Richard Fungeb37ed32018-09-24 16:33:45 -07001635 {"arm64", "armv8-a", "cortex-a72", []string{"arm64-v8a"}},
Jake Weinstein6600a442017-05-15 18:27:12 -04001636 {"arm64", "armv8-a", "cortex-a73", []string{"arm64-v8a"}},
Alex Naidisac01ff52016-08-30 15:56:33 +02001637 {"arm64", "armv8-a", "kryo", []string{"arm64-v8a"}},
Junmo Park8ea49592017-07-24 07:14:55 +09001638 {"arm64", "armv8-a", "exynos-m1", []string{"arm64-v8a"}},
Junmo Parkd86c9022017-07-21 09:07:47 +09001639 {"arm64", "armv8-a", "exynos-m2", []string{"arm64-v8a"}},
Artem Serovd3072b02018-11-15 15:21:51 +00001640 {"arm64", "armv8-2a", "kryo385", []string{"arm64-v8a"}},
Raphael Gault70b96b02020-06-18 09:56:53 +00001641 {"arm64", "armv8-2a-dotprod", "cortex-a55", []string{"arm64-v8a"}},
1642 {"arm64", "armv8-2a-dotprod", "cortex-a75", []string{"arm64-v8a"}},
1643 {"arm64", "armv8-2a-dotprod", "cortex-a76", []string{"arm64-v8a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001644 {"x86", "", "", []string{"x86"}},
1645 {"x86", "atom", "", []string{"x86"}},
1646 {"x86", "haswell", "", []string{"x86"}},
1647 {"x86", "ivybridge", "", []string{"x86"}},
1648 {"x86", "sandybridge", "", []string{"x86"}},
1649 {"x86", "silvermont", "", []string{"x86"}},
Benjamin Gordon87e7f2f2019-02-14 10:59:48 -07001650 {"x86", "stoneyridge", "", []string{"x86"}},
Dan Willemsen8a354052016-05-10 14:30:51 -07001651 {"x86", "x86_64", "", []string{"x86"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001652 {"x86_64", "", "", []string{"x86_64"}},
1653 {"x86_64", "haswell", "", []string{"x86_64"}},
1654 {"x86_64", "ivybridge", "", []string{"x86_64"}},
1655 {"x86_64", "sandybridge", "", []string{"x86_64"}},
1656 {"x86_64", "silvermont", "", []string{"x86_64"}},
Benjamin Gordon87e7f2f2019-02-14 10:59:48 -07001657 {"x86_64", "stoneyridge", "", []string{"x86_64"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001658 }
Dan Albert4098deb2016-10-19 14:04:41 -07001659}
Dan Willemsen322acaf2016-01-12 23:07:05 -08001660
Dan Albert4098deb2016-10-19 14:04:41 -07001661func getNdkAbisConfig() []archConfig {
1662 return []archConfig{
Dan Albert6bba6442020-01-30 15:16:49 -08001663 {"arm", "armv7-a", "", []string{"armeabi-v7a"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001664 {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001665 {"x86", "", "", []string{"x86"}},
1666 {"x86_64", "", "", []string{"x86_64"}},
1667 }
1668}
1669
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001670func getAmlAbisConfig() []archConfig {
1671 return []archConfig{
1672 {"arm", "armv7-a", "", []string{"armeabi-v7a"}},
1673 {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
1674 {"x86", "", "", []string{"x86"}},
1675 {"x86_64", "", "", []string{"x86_64"}},
1676 }
1677}
1678
Dan Willemsen01a3c252019-01-11 19:02:16 -08001679func decodeArchSettings(os OsType, archConfigs []archConfig) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001680 var ret []Target
Dan Willemsen322acaf2016-01-12 23:07:05 -08001681
Dan Albert4098deb2016-10-19 14:04:41 -07001682 for _, config := range archConfigs {
Dan Willemsen01a3c252019-01-11 19:02:16 -08001683 arch, err := decodeArch(os, config.arch, &config.archVariant,
Colin Crossa74ca042019-01-31 14:31:51 -08001684 &config.cpuVariant, config.abi)
Dan Willemsen322acaf2016-01-12 23:07:05 -08001685 if err != nil {
1686 return nil, err
1687 }
Colin Cross3b19f5d2019-09-17 14:45:31 -07001688
Colin Crossa1ad8d12016-06-01 17:09:44 -07001689 ret = append(ret, Target{
1690 Os: Android,
1691 Arch: arch,
1692 })
Dan Willemsen322acaf2016-01-12 23:07:05 -08001693 }
1694
1695 return ret, nil
1696}
1697
Colin Cross4225f652015-09-17 14:33:42 -07001698// Convert a set of strings from product variables into a single Arch struct
Colin Crossa74ca042019-01-31 14:31:51 -08001699func decodeArch(os OsType, arch string, archVariant, cpuVariant *string, abi []string) (Arch, error) {
Colin Cross4225f652015-09-17 14:33:42 -07001700 stringPtr := func(p *string) string {
1701 if p != nil {
1702 return *p
1703 }
1704 return ""
1705 }
1706
Colin Crosseeabb892015-11-20 13:07:51 -08001707 archType, ok := archTypeMap[arch]
1708 if !ok {
1709 return Arch{}, fmt.Errorf("unknown arch %q", arch)
1710 }
Colin Cross4225f652015-09-17 14:33:42 -07001711
Colin Crosseeabb892015-11-20 13:07:51 -08001712 a := Arch{
Colin Cross4225f652015-09-17 14:33:42 -07001713 ArchType: archType,
1714 ArchVariant: stringPtr(archVariant),
1715 CpuVariant: stringPtr(cpuVariant),
Colin Crossa74ca042019-01-31 14:31:51 -08001716 Abi: abi,
Colin Crosseeabb892015-11-20 13:07:51 -08001717 }
1718
1719 if a.ArchVariant == a.ArchType.Name || a.ArchVariant == "generic" {
1720 a.ArchVariant = ""
1721 }
1722
1723 if a.CpuVariant == a.ArchType.Name || a.CpuVariant == "generic" {
1724 a.CpuVariant = ""
1725 }
1726
1727 for i := 0; i < len(a.Abi); i++ {
1728 if a.Abi[i] == "" {
1729 a.Abi = append(a.Abi[:i], a.Abi[i+1:]...)
1730 i--
1731 }
1732 }
1733
Dan Willemsen01a3c252019-01-11 19:02:16 -08001734 if a.ArchVariant == "" {
1735 if featureMap, ok := defaultArchFeatureMap[os]; ok {
1736 a.ArchFeatures = featureMap[archType]
1737 }
1738 } else {
1739 if featureMap, ok := archFeatureMap[archType]; ok {
1740 a.ArchFeatures = featureMap[a.ArchVariant]
1741 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001742 }
1743
Colin Crosseeabb892015-11-20 13:07:51 -08001744 return a, nil
Colin Cross4225f652015-09-17 14:33:42 -07001745}
1746
Colin Cross69617d32016-09-06 10:39:07 -07001747func filterMultilibTargets(targets []Target, multilib string) []Target {
1748 var ret []Target
1749 for _, t := range targets {
1750 if t.Arch.ArchType.Multilib == multilib {
1751 ret = append(ret, t)
1752 }
1753 }
1754 return ret
1755}
1756
Paul Duffinca7f0ef2020-02-25 15:50:49 +00001757// Return the set of Os specific common architecture targets for each Os in a list of
1758// targets.
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001759func getCommonTargets(targets []Target) []Target {
1760 var ret []Target
1761 set := make(map[string]bool)
1762
1763 for _, t := range targets {
1764 if _, found := set[t.Os.String()]; !found {
1765 set[t.Os.String()] = true
1766 ret = append(ret, commonTargetMap[t.Os.String()])
1767 }
1768 }
1769
1770 return ret
1771}
1772
Colin Cross3dceee32018-09-06 10:19:57 -07001773func firstTarget(targets []Target, filters ...string) []Target {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001774 for _, filter := range filters {
1775 buildTargets := filterMultilibTargets(targets, filter)
1776 if len(buildTargets) > 0 {
Colin Cross3dceee32018-09-06 10:19:57 -07001777 return buildTargets[:1]
Colin Cross6b4a32d2017-12-05 13:42:45 -08001778 }
1779 }
1780 return nil
1781}
1782
Colin Crossa1ad8d12016-06-01 17:09:44 -07001783// Use the module multilib setting to select one or more targets from a target list
Colin Crossee0bc3b2018-10-02 22:01:37 -07001784func decodeMultilibTargets(multilib string, targets []Target, prefer32 bool) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001785 buildTargets := []Target{}
Colin Cross6b4a32d2017-12-05 13:42:45 -08001786
Colin Cross4225f652015-09-17 14:33:42 -07001787 switch multilib {
1788 case "common":
Colin Cross6b4a32d2017-12-05 13:42:45 -08001789 buildTargets = getCommonTargets(targets)
1790 case "common_first":
1791 buildTargets = getCommonTargets(targets)
1792 if prefer32 {
Colin Cross3dceee32018-09-06 10:19:57 -07001793 buildTargets = append(buildTargets, firstTarget(targets, "lib32", "lib64")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001794 } else {
Colin Cross3dceee32018-09-06 10:19:57 -07001795 buildTargets = append(buildTargets, firstTarget(targets, "lib64", "lib32")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001796 }
Colin Cross4225f652015-09-17 14:33:42 -07001797 case "both":
Colin Cross8b74d172016-09-13 09:59:14 -07001798 if prefer32 {
1799 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1800 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1801 } else {
1802 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1803 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1804 }
Colin Cross4225f652015-09-17 14:33:42 -07001805 case "32":
Colin Cross69617d32016-09-06 10:39:07 -07001806 buildTargets = filterMultilibTargets(targets, "lib32")
Colin Cross4225f652015-09-17 14:33:42 -07001807 case "64":
Colin Cross69617d32016-09-06 10:39:07 -07001808 buildTargets = filterMultilibTargets(targets, "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001809 case "first":
1810 if prefer32 {
Colin Cross3dceee32018-09-06 10:19:57 -07001811 buildTargets = firstTarget(targets, "lib32", "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001812 } else {
Colin Cross3dceee32018-09-06 10:19:57 -07001813 buildTargets = firstTarget(targets, "lib64", "lib32")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001814 }
Colin Cross69617d32016-09-06 10:39:07 -07001815 case "prefer32":
Colin Cross3dceee32018-09-06 10:19:57 -07001816 buildTargets = filterMultilibTargets(targets, "lib32")
1817 if len(buildTargets) == 0 {
1818 buildTargets = filterMultilibTargets(targets, "lib64")
1819 }
Colin Cross4225f652015-09-17 14:33:42 -07001820 default:
Colin Cross69617d32016-09-06 10:39:07 -07001821 return nil, fmt.Errorf(`compile_multilib must be "both", "first", "32", "64", or "prefer32" found %q`,
Colin Cross4225f652015-09-17 14:33:42 -07001822 multilib)
Colin Cross4225f652015-09-17 14:33:42 -07001823 }
1824
Colin Crossa1ad8d12016-06-01 17:09:44 -07001825 return buildTargets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001826}