Debian Codegraph @

gen/main.go

raw symbols SCIP
1 //go:generate go run main.go
2
3 package main
4
5 import (
6 "fmt"
7 "log"
8 "os"
9 "path"
10 "path/filepath"
11 "regexp"
12 "runtime"
13 "strings"
14 )
15
16 func main() {
17 _, filename, _, ok := runtime.Caller(0)
18 if !ok {
19 panic("runtime err")
20 }
21
22 rootDir := path.Join(path.Dir(filename), "..")
23
24 dstDir := filepath.Join(rootDir, "internal/libwebp")
25 srcFolders := []string{"src", "sharpyuv"}
26 srcBaseDir := filepath.Join(rootDir, "libwebp_src")
27
28 // The Go and the Webp source must live side-by-side in the same
29 // directory.
30 //
31 // The custom bindings are named with a "a__" prefix. Keep those.
32 fis, err := os.ReadDir(dstDir)
33 if err != nil {
34 log.Fatal(err)
35 }
36
37 keepRe := regexp.MustCompile(`^(a__|\.)`)
38
39 for _, fi := range fis {
40 if keepRe.MatchString(fi.Name()) {
41 continue
42 }
43 os.Remove(filepath.Join(dstDir, fi.Name()))
44 }
45
46 csourceRe := regexp.MustCompile(`\.[ch]$`)
47
48 for _, srcFolder := range srcFolders {
49 srcDir := filepath.Join(srcBaseDir, srcFolder)
50
51 err = filepath.Walk(srcDir, func(path string, fi os.FileInfo, err error) error {
52 if err != nil {
53 return err
54 }
55 if fi.IsDir() || !csourceRe.MatchString(fi.Name()) {
56 return nil
57 }
58
59 filename := filepath.ToSlash(strings.TrimPrefix(path, srcBaseDir))
60 filename = strings.TrimPrefix(filename, "/")
61 target := filepath.Join(dstDir, fi.Name())
62
63 if err := os.WriteFile(target, []byte(fmt.Sprintf(`#ifndef LIBWEBP_NO_SRC
64 #include "../../libwebp_src/%s"
65 #endif
66 `, filename)), 0o644); err != nil {
67 return err
68 }
69
70 return nil
71 })
72 if err != nil {
73 log.Fatal(err)
74 }
75 }
76 }