syncfiles/gen/main.go

106 lines
2.2 KiB
Go
Raw Normal View History

package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
2022-03-23 22:04:39 +00:00
const (
header = "/* This file is automatically generated. */\n"
srcdirname = "convert"
)
var (
flagDest string
flagSrc string
flagQuiet bool
flagFormat bool
)
func getSrcdir() (string, error) {
if flagSrc != "" {
return flagSrc, nil
}
workspace := os.Getenv("BUILD_WORKSPACE_DIRECTORY")
if workspace != "" {
return workspace, nil
}
exe, err := os.Executable()
if err != nil {
return "", err
}
return filepath.Dir(filepath.Dir(exe)), nil
}
func mainE() error {
srcdir, err := getSrcdir()
if err != nil {
return fmt.Errorf("could not find source dir: %v", err)
}
destdir := flagDest
if destdir == "" {
2022-03-23 22:04:39 +00:00
destdir = filepath.Join(srcdir, srcdirname)
}
// Read metadata.
d, err := readData(srcdir)
if err != nil {
return err
}
// Compile and emit charmap data.
var hascmap bool
for _, c := range d.charmaps {
if len(c.data) != 0 {
name := "charmap_" + c.filename + ".dat"
fpath := filepath.Join(destdir, name)
if !flagQuiet {
fmt.Fprintln(os.Stderr, "Writing:", fpath)
}
if err := ioutil.WriteFile(fpath, c.data, 0666); err != nil {
return err
}
hascmap = true
}
}
if !hascmap {
return errors.New("could not compile any character map")
}
// Write generated output.
m := genMap(&d)
if err := writeMap(&d, m, filepath.Join(destdir, "charmap_region.c")); err != nil {
return err
}
if err := writeInfo(&d, filepath.Join(destdir, "charmap_info.c")); err != nil {
return err
}
if err := writeData(&d, filepath.Join(destdir, "charmap_data.c")); err != nil {
return err
}
if err := writeRez(&d, filepath.Join(destdir, "charmap.r")); err != nil {
return err
}
return nil
}
func main() {
flag.StringVar(&flagDest, "dest", "", "output directory")
flag.StringVar(&flagSrc, "src", "", "source directory")
flag.BoolVar(&flagQuiet, "quiet", false, "only output error messages")
flag.BoolVar(&flagFormat, "format", true, "run clang-format on C output")
flag.Parse()
if args := flag.Args(); len(args) != 0 {
fmt.Fprintf(os.Stderr, "Error: unexpected argument: %q\n", args[0])
os.Exit(2)
}
if err := mainE(); err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(1)
}
}