Generate gen/c/base.c

This commit is contained in:
Nigel Tao committed 2018-06-10 09:17:27 +10:00
1 parent a177e798f4
commit a1ed77cc35
7 files changed
+1363 -38

No files matched your search

@@ -0,0 +1,23 @@
// After editing this file, run "go generate" in the parent directory.
// Copyright 2018 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// !! base-public.h goes here.
// !! wuffs_base__status__string data goes here.
const char* wuffs_base__status__string(int32_t s) {
return "unknown status";
}
+60 -12
View File
@@ -72,18 +72,33 @@ func Do(args []string) error {
return nil, fmt.Errorf("bad -cformatter flag value %q", *cformatterFlag)
}
g := &gen{
PKGPREFIX: "WUFFS_" + strings.ToUpper(pkgName) + "__",
pkgPrefix: "wuffs_" + pkgName + "__",
pkgName: pkgName,
tm: tm,
checker: c,
files: files,
}
unformatted, err := g.generate()
if err != nil {
return nil, err
unformatted := []byte(nil)
if pkgName == "base" {
if len(files) != 0 {
return nil, fmt.Errorf("base package shouldn't have any .wuffs files")
}
var err error
unformatted, err = genBase()
if err != nil {
return nil, err
}
} else {
g := &gen{
PKGPREFIX: "WUFFS_" + strings.ToUpper(pkgName) + "__",
pkgPrefix: "wuffs_" + pkgName + "__",
pkgName: pkgName,
tm: tm,
checker: c,
files: files,
}
var err error
unformatted, err = g.generate()
if err != nil {
return nil, err
}
}
stdout := &bytes.Buffer{}
cmd := exec.Command(*cformatterFlag, "-style=Chromium")
cmd.Stdin = bytes.NewReader(unformatted)
@@ -158,6 +173,39 @@ func (b *buffer) writeb(x byte) { *b = append(*b, x)
func (b *buffer) writes(s string) { *b = append(*b, s...) }
func (b *buffer) writex(s []byte) { *b = append(*b, s...) }
func genBase() ([]byte, error) {
b := make(buffer, 0, 128*1024)
for s := baseImplC; ; {
remaining := ""
if i := strings.IndexByte(s, '\n'); i >= 0 {
s, remaining = s[:i+1], s[i+1:]
}
if !strings.HasPrefix(s, "// !! ") {
b.writes(s)
} else {
switch s {
default:
return nil, fmt.Errorf("unrecognized base-impl.c line %q", s)
case "// !! base-public.h goes here.\n":
b.writes("// Code generated by wuffs-c. DO NOT EDIT.\n\n")
b.writes(basePublicH)
b.writeb('\n')
case "// !! wuffs_base__status__string data goes here.\n":
// TODO.
}
}
if remaining == "" {
break
}
s = remaining
}
return b, nil
}
type gen struct {
PKGPREFIX string // e.g. "WUFFS_JPEG__"
pkgPrefix string // e.g. "wuffs_jpeg__"
@@ -225,7 +273,7 @@ func (g *gen) genHeader(b *buffer) error {
includeGuard := "WUFFS_" + strings.ToUpper(g.pkgName) + "_H"
b.printf("#ifndef %s\n#define %s\n\n", includeGuard, includeGuard)
b.printf("// Code generated by wuffs-c. DO NOT EDIT.\n\n")
b.writes("// Code generated by wuffs-c. DO NOT EDIT.\n\n")
b.writes(basePublicH)
b.writeb('\n')
+4
View File
@@ -120,6 +120,10 @@ const basePrivateH = "" +
"_impl.bounds[1] = ioptr_r + limit;\n }\n return ((wuffs_base__empty_struct){});\n}\n\nstatic inline wuffs_base__empty_struct wuffs_base__io_reader__set_mark(\n wuffs_base__io_reader* o,\n uint8_t* mark) {\n o->private_impl.bounds[0] = mark;\n return ((wuffs_base__empty_struct){});\n}\n\nstatic inline wuffs_base__empty_struct wuffs_base__io_writer__set(\n wuffs_base__io_writer* o,\n wuffs_base__io_buffer* b,\n uint8_t** ioptr1_ptr,\n uint8_t** ioptr2_ptr,\n wuffs_base__slice_u8 s) {\n b->ptr = s.ptr;\n b->len = s.len;\n b->wi = 0;\n b->ri = 0;\n b->closed = false;\n o->private_impl.buf = b;\n o->private_impl.bounds[0] = s.ptr;\n o->private_impl.bounds[1] = s.ptr + s.len;\n *ioptr1_ptr = s.ptr;\n *ioptr2_ptr = s.ptr + s.len;\n return ((wuffs_base__empty_struct){});\n}\n\nstatic inline wuffs_base__empty_struct wuffs_base__io_writer__set_mark(\n wuffs_base__io_writer* o,\n uint8_t* mark) {\n o->private_impl.bounds[0] = mark;\n return ((wuffs_base__empty_struct){});\n}\n" +
""
const baseImplC = "" +
"// Copyright 2018 The Wuffs Authors.\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// https://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\n// !! base-public.h goes here.\n\n// !! wuffs_base__status__string data goes here.\n\nconst char* wuffs_base__status__string(int32_t s) {\n return \"unknown status\";\n}\n" +
""
type template_args_short_read struct {
PKGPREFIX string
name string
+1
View File
@@ -81,6 +81,7 @@ func genBase(out *bytes.Buffer) error {
}{
{"base/base-public.h", "basePublicH"},
{"base/base-private.h", "basePrivateH"},
{"base/base-impl.c", "baseImplC"},
}
for _, f := range files {
+11 -3
View File
@@ -107,6 +107,14 @@ func (h *genHelper) gen(dirname string, recursive bool) error {
}
h.seen[dirname] = struct{}{}
if dirname == "base" {
if err := h.genDir(dirname, nil); err != nil {
return err
}
h.affected = append(h.affected, dirname)
return nil
}
if !cf.IsValidUsePath(dirname) {
return fmt.Errorf("invalid package path %q", dirname)
}
@@ -175,7 +183,7 @@ func (h *genHelper) genDir(dirname string, filenames []string) error {
}
// Special-case the "c" generator to also write a .h file.
if lang != "c" {
if lang != "c" || packageName == "base" {
continue
}
if i := bytes.Index(out, cHeaderEndsHere); i < 0 {
@@ -187,7 +195,7 @@ func (h *genHelper) genDir(dirname string, filenames []string) error {
return err
}
}
if len(h.langs) > 0 {
if len(h.langs) > 0 && packageName != "base" {
if err := h.genWuffs(dirname, qualifiedFilenames); err != nil {
return err
}
@@ -214,7 +222,7 @@ func (h *genHelper) genDirDependencies(qualifiedFilenames []string) error {
}
}
}
return nil
return h.gen("base", false)
}
func (h *genHelper) genFile(dirname string, lang string, out []byte) error {