blob: 16211f8531b345ad68f58b1a84a9f37beff70d8a [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
Colin Cross62a0cfd2020-11-10 18:12:15 -0800601 // CommonArch is the Arch for all modules that are os-specific but not arch specific,
602 // for example most Java modules.
603 CommonArch = Arch{ArchType: Common}
604
Dan Willemsenb1957a52016-06-23 23:44:54 -0700605 osArchTypeMap = map[OsType][]ArchType{
Dan Willemsen00fcbde2016-11-17 00:25:59 -0800606 Linux: []ArchType{X86, X86_64},
Jiyong Park4afa2e22020-07-13 15:43:45 +0900607 LinuxBionic: []ArchType{Arm64, X86_64},
Dan Willemsene97e68a2018-08-28 17:12:56 -0700608 Darwin: []ArchType{X86_64},
Dan Willemsen00fcbde2016-11-17 00:25:59 -0800609 Windows: []ArchType{X86, X86_64},
Elliott Hughesda3a0712020-03-06 16:55:28 -0800610 Android: []ArchType{Arm, Arm64, X86, X86_64},
Doug Horn21b94272019-01-16 12:06:11 -0800611 Fuchsia: []ArchType{Arm64, X86_64},
Dan Willemsenb1957a52016-06-23 23:44:54 -0700612 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700613)
614
615type OsType struct {
616 Name, Field string
617 Class OsClass
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800618
619 DefaultDisabled bool
Dan Willemsen490fd492015-11-24 17:53:15 -0800620}
621
Colin Crossa1ad8d12016-06-01 17:09:44 -0700622type OsClass int
623
624const (
Dan Willemsen0e2d97b2016-11-28 17:50:06 -0800625 Generic OsClass = iota
626 Device
Colin Crossa1ad8d12016-06-01 17:09:44 -0700627 Host
Colin Crossa1ad8d12016-06-01 17:09:44 -0700628)
629
Colin Cross67a5c132017-05-09 13:45:28 -0700630func (class OsClass) String() string {
631 switch class {
632 case Generic:
633 return "generic"
634 case Device:
635 return "device"
636 case Host:
637 return "host"
Colin Cross67a5c132017-05-09 13:45:28 -0700638 default:
639 panic(fmt.Errorf("unknown class %d", class))
640 }
641}
642
Colin Crossa1ad8d12016-06-01 17:09:44 -0700643func (os OsType) String() string {
644 return os.Name
Colin Cross54c71122016-06-01 17:09:44 -0700645}
646
Dan Willemsen866b5632017-09-22 12:28:24 -0700647func (os OsType) Bionic() bool {
648 return os == Android || os == LinuxBionic
649}
650
651func (os OsType) Linux() bool {
652 return os == Android || os == Linux || os == LinuxBionic
653}
654
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800655func NewOsType(name string, class OsClass, defDisabled bool) OsType {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700656 os := OsType{
657 Name: name,
658 Field: strings.Title(name),
659 Class: class,
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800660
661 DefaultDisabled: defDisabled,
Colin Cross54c71122016-06-01 17:09:44 -0700662 }
Paul Duffina04c1072020-03-02 10:16:35 +0000663 OsTypeList = append(OsTypeList, os)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800664
665 if _, found := commonTargetMap[name]; found {
666 panic(fmt.Errorf("Found Os type duplicate during OsType registration: %q", name))
667 } else {
Colin Cross62a0cfd2020-11-10 18:12:15 -0800668 commonTargetMap[name] = Target{Os: os, Arch: CommonArch}
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800669 }
670
Colin Crossa1ad8d12016-06-01 17:09:44 -0700671 return os
672}
673
674func osByName(name string) OsType {
Paul Duffina04c1072020-03-02 10:16:35 +0000675 for _, os := range OsTypeList {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700676 if os.Name == name {
677 return os
678 }
679 }
680
681 return NoOsType
Dan Willemsen490fd492015-11-24 17:53:15 -0800682}
683
dimitry1f33e402019-03-26 12:39:31 +0100684type NativeBridgeSupport bool
685
686const (
687 NativeBridgeDisabled NativeBridgeSupport = false
688 NativeBridgeEnabled NativeBridgeSupport = true
689)
690
Colin Crossa1ad8d12016-06-01 17:09:44 -0700691type Target struct {
dimitry8d6dde82019-07-11 10:23:53 +0200692 Os OsType
693 Arch Arch
694 NativeBridge NativeBridgeSupport
695 NativeBridgeHostArchName string
696 NativeBridgeRelativePath string
Jiyong Park1613e552020-09-14 19:43:17 +0900697
698 // HostCross is true when the target cannot run natively on the current build host.
699 // For example, linux_glibc_x86 returns true on a regular x86/i686/Linux machines, but returns false
700 // on Mac (different OS), or on 64-bit only i686/Linux machines (unsupported arch).
701 HostCross bool
Colin Crossd3ba0392015-05-07 14:11:29 -0700702}
703
Colin Crossa1ad8d12016-06-01 17:09:44 -0700704func (target Target) String() string {
Colin Crossa195f912019-10-16 11:07:20 -0700705 return target.OsVariation() + "_" + target.ArchVariation()
706}
707
708func (target Target) OsVariation() string {
709 return target.Os.String()
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700710}
711
712func (target Target) ArchVariation() string {
713 var variation string
dimitry1f33e402019-03-26 12:39:31 +0100714 if target.NativeBridge {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700715 variation = "native_bridge_"
dimitry1f33e402019-03-26 12:39:31 +0100716 }
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700717 variation += target.Arch.String()
718
Colin Crossa195f912019-10-16 11:07:20 -0700719 return variation
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700720}
721
722func (target Target) Variations() []blueprint.Variation {
723 return []blueprint.Variation{
Colin Crossa195f912019-10-16 11:07:20 -0700724 {Mutator: "os", Variation: target.OsVariation()},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700725 {Mutator: "arch", Variation: target.ArchVariation()},
726 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800727}
728
Colin Cross617b88a2020-08-24 18:04:09 -0700729func osMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Crossa195f912019-10-16 11:07:20 -0700730 var module Module
731 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700732 if module, ok = bpctx.Module().(Module); !ok {
733 if bootstrap.IsBootstrapModule(bpctx.Module()) {
734 // Bootstrap Go modules are always the build OS or linux bionic.
735 config := bpctx.Config().(Config)
736 osNames := []string{config.BuildOSTarget.OsVariation()}
737 for _, hostCrossTarget := range config.Targets[LinuxBionic] {
738 if hostCrossTarget.Arch.ArchType == config.BuildOSTarget.Arch.ArchType {
739 osNames = append(osNames, hostCrossTarget.OsVariation())
740 }
741 }
742 osNames = FirstUniqueStrings(osNames)
743 bpctx.CreateVariations(osNames...)
744 }
Colin Crossa195f912019-10-16 11:07:20 -0700745 return
746 }
747
Colin Cross617b88a2020-08-24 18:04:09 -0700748 // Bootstrap Go module support above requires this mutator to be a
749 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
750 // filters out non-Soong modules. Now that we've handled them, create a
751 // normal android.BottomUpMutatorContext.
752 mctx := bottomUpMutatorContextFactory(bpctx, module, false)
753
Colin Crossa195f912019-10-16 11:07:20 -0700754 base := module.base()
755
756 if !base.ArchSpecific() {
757 return
758 }
759
Colin Crossa195f912019-10-16 11:07:20 -0700760 var moduleOSList []OsType
761
Paul Duffina04c1072020-03-02 10:16:35 +0000762 for _, os := range OsTypeList {
Jiyong Park1613e552020-09-14 19:43:17 +0900763 for _, t := range mctx.Config().Targets[os] {
764 if base.supportsTarget(t, mctx.Config()) {
765 moduleOSList = append(moduleOSList, os)
766 break
Colin Crossa195f912019-10-16 11:07:20 -0700767 }
768 }
Colin Crossa195f912019-10-16 11:07:20 -0700769 }
770
771 if len(moduleOSList) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900772 base.Disable()
Colin Crossa195f912019-10-16 11:07:20 -0700773 return
774 }
775
776 osNames := make([]string, len(moduleOSList))
777
778 for i, os := range moduleOSList {
779 osNames[i] = os.String()
780 }
781
Paul Duffin1356d8c2020-02-25 19:26:33 +0000782 createCommonOSVariant := base.commonProperties.CreateCommonOSVariant
783 if createCommonOSVariant {
784 // A CommonOS variant was requested so add it to the list of OS's variants to
785 // create. It needs to be added to the end because it needs to depend on the
786 // the other variants in the list returned by CreateVariations(...) and inter
787 // variant dependencies can only be created from a later variant in that list to
788 // an earlier one. That is because variants are always processed in the order in
789 // which they are returned from CreateVariations(...).
790 osNames = append(osNames, CommonOS.Name)
791 moduleOSList = append(moduleOSList, CommonOS)
Colin Crossa195f912019-10-16 11:07:20 -0700792 }
793
Paul Duffin1356d8c2020-02-25 19:26:33 +0000794 modules := mctx.CreateVariations(osNames...)
795 for i, m := range modules {
796 m.base().commonProperties.CompileOS = moduleOSList[i]
797 m.base().setOSProperties(mctx)
798 }
799
800 if createCommonOSVariant {
801 // A CommonOS variant was requested so add dependencies from it (the last one in
802 // the list) to the OS type specific variants.
803 last := len(modules) - 1
804 commonOSVariant := modules[last]
805 commonOSVariant.base().commonProperties.CommonOSVariant = true
806 for _, module := range modules[0:last] {
807 // Ignore modules that are enabled. Note, this will only avoid adding
808 // dependencies on OsType variants that are explicitly disabled in their
809 // properties. The CommonOS variant will still depend on disabled variants
810 // if they are disabled afterwards, e.g. in archMutator if
811 if module.Enabled() {
812 mctx.AddInterVariantDependency(commonOsToOsSpecificVariantTag, commonOSVariant, module)
813 }
814 }
815 }
816}
817
Colin Crossc179ea62020-10-09 10:54:15 -0700818type archDepTag struct {
819 blueprint.BaseDependencyTag
820 name string
821}
Paul Duffin1356d8c2020-02-25 19:26:33 +0000822
Colin Crossc179ea62020-10-09 10:54:15 -0700823// Identifies the dependency from CommonOS variant to the os specific variants.
824var commonOsToOsSpecificVariantTag = archDepTag{name: "common os to os specific"}
825
Paul Duffin1356d8c2020-02-25 19:26:33 +0000826// Get the OsType specific variants for the current CommonOS variant.
827//
828// The returned list will only contain enabled OsType specific variants of the
829// module referenced in the supplied context. An empty list is returned if there
830// are no enabled variants or the supplied context is not for an CommonOS
831// variant.
832func GetOsSpecificVariantsOfCommonOSVariant(mctx BaseModuleContext) []Module {
833 var variants []Module
834 mctx.VisitDirectDeps(func(m Module) {
835 if mctx.OtherModuleDependencyTag(m) == commonOsToOsSpecificVariantTag {
836 if m.Enabled() {
837 variants = append(variants, m)
838 }
839 }
840 })
Paul Duffin1356d8c2020-02-25 19:26:33 +0000841 return variants
Colin Crossa195f912019-10-16 11:07:20 -0700842}
843
Colin Crossee0bc3b2018-10-02 22:01:37 -0700844// archMutator splits a module into a variant for each Target requested by the module. Target selection
845// for a module is in three levels, OsClass, mulitlib, and then Target.
846// OsClass selection is determined by:
847// - The HostOrDeviceSupported value passed in to InitAndroidArchModule by the module type factory, which selects
848// whether the module type can compile for host, device or both.
849// - The host_supported and device_supported properties on the module.
Roland Levillainf5b635d2019-06-05 14:42:57 +0100850// 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 -0700851// for the module, the Device OsClass is selected.
852// Within each selected OsClass, the multilib selection is determined by:
Jaewoong Jung02b2d4d2019-06-06 15:19:57 -0700853// - The compile_multilib property if it set (which may be overridden by target.android.compile_multilib or
Colin Crossee0bc3b2018-10-02 22:01:37 -0700854// target.host.compile_multilib).
855// - The default multilib passed to InitAndroidArchModule if compile_multilib was not set.
856// Valid multilib values include:
857// "both": compile for all Targets supported by the OsClass (generally x86_64 and x86, or arm64 and arm).
858// "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 -0700859// but may be arm for a 32-bit only build.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700860// "32": compile for only a single 32-bit Target supported by the OsClass.
861// "64": compile for only a single 64-bit Target supported by the OsClass.
862// "common": compile a for a single Target that will work on all Targets suported by the OsClass (for example Java).
863//
864// Once the list of Targets is determined, the module is split into a variant for each Target.
865//
866// Modules can be initialized with InitAndroidMultiTargetsArchModule, in which case they will be split by OsClass,
867// 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 -0700868func archMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700869 var module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800870 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700871 if module, ok = bpctx.Module().(Module); !ok {
872 if bootstrap.IsBootstrapModule(bpctx.Module()) {
873 // Bootstrap Go modules are always the build architecture.
874 bpctx.CreateVariations(bpctx.Config().(Config).BuildOSTarget.ArchVariation())
875 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800876 return
877 }
878
Colin Cross617b88a2020-08-24 18:04:09 -0700879 // Bootstrap Go module support above requires this mutator to be a
880 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
881 // filters out non-Soong modules. Now that we've handled them, create a
882 // normal android.BottomUpMutatorContext.
883 mctx := bottomUpMutatorContextFactory(bpctx, module, false)
884
Colin Cross5eca7cb2018-10-02 14:02:10 -0700885 base := module.base()
886
887 if !base.ArchSpecific() {
Colin Crossb9db4802016-06-03 01:50:47 +0000888 return
889 }
890
Colin Crossa195f912019-10-16 11:07:20 -0700891 os := base.commonProperties.CompileOS
Paul Duffin1356d8c2020-02-25 19:26:33 +0000892 if os == CommonOS {
893 // Make sure that the target related properties are initialized for the
894 // CommonOS variant.
895 addTargetProperties(module, commonTargetMap[os.Name], nil, true)
896
897 // Do not create arch specific variants for the CommonOS variant.
898 return
899 }
900
Colin Crossa195f912019-10-16 11:07:20 -0700901 osTargets := mctx.Config().Targets[os]
Colin Crossfb0c16e2019-11-20 17:12:35 -0800902 image := base.commonProperties.ImageVariation
Colin Crossa195f912019-10-16 11:07:20 -0700903 // Filter NativeBridge targets unless they are explicitly supported
Colin Cross83bead42019-12-18 10:45:46 -0800904 // Skip creating native bridge variants for vendor modules
905 if os == Android &&
906 !(Bool(base.commonProperties.Native_bridge_supported) && image == CoreVariation) {
907
Colin Crossa195f912019-10-16 11:07:20 -0700908 var targets []Target
909 for _, t := range osTargets {
910 if !t.NativeBridge {
911 targets = append(targets, t)
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700912 }
913 }
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700914
Colin Crossa195f912019-10-16 11:07:20 -0700915 osTargets = targets
916 }
Colin Crossee0bc3b2018-10-02 22:01:37 -0700917
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700918 // only the primary arch in the ramdisk / vendor_ramdisk / recovery partition
919 if os == Android && (module.InstallInRecovery() || module.InstallInRamdisk() || module.InstallInVendorRamdisk()) {
Colin Crossa195f912019-10-16 11:07:20 -0700920 osTargets = []Target{osTargets[0]}
921 }
dimitry1f33e402019-03-26 12:39:31 +0100922
Colin Crossa195f912019-10-16 11:07:20 -0700923 prefer32 := false
924 if base.prefer32 != nil {
Jiyong Park1613e552020-09-14 19:43:17 +0900925 prefer32 = base.prefer32(mctx, base, os)
Colin Crossa195f912019-10-16 11:07:20 -0700926 }
dimitry1f33e402019-03-26 12:39:31 +0100927
Colin Crossa195f912019-10-16 11:07:20 -0700928 multilib, extraMultilib := decodeMultilib(base, os.Class)
929 targets, err := decodeMultilibTargets(multilib, osTargets, prefer32)
930 if err != nil {
931 mctx.ModuleErrorf("%s", err.Error())
932 }
Colin Cross5eca7cb2018-10-02 14:02:10 -0700933
Colin Crossa195f912019-10-16 11:07:20 -0700934 var multiTargets []Target
935 if extraMultilib != "" {
936 multiTargets, err = decodeMultilibTargets(extraMultilib, osTargets, prefer32)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700937 if err != nil {
938 mctx.ModuleErrorf("%s", err.Error())
939 }
Colin Crossb9db4802016-06-03 01:50:47 +0000940 }
941
Colin Crossfb0c16e2019-11-20 17:12:35 -0800942 if image == RecoveryVariation {
943 primaryArch := mctx.Config().DevicePrimaryArchType()
944 targets = filterToArch(targets, primaryArch)
945 multiTargets = filterToArch(multiTargets, primaryArch)
946 }
947
Colin Crossa195f912019-10-16 11:07:20 -0700948 if len(targets) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900949 base.Disable()
Dan Willemsen3f32f032016-07-11 14:36:48 -0700950 return
951 }
952
Colin Crossa195f912019-10-16 11:07:20 -0700953 targetNames := make([]string, len(targets))
Colin Crossb9db4802016-06-03 01:50:47 +0000954
Colin Crossa195f912019-10-16 11:07:20 -0700955 for i, target := range targets {
956 targetNames[i] = target.ArchVariation()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700957 }
958
959 modules := mctx.CreateVariations(targetNames...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800960 for i, m := range modules {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000961 addTargetProperties(m, targets[i], multiTargets, i == 0)
Colin Cross617b88a2020-08-24 18:04:09 -0700962 m.base().setArchProperties(mctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800963 }
964}
965
Paul Duffin1356d8c2020-02-25 19:26:33 +0000966func addTargetProperties(m Module, target Target, multiTargets []Target, primaryTarget bool) {
967 m.base().commonProperties.CompileTarget = target
968 m.base().commonProperties.CompileMultiTargets = multiTargets
969 m.base().commonProperties.CompilePrimary = primaryTarget
970}
971
Colin Crossee0bc3b2018-10-02 22:01:37 -0700972func decodeMultilib(base *ModuleBase, class OsClass) (multilib, extraMultilib string) {
973 switch class {
974 case Device:
975 multilib = String(base.commonProperties.Target.Android.Compile_multilib)
Jiyong Park1613e552020-09-14 19:43:17 +0900976 case Host:
Colin Crossee0bc3b2018-10-02 22:01:37 -0700977 multilib = String(base.commonProperties.Target.Host.Compile_multilib)
978 }
979 if multilib == "" {
980 multilib = String(base.commonProperties.Compile_multilib)
981 }
982 if multilib == "" {
983 multilib = base.commonProperties.Default_multilib
984 }
985
986 if base.commonProperties.UseTargetVariants {
987 return multilib, ""
988 } else {
989 // For app modules a single arch variant will be created per OS class which is expected to handle all the
990 // selected arches. Return the common-type as multilib and any Android.bp provided multilib as extraMultilib
991 if multilib == base.commonProperties.Default_multilib {
992 multilib = "first"
993 }
994 return base.commonProperties.Default_multilib, multilib
995 }
996}
997
Colin Crossfb0c16e2019-11-20 17:12:35 -0800998func filterToArch(targets []Target, arch ArchType) []Target {
999 for i := 0; i < len(targets); i++ {
1000 if targets[i].Arch.ArchType != arch {
1001 targets = append(targets[:i], targets[i+1:]...)
1002 i--
1003 }
1004 }
1005 return targets
1006}
1007
Colin Crosscbbd13f2020-01-17 14:08:22 -08001008type archPropTypeDesc struct {
1009 arch, multilib, target reflect.Type
1010}
1011
1012type archPropRoot struct {
1013 Arch, Multilib, Target interface{}
1014}
1015
1016// createArchPropTypeDesc takes a reflect.Type that is either a struct or a pointer to a struct, and
1017// returns lists of reflect.Types that contains the arch-variant properties inside structs for each
1018// arch, multilib and target property.
1019func createArchPropTypeDesc(props reflect.Type) []archPropTypeDesc {
Colin Crossb1d8c992020-01-21 11:43:29 -08001020 // Each property struct shard will be nested many times under the runtime generated arch struct,
1021 // which can hit the limit of 64kB for the name of runtime generated structs. They are nested
1022 // 97 times now, which may grow in the future, plus there is some overhead for the containing
1023 // type. This number may need to be reduced if too many are added, but reducing it too far
1024 // could cause problems if a single deeply nested property no longer fits in the name.
1025 const maxArchTypeNameSize = 500
1026
1027 propShards, _ := proptools.FilterPropertyStructSharded(props, maxArchTypeNameSize, filterArchStruct)
Colin Crosscb988072019-01-24 14:58:11 -08001028 if len(propShards) == 0 {
Dan Willemsenb1957a52016-06-23 23:44:54 -07001029 return nil
1030 }
1031
Colin Crosscbbd13f2020-01-17 14:08:22 -08001032 var ret []archPropTypeDesc
Colin Crossc17727d2018-10-24 12:42:09 -07001033 for _, props := range propShards {
Dan Willemsenb1957a52016-06-23 23:44:54 -07001034
Colin Crossc17727d2018-10-24 12:42:09 -07001035 variantFields := func(names []string) []reflect.StructField {
1036 ret := make([]reflect.StructField, len(names))
Dan Willemsenb1957a52016-06-23 23:44:54 -07001037
Colin Crossc17727d2018-10-24 12:42:09 -07001038 for i, name := range names {
1039 ret[i].Name = name
1040 ret[i].Type = props
Dan Willemsen866b5632017-09-22 12:28:24 -07001041 }
Colin Crossc17727d2018-10-24 12:42:09 -07001042
1043 return ret
1044 }
1045
1046 archFields := make([]reflect.StructField, len(archTypeList))
1047 for i, arch := range archTypeList {
1048 variants := []string{}
1049
1050 for _, archVariant := range archVariants[arch] {
1051 archVariant := variantReplacer.Replace(archVariant)
1052 variants = append(variants, proptools.FieldNameForProperty(archVariant))
1053 }
1054 for _, feature := range archFeatures[arch] {
1055 feature := variantReplacer.Replace(feature)
1056 variants = append(variants, proptools.FieldNameForProperty(feature))
1057 }
1058
1059 fields := variantFields(variants)
1060
1061 fields = append([]reflect.StructField{{
1062 Name: "BlueprintEmbed",
1063 Type: props,
1064 Anonymous: true,
1065 }}, fields...)
1066
1067 archFields[i] = reflect.StructField{
1068 Name: arch.Field,
1069 Type: reflect.StructOf(fields),
1070 }
1071 }
1072 archType := reflect.StructOf(archFields)
1073
1074 multilibType := reflect.StructOf(variantFields([]string{"Lib32", "Lib64"}))
1075
1076 targets := []string{
1077 "Host",
1078 "Android64",
1079 "Android32",
1080 "Bionic",
1081 "Linux",
1082 "Not_windows",
1083 "Arm_on_x86",
1084 "Arm_on_x86_64",
Victor Khimenkoc26fcf42020-05-07 22:16:33 +02001085 "Native_bridge",
Colin Crossc17727d2018-10-24 12:42:09 -07001086 }
Paul Duffina04c1072020-03-02 10:16:35 +00001087 for _, os := range OsTypeList {
Colin Crossc17727d2018-10-24 12:42:09 -07001088 targets = append(targets, os.Field)
1089
1090 for _, archType := range osArchTypeMap[os] {
1091 targets = append(targets, os.Field+"_"+archType.Name)
1092
1093 if os.Linux() {
1094 target := "Linux_" + archType.Name
1095 if !InList(target, targets) {
1096 targets = append(targets, target)
1097 }
1098 }
1099 if os.Bionic() {
1100 target := "Bionic_" + archType.Name
1101 if !InList(target, targets) {
1102 targets = append(targets, target)
1103 }
Dan Willemsen866b5632017-09-22 12:28:24 -07001104 }
1105 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001106 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001107
Colin Crossc17727d2018-10-24 12:42:09 -07001108 targetType := reflect.StructOf(variantFields(targets))
Colin Crosscbbd13f2020-01-17 14:08:22 -08001109
1110 ret = append(ret, archPropTypeDesc{
1111 arch: reflect.PtrTo(archType),
1112 multilib: reflect.PtrTo(multilibType),
1113 target: reflect.PtrTo(targetType),
1114 })
Colin Crossc17727d2018-10-24 12:42:09 -07001115 }
1116 return ret
Dan Willemsenb1957a52016-06-23 23:44:54 -07001117}
1118
Colin Cross74449102019-09-25 11:26:40 -07001119func filterArchStruct(field reflect.StructField, prefix string) (bool, reflect.StructField) {
1120 if proptools.HasTag(field, "android", "arch_variant") {
1121 // The arch_variant field isn't necessary past this point
1122 // Instead of wasting space, just remove it. Go also has a
1123 // 16-bit limit on structure name length. The name is constructed
1124 // based on the Go source representation of the structure, so
1125 // the tag names count towards that length.
Colin Crossb4fecbf2020-01-21 11:38:47 -08001126
1127 androidTag := field.Tag.Get("android")
1128 values := strings.Split(androidTag, ",")
1129
1130 if string(field.Tag) != `android:"`+strings.Join(values, ",")+`"` {
1131 panic(fmt.Errorf("unexpected tag format %q", field.Tag))
Colin Cross74449102019-09-25 11:26:40 -07001132 }
Colin Crossb4fecbf2020-01-21 11:38:47 -08001133 // these tags don't need to be present in the runtime generated struct type.
1134 values = RemoveListFromList(values, []string{"arch_variant", "variant_prepend", "path"})
1135 if len(values) > 0 {
1136 panic(fmt.Errorf("unknown tags %q in field %q", values, prefix+field.Name))
1137 }
1138
1139 field.Tag = ""
Colin Cross74449102019-09-25 11:26:40 -07001140 return true, field
1141 }
1142 return false, field
1143}
1144
Dan Willemsenb1957a52016-06-23 23:44:54 -07001145var archPropTypeMap OncePer
1146
Colin Cross36242852017-06-23 15:06:31 -07001147func InitArchModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001148
1149 base := m.base()
1150
Colin Cross36242852017-06-23 15:06:31 -07001151 base.generalProperties = m.GetProperties()
Colin Cross3f40fa42015-01-30 17:27:36 -08001152
1153 for _, properties := range base.generalProperties {
1154 propertiesValue := reflect.ValueOf(properties)
Colin Cross62496a02016-08-08 15:49:17 -07001155 t := propertiesValue.Type()
Colin Cross3f40fa42015-01-30 17:27:36 -08001156 if propertiesValue.Kind() != reflect.Ptr {
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
1161 propertiesValue = propertiesValue.Elem()
1162 if propertiesValue.Kind() != reflect.Struct {
Colin Crossca860ac2016-01-04 14:34:37 -08001163 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
1164 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001165 }
1166
Colin Cross571cccf2019-02-04 11:22:08 -08001167 archPropTypes := archPropTypeMap.Once(NewCustomOnceKey(t), func() interface{} {
Colin Crosscbbd13f2020-01-17 14:08:22 -08001168 return createArchPropTypeDesc(t)
1169 }).([]archPropTypeDesc)
Colin Cross3f40fa42015-01-30 17:27:36 -08001170
Colin Crossc17727d2018-10-24 12:42:09 -07001171 var archProperties []interface{}
1172 for _, t := range archPropTypes {
Colin Crosscbbd13f2020-01-17 14:08:22 -08001173 archProperties = append(archProperties, &archPropRoot{
1174 Arch: reflect.Zero(t.arch).Interface(),
1175 Multilib: reflect.Zero(t.multilib).Interface(),
1176 Target: reflect.Zero(t.target).Interface(),
1177 })
Dan Willemsenb1957a52016-06-23 23:44:54 -07001178 }
Colin Crossc17727d2018-10-24 12:42:09 -07001179 base.archProperties = append(base.archProperties, archProperties)
1180 m.AddProperties(archProperties...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001181 }
1182
Colin Cross36242852017-06-23 15:06:31 -07001183 base.customizableProperties = m.GetProperties()
Colin Cross3f40fa42015-01-30 17:27:36 -08001184}
1185
Colin Crossa716add2015-12-16 11:07:39 -08001186var variantReplacer = strings.NewReplacer("-", "_", ".", "_")
Colin Crossec193632015-07-06 17:49:43 -07001187
Colin Cross4157e882019-06-06 16:57:04 -07001188func (m *ModuleBase) appendProperties(ctx BottomUpMutatorContext,
Dan Willemsenb1957a52016-06-23 23:44:54 -07001189 dst interface{}, src reflect.Value, field, srcPrefix string) reflect.Value {
Colin Cross06a931b2015-10-28 17:23:31 -07001190
Colin Crosscbbd13f2020-01-17 14:08:22 -08001191 if src.Kind() == reflect.Ptr {
1192 if src.IsNil() {
1193 return src
1194 }
1195 src = src.Elem()
1196 }
1197
Dan Willemsenb1957a52016-06-23 23:44:54 -07001198 src = src.FieldByName(field)
1199 if !src.IsValid() {
Colin Crosseeabb892015-11-20 13:07:51 -08001200 ctx.ModuleErrorf("field %q does not exist", srcPrefix)
Dan Willemsenb1957a52016-06-23 23:44:54 -07001201 return src
Colin Cross85a88972015-11-23 13:29:51 -08001202 }
1203
Dan Willemsenb1957a52016-06-23 23:44:54 -07001204 ret := src
Colin Cross85a88972015-11-23 13:29:51 -08001205
Dan Willemsenb1957a52016-06-23 23:44:54 -07001206 if src.Kind() == reflect.Struct {
1207 src = src.FieldByName("BlueprintEmbed")
Colin Cross06a931b2015-10-28 17:23:31 -07001208 }
1209
Colin Cross6ee75b62016-05-05 15:57:15 -07001210 order := func(property string,
1211 dstField, srcField reflect.StructField,
1212 dstValue, srcValue interface{}) (proptools.Order, error) {
1213 if proptools.HasTag(dstField, "android", "variant_prepend") {
1214 return proptools.Prepend, nil
1215 } else {
1216 return proptools.Append, nil
1217 }
1218 }
1219
Dan Willemsenb1957a52016-06-23 23:44:54 -07001220 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, order)
Colin Cross06a931b2015-10-28 17:23:31 -07001221 if err != nil {
1222 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
1223 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
1224 } else {
1225 panic(err)
1226 }
1227 }
Colin Cross85a88972015-11-23 13:29:51 -08001228
Dan Willemsenb1957a52016-06-23 23:44:54 -07001229 return ret
Colin Cross06a931b2015-10-28 17:23:31 -07001230}
1231
Colin Crossa195f912019-10-16 11:07:20 -07001232// Rewrite the module's properties structs to contain os-specific values.
1233func (m *ModuleBase) setOSProperties(ctx BottomUpMutatorContext) {
1234 os := m.commonProperties.CompileOS
1235
1236 for i := range m.generalProperties {
1237 genProps := m.generalProperties[i]
1238 if m.archProperties[i] == nil {
1239 continue
1240 }
1241 for _, archProperties := range m.archProperties[i] {
1242 archPropValues := reflect.ValueOf(archProperties).Elem()
1243
Colin Crosscbbd13f2020-01-17 14:08:22 -08001244 targetProp := archPropValues.FieldByName("Target").Elem()
Colin Crossa195f912019-10-16 11:07:20 -07001245
1246 // Handle host-specific properties in the form:
1247 // target: {
1248 // host: {
1249 // key: value,
1250 // },
1251 // },
Jiyong Park1613e552020-09-14 19:43:17 +09001252 if os.Class == Host {
Colin Crossa195f912019-10-16 11:07:20 -07001253 field := "Host"
1254 prefix := "target.host"
1255 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1256 }
1257
1258 // Handle target OS generalities of the form:
1259 // target: {
1260 // bionic: {
1261 // key: value,
1262 // },
1263 // }
1264 if os.Linux() {
1265 field := "Linux"
1266 prefix := "target.linux"
1267 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1268 }
1269
1270 if os.Bionic() {
1271 field := "Bionic"
1272 prefix := "target.bionic"
1273 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1274 }
1275
1276 // Handle target OS properties in the form:
1277 // target: {
1278 // linux_glibc: {
1279 // key: value,
1280 // },
1281 // not_windows: {
1282 // key: value,
1283 // },
1284 // android {
1285 // key: value,
1286 // },
1287 // },
1288 field := os.Field
1289 prefix := "target." + os.Name
1290 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1291
Jiyong Park1613e552020-09-14 19:43:17 +09001292 if os.Class == Host && os != Windows {
Colin Crossa195f912019-10-16 11:07:20 -07001293 field := "Not_windows"
1294 prefix := "target.not_windows"
1295 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1296 }
1297
1298 // Handle 64-bit device properties in the form:
1299 // target {
1300 // android64 {
1301 // key: value,
1302 // },
1303 // android32 {
1304 // key: value,
1305 // },
1306 // },
1307 // WARNING: this is probably not what you want to use in your blueprints file, it selects
1308 // options for all targets on a device that supports 64-bit binaries, not just the targets
1309 // that are being compiled for 64-bit. Its expected use case is binaries like linker and
1310 // debuggerd that need to know when they are a 32-bit process running on a 64-bit device
1311 if os.Class == Device {
1312 if ctx.Config().Android64() {
1313 field := "Android64"
1314 prefix := "target.android64"
1315 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1316 } else {
1317 field := "Android32"
1318 prefix := "target.android32"
1319 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1320 }
1321 }
1322 }
1323 }
1324}
1325
Colin Cross3f40fa42015-01-30 17:27:36 -08001326// Rewrite the module's properties structs to contain arch-specific values.
Colin Cross4157e882019-06-06 16:57:04 -07001327func (m *ModuleBase) setArchProperties(ctx BottomUpMutatorContext) {
1328 arch := m.Arch()
1329 os := m.Os()
Colin Crossd3ba0392015-05-07 14:11:29 -07001330
Colin Cross4157e882019-06-06 16:57:04 -07001331 for i := range m.generalProperties {
1332 genProps := m.generalProperties[i]
1333 if m.archProperties[i] == nil {
Dan Willemsenb1957a52016-06-23 23:44:54 -07001334 continue
1335 }
Colin Cross4157e882019-06-06 16:57:04 -07001336 for _, archProperties := range m.archProperties[i] {
Colin Crossc17727d2018-10-24 12:42:09 -07001337 archPropValues := reflect.ValueOf(archProperties).Elem()
Dan Willemsenb1957a52016-06-23 23:44:54 -07001338
Colin Crosscbbd13f2020-01-17 14:08:22 -08001339 archProp := archPropValues.FieldByName("Arch").Elem()
1340 multilibProp := archPropValues.FieldByName("Multilib").Elem()
1341 targetProp := archPropValues.FieldByName("Target").Elem()
Dan Willemsenb1957a52016-06-23 23:44:54 -07001342
Colin Crossc17727d2018-10-24 12:42:09 -07001343 // Handle arch-specific properties in the form:
Colin Crossd5934c82017-10-02 13:55:26 -07001344 // arch: {
Colin Crossc17727d2018-10-24 12:42:09 -07001345 // arm64: {
Colin Crossd5934c82017-10-02 13:55:26 -07001346 // key: value,
1347 // },
1348 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001349 t := arch.ArchType
1350
1351 if arch.ArchType != Common {
1352 field := proptools.FieldNameForProperty(t.Name)
1353 prefix := "arch." + t.Name
Colin Cross4157e882019-06-06 16:57:04 -07001354 archStruct := m.appendProperties(ctx, genProps, archProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001355
1356 // Handle arch-variant-specific properties in the form:
1357 // arch: {
1358 // variant: {
1359 // key: value,
1360 // },
1361 // },
1362 v := variantReplacer.Replace(arch.ArchVariant)
1363 if v != "" {
1364 field := proptools.FieldNameForProperty(v)
1365 prefix := "arch." + t.Name + "." + v
Colin Cross4157e882019-06-06 16:57:04 -07001366 m.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001367 }
1368
1369 // Handle cpu-variant-specific properties in the form:
1370 // arch: {
1371 // variant: {
1372 // key: value,
1373 // },
1374 // },
1375 if arch.CpuVariant != arch.ArchVariant {
1376 c := variantReplacer.Replace(arch.CpuVariant)
1377 if c != "" {
1378 field := proptools.FieldNameForProperty(c)
1379 prefix := "arch." + t.Name + "." + c
Colin Cross4157e882019-06-06 16:57:04 -07001380 m.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001381 }
1382 }
1383
1384 // Handle arch-feature-specific properties in the form:
1385 // arch: {
1386 // feature: {
1387 // key: value,
1388 // },
1389 // },
1390 for _, feature := range arch.ArchFeatures {
1391 field := proptools.FieldNameForProperty(feature)
1392 prefix := "arch." + t.Name + "." + feature
Colin Cross4157e882019-06-06 16:57:04 -07001393 m.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001394 }
1395
1396 // Handle multilib-specific properties in the form:
1397 // multilib: {
1398 // lib32: {
1399 // key: value,
1400 // },
1401 // },
1402 field = proptools.FieldNameForProperty(t.Multilib)
1403 prefix = "multilib." + t.Multilib
Colin Cross4157e882019-06-06 16:57:04 -07001404 m.appendProperties(ctx, genProps, multilibProp, field, prefix)
Colin Cross08016332016-12-20 09:53:14 -08001405 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001406
Colin Crossa195f912019-10-16 11:07:20 -07001407 // Handle combined OS-feature and arch specific properties in the form:
Colin Crossc17727d2018-10-24 12:42:09 -07001408 // target: {
Colin Crossc17727d2018-10-24 12:42:09 -07001409 // bionic_x86: {
1410 // key: value,
1411 // },
1412 // }
Colin Crossa195f912019-10-16 11:07:20 -07001413 if os.Linux() && arch.ArchType != Common {
1414 field := "Linux_" + arch.ArchType.Name
1415 prefix := "target.linux_" + arch.ArchType.Name
Colin Cross4157e882019-06-06 16:57:04 -07001416 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossd5934c82017-10-02 13:55:26 -07001417 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001418
Colin Crossa195f912019-10-16 11:07:20 -07001419 if os.Bionic() && arch.ArchType != Common {
1420 field := "Bionic_" + t.Name
1421 prefix := "target.bionic_" + t.Name
Colin Cross4157e882019-06-06 16:57:04 -07001422 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001423 }
1424
Colin Crossa195f912019-10-16 11:07:20 -07001425 // Handle combined OS and arch specific properties in the form:
Colin Crossc17727d2018-10-24 12:42:09 -07001426 // target: {
Colin Crossc17727d2018-10-24 12:42:09 -07001427 // linux_glibc_x86: {
1428 // key: value,
1429 // },
1430 // linux_glibc_arm: {
1431 // key: value,
1432 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001433 // android_arm {
1434 // key: value,
1435 // },
1436 // android_x86 {
Colin Crossd5934c82017-10-02 13:55:26 -07001437 // key: value,
1438 // },
1439 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001440 if arch.ArchType != Common {
Colin Crossa195f912019-10-16 11:07:20 -07001441 field := os.Field + "_" + t.Name
1442 prefix := "target." + os.Name + "_" + t.Name
Colin Cross4157e882019-06-06 16:57:04 -07001443 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossd5934c82017-10-02 13:55:26 -07001444 }
1445
Colin Crossa195f912019-10-16 11:07:20 -07001446 // Handle arm on x86 properties in the form:
Colin Crossc17727d2018-10-24 12:42:09 -07001447 // target {
Colin Crossa195f912019-10-16 11:07:20 -07001448 // arm_on_x86 {
Colin Crossc17727d2018-10-24 12:42:09 -07001449 // key: value,
1450 // },
Colin Crossa195f912019-10-16 11:07:20 -07001451 // arm_on_x86_64 {
Colin Crossd5934c82017-10-02 13:55:26 -07001452 // key: value,
1453 // },
1454 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001455 if os.Class == Device {
Victor Khimenko1a31f802020-09-17 03:07:31 +02001456 if arch.ArchType == X86 && (hasArmAbi(arch) ||
1457 hasArmAndroidArch(ctx.Config().Targets[Android])) {
Colin Crossc17727d2018-10-24 12:42:09 -07001458 field := "Arm_on_x86"
1459 prefix := "target.arm_on_x86"
Colin Cross4157e882019-06-06 16:57:04 -07001460 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001461 }
Victor Khimenko1a31f802020-09-17 03:07:31 +02001462 if arch.ArchType == X86_64 && (hasArmAbi(arch) ||
1463 hasArmAndroidArch(ctx.Config().Targets[Android])) {
Colin Crossc17727d2018-10-24 12:42:09 -07001464 field := "Arm_on_x86_64"
1465 prefix := "target.arm_on_x86_64"
Colin Cross4157e882019-06-06 16:57:04 -07001466 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001467 }
Victor Khimenkoc26fcf42020-05-07 22:16:33 +02001468 if os == Android && m.Target().NativeBridge == NativeBridgeEnabled {
1469 field := "Native_bridge"
1470 prefix := "target.native_bridge"
1471 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1472 }
Colin Cross4247f0d2017-04-13 16:56:14 -07001473 }
Colin Crossbb2e2b72016-12-08 17:23:53 -08001474 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001475 }
1476}
1477
1478func forEachInterface(v reflect.Value, f func(reflect.Value)) {
1479 switch v.Kind() {
1480 case reflect.Interface:
1481 f(v)
1482 case reflect.Struct:
1483 for i := 0; i < v.NumField(); i++ {
1484 forEachInterface(v.Field(i), f)
1485 }
1486 case reflect.Ptr:
1487 forEachInterface(v.Elem(), f)
1488 default:
1489 panic(fmt.Errorf("Unsupported kind %s", v.Kind()))
1490 }
1491}
Colin Cross4225f652015-09-17 14:33:42 -07001492
Colin Crossa1ad8d12016-06-01 17:09:44 -07001493// Convert the arch product variables into a list of targets for each os class structs
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001494func decodeTargetProductVariables(config *config) (map[OsType][]Target, error) {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001495 variables := config.productVariables
Dan Willemsen490fd492015-11-24 17:53:15 -08001496
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001497 targets := make(map[OsType][]Target)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001498 var targetErr error
1499
dimitry1f33e402019-03-26 12:39:31 +01001500 addTarget := func(os OsType, archName string, archVariant, cpuVariant *string, abi []string,
dimitry8d6dde82019-07-11 10:23:53 +02001501 nativeBridgeEnabled NativeBridgeSupport, nativeBridgeHostArchName *string,
1502 nativeBridgeRelativePath *string) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001503 if targetErr != nil {
1504 return
Dan Willemsen490fd492015-11-24 17:53:15 -08001505 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001506
Dan Willemsen01a3c252019-01-11 19:02:16 -08001507 arch, err := decodeArch(os, archName, archVariant, cpuVariant, abi)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001508 if err != nil {
1509 targetErr = err
1510 return
1511 }
dimitry8d6dde82019-07-11 10:23:53 +02001512 nativeBridgeRelativePathStr := String(nativeBridgeRelativePath)
1513 nativeBridgeHostArchNameStr := String(nativeBridgeHostArchName)
1514
1515 // Use guest arch as relative install path by default
1516 if nativeBridgeEnabled && nativeBridgeRelativePathStr == "" {
1517 nativeBridgeRelativePathStr = arch.ArchType.String()
1518 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001519
Jiyong Park1613e552020-09-14 19:43:17 +09001520 // A target is considered as HostCross if it's a host target which can't run natively on
1521 // the currently configured build machine (either because the OS is different or because of
1522 // the unsupported arch)
1523 hostCross := false
1524 if os.Class == Host {
1525 var osSupported bool
1526 if os == BuildOs {
1527 osSupported = true
1528 } else if BuildOs.Linux() && os.Linux() {
1529 // LinuxBionic and Linux are compatible
1530 osSupported = true
1531 } else {
1532 osSupported = false
1533 }
1534
1535 var archSupported bool
1536 if arch.ArchType == Common {
1537 archSupported = true
1538 } else if arch.ArchType.Name == *variables.HostArch {
1539 archSupported = true
1540 } else if variables.HostSecondaryArch != nil && arch.ArchType.Name == *variables.HostSecondaryArch {
1541 archSupported = true
1542 } else {
1543 archSupported = false
1544 }
1545 if !osSupported || !archSupported {
1546 hostCross = true
1547 }
1548 }
1549
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001550 targets[os] = append(targets[os],
Colin Crossa1ad8d12016-06-01 17:09:44 -07001551 Target{
dimitry8d6dde82019-07-11 10:23:53 +02001552 Os: os,
1553 Arch: arch,
1554 NativeBridge: nativeBridgeEnabled,
1555 NativeBridgeHostArchName: nativeBridgeHostArchNameStr,
1556 NativeBridgeRelativePath: nativeBridgeRelativePathStr,
Jiyong Park1613e552020-09-14 19:43:17 +09001557 HostCross: hostCross,
Colin Crossa1ad8d12016-06-01 17:09:44 -07001558 })
Dan Willemsen490fd492015-11-24 17:53:15 -08001559 }
1560
Colin Cross4225f652015-09-17 14:33:42 -07001561 if variables.HostArch == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001562 return nil, fmt.Errorf("No host primary architecture set")
Colin Cross4225f652015-09-17 14:33:42 -07001563 }
1564
dimitry8d6dde82019-07-11 10:23:53 +02001565 addTarget(BuildOs, *variables.HostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001566
Colin Crosseeabb892015-11-20 13:07:51 -08001567 if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" {
dimitry8d6dde82019-07-11 10:23:53 +02001568 addTarget(BuildOs, *variables.HostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001569 }
1570
Colin Crossff3ae9d2018-04-10 16:15:18 -07001571 if Bool(config.Host_bionic) {
dimitry8d6dde82019-07-11 10:23:53 +02001572 addTarget(LinuxBionic, "x86_64", nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen01a405a2016-06-13 17:19:03 -07001573 }
Jiyong Park22101982020-09-17 19:09:58 +09001574 if Bool(config.Host_bionic_arm64) {
1575 addTarget(LinuxBionic, "arm64", nil, nil, nil, NativeBridgeDisabled, nil, nil)
1576 }
Dan Willemsen01a405a2016-06-13 17:19:03 -07001577
Colin Crossff3ae9d2018-04-10 16:15:18 -07001578 if String(variables.CrossHost) != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001579 crossHostOs := osByName(*variables.CrossHost)
1580 if crossHostOs == NoOsType {
1581 return nil, fmt.Errorf("Unknown cross host OS %q", *variables.CrossHost)
1582 }
1583
Colin Crossff3ae9d2018-04-10 16:15:18 -07001584 if String(variables.CrossHostArch) == "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001585 return nil, fmt.Errorf("No cross-host primary architecture set")
Dan Willemsen490fd492015-11-24 17:53:15 -08001586 }
1587
dimitry8d6dde82019-07-11 10:23:53 +02001588 addTarget(crossHostOs, *variables.CrossHostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001589
1590 if variables.CrossHostSecondaryArch != nil && *variables.CrossHostSecondaryArch != "" {
dimitry8d6dde82019-07-11 10:23:53 +02001591 addTarget(crossHostOs, *variables.CrossHostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001592 }
1593 }
1594
Dan Willemsen3f32f032016-07-11 14:36:48 -07001595 if variables.DeviceArch != nil && *variables.DeviceArch != "" {
Doug Horn21b94272019-01-16 12:06:11 -08001596 var target = Android
1597 if Bool(variables.Fuchsia) {
1598 target = Fuchsia
1599 }
1600
1601 addTarget(target, *variables.DeviceArch, variables.DeviceArchVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001602 variables.DeviceCpuVariant, variables.DeviceAbi, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001603
Dan Willemsen3f32f032016-07-11 14:36:48 -07001604 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" {
1605 addTarget(Android, *variables.DeviceSecondaryArch,
1606 variables.DeviceSecondaryArchVariant, variables.DeviceSecondaryCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001607 variables.DeviceSecondaryAbi, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001608 }
dimitry1f33e402019-03-26 12:39:31 +01001609
1610 if variables.NativeBridgeArch != nil && *variables.NativeBridgeArch != "" {
1611 addTarget(Android, *variables.NativeBridgeArch,
1612 variables.NativeBridgeArchVariant, variables.NativeBridgeCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001613 variables.NativeBridgeAbi, NativeBridgeEnabled, variables.DeviceArch,
1614 variables.NativeBridgeRelativePath)
dimitry1f33e402019-03-26 12:39:31 +01001615 }
1616
1617 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" &&
1618 variables.NativeBridgeSecondaryArch != nil && *variables.NativeBridgeSecondaryArch != "" {
1619 addTarget(Android, *variables.NativeBridgeSecondaryArch,
1620 variables.NativeBridgeSecondaryArchVariant,
1621 variables.NativeBridgeSecondaryCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001622 variables.NativeBridgeSecondaryAbi,
1623 NativeBridgeEnabled,
1624 variables.DeviceSecondaryArch,
1625 variables.NativeBridgeSecondaryRelativePath)
dimitry1f33e402019-03-26 12:39:31 +01001626 }
Colin Cross4225f652015-09-17 14:33:42 -07001627 }
1628
Colin Crossa1ad8d12016-06-01 17:09:44 -07001629 if targetErr != nil {
1630 return nil, targetErr
1631 }
1632
1633 return targets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001634}
1635
Colin Crossbb2e2b72016-12-08 17:23:53 -08001636// hasArmAbi returns true if arch has at least one arm ABI
1637func hasArmAbi(arch Arch) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -08001638 return PrefixInList(arch.Abi, "arm")
Colin Crossbb2e2b72016-12-08 17:23:53 -08001639}
1640
dimitry628db6f2019-05-22 17:16:21 +02001641// hasArmArch returns true if targets has at least non-native_bridge arm Android arch
Colin Cross4247f0d2017-04-13 16:56:14 -07001642func hasArmAndroidArch(targets []Target) bool {
1643 for _, target := range targets {
Victor Khimenko1a31f802020-09-17 03:07:31 +02001644 if target.Os == Android && target.Arch.ArchType == Arm {
Victor Khimenko5eb8ec12018-03-21 20:30:54 +01001645 return true
1646 }
1647 }
1648 return false
1649}
1650
Dan Albert4098deb2016-10-19 14:04:41 -07001651type archConfig struct {
1652 arch string
1653 archVariant string
1654 cpuVariant string
1655 abi []string
1656}
1657
1658func getMegaDeviceConfig() []archConfig {
1659 return []archConfig{
Dan Albert8818f492019-02-19 13:53:01 -08001660 {"arm", "armv7-a", "generic", []string{"armeabi-v7a"}},
Dan Willemsen110a89d2016-01-14 15:17:19 -08001661 {"arm", "armv7-a-neon", "generic", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001662 {"arm", "armv7-a-neon", "cortex-a7", []string{"armeabi-v7a"}},
1663 {"arm", "armv7-a-neon", "cortex-a8", []string{"armeabi-v7a"}},
Dan Willemsen110a89d2016-01-14 15:17:19 -08001664 {"arm", "armv7-a-neon", "cortex-a9", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001665 {"arm", "armv7-a-neon", "cortex-a15", []string{"armeabi-v7a"}},
1666 {"arm", "armv7-a-neon", "cortex-a53", []string{"armeabi-v7a"}},
1667 {"arm", "armv7-a-neon", "cortex-a53.a57", []string{"armeabi-v7a"}},
Richard Fungeb37ed32018-09-24 16:33:45 -07001668 {"arm", "armv7-a-neon", "cortex-a72", []string{"armeabi-v7a"}},
Jake Weinstein6600a442017-05-15 18:27:12 -04001669 {"arm", "armv7-a-neon", "cortex-a73", []string{"armeabi-v7a"}},
Christopher Ferrisba14a8f2018-04-23 18:15:25 -07001670 {"arm", "armv7-a-neon", "cortex-a75", []string{"armeabi-v7a"}},
Haibo Huanga31e2bd2018-10-09 14:27:28 -07001671 {"arm", "armv7-a-neon", "cortex-a76", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001672 {"arm", "armv7-a-neon", "krait", []string{"armeabi-v7a"}},
Alex Naidisae4fc182016-08-20 00:14:56 +02001673 {"arm", "armv7-a-neon", "kryo", []string{"armeabi-v7a"}},
Artem Serovd3072b02018-11-15 15:21:51 +00001674 {"arm", "armv7-a-neon", "kryo385", []string{"armeabi-v7a"}},
Junmo Park8ea49592017-07-24 07:14:55 +09001675 {"arm", "armv7-a-neon", "exynos-m1", []string{"armeabi-v7a"}},
Junmo Parkd86c9022017-07-21 09:07:47 +09001676 {"arm", "armv7-a-neon", "exynos-m2", []string{"armeabi-v7a"}},
Dan Willemsen110a89d2016-01-14 15:17:19 -08001677 {"arm64", "armv8-a", "cortex-a53", []string{"arm64-v8a"}},
Richard Fungeb37ed32018-09-24 16:33:45 -07001678 {"arm64", "armv8-a", "cortex-a72", []string{"arm64-v8a"}},
Jake Weinstein6600a442017-05-15 18:27:12 -04001679 {"arm64", "armv8-a", "cortex-a73", []string{"arm64-v8a"}},
Alex Naidisac01ff52016-08-30 15:56:33 +02001680 {"arm64", "armv8-a", "kryo", []string{"arm64-v8a"}},
Junmo Park8ea49592017-07-24 07:14:55 +09001681 {"arm64", "armv8-a", "exynos-m1", []string{"arm64-v8a"}},
Junmo Parkd86c9022017-07-21 09:07:47 +09001682 {"arm64", "armv8-a", "exynos-m2", []string{"arm64-v8a"}},
Artem Serovd3072b02018-11-15 15:21:51 +00001683 {"arm64", "armv8-2a", "kryo385", []string{"arm64-v8a"}},
Raphael Gault70b96b02020-06-18 09:56:53 +00001684 {"arm64", "armv8-2a-dotprod", "cortex-a55", []string{"arm64-v8a"}},
1685 {"arm64", "armv8-2a-dotprod", "cortex-a75", []string{"arm64-v8a"}},
1686 {"arm64", "armv8-2a-dotprod", "cortex-a76", []string{"arm64-v8a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001687 {"x86", "", "", []string{"x86"}},
1688 {"x86", "atom", "", []string{"x86"}},
1689 {"x86", "haswell", "", []string{"x86"}},
1690 {"x86", "ivybridge", "", []string{"x86"}},
1691 {"x86", "sandybridge", "", []string{"x86"}},
1692 {"x86", "silvermont", "", []string{"x86"}},
Benjamin Gordon87e7f2f2019-02-14 10:59:48 -07001693 {"x86", "stoneyridge", "", []string{"x86"}},
Dan Willemsen8a354052016-05-10 14:30:51 -07001694 {"x86", "x86_64", "", []string{"x86"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001695 {"x86_64", "", "", []string{"x86_64"}},
1696 {"x86_64", "haswell", "", []string{"x86_64"}},
1697 {"x86_64", "ivybridge", "", []string{"x86_64"}},
1698 {"x86_64", "sandybridge", "", []string{"x86_64"}},
1699 {"x86_64", "silvermont", "", []string{"x86_64"}},
Benjamin Gordon87e7f2f2019-02-14 10:59:48 -07001700 {"x86_64", "stoneyridge", "", []string{"x86_64"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001701 }
Dan Albert4098deb2016-10-19 14:04:41 -07001702}
Dan Willemsen322acaf2016-01-12 23:07:05 -08001703
Dan Albert4098deb2016-10-19 14:04:41 -07001704func getNdkAbisConfig() []archConfig {
1705 return []archConfig{
Dan Albert6bba6442020-01-30 15:16:49 -08001706 {"arm", "armv7-a", "", []string{"armeabi-v7a"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001707 {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001708 {"x86", "", "", []string{"x86"}},
1709 {"x86_64", "", "", []string{"x86_64"}},
1710 }
1711}
1712
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001713func getAmlAbisConfig() []archConfig {
1714 return []archConfig{
Martin Stjernholm93688342020-10-16 21:45:10 +01001715 {"arm", "armv7-a-neon", "", []string{"armeabi-v7a"}},
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001716 {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
1717 {"x86", "", "", []string{"x86"}},
1718 {"x86_64", "", "", []string{"x86_64"}},
1719 }
1720}
1721
Dan Willemsen01a3c252019-01-11 19:02:16 -08001722func decodeArchSettings(os OsType, archConfigs []archConfig) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001723 var ret []Target
Dan Willemsen322acaf2016-01-12 23:07:05 -08001724
Dan Albert4098deb2016-10-19 14:04:41 -07001725 for _, config := range archConfigs {
Dan Willemsen01a3c252019-01-11 19:02:16 -08001726 arch, err := decodeArch(os, config.arch, &config.archVariant,
Colin Crossa74ca042019-01-31 14:31:51 -08001727 &config.cpuVariant, config.abi)
Dan Willemsen322acaf2016-01-12 23:07:05 -08001728 if err != nil {
1729 return nil, err
1730 }
Colin Cross3b19f5d2019-09-17 14:45:31 -07001731
Colin Crossa1ad8d12016-06-01 17:09:44 -07001732 ret = append(ret, Target{
1733 Os: Android,
1734 Arch: arch,
1735 })
Dan Willemsen322acaf2016-01-12 23:07:05 -08001736 }
1737
1738 return ret, nil
1739}
1740
Colin Cross4225f652015-09-17 14:33:42 -07001741// Convert a set of strings from product variables into a single Arch struct
Colin Crossa74ca042019-01-31 14:31:51 -08001742func decodeArch(os OsType, arch string, archVariant, cpuVariant *string, abi []string) (Arch, error) {
Colin Cross4225f652015-09-17 14:33:42 -07001743 stringPtr := func(p *string) string {
1744 if p != nil {
1745 return *p
1746 }
1747 return ""
1748 }
1749
Colin Crosseeabb892015-11-20 13:07:51 -08001750 archType, ok := archTypeMap[arch]
1751 if !ok {
1752 return Arch{}, fmt.Errorf("unknown arch %q", arch)
1753 }
Colin Cross4225f652015-09-17 14:33:42 -07001754
Colin Crosseeabb892015-11-20 13:07:51 -08001755 a := Arch{
Colin Cross4225f652015-09-17 14:33:42 -07001756 ArchType: archType,
1757 ArchVariant: stringPtr(archVariant),
1758 CpuVariant: stringPtr(cpuVariant),
Colin Crossa74ca042019-01-31 14:31:51 -08001759 Abi: abi,
Colin Crosseeabb892015-11-20 13:07:51 -08001760 }
1761
1762 if a.ArchVariant == a.ArchType.Name || a.ArchVariant == "generic" {
1763 a.ArchVariant = ""
1764 }
1765
1766 if a.CpuVariant == a.ArchType.Name || a.CpuVariant == "generic" {
1767 a.CpuVariant = ""
1768 }
1769
1770 for i := 0; i < len(a.Abi); i++ {
1771 if a.Abi[i] == "" {
1772 a.Abi = append(a.Abi[:i], a.Abi[i+1:]...)
1773 i--
1774 }
1775 }
1776
Dan Willemsen01a3c252019-01-11 19:02:16 -08001777 if a.ArchVariant == "" {
1778 if featureMap, ok := defaultArchFeatureMap[os]; ok {
1779 a.ArchFeatures = featureMap[archType]
1780 }
1781 } else {
1782 if featureMap, ok := archFeatureMap[archType]; ok {
1783 a.ArchFeatures = featureMap[a.ArchVariant]
1784 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001785 }
1786
Colin Crosseeabb892015-11-20 13:07:51 -08001787 return a, nil
Colin Cross4225f652015-09-17 14:33:42 -07001788}
1789
Colin Cross69617d32016-09-06 10:39:07 -07001790func filterMultilibTargets(targets []Target, multilib string) []Target {
1791 var ret []Target
1792 for _, t := range targets {
1793 if t.Arch.ArchType.Multilib == multilib {
1794 ret = append(ret, t)
1795 }
1796 }
1797 return ret
1798}
1799
Paul Duffinca7f0ef2020-02-25 15:50:49 +00001800// Return the set of Os specific common architecture targets for each Os in a list of
1801// targets.
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001802func getCommonTargets(targets []Target) []Target {
1803 var ret []Target
1804 set := make(map[string]bool)
1805
1806 for _, t := range targets {
1807 if _, found := set[t.Os.String()]; !found {
1808 set[t.Os.String()] = true
1809 ret = append(ret, commonTargetMap[t.Os.String()])
1810 }
1811 }
1812
1813 return ret
1814}
1815
Colin Cross3dceee32018-09-06 10:19:57 -07001816func firstTarget(targets []Target, filters ...string) []Target {
Jiyong Park22101982020-09-17 19:09:58 +09001817 // find the first target from each OS
1818 var ret []Target
1819 hasHost := false
1820 set := make(map[OsType]bool)
1821
Colin Cross6b4a32d2017-12-05 13:42:45 -08001822 for _, filter := range filters {
1823 buildTargets := filterMultilibTargets(targets, filter)
Jiyong Park22101982020-09-17 19:09:58 +09001824 for _, t := range buildTargets {
1825 if _, found := set[t.Os]; !found {
1826 hasHost = hasHost || (t.Os.Class == Host)
1827 set[t.Os] = true
1828 ret = append(ret, t)
1829 }
Colin Cross6b4a32d2017-12-05 13:42:45 -08001830 }
1831 }
Jiyong Park22101982020-09-17 19:09:58 +09001832 return ret
Colin Cross6b4a32d2017-12-05 13:42:45 -08001833}
1834
Colin Crossa1ad8d12016-06-01 17:09:44 -07001835// Use the module multilib setting to select one or more targets from a target list
Colin Crossee0bc3b2018-10-02 22:01:37 -07001836func decodeMultilibTargets(multilib string, targets []Target, prefer32 bool) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001837 buildTargets := []Target{}
Colin Cross6b4a32d2017-12-05 13:42:45 -08001838
Colin Cross4225f652015-09-17 14:33:42 -07001839 switch multilib {
1840 case "common":
Colin Cross6b4a32d2017-12-05 13:42:45 -08001841 buildTargets = getCommonTargets(targets)
1842 case "common_first":
1843 buildTargets = getCommonTargets(targets)
1844 if prefer32 {
Colin Cross3dceee32018-09-06 10:19:57 -07001845 buildTargets = append(buildTargets, firstTarget(targets, "lib32", "lib64")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001846 } else {
Colin Cross3dceee32018-09-06 10:19:57 -07001847 buildTargets = append(buildTargets, firstTarget(targets, "lib64", "lib32")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001848 }
Colin Cross4225f652015-09-17 14:33:42 -07001849 case "both":
Colin Cross8b74d172016-09-13 09:59:14 -07001850 if prefer32 {
1851 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1852 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1853 } else {
1854 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1855 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1856 }
Colin Cross4225f652015-09-17 14:33:42 -07001857 case "32":
Colin Cross69617d32016-09-06 10:39:07 -07001858 buildTargets = filterMultilibTargets(targets, "lib32")
Colin Cross4225f652015-09-17 14:33:42 -07001859 case "64":
Colin Cross69617d32016-09-06 10:39:07 -07001860 buildTargets = filterMultilibTargets(targets, "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001861 case "first":
1862 if prefer32 {
Colin Cross3dceee32018-09-06 10:19:57 -07001863 buildTargets = firstTarget(targets, "lib32", "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001864 } else {
Colin Cross3dceee32018-09-06 10:19:57 -07001865 buildTargets = firstTarget(targets, "lib64", "lib32")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001866 }
Colin Cross69617d32016-09-06 10:39:07 -07001867 case "prefer32":
Colin Cross3dceee32018-09-06 10:19:57 -07001868 buildTargets = filterMultilibTargets(targets, "lib32")
1869 if len(buildTargets) == 0 {
1870 buildTargets = filterMultilibTargets(targets, "lib64")
1871 }
Colin Cross4225f652015-09-17 14:33:42 -07001872 default:
Colin Cross69617d32016-09-06 10:39:07 -07001873 return nil, fmt.Errorf(`compile_multilib must be "both", "first", "32", "64", or "prefer32" found %q`,
Colin Cross4225f652015-09-17 14:33:42 -07001874 multilib)
Colin Cross4225f652015-09-17 14:33:42 -07001875 }
1876
Colin Crossa1ad8d12016-06-01 17:09:44 -07001877 return buildTargets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001878}