-
Notifications
You must be signed in to change notification settings - Fork 92
/
gen_helper.go
123 lines (106 loc) · 2.55 KB
/
gen_helper.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
// Copyright 2014 <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
// +build ignore
package main
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"log"
"path/filepath"
"strings"
)
var (
oldGenFiles = make(map[string]bool)
)
func main() {
clearOldGenFiles()
genIncludeFiles()
printOldGenFiles()
}
func clearOldGenFiles() {
ss, err := filepath.Glob("z_*.c")
if err != nil {
log.Fatal(err)
}
for i := 0; i < len(ss); i++ {
ioutil.WriteFile(ss[i], []byte("#error file removed!!!\n"), 0666)
oldGenFiles[ss[i]] = true
}
}
func genIncludeFiles() {
ss := parseCMakeListsTxt("internal/libwebp-1.0.3/CMakeLists.txt", "WEBP_SRC_DIR", "*.c")
muxSS, err := findFiles("internal/libwebp-1.0.3/src/mux", "*.c")
if err != nil {
log.Fatal(err)
}
ss = append(ss, muxSS...)
for i := 0; i < len(ss); i++ {
relpath := ss[i][23:] // drop `./`
newname := "z_libwebp_" + strings.Replace(relpath, "/", "_", -1)
ioutil.WriteFile(newname, []byte(fmt.Sprintf(
`// Copyright 2014 <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Auto Generated By 'go generate', DONOT EDIT!!!
// +build cgo
#include "%s"
`, relpath,
)), 0666)
delete(oldGenFiles, newname)
}
}
func printOldGenFiles() {
if len(oldGenFiles) == 0 {
return
}
fmt.Printf("Removed Files:\n")
for k, _ := range oldGenFiles {
fmt.Printf("%s\n", k)
}
fmt.Printf("Total %d\n", len(oldGenFiles))
}
func parseCMakeListsTxt(filename, varname, ext string) (ss []string) {
data, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatal(err)
}
br := bufio.NewReader(bytes.NewReader(data))
// find set($varname
for {
line, _, err := br.ReadLine()
if err != nil {
log.Fatal(err)
}
if strings.HasPrefix(string(line), "set("+varname) {
break
}
}
// read parse_makefile_am(${varname}, end with `)`
prefix := fmt.Sprintf("parse_makefile_am(${%s}/", varname)
baseDir := filepath.Join(filepath.Dir(filename), "src")
for {
line, _, err := br.ReadLine()
if err != nil {
log.Fatal(err)
}
s := string(line)
if !strings.HasPrefix(s, prefix) {
break
}
subdir := strings.Split(s, " ")[0][len(prefix):]
dir := filepath.Join(baseDir, subdir)
sf, err := findFiles(dir, ext)
if err != nil {
log.Fatal(err)
}
ss = append(ss, sf...)
}
return
}
func findFiles(dir, ext string) ([]string, error) {
return filepath.Glob(fmt.Sprintf("%s/%s", dir, ext))
}