This repository has been archived by the owner on Feb 25, 2021. It is now read-only.
forked from chai2010/webp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_helper.go
107 lines (92 loc) · 2.19 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
// 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.
// +build ingore
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/CMakeLists.txt", "WEBP_SRC", ".c")
for i := 0; i < len(ss); i++ {
relpath := ss[i][2:] // 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!!!
#include "./internal/libwebp/%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 $varname, end with `)`
for {
line, _, err := br.ReadLine()
if err != nil {
log.Fatal(err)
}
if strings.HasPrefix(string(line), ")") {
break
}
switch v := strings.TrimSpace(string(line)); {
case strings.HasPrefix(v, `${`): // parse ${?}
ss = append(ss, parseCMakeListsTxt(filename, v[2:len(v)-3], ext)...)
case strings.HasSuffix(v, ext): // *.ext
ss = append(ss, v)
}
}
return
}