|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "archive/tar" |
| 5 | + "bufio" |
| 6 | + "bytes" |
| 7 | + "compress/gzip" |
| 8 | + "context" |
| 9 | + "flag" |
| 10 | + "fmt" |
| 11 | + "io" |
| 12 | + "io/fs" |
| 13 | + "net/http" |
| 14 | + "os" |
| 15 | + "path/filepath" |
| 16 | + "strings" |
| 17 | + |
| 18 | + "modernc.org/cc/v4" |
| 19 | +) |
| 20 | + |
| 21 | +const tarURL = "https://github.com/microsoft/onnxruntime/releases/download/v1.15.1/onnxruntime-linux-x64-1.15.1.tgz" |
| 22 | + |
| 23 | +func main() { |
| 24 | + var code int |
| 25 | + defer func() { |
| 26 | + if code != 0 { |
| 27 | + os.Exit(code) |
| 28 | + } |
| 29 | + }() |
| 30 | + var out *os.File |
| 31 | + in := flag.String("url", tarURL, "fetch ONNX Runtime release tarball from `URL`") |
| 32 | + pkg := flag.String("pacakge", "magika", "generated package `name`") |
| 33 | + flag.Func("o", "output to `file`", func(p string) error { |
| 34 | + if out != nil { |
| 35 | + if err := out.Close(); err != nil { |
| 36 | + return err |
| 37 | + } |
| 38 | + } |
| 39 | + f, err := os.Create(p) |
| 40 | + if err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + out = f |
| 44 | + return nil |
| 45 | + }) |
| 46 | + flag.Parse() |
| 47 | + |
| 48 | + if out == nil { |
| 49 | + out = os.Stdout |
| 50 | + } |
| 51 | + defer out.Close() |
| 52 | + |
| 53 | + ctx := context.Background() |
| 54 | + if err := Main(ctx, out, *in, *pkg); err != nil { |
| 55 | + fmt.Fprintln(os.Stderr, err) |
| 56 | + code = 1 |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +const genHeader = `// Code generated by ortgen. DO NOT EDIT.` |
| 61 | + |
| 62 | +func Main(ctx context.Context, out io.Writer, in, pkg string) error { |
| 63 | + const header = `onnxruntime_c_api.h` |
| 64 | + dir, err := fetchTarball(ctx, in) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + defer os.RemoveAll(dir.Name()) |
| 69 | + defer dir.Close() |
| 70 | + |
| 71 | + cfg, err := cc.NewConfig("linux", "amd64") |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + ms, _ := fs.Glob(dir.FS(), `*/include/`+header) |
| 76 | + f, err := dir.Open(ms[0]) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + src := []cc.Source{ |
| 81 | + {Name: "<predefined>", Value: cfg.Predefined}, |
| 82 | + {Name: "<builtin>", Value: cc.Builtin}, |
| 83 | + {Name: header, Value: f}, |
| 84 | + } |
| 85 | + ast, err := cc.Translate(cfg, src) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + |
| 90 | + fmt.Fprintf(out, "%s\n\npackage %s\n\nimport \"structs\"\n", genHeader, pkg) |
| 91 | + // Walk the list, looking for the bits we want: |
| 92 | + var buf bytes.Buffer |
| 93 | + for cur := ast.TranslationUnit; cur != nil; cur = cur.TranslationUnit { |
| 94 | + buf.Reset() |
| 95 | + if cur.ExternalDeclaration.Case != cc.ExternalDeclarationDecl { |
| 96 | + continue |
| 97 | + } |
| 98 | + decl := cur.ExternalDeclaration.Declaration |
| 99 | + if decl.Position().Filename != header { |
| 100 | + continue |
| 101 | + } |
| 102 | + spec := decl.DeclarationSpecifiers |
| 103 | + if spec == nil || spec.Case != cc.DeclarationSpecifiersTypeSpec { |
| 104 | + continue |
| 105 | + } |
| 106 | + ty := spec.TypeSpecifier |
| 107 | + if ty == nil || ty.Case != cc.TypeSpecifierStructOrUnion { |
| 108 | + continue |
| 109 | + } |
| 110 | + structSpec := ty.StructOrUnionSpecifier |
| 111 | + if structSpec == nil || |
| 112 | + structSpec.Case != cc.StructOrUnionSpecifierDef || |
| 113 | + structSpec.StructOrUnion.Case != cc.StructOrUnionStruct { |
| 114 | + continue |
| 115 | + } |
| 116 | + |
| 117 | + n := structSpec.Token.SrcStr() |
| 118 | + switch n { |
| 119 | + case "OrtApi": |
| 120 | + case "OrtApiBase": |
| 121 | + default: |
| 122 | + continue |
| 123 | + } |
| 124 | + fmt.Fprintf(&buf, "\ntype %s struct {\n\t_ structs.HostLayout\n\n", strings.Replace(n, "O", "o", 1)) |
| 125 | + |
| 126 | + for cur := structSpec.StructDeclarationList; cur != nil; cur = cur.StructDeclarationList { |
| 127 | + buf.WriteString("\t// ") |
| 128 | + buf.WriteString(cc.NodeSource(cur.StructDeclaration)) |
| 129 | + buf.WriteByte('\n') |
| 130 | + |
| 131 | + // pull out the function pointer ident: |
| 132 | + decl := cur.StructDeclaration.StructDeclaratorList.StructDeclarator.Declarator.DirectDeclarator.DirectDeclarator.Declarator.DirectDeclarator |
| 133 | + buf.WriteByte('\t') |
| 134 | + buf.Write(decl.Token.Src()) |
| 135 | + buf.WriteString(" uintptr\n") |
| 136 | + } |
| 137 | + |
| 138 | + buf.WriteString("}\n") |
| 139 | + if _, err := io.Copy(out, &buf); err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return nil |
| 145 | +} |
| 146 | + |
| 147 | +func fetchTarball(ctx context.Context, in string) (*os.Root, error) { |
| 148 | + res, err := http.Get(in) |
| 149 | + if err != nil { |
| 150 | + return nil, err |
| 151 | + } |
| 152 | + if res.StatusCode != http.StatusOK { |
| 153 | + return nil, fmt.Errorf("unexpected status: %v", res.Status) |
| 154 | + } |
| 155 | + defer res.Body.Close() |
| 156 | + z, err := gzip.NewReader(res.Body) |
| 157 | + if err != nil { |
| 158 | + return nil, err |
| 159 | + } |
| 160 | + |
| 161 | + d, err := os.MkdirTemp("", "ortgen.") |
| 162 | + if err != nil { |
| 163 | + return nil, err |
| 164 | + } |
| 165 | + root, err := os.OpenRoot(d) |
| 166 | + if err != nil { |
| 167 | + return nil, err |
| 168 | + } |
| 169 | + |
| 170 | + ok := false |
| 171 | + defer func() { |
| 172 | + if !ok { |
| 173 | + os.RemoveAll(d) |
| 174 | + root.Close() |
| 175 | + } |
| 176 | + }() |
| 177 | + |
| 178 | + rd := tar.NewReader(bufio.NewReader(z)) |
| 179 | + h, err := rd.Next() |
| 180 | + for ; err == nil; h, err = rd.Next() { |
| 181 | + err := func() error { |
| 182 | + p := filepath.Join(".", h.Name) |
| 183 | + fi := h.FileInfo() |
| 184 | + if fi.IsDir() { |
| 185 | + return root.Mkdir(p, 0o755) |
| 186 | + } |
| 187 | + f, err := root.Create(p) |
| 188 | + if err != nil { |
| 189 | + return err |
| 190 | + } |
| 191 | + defer f.Close() |
| 192 | + |
| 193 | + if _, err := io.Copy(f, rd); err != nil { |
| 194 | + return err |
| 195 | + } |
| 196 | + return nil |
| 197 | + }() |
| 198 | + if err != nil { |
| 199 | + return nil, err |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + ok = true |
| 204 | + return root, nil |
| 205 | +} |
0 commit comments