/
main.go
178 lines (148 loc) · 4.69 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
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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"strings"
"github.com/kitech/qt.go/miscutil"
"github.com/kitech/qt.go/toolutil"
)
var file string
var filep string // file name without externsion
var pkgname string = "main" // final code's package name
var cp = toolutil.NewCodePager()
const (
INSEC_NONE = iota
INSEC_RES_DATA
INSEC_RES_NAME
INSEC_RES_STRUCT
INSEC_DONE
)
func init() {
flag.StringVar(&pkgname, "pkg", pkgname, "generated code's package name")
}
// cgo version rcc compile. feature: decrease go compile time RAM usage(~72%, 2.8G=>0.8G). but depend on gcc.
// usage: go-rcc -pkg [name] <rcc.qrc>
// depend on: /usr/bin/rcc
func main() {
flag.Parse()
log.SetFlags(log.Flags() | log.Lshortfile)
file = flag.Arg(0)
log.Println(flag.Arg(0))
filep = path.Base(file)[0:strings.LastIndex(path.Base(file), ".")]
filep = miscutil.IfElseStr(path.Dir(file) == "", filep, path.Dir(file)+"/"+filep)
log.Println(file, filep)
scc, err := toolutil.RunCmdOut("rcc", file)
miscutil.ErrPrint(err, "rcc", file)
if err != nil {
os.Exit(1)
}
lines := strings.Split(scc, "\n")
log.Println("lines:", len(lines), "size:", len(scc))
cp.APf("header", "")
cp.APf("header", "/*")
cp.APf("header", "#include <string.h>")
hasRccData, hasRccName, hasRccStruct := false, false, false
insection := INSEC_NONE
for _, line := range lines {
line = strings.TrimSpace(line)
if strings.Contains(line, "qt_resource_data[]") {
insection = INSEC_RES_DATA
hasRccData = true
// cp.APf("body", "var qt_resource_data = []byte{")
cp.APf("body", line[7:])
continue
}
if strings.Contains(line, "qt_resource_name[]") {
insection = INSEC_RES_NAME
hasRccName = true
// cp.APf("body", "var qt_resource_name = []byte{")
cp.APf("body", line[7:])
continue
}
if strings.Contains(line, "qt_resource_struct[]") {
insection = INSEC_RES_STRUCT
hasRccStruct = true
// cp.APf("body", "var qt_resource_struct = []byte{")
cp.APf("body", line[7:])
continue
}
if strings.HasPrefix(line, "#ifdef QT_NAMESPACE") {
insection = INSEC_DONE
break
}
switch insection {
case INSEC_RES_DATA:
// do smth...
// log.Println(line)
transformMember(line)
case INSEC_RES_NAME:
onSetupUi(line)
case INSEC_RES_STRUCT:
onRetranslateUi(line)
}
}
// fix not exists
if !hasRccData {
cp.APf("body", "var qt_resource_data = []byte{} // empty")
}
if !hasRccName {
cp.APf("body", "var qt_resource_name = []byte{} // empty")
}
if !hasRccStruct {
cp.APf("body", "var qt_resource_struct = []byte{} // empty")
}
cp.APf("header2", "*/")
cp.APf("header2", "import \"C\"")
cp.APf("header2", "import \"unsafe\"")
cp.APf("header2", "// import \"github.com/kitech/qt.go/qtcore\"")
cp.APf("header2", "import \"github.com/kitech/qt.go/qtmock\"")
saveCode()
}
func transformMember(line string) {
home := os.Getenv("HOME")
line = strings.Replace(line, home, "/home/me", -1)
cp.APf("body", line)
}
func onSetupUi(line string) {
cp.APf("body", line)
}
func onRetranslateUi(line string) {
cp.APf("body", line)
}
func saveCode() {
var code string
var mod os.FileMode = 0644
// bool qRegisterResourceData (int, const unsigned char *, const unsigned char *, const unsigned char *).
// bool qUnregisterResourceData (int, const unsigned char *, const unsigned char *, const unsigned char *)
cp.APf("bodygo", "var qt_rcc_version = 0x2 // >= 5.8.0, else 0x1")
cp.APf("bodygo", "func qInitResources(){")
cp.APf("bodygo", " if C.sizeof_qt_resource_name > 0 {")
cp.APf("bodygo", " qtmock.QRegisterResourceData(qt_rcc_version, unsafe.Pointer(&C.qt_resource_struct[0]), unsafe.Pointer(&C.qt_resource_name[0]), unsafe.Pointer(&C.qt_resource_data[0]))")
cp.APf("bodygo", " }")
cp.APf("bodygo", "}")
cp.APf("bodygo", "func qCleanupResources(){")
cp.APf("bodygo", " if C.sizeof_qt_resource_name > 0 {")
cp.APf("bodygo", " qtmock.QUnregisterResourceData(qt_rcc_version, unsafe.Pointer(&C.qt_resource_struct[0]), unsafe.Pointer(&C.qt_resource_name[0]), unsafe.Pointer(&C.qt_resource_data[0]))")
cp.APf("bodygo", " }")
cp.APf("bodygo", "}")
cp.APf("bodygo", "func init(){qInitResources()}")
log.Printf("saving... %s_rc.go...\n", filep)
code = fmt.Sprintf("package %s\n", pkgname)
code += cp.ExportAll()
savefile := fmt.Sprintf("%s_rc.go", filep)
err := ioutil.WriteFile(savefile, []byte(code), mod)
miscutil.ErrPrint(err, savefile)
// gofmt the code
gofmtPath, err := exec.LookPath("gofmt")
miscutil.ErrPrint(err)
cmd := exec.Command(gofmtPath, "-w", savefile)
err = cmd.Run()
miscutil.ErrPrint(err, cmd)
}
func colon2uline(s string) string { return strings.Replace(s, ":", "_", -1) }
func untitle(s string) string { return strings.ToLower(s[0:1]) + s[1:] }