mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-21 08:47:28 +01:00
Generate inline headers for use declarations
This commit is contained in:
1 parent
c5b5efd7c8
commit
9867cd5e3a
6 files changed
+549
-16
No files matched your search
@@ -18,6 +18,14 @@
|
||||
// It also holds functions to parse and validate these flag values.
|
||||
package commonflags
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"go/build"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
const (
|
||||
CcompilersDefault = "clang,gcc"
|
||||
CcompilersUsage = `comma-separated list of C compilers, e.g. "clang,gcc"`
|
||||
@@ -34,6 +42,9 @@ const (
|
||||
RepsUsage = `the number of repetitions per benchmark`
|
||||
)
|
||||
|
||||
// TODO: do IsAlphaNumericIsh and IsValidUsePath belong in a separate package,
|
||||
// such as lang/validate? Perhaps together with token.Unescape?
|
||||
|
||||
// IsAlphaNumericIsh returns whether s contains only ASCII alpha-numerics and a
|
||||
// limited set of punctuation such as commas and slahes, but not containing
|
||||
// e.g. spaces, semi-colons, colons or backslashes.
|
||||
@@ -51,3 +62,18 @@ func IsAlphaNumericIsh(s string) bool {
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func IsValidUsePath(s string) bool {
|
||||
return s == path.Clean(s) && s != "" && s[0] != '.' && s[0] != '/'
|
||||
}
|
||||
|
||||
func WuffsRoot() (string, error) {
|
||||
// TODO: look for a WUFFSROOT environment variable?
|
||||
for _, p := range filepath.SplitList(build.Default.GOPATH) {
|
||||
p = filepath.Join(p, "src", "github.com", "google", "wuffs")
|
||||
if o, err := os.Stat(p); err == nil && o.IsDir() {
|
||||
return p, nil
|
||||
}
|
||||
}
|
||||
return "", errors.New("could not find Wuffs root directory")
|
||||
}
|
||||
@@ -19,9 +19,11 @@ package cgen
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"math/big"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/google/wuffs/lang/base38"
|
||||
@@ -29,6 +31,8 @@ import (
|
||||
"github.com/google/wuffs/lang/check"
|
||||
"github.com/google/wuffs/lang/generate"
|
||||
|
||||
cf "github.com/google/wuffs/cmd/commonflags"
|
||||
|
||||
a "github.com/google/wuffs/lang/ast"
|
||||
t "github.com/google/wuffs/lang/token"
|
||||
)
|
||||
@@ -166,6 +170,7 @@ type gen struct {
|
||||
structMap map[t.ID]*a.Struct
|
||||
currFunk funk
|
||||
funks map[t.QID]funk
|
||||
wuffsRoot string
|
||||
usesSeen map[string]struct{}
|
||||
}
|
||||
|
||||
@@ -577,11 +582,16 @@ func (g *gen) writeStruct(b *buffer, n *a.Struct) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
var (
|
||||
wuffsBaseHeaderHStart = []byte("#ifndef WUFFS_BASE_HEADER_H\n")
|
||||
wuffsBaseHeaderHEnd = []byte("#endif // WUFFS_BASE_HEADER_H\n")
|
||||
)
|
||||
|
||||
func (g *gen) writeUse(b *buffer, n *a.Use) error {
|
||||
useDirname := g.tm.ByID(n.Path())
|
||||
useDirname, _ = t.Unescape(useDirname)
|
||||
|
||||
// TODO: sanity check useDirname?
|
||||
// TODO: sanity check useDirname via commonflags.IsValidUsePath?
|
||||
|
||||
if g.usesSeen == nil {
|
||||
g.usesSeen = map[string]struct{}{}
|
||||
@@ -590,7 +600,37 @@ func (g *gen) writeUse(b *buffer, n *a.Use) error {
|
||||
}
|
||||
g.usesSeen[useDirname] = struct{}{}
|
||||
|
||||
if g.wuffsRoot == "" {
|
||||
var err error
|
||||
g.wuffsRoot, err = cf.WuffsRoot()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
hdrFilename := filepath.Join(g.wuffsRoot, "gen", "h", filepath.FromSlash(useDirname)+".h")
|
||||
hdr, err := ioutil.ReadFile(hdrFilename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
b.printf("// ---------------- BEGIN USE %q\n\n", useDirname)
|
||||
|
||||
// Inline the previously generated .h file, stripping out the redundant
|
||||
// copy of the WUFFS_BASE_HEADER_H code.
|
||||
if i := bytes.Index(hdr, wuffsBaseHeaderHStart); i < 0 {
|
||||
return fmt.Errorf("use %q: previously generated header %q could not be inlined", useDirname, hdrFilename)
|
||||
} else {
|
||||
b.Write(hdr[:i])
|
||||
hdr = hdr[i+len(wuffsBaseHeaderHStart):]
|
||||
}
|
||||
if i := bytes.Index(hdr, wuffsBaseHeaderHEnd); i < 0 {
|
||||
return fmt.Errorf("use %q: previously generated header %q could not be inlined", useDirname, hdrFilename)
|
||||
} else {
|
||||
b.Write(hdr[i+len(wuffsBaseHeaderHEnd):])
|
||||
}
|
||||
|
||||
b.writeb('\n')
|
||||
b.printf("// ---------------- END USE %q\n\n", useDirname)
|
||||
return nil
|
||||
}
|
||||
|
||||
+3
-1
@@ -27,6 +27,8 @@ import (
|
||||
|
||||
"github.com/google/wuffs/lang/generate"
|
||||
|
||||
cf "github.com/google/wuffs/cmd/commonflags"
|
||||
|
||||
a "github.com/google/wuffs/lang/ast"
|
||||
t "github.com/google/wuffs/lang/token"
|
||||
)
|
||||
@@ -89,7 +91,7 @@ func (h *genHelper) gen(dirname string, recursive bool) error {
|
||||
}
|
||||
h.seen[dirname] = struct{}{}
|
||||
|
||||
if dirname != path.Clean(dirname) || dirname == "" || dirname[0] == '.' || dirname[0] == '/' {
|
||||
if !cf.IsValidUsePath(dirname) {
|
||||
return fmt.Errorf("invalid package path %q", dirname)
|
||||
}
|
||||
|
||||
|
||||
+3
-14
@@ -16,14 +16,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"go/build"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
cf "github.com/google/wuffs/cmd/commonflags"
|
||||
)
|
||||
|
||||
var commands = []struct {
|
||||
@@ -63,7 +63,7 @@ func main1() error {
|
||||
flag.Usage = usage
|
||||
flag.Parse()
|
||||
|
||||
wuffsRoot, err := findWuffsRoot()
|
||||
wuffsRoot, err := cf.WuffsRoot()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -79,17 +79,6 @@ func main1() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func findWuffsRoot() (string, error) {
|
||||
// TODO: look for a WUFFSROOT environment variable?
|
||||
for _, p := range filepath.SplitList(build.Default.GOPATH) {
|
||||
p = filepath.Join(p, "src", "github.com", "google", "wuffs")
|
||||
if o, err := os.Stat(p); err == nil && o.IsDir() {
|
||||
return p, nil
|
||||
}
|
||||
}
|
||||
return "", errors.New("could not find Wuffs root directory")
|
||||
}
|
||||
|
||||
func listDir(wuffsRoot string, dirname string, returnSubdirs bool) (filenames []string, dirnames []string, err error) {
|
||||
f, err := os.Open(filepath.Join(wuffsRoot, filepath.FromSlash(dirname)))
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user