2022-03-15 17:38:45 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-03-15 20:19:56 +00:00
|
|
|
"errors"
|
2022-03-15 17:38:45 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2022-03-15 20:19:56 +00:00
|
|
|
"io/ioutil"
|
2022-03-15 17:38:45 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
2022-03-23 22:04:39 +00:00
|
|
|
const (
|
|
|
|
header = "/* This file is automatically generated. */\n"
|
|
|
|
srcdirname = "convert"
|
|
|
|
)
|
2022-03-15 17:38:45 +00:00
|
|
|
|
2022-03-22 22:43:47 +00:00
|
|
|
var (
|
2022-03-23 05:26:25 +00:00
|
|
|
flagDest string
|
|
|
|
flagSrc string
|
|
|
|
flagQuiet bool
|
|
|
|
flagFormat bool
|
2022-03-22 22:43:47 +00:00
|
|
|
)
|
|
|
|
|
2022-03-15 17:38:45 +00:00
|
|
|
func getSrcdir() (string, error) {
|
2022-03-22 22:43:47 +00:00
|
|
|
if flagSrc != "" {
|
|
|
|
return flagSrc, nil
|
|
|
|
}
|
2022-03-23 16:00:18 +00:00
|
|
|
workspace := os.Getenv("BUILD_WORKSPACE_DIRECTORY")
|
|
|
|
if workspace != "" {
|
|
|
|
return workspace, nil
|
|
|
|
}
|
2022-03-15 17:38:45 +00:00
|
|
|
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)
|
|
|
|
}
|
2022-03-22 22:43:47 +00:00
|
|
|
destdir := flagDest
|
|
|
|
if destdir == "" {
|
2022-03-23 22:04:39 +00:00
|
|
|
destdir = filepath.Join(srcdir, srcdirname)
|
2022-03-15 17:38:45 +00:00
|
|
|
}
|
2022-03-15 20:19:56 +00:00
|
|
|
|
|
|
|
// Read metadata.
|
2022-03-22 22:43:47 +00:00
|
|
|
d, err := readData(srcdir)
|
2022-03-15 17:38:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-15 20:19:56 +00:00
|
|
|
|
|
|
|
// Compile and emit charmap data.
|
|
|
|
var hascmap bool
|
2022-03-25 02:34:32 +00:00
|
|
|
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)
|
2022-03-15 20:19:56 +00:00
|
|
|
}
|
2022-03-25 02:34:32 +00:00
|
|
|
if err := ioutil.WriteFile(fpath, c.data, 0666); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hascmap = true
|
2022-03-15 20:19:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !hascmap {
|
|
|
|
return errors.New("could not compile any character map")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write generated output.
|
2022-03-15 18:31:12 +00:00
|
|
|
m := genMap(&d)
|
2022-03-25 02:34:32 +00:00
|
|
|
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 {
|
2022-03-15 18:31:12 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-03-25 02:34:32 +00:00
|
|
|
if err := writeData(&d, filepath.Join(destdir, "charmap_data.c")); err != nil {
|
2022-03-23 05:26:25 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-03-25 02:34:32 +00:00
|
|
|
if err := writeRez(&d, filepath.Join(destdir, "charmap.r")); err != nil {
|
2022-03-15 19:34:39 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-03-15 17:38:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2022-03-22 22:43:47 +00:00
|
|
|
flag.StringVar(&flagDest, "dest", "", "output directory")
|
|
|
|
flag.StringVar(&flagSrc, "src", "", "source directory")
|
|
|
|
flag.BoolVar(&flagQuiet, "quiet", false, "only output error messages")
|
2022-03-23 05:26:25 +00:00
|
|
|
flag.BoolVar(&flagFormat, "format", true, "run clang-format on C output")
|
2022-03-15 17:38:45 +00:00
|
|
|
flag.Parse()
|
2022-03-15 20:19:56 +00:00
|
|
|
if args := flag.Args(); len(args) != 0 {
|
|
|
|
fmt.Fprintf(os.Stderr, "Error: unexpected argument: %q\n", args[0])
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
2022-03-15 17:38:45 +00:00
|
|
|
if err := mainE(); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, "Error:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|