forked from tidbyt/go-libwebp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (49 loc) · 1.83 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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
)
const cFileTemplate = `// Code generated by 'go generate'; DO NOT EDIT.
//go:build cgo
#include "%s"
`
var basePath = filepath.Join(".", "..", "..", "libwebp")
func mustFindFiles(glob string) []string {
files, err := filepath.Glob(glob)
if err != nil {
log.Panicf("Couldn't list files by pattern %q: %v", glob, err)
}
return files
}
func removeOldCFiles() {
for _, filename := range mustFindFiles(filepath.Join(".", "_gen_*.c")) {
os.Remove(filename)
}
}
func generateCFiles() {
// Generate list of C files that need to be compiled.
// This is roughly based on the cmake files, see the lines following `set(WEBP_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)` in CMakeLists.txt and similar things.
cFiles := []string{}
cFiles = append(cFiles, mustFindFiles(filepath.Join(basePath, "src", "dec", "*.c"))...)
cFiles = append(cFiles, mustFindFiles(filepath.Join(basePath, "src", "demux", "*.c"))...)
cFiles = append(cFiles, mustFindFiles(filepath.Join(basePath, "src", "dsp", "*.c"))...)
cFiles = append(cFiles, mustFindFiles(filepath.Join(basePath, "src", "enc", "*.c"))...)
cFiles = append(cFiles, mustFindFiles(filepath.Join(basePath, "src", "mux", "*.c"))...)
cFiles = append(cFiles, mustFindFiles(filepath.Join(basePath, "src", "utils", "*.c"))...)
cFiles = append(cFiles, mustFindFiles(filepath.Join(basePath, "sharpyuv", "*.c"))...)
for _, cFile := range cFiles {
relPath, err := filepath.Rel(basePath, cFile)
if err != nil {
log.Panicf("Failed to get path %q relative to base path %q: %v", cFile, basePath, err)
}
mangledName := "generated_" + strings.ReplaceAll(relPath, string(filepath.Separator), "_")
os.WriteFile(mangledName, []byte(fmt.Sprintf(cFileTemplate, filepath.ToSlash(relPath))), 0666)
}
}
func main() {
removeOldCFiles()
generateCFiles()
}