|
| 1 | +//go:build ignore |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "io/ioutil" |
| 8 | + "log" |
| 9 | + "os" |
| 10 | + "os/exec" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/progrium/macdriver/generate" |
| 14 | + "github.com/progrium/macdriver/generate/modules" |
| 15 | +) |
| 16 | + |
| 17 | +const TargetPlatform = "macos" |
| 18 | +const TargetVersion = 12 |
| 19 | + |
| 20 | +// go run ./generate/tools/structs.go [framework] |
| 21 | +func main() { |
| 22 | + |
| 23 | + db, err := generate.OpenSymbols("./generate/symbols.zip") |
| 24 | + if err != nil { |
| 25 | + log.Fatal(err) |
| 26 | + } |
| 27 | + defer db.Close() |
| 28 | + |
| 29 | + if len(os.Args) > 1 { |
| 30 | + mod := modules.Get(strings.ToLower(os.Args[1])) |
| 31 | + |
| 32 | + var structDefs []string |
| 33 | + var badStructs []string |
| 34 | + docComments := make(map[string]string) |
| 35 | + |
| 36 | + for _, s := range db.ModuleSymbols(mod.Package) { |
| 37 | + if TargetPlatform == "macos" && mod.Package == "uikit" { |
| 38 | + // we're going to pretend to be appkit later |
| 39 | + // to handle the uikit symbols existing in appkit |
| 40 | + if !s.HasFramework("appkit") { |
| 41 | + continue |
| 42 | + } |
| 43 | + } |
| 44 | + if !s.HasPlatform(TargetPlatform, TargetVersion, true) { |
| 45 | + continue |
| 46 | + } |
| 47 | + |
| 48 | + if s.Kind == "Struct" { |
| 49 | + |
| 50 | + structDef := fmt.Sprintf("type %s C.%s\n", modules.TrimPrefix(s.Name), s.Name) |
| 51 | + |
| 52 | + // generate for just this struct to see that it works |
| 53 | + // before adding so the full list generates without error. |
| 54 | + // however we can't just use this because godefs will only |
| 55 | + // use the right sub struct type if they're generated together. |
| 56 | + _, err := goDefsFor(mod, []string{structDef}) |
| 57 | + if err != nil { |
| 58 | + badStructs = append(badStructs, s.Name) |
| 59 | + continue |
| 60 | + } |
| 61 | + |
| 62 | + if s.DocURL() != "" { |
| 63 | + // godefs strips comments so we'll have to reassemble with |
| 64 | + // comments using this |
| 65 | + docComments[modules.TrimPrefix(s.Name)] = fmt.Sprintf("// %s [Full Topic]\n//\n// [Full Topic]: %s\n", s.Description, s.DocURL()) |
| 66 | + } |
| 67 | + |
| 68 | + structDefs = append(structDefs, structDef) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if len(structDefs) == 0 { |
| 73 | + fmt.Fprintln(os.Stderr, "No structs for this framework") |
| 74 | + os.Exit(1) |
| 75 | + } |
| 76 | + |
| 77 | + out, err := goDefsFor(mod, structDefs) |
| 78 | + if err != nil { |
| 79 | + fmt.Println(out) |
| 80 | + log.Fatal(err) |
| 81 | + } |
| 82 | + |
| 83 | + // *byte and *[0]byte fields should just be uintptr |
| 84 | + out = strings.ReplaceAll(out, "*byte", "uintptr") |
| 85 | + out = strings.ReplaceAll(out, "*[0]byte", "uintptr") |
| 86 | + |
| 87 | + // reassemble with comments |
| 88 | + fmt.Println("package", mod.Package, "\n") |
| 89 | + parts := strings.Split(out, "type ") |
| 90 | + for _, p := range parts[1:] { |
| 91 | + words := strings.Fields(p) |
| 92 | + if s, ok := docComments[words[0]]; ok { |
| 93 | + fmt.Print(s) |
| 94 | + } |
| 95 | + fmt.Println("type", p) |
| 96 | + } |
| 97 | + |
| 98 | + if len(badStructs) > 0 { |
| 99 | + fmt.Println("// TODO (unable to generate):") |
| 100 | + fmt.Println("//", strings.Join(badStructs, " ")) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | +} |
| 105 | + |
| 106 | +func goDefsFor(mod *modules.Module, structDefs []string) (string, error) { |
| 107 | + extraLoad := "" |
| 108 | + extraInclude := "" |
| 109 | + source := fmt.Sprintf(`package main |
| 110 | +
|
| 111 | +/* |
| 112 | +#cgo CFLAGS: -w -x objective-c |
| 113 | +#cgo LDFLAGS: -lobjc -framework %s %s |
| 114 | +#include <%s> |
| 115 | +%s |
| 116 | +*/ |
| 117 | +import "C" |
| 118 | +
|
| 119 | +%s |
| 120 | +`, mod.Name, extraLoad, mod.Header, extraInclude, strings.Join(structDefs, "\n")) |
| 121 | + b, err := evalSource(source) |
| 122 | + return string(b), err |
| 123 | +} |
| 124 | + |
| 125 | +func evalSource(source string) ([]byte, error) { |
| 126 | + tempFile, err := ioutil.TempFile("", "temp-code-*.go") |
| 127 | + if err != nil { |
| 128 | + log.Fatal("Failed to create temporary file:", err) |
| 129 | + } |
| 130 | + defer os.Remove(tempFile.Name()) |
| 131 | + |
| 132 | + if _, err := tempFile.WriteString(source); err != nil { |
| 133 | + log.Fatal("Failed to write to temporary file:", err) |
| 134 | + } |
| 135 | + |
| 136 | + if err := tempFile.Close(); err != nil { |
| 137 | + log.Fatal("Failed to close temporary file:", err) |
| 138 | + } |
| 139 | + |
| 140 | + cmd := exec.Command("go", "tool", "cgo", "-godefs", "--", "-x", "objective-c", tempFile.Name()) |
| 141 | + return cmd.CombinedOutput() |
| 142 | +} |
0 commit comments