-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
1092 lines (927 loc) · 25.4 KB
/
main.go
File metadata and controls
1092 lines (927 loc) · 25.4 KB
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
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"github.com/AndroidGoLab/binder/binder"
"github.com/AndroidGoLab/binder/tools/pkg/codegen"
"github.com/AndroidGoLab/binder/tools/pkg/parser"
"github.com/AndroidGoLab/binder/tools/pkg/resolver"
"github.com/AndroidGoLab/binder/tools/pkg/spec"
)
// apiLevelMajorVersion maps Android API levels to the major.minor.patch
// prefix used in AOSP tag names (e.g. "android-16.0.0_r4").
var apiLevelMajorVersion = map[int]string{
34: "14.0.0",
35: "15.0.0",
36: "16.0.0",
}
// submoduleNames lists the 3rdparty submodule directory basenames.
var submoduleNames = []string{
"frameworks-base",
"frameworks-native",
"frameworks-hardware-interfaces",
"frameworks-av",
"hardware-interfaces",
"system-hardware-interfaces",
"system-netd",
"system-connectivity-wificond",
"packages-modules-bluetooth",
}
type searchPathsFlag []string
func (s *searchPathsFlag) String() string {
return strings.Join(*s, ",")
}
func (s *searchPathsFlag) Set(value string) error {
*s = append(*s, value)
return nil
}
func main() {
thirdpartyDir := flag.String("3rdparty", "", "Path to the 3rdparty directory containing AOSP submodules")
outputDir := flag.String("output", "specs/", "Output directory for spec files")
fetchVersions := flag.Bool("versions", false, "Fetch AOSP tags and embed multi-version transaction codes")
defaultAPI := flag.Int("default-api", 36, "API level for the local version entry")
baseline3rdparty := flag.String("baseline-3rdparty", "", "Path to the baseline 3rdparty directory for param version diffing")
baselineAPI := flag.Int("baseline-api", 35, "API level of the baseline 3rdparty sources")
var searchPaths searchPathsFlag
flag.Var(&searchPaths, "I", "Search path for AIDL imports (can be repeated)")
flag.Parse()
positionalFiles := flag.Args()
if err := run(
*thirdpartyDir,
*outputDir,
*fetchVersions,
*defaultAPI,
*baseline3rdparty,
*baselineAPI,
searchPaths,
positionalFiles,
); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
func run(
thirdpartyDir string,
outputDir string,
fetchVersions bool,
defaultAPI int,
baseline3rdparty string,
baselineAPI int,
searchPaths []string,
positionalFiles []string,
) error {
if len(positionalFiles) > 0 {
return runExplicitFiles(outputDir, searchPaths, positionalFiles)
}
if thirdpartyDir == "" {
return fmt.Errorf("-3rdparty flag is required in discovery mode")
}
return runDiscovery(
thirdpartyDir,
outputDir,
fetchVersions,
defaultAPI,
baseline3rdparty,
baselineAPI,
)
}
func runExplicitFiles(
outputDir string,
searchPaths []string,
files []string,
) error {
if len(searchPaths) == 0 {
return fmt.Errorf("no search paths specified; use -I <search-path>")
}
r := resolver.New(searchPaths)
r.SetSkipUnresolved(true)
for _, f := range files {
if err := r.ResolveFile(f); err != nil {
fmt.Fprintf(os.Stderr, "Warning: resolving %s: %v\n", f, err)
continue
}
}
allDefs := r.Registry.All()
specs := convertToSpecs(allDefs)
return spec.WriteAllSpecs(outputDir, specs)
}
func runDiscovery(
thirdpartyDir string,
outputDir string,
fetchVersions bool,
defaultAPI int,
baseline3rdparty string,
baselineAPI int,
) error {
absThirdparty, err := filepath.Abs(thirdpartyDir)
if err != nil {
return fmt.Errorf("resolving 3rdparty path: %w", err)
}
if _, err := os.Stat(absThirdparty); os.IsNotExist(err) {
return fmt.Errorf("3rdparty directory not found: %s", absThirdparty)
}
fmt.Fprintf(os.Stderr, "Discovering AIDL files in %s...\n", absThirdparty)
aidlFiles, err := discoverAIDLFiles(absThirdparty)
if err != nil {
return fmt.Errorf("discovering AIDL files: %w", err)
}
fmt.Fprintf(os.Stderr, "Found %d AIDL files\n", len(aidlFiles))
fmt.Fprintf(os.Stderr, "Discovering search roots...\n")
searchRoots, err := discoverSearchRoots(aidlFiles)
if err != nil {
return fmt.Errorf("discovering search roots: %w", err)
}
fmt.Fprintf(os.Stderr, "Found %d search roots\n", len(searchRoots))
r := resolver.New(searchRoots)
r.SetSkipUnresolved(true)
var parseFailCount int
var resolvedCount int
for _, f := range aidlFiles {
if err := r.ResolveFile(f); err != nil {
parseFailCount++
continue
}
resolvedCount++
}
fmt.Fprintf(os.Stderr, "Resolved %d files (%d parse failures)\n", resolvedCount, parseFailCount)
allDefs := r.Registry.All()
fmt.Fprintf(os.Stderr, "Total definitions: %d\n", len(allDefs))
specs := convertToSpecs(allDefs)
if fetchVersions {
if err := embedVersionCodes(absThirdparty, defaultAPI, specs); err != nil {
return fmt.Errorf("embedding version codes: %w", err)
}
}
if baseline3rdparty != "" {
if err := diffBaselineParams(baseline3rdparty, baselineAPI, defaultAPI, specs); err != nil {
return fmt.Errorf("diffing baseline params: %w", err)
}
}
if err := spec.WriteAllSpecs(outputDir, specs); err != nil {
return fmt.Errorf("writing specs: %w", err)
}
fmt.Fprintf(os.Stderr, "Wrote %d spec files to %s\n", len(specs), outputDir)
return nil
}
// convertToSpecs converts the resolver's type registry into spec.PackageSpec
// grouped by Go package.
func convertToSpecs(
allDefs map[string]parser.Definition,
) map[string]*spec.PackageSpec {
specs := map[string]*spec.PackageSpec{}
// Process definitions in sorted order for deterministic output.
qualifiedNames := make([]string, 0, len(allDefs))
for qn := range allDefs {
qualifiedNames = append(qualifiedNames, qn)
}
sort.Strings(qualifiedNames)
for _, qualifiedName := range qualifiedNames {
def := allDefs[qualifiedName]
aidlPkg, typeName := splitQualifiedName(qualifiedName)
if aidlPkg == "" {
continue
}
goPkg := codegen.AIDLToGoPackage(aidlPkg)
ps, ok := specs[goPkg]
if !ok {
ps = &spec.PackageSpec{
AIDLPackage: aidlPkg,
GoPackage: goPkg,
}
specs[goPkg] = ps
}
convertDefinition(ps, typeName, qualifiedName, def)
}
// Sort all slices within each package spec for deterministic output.
for _, ps := range specs {
sortPackageSpec(ps)
}
return specs
}
// splitQualifiedName splits "android.os.IServiceManager" into
// ("android.os", "IServiceManager"). For nested types like
// "android.os.IServiceManager.InnerType", returns ("android.os", "IServiceManager.InnerType").
func splitQualifiedName(
qualifiedName string,
) (string, string) {
parts := strings.Split(qualifiedName, ".")
if len(parts) < 2 {
return "", qualifiedName
}
// Find the split point: the first segment that starts with an uppercase letter
// is the type name boundary. Everything before it is the package.
for i := 1; i < len(parts); i++ {
if len(parts[i]) > 0 && parts[i][0] >= 'A' && parts[i][0] <= 'Z' {
return strings.Join(parts[:i], "."), strings.Join(parts[i:], ".")
}
}
// Fallback: last segment is the type name.
return strings.Join(parts[:len(parts)-1], "."), parts[len(parts)-1]
}
func convertDefinition(
ps *spec.PackageSpec,
typeName string,
qualifiedName string,
def parser.Definition,
) {
switch d := def.(type) {
case *parser.InterfaceDecl:
iface := convertInterface(typeName, qualifiedName, d)
ps.Interfaces = append(ps.Interfaces, iface)
case *parser.ParcelableDecl:
parc := convertParcelable(typeName, d)
ps.Parcelables = append(ps.Parcelables, parc)
case *parser.EnumDecl:
enum := convertEnum(typeName, d)
ps.Enums = append(ps.Enums, enum)
case *parser.UnionDecl:
union := convertUnion(typeName, d)
ps.Unions = append(ps.Unions, union)
}
}
func convertInterface(
typeName string,
qualifiedName string,
iface *parser.InterfaceDecl,
) spec.InterfaceSpec {
is := spec.InterfaceSpec{
Name: typeName,
Descriptor: qualifiedName,
Oneway: iface.Oneway,
Annotations: collectAnnotationNames(iface.Annots),
}
codes := codegen.ComputeTransactionCodes(iface.Methods)
for _, m := range iface.Methods {
ms := convertMethod(m, codes)
is.Methods = append(is.Methods, ms)
}
for _, c := range iface.Constants {
cs := convertConstant(c)
is.Constants = append(is.Constants, cs)
}
// Record nested type names for reference.
for _, nd := range iface.NestedTypes {
is.NestedTypes = append(is.NestedTypes, typeName+"."+nd.GetName())
}
return is
}
func convertMethod(
m *parser.MethodDecl,
codes map[string]binder.TransactionCode,
) spec.MethodSpec {
ms := spec.MethodSpec{
Name: m.MethodName,
Oneway: m.Oneway,
}
if code, ok := codes[m.MethodName]; ok {
ms.TransactionCodeOffset = int(code - binder.FirstCallTransaction)
}
if m.ReturnType != nil {
ms.ReturnType = convertTypeRef(m.ReturnType)
}
for _, p := range m.Params {
ps := convertParam(p)
ms.Params = append(ms.Params, ps)
}
ms.Annotations = collectAnnotationNames(m.Annots)
return ms
}
func convertParam(
p *parser.ParamDecl,
) spec.ParamSpec {
ps := spec.ParamSpec{
Name: p.ParamName,
Type: convertTypeRef(p.Type),
}
switch p.Direction {
case parser.DirectionIn:
ps.Direction = spec.DirectionIn
case parser.DirectionOut:
ps.Direction = spec.DirectionOut
case parser.DirectionInOut:
ps.Direction = spec.DirectionInOut
}
ps.Annotations = collectAnnotationNames(p.Annots)
return ps
}
func convertTypeRef(
ts *parser.TypeSpecifier,
) spec.TypeRef {
if ts == nil {
return spec.TypeRef{}
}
tr := spec.TypeRef{
Name: ts.Name,
IsArray: ts.IsArray,
FixedSize: ts.FixedSize,
IsNullable: hasAnnotation(ts.Annots, "nullable"),
}
// Collect type-level annotations other than @nullable.
for _, a := range ts.Annots {
if a.Name == "nullable" {
continue
}
tr.Annotations = append(tr.Annotations, a.Name)
}
for _, arg := range ts.TypeArgs {
tr.TypeArgs = append(tr.TypeArgs, convertTypeRef(arg))
}
return tr
}
func convertParcelable(
typeName string,
d *parser.ParcelableDecl,
) spec.ParcelableSpec {
ps := spec.ParcelableSpec{
Name: typeName,
Annotations: collectAnnotationNames(d.Annots),
}
for _, f := range d.Fields {
ps.Fields = append(ps.Fields, convertField(f))
}
for _, c := range d.Constants {
ps.Constants = append(ps.Constants, convertConstant(c))
}
// Record nested type names for reference.
for _, nd := range d.NestedTypes {
ps.NestedTypes = append(ps.NestedTypes, typeName+"."+nd.GetName())
}
return ps
}
func convertField(
f *parser.FieldDecl,
) spec.FieldSpec {
fs := spec.FieldSpec{
Name: f.FieldName,
Type: convertTypeRef(f.Type),
Annotations: collectAnnotationNames(f.Annots),
}
if f.DefaultValue != nil {
fs.DefaultValue = constExprToString(f.DefaultValue)
}
return fs
}
func convertEnum(
typeName string,
d *parser.EnumDecl,
) spec.EnumSpec {
es := spec.EnumSpec{
Name: typeName,
Annotations: collectAnnotationNames(d.Annots),
}
if d.BackingType != nil {
es.BackingType = d.BackingType.Name
}
for _, e := range d.Enumerators {
ev := spec.EnumeratorSpec{
Name: e.Name,
}
if e.Value != nil {
ev.Value = constExprToString(e.Value)
}
es.Values = append(es.Values, ev)
}
return es
}
func convertUnion(
typeName string,
d *parser.UnionDecl,
) spec.UnionSpec {
us := spec.UnionSpec{
Name: typeName,
Annotations: collectAnnotationNames(d.Annots),
}
for _, f := range d.Fields {
us.Fields = append(us.Fields, convertField(f))
}
for _, c := range d.Constants {
us.Constants = append(us.Constants, convertConstant(c))
}
// Record nested type names for reference.
for _, nd := range d.NestedTypes {
us.NestedTypes = append(us.NestedTypes, typeName+"."+nd.GetName())
}
return us
}
func convertConstant(
c *parser.ConstantDecl,
) spec.ConstantSpec {
cs := spec.ConstantSpec{
Name: c.ConstName,
}
if c.Type != nil {
cs.Type = c.Type.Name
}
if c.Value != nil {
cs.Value = constExprToString(c.Value)
}
return cs
}
// constExprToString renders a ConstExpr back to its string representation.
// Binary sub-expressions are wrapped in parentheses to preserve operator
// precedence during round-trip through spec2go's parseConstExpr.
func constExprToString(
expr parser.ConstExpr,
) string {
if expr == nil {
return ""
}
switch e := expr.(type) {
case *parser.IntegerLiteral:
return e.Value
case *parser.FloatLiteral:
return e.Value
case *parser.StringLiteralExpr:
return `"` + e.Value + `"`
case *parser.CharLiteralExpr:
return "'" + e.Value + "'"
case *parser.BoolLiteral:
if e.Value {
return "true"
}
return "false"
case *parser.NullLiteral:
return "null"
case *parser.IdentExpr:
return e.Name
case *parser.UnaryExpr:
operand := constExprToString(e.Operand)
// Wrap compound operands in parentheses so the unary binds tightly.
if needsParens(e.Operand) {
operand = "(" + operand + ")"
}
return e.Op.String() + operand
case *parser.BinaryExpr:
left := constExprToString(e.Left)
right := constExprToString(e.Right)
// Wrap binary/ternary sub-expressions in parentheses to
// preserve precedence across a serialize/parse round-trip.
if needsParens(e.Left) {
left = "(" + left + ")"
}
if needsParens(e.Right) {
right = "(" + right + ")"
}
return left + " " + e.Op.String() + " " + right
case *parser.TernaryExpr:
cond := constExprToString(e.Cond)
then := constExprToString(e.Then)
elseStr := constExprToString(e.Else)
if needsParens(e.Cond) {
cond = "(" + cond + ")"
}
if needsParens(e.Then) {
then = "(" + then + ")"
}
if needsParens(e.Else) {
elseStr = "(" + elseStr + ")"
}
return cond + " ? " + then + " : " + elseStr
default:
return fmt.Sprintf("%v", expr)
}
}
// needsParens returns true if the expression is compound (binary or ternary)
// and should be wrapped in parentheses when used as a sub-expression.
func needsParens(
expr parser.ConstExpr,
) bool {
switch expr.(type) {
case *parser.BinaryExpr, *parser.TernaryExpr:
return true
}
return false
}
func hasAnnotation(
annots []*parser.Annotation,
name string,
) bool {
for _, a := range annots {
if a.Name == name {
return true
}
}
return false
}
func collectAnnotationNames(
annots []*parser.Annotation,
) []string {
if len(annots) == 0 {
return nil
}
names := make([]string, 0, len(annots))
for _, a := range annots {
names = append(names, a.Name)
}
return names
}
func sortPackageSpec(
ps *spec.PackageSpec,
) {
sort.Slice(ps.Interfaces, func(i, j int) bool {
return ps.Interfaces[i].Name < ps.Interfaces[j].Name
})
sort.Slice(ps.Parcelables, func(i, j int) bool {
return ps.Parcelables[i].Name < ps.Parcelables[j].Name
})
sort.Slice(ps.Enums, func(i, j int) bool {
return ps.Enums[i].Name < ps.Enums[j].Name
})
sort.Slice(ps.Unions, func(i, j int) bool {
return ps.Unions[i].Name < ps.Unions[j].Name
})
}
// --- Discovery functions (shared with genaidl) ---
// discoverAIDLFiles walks the directory tree and returns all .aidl files,
// excluding versioned aidl_api snapshot directories.
func discoverAIDLFiles(
rootDir string,
) ([]string, error) {
var files []string
err := filepath.Walk(rootDir, func(
path string,
info os.FileInfo,
err error,
) error {
if err != nil {
return nil
}
if info.IsDir() {
if info.Name() == "aidl_api" {
return filepath.SkipDir
}
return nil
}
if strings.HasSuffix(path, ".aidl") {
files = append(files, path)
}
return nil
})
return files, err
}
// discoverSearchRoots determines import root directories by analyzing
// package declarations in AIDL files and inferring the root from the
// file's path relative to the package structure.
func discoverSearchRoots(
aidlFiles []string,
) ([]string, error) {
rootSet := make(map[string]bool)
for _, f := range aidlFiles {
root, err := inferSearchRoot(f)
if err != nil {
continue
}
if root != "" {
rootSet[root] = true
}
}
roots := make([]string, 0, len(rootSet))
for r := range rootSet {
roots = append(roots, r)
}
return roots, nil
}
// inferSearchRoot parses an AIDL file's package declaration and computes
// the search root directory by stripping the package-derived path suffix
// from the file's directory.
func inferSearchRoot(
filePath string,
) (string, error) {
doc, err := parser.ParseFile(filePath)
if err != nil {
return "", err
}
if doc.Package == nil || doc.Package.Name == "" {
return "", nil
}
pkgPath := strings.ReplaceAll(doc.Package.Name, ".", string(filepath.Separator))
dir := filepath.Dir(filePath)
if !strings.HasSuffix(dir, pkgPath) {
return "", nil
}
root := strings.TrimSuffix(dir, pkgPath)
root = strings.TrimRight(root, string(filepath.Separator))
if root == "" {
return "", nil
}
return root, nil
}
// --- Version codes embedding ---
func embedVersionCodes(
absThirdparty string,
defaultAPI int,
specs map[string]*spec.PackageSpec,
) error {
apiLevels := sortedAPILevels()
allTables := map[string]map[string]map[string]binder.TransactionCode{}
apiRevisions := map[int][]string{}
if err := fetchAOSPVersionTables(
absThirdparty,
apiLevels,
allTables,
apiRevisions,
); err != nil {
return err
}
// Add a local entry from the current 3rdparty state.
localVersionID := fmt.Sprintf("%d.local", defaultAPI)
fmt.Fprintf(os.Stderr, "Parsing current 3rdparty state as %s...\n", localVersionID)
localTable, err := parseVersionTable(absThirdparty)
if err != nil {
return fmt.Errorf("parsing local version table: %w", err)
}
allTables[localVersionID] = localTable
apiRevisions[defaultAPI] = append([]string{localVersionID}, apiRevisions[defaultAPI]...)
// Embed version codes into interface specs.
for _, ps := range specs {
for i := range ps.Interfaces {
iface := &ps.Interfaces[i]
versionCodes := buildVersionCodesForInterface(iface.Descriptor, allTables)
if len(versionCodes) > 0 {
iface.VersionCodes = versionCodes
}
}
}
return nil
}
// buildVersionCodesForInterface builds the version_codes map for a single
// interface descriptor from the collected version tables.
func buildVersionCodesForInterface(
descriptor string,
allTables map[string]map[string]map[string]binder.TransactionCode,
) map[string]map[string]int {
result := map[string]map[string]int{}
versionIDs := sortedKeys(allTables)
for _, vid := range versionIDs {
table := allTables[vid]
methods, ok := table[descriptor]
if !ok {
continue
}
offsets := map[string]int{}
for methodName, code := range methods {
offsets[methodName] = int(code - binder.FirstCallTransaction)
}
if len(offsets) > 0 {
result[vid] = offsets
}
}
return result
}
// --- AOSP tag fetching functions ---
// revisionTag represents a single AOSP revision tag.
type revisionTag struct {
APILevel int
Revision int
Tag string
}
func (r revisionTag) versionID() string {
return fmt.Sprintf("%d.r%d", r.APILevel, r.Revision)
}
func sortedAPILevels() []int {
levels := make([]int, 0, len(apiLevelMajorVersion))
for level := range apiLevelMajorVersion {
levels = append(levels, level)
}
sort.Ints(levels)
return levels
}
func discoverRevisionTags(
repoDir string,
apiLevels []int,
) (map[int][]revisionTag, error) {
result := make(map[int][]revisionTag, len(apiLevels))
tagRe := regexp.MustCompile(`refs/tags/(android-[\d.]+_r(\d+))$`)
for _, level := range apiLevels {
majorVersion, ok := apiLevelMajorVersion[level]
if !ok {
return nil, fmt.Errorf("no major version mapping for API %d", level)
}
pattern := fmt.Sprintf("android-%s_r*", majorVersion)
out, err := exec.Command("git", "-C", repoDir, "ls-remote", "--tags", "origin", pattern).Output()
if err != nil {
return nil, fmt.Errorf("ls-remote for API %d: %w", level, err)
}
var tags []revisionTag
for _, line := range strings.Split(string(out), "\n") {
matches := tagRe.FindStringSubmatch(line)
if matches == nil {
continue
}
tag := matches[1]
rev, err := strconv.Atoi(matches[2])
if err != nil {
continue
}
tags = append(tags, revisionTag{
APILevel: level,
Revision: rev,
Tag: tag,
})
}
if len(tags) == 0 {
return nil, fmt.Errorf("no tags found for API %d (pattern %s)", level, pattern)
}
result[level] = tags
fmt.Fprintf(os.Stderr, "API %d: found %d revision tags\n", level, len(tags))
}
return result, nil
}
func fetchAOSPVersionTables(
absThirdparty string,
apiLevels []int,
allTables map[string]map[string]map[string]binder.TransactionCode,
apiRevisions map[int][]string,
) error {
submoduleDirs := make([]string, len(submoduleNames))
for i, name := range submoduleNames {
submoduleDirs[i] = filepath.Join(absThirdparty, name)
}
originalCommits, err := saveCurrentCommits(submoduleDirs)
if err != nil {
return fmt.Errorf("saving current commits: %w", err)
}
defer restoreCommits(submoduleDirs, originalCommits)
allRevTags, err := discoverRevisionTags(submoduleDirs[0], apiLevels)
if err != nil {
return fmt.Errorf("discovering revision tags: %w", err)
}
for _, level := range apiLevels {
tags := allRevTags[level]
if len(tags) == 0 {
return fmt.Errorf("no revision tags found for API %d", level)
}
sort.Slice(tags, func(i, j int) bool {
return tags[i].Revision < tags[j].Revision
})
var prevTable map[string]map[string]binder.TransactionCode
var prevVersionID string
var distinctVersions []string
for _, rt := range tags {
fmt.Fprintf(os.Stderr, "Fetching API %d revision r%d (tag %s)...\n", level, rt.Revision, rt.Tag)
if err := checkoutTag(submoduleDirs, rt.Tag); err != nil {
return fmt.Errorf("checking out tag %s for API %d r%d: %w", rt.Tag, level, rt.Revision, err)
}
table, err := parseVersionTable(absThirdparty)
if err != nil {
return fmt.Errorf("parsing API %d r%d: %w", level, rt.Revision, err)
}
vid := rt.versionID()
if prevTable != nil && tablesEqual(prevTable, table) {
fmt.Fprintf(os.Stderr, " -> same as %s, skipping\n", prevVersionID)
continue
}
allTables[vid] = table
distinctVersions = append(distinctVersions, vid)
prevTable = table
prevVersionID = vid
fmt.Fprintf(os.Stderr, "API %d r%d: %d interfaces (distinct)\n", level, rt.Revision, len(table))
}
// Store revisions latest-first for probing (most likely match).
reversed := make([]string, len(distinctVersions))
for i, v := range distinctVersions {
reversed[len(distinctVersions)-1-i] = v
}
apiRevisions[level] = reversed
}
return nil
}
func tablesEqual(
a, b map[string]map[string]binder.TransactionCode,
) bool {
if len(a) != len(b) {
return false
}
for desc, aMethods := range a {
bMethods, ok := b[desc]
if !ok {
return false
}
if len(aMethods) != len(bMethods) {
return false
}
for name, aCode := range aMethods {
if bMethods[name] != aCode {
return false
}
}
}
return true
}
func saveCurrentCommits(
dirs []string,
) (map[string]string, error) {
commits := make(map[string]string, len(dirs))
for _, dir := range dirs {
out, err := exec.Command("git", "-C", dir, "rev-parse", "HEAD").Output()
if err != nil {
return nil, fmt.Errorf("rev-parse HEAD in %s: %w", dir, err)
}
commits[dir] = strings.TrimSpace(string(out))
}
return commits, nil
}
func restoreCommits(
dirs []string,
commits map[string]string,
) {
for _, dir := range dirs {
commit, ok := commits[dir]
if !ok {
continue
}
cmd := exec.Command("git", "-C", dir, "checkout", commit)
cmd.Stderr = os.Stderr
_ = cmd.Run()
}
}
func checkoutTag(
dirs []string,
tag string,
) error {
for _, dir := range dirs {