-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharch.go
1718 lines (1534 loc) · 40.5 KB
/
arch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2015 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package android
import (
"encoding"
"fmt"
"reflect"
"runtime"
"strconv"
"strings"
"github.com/google/blueprint/proptools"
)
var (
archTypeList []ArchType
Arm = newArch("arm", "lib32")
Arm64 = newArch("arm64", "lib64")
Mips = newArch("mips", "lib32")
Mips64 = newArch("mips64", "lib64")
X86 = newArch("x86", "lib32")
X86_64 = newArch("x86_64", "lib64")
Common = ArchType{
Name: "common",
}
)
var archTypeMap = map[string]ArchType{
"arm": Arm,
"arm64": Arm64,
"mips": Mips,
"mips64": Mips64,
"x86": X86,
"x86_64": X86_64,
}
/*
Example blueprints file containing all variant property groups, with comment listing what type
of variants get properties in that group:
module {
arch: {
arm: {
// Host or device variants with arm architecture
},
arm64: {
// Host or device variants with arm64 architecture
},
mips: {
// Host or device variants with mips architecture
},
mips64: {
// Host or device variants with mips64 architecture
},
x86: {
// Host or device variants with x86 architecture
},
x86_64: {
// Host or device variants with x86_64 architecture
},
},
multilib: {
lib32: {
// Host or device variants for 32-bit architectures
},
lib64: {
// Host or device variants for 64-bit architectures
},
},
target: {
android: {
// Device variants
},
host: {
// Host variants
},
linux_glibc: {
// Linux host variants
},
darwin: {
// Darwin host variants
},
windows: {
// Windows host variants
},
not_windows: {
// Non-windows host variants
},
},
}
*/
var archVariants = map[ArchType][]string{
Arm: {
"armv7-a",
"armv7-a-neon",
"armv8-a",
"armv8-2a",
"cortex-a7",
"cortex-a8",
"cortex-a9",
"cortex-a15",
"cortex-a53",
"cortex-a53-a57",
"cortex-a55",
"cortex-a72",
"cortex-a73",
"cortex-a75",
"cortex-a76",
"krait",
"kryo",
"kryo385",
"exynos-m1",
"exynos-m2",
},
Arm64: {
"armv8_a",
"armv8_2a",
"cortex-a53",
"cortex-a55",
"cortex-a72",
"cortex-a73",
"cortex-a75",
"cortex-a76",
"kryo",
"kryo385",
"exynos-m1",
"exynos-m2",
},
Mips: {
"mips32_fp",
"mips32r2_fp",
"mips32r2_fp_xburst",
"mips32r2dsp_fp",
"mips32r2dspr2_fp",
"mips32r6",
},
Mips64: {
"mips64r2",
"mips64r6",
},
X86: {
"amberlake",
"atom",
"broadwell",
"haswell",
"icelake",
"ivybridge",
"kabylake",
"sandybridge",
"silvermont",
"skylake",
"stoneyridge",
"tigerlake",
"whiskeylake",
"x86_64",
},
X86_64: {
"amberlake",
"broadwell",
"haswell",
"icelake",
"ivybridge",
"kabylake",
"sandybridge",
"silvermont",
"skylake",
"stoneyridge",
"tigerlake",
"whiskeylake",
},
}
var archFeatures = map[ArchType][]string{
Arm: {
"neon",
},
Mips: {
"dspr2",
"rev6",
"msa",
},
Mips64: {
"rev6",
"msa",
},
X86: {
"ssse3",
"sse4",
"sse4_1",
"sse4_2",
"aes_ni",
"avx",
"avx2",
"avx512",
"popcnt",
"movbe",
},
X86_64: {
"ssse3",
"sse4",
"sse4_1",
"sse4_2",
"aes_ni",
"avx",
"avx2",
"avx512",
"popcnt",
},
}
var archFeatureMap = map[ArchType]map[string][]string{
Arm: {
"armv7-a-neon": {
"neon",
},
"armv8-a": {
"neon",
},
"armv8-2a": {
"neon",
},
},
Mips: {
"mips32r2dspr2_fp": {
"dspr2",
},
"mips32r6": {
"rev6",
},
},
Mips64: {
"mips64r6": {
"rev6",
},
},
X86: {
"amberlake": {
"ssse3",
"sse4",
"sse4_1",
"sse4_2",
"avx",
"avx2",
"aes_ni",
"popcnt",
},
"atom": {
"ssse3",
"movbe",
},
"broadwell": {
"ssse3",
"sse4",
"sse4_1",
"sse4_2",
"avx",
"avx2",
"aes_ni",
"popcnt",
},
"haswell": {
"ssse3",
"sse4",
"sse4_1",
"sse4_2",
"aes_ni",
"avx",
"popcnt",
"movbe",
},
"icelake": {
"ssse3",
"sse4",
"sse4_1",
"sse4_2",
"avx",
"avx2",
"avx512",
"aes_ni",
"popcnt",
},
"ivybridge": {
"ssse3",
"sse4",
"sse4_1",
"sse4_2",
"aes_ni",
"avx",
"popcnt",
},
"kabylake": {
"ssse3",
"sse4",
"sse4_1",
"sse4_2",
"avx",
"avx2",
"aes_ni",
"popcnt",
},
"sandybridge": {
"ssse3",
"sse4",
"sse4_1",
"sse4_2",
"popcnt",
},
"silvermont": {
"ssse3",
"sse4",
"sse4_1",
"sse4_2",
"aes_ni",
"popcnt",
"movbe",
},
"skylake": {
"ssse3",
"sse4",
"sse4_1",
"sse4_2",
"avx",
"avx2",
"avx512",
"aes_ni",
"popcnt",
},
"stoneyridge": {
"ssse3",
"sse4",
"sse4_1",
"sse4_2",
"aes_ni",
"avx",
"avx2",
"popcnt",
"movbe",
},
"tigerlake": {
"ssse3",
"sse4",
"sse4_1",
"sse4_2",
"avx",
"avx2",
"avx512",
"aes_ni",
"popcnt",
},
"whiskeylake": {
"ssse3",
"sse4",
"sse4_1",
"sse4_2",
"avx",
"avx2",
"avx512",
"aes_ni",
"popcnt",
},
"x86_64": {
"ssse3",
"sse4",
"sse4_1",
"sse4_2",
"popcnt",
},
},
X86_64: {
"amberlake": {
"ssse3",
"sse4",
"sse4_1",
"sse4_2",
"avx",
"avx2",
"aes_ni",
"popcnt",
},
"broadwell": {
"ssse3",
"sse4",
"sse4_1",
"sse4_2",
"avx",
"avx2",
"aes_ni",
"popcnt",
},
"haswell": {
"ssse3",
"sse4",
"sse4_1",
"sse4_2",
"aes_ni",
"avx",
"popcnt",
},
"icelake": {
"ssse3",
"sse4",
"sse4_1",
"sse4_2",
"avx",
"avx2",
"avx512",
"aes_ni",
"popcnt",
},
"ivybridge": {
"ssse3",
"sse4",
"sse4_1",
"sse4_2",
"aes_ni",
"avx",
"popcnt",
},
"kabylake": {
"ssse3",
"sse4",
"sse4_1",
"sse4_2",
"avx",
"avx2",
"aes_ni",
"popcnt",
},
"sandybridge": {
"ssse3",
"sse4",
"sse4_1",
"sse4_2",
"popcnt",
},
"silvermont": {
"ssse3",
"sse4",
"sse4_1",
"sse4_2",
"aes_ni",
"popcnt",
},
"skylake": {
"ssse3",
"sse4",
"sse4_1",
"sse4_2",
"avx",
"avx2",
"avx512",
"aes_ni",
"popcnt",
},
"stoneyridge": {
"ssse3",
"sse4",
"sse4_1",
"sse4_2",
"aes_ni",
"avx",
"avx2",
"popcnt",
},
"tigerlake": {
"ssse3",
"sse4",
"sse4_1",
"sse4_2",
"avx",
"avx2",
"avx512",
"aes_ni",
"popcnt",
},
"whiskeylake": {
"ssse3",
"sse4",
"sse4_1",
"sse4_2",
"avx",
"avx2",
"avx512",
"aes_ni",
"popcnt",
},
},
}
var defaultArchFeatureMap = map[OsType]map[ArchType][]string{}
func RegisterDefaultArchVariantFeatures(os OsType, arch ArchType, features ...string) {
checkCalledFromInit()
for _, feature := range features {
if !InList(feature, archFeatures[arch]) {
panic(fmt.Errorf("Invalid feature %q for arch %q variant \"\"", feature, arch))
}
}
if defaultArchFeatureMap[os] == nil {
defaultArchFeatureMap[os] = make(map[ArchType][]string)
}
defaultArchFeatureMap[os][arch] = features
}
// An Arch indicates a single CPU architecture.
type Arch struct {
ArchType ArchType
ArchVariant string
CpuVariant string
Abi []string
ArchFeatures []string
Native bool
}
func (a Arch) String() string {
s := a.ArchType.String()
if a.ArchVariant != "" {
s += "_" + a.ArchVariant
}
if a.CpuVariant != "" {
s += "_" + a.CpuVariant
}
return s
}
type ArchType struct {
Name string
Field string
Multilib string
}
func newArch(name, multilib string) ArchType {
archType := ArchType{
Name: name,
Field: proptools.FieldNameForProperty(name),
Multilib: multilib,
}
archTypeList = append(archTypeList, archType)
return archType
}
func ArchTypeList() []ArchType {
return append([]ArchType(nil), archTypeList...)
}
func (a ArchType) String() string {
return a.Name
}
var _ encoding.TextMarshaler = ArchType{}
func (a ArchType) MarshalText() ([]byte, error) {
return []byte(strconv.Quote(a.String())), nil
}
var _ encoding.TextUnmarshaler = &ArchType{}
func (a *ArchType) UnmarshalText(text []byte) error {
if u, ok := archTypeMap[string(text)]; ok {
*a = u
return nil
}
return fmt.Errorf("unknown ArchType %q", text)
}
var BuildOs = func() OsType {
switch runtime.GOOS {
case "linux":
return Linux
case "darwin":
return Darwin
default:
panic(fmt.Sprintf("unsupported OS: %s", runtime.GOOS))
}
}()
var (
osTypeList []OsType
commonTargetMap = make(map[string]Target)
NoOsType OsType
Linux = NewOsType("linux_glibc", Host, false)
Darwin = NewOsType("darwin", Host, false)
LinuxBionic = NewOsType("linux_bionic", Host, false)
Windows = NewOsType("windows", HostCross, true)
Android = NewOsType("android", Device, false)
Fuchsia = NewOsType("fuchsia", Device, false)
osArchTypeMap = map[OsType][]ArchType{
Linux: []ArchType{X86, X86_64},
LinuxBionic: []ArchType{X86_64},
Darwin: []ArchType{X86_64},
Windows: []ArchType{X86, X86_64},
Android: []ArchType{Arm, Arm64, Mips, Mips64, X86, X86_64},
Fuchsia: []ArchType{Arm64, X86_64},
}
)
type OsType struct {
Name, Field string
Class OsClass
DefaultDisabled bool
}
type OsClass int
const (
Generic OsClass = iota
Device
Host
HostCross
)
func (class OsClass) String() string {
switch class {
case Generic:
return "generic"
case Device:
return "device"
case Host:
return "host"
case HostCross:
return "host cross"
default:
panic(fmt.Errorf("unknown class %d", class))
}
}
func (os OsType) String() string {
return os.Name
}
func (os OsType) Bionic() bool {
return os == Android || os == LinuxBionic
}
func (os OsType) Linux() bool {
return os == Android || os == Linux || os == LinuxBionic
}
func NewOsType(name string, class OsClass, defDisabled bool) OsType {
os := OsType{
Name: name,
Field: strings.Title(name),
Class: class,
DefaultDisabled: defDisabled,
}
osTypeList = append(osTypeList, os)
if _, found := commonTargetMap[name]; found {
panic(fmt.Errorf("Found Os type duplicate during OsType registration: %q", name))
} else {
commonTargetMap[name] = Target{Os: os, Arch: Arch{ArchType: Common}}
}
return os
}
func osByName(name string) OsType {
for _, os := range osTypeList {
if os.Name == name {
return os
}
}
return NoOsType
}
type Target struct {
Os OsType
Arch Arch
}
func (target Target) String() string {
return target.Os.String() + "_" + target.Arch.String()
}
// archMutator splits a module into a variant for each Target requested by the module. Target selection
// for a module is in three levels, OsClass, mulitlib, and then Target.
// OsClass selection is determined by:
// - The HostOrDeviceSupported value passed in to InitAndroidArchModule by the module type factory, which selects
// whether the module type can compile for host, device or both.
// - The host_supported and device_supported properties on the module.
// If host is supported for the module, the Host and HostCross OsClasses are are selected. If device is supported
// for the module, the Device OsClass is selected.
// Within each selected OsClass, the multilib selection is determined by:
// - The compile_multilib property if it set (which may be overriden by target.android.compile_multlib or
// target.host.compile_multilib).
// - The default multilib passed to InitAndroidArchModule if compile_multilib was not set.
// Valid multilib values include:
// "both": compile for all Targets supported by the OsClass (generally x86_64 and x86, or arm64 and arm).
// "first": compile for only a single preferred Target supported by the OsClass. This is generally x86_64 or arm64,
// but may be arm for a 32-bit only build or a build with TARGET_PREFER_32_BIT=true set.
// "32": compile for only a single 32-bit Target supported by the OsClass.
// "64": compile for only a single 64-bit Target supported by the OsClass.
// "common": compile a for a single Target that will work on all Targets suported by the OsClass (for example Java).
//
// Once the list of Targets is determined, the module is split into a variant for each Target.
//
// Modules can be initialized with InitAndroidMultiTargetsArchModule, in which case they will be split by OsClass,
// but will have a common Target that is expected to handle all other selected Targets via ctx.MultiTargets().
func archMutator(mctx BottomUpMutatorContext) {
var module Module
var ok bool
if module, ok = mctx.Module().(Module); !ok {
return
}
base := module.base()
if !base.ArchSpecific() {
return
}
var moduleTargets []Target
moduleMultiTargets := make(map[int][]Target)
primaryModules := make(map[int]bool)
osClasses := base.OsClassSupported()
for _, os := range osTypeList {
supportedClass := false
for _, osClass := range osClasses {
if os.Class == osClass {
supportedClass = true
}
}
if !supportedClass {
continue
}
osTargets := mctx.Config().Targets[os]
if len(osTargets) == 0 {
continue
}
// only the primary arch in the recovery partition
if os == Android && module.InstallInRecovery() {
osTargets = []Target{osTargets[0]}
}
prefer32 := false
if base.prefer32 != nil {
prefer32 = base.prefer32(mctx, base, os.Class)
}
multilib, extraMultilib := decodeMultilib(base, os.Class)
targets, err := decodeMultilibTargets(multilib, osTargets, prefer32)
if err != nil {
mctx.ModuleErrorf("%s", err.Error())
}
var multiTargets []Target
if extraMultilib != "" {
multiTargets, err = decodeMultilibTargets(extraMultilib, osTargets, prefer32)
if err != nil {
mctx.ModuleErrorf("%s", err.Error())
}
}
if len(targets) > 0 {
primaryModules[len(moduleTargets)] = true
moduleMultiTargets[len(moduleTargets)] = multiTargets
moduleTargets = append(moduleTargets, targets...)
}
}
if len(moduleTargets) == 0 {
base.commonProperties.Enabled = boolPtr(false)
return
}
targetNames := make([]string, len(moduleTargets))
for i, target := range moduleTargets {
targetNames[i] = target.String()
}
modules := mctx.CreateVariations(targetNames...)
for i, m := range modules {
m.(Module).base().SetTarget(moduleTargets[i], moduleMultiTargets[i], primaryModules[i])
m.(Module).base().setArchProperties(mctx)
}
}
func decodeMultilib(base *ModuleBase, class OsClass) (multilib, extraMultilib string) {
switch class {
case Device:
multilib = String(base.commonProperties.Target.Android.Compile_multilib)
case Host, HostCross:
multilib = String(base.commonProperties.Target.Host.Compile_multilib)
}
if multilib == "" {
multilib = String(base.commonProperties.Compile_multilib)
}
if multilib == "" {
multilib = base.commonProperties.Default_multilib
}
if base.commonProperties.UseTargetVariants {
return multilib, ""
} else {
// For app modules a single arch variant will be created per OS class which is expected to handle all the
// selected arches. Return the common-type as multilib and any Android.bp provided multilib as extraMultilib
if multilib == base.commonProperties.Default_multilib {
multilib = "first"
}
return base.commonProperties.Default_multilib, multilib
}
}
func filterArchStructFields(fields []reflect.StructField) (filteredFields []reflect.StructField, filtered bool) {
for _, field := range fields {
if !proptools.HasTag(field, "android", "arch_variant") {
filtered = true
continue
}
// The arch_variant field isn't necessary past this point
// Instead of wasting space, just remove it. Go also has a
// 16-bit limit on structure name length. The name is constructed
// based on the Go source representation of the structure, so
// the tag names count towards that length.
//
// TODO: handle the uncommon case of other tags being involved
if field.Tag == `android:"arch_variant"` {
field.Tag = ""
}
// Recurse into structs
switch field.Type.Kind() {
case reflect.Struct:
var subFiltered bool
field.Type, subFiltered = filterArchStruct(field.Type)
filtered = filtered || subFiltered
if field.Type == nil {
continue
}
case reflect.Ptr:
if field.Type.Elem().Kind() == reflect.Struct {
nestedType, subFiltered := filterArchStruct(field.Type.Elem())
filtered = filtered || subFiltered
if nestedType == nil {
continue
}
field.Type = reflect.PtrTo(nestedType)
}
case reflect.Interface:
panic("Interfaces are not supported in arch_variant properties")
}
filteredFields = append(filteredFields, field)
}
return filteredFields, filtered
}
// filterArchStruct takes a reflect.Type that is either a sturct or a pointer to a struct, and returns a reflect.Type
// that only contains the fields in the original type that have an `android:"arch_variant"` struct tag, and a bool
// that is true if the new struct type has fewer fields than the original type. If there are no fields in the
// original type with the struct tag it returns nil and true.
func filterArchStruct(prop reflect.Type) (filteredProp reflect.Type, filtered bool) {
var fields []reflect.StructField
ptr := prop.Kind() == reflect.Ptr
if ptr {
prop = prop.Elem()
}
for i := 0; i < prop.NumField(); i++ {
fields = append(fields, prop.Field(i))
}
filteredFields, filtered := filterArchStructFields(fields)
if len(filteredFields) == 0 {
return nil, true
}
if !filtered {
if ptr {
return reflect.PtrTo(prop), false
}
return prop, false
}
ret := reflect.StructOf(filteredFields)
if ptr {
ret = reflect.PtrTo(ret)
}
return ret, true
}
// filterArchStruct takes a reflect.Type that is either a sturct or a pointer to a struct, and returns a list of
// reflect.Type that only contains the fields in the original type that have an `android:"arch_variant"` struct tag,
// and a bool that is true if the new struct type has fewer fields than the original type. If there are no fields in
// the original type with the struct tag it returns nil and true. Each returned struct type will have a maximum of
// 10 top level fields in it to attempt to avoid hitting the reflect.StructOf name length limit, although the limit
// can still be reached with a single struct field with many fields in it.
func filterArchStructSharded(prop reflect.Type) (filteredProp []reflect.Type, filtered bool) {
var fields []reflect.StructField
ptr := prop.Kind() == reflect.Ptr
if ptr {
prop = prop.Elem()
}
for i := 0; i < prop.NumField(); i++ {
fields = append(fields, prop.Field(i))
}
fields, filtered = filterArchStructFields(fields)
if !filtered {
if ptr {
return []reflect.Type{reflect.PtrTo(prop)}, false
}
return []reflect.Type{prop}, false
}
if len(fields) == 0 {
return nil, true
}
shards := shardFields(fields, 10)
for _, shard := range shards {
s := reflect.StructOf(shard)
if ptr {
s = reflect.PtrTo(s)
}
filteredProp = append(filteredProp, s)
}
return filteredProp, true
}
func shardFields(fields []reflect.StructField, shardSize int) [][]reflect.StructField {
ret := make([][]reflect.StructField, 0, (len(fields)+shardSize-1)/shardSize)
for len(fields) > shardSize {
ret = append(ret, fields[0:shardSize])
fields = fields[shardSize:]
}
if len(fields) > 0 {
ret = append(ret, fields)
}
return ret
}
// createArchType takes a reflect.Type that is either a struct or a pointer to a struct, and returns a list of
// reflect.Type that contains the arch-variant properties inside structs for each architecture, os, target, multilib,
// etc.
func createArchType(props reflect.Type) []reflect.Type {
propShards, _ := filterArchStructSharded(props)
if len(propShards) == 0 {
return nil
}
var ret []reflect.Type
for _, props := range propShards {
variantFields := func(names []string) []reflect.StructField {
ret := make([]reflect.StructField, len(names))
for i, name := range names {
ret[i].Name = name
ret[i].Type = props
}
return ret
}
archFields := make([]reflect.StructField, len(archTypeList))
for i, arch := range archTypeList {
variants := []string{}
for _, archVariant := range archVariants[arch] {
archVariant := variantReplacer.Replace(archVariant)
variants = append(variants, proptools.FieldNameForProperty(archVariant))
}