diff --git a/cmd/wuffs-c/genlib.go b/cmd/wuffs-c/genlib.go index c9889c8f..460d5001 100644 --- a/cmd/wuffs-c/genlib.go +++ b/cmd/wuffs-c/genlib.go @@ -82,7 +82,7 @@ func genObj(outDir string, inDir string, cc string, dynamism string, filenames [ out := genlibOutFilename(outDir, dynamism, filename) args := []string(nil) - args = append(args, "-x", "c", "-O3", "-std=c99", "-DWUFFS_IMPLEMENTATION") + args = append(args, "-x", "c", "-O3", "-std=c99", "-DWUFFS_IMPLEMENTATION", "-I", inDir) if dynamism == "dynamic" { args = append(args, "-fPIC", "-DPIC") } diff --git a/cmd/wuffs-c/internal/cgen/base/base-impl.c b/cmd/wuffs-c/internal/cgen/base/base-impl.c index 30c595d2..876858e5 100644 --- a/cmd/wuffs-c/internal/cgen/base/base-impl.c +++ b/cmd/wuffs-c/internal/cgen/base/base-impl.c @@ -14,6 +14,16 @@ // See the License for the specific language governing permissions and // limitations under the License. +#ifndef WUFFS_INCLUDE_GUARD__BASE +#define WUFFS_INCLUDE_GUARD__BASE + +#if defined(WUFFS_IMPLEMENTATION) && !defined(WUFFS_CONFIG__MODULES) +#define WUFFS_CONFIG__MODULES +#define WUFFS_CONFIG__MODULE__BASE +#endif + +// !! WUFFS MONOLITHIC RELEASE DISCARDS EVERYTHING ABOVE. + // !! INSERT base-public.h. // WUFFS C HEADER ENDS HERE. @@ -29,3 +39,7 @@ // defined(WUFFS_CONFIG__MODULE__BASE) #endif // WUFFS_IMPLEMENTATION + +// !! WUFFS MONOLITHIC RELEASE DISCARDS EVERYTHING BELOW. + +#endif // WUFFS_INCLUDE_GUARD__BASE diff --git a/cmd/wuffs-c/internal/cgen/base/base-private.h b/cmd/wuffs-c/internal/cgen/base/base-private.h index 9f61b440..b62cb629 100644 --- a/cmd/wuffs-c/internal/cgen/base/base-private.h +++ b/cmd/wuffs-c/internal/cgen/base/base-private.h @@ -1,8 +1,5 @@ // After editing this file, run "go generate" in the parent directory. -#ifndef WUFFS_INCLUDE_GUARD__BASE_PRIVATE -#define WUFFS_INCLUDE_GUARD__BASE_PRIVATE - // Copyright 2017 The Wuffs Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -722,5 +719,3 @@ wuffs_base__io_writer__set_mark(wuffs_base__io_writer* o, uint8_t* mark) { #ifdef __cplusplus } // extern "C" #endif - -#endif // WUFFS_INCLUDE_GUARD__BASE_PRIVATE diff --git a/cmd/wuffs-c/internal/cgen/cgen.go b/cmd/wuffs-c/internal/cgen/cgen.go index fbeb4ed8..8ad0e3f6 100644 --- a/cmd/wuffs-c/internal/cgen/cgen.go +++ b/cmd/wuffs-c/internal/cgen/cgen.go @@ -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 { diff --git a/cmd/wuffs-c/internal/cgen/data.go b/cmd/wuffs-c/internal/cgen/data.go index b52c801a..32e66b3a 100644 --- a/cmd/wuffs-c/internal/cgen/data.go +++ b/cmd/wuffs-c/internal/cgen/data.go @@ -76,10 +76,10 @@ const baseBasePublicH = "" + "" const baseBasePrivateH = "" + - "#ifndef WUFFS_INCLUDE_GUARD__BASE_PRIVATE\n#define WUFFS_INCLUDE_GUARD__BASE_PRIVATE\n\n// Copyright 2017 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#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#define WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x)\n\nstatic inline void wuffs_base__ignore_check_wuffs_version_status(\n wuffs_base__status z) {}\n\n// WUFFS_BASE__MAGIC is a magic number to check that initializers are called.\n// It's not foolproof, given C doesn't automatically zero me" + - "mory before use,\n// but it should catch 99.99% of cases.\n//\n// Its (non-zero) value is arbitrary, based on md5sum(\"wuffs\").\n#define WUFFS_BASE__MAGIC ((uint32_t)0x3CCB6C71)\n\n// WUFFS_BASE__DISABLED is a magic number to indicate that a non-recoverable\n// error was previously encountered.\n//\n// Its (non-zero) value is arbitrary, based on md5sum(\"disabled\").\n#define WUFFS_BASE__DISABLED ((uint32_t)0x075AE3D2)\n\n// Denote intentional fallthroughs for -Wimplicit-fallthrough.\n//\n// The order matters here. Clang also defines \"__GNUC__\".\n#if defined(__clang__) && __cplusplus >= 201103L\n#define WUFFS_BASE__FALLTHROUGH [[clang::fallthrough]]\n#elif !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 7)\n#define WUFFS_BASE__FALLTHROUGH __attribute__((fallthrough))\n#else\n#define WUFFS_BASE__FALLTHROUGH\n#endif\n\n// Use switch cases for coroutine suspension points, similar to the technique\n// in https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html\n//\n// We use trivial macros instead of an explicit assignment and c" + - "ase statement\n// so that clang-format doesn't get confused by the unusual \"case\"s.\n#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0 case 0:;\n#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT(n) \\\n coro_susp_point = n; \\\n WUFFS_BASE__FALLTHROUGH; \\\n case n:;\n\n#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(n) \\\n if (!status) { \\\n goto ok; \\\n } else if (*status != '$') { \\\n goto exit; \\\n } \\\n coro_susp_point = n; \\\n goto suspend; \\\n case n:;\n\n// Clang also defines \"__GNUC__\".\n#if defined(__GNUC__)\n#define WUFFS_BASE__LIKELY(expr) (__builtin_expect(!!(expr), 1))\n#define WUFFS_BASE__UNLIKELY(expr) (__builtin_expect(!" + - "!(expr), 0))\n#else\n#define WUFFS_BASE__LIKELY(expr) (expr)\n#define WUFFS_BASE__UNLIKELY(expr) (expr)\n#endif\n\n// The helpers below are functions, instead of macros, because their arguments\n// can be an expression that we shouldn't evaluate more than once.\n//\n// They are static, so that linking multiple wuffs .o files won't complain about\n// duplicate function definitions.\n//\n// They are explicitly marked inline, even if modern compilers don't use the\n// inline attribute to guide optimizations such as inlining, to avoid the\n// -Wunused-function warning, and we like to compile with -Wall -Werror.\n\nstatic inline wuffs_base__empty_struct //\nwuffs_base__return_empty_struct() {\n return ((wuffs_base__empty_struct){});\n}\n\n" + + "// Copyright 2017 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#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#define WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x)\n\nstatic inline void wuffs_base__ignore_check_wuffs_version_status(\n wuffs_base__status z) {}\n\n// WUFFS_BASE__MAGIC is a magic number to check that initializers are called.\n// It's not foolproof, given C doesn't automatically zero memory before use,\n// but it should catch 99.99% of cases.\n//\n// Its (non-zero) value i" + + "s arbitrary, based on md5sum(\"wuffs\").\n#define WUFFS_BASE__MAGIC ((uint32_t)0x3CCB6C71)\n\n// WUFFS_BASE__DISABLED is a magic number to indicate that a non-recoverable\n// error was previously encountered.\n//\n// Its (non-zero) value is arbitrary, based on md5sum(\"disabled\").\n#define WUFFS_BASE__DISABLED ((uint32_t)0x075AE3D2)\n\n// Denote intentional fallthroughs for -Wimplicit-fallthrough.\n//\n// The order matters here. Clang also defines \"__GNUC__\".\n#if defined(__clang__) && __cplusplus >= 201103L\n#define WUFFS_BASE__FALLTHROUGH [[clang::fallthrough]]\n#elif !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 7)\n#define WUFFS_BASE__FALLTHROUGH __attribute__((fallthrough))\n#else\n#define WUFFS_BASE__FALLTHROUGH\n#endif\n\n// Use switch cases for coroutine suspension points, similar to the technique\n// in https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html\n//\n// We use trivial macros instead of an explicit assignment and case statement\n// so that clang-format doesn't get confused by the unusual \"case\"s.\n#d" + + "efine WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0 case 0:;\n#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT(n) \\\n coro_susp_point = n; \\\n WUFFS_BASE__FALLTHROUGH; \\\n case n:;\n\n#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(n) \\\n if (!status) { \\\n goto ok; \\\n } else if (*status != '$') { \\\n goto exit; \\\n } \\\n coro_susp_point = n; \\\n goto suspend; \\\n case n:;\n\n// Clang also defines \"__GNUC__\".\n#if defined(__GNUC__)\n#define WUFFS_BASE__LIKELY(expr) (__builtin_expect(!!(expr), 1))\n#define WUFFS_BASE__UNLIKELY(expr) (__builtin_expect(!!(expr), 0))\n#else\n#define WUFFS_BASE__LIKELY(expr) (expr)\n#define WUFFS_BASE__UNLIKE" + + "LY(expr) (expr)\n#endif\n\n// The helpers below are functions, instead of macros, because their arguments\n// can be an expression that we shouldn't evaluate more than once.\n//\n// They are static, so that linking multiple wuffs .o files won't complain about\n// duplicate function definitions.\n//\n// They are explicitly marked inline, even if modern compilers don't use the\n// inline attribute to guide optimizations such as inlining, to avoid the\n// -Wunused-function warning, and we like to compile with -Wall -Werror.\n\nstatic inline wuffs_base__empty_struct //\nwuffs_base__return_empty_struct() {\n return ((wuffs_base__empty_struct){});\n}\n\n" + "" + "// ---------------- Numeric Types\n\nstatic inline uint8_t //\nwuffs_base__load_u8be(uint8_t* p) {\n return p[0];\n}\n\nstatic inline uint16_t //\nwuffs_base__load_u16be(uint8_t* p) {\n return ((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1]) << 0);\n}\n\nstatic inline uint16_t //\nwuffs_base__load_u16le(uint8_t* p) {\n return ((uint16_t)(p[0]) << 0) | ((uint16_t)(p[1]) << 8);\n}\n\nstatic inline uint32_t //\nwuffs_base__load_u24be(uint8_t* p) {\n return ((uint32_t)(p[0]) << 16) | ((uint32_t)(p[1]) << 8) |\n ((uint32_t)(p[2]) << 0);\n}\n\nstatic inline uint32_t //\nwuffs_base__load_u24le(uint8_t* p) {\n return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) |\n ((uint32_t)(p[2]) << 16);\n}\n\nstatic inline uint32_t //\nwuffs_base__load_u32be(uint8_t* p) {\n return ((uint32_t)(p[0]) << 24) | ((uint32_t)(p[1]) << 16) |\n ((uint32_t)(p[2]) << 8) | ((uint32_t)(p[3]) << 0);\n}\n\nstatic inline uint32_t //\nwuffs_base__load_u32le(uint8_t* p) {\n return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) |\n ((uin" + "t32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24);\n}\n\nstatic inline uint64_t //\nwuffs_base__load_u40be(uint8_t* p) {\n return ((uint64_t)(p[0]) << 32) | ((uint64_t)(p[1]) << 24) |\n ((uint64_t)(p[2]) << 16) | ((uint64_t)(p[3]) << 8) |\n ((uint64_t)(p[4]) << 0);\n}\n\nstatic inline uint64_t //\nwuffs_base__load_u40le(uint8_t* p) {\n return ((uint64_t)(p[0]) << 0) | ((uint64_t)(p[1]) << 8) |\n ((uint64_t)(p[2]) << 16) | ((uint64_t)(p[3]) << 24) |\n ((uint64_t)(p[4]) << 32);\n}\n\nstatic inline uint64_t //\nwuffs_base__load_u48be(uint8_t* p) {\n return ((uint64_t)(p[0]) << 40) | ((uint64_t)(p[1]) << 32) |\n ((uint64_t)(p[2]) << 24) | ((uint64_t)(p[3]) << 16) |\n ((uint64_t)(p[4]) << 8) | ((uint64_t)(p[5]) << 0);\n}\n\nstatic inline uint64_t //\nwuffs_base__load_u48le(uint8_t* p) {\n return ((uint64_t)(p[0]) << 0) | ((uint64_t)(p[1]) << 8) |\n ((uint64_t)(p[2]) << 16) | ((uint64_t)(p[3]) << 24) |\n ((uint64_t)(p[4]) << 32) | ((uint64_t)(p[5]) << 40);\n}\n\nstatic inline u" + @@ -110,11 +110,12 @@ const baseBasePrivateH = "" + "r (; n >= 3; n -= 3) {\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n }\n for (; n; n--) {\n *ptr++ = *start++;\n }\n *ptr_ptr = ptr;\n return length;\n}\n\nstatic inline uint32_t //\nwuffs_base__io_writer__copy_n_from_reader(uint8_t** ptr_ioptr_w,\n uint8_t* iobounds1_w,\n uint32_t length,\n uint8_t** ptr_ioptr_r,\n uint8_t* iobounds1_r) {\n uint8_t* ioptr_w = *ptr_ioptr_w;\n size_t n = length;\n if (n > ((size_t)(iobounds1_w - ioptr_w))) {\n n = iobounds1_w - ioptr_w;\n }\n uint8_t* ioptr_r = *ptr_ioptr_r;\n if (n > ((size_t)(iobounds1_r - ioptr_r))) {\n n = iobounds1_r - ioptr_r;\n }\n if (n > 0) {\n memmove(ioptr_w, ioptr_r, n);\n *ptr_ioptr_w += n;\n *ptr_ioptr_r += n;\n }\n return n;\n}\n\nstatic inline uint64_t //\nwuffs_base__io_writer__copy_from_slice(uint8_t** ptr_ioptr_w,\n u" + "int8_t* iobounds1_w,\n wuffs_base__slice_u8 src) {\n uint8_t* ioptr_w = *ptr_ioptr_w;\n size_t n = src.len;\n if (n > ((size_t)(iobounds1_w - ioptr_w))) {\n n = iobounds1_w - ioptr_w;\n }\n if (n > 0) {\n memmove(ioptr_w, src.ptr, n);\n *ptr_ioptr_w += n;\n }\n return n;\n}\n\nstatic inline uint32_t //\nwuffs_base__io_writer__copy_n_from_slice(uint8_t** ptr_ioptr_w,\n uint8_t* iobounds1_w,\n uint32_t length,\n wuffs_base__slice_u8 src) {\n uint8_t* ioptr_w = *ptr_ioptr_w;\n size_t n = src.len;\n if (n > length) {\n n = length;\n }\n if (n > ((size_t)(iobounds1_w - ioptr_w))) {\n n = iobounds1_w - ioptr_w;\n }\n if (n > 0) {\n memmove(ioptr_w, src.ptr, n);\n *ptr_ioptr_w += n;\n }\n return n;\n}\n\nstatic inline wuffs_base__empty_struct //\nwuffs_base__io_reader__set_limit(wuffs_base__io_reader* o,\n uint8_t* ioptr_r,\n " + " uint64_t limit) {\n if (o && (((size_t)(o->private_impl.limit - ioptr_r)) > limit)) {\n o->private_impl.limit = ioptr_r + limit;\n }\n return ((wuffs_base__empty_struct){});\n}\n\nstatic inline wuffs_base__empty_struct //\nwuffs_base__io_reader__set_mark(wuffs_base__io_reader* o, uint8_t* mark) {\n o->private_impl.mark = mark;\n return ((wuffs_base__empty_struct){});\n}\n\nstatic inline wuffs_base__empty_struct //\nwuffs_base__io_writer__set(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->data.ptr = s.ptr;\n b->data.len = s.len;\n b->meta.wi = 0;\n b->meta.ri = 0;\n b->meta.pos = 0;\n b->meta.closed = false;\n\n o->private_impl.buf = b;\n o->private_impl.mark = s.ptr;\n o->private_impl.limit = 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 //\nwuffs_base__io_writer__set_mark(wuffs_base__io_writer* o, uint8_t* mark) {\n o->private_impl.mark = mark;\n return ((wuffs_base__empty_struct){});\n}\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n\n#endif // WUFFS_INCLUDE_GUARD__BASE_PRIVATE\n" + + ";\n}\n\nstatic inline wuffs_base__empty_struct //\nwuffs_base__io_writer__set_mark(wuffs_base__io_writer* o, uint8_t* mark) {\n o->private_impl.mark = mark;\n return ((wuffs_base__empty_struct){});\n}\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n" + "" const baseBaseImplC = "" + - "// 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// !! INSERT base-public.h.\n\n// WUFFS C HEADER ENDS HERE.\n#ifdef WUFFS_IMPLEMENTATION\n\n// !! INSERT base-private.h.\n\n#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BASE)\n\n// !! INSERT wuffs_base__status strings.\n\n#endif // !defined(WUFFS_CONFIG__MODULES) ||\n // defined(WUFFS_CONFIG__MODULE__BASE)\n\n#endif // WUFFS_IMPLEMENTATION\n" + + "// 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#ifndef WUFFS_INCLUDE_GUARD__BASE\n#define WUFFS_INCLUDE_GUARD__BASE\n\n#if defined(WUFFS_IMPLEMENTATION) && !defined(WUFFS_CONFIG__MODULES)\n#define WUFFS_CONFIG__MODULES\n#define WUFFS_CONFIG__MODULE__BASE\n#endif\n\n// !! WUFFS MONOLITHIC RELEASE DISCARDS EVERYTHING ABOVE.\n\n// !! INSERT base-public.h.\n\n// WUFFS C HEADER ENDS HERE.\n#ifdef WUFFS_IMPLEMENTATION\n\n// !! INSERT base-private.h.\n\n#if !defined(WUFFS_CONFIG__MODULES) || de" + + "fined(WUFFS_CONFIG__MODULE__BASE)\n\n// !! INSERT wuffs_base__status strings.\n\n#endif // !defined(WUFFS_CONFIG__MODULES) ||\n // defined(WUFFS_CONFIG__MODULE__BASE)\n\n#endif // WUFFS_IMPLEMENTATION\n\n// !! WUFFS MONOLITHIC RELEASE DISCARDS EVERYTHING BELOW.\n\n#endif // WUFFS_INCLUDE_GUARD__BASE\n" + "" const baseImagePublicH = "" + diff --git a/cmd/wuffs-c/release.go b/cmd/wuffs-c/release.go index 20fc315d..31acc647 100644 --- a/cmd/wuffs-c/release.go +++ b/cmd/wuffs-c/release.go @@ -21,6 +21,9 @@ import ( "io/ioutil" "os" "os/exec" + "path/filepath" + "sort" + "strings" "time" cf "github.com/google/wuffs/cmd/commonflags" @@ -47,54 +50,63 @@ func doGenrelease(args []string) error { } args = flags.Args() - h := &genReleaseHelper{ - seen: map[string]bool{}, - revision: *revisionFlag, - version: v, + // Calculate the base directory. + baseDir := "" + for _, filename := range args { + if filepath.Base(filename) != "base.h" { + continue + } + baseDir = filepath.Dir(filename) } + if baseDir == "" { + return fmt.Errorf("could not determine base directory") + } + baseDirSlash := baseDir + string(filepath.Separator) + + h := &genReleaseHelper{ + filesList: nil, + filesMap: map[string]parsedCFile{}, + seen: nil, + revision: *revisionFlag, + version: v, + } + + for _, filename := range args { + if !strings.HasPrefix(filename, baseDirSlash) { + return fmt.Errorf("filename %q is not under base directory %q", filename, baseDir) + } + relFilename := filename[len(baseDirSlash):] + + s, err := ioutil.ReadFile(filename) + if err != nil { + return err + } + + if err := h.parse(relFilename, s); err != nil { + return err + } + } + sort.Strings(h.filesList) unformatted := bytes.NewBuffer(nil) unformatted.WriteString("#ifndef WUFFS_INCLUDE_GUARD\n") unformatted.WriteString("#define WUFFS_INCLUDE_GUARD\n\n") unformatted.WriteString(grSingleFileGuidance) - // First, cat all of the headers together, filtering out duplicate - // WUFFS_INCLUDE_GUARD__FOO sections and overriding WUFFS_VERSION. - implementations := [][]byte(nil) - for _, filename := range args { - s, err := ioutil.ReadFile(filename) - if err != nil { + h.seen = map[string]bool{} + for _, f := range h.filesList { + if err := h.gen(unformatted, f, 0, 0); err != nil { return err } - - trimmed := []byte(nil) - if i := bytes.Index(s, grImplStartsHere); i >= 0 { - remaining := s[i+len(grImplStartsHere):] - if j := bytes.Index(remaining, grImplEndsHere); j >= 0 { - implementations = append(implementations, remaining[:j]) - trimmed = append(trimmed, s[:i]...) - trimmed = append(trimmed, '\n') - trimmed = append(trimmed, remaining[j+len(grImplEndsHere):]...) - s = trimmed - } - } - if len(trimmed) == 0 { - return fmt.Errorf("could not find %q or %q in %s", grImplStartsHere, grImplEndsHere, filename) - } - - if err := h.gen(unformatted, s); err != nil { - return fmt.Errorf("%v in %s", err, filename) - } } unformatted.Write(grImplStartsHere) unformatted.WriteString("\n") - // Then, cat all of the implementations together, filtering out duplicate - // WUFFS_INCLUDE_GUARD__BASE_PRIVATE sections. - for i, s := range implementations { - if err := h.gen(unformatted, s); err != nil { - return fmt.Errorf("%v in %s", err, args[i]) + h.seen = map[string]bool{} + for _, f := range h.filesList { + if err := h.gen(unformatted, f, 1, 0); err != nil { + return err } } @@ -112,13 +124,12 @@ func doGenrelease(args []string) error { var ( grImplStartsHere = []byte("\n// WUFFS C HEADER ENDS HERE.\n#ifdef WUFFS_IMPLEMENTATION\n") grImplEndsHere = []byte("#endif // WUFFS_IMPLEMENTATION\n") + grIncludeQuote = []byte("#include \"") grNN = []byte("\n\n") grVOverride = []byte("// !! Some code generation programs can override WUFFS_VERSION.\n") grVString = []byte(`#define WUFFS_VERSION_STRING "0.0.0"`) - - grWigDefine = []byte("#define WUFFS_INCLUDE_GUARD__") - grWigEndif = []byte("#endif // WUFFS_INCLUDE_GUARD__") - grWigIfndef = []byte("#ifndef WUFFS_INCLUDE_GUARD__") + grWmrAbove = []byte("// !! WUFFS MONOLITHIC RELEASE DISCARDS EVERYTHING ABOVE.\n") + grWmrBelow = []byte("// !! WUFFS MONOLITHIC RELEASE DISCARDS EVERYTHING BELOW.\n") ) const grSingleFileGuidance = ` @@ -131,105 +142,134 @@ const grSingleFileGuidance = ` ` -type genReleaseHelper struct { - seen map[string]bool - revision string - version cf.Version +type parsedCFile struct { + includes []string + // fragments[0] is the header, fragments[1] is the implementation. + fragments [2][]byte } -func (h *genReleaseHelper) gen(w *bytes.Buffer, s []byte) error { - skipping := 0 - stack := []string{} +type genReleaseHelper struct { + filesList []string + filesMap map[string]parsedCFile + seen map[string]bool + revision string + version cf.Version +} +func (h *genReleaseHelper) parse(relFilename string, s []byte) error { + if _, ok := h.filesMap[relFilename]; ok { + return fmt.Errorf("duplicate %q", relFilename) + } + + f := parsedCFile{} + + if i := bytes.Index(s, grWmrAbove); i < 0 { + return fmt.Errorf("could not find %q in %s", grWmrAbove, relFilename) + } else { + f.includes = parseIncludes(s[:i]) + s = s[i+len(grWmrAbove):] + } + + if i := bytes.LastIndex(s, grWmrBelow); i < 0 { + return fmt.Errorf("could not find %q in %s", grWmrBelow, relFilename) + } else { + s = s[:i] + } + + if i := bytes.Index(s, grImplStartsHere); i < 0 { + return fmt.Errorf("could not find %q in %s", grImplStartsHere, relFilename) + } else { + f.fragments[0], s = s[:i], s[i+len(grImplStartsHere):] + } + + if i := bytes.LastIndex(s, grImplEndsHere); i < 0 { + return fmt.Errorf("could not find %q in %s", grImplEndsHere, relFilename) + } else { + f.fragments[1] = s[:i] + } + + if relFilename == "base.h" && (h.version != cf.Version{}) { + if subs, err := h.substituteWuffsVersion(f.fragments[0]); err != nil { + return err + } else { + f.fragments[0] = subs + } + } + + h.filesList = append(h.filesList, relFilename) + h.filesMap[relFilename] = f + return nil +} + +func parseIncludes(s []byte) (ret []string) { for remaining := []byte(nil); len(s) > 0; s, remaining = remaining, nil { if i := bytes.IndexByte(s, '\n'); i >= 0 { s, remaining = s[:i+1], s[i+1:] } - if len(s) == 0 { + if len(s) == 0 || s[0] != '#' || !bytes.HasPrefix(s, grIncludeQuote) { continue } - - if s[0] == '#' { - switch { - case bytes.HasPrefix(s, grWigIfndef): - pkg := string(s[len(grWigIfndef):]) - for _, p := range stack { - if p == pkg { - return fmt.Errorf("unexpected %q", s) - } - } - - if h.seen[pkg] { - skipping++ - } - - stack = append(stack, pkg) - continue - - case bytes.HasPrefix(s, grWigDefine): - pkg := string(s[len(grWigDefine):]) - if (len(stack) == 0) || (stack[len(stack)-1] != pkg) { - return fmt.Errorf("unexpected %q", s) - } - continue - - case bytes.HasPrefix(s, grWigEndif): - pkg := string(s[len(grWigEndif):]) - if (len(stack) == 0) || (stack[len(stack)-1] != pkg) { - return fmt.Errorf("unexpected %q", s) - } - - if h.seen[pkg] { - skipping-- - } else { - h.seen[pkg] = true - } - - stack = stack[:len(stack)-1] - continue - } - } - - if skipping > 0 { + s = s[len(grIncludeQuote):] + if len(s) < 2 || s[len(s)-2] != '"' || s[len(s)-1] != '\n' { continue } + s = s[:len(s)-2] - if s[0] == '/' { - switch { - case bytes.Equal(s, grVOverride): - if (h.version == cf.Version{}) { - break - } - var err error - remaining, err = h.genWuffsVersion(w, remaining) - if err != nil { - return err - } - continue + ret = append(ret, string(s)) + } + sort.Strings(ret) + return ret +} + +func (h *genReleaseHelper) gen(w *bytes.Buffer, relFilename string, which int, depth uint32) error { + if depth > 1024 { + return fmt.Errorf("genrelease recursion depth too large") + } + depth++ + + if h.seen[relFilename] { + return nil + } + f, ok := h.filesMap[relFilename] + if !ok { + return fmt.Errorf("cannot resolve %q", relFilename) + } + + // For headers, process the files in #include-ee before #include-er order. + if which == 0 { + for _, inc := range f.includes { + if err := h.gen(w, inc, which, depth); err != nil { + return err } } - - w.Write(s) } - if len(stack) != 0 { - return fmt.Errorf("unmatched %q", string(grWigIfndef)+stack[len(stack)-1]) - } + w.Write(f.fragments[which]) + h.seen[relFilename] = true return nil } -func (h *genReleaseHelper) genWuffsVersion(w *bytes.Buffer, s []byte) (remaining []byte, err error) { - cut := []byte(nil) - if i := bytes.Index(s, grNN); i >= 0 { - cut, s = s[:i], s[i+len(grNN):] +func (h *genReleaseHelper) substituteWuffsVersion(s []byte) ([]byte, error) { + ret := []byte(nil) + if i := bytes.Index(s, grVOverride); i < 0 { + return nil, fmt.Errorf("could not find %q in %s", grVOverride, "base.h") } else { + ret = append(ret, s[:i]...) + s = s[i+len(grVOverride):] + } + + cut := []byte(nil) + if i := bytes.Index(s, grNN); i < 0 { return nil, fmt.Errorf(`could not find "\n\n" near WUFFS_VERSION`) + } else { + cut, s = s[:i], s[i+len(grNN):] } if !bytes.HasSuffix(cut, grVString) { return nil, fmt.Errorf("%q did not end with %q", cut, grVString) } + w := bytes.NewBuffer(nil) fmt.Fprintf(w, "// WUFFS_VERSION was overridden by \"wuffs gen -version\" on %v UTC", time.Now().UTC().Format("2006-01-02")) if h.revision != "" { @@ -247,5 +287,7 @@ func (h *genReleaseHelper) genWuffsVersion(w *bytes.Buffer, s []byte) (remaining `, h.version.Uint64(), h.version.Major, h.version.Minor, h.version.Patch, h.version.Extension, h.version) - return s, nil + ret = append(ret, w.Bytes()...) + ret = append(ret, s...) + return ret, nil } diff --git a/release/c/wuffs-unsupported-snapshot.h b/release/c/wuffs-unsupported-snapshot.h index f590810d..4c8e365b 100644 --- a/release/c/wuffs-unsupported-snapshot.h +++ b/release/c/wuffs-unsupported-snapshot.h @@ -8,22 +8,6 @@ // "foo.h"-like header, #define WUFFS_IMPLEMENTATION before #include'ing or // compiling it. -// 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. - -// Code generated by wuffs-c. DO NOT EDIT. - // Copyright 2017 The Wuffs Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -2134,10 +2118,6 @@ typedef struct { } // extern "C" #endif -// Code generated by wuffs-c. DO NOT EDIT. - -// ---------------- Use Declarations - #ifdef __cplusplus extern "C" { #endif @@ -2219,10 +2199,6 @@ struct wuffs_adler32__hasher__struct { } // extern "C" #endif -// Code generated by wuffs-c. DO NOT EDIT. - -// ---------------- Use Declarations - #ifdef __cplusplus extern "C" { #endif @@ -2303,10 +2279,6 @@ struct wuffs_crc32__ieee_hasher__struct { } // extern "C" #endif -// Code generated by wuffs-c. DO NOT EDIT. - -// ---------------- Use Declarations - #ifdef __cplusplus extern "C" { #endif @@ -2461,16 +2433,6 @@ struct wuffs_deflate__decoder__struct { } // extern "C" #endif -// Code generated by wuffs-c. DO NOT EDIT. - -// ---------------- Use Declarations - -// ---------------- BEGIN USE "std/lzw" - -// Code generated by wuffs-c. DO NOT EDIT. - -// ---------------- Use Declarations - #ifdef __cplusplus extern "C" { #endif @@ -2592,8 +2554,6 @@ struct wuffs_lzw__decoder__struct { } // extern "C" #endif -// ---------------- END USE "std/lzw" - #ifdef __cplusplus extern "C" { #endif @@ -2854,18 +2814,6 @@ struct wuffs_gif__decoder__struct { } // extern "C" #endif -// Code generated by wuffs-c. DO NOT EDIT. - -// ---------------- Use Declarations - -// ---------------- BEGIN USE "std/crc32" - -// ---------------- END USE "std/crc32" - -// ---------------- BEGIN USE "std/deflate" - -// ---------------- END USE "std/deflate" - #ifdef __cplusplus extern "C" { #endif @@ -2974,18 +2922,6 @@ struct wuffs_gzip__decoder__struct { } // extern "C" #endif -// Code generated by wuffs-c. DO NOT EDIT. - -// ---------------- Use Declarations - -// ---------------- BEGIN USE "std/adler32" - -// ---------------- END USE "std/adler32" - -// ---------------- BEGIN USE "std/deflate" - -// ---------------- END USE "std/deflate" - #ifdef __cplusplus extern "C" { #endif