forked from rsc/c2go
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
86 lines (74 loc) · 1.94 KB
/
main.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
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// C2go converts (some) C programs to Go.
// It was developed by Russ Cox to convert the Go runtime and compiler to Go.
// It is progressing toward becoming a general-purpose translation tool,
// but it does not (and never will) translate all possible C programs.
// You will need to refactor your C program into the subset of C that c2go
// can handle, and give it translation hints in the configuration file.
//
// Usage of c2go:
// c2go [options] *.c
//
// Options:
// -c string
// config file
// -dst string
// root path of destination (default "/tmp/c2go")
package main
import (
"flag"
"fmt"
"log"
"os"
"github.com/andybalholm/c2go/cc"
)
type options struct {
cfgFile string
dst string
preprocessor string
}
func main() {
log.SetFlags(0)
var opt options
flag.StringVar(&opt.cfgFile, "c", "", "config file")
flag.StringVar(&opt.dst, "dst", "/tmp/c2go", "root path of destination")
flag.StringVar(&opt.preprocessor, "cpp", "", "C compiler to preprocess files (gcc, clang, or blank for none)")
flag.Parse()
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: c2go [options] *.c\n")
flag.PrintDefaults()
os.Exit(2)
}
if len(flag.Args()) == 0 {
flag.Usage()
}
C2Go(flag.Args(), opt)
}
func C2Go(files []string, opt options) {
cfg := new(Config)
if opt.cfgFile != "" {
cfg.read(opt.cfgFile)
}
for _, t := range cfg.typedefs {
cc.AddTypeName(t)
}
prog, err := cc.ReadMany(files, opt.preprocessor)
if err != nil {
log.Fatal(err)
}
rewriteTypes(cfg, prog)
rewriteSyntax(cfg, prog)
rewriteLen(cfg, prog)
fixGoTypes(cfg, prog)
simplifyBool(cfg, prog)
renameDecls(cfg, prog)
exportDecls(cfg, prog)
writeGoFiles(cfg, prog, opt.dst)
for _, d := range cfg.diffs {
if d.used == 0 {
fmt.Fprintf(os.Stderr, "%s: unused diff\n", d.line)
}
}
}