syncfiles/gen/main.go
Dietrich Epp 5ad207f785 Embed character map tables in executable
This simplifies the conversion test, since we don't need to be careful
about which data we run the conversion test in. It will also simplify
the command-line conversion tool and its distribution. The classic Mac
OS version of this program will continue to embed conversion tables in
the resource fork.
2022-03-24 23:44:37 -04:00

106 lines
2.2 KiB
Go

package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
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 == "" {
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)
}
}