blob: f4b0d66d91ad3c0d879091c5b6a4433219f944db [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: {
Martin Stjernholme284b482020-09-23 21:03:27 +010081 // Device variants (implies Bionic)
Colin Cross3f40fa42015-01-30 17:27:36 -080082 },
83 host: {
84 // Host variants
85 },
Martin Stjernholme284b482020-09-23 21:03:27 +010086 bionic: {
87 // Bionic (device and host) variants
88 },
89 linux_bionic: {
90 // Bionic host variants
91 },
92 linux: {
93 // Bionic (device and host) and Linux glibc variants
94 },
Dan Willemsen5746bd42017-10-02 19:42:01 -070095 linux_glibc: {
Martin Stjernholme284b482020-09-23 21:03:27 +010096 // Linux host variants (using non-Bionic libc)
Colin Cross3f40fa42015-01-30 17:27:36 -080097 },
98 darwin: {
99 // Darwin host variants
100 },
101 windows: {
102 // Windows host variants
103 },
104 not_windows: {
105 // Non-windows host variants
106 },
Martin Stjernholme284b482020-09-23 21:03:27 +0100107 android_arm: {
108 // Any <os>_<arch> combination restricts to that os and arch
109 },
Colin Cross3f40fa42015-01-30 17:27:36 -0800110 },
111}
112*/
Colin Cross7d5136f2015-05-11 13:39:40 -0700113
Jaewoong Junge46114c2019-01-16 14:33:13 -0800114var archVariants = map[ArchType][]string{
115 Arm: {
Dan Albert8818f492019-02-19 13:53:01 -0800116 "armv7-a",
Jaewoong Junge46114c2019-01-16 14:33:13 -0800117 "armv7-a-neon",
118 "armv8-a",
119 "armv8-2a",
120 "cortex-a7",
121 "cortex-a8",
122 "cortex-a9",
123 "cortex-a15",
124 "cortex-a53",
125 "cortex-a53-a57",
126 "cortex-a55",
127 "cortex-a72",
128 "cortex-a73",
129 "cortex-a75",
130 "cortex-a76",
131 "krait",
132 "kryo",
133 "kryo385",
134 "exynos-m1",
135 "exynos-m2",
136 },
137 Arm64: {
138 "armv8_a",
139 "armv8_2a",
Raphael Gault70b96b02020-06-18 09:56:53 +0000140 "armv8-2a-dotprod",
Jaewoong Junge46114c2019-01-16 14:33:13 -0800141 "cortex-a53",
142 "cortex-a55",
143 "cortex-a72",
144 "cortex-a73",
145 "cortex-a75",
146 "cortex-a76",
147 "kryo",
148 "kryo385",
149 "exynos-m1",
150 "exynos-m2",
151 },
Jaewoong Junge46114c2019-01-16 14:33:13 -0800152 X86: {
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530153 "amberlake",
Jaewoong Junge46114c2019-01-16 14:33:13 -0800154 "atom",
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530155 "broadwell",
Jaewoong Junge46114c2019-01-16 14:33:13 -0800156 "haswell",
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530157 "icelake",
Jaewoong Junge46114c2019-01-16 14:33:13 -0800158 "ivybridge",
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530159 "kabylake",
Jaewoong Junge46114c2019-01-16 14:33:13 -0800160 "sandybridge",
161 "silvermont",
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530162 "skylake",
Benjamin Gordon87e7f2f2019-02-14 10:59:48 -0700163 "stoneyridge",
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530164 "tigerlake",
165 "whiskeylake",
Jaewoong Junge46114c2019-01-16 14:33:13 -0800166 "x86_64",
167 },
168 X86_64: {
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530169 "amberlake",
170 "broadwell",
Jaewoong Junge46114c2019-01-16 14:33:13 -0800171 "haswell",
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530172 "icelake",
Jaewoong Junge46114c2019-01-16 14:33:13 -0800173 "ivybridge",
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530174 "kabylake",
Jaewoong Junge46114c2019-01-16 14:33:13 -0800175 "sandybridge",
176 "silvermont",
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530177 "skylake",
Benjamin Gordon87e7f2f2019-02-14 10:59:48 -0700178 "stoneyridge",
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530179 "tigerlake",
180 "whiskeylake",
Jaewoong Junge46114c2019-01-16 14:33:13 -0800181 },
182}
183
184var archFeatures = map[ArchType][]string{
185 Arm: {
186 "neon",
187 },
Raphael Gault70b96b02020-06-18 09:56:53 +0000188 Arm64: {
189 "dotprod",
190 },
Jaewoong Junge46114c2019-01-16 14:33:13 -0800191 X86: {
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 "movbe",
202 },
203 X86_64: {
204 "ssse3",
205 "sse4",
206 "sse4_1",
207 "sse4_2",
208 "aes_ni",
209 "avx",
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530210 "avx2",
211 "avx512",
Jaewoong Junge46114c2019-01-16 14:33:13 -0800212 "popcnt",
213 },
214}
215
216var archFeatureMap = map[ArchType]map[string][]string{
217 Arm: {
218 "armv7-a-neon": {
219 "neon",
220 },
221 "armv8-a": {
222 "neon",
223 },
224 "armv8-2a": {
225 "neon",
226 },
227 },
Raphael Gault70b96b02020-06-18 09:56:53 +0000228 Arm64: {
229 "armv8-2a-dotprod": {
230 "dotprod",
231 },
232 },
Jaewoong Junge46114c2019-01-16 14:33:13 -0800233 X86: {
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530234 "amberlake": {
235 "ssse3",
236 "sse4",
237 "sse4_1",
238 "sse4_2",
239 "avx",
240 "avx2",
241 "aes_ni",
242 "popcnt",
243 },
Jaewoong Junge46114c2019-01-16 14:33:13 -0800244 "atom": {
245 "ssse3",
246 "movbe",
247 },
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530248 "broadwell": {
249 "ssse3",
250 "sse4",
251 "sse4_1",
252 "sse4_2",
253 "avx",
254 "avx2",
255 "aes_ni",
256 "popcnt",
257 },
Jaewoong Junge46114c2019-01-16 14:33:13 -0800258 "haswell": {
259 "ssse3",
260 "sse4",
261 "sse4_1",
262 "sse4_2",
263 "aes_ni",
264 "avx",
265 "popcnt",
266 "movbe",
267 },
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530268 "icelake": {
269 "ssse3",
270 "sse4",
271 "sse4_1",
272 "sse4_2",
273 "avx",
274 "avx2",
275 "avx512",
276 "aes_ni",
277 "popcnt",
278 },
Jaewoong Junge46114c2019-01-16 14:33:13 -0800279 "ivybridge": {
280 "ssse3",
281 "sse4",
282 "sse4_1",
283 "sse4_2",
284 "aes_ni",
285 "avx",
286 "popcnt",
287 },
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530288 "kabylake": {
289 "ssse3",
290 "sse4",
291 "sse4_1",
292 "sse4_2",
293 "avx",
294 "avx2",
295 "aes_ni",
296 "popcnt",
297 },
Jaewoong Junge46114c2019-01-16 14:33:13 -0800298 "sandybridge": {
299 "ssse3",
300 "sse4",
301 "sse4_1",
302 "sse4_2",
303 "popcnt",
304 },
305 "silvermont": {
306 "ssse3",
307 "sse4",
308 "sse4_1",
309 "sse4_2",
310 "aes_ni",
311 "popcnt",
312 "movbe",
313 },
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530314 "skylake": {
315 "ssse3",
316 "sse4",
317 "sse4_1",
318 "sse4_2",
319 "avx",
320 "avx2",
321 "avx512",
322 "aes_ni",
323 "popcnt",
324 },
Benjamin Gordon87e7f2f2019-02-14 10:59:48 -0700325 "stoneyridge": {
326 "ssse3",
327 "sse4",
328 "sse4_1",
329 "sse4_2",
330 "aes_ni",
331 "avx",
332 "avx2",
333 "popcnt",
334 "movbe",
335 },
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530336 "tigerlake": {
337 "ssse3",
338 "sse4",
339 "sse4_1",
340 "sse4_2",
341 "avx",
342 "avx2",
343 "avx512",
344 "aes_ni",
345 "popcnt",
346 },
347 "whiskeylake": {
348 "ssse3",
349 "sse4",
350 "sse4_1",
351 "sse4_2",
352 "avx",
353 "avx2",
354 "avx512",
355 "aes_ni",
356 "popcnt",
357 },
Jaewoong Junge46114c2019-01-16 14:33:13 -0800358 "x86_64": {
359 "ssse3",
360 "sse4",
361 "sse4_1",
362 "sse4_2",
363 "popcnt",
364 },
365 },
366 X86_64: {
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530367 "amberlake": {
368 "ssse3",
369 "sse4",
370 "sse4_1",
371 "sse4_2",
372 "avx",
373 "avx2",
374 "aes_ni",
375 "popcnt",
376 },
377 "broadwell": {
378 "ssse3",
379 "sse4",
380 "sse4_1",
381 "sse4_2",
382 "avx",
383 "avx2",
384 "aes_ni",
385 "popcnt",
386 },
Jaewoong Junge46114c2019-01-16 14:33:13 -0800387 "haswell": {
388 "ssse3",
389 "sse4",
390 "sse4_1",
391 "sse4_2",
392 "aes_ni",
393 "avx",
394 "popcnt",
395 },
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530396 "icelake": {
397 "ssse3",
398 "sse4",
399 "sse4_1",
400 "sse4_2",
401 "avx",
402 "avx2",
403 "avx512",
404 "aes_ni",
405 "popcnt",
406 },
Jaewoong Junge46114c2019-01-16 14:33:13 -0800407 "ivybridge": {
408 "ssse3",
409 "sse4",
410 "sse4_1",
411 "sse4_2",
412 "aes_ni",
413 "avx",
414 "popcnt",
415 },
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530416 "kabylake": {
417 "ssse3",
418 "sse4",
419 "sse4_1",
420 "sse4_2",
421 "avx",
422 "avx2",
423 "aes_ni",
424 "popcnt",
425 },
Jaewoong Junge46114c2019-01-16 14:33:13 -0800426 "sandybridge": {
427 "ssse3",
428 "sse4",
429 "sse4_1",
430 "sse4_2",
431 "popcnt",
432 },
433 "silvermont": {
434 "ssse3",
435 "sse4",
436 "sse4_1",
437 "sse4_2",
438 "aes_ni",
439 "popcnt",
440 },
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530441 "skylake": {
442 "ssse3",
443 "sse4",
444 "sse4_1",
445 "sse4_2",
446 "avx",
447 "avx2",
448 "avx512",
449 "aes_ni",
450 "popcnt",
451 },
Benjamin Gordon87e7f2f2019-02-14 10:59:48 -0700452 "stoneyridge": {
453 "ssse3",
454 "sse4",
455 "sse4_1",
456 "sse4_2",
457 "aes_ni",
458 "avx",
459 "avx2",
460 "popcnt",
461 },
Shalini Salomi Bodapati4a0459d2019-01-22 10:00:15 +0530462 "tigerlake": {
463 "ssse3",
464 "sse4",
465 "sse4_1",
466 "sse4_2",
467 "avx",
468 "avx2",
469 "avx512",
470 "aes_ni",
471 "popcnt",
472 },
473 "whiskeylake": {
474 "ssse3",
475 "sse4",
476 "sse4_1",
477 "sse4_2",
478 "avx",
479 "avx2",
480 "avx512",
481 "aes_ni",
482 "popcnt",
483 },
Jaewoong Junge46114c2019-01-16 14:33:13 -0800484 },
485}
486
Dan Willemsen01a3c252019-01-11 19:02:16 -0800487var defaultArchFeatureMap = map[OsType]map[ArchType][]string{}
Colin Crossc5c24ad2015-11-20 15:35:00 -0800488
Dan Willemsen01a3c252019-01-11 19:02:16 -0800489func RegisterDefaultArchVariantFeatures(os OsType, arch ArchType, features ...string) {
490 checkCalledFromInit()
491
492 for _, feature := range features {
493 if !InList(feature, archFeatures[arch]) {
494 panic(fmt.Errorf("Invalid feature %q for arch %q variant \"\"", feature, arch))
495 }
496 }
497
498 if defaultArchFeatureMap[os] == nil {
499 defaultArchFeatureMap[os] = make(map[ArchType][]string)
500 }
501 defaultArchFeatureMap[os][arch] = features
502}
503
Colin Cross3f40fa42015-01-30 17:27:36 -0800504// An Arch indicates a single CPU architecture.
505type Arch struct {
Colin Crossc5c24ad2015-11-20 15:35:00 -0800506 ArchType ArchType
507 ArchVariant string
508 CpuVariant string
509 Abi []string
510 ArchFeatures []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800511}
512
513func (a Arch) String() string {
Colin Crossd3ba0392015-05-07 14:11:29 -0700514 s := a.ArchType.String()
Colin Cross3f40fa42015-01-30 17:27:36 -0800515 if a.ArchVariant != "" {
516 s += "_" + a.ArchVariant
517 }
518 if a.CpuVariant != "" {
519 s += "_" + a.CpuVariant
520 }
521 return s
522}
523
524type ArchType struct {
Colin Crossec193632015-07-06 17:49:43 -0700525 Name string
Dan Willemsenb1957a52016-06-23 23:44:54 -0700526 Field string
Colin Crossec193632015-07-06 17:49:43 -0700527 Multilib string
Colin Cross3f40fa42015-01-30 17:27:36 -0800528}
529
Colin Crossec193632015-07-06 17:49:43 -0700530func newArch(name, multilib string) ArchType {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700531 archType := ArchType{
Colin Crossec193632015-07-06 17:49:43 -0700532 Name: name,
Dan Willemsenb1957a52016-06-23 23:44:54 -0700533 Field: proptools.FieldNameForProperty(name),
Colin Crossec193632015-07-06 17:49:43 -0700534 Multilib: multilib,
Colin Cross3f40fa42015-01-30 17:27:36 -0800535 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700536 archTypeList = append(archTypeList, archType)
537 return archType
Colin Cross3f40fa42015-01-30 17:27:36 -0800538}
539
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -0700540func ArchTypeList() []ArchType {
541 return append([]ArchType(nil), archTypeList...)
542}
543
Colin Cross3f40fa42015-01-30 17:27:36 -0800544func (a ArchType) String() string {
545 return a.Name
546}
547
Colin Cross74ba9622019-02-11 15:11:14 -0800548var _ encoding.TextMarshaler = ArchType{}
549
550func (a ArchType) MarshalText() ([]byte, error) {
551 return []byte(strconv.Quote(a.String())), nil
552}
553
554var _ encoding.TextUnmarshaler = &ArchType{}
555
556func (a *ArchType) UnmarshalText(text []byte) error {
557 if u, ok := archTypeMap[string(text)]; ok {
558 *a = u
559 return nil
560 }
561
562 return fmt.Errorf("unknown ArchType %q", text)
563}
564
Colin Crossa1ad8d12016-06-01 17:09:44 -0700565var BuildOs = func() OsType {
Dan Willemsen490fd492015-11-24 17:53:15 -0800566 switch runtime.GOOS {
567 case "linux":
568 return Linux
569 case "darwin":
570 return Darwin
571 default:
572 panic(fmt.Sprintf("unsupported OS: %s", runtime.GOOS))
573 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700574}()
575
Jiyong Park87788b52020-09-01 12:37:45 +0900576var BuildArch = func() ArchType {
577 switch runtime.GOARCH {
578 case "amd64":
579 return X86_64
580 default:
581 panic(fmt.Sprintf("unsupported Arch: %s", runtime.GOARCH))
582 }
583}()
584
Colin Crossa1ad8d12016-06-01 17:09:44 -0700585var (
Paul Duffina04c1072020-03-02 10:16:35 +0000586 OsTypeList []OsType
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800587 commonTargetMap = make(map[string]Target)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700588
Dan Willemsen00fcbde2016-11-17 00:25:59 -0800589 NoOsType OsType
Dan Willemsen866b5632017-09-22 12:28:24 -0700590 Linux = NewOsType("linux_glibc", Host, false)
Dan Willemsen00fcbde2016-11-17 00:25:59 -0800591 Darwin = NewOsType("darwin", Host, false)
Dan Willemsen9ff34c02018-10-10 17:58:19 -0700592 LinuxBionic = NewOsType("linux_bionic", Host, false)
Jiyong Park1613e552020-09-14 19:43:17 +0900593 Windows = NewOsType("windows", Host, true)
Dan Willemsen00fcbde2016-11-17 00:25:59 -0800594 Android = NewOsType("android", Device, false)
Doug Horn21b94272019-01-16 12:06:11 -0800595 Fuchsia = NewOsType("fuchsia", Device, false)
Dan Willemsenb1957a52016-06-23 23:44:54 -0700596
Paul Duffin1356d8c2020-02-25 19:26:33 +0000597 // A pseudo OSType for a common os variant, which is OSType agnostic and which
598 // has dependencies on all the OS variants.
599 CommonOS = NewOsType("common_os", Generic, false)
600
Dan Willemsenb1957a52016-06-23 23:44:54 -0700601 osArchTypeMap = map[OsType][]ArchType{
Dan Willemsen00fcbde2016-11-17 00:25:59 -0800602 Linux: []ArchType{X86, X86_64},
Jiyong Park4afa2e22020-07-13 15:43:45 +0900603 LinuxBionic: []ArchType{Arm64, X86_64},
Dan Willemsene97e68a2018-08-28 17:12:56 -0700604 Darwin: []ArchType{X86_64},
Dan Willemsen00fcbde2016-11-17 00:25:59 -0800605 Windows: []ArchType{X86, X86_64},
Elliott Hughesda3a0712020-03-06 16:55:28 -0800606 Android: []ArchType{Arm, Arm64, X86, X86_64},
Doug Horn21b94272019-01-16 12:06:11 -0800607 Fuchsia: []ArchType{Arm64, X86_64},
Dan Willemsenb1957a52016-06-23 23:44:54 -0700608 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700609)
610
611type OsType struct {
612 Name, Field string
613 Class OsClass
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800614
615 DefaultDisabled bool
Dan Willemsen490fd492015-11-24 17:53:15 -0800616}
617
Colin Crossa1ad8d12016-06-01 17:09:44 -0700618type OsClass int
619
620const (
Dan Willemsen0e2d97b2016-11-28 17:50:06 -0800621 Generic OsClass = iota
622 Device
Colin Crossa1ad8d12016-06-01 17:09:44 -0700623 Host
Colin Crossa1ad8d12016-06-01 17:09:44 -0700624)
625
Colin Cross67a5c132017-05-09 13:45:28 -0700626func (class OsClass) String() string {
627 switch class {
628 case Generic:
629 return "generic"
630 case Device:
631 return "device"
632 case Host:
633 return "host"
Colin Cross67a5c132017-05-09 13:45:28 -0700634 default:
635 panic(fmt.Errorf("unknown class %d", class))
636 }
637}
638
Colin Crossa1ad8d12016-06-01 17:09:44 -0700639func (os OsType) String() string {
640 return os.Name
Colin Cross54c71122016-06-01 17:09:44 -0700641}
642
Dan Willemsen866b5632017-09-22 12:28:24 -0700643func (os OsType) Bionic() bool {
644 return os == Android || os == LinuxBionic
645}
646
647func (os OsType) Linux() bool {
648 return os == Android || os == Linux || os == LinuxBionic
649}
650
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800651func NewOsType(name string, class OsClass, defDisabled bool) OsType {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700652 os := OsType{
653 Name: name,
654 Field: strings.Title(name),
655 Class: class,
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800656
657 DefaultDisabled: defDisabled,
Colin Cross54c71122016-06-01 17:09:44 -0700658 }
Paul Duffina04c1072020-03-02 10:16:35 +0000659 OsTypeList = append(OsTypeList, os)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800660
661 if _, found := commonTargetMap[name]; found {
662 panic(fmt.Errorf("Found Os type duplicate during OsType registration: %q", name))
663 } else {
664 commonTargetMap[name] = Target{Os: os, Arch: Arch{ArchType: Common}}
665 }
666
Colin Crossa1ad8d12016-06-01 17:09:44 -0700667 return os
668}
669
670func osByName(name string) OsType {
Paul Duffina04c1072020-03-02 10:16:35 +0000671 for _, os := range OsTypeList {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700672 if os.Name == name {
673 return os
674 }
675 }
676
677 return NoOsType
Dan Willemsen490fd492015-11-24 17:53:15 -0800678}
679
dimitry1f33e402019-03-26 12:39:31 +0100680type NativeBridgeSupport bool
681
682const (
683 NativeBridgeDisabled NativeBridgeSupport = false
684 NativeBridgeEnabled NativeBridgeSupport = true
685)
686
Colin Crossa1ad8d12016-06-01 17:09:44 -0700687type Target struct {
dimitry8d6dde82019-07-11 10:23:53 +0200688 Os OsType
689 Arch Arch
690 NativeBridge NativeBridgeSupport
691 NativeBridgeHostArchName string
692 NativeBridgeRelativePath string
Jiyong Park1613e552020-09-14 19:43:17 +0900693
694 // HostCross is true when the target cannot run natively on the current build host.
695 // For example, linux_glibc_x86 returns true on a regular x86/i686/Linux machines, but returns false
696 // on Mac (different OS), or on 64-bit only i686/Linux machines (unsupported arch).
697 HostCross bool
Colin Crossd3ba0392015-05-07 14:11:29 -0700698}
699
Colin Crossa1ad8d12016-06-01 17:09:44 -0700700func (target Target) String() string {
Colin Crossa195f912019-10-16 11:07:20 -0700701 return target.OsVariation() + "_" + target.ArchVariation()
702}
703
704func (target Target) OsVariation() string {
705 return target.Os.String()
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700706}
707
708func (target Target) ArchVariation() string {
709 var variation string
dimitry1f33e402019-03-26 12:39:31 +0100710 if target.NativeBridge {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700711 variation = "native_bridge_"
dimitry1f33e402019-03-26 12:39:31 +0100712 }
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700713 variation += target.Arch.String()
714
Colin Crossa195f912019-10-16 11:07:20 -0700715 return variation
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700716}
717
718func (target Target) Variations() []blueprint.Variation {
719 return []blueprint.Variation{
Colin Crossa195f912019-10-16 11:07:20 -0700720 {Mutator: "os", Variation: target.OsVariation()},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700721 {Mutator: "arch", Variation: target.ArchVariation()},
722 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800723}
724
Colin Cross617b88a2020-08-24 18:04:09 -0700725func osMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Crossa195f912019-10-16 11:07:20 -0700726 var module Module
727 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700728 if module, ok = bpctx.Module().(Module); !ok {
729 if bootstrap.IsBootstrapModule(bpctx.Module()) {
730 // Bootstrap Go modules are always the build OS or linux bionic.
731 config := bpctx.Config().(Config)
732 osNames := []string{config.BuildOSTarget.OsVariation()}
733 for _, hostCrossTarget := range config.Targets[LinuxBionic] {
734 if hostCrossTarget.Arch.ArchType == config.BuildOSTarget.Arch.ArchType {
735 osNames = append(osNames, hostCrossTarget.OsVariation())
736 }
737 }
738 osNames = FirstUniqueStrings(osNames)
739 bpctx.CreateVariations(osNames...)
740 }
Colin Crossa195f912019-10-16 11:07:20 -0700741 return
742 }
743
Colin Cross617b88a2020-08-24 18:04:09 -0700744 // Bootstrap Go module support above requires this mutator to be a
745 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
746 // filters out non-Soong modules. Now that we've handled them, create a
747 // normal android.BottomUpMutatorContext.
748 mctx := bottomUpMutatorContextFactory(bpctx, module, false)
749
Colin Crossa195f912019-10-16 11:07:20 -0700750 base := module.base()
751
752 if !base.ArchSpecific() {
753 return
754 }
755
Colin Crossa195f912019-10-16 11:07:20 -0700756 var moduleOSList []OsType
757
Paul Duffina04c1072020-03-02 10:16:35 +0000758 for _, os := range OsTypeList {
Jiyong Park1613e552020-09-14 19:43:17 +0900759 for _, t := range mctx.Config().Targets[os] {
760 if base.supportsTarget(t, mctx.Config()) {
761 moduleOSList = append(moduleOSList, os)
762 break
Colin Crossa195f912019-10-16 11:07:20 -0700763 }
764 }
Colin Crossa195f912019-10-16 11:07:20 -0700765 }
766
767 if len(moduleOSList) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900768 base.Disable()
Colin Crossa195f912019-10-16 11:07:20 -0700769 return
770 }
771
772 osNames := make([]string, len(moduleOSList))
773
774 for i, os := range moduleOSList {
775 osNames[i] = os.String()
776 }
777
Paul Duffin1356d8c2020-02-25 19:26:33 +0000778 createCommonOSVariant := base.commonProperties.CreateCommonOSVariant
779 if createCommonOSVariant {
780 // A CommonOS variant was requested so add it to the list of OS's variants to
781 // create. It needs to be added to the end because it needs to depend on the
782 // the other variants in the list returned by CreateVariations(...) and inter
783 // variant dependencies can only be created from a later variant in that list to
784 // an earlier one. That is because variants are always processed in the order in
785 // which they are returned from CreateVariations(...).
786 osNames = append(osNames, CommonOS.Name)
787 moduleOSList = append(moduleOSList, CommonOS)
Colin Crossa195f912019-10-16 11:07:20 -0700788 }
789
Paul Duffin1356d8c2020-02-25 19:26:33 +0000790 modules := mctx.CreateVariations(osNames...)
791 for i, m := range modules {
792 m.base().commonProperties.CompileOS = moduleOSList[i]
793 m.base().setOSProperties(mctx)
794 }
795
796 if createCommonOSVariant {
797 // A CommonOS variant was requested so add dependencies from it (the last one in
798 // the list) to the OS type specific variants.
799 last := len(modules) - 1
800 commonOSVariant := modules[last]
801 commonOSVariant.base().commonProperties.CommonOSVariant = true
802 for _, module := range modules[0:last] {
803 // Ignore modules that are enabled. Note, this will only avoid adding
804 // dependencies on OsType variants that are explicitly disabled in their
805 // properties. The CommonOS variant will still depend on disabled variants
806 // if they are disabled afterwards, e.g. in archMutator if
807 if module.Enabled() {
808 mctx.AddInterVariantDependency(commonOsToOsSpecificVariantTag, commonOSVariant, module)
809 }
810 }
811 }
812}
813
814// Identifies the dependency from CommonOS variant to the os specific variants.
815type commonOSTag struct{ blueprint.BaseDependencyTag }
816
817var commonOsToOsSpecificVariantTag = commonOSTag{}
818
819// Get the OsType specific variants for the current CommonOS variant.
820//
821// The returned list will only contain enabled OsType specific variants of the
822// module referenced in the supplied context. An empty list is returned if there
823// are no enabled variants or the supplied context is not for an CommonOS
824// variant.
825func GetOsSpecificVariantsOfCommonOSVariant(mctx BaseModuleContext) []Module {
826 var variants []Module
827 mctx.VisitDirectDeps(func(m Module) {
828 if mctx.OtherModuleDependencyTag(m) == commonOsToOsSpecificVariantTag {
829 if m.Enabled() {
830 variants = append(variants, m)
831 }
832 }
833 })
834
835 return variants
Colin Crossa195f912019-10-16 11:07:20 -0700836}
837
Colin Crossee0bc3b2018-10-02 22:01:37 -0700838// archMutator splits a module into a variant for each Target requested by the module. Target selection
839// for a module is in three levels, OsClass, mulitlib, and then Target.
840// OsClass selection is determined by:
841// - The HostOrDeviceSupported value passed in to InitAndroidArchModule by the module type factory, which selects
842// whether the module type can compile for host, device or both.
843// - The host_supported and device_supported properties on the module.
Roland Levillainf5b635d2019-06-05 14:42:57 +0100844// 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 -0700845// for the module, the Device OsClass is selected.
846// Within each selected OsClass, the multilib selection is determined by:
Jaewoong Jung02b2d4d2019-06-06 15:19:57 -0700847// - The compile_multilib property if it set (which may be overridden by target.android.compile_multilib or
Colin Crossee0bc3b2018-10-02 22:01:37 -0700848// target.host.compile_multilib).
849// - The default multilib passed to InitAndroidArchModule if compile_multilib was not set.
850// Valid multilib values include:
851// "both": compile for all Targets supported by the OsClass (generally x86_64 and x86, or arm64 and arm).
852// "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 -0700853// but may be arm for a 32-bit only build.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700854// "32": compile for only a single 32-bit Target supported by the OsClass.
855// "64": compile for only a single 64-bit Target supported by the OsClass.
856// "common": compile a for a single Target that will work on all Targets suported by the OsClass (for example Java).
857//
858// Once the list of Targets is determined, the module is split into a variant for each Target.
859//
860// Modules can be initialized with InitAndroidMultiTargetsArchModule, in which case they will be split by OsClass,
861// 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 -0700862func archMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700863 var module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800864 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700865 if module, ok = bpctx.Module().(Module); !ok {
866 if bootstrap.IsBootstrapModule(bpctx.Module()) {
867 // Bootstrap Go modules are always the build architecture.
868 bpctx.CreateVariations(bpctx.Config().(Config).BuildOSTarget.ArchVariation())
869 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800870 return
871 }
872
Colin Cross617b88a2020-08-24 18:04:09 -0700873 // Bootstrap Go module support above requires this mutator to be a
874 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
875 // filters out non-Soong modules. Now that we've handled them, create a
876 // normal android.BottomUpMutatorContext.
877 mctx := bottomUpMutatorContextFactory(bpctx, module, false)
878
Colin Cross5eca7cb2018-10-02 14:02:10 -0700879 base := module.base()
880
881 if !base.ArchSpecific() {
Colin Crossb9db4802016-06-03 01:50:47 +0000882 return
883 }
884
Colin Crossa195f912019-10-16 11:07:20 -0700885 os := base.commonProperties.CompileOS
Paul Duffin1356d8c2020-02-25 19:26:33 +0000886 if os == CommonOS {
887 // Make sure that the target related properties are initialized for the
888 // CommonOS variant.
889 addTargetProperties(module, commonTargetMap[os.Name], nil, true)
890
891 // Do not create arch specific variants for the CommonOS variant.
892 return
893 }
894
Colin Crossa195f912019-10-16 11:07:20 -0700895 osTargets := mctx.Config().Targets[os]
Colin Crossfb0c16e2019-11-20 17:12:35 -0800896 image := base.commonProperties.ImageVariation
Colin Crossa195f912019-10-16 11:07:20 -0700897 // Filter NativeBridge targets unless they are explicitly supported
Colin Cross83bead42019-12-18 10:45:46 -0800898 // Skip creating native bridge variants for vendor modules
899 if os == Android &&
900 !(Bool(base.commonProperties.Native_bridge_supported) && image == CoreVariation) {
901
Colin Crossa195f912019-10-16 11:07:20 -0700902 var targets []Target
903 for _, t := range osTargets {
904 if !t.NativeBridge {
905 targets = append(targets, t)
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700906 }
907 }
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700908
Colin Crossa195f912019-10-16 11:07:20 -0700909 osTargets = targets
910 }
Colin Crossee0bc3b2018-10-02 22:01:37 -0700911
Yifan Hong1b3348d2020-01-21 15:53:22 -0800912 // only the primary arch in the ramdisk / recovery partition
913 if os == Android && (module.InstallInRecovery() || module.InstallInRamdisk()) {
Colin Crossa195f912019-10-16 11:07:20 -0700914 osTargets = []Target{osTargets[0]}
915 }
dimitry1f33e402019-03-26 12:39:31 +0100916
Colin Crossa195f912019-10-16 11:07:20 -0700917 prefer32 := false
918 if base.prefer32 != nil {
Jiyong Park1613e552020-09-14 19:43:17 +0900919 prefer32 = base.prefer32(mctx, base, os)
Colin Crossa195f912019-10-16 11:07:20 -0700920 }
dimitry1f33e402019-03-26 12:39:31 +0100921
Colin Crossa195f912019-10-16 11:07:20 -0700922 multilib, extraMultilib := decodeMultilib(base, os.Class)
923 targets, err := decodeMultilibTargets(multilib, osTargets, prefer32)
924 if err != nil {
925 mctx.ModuleErrorf("%s", err.Error())
926 }
Colin Cross5eca7cb2018-10-02 14:02:10 -0700927
Colin Crossa195f912019-10-16 11:07:20 -0700928 var multiTargets []Target
929 if extraMultilib != "" {
930 multiTargets, err = decodeMultilibTargets(extraMultilib, osTargets, prefer32)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700931 if err != nil {
932 mctx.ModuleErrorf("%s", err.Error())
933 }
Colin Crossb9db4802016-06-03 01:50:47 +0000934 }
935
Colin Crossfb0c16e2019-11-20 17:12:35 -0800936 if image == RecoveryVariation {
937 primaryArch := mctx.Config().DevicePrimaryArchType()
938 targets = filterToArch(targets, primaryArch)
939 multiTargets = filterToArch(multiTargets, primaryArch)
940 }
941
Colin Crossa195f912019-10-16 11:07:20 -0700942 if len(targets) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900943 base.Disable()
Dan Willemsen3f32f032016-07-11 14:36:48 -0700944 return
945 }
946
Colin Crossa195f912019-10-16 11:07:20 -0700947 targetNames := make([]string, len(targets))
Colin Crossb9db4802016-06-03 01:50:47 +0000948
Colin Crossa195f912019-10-16 11:07:20 -0700949 for i, target := range targets {
950 targetNames[i] = target.ArchVariation()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700951 }
952
953 modules := mctx.CreateVariations(targetNames...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800954 for i, m := range modules {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000955 addTargetProperties(m, targets[i], multiTargets, i == 0)
Colin Cross617b88a2020-08-24 18:04:09 -0700956 m.base().setArchProperties(mctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800957 }
958}
959
Paul Duffin1356d8c2020-02-25 19:26:33 +0000960func addTargetProperties(m Module, target Target, multiTargets []Target, primaryTarget bool) {
961 m.base().commonProperties.CompileTarget = target
962 m.base().commonProperties.CompileMultiTargets = multiTargets
963 m.base().commonProperties.CompilePrimary = primaryTarget
964}
965
Colin Crossee0bc3b2018-10-02 22:01:37 -0700966func decodeMultilib(base *ModuleBase, class OsClass) (multilib, extraMultilib string) {
967 switch class {
968 case Device:
969 multilib = String(base.commonProperties.Target.Android.Compile_multilib)
Jiyong Park1613e552020-09-14 19:43:17 +0900970 case Host:
Colin Crossee0bc3b2018-10-02 22:01:37 -0700971 multilib = String(base.commonProperties.Target.Host.Compile_multilib)
972 }
973 if multilib == "" {
974 multilib = String(base.commonProperties.Compile_multilib)
975 }
976 if multilib == "" {
977 multilib = base.commonProperties.Default_multilib
978 }
979
980 if base.commonProperties.UseTargetVariants {
981 return multilib, ""
982 } else {
983 // For app modules a single arch variant will be created per OS class which is expected to handle all the
984 // selected arches. Return the common-type as multilib and any Android.bp provided multilib as extraMultilib
985 if multilib == base.commonProperties.Default_multilib {
986 multilib = "first"
987 }
988 return base.commonProperties.Default_multilib, multilib
989 }
990}
991
Colin Crossfb0c16e2019-11-20 17:12:35 -0800992func filterToArch(targets []Target, arch ArchType) []Target {
993 for i := 0; i < len(targets); i++ {
994 if targets[i].Arch.ArchType != arch {
995 targets = append(targets[:i], targets[i+1:]...)
996 i--
997 }
998 }
999 return targets
1000}
1001
Colin Crosscbbd13f2020-01-17 14:08:22 -08001002type archPropTypeDesc struct {
1003 arch, multilib, target reflect.Type
1004}
1005
1006type archPropRoot struct {
1007 Arch, Multilib, Target interface{}
1008}
1009
1010// createArchPropTypeDesc takes a reflect.Type that is either a struct or a pointer to a struct, and
1011// returns lists of reflect.Types that contains the arch-variant properties inside structs for each
1012// arch, multilib and target property.
1013func createArchPropTypeDesc(props reflect.Type) []archPropTypeDesc {
Colin Crossb1d8c992020-01-21 11:43:29 -08001014 // Each property struct shard will be nested many times under the runtime generated arch struct,
1015 // which can hit the limit of 64kB for the name of runtime generated structs. They are nested
1016 // 97 times now, which may grow in the future, plus there is some overhead for the containing
1017 // type. This number may need to be reduced if too many are added, but reducing it too far
1018 // could cause problems if a single deeply nested property no longer fits in the name.
1019 const maxArchTypeNameSize = 500
1020
1021 propShards, _ := proptools.FilterPropertyStructSharded(props, maxArchTypeNameSize, filterArchStruct)
Colin Crosscb988072019-01-24 14:58:11 -08001022 if len(propShards) == 0 {
Dan Willemsenb1957a52016-06-23 23:44:54 -07001023 return nil
1024 }
1025
Colin Crosscbbd13f2020-01-17 14:08:22 -08001026 var ret []archPropTypeDesc
Colin Crossc17727d2018-10-24 12:42:09 -07001027 for _, props := range propShards {
Dan Willemsenb1957a52016-06-23 23:44:54 -07001028
Colin Crossc17727d2018-10-24 12:42:09 -07001029 variantFields := func(names []string) []reflect.StructField {
1030 ret := make([]reflect.StructField, len(names))
Dan Willemsenb1957a52016-06-23 23:44:54 -07001031
Colin Crossc17727d2018-10-24 12:42:09 -07001032 for i, name := range names {
1033 ret[i].Name = name
1034 ret[i].Type = props
Dan Willemsen866b5632017-09-22 12:28:24 -07001035 }
Colin Crossc17727d2018-10-24 12:42:09 -07001036
1037 return ret
1038 }
1039
1040 archFields := make([]reflect.StructField, len(archTypeList))
1041 for i, arch := range archTypeList {
1042 variants := []string{}
1043
1044 for _, archVariant := range archVariants[arch] {
1045 archVariant := variantReplacer.Replace(archVariant)
1046 variants = append(variants, proptools.FieldNameForProperty(archVariant))
1047 }
1048 for _, feature := range archFeatures[arch] {
1049 feature := variantReplacer.Replace(feature)
1050 variants = append(variants, proptools.FieldNameForProperty(feature))
1051 }
1052
1053 fields := variantFields(variants)
1054
1055 fields = append([]reflect.StructField{{
1056 Name: "BlueprintEmbed",
1057 Type: props,
1058 Anonymous: true,
1059 }}, fields...)
1060
1061 archFields[i] = reflect.StructField{
1062 Name: arch.Field,
1063 Type: reflect.StructOf(fields),
1064 }
1065 }
1066 archType := reflect.StructOf(archFields)
1067
1068 multilibType := reflect.StructOf(variantFields([]string{"Lib32", "Lib64"}))
1069
1070 targets := []string{
1071 "Host",
1072 "Android64",
1073 "Android32",
1074 "Bionic",
1075 "Linux",
1076 "Not_windows",
1077 "Arm_on_x86",
1078 "Arm_on_x86_64",
Victor Khimenkoc26fcf42020-05-07 22:16:33 +02001079 "Native_bridge",
Colin Crossc17727d2018-10-24 12:42:09 -07001080 }
Paul Duffina04c1072020-03-02 10:16:35 +00001081 for _, os := range OsTypeList {
Colin Crossc17727d2018-10-24 12:42:09 -07001082 targets = append(targets, os.Field)
1083
1084 for _, archType := range osArchTypeMap[os] {
1085 targets = append(targets, os.Field+"_"+archType.Name)
1086
1087 if os.Linux() {
1088 target := "Linux_" + archType.Name
1089 if !InList(target, targets) {
1090 targets = append(targets, target)
1091 }
1092 }
1093 if os.Bionic() {
1094 target := "Bionic_" + archType.Name
1095 if !InList(target, targets) {
1096 targets = append(targets, target)
1097 }
Dan Willemsen866b5632017-09-22 12:28:24 -07001098 }
1099 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001100 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001101
Colin Crossc17727d2018-10-24 12:42:09 -07001102 targetType := reflect.StructOf(variantFields(targets))
Colin Crosscbbd13f2020-01-17 14:08:22 -08001103
1104 ret = append(ret, archPropTypeDesc{
1105 arch: reflect.PtrTo(archType),
1106 multilib: reflect.PtrTo(multilibType),
1107 target: reflect.PtrTo(targetType),
1108 })
Colin Crossc17727d2018-10-24 12:42:09 -07001109 }
1110 return ret
Dan Willemsenb1957a52016-06-23 23:44:54 -07001111}
1112
Colin Cross74449102019-09-25 11:26:40 -07001113func filterArchStruct(field reflect.StructField, prefix string) (bool, reflect.StructField) {
1114 if proptools.HasTag(field, "android", "arch_variant") {
1115 // The arch_variant field isn't necessary past this point
1116 // Instead of wasting space, just remove it. Go also has a
1117 // 16-bit limit on structure name length. The name is constructed
1118 // based on the Go source representation of the structure, so
1119 // the tag names count towards that length.
Colin Crossb4fecbf2020-01-21 11:38:47 -08001120
1121 androidTag := field.Tag.Get("android")
1122 values := strings.Split(androidTag, ",")
1123
1124 if string(field.Tag) != `android:"`+strings.Join(values, ",")+`"` {
1125 panic(fmt.Errorf("unexpected tag format %q", field.Tag))
Colin Cross74449102019-09-25 11:26:40 -07001126 }
Colin Crossb4fecbf2020-01-21 11:38:47 -08001127 // these tags don't need to be present in the runtime generated struct type.
1128 values = RemoveListFromList(values, []string{"arch_variant", "variant_prepend", "path"})
1129 if len(values) > 0 {
1130 panic(fmt.Errorf("unknown tags %q in field %q", values, prefix+field.Name))
1131 }
1132
1133 field.Tag = ""
Colin Cross74449102019-09-25 11:26:40 -07001134 return true, field
1135 }
1136 return false, field
1137}
1138
Dan Willemsenb1957a52016-06-23 23:44:54 -07001139var archPropTypeMap OncePer
1140
Colin Cross36242852017-06-23 15:06:31 -07001141func InitArchModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001142
1143 base := m.base()
1144
Colin Cross36242852017-06-23 15:06:31 -07001145 base.generalProperties = m.GetProperties()
Colin Cross3f40fa42015-01-30 17:27:36 -08001146
1147 for _, properties := range base.generalProperties {
1148 propertiesValue := reflect.ValueOf(properties)
Colin Cross62496a02016-08-08 15:49:17 -07001149 t := propertiesValue.Type()
Colin Cross3f40fa42015-01-30 17:27:36 -08001150 if propertiesValue.Kind() != reflect.Ptr {
Colin Crossca860ac2016-01-04 14:34:37 -08001151 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
1152 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001153 }
1154
1155 propertiesValue = propertiesValue.Elem()
1156 if propertiesValue.Kind() != reflect.Struct {
Colin Crossca860ac2016-01-04 14:34:37 -08001157 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
1158 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001159 }
1160
Colin Cross571cccf2019-02-04 11:22:08 -08001161 archPropTypes := archPropTypeMap.Once(NewCustomOnceKey(t), func() interface{} {
Colin Crosscbbd13f2020-01-17 14:08:22 -08001162 return createArchPropTypeDesc(t)
1163 }).([]archPropTypeDesc)
Colin Cross3f40fa42015-01-30 17:27:36 -08001164
Colin Crossc17727d2018-10-24 12:42:09 -07001165 var archProperties []interface{}
1166 for _, t := range archPropTypes {
Colin Crosscbbd13f2020-01-17 14:08:22 -08001167 archProperties = append(archProperties, &archPropRoot{
1168 Arch: reflect.Zero(t.arch).Interface(),
1169 Multilib: reflect.Zero(t.multilib).Interface(),
1170 Target: reflect.Zero(t.target).Interface(),
1171 })
Dan Willemsenb1957a52016-06-23 23:44:54 -07001172 }
Colin Crossc17727d2018-10-24 12:42:09 -07001173 base.archProperties = append(base.archProperties, archProperties)
1174 m.AddProperties(archProperties...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001175 }
1176
Colin Cross36242852017-06-23 15:06:31 -07001177 base.customizableProperties = m.GetProperties()
Colin Cross3f40fa42015-01-30 17:27:36 -08001178}
1179
Colin Crossa716add2015-12-16 11:07:39 -08001180var variantReplacer = strings.NewReplacer("-", "_", ".", "_")
Colin Crossec193632015-07-06 17:49:43 -07001181
Colin Cross4157e882019-06-06 16:57:04 -07001182func (m *ModuleBase) appendProperties(ctx BottomUpMutatorContext,
Dan Willemsenb1957a52016-06-23 23:44:54 -07001183 dst interface{}, src reflect.Value, field, srcPrefix string) reflect.Value {
Colin Cross06a931b2015-10-28 17:23:31 -07001184
Colin Crosscbbd13f2020-01-17 14:08:22 -08001185 if src.Kind() == reflect.Ptr {
1186 if src.IsNil() {
1187 return src
1188 }
1189 src = src.Elem()
1190 }
1191
Dan Willemsenb1957a52016-06-23 23:44:54 -07001192 src = src.FieldByName(field)
1193 if !src.IsValid() {
Colin Crosseeabb892015-11-20 13:07:51 -08001194 ctx.ModuleErrorf("field %q does not exist", srcPrefix)
Dan Willemsenb1957a52016-06-23 23:44:54 -07001195 return src
Colin Cross85a88972015-11-23 13:29:51 -08001196 }
1197
Dan Willemsenb1957a52016-06-23 23:44:54 -07001198 ret := src
Colin Cross85a88972015-11-23 13:29:51 -08001199
Dan Willemsenb1957a52016-06-23 23:44:54 -07001200 if src.Kind() == reflect.Struct {
1201 src = src.FieldByName("BlueprintEmbed")
Colin Cross06a931b2015-10-28 17:23:31 -07001202 }
1203
Colin Cross6ee75b62016-05-05 15:57:15 -07001204 order := func(property string,
1205 dstField, srcField reflect.StructField,
1206 dstValue, srcValue interface{}) (proptools.Order, error) {
1207 if proptools.HasTag(dstField, "android", "variant_prepend") {
1208 return proptools.Prepend, nil
1209 } else {
1210 return proptools.Append, nil
1211 }
1212 }
1213
Dan Willemsenb1957a52016-06-23 23:44:54 -07001214 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, order)
Colin Cross06a931b2015-10-28 17:23:31 -07001215 if err != nil {
1216 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
1217 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
1218 } else {
1219 panic(err)
1220 }
1221 }
Colin Cross85a88972015-11-23 13:29:51 -08001222
Dan Willemsenb1957a52016-06-23 23:44:54 -07001223 return ret
Colin Cross06a931b2015-10-28 17:23:31 -07001224}
1225
Colin Crossa195f912019-10-16 11:07:20 -07001226// Rewrite the module's properties structs to contain os-specific values.
1227func (m *ModuleBase) setOSProperties(ctx BottomUpMutatorContext) {
1228 os := m.commonProperties.CompileOS
1229
1230 for i := range m.generalProperties {
1231 genProps := m.generalProperties[i]
1232 if m.archProperties[i] == nil {
1233 continue
1234 }
1235 for _, archProperties := range m.archProperties[i] {
1236 archPropValues := reflect.ValueOf(archProperties).Elem()
1237
Colin Crosscbbd13f2020-01-17 14:08:22 -08001238 targetProp := archPropValues.FieldByName("Target").Elem()
Colin Crossa195f912019-10-16 11:07:20 -07001239
1240 // Handle host-specific properties in the form:
1241 // target: {
1242 // host: {
1243 // key: value,
1244 // },
1245 // },
Jiyong Park1613e552020-09-14 19:43:17 +09001246 if os.Class == Host {
Colin Crossa195f912019-10-16 11:07:20 -07001247 field := "Host"
1248 prefix := "target.host"
1249 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1250 }
1251
1252 // Handle target OS generalities of the form:
1253 // target: {
1254 // bionic: {
1255 // key: value,
1256 // },
1257 // }
1258 if os.Linux() {
1259 field := "Linux"
1260 prefix := "target.linux"
1261 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1262 }
1263
1264 if os.Bionic() {
1265 field := "Bionic"
1266 prefix := "target.bionic"
1267 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1268 }
1269
1270 // Handle target OS properties in the form:
1271 // target: {
1272 // linux_glibc: {
1273 // key: value,
1274 // },
1275 // not_windows: {
1276 // key: value,
1277 // },
1278 // android {
1279 // key: value,
1280 // },
1281 // },
1282 field := os.Field
1283 prefix := "target." + os.Name
1284 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1285
Jiyong Park1613e552020-09-14 19:43:17 +09001286 if os.Class == Host && os != Windows {
Colin Crossa195f912019-10-16 11:07:20 -07001287 field := "Not_windows"
1288 prefix := "target.not_windows"
1289 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1290 }
1291
1292 // Handle 64-bit device properties in the form:
1293 // target {
1294 // android64 {
1295 // key: value,
1296 // },
1297 // android32 {
1298 // key: value,
1299 // },
1300 // },
1301 // WARNING: this is probably not what you want to use in your blueprints file, it selects
1302 // options for all targets on a device that supports 64-bit binaries, not just the targets
1303 // that are being compiled for 64-bit. Its expected use case is binaries like linker and
1304 // debuggerd that need to know when they are a 32-bit process running on a 64-bit device
1305 if os.Class == Device {
1306 if ctx.Config().Android64() {
1307 field := "Android64"
1308 prefix := "target.android64"
1309 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1310 } else {
1311 field := "Android32"
1312 prefix := "target.android32"
1313 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1314 }
1315 }
1316 }
1317 }
1318}
1319
Colin Cross3f40fa42015-01-30 17:27:36 -08001320// Rewrite the module's properties structs to contain arch-specific values.
Colin Cross4157e882019-06-06 16:57:04 -07001321func (m *ModuleBase) setArchProperties(ctx BottomUpMutatorContext) {
1322 arch := m.Arch()
1323 os := m.Os()
Colin Crossd3ba0392015-05-07 14:11:29 -07001324
Colin Cross4157e882019-06-06 16:57:04 -07001325 for i := range m.generalProperties {
1326 genProps := m.generalProperties[i]
1327 if m.archProperties[i] == nil {
Dan Willemsenb1957a52016-06-23 23:44:54 -07001328 continue
1329 }
Colin Cross4157e882019-06-06 16:57:04 -07001330 for _, archProperties := range m.archProperties[i] {
Colin Crossc17727d2018-10-24 12:42:09 -07001331 archPropValues := reflect.ValueOf(archProperties).Elem()
Dan Willemsenb1957a52016-06-23 23:44:54 -07001332
Colin Crosscbbd13f2020-01-17 14:08:22 -08001333 archProp := archPropValues.FieldByName("Arch").Elem()
1334 multilibProp := archPropValues.FieldByName("Multilib").Elem()
1335 targetProp := archPropValues.FieldByName("Target").Elem()
Dan Willemsenb1957a52016-06-23 23:44:54 -07001336
Colin Crossc17727d2018-10-24 12:42:09 -07001337 // Handle arch-specific properties in the form:
Colin Crossd5934c82017-10-02 13:55:26 -07001338 // arch: {
Colin Crossc17727d2018-10-24 12:42:09 -07001339 // arm64: {
Colin Crossd5934c82017-10-02 13:55:26 -07001340 // key: value,
1341 // },
1342 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001343 t := arch.ArchType
1344
1345 if arch.ArchType != Common {
1346 field := proptools.FieldNameForProperty(t.Name)
1347 prefix := "arch." + t.Name
Colin Cross4157e882019-06-06 16:57:04 -07001348 archStruct := m.appendProperties(ctx, genProps, archProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001349
1350 // Handle arch-variant-specific properties in the form:
1351 // arch: {
1352 // variant: {
1353 // key: value,
1354 // },
1355 // },
1356 v := variantReplacer.Replace(arch.ArchVariant)
1357 if v != "" {
1358 field := proptools.FieldNameForProperty(v)
1359 prefix := "arch." + t.Name + "." + v
Colin Cross4157e882019-06-06 16:57:04 -07001360 m.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001361 }
1362
1363 // Handle cpu-variant-specific properties in the form:
1364 // arch: {
1365 // variant: {
1366 // key: value,
1367 // },
1368 // },
1369 if arch.CpuVariant != arch.ArchVariant {
1370 c := variantReplacer.Replace(arch.CpuVariant)
1371 if c != "" {
1372 field := proptools.FieldNameForProperty(c)
1373 prefix := "arch." + t.Name + "." + c
Colin Cross4157e882019-06-06 16:57:04 -07001374 m.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001375 }
1376 }
1377
1378 // Handle arch-feature-specific properties in the form:
1379 // arch: {
1380 // feature: {
1381 // key: value,
1382 // },
1383 // },
1384 for _, feature := range arch.ArchFeatures {
1385 field := proptools.FieldNameForProperty(feature)
1386 prefix := "arch." + t.Name + "." + feature
Colin Cross4157e882019-06-06 16:57:04 -07001387 m.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001388 }
1389
1390 // Handle multilib-specific properties in the form:
1391 // multilib: {
1392 // lib32: {
1393 // key: value,
1394 // },
1395 // },
1396 field = proptools.FieldNameForProperty(t.Multilib)
1397 prefix = "multilib." + t.Multilib
Colin Cross4157e882019-06-06 16:57:04 -07001398 m.appendProperties(ctx, genProps, multilibProp, field, prefix)
Colin Cross08016332016-12-20 09:53:14 -08001399 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001400
Colin Crossa195f912019-10-16 11:07:20 -07001401 // Handle combined OS-feature and arch specific properties in the form:
Colin Crossc17727d2018-10-24 12:42:09 -07001402 // target: {
Colin Crossc17727d2018-10-24 12:42:09 -07001403 // bionic_x86: {
1404 // key: value,
1405 // },
1406 // }
Colin Crossa195f912019-10-16 11:07:20 -07001407 if os.Linux() && arch.ArchType != Common {
1408 field := "Linux_" + arch.ArchType.Name
1409 prefix := "target.linux_" + arch.ArchType.Name
Colin Cross4157e882019-06-06 16:57:04 -07001410 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossd5934c82017-10-02 13:55:26 -07001411 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001412
Colin Crossa195f912019-10-16 11:07:20 -07001413 if os.Bionic() && arch.ArchType != Common {
1414 field := "Bionic_" + t.Name
1415 prefix := "target.bionic_" + t.Name
Colin Cross4157e882019-06-06 16:57:04 -07001416 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001417 }
1418
Colin Crossa195f912019-10-16 11:07:20 -07001419 // Handle combined OS and arch specific properties in the form:
Colin Crossc17727d2018-10-24 12:42:09 -07001420 // target: {
Colin Crossc17727d2018-10-24 12:42:09 -07001421 // linux_glibc_x86: {
1422 // key: value,
1423 // },
1424 // linux_glibc_arm: {
1425 // key: value,
1426 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001427 // android_arm {
1428 // key: value,
1429 // },
1430 // android_x86 {
Colin Crossd5934c82017-10-02 13:55:26 -07001431 // key: value,
1432 // },
1433 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001434 if arch.ArchType != Common {
Colin Crossa195f912019-10-16 11:07:20 -07001435 field := os.Field + "_" + t.Name
1436 prefix := "target." + os.Name + "_" + t.Name
Colin Cross4157e882019-06-06 16:57:04 -07001437 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossd5934c82017-10-02 13:55:26 -07001438 }
1439
Colin Crossa195f912019-10-16 11:07:20 -07001440 // Handle arm on x86 properties in the form:
Colin Crossc17727d2018-10-24 12:42:09 -07001441 // target {
Colin Crossa195f912019-10-16 11:07:20 -07001442 // arm_on_x86 {
Colin Crossc17727d2018-10-24 12:42:09 -07001443 // key: value,
1444 // },
Colin Crossa195f912019-10-16 11:07:20 -07001445 // arm_on_x86_64 {
Colin Crossd5934c82017-10-02 13:55:26 -07001446 // key: value,
1447 // },
1448 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001449 if os.Class == Device {
Victor Khimenko1a31f802020-09-17 03:07:31 +02001450 if arch.ArchType == X86 && (hasArmAbi(arch) ||
1451 hasArmAndroidArch(ctx.Config().Targets[Android])) {
Colin Crossc17727d2018-10-24 12:42:09 -07001452 field := "Arm_on_x86"
1453 prefix := "target.arm_on_x86"
Colin Cross4157e882019-06-06 16:57:04 -07001454 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001455 }
Victor Khimenko1a31f802020-09-17 03:07:31 +02001456 if arch.ArchType == X86_64 && (hasArmAbi(arch) ||
1457 hasArmAndroidArch(ctx.Config().Targets[Android])) {
Colin Crossc17727d2018-10-24 12:42:09 -07001458 field := "Arm_on_x86_64"
1459 prefix := "target.arm_on_x86_64"
Colin Cross4157e882019-06-06 16:57:04 -07001460 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001461 }
Victor Khimenkoc26fcf42020-05-07 22:16:33 +02001462 if os == Android && m.Target().NativeBridge == NativeBridgeEnabled {
1463 field := "Native_bridge"
1464 prefix := "target.native_bridge"
1465 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1466 }
Colin Cross4247f0d2017-04-13 16:56:14 -07001467 }
Colin Crossbb2e2b72016-12-08 17:23:53 -08001468 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001469 }
1470}
1471
1472func forEachInterface(v reflect.Value, f func(reflect.Value)) {
1473 switch v.Kind() {
1474 case reflect.Interface:
1475 f(v)
1476 case reflect.Struct:
1477 for i := 0; i < v.NumField(); i++ {
1478 forEachInterface(v.Field(i), f)
1479 }
1480 case reflect.Ptr:
1481 forEachInterface(v.Elem(), f)
1482 default:
1483 panic(fmt.Errorf("Unsupported kind %s", v.Kind()))
1484 }
1485}
Colin Cross4225f652015-09-17 14:33:42 -07001486
Colin Crossa1ad8d12016-06-01 17:09:44 -07001487// Convert the arch product variables into a list of targets for each os class structs
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001488func decodeTargetProductVariables(config *config) (map[OsType][]Target, error) {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001489 variables := config.productVariables
Dan Willemsen490fd492015-11-24 17:53:15 -08001490
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001491 targets := make(map[OsType][]Target)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001492 var targetErr error
1493
dimitry1f33e402019-03-26 12:39:31 +01001494 addTarget := func(os OsType, archName string, archVariant, cpuVariant *string, abi []string,
dimitry8d6dde82019-07-11 10:23:53 +02001495 nativeBridgeEnabled NativeBridgeSupport, nativeBridgeHostArchName *string,
1496 nativeBridgeRelativePath *string) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001497 if targetErr != nil {
1498 return
Dan Willemsen490fd492015-11-24 17:53:15 -08001499 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001500
Dan Willemsen01a3c252019-01-11 19:02:16 -08001501 arch, err := decodeArch(os, archName, archVariant, cpuVariant, abi)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001502 if err != nil {
1503 targetErr = err
1504 return
1505 }
dimitry8d6dde82019-07-11 10:23:53 +02001506 nativeBridgeRelativePathStr := String(nativeBridgeRelativePath)
1507 nativeBridgeHostArchNameStr := String(nativeBridgeHostArchName)
1508
1509 // Use guest arch as relative install path by default
1510 if nativeBridgeEnabled && nativeBridgeRelativePathStr == "" {
1511 nativeBridgeRelativePathStr = arch.ArchType.String()
1512 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001513
Jiyong Park1613e552020-09-14 19:43:17 +09001514 // A target is considered as HostCross if it's a host target which can't run natively on
1515 // the currently configured build machine (either because the OS is different or because of
1516 // the unsupported arch)
1517 hostCross := false
1518 if os.Class == Host {
1519 var osSupported bool
1520 if os == BuildOs {
1521 osSupported = true
1522 } else if BuildOs.Linux() && os.Linux() {
1523 // LinuxBionic and Linux are compatible
1524 osSupported = true
1525 } else {
1526 osSupported = false
1527 }
1528
1529 var archSupported bool
1530 if arch.ArchType == Common {
1531 archSupported = true
1532 } else if arch.ArchType.Name == *variables.HostArch {
1533 archSupported = true
1534 } else if variables.HostSecondaryArch != nil && arch.ArchType.Name == *variables.HostSecondaryArch {
1535 archSupported = true
1536 } else {
1537 archSupported = false
1538 }
1539 if !osSupported || !archSupported {
1540 hostCross = true
1541 }
1542 }
1543
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001544 targets[os] = append(targets[os],
Colin Crossa1ad8d12016-06-01 17:09:44 -07001545 Target{
dimitry8d6dde82019-07-11 10:23:53 +02001546 Os: os,
1547 Arch: arch,
1548 NativeBridge: nativeBridgeEnabled,
1549 NativeBridgeHostArchName: nativeBridgeHostArchNameStr,
1550 NativeBridgeRelativePath: nativeBridgeRelativePathStr,
Jiyong Park1613e552020-09-14 19:43:17 +09001551 HostCross: hostCross,
Colin Crossa1ad8d12016-06-01 17:09:44 -07001552 })
Dan Willemsen490fd492015-11-24 17:53:15 -08001553 }
1554
Colin Cross4225f652015-09-17 14:33:42 -07001555 if variables.HostArch == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001556 return nil, fmt.Errorf("No host primary architecture set")
Colin Cross4225f652015-09-17 14:33:42 -07001557 }
1558
dimitry8d6dde82019-07-11 10:23:53 +02001559 addTarget(BuildOs, *variables.HostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001560
Colin Crosseeabb892015-11-20 13:07:51 -08001561 if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" {
dimitry8d6dde82019-07-11 10:23:53 +02001562 addTarget(BuildOs, *variables.HostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001563 }
1564
Colin Crossff3ae9d2018-04-10 16:15:18 -07001565 if Bool(config.Host_bionic) {
dimitry8d6dde82019-07-11 10:23:53 +02001566 addTarget(LinuxBionic, "x86_64", nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen01a405a2016-06-13 17:19:03 -07001567 }
Jiyong Park22101982020-09-17 19:09:58 +09001568 if Bool(config.Host_bionic_arm64) {
1569 addTarget(LinuxBionic, "arm64", nil, nil, nil, NativeBridgeDisabled, nil, nil)
1570 }
Dan Willemsen01a405a2016-06-13 17:19:03 -07001571
Colin Crossff3ae9d2018-04-10 16:15:18 -07001572 if String(variables.CrossHost) != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001573 crossHostOs := osByName(*variables.CrossHost)
1574 if crossHostOs == NoOsType {
1575 return nil, fmt.Errorf("Unknown cross host OS %q", *variables.CrossHost)
1576 }
1577
Colin Crossff3ae9d2018-04-10 16:15:18 -07001578 if String(variables.CrossHostArch) == "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001579 return nil, fmt.Errorf("No cross-host primary architecture set")
Dan Willemsen490fd492015-11-24 17:53:15 -08001580 }
1581
dimitry8d6dde82019-07-11 10:23:53 +02001582 addTarget(crossHostOs, *variables.CrossHostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001583
1584 if variables.CrossHostSecondaryArch != nil && *variables.CrossHostSecondaryArch != "" {
dimitry8d6dde82019-07-11 10:23:53 +02001585 addTarget(crossHostOs, *variables.CrossHostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001586 }
1587 }
1588
Dan Willemsen3f32f032016-07-11 14:36:48 -07001589 if variables.DeviceArch != nil && *variables.DeviceArch != "" {
Doug Horn21b94272019-01-16 12:06:11 -08001590 var target = Android
1591 if Bool(variables.Fuchsia) {
1592 target = Fuchsia
1593 }
1594
1595 addTarget(target, *variables.DeviceArch, variables.DeviceArchVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001596 variables.DeviceCpuVariant, variables.DeviceAbi, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001597
Dan Willemsen3f32f032016-07-11 14:36:48 -07001598 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" {
1599 addTarget(Android, *variables.DeviceSecondaryArch,
1600 variables.DeviceSecondaryArchVariant, variables.DeviceSecondaryCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001601 variables.DeviceSecondaryAbi, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001602 }
dimitry1f33e402019-03-26 12:39:31 +01001603
1604 if variables.NativeBridgeArch != nil && *variables.NativeBridgeArch != "" {
1605 addTarget(Android, *variables.NativeBridgeArch,
1606 variables.NativeBridgeArchVariant, variables.NativeBridgeCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001607 variables.NativeBridgeAbi, NativeBridgeEnabled, variables.DeviceArch,
1608 variables.NativeBridgeRelativePath)
dimitry1f33e402019-03-26 12:39:31 +01001609 }
1610
1611 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" &&
1612 variables.NativeBridgeSecondaryArch != nil && *variables.NativeBridgeSecondaryArch != "" {
1613 addTarget(Android, *variables.NativeBridgeSecondaryArch,
1614 variables.NativeBridgeSecondaryArchVariant,
1615 variables.NativeBridgeSecondaryCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001616 variables.NativeBridgeSecondaryAbi,
1617 NativeBridgeEnabled,
1618 variables.DeviceSecondaryArch,
1619 variables.NativeBridgeSecondaryRelativePath)
dimitry1f33e402019-03-26 12:39:31 +01001620 }
Colin Cross4225f652015-09-17 14:33:42 -07001621 }
1622
Colin Crossa1ad8d12016-06-01 17:09:44 -07001623 if targetErr != nil {
1624 return nil, targetErr
1625 }
1626
1627 return targets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001628}
1629
Colin Crossbb2e2b72016-12-08 17:23:53 -08001630// hasArmAbi returns true if arch has at least one arm ABI
1631func hasArmAbi(arch Arch) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -08001632 return PrefixInList(arch.Abi, "arm")
Colin Crossbb2e2b72016-12-08 17:23:53 -08001633}
1634
dimitry628db6f2019-05-22 17:16:21 +02001635// hasArmArch returns true if targets has at least non-native_bridge arm Android arch
Colin Cross4247f0d2017-04-13 16:56:14 -07001636func hasArmAndroidArch(targets []Target) bool {
1637 for _, target := range targets {
Victor Khimenko1a31f802020-09-17 03:07:31 +02001638 if target.Os == Android && target.Arch.ArchType == Arm {
Victor Khimenko5eb8ec12018-03-21 20:30:54 +01001639 return true
1640 }
1641 }
1642 return false
1643}
1644
Dan Albert4098deb2016-10-19 14:04:41 -07001645type archConfig struct {
1646 arch string
1647 archVariant string
1648 cpuVariant string
1649 abi []string
1650}
1651
1652func getMegaDeviceConfig() []archConfig {
1653 return []archConfig{
Dan Albert8818f492019-02-19 13:53:01 -08001654 {"arm", "armv7-a", "generic", []string{"armeabi-v7a"}},
Dan Willemsen110a89d2016-01-14 15:17:19 -08001655 {"arm", "armv7-a-neon", "generic", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001656 {"arm", "armv7-a-neon", "cortex-a7", []string{"armeabi-v7a"}},
1657 {"arm", "armv7-a-neon", "cortex-a8", []string{"armeabi-v7a"}},
Dan Willemsen110a89d2016-01-14 15:17:19 -08001658 {"arm", "armv7-a-neon", "cortex-a9", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001659 {"arm", "armv7-a-neon", "cortex-a15", []string{"armeabi-v7a"}},
1660 {"arm", "armv7-a-neon", "cortex-a53", []string{"armeabi-v7a"}},
1661 {"arm", "armv7-a-neon", "cortex-a53.a57", []string{"armeabi-v7a"}},
Richard Fungeb37ed32018-09-24 16:33:45 -07001662 {"arm", "armv7-a-neon", "cortex-a72", []string{"armeabi-v7a"}},
Jake Weinstein6600a442017-05-15 18:27:12 -04001663 {"arm", "armv7-a-neon", "cortex-a73", []string{"armeabi-v7a"}},
Christopher Ferrisba14a8f2018-04-23 18:15:25 -07001664 {"arm", "armv7-a-neon", "cortex-a75", []string{"armeabi-v7a"}},
Haibo Huanga31e2bd2018-10-09 14:27:28 -07001665 {"arm", "armv7-a-neon", "cortex-a76", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001666 {"arm", "armv7-a-neon", "krait", []string{"armeabi-v7a"}},
Alex Naidisae4fc182016-08-20 00:14:56 +02001667 {"arm", "armv7-a-neon", "kryo", []string{"armeabi-v7a"}},
Artem Serovd3072b02018-11-15 15:21:51 +00001668 {"arm", "armv7-a-neon", "kryo385", []string{"armeabi-v7a"}},
Junmo Park8ea49592017-07-24 07:14:55 +09001669 {"arm", "armv7-a-neon", "exynos-m1", []string{"armeabi-v7a"}},
Junmo Parkd86c9022017-07-21 09:07:47 +09001670 {"arm", "armv7-a-neon", "exynos-m2", []string{"armeabi-v7a"}},
Dan Willemsen110a89d2016-01-14 15:17:19 -08001671 {"arm64", "armv8-a", "cortex-a53", []string{"arm64-v8a"}},
Richard Fungeb37ed32018-09-24 16:33:45 -07001672 {"arm64", "armv8-a", "cortex-a72", []string{"arm64-v8a"}},
Jake Weinstein6600a442017-05-15 18:27:12 -04001673 {"arm64", "armv8-a", "cortex-a73", []string{"arm64-v8a"}},
Alex Naidisac01ff52016-08-30 15:56:33 +02001674 {"arm64", "armv8-a", "kryo", []string{"arm64-v8a"}},
Junmo Park8ea49592017-07-24 07:14:55 +09001675 {"arm64", "armv8-a", "exynos-m1", []string{"arm64-v8a"}},
Junmo Parkd86c9022017-07-21 09:07:47 +09001676 {"arm64", "armv8-a", "exynos-m2", []string{"arm64-v8a"}},
Artem Serovd3072b02018-11-15 15:21:51 +00001677 {"arm64", "armv8-2a", "kryo385", []string{"arm64-v8a"}},
Raphael Gault70b96b02020-06-18 09:56:53 +00001678 {"arm64", "armv8-2a-dotprod", "cortex-a55", []string{"arm64-v8a"}},
1679 {"arm64", "armv8-2a-dotprod", "cortex-a75", []string{"arm64-v8a"}},
1680 {"arm64", "armv8-2a-dotprod", "cortex-a76", []string{"arm64-v8a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001681 {"x86", "", "", []string{"x86"}},
1682 {"x86", "atom", "", []string{"x86"}},
1683 {"x86", "haswell", "", []string{"x86"}},
1684 {"x86", "ivybridge", "", []string{"x86"}},
1685 {"x86", "sandybridge", "", []string{"x86"}},
1686 {"x86", "silvermont", "", []string{"x86"}},
Benjamin Gordon87e7f2f2019-02-14 10:59:48 -07001687 {"x86", "stoneyridge", "", []string{"x86"}},
Dan Willemsen8a354052016-05-10 14:30:51 -07001688 {"x86", "x86_64", "", []string{"x86"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001689 {"x86_64", "", "", []string{"x86_64"}},
1690 {"x86_64", "haswell", "", []string{"x86_64"}},
1691 {"x86_64", "ivybridge", "", []string{"x86_64"}},
1692 {"x86_64", "sandybridge", "", []string{"x86_64"}},
1693 {"x86_64", "silvermont", "", []string{"x86_64"}},
Benjamin Gordon87e7f2f2019-02-14 10:59:48 -07001694 {"x86_64", "stoneyridge", "", []string{"x86_64"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001695 }
Dan Albert4098deb2016-10-19 14:04:41 -07001696}
Dan Willemsen322acaf2016-01-12 23:07:05 -08001697
Dan Albert4098deb2016-10-19 14:04:41 -07001698func getNdkAbisConfig() []archConfig {
1699 return []archConfig{
Dan Albert6bba6442020-01-30 15:16:49 -08001700 {"arm", "armv7-a", "", []string{"armeabi-v7a"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001701 {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001702 {"x86", "", "", []string{"x86"}},
1703 {"x86_64", "", "", []string{"x86_64"}},
1704 }
1705}
1706
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001707func getAmlAbisConfig() []archConfig {
1708 return []archConfig{
1709 {"arm", "armv7-a", "", []string{"armeabi-v7a"}},
1710 {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
1711 {"x86", "", "", []string{"x86"}},
1712 {"x86_64", "", "", []string{"x86_64"}},
1713 }
1714}
1715
Dan Willemsen01a3c252019-01-11 19:02:16 -08001716func decodeArchSettings(os OsType, archConfigs []archConfig) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001717 var ret []Target
Dan Willemsen322acaf2016-01-12 23:07:05 -08001718
Dan Albert4098deb2016-10-19 14:04:41 -07001719 for _, config := range archConfigs {
Dan Willemsen01a3c252019-01-11 19:02:16 -08001720 arch, err := decodeArch(os, config.arch, &config.archVariant,
Colin Crossa74ca042019-01-31 14:31:51 -08001721 &config.cpuVariant, config.abi)
Dan Willemsen322acaf2016-01-12 23:07:05 -08001722 if err != nil {
1723 return nil, err
1724 }
Colin Cross3b19f5d2019-09-17 14:45:31 -07001725
Colin Crossa1ad8d12016-06-01 17:09:44 -07001726 ret = append(ret, Target{
1727 Os: Android,
1728 Arch: arch,
1729 })
Dan Willemsen322acaf2016-01-12 23:07:05 -08001730 }
1731
1732 return ret, nil
1733}
1734
Colin Cross4225f652015-09-17 14:33:42 -07001735// Convert a set of strings from product variables into a single Arch struct
Colin Crossa74ca042019-01-31 14:31:51 -08001736func decodeArch(os OsType, arch string, archVariant, cpuVariant *string, abi []string) (Arch, error) {
Colin Cross4225f652015-09-17 14:33:42 -07001737 stringPtr := func(p *string) string {
1738 if p != nil {
1739 return *p
1740 }
1741 return ""
1742 }
1743
Colin Crosseeabb892015-11-20 13:07:51 -08001744 archType, ok := archTypeMap[arch]
1745 if !ok {
1746 return Arch{}, fmt.Errorf("unknown arch %q", arch)
1747 }
Colin Cross4225f652015-09-17 14:33:42 -07001748
Colin Crosseeabb892015-11-20 13:07:51 -08001749 a := Arch{
Colin Cross4225f652015-09-17 14:33:42 -07001750 ArchType: archType,
1751 ArchVariant: stringPtr(archVariant),
1752 CpuVariant: stringPtr(cpuVariant),
Colin Crossa74ca042019-01-31 14:31:51 -08001753 Abi: abi,
Colin Crosseeabb892015-11-20 13:07:51 -08001754 }
1755
1756 if a.ArchVariant == a.ArchType.Name || a.ArchVariant == "generic" {
1757 a.ArchVariant = ""
1758 }
1759
1760 if a.CpuVariant == a.ArchType.Name || a.CpuVariant == "generic" {
1761 a.CpuVariant = ""
1762 }
1763
1764 for i := 0; i < len(a.Abi); i++ {
1765 if a.Abi[i] == "" {
1766 a.Abi = append(a.Abi[:i], a.Abi[i+1:]...)
1767 i--
1768 }
1769 }
1770
Dan Willemsen01a3c252019-01-11 19:02:16 -08001771 if a.ArchVariant == "" {
1772 if featureMap, ok := defaultArchFeatureMap[os]; ok {
1773 a.ArchFeatures = featureMap[archType]
1774 }
1775 } else {
1776 if featureMap, ok := archFeatureMap[archType]; ok {
1777 a.ArchFeatures = featureMap[a.ArchVariant]
1778 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001779 }
1780
Colin Crosseeabb892015-11-20 13:07:51 -08001781 return a, nil
Colin Cross4225f652015-09-17 14:33:42 -07001782}
1783
Colin Cross69617d32016-09-06 10:39:07 -07001784func filterMultilibTargets(targets []Target, multilib string) []Target {
1785 var ret []Target
1786 for _, t := range targets {
1787 if t.Arch.ArchType.Multilib == multilib {
1788 ret = append(ret, t)
1789 }
1790 }
1791 return ret
1792}
1793
Paul Duffinca7f0ef2020-02-25 15:50:49 +00001794// Return the set of Os specific common architecture targets for each Os in a list of
1795// targets.
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001796func getCommonTargets(targets []Target) []Target {
1797 var ret []Target
1798 set := make(map[string]bool)
1799
1800 for _, t := range targets {
1801 if _, found := set[t.Os.String()]; !found {
1802 set[t.Os.String()] = true
1803 ret = append(ret, commonTargetMap[t.Os.String()])
1804 }
1805 }
1806
1807 return ret
1808}
1809
Colin Cross3dceee32018-09-06 10:19:57 -07001810func firstTarget(targets []Target, filters ...string) []Target {
Jiyong Park22101982020-09-17 19:09:58 +09001811 // find the first target from each OS
1812 var ret []Target
1813 hasHost := false
1814 set := make(map[OsType]bool)
1815
Colin Cross6b4a32d2017-12-05 13:42:45 -08001816 for _, filter := range filters {
1817 buildTargets := filterMultilibTargets(targets, filter)
Jiyong Park22101982020-09-17 19:09:58 +09001818 for _, t := range buildTargets {
1819 if _, found := set[t.Os]; !found {
1820 hasHost = hasHost || (t.Os.Class == Host)
1821 set[t.Os] = true
1822 ret = append(ret, t)
1823 }
Colin Cross6b4a32d2017-12-05 13:42:45 -08001824 }
1825 }
Jiyong Park22101982020-09-17 19:09:58 +09001826 return ret
Colin Cross6b4a32d2017-12-05 13:42:45 -08001827}
1828
Colin Crossa1ad8d12016-06-01 17:09:44 -07001829// Use the module multilib setting to select one or more targets from a target list
Colin Crossee0bc3b2018-10-02 22:01:37 -07001830func decodeMultilibTargets(multilib string, targets []Target, prefer32 bool) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001831 buildTargets := []Target{}
Colin Cross6b4a32d2017-12-05 13:42:45 -08001832
Colin Cross4225f652015-09-17 14:33:42 -07001833 switch multilib {
1834 case "common":
Colin Cross6b4a32d2017-12-05 13:42:45 -08001835 buildTargets = getCommonTargets(targets)
1836 case "common_first":
1837 buildTargets = getCommonTargets(targets)
1838 if prefer32 {
Colin Cross3dceee32018-09-06 10:19:57 -07001839 buildTargets = append(buildTargets, firstTarget(targets, "lib32", "lib64")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001840 } else {
Colin Cross3dceee32018-09-06 10:19:57 -07001841 buildTargets = append(buildTargets, firstTarget(targets, "lib64", "lib32")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001842 }
Colin Cross4225f652015-09-17 14:33:42 -07001843 case "both":
Colin Cross8b74d172016-09-13 09:59:14 -07001844 if prefer32 {
1845 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1846 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1847 } else {
1848 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1849 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1850 }
Colin Cross4225f652015-09-17 14:33:42 -07001851 case "32":
Colin Cross69617d32016-09-06 10:39:07 -07001852 buildTargets = filterMultilibTargets(targets, "lib32")
Colin Cross4225f652015-09-17 14:33:42 -07001853 case "64":
Colin Cross69617d32016-09-06 10:39:07 -07001854 buildTargets = filterMultilibTargets(targets, "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001855 case "first":
1856 if prefer32 {
Colin Cross3dceee32018-09-06 10:19:57 -07001857 buildTargets = firstTarget(targets, "lib32", "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001858 } else {
Colin Cross3dceee32018-09-06 10:19:57 -07001859 buildTargets = firstTarget(targets, "lib64", "lib32")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001860 }
Colin Cross69617d32016-09-06 10:39:07 -07001861 case "prefer32":
Colin Cross3dceee32018-09-06 10:19:57 -07001862 buildTargets = filterMultilibTargets(targets, "lib32")
1863 if len(buildTargets) == 0 {
1864 buildTargets = filterMultilibTargets(targets, "lib64")
1865 }
Colin Cross4225f652015-09-17 14:33:42 -07001866 default:
Colin Cross69617d32016-09-06 10:39:07 -07001867 return nil, fmt.Errorf(`compile_multilib must be "both", "first", "32", "64", or "prefer32" found %q`,
Colin Cross4225f652015-09-17 14:33:42 -07001868 multilib)
Colin Cross4225f652015-09-17 14:33:42 -07001869 }
1870
Colin Crossa1ad8d12016-06-01 17:09:44 -07001871 return buildTargets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001872}