From 2b631202bcd9cd86eec387bd98c1c2eb4b4afd50 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 29 Dec 2018 15:45:04 +1100 Subject: [PATCH] Generate WUFFS_VERSION_GIT_REV_LIST_COUNT --- cmd/wuffs-c/internal/cgen/base/core-public.h | 1 + cmd/wuffs-c/internal/cgen/data.go | 6 ++-- cmd/wuffs-c/release.go | 35 ++++++++++++-------- cmd/wuffs/release.go | 27 +++++++++++++-- release/c/wuffs-unsupported-snapshot.c | 1 + 5 files changed, 51 insertions(+), 19 deletions(-) diff --git a/cmd/wuffs-c/internal/cgen/base/core-public.h b/cmd/wuffs-c/internal/cgen/base/core-public.h index 72aa23af..8d308170 100644 --- a/cmd/wuffs-c/internal/cgen/base/core-public.h +++ b/cmd/wuffs-c/internal/cgen/base/core-public.h @@ -40,6 +40,7 @@ #define WUFFS_VERSION_PATCH ((uint64_t)0) #define WUFFS_VERSION_EXTENSION "" #define WUFFS_VERSION_STRING "0.0.0" +#define WUFFS_VERSION_GIT_REV_LIST_COUNT 0 // Define WUFFS_CONFIG__STATIC_FUNCTIONS to make all of Wuffs' functions have // static storage. The motivation is discussed in the "ALLOW STATIC diff --git a/cmd/wuffs-c/internal/cgen/data.go b/cmd/wuffs-c/internal/cgen/data.go index 609628bc..984ad2ea 100644 --- a/cmd/wuffs-c/internal/cgen/data.go +++ b/cmd/wuffs-c/internal/cgen/data.go @@ -58,9 +58,9 @@ const baseCorePrivateH = "" + const baseCorePublicH = "" + "// Wuffs assumes that:\n// - converting a uint32_t to a size_t will never overflow.\n// - converting a size_t to a uint64_t will never overflow.\n#ifdef __WORDSIZE\n#if (__WORDSIZE != 32) && (__WORDSIZE != 64)\n#error \"Wuffs requires a word size of either 32 or 64 bits\"\n#endif\n#endif\n\n// WUFFS_VERSION is the major.minor.patch version, as per https://semver.org/,\n// as a uint64_t. The major number is the high 32 bits. The minor number is the\n// middle 16 bits. The patch number is the low 16 bits. The version extension\n// (such as \"\", \"beta\" or \"rc.1\") is part of the string representation (such as\n// \"1.2.3-beta\") but not the uint64_t representation.\n//\n// All three of major, minor and patch being zero means that this is a\n// work-in-progress version, not a release version, and has no backwards or\n// forwards compatibility guarantees.\n//\n// !! Some code generation programs can override WUFFS_VERSION.\n#define WUFFS_VERSION ((uint64_t)0)\n#define WUFFS_VERSION_MAJOR ((uint64_t)0)\n#define WUFFS_VERSION_MINOR ((uint64_" + - "t)0)\n#define WUFFS_VERSION_PATCH ((uint64_t)0)\n#define WUFFS_VERSION_EXTENSION \"\"\n#define WUFFS_VERSION_STRING \"0.0.0\"\n\n// Define WUFFS_CONFIG__STATIC_FUNCTIONS to make all of Wuffs' functions have\n// static storage. The motivation is discussed in the \"ALLOW STATIC\n// IMPLEMENTATION\" section of\n// https://raw.githubusercontent.com/nothings/stb/master/docs/stb_howto.txt\n#ifdef WUFFS_CONFIG__STATIC_FUNCTIONS\n#define WUFFS_BASE__MAYBE_STATIC static\n#else\n#define WUFFS_BASE__MAYBE_STATIC\n#endif\n\n// Clang also defines \"__GNUC__\".\n#if defined(__GNUC__)\n#define WUFFS_BASE__WARN_UNUSED_RESULT __attribute__((warn_unused_result))\n#else\n#define WUFFS_BASE__WARN_UNUSED_RESULT\n#endif\n\n// wuffs_base__empty_struct is used when a Wuffs function returns an empty\n// struct. In C, if a function f returns void, you can't say \"x = f()\", but in\n// Wuffs, if a function g returns empty, you can say \"y = g()\".\ntypedef struct {\n // private_impl is a placeholder field. It isn't explicitly used, except that\n // without it, the sizeof " + - "a struct with no fields can differ across C/C++\n // compilers, and it is undefined behavior in C99. For example, gcc says that\n // the sizeof an empty struct is 0, and g++ says that it is 1. This leads to\n // ABI incompatibility if a Wuffs .c file is processed by one compiler and\n // its .h file with another compiler.\n //\n // Instead, we explicitly insert an otherwise unused field, so that the\n // sizeof this struct is always 1.\n uint8_t private_impl;\n} wuffs_base__empty_struct;\n\n// wuffs_base__utility is a placeholder receiver type. It enables what Java\n// calls static methods, as opposed to regular methods.\ntypedef struct {\n // private_impl is a placeholder field. It isn't explicitly used, except that\n // without it, the sizeof a struct with no fields can differ across C/C++\n // compilers, and it is undefined behavior in C99. For example, gcc says that\n // the sizeof an empty struct is 0, and g++ says that it is 1. This leads to\n // ABI incompatibility if a Wuffs .c file is processed by one com" + - "piler and\n // its .h file with another compiler.\n //\n // Instead, we explicitly insert an otherwise unused field, so that the\n // sizeof this struct is always 1.\n uint8_t private_impl;\n} wuffs_base__utility;\n\n" + + "t)0)\n#define WUFFS_VERSION_PATCH ((uint64_t)0)\n#define WUFFS_VERSION_EXTENSION \"\"\n#define WUFFS_VERSION_STRING \"0.0.0\"\n#define WUFFS_VERSION_GIT_REV_LIST_COUNT 0\n\n// Define WUFFS_CONFIG__STATIC_FUNCTIONS to make all of Wuffs' functions have\n// static storage. The motivation is discussed in the \"ALLOW STATIC\n// IMPLEMENTATION\" section of\n// https://raw.githubusercontent.com/nothings/stb/master/docs/stb_howto.txt\n#ifdef WUFFS_CONFIG__STATIC_FUNCTIONS\n#define WUFFS_BASE__MAYBE_STATIC static\n#else\n#define WUFFS_BASE__MAYBE_STATIC\n#endif\n\n// Clang also defines \"__GNUC__\".\n#if defined(__GNUC__)\n#define WUFFS_BASE__WARN_UNUSED_RESULT __attribute__((warn_unused_result))\n#else\n#define WUFFS_BASE__WARN_UNUSED_RESULT\n#endif\n\n// wuffs_base__empty_struct is used when a Wuffs function returns an empty\n// struct. In C, if a function f returns void, you can't say \"x = f()\", but in\n// Wuffs, if a function g returns empty, you can say \"y = g()\".\ntypedef struct {\n // private_impl is a placeholder field. It isn't explicitly use" + + "d, except that\n // without it, the sizeof a struct with no fields can differ across C/C++\n // compilers, and it is undefined behavior in C99. For example, gcc says that\n // the sizeof an empty struct is 0, and g++ says that it is 1. This leads to\n // ABI incompatibility if a Wuffs .c file is processed by one compiler and\n // its .h file with another compiler.\n //\n // Instead, we explicitly insert an otherwise unused field, so that the\n // sizeof this struct is always 1.\n uint8_t private_impl;\n} wuffs_base__empty_struct;\n\n// wuffs_base__utility is a placeholder receiver type. It enables what Java\n// calls static methods, as opposed to regular methods.\ntypedef struct {\n // private_impl is a placeholder field. It isn't explicitly used, except that\n // without it, the sizeof a struct with no fields can differ across C/C++\n // compilers, and it is undefined behavior in C99. For example, gcc says that\n // the sizeof an empty struct is 0, and g++ says that it is 1. This leads to\n // ABI incompatibility" + + " if a Wuffs .c file is processed by one compiler and\n // its .h file with another compiler.\n //\n // Instead, we explicitly insert an otherwise unused field, so that the\n // sizeof this struct is always 1.\n uint8_t private_impl;\n} wuffs_base__utility;\n\n" + "" + "// --------\n\n// A status is either NULL (meaning OK) or a string message. That message is\n// human-readable, for programmers, but it is not for end users. It is not\n// localized, and does not contain additional contextual information such as a\n// source filename.\n//\n// Status strings are statically allocated and should never be free'd. They can\n// be compared by the == operator and not just by strcmp.\n//\n// Statuses come in four categories:\n// - OK: the request was completed, successfully.\n// - Warnings: the request was completed, unsuccessfully.\n// - Suspensions: the request was not completed, but can be re-tried.\n// - Errors: the request was not completed, permanently.\n//\n// When a function returns an incomplete status, a suspension means that that\n// function should be called again within a new context, such as after flushing\n// or re-filling an I/O buffer. An error means that an irrecoverable failure\n// state was reached.\ntypedef const char* wuffs_base__status;\n\n// !! INSERT wuffs_bas" + "e__status names.\n\nstatic inline bool //\nwuffs_base__status__is_complete(wuffs_base__status z) {\n return (z == NULL) || ((*z != '$') && (*z != '?'));\n}\n\nstatic inline bool //\nwuffs_base__status__is_error(wuffs_base__status z) {\n return z && (*z == '?');\n}\n\nstatic inline bool //\nwuffs_base__status__is_ok(wuffs_base__status z) {\n return z == NULL;\n}\n\nstatic inline bool //\nwuffs_base__status__is_suspension(wuffs_base__status z) {\n return z && (*z == '$');\n}\n\nstatic inline bool //\nwuffs_base__status__is_warning(wuffs_base__status z) {\n return z && (*z != '$') && (*z != '?');\n}\n\n" + diff --git a/cmd/wuffs-c/release.go b/cmd/wuffs-c/release.go index 59f26f70..5d35bd50 100644 --- a/cmd/wuffs-c/release.go +++ b/cmd/wuffs-c/release.go @@ -32,6 +32,7 @@ import ( func doGenrelease(args []string) error { flags := flag.FlagSet{} cformatterFlag := flags.String("cformatter", cf.CformatterDefault, cf.CformatterUsage) + gitRevListCountFlag := flags.Int("gitrevlistcount", 0, `git "rev-list --count" that the release was built from`) revisionFlag := flags.String("revision", "", "git revision the release was built from") versionFlag := flags.String("version", cf.VersionDefault, cf.VersionUsage) @@ -41,6 +42,9 @@ func doGenrelease(args []string) error { if !cf.IsAlphaNumericIsh(*cformatterFlag) { return fmt.Errorf("bad -cformatter flag value %q", *cformatterFlag) } + if (*gitRevListCountFlag < 0) || (0x7FFFFFFF < *gitRevListCountFlag) { + return fmt.Errorf("bad -gitrevlistcount flag value %d", *gitRevListCountFlag) + } if !cf.IsAlphaNumericIsh(*revisionFlag) { return fmt.Errorf("bad -revision flag value %q", *revisionFlag) } @@ -64,11 +68,12 @@ func doGenrelease(args []string) error { baseDirSlash := baseDir + string(filepath.Separator) h := &genReleaseHelper{ - filesList: nil, - filesMap: map[string]parsedCFile{}, - seen: nil, - revision: *revisionFlag, - version: v, + filesList: nil, + filesMap: map[string]parsedCFile{}, + seen: nil, + gitRevListCount: *gitRevListCountFlag, + revision: *revisionFlag, + version: v, } for _, filename := range args { @@ -127,7 +132,7 @@ var ( 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"`) + grVEnd = []byte("#define WUFFS_VERSION_GIT_REV_LIST_COUNT 0") grWmrAbove = []byte("// !! WUFFS MONOLITHIC RELEASE DISCARDS EVERYTHING ABOVE.\n") grWmrBelow = []byte("// !! WUFFS MONOLITHIC RELEASE DISCARDS EVERYTHING BELOW.\n") ) @@ -149,11 +154,12 @@ type parsedCFile struct { } type genReleaseHelper struct { - filesList []string - filesMap map[string]parsedCFile - seen map[string]bool - revision string - version cf.Version + filesList []string + filesMap map[string]parsedCFile + seen map[string]bool + gitRevListCount int + revision string + version cf.Version } func (h *genReleaseHelper) parse(relFilename string, s []byte) error { @@ -267,8 +273,8 @@ func (h *genReleaseHelper) substituteWuffsVersion(s []byte) ([]byte, error) { 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) + if !bytes.HasSuffix(cut, grVEnd) { + return nil, fmt.Errorf("%q did not end with %q", cut, grVEnd) } w := bytes.NewBuffer(nil) @@ -285,9 +291,10 @@ func (h *genReleaseHelper) substituteWuffsVersion(s []byte) ([]byte, error) { #define WUFFS_VERSION_PATCH ((uint64_t)0x%04X) #define WUFFS_VERSION_EXTENSION %q #define WUFFS_VERSION_STRING %q + #define WUFFS_VERSION_GIT_REV_LIST_COUNT %d `, h.version.Uint64(), h.version.Major, h.version.Minor, h.version.Patch, - h.version.Extension, h.version) + h.version.Extension, h.version, h.gitRevListCount) ret = append(ret, w.Bytes()...) ret = append(ret, s...) diff --git a/cmd/wuffs/release.go b/cmd/wuffs/release.go index adf014be..28fddd99 100644 --- a/cmd/wuffs/release.go +++ b/cmd/wuffs/release.go @@ -27,8 +27,9 @@ import ( func genrelease(wuffsRoot string, langs []string, v cf.Version) error { revision := findRevision(wuffsRoot) + gitRevListCount := findGitRevListCount(wuffsRoot) for _, lang := range langs { - filename, contents, err := genreleaseLang(wuffsRoot, revision, v, lang) + filename, contents, err := genreleaseLang(wuffsRoot, revision, gitRevListCount, v, lang) if err != nil { return err } @@ -39,7 +40,7 @@ func genrelease(wuffsRoot string, langs []string, v cf.Version) error { return nil } -func genreleaseLang(wuffsRoot string, revision string, v cf.Version, lang string) (filename string, contents []byte, err error) { +func genreleaseLang(wuffsRoot string, revision string, gitRevListCount string, v cf.Version, lang string) (filename string, contents []byte, err error) { qualFilenames, err := findFiles(filepath.Join(wuffsRoot, "gen", lang), "."+lang) if err != nil { return "", nil, err @@ -48,6 +49,9 @@ func genreleaseLang(wuffsRoot string, revision string, v cf.Version, lang string command := "wuffs-" + lang args := []string(nil) args = append(args, "genrelease", "-revision", revision, "-version", v.String()) + if gitRevListCount != "" { + args = append(args, "-gitrevlistcount", gitRevListCount) + } args = append(args, qualFilenames...) stdout := &bytes.Buffer{} @@ -97,3 +101,22 @@ func findRevision(wuffsRoot string) string { return string(ref) } + +func findGitRevListCount(wuffsRoot string) string { + // Assume that we're using git. + + wd, err := os.Getwd() + if err != nil { + return "" + } + if err := os.Chdir(wuffsRoot); err != nil { + return "" + } + defer os.Chdir(wd) + + out, err := exec.Command("git", "rev-list", "--count", "HEAD").Output() + if err != nil { + return "" + } + return string(bytes.TrimSpace(out)) +} diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index c0448ec2..e3aa30e7 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -56,6 +56,7 @@ extern "C" { #define WUFFS_VERSION_PATCH ((uint64_t)0) #define WUFFS_VERSION_EXTENSION "" #define WUFFS_VERSION_STRING "0.0.0" +#define WUFFS_VERSION_GIT_REV_LIST_COUNT 0 // Define WUFFS_CONFIG__STATIC_FUNCTIONS to make all of Wuffs' functions have // static storage. The motivation is discussed in the "ALLOW STATIC