blob: eb3c709f75c362e8f242d8db91121e4208cb994b [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)
Dan Willemsen00fcbde2016-11-17 00:25:59 -0800593 Windows = NewOsType("windows", HostCross, true)
594 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
624 HostCross
625)
626
Colin Cross67a5c132017-05-09 13:45:28 -0700627func (class OsClass) String() string {
628 switch class {
629 case Generic:
630 return "generic"
631 case Device:
632 return "device"
633 case Host:
634 return "host"
635 case HostCross:
636 return "host cross"
637 default:
638 panic(fmt.Errorf("unknown class %d", class))
639 }
640}
641
Colin Crossa1ad8d12016-06-01 17:09:44 -0700642func (os OsType) String() string {
643 return os.Name
Colin Cross54c71122016-06-01 17:09:44 -0700644}
645
Dan Willemsen866b5632017-09-22 12:28:24 -0700646func (os OsType) Bionic() bool {
647 return os == Android || os == LinuxBionic
648}
649
650func (os OsType) Linux() bool {
651 return os == Android || os == Linux || os == LinuxBionic
652}
653
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800654func NewOsType(name string, class OsClass, defDisabled bool) OsType {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700655 os := OsType{
656 Name: name,
657 Field: strings.Title(name),
658 Class: class,
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800659
660 DefaultDisabled: defDisabled,
Colin Cross54c71122016-06-01 17:09:44 -0700661 }
Paul Duffina04c1072020-03-02 10:16:35 +0000662 OsTypeList = append(OsTypeList, os)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800663
664 if _, found := commonTargetMap[name]; found {
665 panic(fmt.Errorf("Found Os type duplicate during OsType registration: %q", name))
666 } else {
667 commonTargetMap[name] = Target{Os: os, Arch: Arch{ArchType: Common}}
668 }
669
Colin Crossa1ad8d12016-06-01 17:09:44 -0700670 return os
671}
672
673func osByName(name string) OsType {
Paul Duffina04c1072020-03-02 10:16:35 +0000674 for _, os := range OsTypeList {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700675 if os.Name == name {
676 return os
677 }
678 }
679
680 return NoOsType
Dan Willemsen490fd492015-11-24 17:53:15 -0800681}
682
dimitry1f33e402019-03-26 12:39:31 +0100683type NativeBridgeSupport bool
684
685const (
686 NativeBridgeDisabled NativeBridgeSupport = false
687 NativeBridgeEnabled NativeBridgeSupport = true
688)
689
Colin Crossa1ad8d12016-06-01 17:09:44 -0700690type Target struct {
dimitry8d6dde82019-07-11 10:23:53 +0200691 Os OsType
692 Arch Arch
693 NativeBridge NativeBridgeSupport
694 NativeBridgeHostArchName string
695 NativeBridgeRelativePath string
Colin Crossd3ba0392015-05-07 14:11:29 -0700696}
697
Colin Crossa1ad8d12016-06-01 17:09:44 -0700698func (target Target) String() string {
Colin Crossa195f912019-10-16 11:07:20 -0700699 return target.OsVariation() + "_" + target.ArchVariation()
700}
701
702func (target Target) OsVariation() string {
703 return target.Os.String()
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700704}
705
706func (target Target) ArchVariation() string {
707 var variation string
dimitry1f33e402019-03-26 12:39:31 +0100708 if target.NativeBridge {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700709 variation = "native_bridge_"
dimitry1f33e402019-03-26 12:39:31 +0100710 }
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700711 variation += target.Arch.String()
712
Colin Crossa195f912019-10-16 11:07:20 -0700713 return variation
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700714}
715
716func (target Target) Variations() []blueprint.Variation {
717 return []blueprint.Variation{
Colin Crossa195f912019-10-16 11:07:20 -0700718 {Mutator: "os", Variation: target.OsVariation()},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700719 {Mutator: "arch", Variation: target.ArchVariation()},
720 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800721}
722
Colin Cross617b88a2020-08-24 18:04:09 -0700723func osMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Crossa195f912019-10-16 11:07:20 -0700724 var module Module
725 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700726 if module, ok = bpctx.Module().(Module); !ok {
727 if bootstrap.IsBootstrapModule(bpctx.Module()) {
728 // Bootstrap Go modules are always the build OS or linux bionic.
729 config := bpctx.Config().(Config)
730 osNames := []string{config.BuildOSTarget.OsVariation()}
731 for _, hostCrossTarget := range config.Targets[LinuxBionic] {
732 if hostCrossTarget.Arch.ArchType == config.BuildOSTarget.Arch.ArchType {
733 osNames = append(osNames, hostCrossTarget.OsVariation())
734 }
735 }
736 osNames = FirstUniqueStrings(osNames)
737 bpctx.CreateVariations(osNames...)
738 }
Colin Crossa195f912019-10-16 11:07:20 -0700739 return
740 }
741
Colin Cross617b88a2020-08-24 18:04:09 -0700742 // Bootstrap Go module support above requires this mutator to be a
743 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
744 // filters out non-Soong modules. Now that we've handled them, create a
745 // normal android.BottomUpMutatorContext.
746 mctx := bottomUpMutatorContextFactory(bpctx, module, false)
747
Colin Crossa195f912019-10-16 11:07:20 -0700748 base := module.base()
749
750 if !base.ArchSpecific() {
751 return
752 }
753
754 osClasses := base.OsClassSupported()
755
756 var moduleOSList []OsType
757
Paul Duffina04c1072020-03-02 10:16:35 +0000758 for _, os := range OsTypeList {
Colin Crossa195f912019-10-16 11:07:20 -0700759 supportedClass := false
760 for _, osClass := range osClasses {
761 if os.Class == osClass {
762 supportedClass = true
763 }
764 }
765 if !supportedClass {
766 continue
767 }
768
769 if len(mctx.Config().Targets[os]) == 0 {
770 continue
771 }
772
773 moduleOSList = append(moduleOSList, os)
774 }
775
776 if len(moduleOSList) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900777 base.Disable()
Colin Crossa195f912019-10-16 11:07:20 -0700778 return
779 }
780
781 osNames := make([]string, len(moduleOSList))
782
783 for i, os := range moduleOSList {
784 osNames[i] = os.String()
785 }
786
Paul Duffin1356d8c2020-02-25 19:26:33 +0000787 createCommonOSVariant := base.commonProperties.CreateCommonOSVariant
788 if createCommonOSVariant {
789 // A CommonOS variant was requested so add it to the list of OS's variants to
790 // create. It needs to be added to the end because it needs to depend on the
791 // the other variants in the list returned by CreateVariations(...) and inter
792 // variant dependencies can only be created from a later variant in that list to
793 // an earlier one. That is because variants are always processed in the order in
794 // which they are returned from CreateVariations(...).
795 osNames = append(osNames, CommonOS.Name)
796 moduleOSList = append(moduleOSList, CommonOS)
Colin Crossa195f912019-10-16 11:07:20 -0700797 }
798
Paul Duffin1356d8c2020-02-25 19:26:33 +0000799 modules := mctx.CreateVariations(osNames...)
800 for i, m := range modules {
801 m.base().commonProperties.CompileOS = moduleOSList[i]
802 m.base().setOSProperties(mctx)
803 }
804
805 if createCommonOSVariant {
806 // A CommonOS variant was requested so add dependencies from it (the last one in
807 // the list) to the OS type specific variants.
808 last := len(modules) - 1
809 commonOSVariant := modules[last]
810 commonOSVariant.base().commonProperties.CommonOSVariant = true
811 for _, module := range modules[0:last] {
812 // Ignore modules that are enabled. Note, this will only avoid adding
813 // dependencies on OsType variants that are explicitly disabled in their
814 // properties. The CommonOS variant will still depend on disabled variants
815 // if they are disabled afterwards, e.g. in archMutator if
816 if module.Enabled() {
817 mctx.AddInterVariantDependency(commonOsToOsSpecificVariantTag, commonOSVariant, module)
818 }
819 }
820 }
821}
822
823// Identifies the dependency from CommonOS variant to the os specific variants.
824type commonOSTag struct{ blueprint.BaseDependencyTag }
825
826var commonOsToOsSpecificVariantTag = commonOSTag{}
827
828// Get the OsType specific variants for the current CommonOS variant.
829//
830// The returned list will only contain enabled OsType specific variants of the
831// module referenced in the supplied context. An empty list is returned if there
832// are no enabled variants or the supplied context is not for an CommonOS
833// variant.
834func GetOsSpecificVariantsOfCommonOSVariant(mctx BaseModuleContext) []Module {
835 var variants []Module
836 mctx.VisitDirectDeps(func(m Module) {
837 if mctx.OtherModuleDependencyTag(m) == commonOsToOsSpecificVariantTag {
838 if m.Enabled() {
839 variants = append(variants, m)
840 }
841 }
842 })
843
844 return variants
Colin Crossa195f912019-10-16 11:07:20 -0700845}
846
Colin Crossee0bc3b2018-10-02 22:01:37 -0700847// archMutator splits a module into a variant for each Target requested by the module. Target selection
848// for a module is in three levels, OsClass, mulitlib, and then Target.
849// OsClass selection is determined by:
850// - The HostOrDeviceSupported value passed in to InitAndroidArchModule by the module type factory, which selects
851// whether the module type can compile for host, device or both.
852// - The host_supported and device_supported properties on the module.
Roland Levillainf5b635d2019-06-05 14:42:57 +0100853// 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 -0700854// for the module, the Device OsClass is selected.
855// Within each selected OsClass, the multilib selection is determined by:
Jaewoong Jung02b2d4d2019-06-06 15:19:57 -0700856// - The compile_multilib property if it set (which may be overridden by target.android.compile_multilib or
Colin Crossee0bc3b2018-10-02 22:01:37 -0700857// target.host.compile_multilib).
858// - The default multilib passed to InitAndroidArchModule if compile_multilib was not set.
859// Valid multilib values include:
860// "both": compile for all Targets supported by the OsClass (generally x86_64 and x86, or arm64 and arm).
861// "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 -0700862// but may be arm for a 32-bit only build.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700863// "32": compile for only a single 32-bit Target supported by the OsClass.
864// "64": compile for only a single 64-bit Target supported by the OsClass.
865// "common": compile a for a single Target that will work on all Targets suported by the OsClass (for example Java).
866//
867// Once the list of Targets is determined, the module is split into a variant for each Target.
868//
869// Modules can be initialized with InitAndroidMultiTargetsArchModule, in which case they will be split by OsClass,
870// 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 -0700871func archMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700872 var module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800873 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700874 if module, ok = bpctx.Module().(Module); !ok {
875 if bootstrap.IsBootstrapModule(bpctx.Module()) {
876 // Bootstrap Go modules are always the build architecture.
877 bpctx.CreateVariations(bpctx.Config().(Config).BuildOSTarget.ArchVariation())
878 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800879 return
880 }
881
Colin Cross617b88a2020-08-24 18:04:09 -0700882 // Bootstrap Go module support above requires this mutator to be a
883 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
884 // filters out non-Soong modules. Now that we've handled them, create a
885 // normal android.BottomUpMutatorContext.
886 mctx := bottomUpMutatorContextFactory(bpctx, module, false)
887
Colin Cross5eca7cb2018-10-02 14:02:10 -0700888 base := module.base()
889
890 if !base.ArchSpecific() {
Colin Crossb9db4802016-06-03 01:50:47 +0000891 return
892 }
893
Colin Crossa195f912019-10-16 11:07:20 -0700894 os := base.commonProperties.CompileOS
Paul Duffin1356d8c2020-02-25 19:26:33 +0000895 if os == CommonOS {
896 // Make sure that the target related properties are initialized for the
897 // CommonOS variant.
898 addTargetProperties(module, commonTargetMap[os.Name], nil, true)
899
900 // Do not create arch specific variants for the CommonOS variant.
901 return
902 }
903
Colin Crossa195f912019-10-16 11:07:20 -0700904 osTargets := mctx.Config().Targets[os]
Colin Crossfb0c16e2019-11-20 17:12:35 -0800905 image := base.commonProperties.ImageVariation
Colin Crossa195f912019-10-16 11:07:20 -0700906 // Filter NativeBridge targets unless they are explicitly supported
Colin Cross83bead42019-12-18 10:45:46 -0800907 // Skip creating native bridge variants for vendor modules
908 if os == Android &&
909 !(Bool(base.commonProperties.Native_bridge_supported) && image == CoreVariation) {
910
Colin Crossa195f912019-10-16 11:07:20 -0700911 var targets []Target
912 for _, t := range osTargets {
913 if !t.NativeBridge {
914 targets = append(targets, t)
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700915 }
916 }
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700917
Colin Crossa195f912019-10-16 11:07:20 -0700918 osTargets = targets
919 }
Colin Crossee0bc3b2018-10-02 22:01:37 -0700920
Yifan Hong1b3348d2020-01-21 15:53:22 -0800921 // only the primary arch in the ramdisk / recovery partition
922 if os == Android && (module.InstallInRecovery() || module.InstallInRamdisk()) {
Colin Crossa195f912019-10-16 11:07:20 -0700923 osTargets = []Target{osTargets[0]}
924 }
dimitry1f33e402019-03-26 12:39:31 +0100925
Colin Crossa195f912019-10-16 11:07:20 -0700926 prefer32 := false
927 if base.prefer32 != nil {
928 prefer32 = base.prefer32(mctx, base, os.Class)
929 }
dimitry1f33e402019-03-26 12:39:31 +0100930
Colin Crossa195f912019-10-16 11:07:20 -0700931 multilib, extraMultilib := decodeMultilib(base, os.Class)
932 targets, err := decodeMultilibTargets(multilib, osTargets, prefer32)
933 if err != nil {
934 mctx.ModuleErrorf("%s", err.Error())
935 }
Colin Cross5eca7cb2018-10-02 14:02:10 -0700936
Colin Crossa195f912019-10-16 11:07:20 -0700937 var multiTargets []Target
938 if extraMultilib != "" {
939 multiTargets, err = decodeMultilibTargets(extraMultilib, osTargets, prefer32)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700940 if err != nil {
941 mctx.ModuleErrorf("%s", err.Error())
942 }
Colin Crossb9db4802016-06-03 01:50:47 +0000943 }
944
Colin Crossfb0c16e2019-11-20 17:12:35 -0800945 if image == RecoveryVariation {
946 primaryArch := mctx.Config().DevicePrimaryArchType()
947 targets = filterToArch(targets, primaryArch)
948 multiTargets = filterToArch(multiTargets, primaryArch)
949 }
950
Colin Crossa195f912019-10-16 11:07:20 -0700951 if len(targets) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900952 base.Disable()
Dan Willemsen3f32f032016-07-11 14:36:48 -0700953 return
954 }
955
Colin Crossa195f912019-10-16 11:07:20 -0700956 targetNames := make([]string, len(targets))
Colin Crossb9db4802016-06-03 01:50:47 +0000957
Colin Crossa195f912019-10-16 11:07:20 -0700958 for i, target := range targets {
959 targetNames[i] = target.ArchVariation()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700960 }
961
962 modules := mctx.CreateVariations(targetNames...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800963 for i, m := range modules {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000964 addTargetProperties(m, targets[i], multiTargets, i == 0)
Colin Cross617b88a2020-08-24 18:04:09 -0700965 m.base().setArchProperties(mctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800966 }
967}
968
Paul Duffin1356d8c2020-02-25 19:26:33 +0000969func addTargetProperties(m Module, target Target, multiTargets []Target, primaryTarget bool) {
970 m.base().commonProperties.CompileTarget = target
971 m.base().commonProperties.CompileMultiTargets = multiTargets
972 m.base().commonProperties.CompilePrimary = primaryTarget
973}
974
Colin Crossee0bc3b2018-10-02 22:01:37 -0700975func decodeMultilib(base *ModuleBase, class OsClass) (multilib, extraMultilib string) {
976 switch class {
977 case Device:
978 multilib = String(base.commonProperties.Target.Android.Compile_multilib)
979 case Host, HostCross:
980 multilib = String(base.commonProperties.Target.Host.Compile_multilib)
981 }
982 if multilib == "" {
983 multilib = String(base.commonProperties.Compile_multilib)
984 }
985 if multilib == "" {
986 multilib = base.commonProperties.Default_multilib
987 }
988
989 if base.commonProperties.UseTargetVariants {
990 return multilib, ""
991 } else {
992 // For app modules a single arch variant will be created per OS class which is expected to handle all the
993 // selected arches. Return the common-type as multilib and any Android.bp provided multilib as extraMultilib
994 if multilib == base.commonProperties.Default_multilib {
995 multilib = "first"
996 }
997 return base.commonProperties.Default_multilib, multilib
998 }
999}
1000
Colin Crossfb0c16e2019-11-20 17:12:35 -08001001func filterToArch(targets []Target, arch ArchType) []Target {
1002 for i := 0; i < len(targets); i++ {
1003 if targets[i].Arch.ArchType != arch {
1004 targets = append(targets[:i], targets[i+1:]...)
1005 i--
1006 }
1007 }
1008 return targets
1009}
1010
Colin Crosscbbd13f2020-01-17 14:08:22 -08001011type archPropTypeDesc struct {
1012 arch, multilib, target reflect.Type
1013}
1014
1015type archPropRoot struct {
1016 Arch, Multilib, Target interface{}
1017}
1018
1019// createArchPropTypeDesc takes a reflect.Type that is either a struct or a pointer to a struct, and
1020// returns lists of reflect.Types that contains the arch-variant properties inside structs for each
1021// arch, multilib and target property.
1022func createArchPropTypeDesc(props reflect.Type) []archPropTypeDesc {
Colin Crossb1d8c992020-01-21 11:43:29 -08001023 // Each property struct shard will be nested many times under the runtime generated arch struct,
1024 // which can hit the limit of 64kB for the name of runtime generated structs. They are nested
1025 // 97 times now, which may grow in the future, plus there is some overhead for the containing
1026 // type. This number may need to be reduced if too many are added, but reducing it too far
1027 // could cause problems if a single deeply nested property no longer fits in the name.
1028 const maxArchTypeNameSize = 500
1029
1030 propShards, _ := proptools.FilterPropertyStructSharded(props, maxArchTypeNameSize, filterArchStruct)
Colin Crosscb988072019-01-24 14:58:11 -08001031 if len(propShards) == 0 {
Dan Willemsenb1957a52016-06-23 23:44:54 -07001032 return nil
1033 }
1034
Colin Crosscbbd13f2020-01-17 14:08:22 -08001035 var ret []archPropTypeDesc
Colin Crossc17727d2018-10-24 12:42:09 -07001036 for _, props := range propShards {
Dan Willemsenb1957a52016-06-23 23:44:54 -07001037
Colin Crossc17727d2018-10-24 12:42:09 -07001038 variantFields := func(names []string) []reflect.StructField {
1039 ret := make([]reflect.StructField, len(names))
Dan Willemsenb1957a52016-06-23 23:44:54 -07001040
Colin Crossc17727d2018-10-24 12:42:09 -07001041 for i, name := range names {
1042 ret[i].Name = name
1043 ret[i].Type = props
Dan Willemsen866b5632017-09-22 12:28:24 -07001044 }
Colin Crossc17727d2018-10-24 12:42:09 -07001045
1046 return ret
1047 }
1048
1049 archFields := make([]reflect.StructField, len(archTypeList))
1050 for i, arch := range archTypeList {
1051 variants := []string{}
1052
1053 for _, archVariant := range archVariants[arch] {
1054 archVariant := variantReplacer.Replace(archVariant)
1055 variants = append(variants, proptools.FieldNameForProperty(archVariant))
1056 }
1057 for _, feature := range archFeatures[arch] {
1058 feature := variantReplacer.Replace(feature)
1059 variants = append(variants, proptools.FieldNameForProperty(feature))
1060 }
1061
1062 fields := variantFields(variants)
1063
1064 fields = append([]reflect.StructField{{
1065 Name: "BlueprintEmbed",
1066 Type: props,
1067 Anonymous: true,
1068 }}, fields...)
1069
1070 archFields[i] = reflect.StructField{
1071 Name: arch.Field,
1072 Type: reflect.StructOf(fields),
1073 }
1074 }
1075 archType := reflect.StructOf(archFields)
1076
1077 multilibType := reflect.StructOf(variantFields([]string{"Lib32", "Lib64"}))
1078
1079 targets := []string{
1080 "Host",
1081 "Android64",
1082 "Android32",
1083 "Bionic",
1084 "Linux",
1085 "Not_windows",
1086 "Arm_on_x86",
1087 "Arm_on_x86_64",
Victor Khimenkoc26fcf42020-05-07 22:16:33 +02001088 "Native_bridge",
Colin Crossc17727d2018-10-24 12:42:09 -07001089 }
Paul Duffina04c1072020-03-02 10:16:35 +00001090 for _, os := range OsTypeList {
Colin Crossc17727d2018-10-24 12:42:09 -07001091 targets = append(targets, os.Field)
1092
1093 for _, archType := range osArchTypeMap[os] {
1094 targets = append(targets, os.Field+"_"+archType.Name)
1095
1096 if os.Linux() {
1097 target := "Linux_" + archType.Name
1098 if !InList(target, targets) {
1099 targets = append(targets, target)
1100 }
1101 }
1102 if os.Bionic() {
1103 target := "Bionic_" + archType.Name
1104 if !InList(target, targets) {
1105 targets = append(targets, target)
1106 }
Dan Willemsen866b5632017-09-22 12:28:24 -07001107 }
1108 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001109 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001110
Colin Crossc17727d2018-10-24 12:42:09 -07001111 targetType := reflect.StructOf(variantFields(targets))
Colin Crosscbbd13f2020-01-17 14:08:22 -08001112
1113 ret = append(ret, archPropTypeDesc{
1114 arch: reflect.PtrTo(archType),
1115 multilib: reflect.PtrTo(multilibType),
1116 target: reflect.PtrTo(targetType),
1117 })
Colin Crossc17727d2018-10-24 12:42:09 -07001118 }
1119 return ret
Dan Willemsenb1957a52016-06-23 23:44:54 -07001120}
1121
Colin Cross74449102019-09-25 11:26:40 -07001122func filterArchStruct(field reflect.StructField, prefix string) (bool, reflect.StructField) {
1123 if proptools.HasTag(field, "android", "arch_variant") {
1124 // The arch_variant field isn't necessary past this point
1125 // Instead of wasting space, just remove it. Go also has a
1126 // 16-bit limit on structure name length. The name is constructed
1127 // based on the Go source representation of the structure, so
1128 // the tag names count towards that length.
Colin Crossb4fecbf2020-01-21 11:38:47 -08001129
1130 androidTag := field.Tag.Get("android")
1131 values := strings.Split(androidTag, ",")
1132
1133 if string(field.Tag) != `android:"`+strings.Join(values, ",")+`"` {
1134 panic(fmt.Errorf("unexpected tag format %q", field.Tag))
Colin Cross74449102019-09-25 11:26:40 -07001135 }
Colin Crossb4fecbf2020-01-21 11:38:47 -08001136 // these tags don't need to be present in the runtime generated struct type.
1137 values = RemoveListFromList(values, []string{"arch_variant", "variant_prepend", "path"})
1138 if len(values) > 0 {
1139 panic(fmt.Errorf("unknown tags %q in field %q", values, prefix+field.Name))
1140 }
1141
1142 field.Tag = ""
Colin Cross74449102019-09-25 11:26:40 -07001143 return true, field
1144 }
1145 return false, field
1146}
1147
Dan Willemsenb1957a52016-06-23 23:44:54 -07001148var archPropTypeMap OncePer
1149
Colin Cross36242852017-06-23 15:06:31 -07001150func InitArchModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001151
1152 base := m.base()
1153
Colin Cross36242852017-06-23 15:06:31 -07001154 base.generalProperties = m.GetProperties()
Colin Cross3f40fa42015-01-30 17:27:36 -08001155
1156 for _, properties := range base.generalProperties {
1157 propertiesValue := reflect.ValueOf(properties)
Colin Cross62496a02016-08-08 15:49:17 -07001158 t := propertiesValue.Type()
Colin Cross3f40fa42015-01-30 17:27:36 -08001159 if propertiesValue.Kind() != reflect.Ptr {
Colin Crossca860ac2016-01-04 14:34:37 -08001160 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
1161 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001162 }
1163
1164 propertiesValue = propertiesValue.Elem()
1165 if propertiesValue.Kind() != reflect.Struct {
Colin Crossca860ac2016-01-04 14:34:37 -08001166 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
1167 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001168 }
1169
Colin Cross571cccf2019-02-04 11:22:08 -08001170 archPropTypes := archPropTypeMap.Once(NewCustomOnceKey(t), func() interface{} {
Colin Crosscbbd13f2020-01-17 14:08:22 -08001171 return createArchPropTypeDesc(t)
1172 }).([]archPropTypeDesc)
Colin Cross3f40fa42015-01-30 17:27:36 -08001173
Colin Crossc17727d2018-10-24 12:42:09 -07001174 var archProperties []interface{}
1175 for _, t := range archPropTypes {
Colin Crosscbbd13f2020-01-17 14:08:22 -08001176 archProperties = append(archProperties, &archPropRoot{
1177 Arch: reflect.Zero(t.arch).Interface(),
1178 Multilib: reflect.Zero(t.multilib).Interface(),
1179 Target: reflect.Zero(t.target).Interface(),
1180 })
Dan Willemsenb1957a52016-06-23 23:44:54 -07001181 }
Colin Crossc17727d2018-10-24 12:42:09 -07001182 base.archProperties = append(base.archProperties, archProperties)
1183 m.AddProperties(archProperties...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001184 }
1185
Colin Cross36242852017-06-23 15:06:31 -07001186 base.customizableProperties = m.GetProperties()
Colin Cross3f40fa42015-01-30 17:27:36 -08001187}
1188
Colin Crossa716add2015-12-16 11:07:39 -08001189var variantReplacer = strings.NewReplacer("-", "_", ".", "_")
Colin Crossec193632015-07-06 17:49:43 -07001190
Colin Cross4157e882019-06-06 16:57:04 -07001191func (m *ModuleBase) appendProperties(ctx BottomUpMutatorContext,
Dan Willemsenb1957a52016-06-23 23:44:54 -07001192 dst interface{}, src reflect.Value, field, srcPrefix string) reflect.Value {
Colin Cross06a931b2015-10-28 17:23:31 -07001193
Colin Crosscbbd13f2020-01-17 14:08:22 -08001194 if src.Kind() == reflect.Ptr {
1195 if src.IsNil() {
1196 return src
1197 }
1198 src = src.Elem()
1199 }
1200
Dan Willemsenb1957a52016-06-23 23:44:54 -07001201 src = src.FieldByName(field)
1202 if !src.IsValid() {
Colin Crosseeabb892015-11-20 13:07:51 -08001203 ctx.ModuleErrorf("field %q does not exist", srcPrefix)
Dan Willemsenb1957a52016-06-23 23:44:54 -07001204 return src
Colin Cross85a88972015-11-23 13:29:51 -08001205 }
1206
Dan Willemsenb1957a52016-06-23 23:44:54 -07001207 ret := src
Colin Cross85a88972015-11-23 13:29:51 -08001208
Dan Willemsenb1957a52016-06-23 23:44:54 -07001209 if src.Kind() == reflect.Struct {
1210 src = src.FieldByName("BlueprintEmbed")
Colin Cross06a931b2015-10-28 17:23:31 -07001211 }
1212
Colin Cross6ee75b62016-05-05 15:57:15 -07001213 order := func(property string,
1214 dstField, srcField reflect.StructField,
1215 dstValue, srcValue interface{}) (proptools.Order, error) {
1216 if proptools.HasTag(dstField, "android", "variant_prepend") {
1217 return proptools.Prepend, nil
1218 } else {
1219 return proptools.Append, nil
1220 }
1221 }
1222
Dan Willemsenb1957a52016-06-23 23:44:54 -07001223 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, order)
Colin Cross06a931b2015-10-28 17:23:31 -07001224 if err != nil {
1225 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
1226 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
1227 } else {
1228 panic(err)
1229 }
1230 }
Colin Cross85a88972015-11-23 13:29:51 -08001231
Dan Willemsenb1957a52016-06-23 23:44:54 -07001232 return ret
Colin Cross06a931b2015-10-28 17:23:31 -07001233}
1234
Colin Crossa195f912019-10-16 11:07:20 -07001235// Rewrite the module's properties structs to contain os-specific values.
1236func (m *ModuleBase) setOSProperties(ctx BottomUpMutatorContext) {
1237 os := m.commonProperties.CompileOS
1238
1239 for i := range m.generalProperties {
1240 genProps := m.generalProperties[i]
1241 if m.archProperties[i] == nil {
1242 continue
1243 }
1244 for _, archProperties := range m.archProperties[i] {
1245 archPropValues := reflect.ValueOf(archProperties).Elem()
1246
Colin Crosscbbd13f2020-01-17 14:08:22 -08001247 targetProp := archPropValues.FieldByName("Target").Elem()
Colin Crossa195f912019-10-16 11:07:20 -07001248
1249 // Handle host-specific properties in the form:
1250 // target: {
1251 // host: {
1252 // key: value,
1253 // },
1254 // },
1255 if os.Class == Host || os.Class == HostCross {
1256 field := "Host"
1257 prefix := "target.host"
1258 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1259 }
1260
1261 // Handle target OS generalities of the form:
1262 // target: {
1263 // bionic: {
1264 // key: value,
1265 // },
1266 // }
1267 if os.Linux() {
1268 field := "Linux"
1269 prefix := "target.linux"
1270 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1271 }
1272
1273 if os.Bionic() {
1274 field := "Bionic"
1275 prefix := "target.bionic"
1276 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1277 }
1278
1279 // Handle target OS properties in the form:
1280 // target: {
1281 // linux_glibc: {
1282 // key: value,
1283 // },
1284 // not_windows: {
1285 // key: value,
1286 // },
1287 // android {
1288 // key: value,
1289 // },
1290 // },
1291 field := os.Field
1292 prefix := "target." + os.Name
1293 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1294
1295 if (os.Class == Host || os.Class == HostCross) && os != Windows {
1296 field := "Not_windows"
1297 prefix := "target.not_windows"
1298 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1299 }
1300
1301 // Handle 64-bit device properties in the form:
1302 // target {
1303 // android64 {
1304 // key: value,
1305 // },
1306 // android32 {
1307 // key: value,
1308 // },
1309 // },
1310 // WARNING: this is probably not what you want to use in your blueprints file, it selects
1311 // options for all targets on a device that supports 64-bit binaries, not just the targets
1312 // that are being compiled for 64-bit. Its expected use case is binaries like linker and
1313 // debuggerd that need to know when they are a 32-bit process running on a 64-bit device
1314 if os.Class == Device {
1315 if ctx.Config().Android64() {
1316 field := "Android64"
1317 prefix := "target.android64"
1318 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1319 } else {
1320 field := "Android32"
1321 prefix := "target.android32"
1322 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1323 }
1324 }
1325 }
1326 }
1327}
1328
Colin Cross3f40fa42015-01-30 17:27:36 -08001329// Rewrite the module's properties structs to contain arch-specific values.
Colin Cross4157e882019-06-06 16:57:04 -07001330func (m *ModuleBase) setArchProperties(ctx BottomUpMutatorContext) {
1331 arch := m.Arch()
1332 os := m.Os()
Colin Crossd3ba0392015-05-07 14:11:29 -07001333
Colin Cross4157e882019-06-06 16:57:04 -07001334 for i := range m.generalProperties {
1335 genProps := m.generalProperties[i]
1336 if m.archProperties[i] == nil {
Dan Willemsenb1957a52016-06-23 23:44:54 -07001337 continue
1338 }
Colin Cross4157e882019-06-06 16:57:04 -07001339 for _, archProperties := range m.archProperties[i] {
Colin Crossc17727d2018-10-24 12:42:09 -07001340 archPropValues := reflect.ValueOf(archProperties).Elem()
Dan Willemsenb1957a52016-06-23 23:44:54 -07001341
Colin Crosscbbd13f2020-01-17 14:08:22 -08001342 archProp := archPropValues.FieldByName("Arch").Elem()
1343 multilibProp := archPropValues.FieldByName("Multilib").Elem()
1344 targetProp := archPropValues.FieldByName("Target").Elem()
Dan Willemsenb1957a52016-06-23 23:44:54 -07001345
Colin Crossc17727d2018-10-24 12:42:09 -07001346 // Handle arch-specific properties in the form:
Colin Crossd5934c82017-10-02 13:55:26 -07001347 // arch: {
Colin Crossc17727d2018-10-24 12:42:09 -07001348 // arm64: {
Colin Crossd5934c82017-10-02 13:55:26 -07001349 // key: value,
1350 // },
1351 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001352 t := arch.ArchType
1353
1354 if arch.ArchType != Common {
1355 field := proptools.FieldNameForProperty(t.Name)
1356 prefix := "arch." + t.Name
Colin Cross4157e882019-06-06 16:57:04 -07001357 archStruct := m.appendProperties(ctx, genProps, archProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001358
1359 // Handle arch-variant-specific properties in the form:
1360 // arch: {
1361 // variant: {
1362 // key: value,
1363 // },
1364 // },
1365 v := variantReplacer.Replace(arch.ArchVariant)
1366 if v != "" {
1367 field := proptools.FieldNameForProperty(v)
1368 prefix := "arch." + t.Name + "." + v
Colin Cross4157e882019-06-06 16:57:04 -07001369 m.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001370 }
1371
1372 // Handle cpu-variant-specific properties in the form:
1373 // arch: {
1374 // variant: {
1375 // key: value,
1376 // },
1377 // },
1378 if arch.CpuVariant != arch.ArchVariant {
1379 c := variantReplacer.Replace(arch.CpuVariant)
1380 if c != "" {
1381 field := proptools.FieldNameForProperty(c)
1382 prefix := "arch." + t.Name + "." + c
Colin Cross4157e882019-06-06 16:57:04 -07001383 m.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001384 }
1385 }
1386
1387 // Handle arch-feature-specific properties in the form:
1388 // arch: {
1389 // feature: {
1390 // key: value,
1391 // },
1392 // },
1393 for _, feature := range arch.ArchFeatures {
1394 field := proptools.FieldNameForProperty(feature)
1395 prefix := "arch." + t.Name + "." + feature
Colin Cross4157e882019-06-06 16:57:04 -07001396 m.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001397 }
1398
1399 // Handle multilib-specific properties in the form:
1400 // multilib: {
1401 // lib32: {
1402 // key: value,
1403 // },
1404 // },
1405 field = proptools.FieldNameForProperty(t.Multilib)
1406 prefix = "multilib." + t.Multilib
Colin Cross4157e882019-06-06 16:57:04 -07001407 m.appendProperties(ctx, genProps, multilibProp, field, prefix)
Colin Cross08016332016-12-20 09:53:14 -08001408 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001409
Colin Crossa195f912019-10-16 11:07:20 -07001410 // Handle combined OS-feature and arch specific properties in the form:
Colin Crossc17727d2018-10-24 12:42:09 -07001411 // target: {
Colin Crossc17727d2018-10-24 12:42:09 -07001412 // bionic_x86: {
1413 // key: value,
1414 // },
1415 // }
Colin Crossa195f912019-10-16 11:07:20 -07001416 if os.Linux() && arch.ArchType != Common {
1417 field := "Linux_" + arch.ArchType.Name
1418 prefix := "target.linux_" + arch.ArchType.Name
Colin Cross4157e882019-06-06 16:57:04 -07001419 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossd5934c82017-10-02 13:55:26 -07001420 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001421
Colin Crossa195f912019-10-16 11:07:20 -07001422 if os.Bionic() && arch.ArchType != Common {
1423 field := "Bionic_" + t.Name
1424 prefix := "target.bionic_" + t.Name
Colin Cross4157e882019-06-06 16:57:04 -07001425 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001426 }
1427
Colin Crossa195f912019-10-16 11:07:20 -07001428 // Handle combined OS and arch specific properties in the form:
Colin Crossc17727d2018-10-24 12:42:09 -07001429 // target: {
Colin Crossc17727d2018-10-24 12:42:09 -07001430 // linux_glibc_x86: {
1431 // key: value,
1432 // },
1433 // linux_glibc_arm: {
1434 // key: value,
1435 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001436 // android_arm {
1437 // key: value,
1438 // },
1439 // android_x86 {
Colin Crossd5934c82017-10-02 13:55:26 -07001440 // key: value,
1441 // },
1442 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001443 if arch.ArchType != Common {
Colin Crossa195f912019-10-16 11:07:20 -07001444 field := os.Field + "_" + t.Name
1445 prefix := "target." + os.Name + "_" + t.Name
Colin Cross4157e882019-06-06 16:57:04 -07001446 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossd5934c82017-10-02 13:55:26 -07001447 }
1448
Colin Crossa195f912019-10-16 11:07:20 -07001449 // Handle arm on x86 properties in the form:
Colin Crossc17727d2018-10-24 12:42:09 -07001450 // target {
Colin Crossa195f912019-10-16 11:07:20 -07001451 // arm_on_x86 {
Colin Crossc17727d2018-10-24 12:42:09 -07001452 // key: value,
1453 // },
Colin Crossa195f912019-10-16 11:07:20 -07001454 // arm_on_x86_64 {
Colin Crossd5934c82017-10-02 13:55:26 -07001455 // key: value,
1456 // },
1457 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001458 if os.Class == Device {
Victor Khimenko1a31f802020-09-17 03:07:31 +02001459 if arch.ArchType == X86 && (hasArmAbi(arch) ||
1460 hasArmAndroidArch(ctx.Config().Targets[Android])) {
Colin Crossc17727d2018-10-24 12:42:09 -07001461 field := "Arm_on_x86"
1462 prefix := "target.arm_on_x86"
Colin Cross4157e882019-06-06 16:57:04 -07001463 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001464 }
Victor Khimenko1a31f802020-09-17 03:07:31 +02001465 if arch.ArchType == X86_64 && (hasArmAbi(arch) ||
1466 hasArmAndroidArch(ctx.Config().Targets[Android])) {
Colin Crossc17727d2018-10-24 12:42:09 -07001467 field := "Arm_on_x86_64"
1468 prefix := "target.arm_on_x86_64"
Colin Cross4157e882019-06-06 16:57:04 -07001469 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001470 }
Victor Khimenkoc26fcf42020-05-07 22:16:33 +02001471 if os == Android && m.Target().NativeBridge == NativeBridgeEnabled {
1472 field := "Native_bridge"
1473 prefix := "target.native_bridge"
1474 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1475 }
Colin Cross4247f0d2017-04-13 16:56:14 -07001476 }
Colin Crossbb2e2b72016-12-08 17:23:53 -08001477 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001478 }
1479}
1480
1481func forEachInterface(v reflect.Value, f func(reflect.Value)) {
1482 switch v.Kind() {
1483 case reflect.Interface:
1484 f(v)
1485 case reflect.Struct:
1486 for i := 0; i < v.NumField(); i++ {
1487 forEachInterface(v.Field(i), f)
1488 }
1489 case reflect.Ptr:
1490 forEachInterface(v.Elem(), f)
1491 default:
1492 panic(fmt.Errorf("Unsupported kind %s", v.Kind()))
1493 }
1494}
Colin Cross4225f652015-09-17 14:33:42 -07001495
Colin Crossa1ad8d12016-06-01 17:09:44 -07001496// Convert the arch product variables into a list of targets for each os class structs
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001497func decodeTargetProductVariables(config *config) (map[OsType][]Target, error) {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001498 variables := config.productVariables
Dan Willemsen490fd492015-11-24 17:53:15 -08001499
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001500 targets := make(map[OsType][]Target)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001501 var targetErr error
1502
dimitry1f33e402019-03-26 12:39:31 +01001503 addTarget := func(os OsType, archName string, archVariant, cpuVariant *string, abi []string,
dimitry8d6dde82019-07-11 10:23:53 +02001504 nativeBridgeEnabled NativeBridgeSupport, nativeBridgeHostArchName *string,
1505 nativeBridgeRelativePath *string) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001506 if targetErr != nil {
1507 return
Dan Willemsen490fd492015-11-24 17:53:15 -08001508 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001509
Dan Willemsen01a3c252019-01-11 19:02:16 -08001510 arch, err := decodeArch(os, archName, archVariant, cpuVariant, abi)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001511 if err != nil {
1512 targetErr = err
1513 return
1514 }
dimitry8d6dde82019-07-11 10:23:53 +02001515 nativeBridgeRelativePathStr := String(nativeBridgeRelativePath)
1516 nativeBridgeHostArchNameStr := String(nativeBridgeHostArchName)
1517
1518 // Use guest arch as relative install path by default
1519 if nativeBridgeEnabled && nativeBridgeRelativePathStr == "" {
1520 nativeBridgeRelativePathStr = arch.ArchType.String()
1521 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001522
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001523 targets[os] = append(targets[os],
Colin Crossa1ad8d12016-06-01 17:09:44 -07001524 Target{
dimitry8d6dde82019-07-11 10:23:53 +02001525 Os: os,
1526 Arch: arch,
1527 NativeBridge: nativeBridgeEnabled,
1528 NativeBridgeHostArchName: nativeBridgeHostArchNameStr,
1529 NativeBridgeRelativePath: nativeBridgeRelativePathStr,
Colin Crossa1ad8d12016-06-01 17:09:44 -07001530 })
Dan Willemsen490fd492015-11-24 17:53:15 -08001531 }
1532
Colin Cross4225f652015-09-17 14:33:42 -07001533 if variables.HostArch == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001534 return nil, fmt.Errorf("No host primary architecture set")
Colin Cross4225f652015-09-17 14:33:42 -07001535 }
1536
dimitry8d6dde82019-07-11 10:23:53 +02001537 addTarget(BuildOs, *variables.HostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001538
Colin Crosseeabb892015-11-20 13:07:51 -08001539 if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" {
dimitry8d6dde82019-07-11 10:23:53 +02001540 addTarget(BuildOs, *variables.HostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001541 }
1542
Colin Crossff3ae9d2018-04-10 16:15:18 -07001543 if Bool(config.Host_bionic) {
dimitry8d6dde82019-07-11 10:23:53 +02001544 addTarget(LinuxBionic, "x86_64", nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen01a405a2016-06-13 17:19:03 -07001545 }
1546
Colin Crossff3ae9d2018-04-10 16:15:18 -07001547 if String(variables.CrossHost) != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001548 crossHostOs := osByName(*variables.CrossHost)
1549 if crossHostOs == NoOsType {
1550 return nil, fmt.Errorf("Unknown cross host OS %q", *variables.CrossHost)
1551 }
1552
Colin Crossff3ae9d2018-04-10 16:15:18 -07001553 if String(variables.CrossHostArch) == "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001554 return nil, fmt.Errorf("No cross-host primary architecture set")
Dan Willemsen490fd492015-11-24 17:53:15 -08001555 }
1556
dimitry8d6dde82019-07-11 10:23:53 +02001557 addTarget(crossHostOs, *variables.CrossHostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001558
1559 if variables.CrossHostSecondaryArch != nil && *variables.CrossHostSecondaryArch != "" {
dimitry8d6dde82019-07-11 10:23:53 +02001560 addTarget(crossHostOs, *variables.CrossHostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001561 }
1562 }
1563
Dan Willemsen3f32f032016-07-11 14:36:48 -07001564 if variables.DeviceArch != nil && *variables.DeviceArch != "" {
Doug Horn21b94272019-01-16 12:06:11 -08001565 var target = Android
1566 if Bool(variables.Fuchsia) {
1567 target = Fuchsia
1568 }
1569
1570 addTarget(target, *variables.DeviceArch, variables.DeviceArchVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001571 variables.DeviceCpuVariant, variables.DeviceAbi, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001572
Dan Willemsen3f32f032016-07-11 14:36:48 -07001573 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" {
1574 addTarget(Android, *variables.DeviceSecondaryArch,
1575 variables.DeviceSecondaryArchVariant, variables.DeviceSecondaryCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001576 variables.DeviceSecondaryAbi, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001577 }
dimitry1f33e402019-03-26 12:39:31 +01001578
1579 if variables.NativeBridgeArch != nil && *variables.NativeBridgeArch != "" {
1580 addTarget(Android, *variables.NativeBridgeArch,
1581 variables.NativeBridgeArchVariant, variables.NativeBridgeCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001582 variables.NativeBridgeAbi, NativeBridgeEnabled, variables.DeviceArch,
1583 variables.NativeBridgeRelativePath)
dimitry1f33e402019-03-26 12:39:31 +01001584 }
1585
1586 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" &&
1587 variables.NativeBridgeSecondaryArch != nil && *variables.NativeBridgeSecondaryArch != "" {
1588 addTarget(Android, *variables.NativeBridgeSecondaryArch,
1589 variables.NativeBridgeSecondaryArchVariant,
1590 variables.NativeBridgeSecondaryCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001591 variables.NativeBridgeSecondaryAbi,
1592 NativeBridgeEnabled,
1593 variables.DeviceSecondaryArch,
1594 variables.NativeBridgeSecondaryRelativePath)
dimitry1f33e402019-03-26 12:39:31 +01001595 }
Colin Cross4225f652015-09-17 14:33:42 -07001596 }
1597
Colin Crossa1ad8d12016-06-01 17:09:44 -07001598 if targetErr != nil {
1599 return nil, targetErr
1600 }
1601
1602 return targets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001603}
1604
Colin Crossbb2e2b72016-12-08 17:23:53 -08001605// hasArmAbi returns true if arch has at least one arm ABI
1606func hasArmAbi(arch Arch) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -08001607 return PrefixInList(arch.Abi, "arm")
Colin Crossbb2e2b72016-12-08 17:23:53 -08001608}
1609
dimitry628db6f2019-05-22 17:16:21 +02001610// hasArmArch returns true if targets has at least non-native_bridge arm Android arch
Colin Cross4247f0d2017-04-13 16:56:14 -07001611func hasArmAndroidArch(targets []Target) bool {
1612 for _, target := range targets {
Victor Khimenko1a31f802020-09-17 03:07:31 +02001613 if target.Os == Android && target.Arch.ArchType == Arm {
Victor Khimenko5eb8ec12018-03-21 20:30:54 +01001614 return true
1615 }
1616 }
1617 return false
1618}
1619
Dan Albert4098deb2016-10-19 14:04:41 -07001620type archConfig struct {
1621 arch string
1622 archVariant string
1623 cpuVariant string
1624 abi []string
1625}
1626
1627func getMegaDeviceConfig() []archConfig {
1628 return []archConfig{
Dan Albert8818f492019-02-19 13:53:01 -08001629 {"arm", "armv7-a", "generic", []string{"armeabi-v7a"}},
Dan Willemsen110a89d2016-01-14 15:17:19 -08001630 {"arm", "armv7-a-neon", "generic", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001631 {"arm", "armv7-a-neon", "cortex-a7", []string{"armeabi-v7a"}},
1632 {"arm", "armv7-a-neon", "cortex-a8", []string{"armeabi-v7a"}},
Dan Willemsen110a89d2016-01-14 15:17:19 -08001633 {"arm", "armv7-a-neon", "cortex-a9", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001634 {"arm", "armv7-a-neon", "cortex-a15", []string{"armeabi-v7a"}},
1635 {"arm", "armv7-a-neon", "cortex-a53", []string{"armeabi-v7a"}},
1636 {"arm", "armv7-a-neon", "cortex-a53.a57", []string{"armeabi-v7a"}},
Richard Fungeb37ed32018-09-24 16:33:45 -07001637 {"arm", "armv7-a-neon", "cortex-a72", []string{"armeabi-v7a"}},
Jake Weinstein6600a442017-05-15 18:27:12 -04001638 {"arm", "armv7-a-neon", "cortex-a73", []string{"armeabi-v7a"}},
Christopher Ferrisba14a8f2018-04-23 18:15:25 -07001639 {"arm", "armv7-a-neon", "cortex-a75", []string{"armeabi-v7a"}},
Haibo Huanga31e2bd2018-10-09 14:27:28 -07001640 {"arm", "armv7-a-neon", "cortex-a76", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001641 {"arm", "armv7-a-neon", "krait", []string{"armeabi-v7a"}},
Alex Naidisae4fc182016-08-20 00:14:56 +02001642 {"arm", "armv7-a-neon", "kryo", []string{"armeabi-v7a"}},
Artem Serovd3072b02018-11-15 15:21:51 +00001643 {"arm", "armv7-a-neon", "kryo385", []string{"armeabi-v7a"}},
Junmo Park8ea49592017-07-24 07:14:55 +09001644 {"arm", "armv7-a-neon", "exynos-m1", []string{"armeabi-v7a"}},
Junmo Parkd86c9022017-07-21 09:07:47 +09001645 {"arm", "armv7-a-neon", "exynos-m2", []string{"armeabi-v7a"}},
Dan Willemsen110a89d2016-01-14 15:17:19 -08001646 {"arm64", "armv8-a", "cortex-a53", []string{"arm64-v8a"}},
Richard Fungeb37ed32018-09-24 16:33:45 -07001647 {"arm64", "armv8-a", "cortex-a72", []string{"arm64-v8a"}},
Jake Weinstein6600a442017-05-15 18:27:12 -04001648 {"arm64", "armv8-a", "cortex-a73", []string{"arm64-v8a"}},
Alex Naidisac01ff52016-08-30 15:56:33 +02001649 {"arm64", "armv8-a", "kryo", []string{"arm64-v8a"}},
Junmo Park8ea49592017-07-24 07:14:55 +09001650 {"arm64", "armv8-a", "exynos-m1", []string{"arm64-v8a"}},
Junmo Parkd86c9022017-07-21 09:07:47 +09001651 {"arm64", "armv8-a", "exynos-m2", []string{"arm64-v8a"}},
Artem Serovd3072b02018-11-15 15:21:51 +00001652 {"arm64", "armv8-2a", "kryo385", []string{"arm64-v8a"}},
Raphael Gault70b96b02020-06-18 09:56:53 +00001653 {"arm64", "armv8-2a-dotprod", "cortex-a55", []string{"arm64-v8a"}},
1654 {"arm64", "armv8-2a-dotprod", "cortex-a75", []string{"arm64-v8a"}},
1655 {"arm64", "armv8-2a-dotprod", "cortex-a76", []string{"arm64-v8a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001656 {"x86", "", "", []string{"x86"}},
1657 {"x86", "atom", "", []string{"x86"}},
1658 {"x86", "haswell", "", []string{"x86"}},
1659 {"x86", "ivybridge", "", []string{"x86"}},
1660 {"x86", "sandybridge", "", []string{"x86"}},
1661 {"x86", "silvermont", "", []string{"x86"}},
Benjamin Gordon87e7f2f2019-02-14 10:59:48 -07001662 {"x86", "stoneyridge", "", []string{"x86"}},
Dan Willemsen8a354052016-05-10 14:30:51 -07001663 {"x86", "x86_64", "", []string{"x86"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001664 {"x86_64", "", "", []string{"x86_64"}},
1665 {"x86_64", "haswell", "", []string{"x86_64"}},
1666 {"x86_64", "ivybridge", "", []string{"x86_64"}},
1667 {"x86_64", "sandybridge", "", []string{"x86_64"}},
1668 {"x86_64", "silvermont", "", []string{"x86_64"}},
Benjamin Gordon87e7f2f2019-02-14 10:59:48 -07001669 {"x86_64", "stoneyridge", "", []string{"x86_64"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001670 }
Dan Albert4098deb2016-10-19 14:04:41 -07001671}
Dan Willemsen322acaf2016-01-12 23:07:05 -08001672
Dan Albert4098deb2016-10-19 14:04:41 -07001673func getNdkAbisConfig() []archConfig {
1674 return []archConfig{
Dan Albert6bba6442020-01-30 15:16:49 -08001675 {"arm", "armv7-a", "", []string{"armeabi-v7a"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001676 {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001677 {"x86", "", "", []string{"x86"}},
1678 {"x86_64", "", "", []string{"x86_64"}},
1679 }
1680}
1681
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001682func getAmlAbisConfig() []archConfig {
1683 return []archConfig{
1684 {"arm", "armv7-a", "", []string{"armeabi-v7a"}},
1685 {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
1686 {"x86", "", "", []string{"x86"}},
1687 {"x86_64", "", "", []string{"x86_64"}},
1688 }
1689}
1690
Dan Willemsen01a3c252019-01-11 19:02:16 -08001691func decodeArchSettings(os OsType, archConfigs []archConfig) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001692 var ret []Target
Dan Willemsen322acaf2016-01-12 23:07:05 -08001693
Dan Albert4098deb2016-10-19 14:04:41 -07001694 for _, config := range archConfigs {
Dan Willemsen01a3c252019-01-11 19:02:16 -08001695 arch, err := decodeArch(os, config.arch, &config.archVariant,
Colin Crossa74ca042019-01-31 14:31:51 -08001696 &config.cpuVariant, config.abi)
Dan Willemsen322acaf2016-01-12 23:07:05 -08001697 if err != nil {
1698 return nil, err
1699 }
Colin Cross3b19f5d2019-09-17 14:45:31 -07001700
Colin Crossa1ad8d12016-06-01 17:09:44 -07001701 ret = append(ret, Target{
1702 Os: Android,
1703 Arch: arch,
1704 })
Dan Willemsen322acaf2016-01-12 23:07:05 -08001705 }
1706
1707 return ret, nil
1708}
1709
Colin Cross4225f652015-09-17 14:33:42 -07001710// Convert a set of strings from product variables into a single Arch struct
Colin Crossa74ca042019-01-31 14:31:51 -08001711func decodeArch(os OsType, arch string, archVariant, cpuVariant *string, abi []string) (Arch, error) {
Colin Cross4225f652015-09-17 14:33:42 -07001712 stringPtr := func(p *string) string {
1713 if p != nil {
1714 return *p
1715 }
1716 return ""
1717 }
1718
Colin Crosseeabb892015-11-20 13:07:51 -08001719 archType, ok := archTypeMap[arch]
1720 if !ok {
1721 return Arch{}, fmt.Errorf("unknown arch %q", arch)
1722 }
Colin Cross4225f652015-09-17 14:33:42 -07001723
Colin Crosseeabb892015-11-20 13:07:51 -08001724 a := Arch{
Colin Cross4225f652015-09-17 14:33:42 -07001725 ArchType: archType,
1726 ArchVariant: stringPtr(archVariant),
1727 CpuVariant: stringPtr(cpuVariant),
Colin Crossa74ca042019-01-31 14:31:51 -08001728 Abi: abi,
Colin Crosseeabb892015-11-20 13:07:51 -08001729 }
1730
1731 if a.ArchVariant == a.ArchType.Name || a.ArchVariant == "generic" {
1732 a.ArchVariant = ""
1733 }
1734
1735 if a.CpuVariant == a.ArchType.Name || a.CpuVariant == "generic" {
1736 a.CpuVariant = ""
1737 }
1738
1739 for i := 0; i < len(a.Abi); i++ {
1740 if a.Abi[i] == "" {
1741 a.Abi = append(a.Abi[:i], a.Abi[i+1:]...)
1742 i--
1743 }
1744 }
1745
Dan Willemsen01a3c252019-01-11 19:02:16 -08001746 if a.ArchVariant == "" {
1747 if featureMap, ok := defaultArchFeatureMap[os]; ok {
1748 a.ArchFeatures = featureMap[archType]
1749 }
1750 } else {
1751 if featureMap, ok := archFeatureMap[archType]; ok {
1752 a.ArchFeatures = featureMap[a.ArchVariant]
1753 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001754 }
1755
Colin Crosseeabb892015-11-20 13:07:51 -08001756 return a, nil
Colin Cross4225f652015-09-17 14:33:42 -07001757}
1758
Colin Cross69617d32016-09-06 10:39:07 -07001759func filterMultilibTargets(targets []Target, multilib string) []Target {
1760 var ret []Target
1761 for _, t := range targets {
1762 if t.Arch.ArchType.Multilib == multilib {
1763 ret = append(ret, t)
1764 }
1765 }
1766 return ret
1767}
1768
Paul Duffinca7f0ef2020-02-25 15:50:49 +00001769// Return the set of Os specific common architecture targets for each Os in a list of
1770// targets.
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001771func getCommonTargets(targets []Target) []Target {
1772 var ret []Target
1773 set := make(map[string]bool)
1774
1775 for _, t := range targets {
1776 if _, found := set[t.Os.String()]; !found {
1777 set[t.Os.String()] = true
1778 ret = append(ret, commonTargetMap[t.Os.String()])
1779 }
1780 }
1781
1782 return ret
1783}
1784
Colin Cross3dceee32018-09-06 10:19:57 -07001785func firstTarget(targets []Target, filters ...string) []Target {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001786 for _, filter := range filters {
1787 buildTargets := filterMultilibTargets(targets, filter)
1788 if len(buildTargets) > 0 {
Colin Cross3dceee32018-09-06 10:19:57 -07001789 return buildTargets[:1]
Colin Cross6b4a32d2017-12-05 13:42:45 -08001790 }
1791 }
1792 return nil
1793}
1794
Colin Crossa1ad8d12016-06-01 17:09:44 -07001795// Use the module multilib setting to select one or more targets from a target list
Colin Crossee0bc3b2018-10-02 22:01:37 -07001796func decodeMultilibTargets(multilib string, targets []Target, prefer32 bool) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001797 buildTargets := []Target{}
Colin Cross6b4a32d2017-12-05 13:42:45 -08001798
Colin Cross4225f652015-09-17 14:33:42 -07001799 switch multilib {
1800 case "common":
Colin Cross6b4a32d2017-12-05 13:42:45 -08001801 buildTargets = getCommonTargets(targets)
1802 case "common_first":
1803 buildTargets = getCommonTargets(targets)
1804 if prefer32 {
Colin Cross3dceee32018-09-06 10:19:57 -07001805 buildTargets = append(buildTargets, firstTarget(targets, "lib32", "lib64")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001806 } else {
Colin Cross3dceee32018-09-06 10:19:57 -07001807 buildTargets = append(buildTargets, firstTarget(targets, "lib64", "lib32")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001808 }
Colin Cross4225f652015-09-17 14:33:42 -07001809 case "both":
Colin Cross8b74d172016-09-13 09:59:14 -07001810 if prefer32 {
1811 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1812 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1813 } else {
1814 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1815 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1816 }
Colin Cross4225f652015-09-17 14:33:42 -07001817 case "32":
Colin Cross69617d32016-09-06 10:39:07 -07001818 buildTargets = filterMultilibTargets(targets, "lib32")
Colin Cross4225f652015-09-17 14:33:42 -07001819 case "64":
Colin Cross69617d32016-09-06 10:39:07 -07001820 buildTargets = filterMultilibTargets(targets, "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001821 case "first":
1822 if prefer32 {
Colin Cross3dceee32018-09-06 10:19:57 -07001823 buildTargets = firstTarget(targets, "lib32", "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001824 } else {
Colin Cross3dceee32018-09-06 10:19:57 -07001825 buildTargets = firstTarget(targets, "lib64", "lib32")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001826 }
Colin Cross69617d32016-09-06 10:39:07 -07001827 case "prefer32":
Colin Cross3dceee32018-09-06 10:19:57 -07001828 buildTargets = filterMultilibTargets(targets, "lib32")
1829 if len(buildTargets) == 0 {
1830 buildTargets = filterMultilibTargets(targets, "lib64")
1831 }
Colin Cross4225f652015-09-17 14:33:42 -07001832 default:
Colin Cross69617d32016-09-06 10:39:07 -07001833 return nil, fmt.Errorf(`compile_multilib must be "both", "first", "32", "64", or "prefer32" found %q`,
Colin Cross4225f652015-09-17 14:33:42 -07001834 multilib)
Colin Cross4225f652015-09-17 14:33:42 -07001835 }
1836
Colin Crossa1ad8d12016-06-01 17:09:44 -07001837 return buildTargets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001838}