Create build rules for generated data

Add flags to the generator code to specify the location of input and
output files.
This commit is contained in:
Dietrich Epp 2022-03-22 18:43:47 -04:00
parent 094f2c5016
commit f2317d0ce7
12 changed files with 198 additions and 21 deletions

View File

@ -2,6 +2,19 @@ workspace(name = "syncfiles")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "bazel_skylib",
sha256 = "f7be3474d42aae265405a592bb7da8e171919d74c16f082a5457840f06054728",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.2.1/bazel-skylib-1.2.1.tar.gz",
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.2.1/bazel-skylib-1.2.1.tar.gz",
],
)
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
bazel_skylib_workspace()
http_archive(
name = "io_bazel_rules_go",
sha256 = "f2dcd210c7095febe54b804bb1cd3a58fe8435a909db2ec04e31542631cf715c",

36
bazel/BUILD.bazel Normal file
View File

@ -0,0 +1,36 @@
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
package(
default_visibility = ["//visibility:public"],
)
string_flag(
name = "warnings",
build_setting_default = "on",
values = [
"off",
"on",
"error",
],
)
config_setting(
name = "warnings_off",
flag_values = {
":warnings": "off",
},
)
config_setting(
name = "warnings_on",
flag_values = {
":warnings": "on",
},
)
config_setting(
name = "warnings_error",
flag_values = {
":warnings": "error",
},
)

40
bazel/copts.bzl Normal file
View File

@ -0,0 +1,40 @@
# Bazel + GCC,
# Default:
# -U_FORTIFY_SOURCE
# -Wall
# -Wunused-but-set-parameter
# -Wno-free-nonheap-object
# -fno-omit-frame-pointer
# With -c dbg, adds:
# -g
# With -c opt, adds:
# -g0 -O2
# Base C options
COPTS_BASE = [
"-std=c90",
]
_COPTS_WARNING = [
"-Wall",
"-Wextra",
"-Wpointer-arith",
"-Wwrite-strings",
"-Wmissing-prototypes",
"-Wdouble-promotion",
"-Werror=implicit-function-declaration",
"-Winit-self",
"-Wstrict-prototypes",
"-Wno-format-zero-length",
]
# Internal C compilation options. Use this by default for all C targets in the
# repo.
COPTS = (
COPTS_BASE +
select({
"//bazel:warnings_off": [],
"//bazel:warnings_on": _COPTS_WARNING,
"//bazel:warnings_error": _COPTS_WARNING + ["-Werror"],
})
)

33
charmap/BUILD.bazel Normal file
View File

@ -0,0 +1,33 @@
filegroup(
name = "data",
srcs = [
"ARABIC.TXT",
"CELTIC.TXT",
"CENTEURO.TXT",
"CHINSIMP.TXT",
"CHINTRAD.TXT",
"CORPCHAR.TXT",
"CROATIAN.TXT",
"CYRILLIC.TXT",
"DEVANAGA.TXT",
"DINGBATS.TXT",
"FARSI.TXT",
"GAELIC.TXT",
"GREEK.TXT",
"GUJARATI.TXT",
"GURMUKHI.TXT",
"HEBREW.TXT",
"ICELAND.TXT",
"INUIT.TXT",
"JAPANESE.TXT",
"KEYBOARD.TXT",
"KOREAN.TXT",
"ROMAN.TXT",
"ROMANIAN.TXT",
"SYMBOL.TXT",
"THAI.TXT",
"TURKISH.TXT",
"UKRAINE.TXT",
],
visibility = ["//visibility:public"],
)

View File

@ -8,6 +8,7 @@ go_binary(
"rez.go",
"scriptmap.go",
],
visibility = ["//visibility:public"],
deps = [
"//gen/charmap",
"//gen/table",

View File

@ -6,6 +6,7 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
@ -197,15 +198,15 @@ type scriptdata struct {
charmaps []charmapinfo
}
func readData() (d scriptdata, err error) {
d.scripts, err = readConsts("scripts/script.csv")
func readData(srcdir string) (d scriptdata, err error) {
d.scripts, err = readConsts(filepath.Join(srcdir, "scripts/script.csv"))
if err != nil {
return d, err
}
d.regions, err = readConsts("scripts/region.csv")
d.regions, err = readConsts(filepath.Join(srcdir, "scripts/region.csv"))
if err != nil {
return d, err
}
d.charmaps, err = readCharmaps("scripts/charmap.csv", d.scripts.names, d.regions.names)
d.charmaps, err = readCharmaps(filepath.Join(srcdir, "scripts/charmap.csv"), d.scripts.names, d.regions.names)
return
}

View File

@ -15,7 +15,16 @@ import (
const header = "/* This file is automatically generated. */\n"
var (
flagDest string
flagSrc string
flagQuiet bool
)
func getSrcdir() (string, error) {
if flagSrc != "" {
return flagSrc, nil
}
exe, err := os.Executable()
if err != nil {
return "", err
@ -28,12 +37,13 @@ func mainE() error {
if err != nil {
return fmt.Errorf("could not find source dir: %v", err)
}
if err := os.Chdir(srcdir); err != nil {
return err
destdir := flagDest
if destdir == "" {
destdir = filepath.Join(srcdir, "src")
}
// Read metadata.
d, err := readData()
d, err := readData(srcdir)
if err != nil {
return err
}
@ -45,23 +55,26 @@ func mainE() error {
if c.file == "" {
continue
}
cm, err := charmap.ReadFile(filepath.Join("charmap", c.file))
cm, err := charmap.ReadFile(filepath.Join(srcdir, "charmap", c.file))
if err != nil {
return err
}
t, err := table.Create(cm)
if err != nil {
if e, ok := err.(*table.UnsupportedError); ok {
fmt.Fprintf(os.Stderr, "Warning: unsupported charmap %q: %s\n", c.file, e.Message)
if !flagQuiet {
fmt.Fprintf(os.Stderr, "Warning: unsupported charmap %q: %s\n", c.file, e.Message)
}
continue
}
fmt.Fprintf(os.Stderr, "Error: %s: %v", c.file, err)
os.Exit(1)
return fmt.Errorf("%s: %v", c.file, err)
}
data := t.Data()
name := "charmap_" + strings.ToLower(strings.TrimSuffix(c.file, ".TXT")) + ".dat"
fpath := filepath.Join("src", name)
fmt.Fprintln(os.Stderr, "Writing:", fpath)
fpath := filepath.Join(destdir, name)
if !flagQuiet {
fmt.Fprintln(os.Stderr, "Writing:", fpath)
}
if err := ioutil.WriteFile(fpath, data, 0666); err != nil {
return err
}
@ -74,16 +87,19 @@ func mainE() error {
// Write generated output.
m := genMap(&d)
if err := writeMap(&d, m, "src/getcharmap.c"); err != nil {
if err := writeMap(&d, m, filepath.Join(destdir, "charmap.c")); err != nil {
return err
}
if err := writeRez(&d, cms, "src/charmaps.r"); err != nil {
if err := writeRez(&d, cms, 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.Parse()
if args := flag.Args(); len(args) != 0 {
fmt.Fprintf(os.Stderr, "Error: unexpected argument: %q\n", args[0])

View File

@ -42,7 +42,9 @@ func constStrings(c *constmap) []string {
}
func writeRez(d *scriptdata, charmaps []string, filename string) error {
fmt.Fprintln(os.Stderr, "Writing:", filename)
if !flagQuiet {
fmt.Fprintln(os.Stderr, "Writing:", filename)
}
fp, err := os.Create(filename)
if err != nil {
return err

View File

@ -73,7 +73,9 @@ func genMap(d *scriptdata) []*scriptmap {
// writeMap writes out a C function that returns the correct character map for a
// given script and region.
func writeMap(d *scriptdata, m []*scriptmap, filename string) error {
fmt.Fprintln(os.Stderr, "Writing:", filename)
if !flagQuiet {
fmt.Fprintln(os.Stderr, "Writing:", filename)
}
fp, err := os.Create(filename)
if err != nil {
@ -84,7 +86,8 @@ func writeMap(d *scriptdata, m []*scriptmap, filename string) error {
w.WriteString(header)
w.WriteString(
"int GetCharmap(int script, int region) {\n" +
"#include \"convert.h\"\n" +
"int GetCharmap(int script, int region) {\n" +
"switch (script) {\n")
for _, s := range m {
fmt.Fprintf(w, "case %d: /* %s */\n", s.script, d.scripts.values[s.script])

9
scripts/BUILD.bazel Normal file
View File

@ -0,0 +1,9 @@
filegroup(
name = "data",
srcs = [
"charmap.csv",
"region.csv",
"script.csv",
],
visibility = ["//visibility:public"],
)

3
src/.gitignore vendored
View File

@ -1,3 +0,0 @@
/getcharmap.c
/charmaps.r
/*.dat

26
src/BUILD.bazel Normal file
View File

@ -0,0 +1,26 @@
genrule(
name = "data",
srcs = [
"//charmap:data",
"//scripts:data",
],
outs = [
"charmap.c",
"charmap.r",
"charmap_roman.dat",
"charmap_turkish.dat",
"charmap_croatian.dat",
"charmap_iceland.dat",
"charmap_romanian.dat",
"charmap_celtic.dat",
"charmap_gaelic.dat",
"charmap_greek.dat",
"charmap_cyrillic.dat",
"charmap_inuit.dat",
"charmap_centeuro.dat",
],
cmd = "$(execpath //gen:macscript) -dest=$(RULEDIR) -src=. -quiet",
tools = [
"//gen:macscript",
],
)