-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
513 lines (444 loc) · 13.9 KB
/
main.go
File metadata and controls
513 lines (444 loc) · 13.9 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
// java2spec extracts information from Java source files and merges it
// into YAML spec files previously generated by aidl2spec. It replaces
// the standalone genservicemap, genparcelspec, and genconstants tools.
//
// Usage:
//
// java2spec -3rdparty tools/pkg/3rdparty -config tools/cmd/java2spec/constants.yaml -output specs/
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"sync"
"sync/atomic"
"gopkg.in/yaml.v3"
"github.com/AndroidGoLab/binder/tools/pkg/codegen"
"github.com/AndroidGoLab/binder/tools/pkg/parcelspec"
"github.com/AndroidGoLab/binder/tools/pkg/servicemap"
"github.com/AndroidGoLab/binder/tools/pkg/spec"
)
func main() {
thirdpartyDir := flag.String("3rdparty", "", "Path to the 3rdparty directory containing AOSP submodules")
configPath := flag.String("config", "", "Path to the constants YAML config file")
outputDir := flag.String("output", "specs/", "Output directory for spec files (must already contain aidl2spec output)")
flag.Parse()
if err := run(*thirdpartyDir, *configPath, *outputDir); err != nil {
fmt.Fprintf(os.Stderr, "java2spec: error: %v\n", err)
os.Exit(1)
}
}
func run(
thirdpartyDir string,
configPath string,
outputDir string,
) error {
if thirdpartyDir == "" {
return fmt.Errorf("-3rdparty flag is required")
}
absThirdparty, err := filepath.Abs(thirdpartyDir)
if err != nil {
return fmt.Errorf("resolving 3rdparty path: %w", err)
}
frameworksBase := filepath.Join(absThirdparty, "frameworks-base")
// Read existing specs produced by aidl2spec.
specs, err := spec.ReadAllSpecs(outputDir)
if err != nil {
return fmt.Errorf("reading existing specs: %w", err)
}
fmt.Fprintf(os.Stderr, "java2spec: loaded %d existing package specs\n", len(specs))
if err := mergeServiceMappings(frameworksBase, specs); err != nil {
return fmt.Errorf("service mappings: %w", err)
}
// Walk the entire 3rdparty directory for Java Parcelables, not just
// frameworks-base. APEX modules (Bluetooth, WiFi, Tethering, …) ship
// their own Java Parcelables in separate directories.
if err := mergeJavaWireFormats(absThirdparty, specs); err != nil {
return fmt.Errorf("java wire formats: %w", err)
}
if configPath != "" {
if err := mergeJavaConstants(frameworksBase, configPath, specs); err != nil {
return fmt.Errorf("java constants: %w", err)
}
}
if err := spec.WriteAllSpecs(outputDir, specs); err != nil {
return fmt.Errorf("writing specs: %w", err)
}
fmt.Fprintf(os.Stderr, "java2spec: wrote %d package specs to %s\n", len(specs), outputDir)
return nil
}
// mergeServiceMappings extracts service name -> AIDL descriptor mappings from
// Context.java + SystemServiceRegistry.java and writes them to the
// servicemanager package spec. It also includes all _SERVICE constants from
// Context.java that lack a SystemServiceRegistry match (with empty descriptor)
// so that the generated ServiceName constants cover every well-known service.
func mergeServiceMappings(
frameworksBase string,
specs map[string]*spec.PackageSpec,
) error {
svcMap, err := servicemap.BuildServiceMap(frameworksBase)
if err != nil {
return fmt.Errorf("building service map: %w", err)
}
// Read all _SERVICE constants from Context.java so we can include
// services that are not registered in SystemServiceRegistry.
contextPath := filepath.Join(frameworksBase, "core/java/android/content/Context.java")
contextSrc, err := os.ReadFile(contextPath)
if err != nil {
return fmt.Errorf("reading Context.java: %w", err)
}
allConstants := servicemap.ExtractContextConstants(string(contextSrc))
// Start with registry-matched entries (have descriptor).
seen := map[string]bool{}
mappings := make([]spec.ServiceMapping, 0, len(svcMap)+len(allConstants))
for _, entry := range svcMap {
mappings = append(mappings, spec.ServiceMapping{
ServiceName: entry.ServiceName,
ConstantName: entry.ConstantName,
Descriptor: entry.AIDLDescriptor,
})
seen[entry.ConstantName] = true
}
// Add remaining Context.java constants (no descriptor).
for constName, svcName := range allConstants {
if seen[constName] {
continue
}
mappings = append(mappings, spec.ServiceMapping{
ServiceName: svcName,
ConstantName: constName,
})
}
sort.Slice(mappings, func(i, j int) bool {
return mappings[i].ServiceName < mappings[j].ServiceName
})
const smPkg = "servicemanager"
ps := specs[smPkg]
if ps == nil {
ps = &spec.PackageSpec{
AIDLPackage: "android.os",
GoPackage: smPkg,
}
specs[smPkg] = ps
}
ps.Services = mappings
fmt.Fprintf(os.Stderr, "java2spec: merged %d service mappings into %s\n", len(mappings), smPkg)
return nil
}
// mergeJavaWireFormats walks Java sources in the 3rdparty directory tree,
// extracts writeToParcel wire format specs, and merges them into matching
// parcelables in the existing AIDL specs.
func mergeJavaWireFormats(
frameworksBase string,
specs map[string]*spec.PackageSpec,
) error {
// Build a lookup index: "android.location" + "Location" -> pointer to ParcelableSpec.
type parcelKey struct {
aidlPackage string
name string
}
index := map[parcelKey]*spec.ParcelableSpec{}
for _, ps := range specs {
for i := range ps.Parcelables {
key := parcelKey{
aidlPackage: ps.AIDLPackage,
name: ps.Parcelables[i].Name,
}
index[key] = &ps.Parcelables[i]
}
}
// Collect Java files containing writeToParcel.
type fileWork struct {
path string
packageName string
src []byte
}
var files []fileWork
err := filepath.Walk(frameworksBase, func(
path string,
info os.FileInfo,
err error,
) error {
if err != nil {
return err
}
if info.IsDir() || !strings.HasSuffix(path, ".java") {
return nil
}
packageName := extractJavaPackageName(path)
if packageName == "" {
return nil
}
src, readErr := os.ReadFile(path)
if readErr != nil {
return fmt.Errorf("reading %s: %w", path, readErr)
}
if !strings.Contains(string(src), "writeToParcel") {
return nil
}
files = append(files, fileWork{path: path, packageName: packageName, src: src})
return nil
})
if err != nil {
return fmt.Errorf("walking %s: %w", frameworksBase, err)
}
// Process in parallel with per-worker ANTLR extractors (not thread-safe).
work := make(chan fileWork, len(files))
for _, f := range files {
work <- f
}
close(work)
var merged atomic.Int64
var mu sync.Mutex
numWorkers := runtime.NumCPU()
var wg sync.WaitGroup
var firstErr atomic.Value
for range numWorkers {
wg.Add(1)
go func() {
defer wg.Done()
extractor := parcelspec.NewJavaExtractor()
for fw := range work {
javaSpecs := extractor.ExtractSpecs(string(fw.src), fw.packageName)
for _, js := range javaSpecs {
if len(js.Fields) == 0 {
continue
}
key := parcelKey{
aidlPackage: js.Package,
name: js.Type,
}
mu.Lock()
target, ok := index[key]
mu.Unlock()
if !ok {
continue
}
wireFields := convertFieldSpecs(js.Fields)
mu.Lock()
// Prefer the wire format with more fields — avoids
// overwriting an instance writeToParcel result with
// a shorter helper method's result.
if len(wireFields) > len(target.JavaWireFormat) {
target.JavaWireFormat = wireFields
}
mu.Unlock()
merged.Add(1)
}
}
}()
}
wg.Wait()
if v := firstErr.Load(); v != nil {
return v.(error)
}
fmt.Fprintf(os.Stderr, "java2spec: merged wire formats for %d parcelables\n", merged.Load())
return nil
}
// convertFieldSpecs converts parcelspec.FieldSpec entries to spec.JavaWireField,
// recursively converting Elements for repeated (loop) fields.
func convertFieldSpecs(fields []parcelspec.FieldSpec) []spec.JavaWireField {
result := make([]spec.JavaWireField, len(fields))
for i, f := range fields {
result[i] = spec.JavaWireField{
Name: f.Name,
WriteMethod: f.Type,
Condition: f.Condition,
DelegateType: f.DelegateType,
Elements: convertFieldSpecs(f.Elements),
}
}
return result
}
// constantSpec describes one set of Java constants to extract.
type constantSpec struct {
JavaFile string `yaml:"java_file"`
Pattern string `yaml:"pattern"`
GoPackage string `yaml:"go_package"`
GoType string `yaml:"go_type"`
GoOutput string `yaml:"go_output"`
NameTransform string `yaml:"name_transform"`
}
// mergeJavaConstants reads the constants config, extracts matching constants
// from Java sources, and adds them as JavaConstantGroup entries in the
// appropriate package specs.
func mergeJavaConstants(
frameworksBase string,
configPath string,
specs map[string]*spec.PackageSpec,
) error {
configData, err := os.ReadFile(configPath)
if err != nil {
return fmt.Errorf("reading config %s: %w", configPath, err)
}
var constSpecs []constantSpec
if err := yaml.Unmarshal(configData, &constSpecs); err != nil {
return fmt.Errorf("parsing config %s: %w", configPath, err)
}
for _, cs := range constSpecs {
if err := mergeOneConstantGroup(frameworksBase, cs, specs); err != nil {
return fmt.Errorf("constants for %s: %w", cs.GoType, err)
}
}
return nil
}
func mergeOneConstantGroup(
frameworksBase string,
cs constantSpec,
specs map[string]*spec.PackageSpec,
) error {
javaPath := filepath.Join(frameworksBase, cs.JavaFile)
src, err := os.ReadFile(javaPath)
if err != nil {
return fmt.Errorf("reading %s: %w", javaPath, err)
}
allConstants := servicemap.ExtractStringConstants(string(src))
matched := filterByPattern(allConstants, cs.Pattern)
if len(matched) == 0 {
return fmt.Errorf("no constants matching pattern %q found in %s", cs.Pattern, cs.JavaFile)
}
transform, err := parseNameTransform(cs.NameTransform)
if err != nil {
return fmt.Errorf("parsing name_transform %q: %w", cs.NameTransform, err)
}
values := make([]spec.JavaConstantValue, 0, len(matched))
for javaName, value := range matched {
values = append(values, spec.JavaConstantValue{
Name: transform(javaName),
Value: value,
})
}
sort.Slice(values, func(i, j int) bool {
return values[i].Name < values[j].Name
})
group := spec.JavaConstantGroup{
Name: cs.GoType,
GoType: cs.GoType,
Values: values,
}
// Determine the target package. First try deriving the Go package from
// the java_file path (most precise). Fall back to matching the short
// go_package name against existing specs.
aidlPkg := aidlPackageFromJavaFile(cs.JavaFile)
goPkg := spec.GoPackageFromAIDL(aidlPkg)
ps := specs[goPkg]
if ps == nil {
goPkg = findGoPackage(specs, cs.GoPackage)
ps = specs[goPkg]
}
if ps == nil {
ps = &spec.PackageSpec{
AIDLPackage: aidlPkg,
GoPackage: goPkg,
}
specs[goPkg] = ps
}
// Replace existing group with the same name, or append.
replaced := false
for i := range ps.JavaConstants {
if ps.JavaConstants[i].Name == group.Name {
ps.JavaConstants[i] = group
replaced = true
break
}
}
if !replaced {
ps.JavaConstants = append(ps.JavaConstants, group)
}
fmt.Fprintf(os.Stderr, "java2spec: merged %d %s constants into %s\n", len(values), cs.GoType, goPkg)
return nil
}
// findGoPackage searches existing specs for a Go package whose path ends
// with the given short package name. Returns the full Go package path,
// or the short name itself if no match is found.
func findGoPackage(
specs map[string]*spec.PackageSpec,
shortPkg string,
) string {
suffix := "/" + shortPkg
for goPkg := range specs {
if goPkg == shortPkg || strings.HasSuffix(goPkg, suffix) {
return goPkg
}
}
return shortPkg
}
// aidlPackageFromJavaFile extracts an AIDL-style package from a java_file
// path like "location/java/android/location/LocationManager.java" ->
// "android.location".
func aidlPackageFromJavaFile(javaFile string) string {
normalized := filepath.ToSlash(javaFile)
idx := strings.Index(normalized, "/java/")
if idx < 0 {
return ""
}
afterJava := normalized[idx+len("/java/"):]
lastSlash := strings.LastIndex(afterJava, "/")
if lastSlash < 0 {
return ""
}
return strings.ReplaceAll(afterJava[:lastSlash], "/", ".")
}
// extractJavaPackageName derives the Java package from a file path by
// looking for a "java/" directory component.
func extractJavaPackageName(path string) string {
normalized := filepath.ToSlash(path)
idx := strings.LastIndex(normalized, "/java/")
if idx < 0 {
return ""
}
afterJava := normalized[idx+len("/java/"):]
lastSlash := strings.LastIndex(afterJava, "/")
if lastSlash < 0 {
return ""
}
return strings.ReplaceAll(afterJava[:lastSlash], "/", ".")
}
// filterByPattern returns constants whose names match the given glob pattern.
func filterByPattern(
constants map[string]string,
pattern string,
) map[string]string {
result := make(map[string]string)
for name, value := range constants {
matched, err := filepath.Match(pattern, name)
if err != nil {
continue
}
if matched {
result[name] = value
}
}
return result
}
// nameTransformFunc converts a Java SCREAMING_SNAKE_CASE constant name
// to a Go PascalCase name.
type nameTransformFunc func(javaName string) string
// parseNameTransform parses a name_transform directive and returns the
// corresponding transform function.
func parseNameTransform(directive string) (nameTransformFunc, error) {
switch {
case directive == "":
return codegen.ScreamingSnakeToPascal, nil
case strings.HasPrefix(directive, "strip_suffix:"):
suffix := strings.TrimPrefix(directive, "strip_suffix:")
return makeStripSuffixTransform(suffix), nil
default:
return nil, fmt.Errorf("unknown name_transform directive: %s", directive)
}
}
// makeStripSuffixTransform returns a transform that strips the given suffix
// (with underscore prefix) from SCREAMING_SNAKE names, converts the remainder
// to PascalCase, and appends the suffix in PascalCase.
func makeStripSuffixTransform(suffix string) nameTransformFunc {
suffixWithUnderscore := "_" + suffix
pascalSuffix := codegen.ScreamingSnakeToPascal(suffix)
return func(javaName string) string {
stripped := strings.TrimSuffix(javaName, suffixWithUnderscore)
return codegen.ScreamingSnakeToPascal(stripped) + pascalSuffix
}
}