mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-26 09:01:35 +01:00
De-duplicate the gen/c/* files
The gen/c/* files are not checked in, due to the .gitignore file In Wuffs version 0.1, they were checked in, and programs were expected to #include them directly. Another goal was for them to be self-contained, without any #include lines to other Wuffs files, so that they would be easy to build, without having to configure an -I dir to a C compiler. They therefore inlined what would otherwise be an #include. For example, the generated std/gif.h code would inline the contents of the std/lzw.h file. The genrelease program would have to de-duplicate these inlines in order to produce a clean, monolithic release/c/* file. In Wuffs version 0.2 (the latest version), programs don't #include gen/c/*. Instead, they #include the monolithic release/c/*. Avoiding the need to configure an -I directory isn't as important as it used to be. This change removes that inlining, using the simpler #include line, and the genrelease program is then simpler because it doesn't need to de-duplicate.
This commit is contained in:
1 parent
51a0b347ed
commit
61d03537c2
7 files changed
+222
-289
No files matched your search
@@ -21,11 +21,9 @@ import (
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"math/big"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
@@ -254,10 +252,6 @@ func insertBasePrivateH(buf *buffer) error {
|
||||
}
|
||||
|
||||
func insertBasePublicH(buf *buffer) error {
|
||||
buf.writes("// Code generated by wuffs-c. DO NOT EDIT.\n\n")
|
||||
buf.writes("#ifndef WUFFS_INCLUDE_GUARD__BASE_PUBLIC\n")
|
||||
buf.writes("#define WUFFS_INCLUDE_GUARD__BASE_PUBLIC\n\n")
|
||||
|
||||
if err := expandBangBangInsert(buf, baseBasePublicH, map[string]func(*buffer) error{
|
||||
"// !! INSERT wuffs_base__status names.\n": func(b *buffer) error {
|
||||
for _, z := range builtin.StatusList {
|
||||
@@ -278,7 +272,6 @@ func insertBasePublicH(buf *buffer) error {
|
||||
buf.writeb('\n')
|
||||
buf.writes(baseImagePublicH)
|
||||
|
||||
buf.writes("\n#endif // WUFFS_INCLUDE_GUARD__BASE_PUBLIC\n\n")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -294,12 +287,9 @@ type gen struct {
|
||||
statusMap map[t.QID]status
|
||||
structList []*a.Struct
|
||||
structMap map[t.QID]*a.Struct
|
||||
usesList []string
|
||||
usesMap map[string]struct{}
|
||||
|
||||
currFunk funk
|
||||
funks map[t.QQID]funk
|
||||
wuffsRoot string
|
||||
currFunk funk
|
||||
funks map[t.QQID]funk
|
||||
}
|
||||
|
||||
func (g *gen) generate() ([]byte, error) {
|
||||
@@ -337,6 +327,12 @@ func (g *gen) generate() ([]byte, error) {
|
||||
includeGuard := "WUFFS_INCLUDE_GUARD__" + strings.ToUpper(g.pkgName)
|
||||
b.printf("#ifndef %s\n#define %s\n\n", includeGuard, includeGuard)
|
||||
|
||||
if err := g.genIncludes(b); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
b.writes("// !! WUFFS MONOLITHIC RELEASE DISCARDS EVERYTHING ABOVE.\n\n")
|
||||
|
||||
if err := g.genHeader(b); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -346,20 +342,50 @@ func (g *gen) generate() ([]byte, error) {
|
||||
}
|
||||
b.writex(wiEnd)
|
||||
|
||||
b.writes("// !! WUFFS MONOLITHIC RELEASE DISCARDS EVERYTHING BELOW.\n\n")
|
||||
|
||||
b.printf("#endif // %s\n\n", includeGuard)
|
||||
return *b, nil
|
||||
}
|
||||
|
||||
func (g *gen) genIncludes(b *buffer) error {
|
||||
b.writes("#if defined(WUFFS_IMPLEMENTATION) && !defined(WUFFS_CONFIG__MODULES)\n")
|
||||
b.writes("#define WUFFS_CONFIG__MODULES\n")
|
||||
b.printf("#define WUFFS_CONFIG__MODULE__%s\n", strings.ToUpper(g.pkgName))
|
||||
b.writes("#endif\n\n")
|
||||
|
||||
usesList := []string(nil)
|
||||
usesMap := map[string]struct{}{}
|
||||
|
||||
for _, file := range g.files {
|
||||
for _, tld := range file.TopLevelDecls() {
|
||||
if tld.Kind() != a.KUse {
|
||||
continue
|
||||
}
|
||||
useDirname := g.tm.ByID(tld.AsUse().Path())
|
||||
useDirname, _ = t.Unescape(useDirname)
|
||||
|
||||
// TODO: sanity check useDirname via commonflags.IsValidUsePath?
|
||||
|
||||
if _, ok := usesMap[useDirname]; ok {
|
||||
continue
|
||||
}
|
||||
usesMap[useDirname] = struct{}{}
|
||||
usesList = append(usesList, useDirname)
|
||||
}
|
||||
}
|
||||
sort.Strings(usesList)
|
||||
|
||||
b.writes("#include \"base.h\"\n")
|
||||
for _, use := range usesList {
|
||||
b.printf("#include \"%s.h\"\n", use)
|
||||
}
|
||||
|
||||
b.writeb('\n')
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *gen) genHeader(b *buffer) error {
|
||||
if err := insertBasePublicH(b); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
b.writes("// ---------------- Use Declarations\n\n")
|
||||
if err := g.forEachUse(b, (*gen).writeUse); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
b.writes("\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n")
|
||||
|
||||
b.writes("// ---------------- Status Codes\n\n")
|
||||
@@ -415,9 +441,6 @@ func (g *gen) genHeader(b *buffer) error {
|
||||
}
|
||||
|
||||
func (g *gen) genImpl(b *buffer) error {
|
||||
b.writes(baseBasePrivateH)
|
||||
b.writeb('\n')
|
||||
|
||||
module := "!defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__" +
|
||||
strings.ToUpper(g.pkgName) + ")"
|
||||
b.printf("#if %s\n\n", module)
|
||||
@@ -515,20 +538,6 @@ func (g *gen) forEachStatus(b *buffer, v visibility, f func(*gen, *buffer, *a.St
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *gen) forEachUse(b *buffer, f func(*gen, *buffer, *a.Use) error) error {
|
||||
for _, file := range g.files {
|
||||
for _, tld := range file.TopLevelDecls() {
|
||||
if tld.Kind() != a.KUse {
|
||||
continue
|
||||
}
|
||||
if err := f(g, b, tld.AsUse()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *gen) cName(name string) string {
|
||||
return cName(name, g.pkgPrefix)
|
||||
}
|
||||
@@ -748,72 +757,8 @@ var (
|
||||
wiStartImpl = []byte("\n// WUFFS C HEADER ENDS HERE.\n#ifdef WUFFS_IMPLEMENTATION\n\n")
|
||||
wiStart = []byte("\n#ifdef WUFFS_IMPLEMENTATION\n\n")
|
||||
wiEnd = []byte("\n#endif // WUFFS_IMPLEMENTATION\n\n")
|
||||
|
||||
wigbpStart = []byte("\n#ifndef WUFFS_INCLUDE_GUARD__BASE_PUBLIC\n")
|
||||
wigbpEnd = []byte("\n#endif // WUFFS_INCLUDE_GUARD__BASE_PUBLIC\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 via commonflags.IsValidUsePath?
|
||||
|
||||
if g.usesMap == nil {
|
||||
g.usesMap = map[string]struct{}{}
|
||||
} else if _, ok := g.usesMap[useDirname]; ok {
|
||||
return nil
|
||||
}
|
||||
g.usesList = append(g.usesList, useDirname)
|
||||
g.usesMap[useDirname] = struct{}{}
|
||||
|
||||
if g.wuffsRoot == "" {
|
||||
var err error
|
||||
g.wuffsRoot, err = generate.WuffsRoot()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
useeFilename := filepath.Join(g.wuffsRoot, "gen", "c", filepath.FromSlash(useDirname)+".h")
|
||||
usee, err := ioutil.ReadFile(useeFilename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
trimmed := []byte(nil)
|
||||
if i := bytes.Index(usee, wiStartImpl); i >= 0 {
|
||||
remaining := usee[i+len(wiStartImpl):]
|
||||
if j := bytes.Index(remaining, wiEnd); j >= 0 {
|
||||
trimmed = append(trimmed, usee[:i]...)
|
||||
trimmed = append(trimmed, '\n')
|
||||
trimmed = append(trimmed, remaining[j+len(wiEnd):]...)
|
||||
usee = trimmed
|
||||
}
|
||||
}
|
||||
if len(trimmed) == 0 {
|
||||
return fmt.Errorf("use %q: previously generated code %q could not be inlined", useDirname, useeFilename)
|
||||
}
|
||||
|
||||
b.printf("// ---------------- BEGIN USE %q\n\n", useDirname)
|
||||
|
||||
// Inline the previously generated .h file, stripping out the redundant
|
||||
// copy of the WUFFS_INCLUDE_GUARD__BASE_PUBLIC code.
|
||||
if i := bytes.Index(usee, wigbpStart); i < 0 {
|
||||
return fmt.Errorf("use %q: previously generated code %q could not be inlined", useDirname, useeFilename)
|
||||
} else {
|
||||
b.Write(usee[:i])
|
||||
usee = usee[i+len(wigbpStart):]
|
||||
}
|
||||
if i := bytes.Index(usee, wigbpEnd); i < 0 {
|
||||
return fmt.Errorf("use %q: previously generated code %q could not be inlined", useDirname, useeFilename)
|
||||
} else {
|
||||
b.Write(usee[i+len(wigbpEnd):])
|
||||
}
|
||||
|
||||
b.printf("\n\n// ---------------- END USE %q\n\n", useDirname)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *gen) writeInitializerSignature(b *buffer, n *a.Struct, public bool) error {
|
||||
structName := n.QID().Str(g.tm)
|
||||
if public {
|
||||
|
||||
Reference in new issue
Block a user