mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-23 08:47:56 +01:00
Generate .h files too, not just .c files.
This commit is contained in:
1 parent
3642dd3d0e
commit
5d5381ea17
11 files changed
+105
-51
No files matched your search
@@ -0,0 +1,51 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be found
|
||||
// in the LICENSE file.
|
||||
|
||||
//go:generate go run gen.go
|
||||
|
||||
package cgen
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
type Generator struct {
|
||||
// Extension should be either 'c' or 'h'.
|
||||
Extension byte
|
||||
}
|
||||
|
||||
func (g *Generator) WritePreamble(w *bytes.Buffer) error {
|
||||
fmt.Fprintf(w, "// Code generated by puffs-gen-%c. DO NOT EDIT.\n\n", g.Extension)
|
||||
w.WriteString(preamble)
|
||||
if g.Extension == 'h' {
|
||||
w.WriteString("\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Generator) WritePostamble(w *bytes.Buffer) error {
|
||||
if g.Extension == 'h' {
|
||||
w.WriteString("\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Generator) Format(rawSource []byte) ([]byte, error) {
|
||||
cmd := exec.Command("clang-format", "-style=Chromium")
|
||||
cmd.Stdin = bytes.NewReader(rawSource)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if _, ok := err.(*exec.ExitError); ok && len(out) > 0 {
|
||||
// Go error messages usually don't end in "\n". The caller, who sees
|
||||
// the error we return here, usually adds the "\n" themselves.
|
||||
if out[len(out)-1] == '\n' {
|
||||
out = out[:len(out)-1]
|
||||
}
|
||||
// The exec.ExitError's Error message doesn't include stderr, so we
|
||||
// return that explicitly.
|
||||
return nil, errors.New(string(out))
|
||||
}
|
||||
return out, err
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// Code generated by running "go generate". DO NOT EDIT.
|
||||
|
||||
// Use of this source code is governed by a BSD-style license that can be found
|
||||
// in the LICENSE file.
|
||||
|
||||
package cgen
|
||||
|
||||
const preamble = "" +
|
||||
"// Use of this source code is governed by a BSD-style license that can be found\n// in the LICENSE file.\n\n#include <stdint.h>\n\n#if __WORDSIZE < 32\n#error \"Puffs requires a word size of at least 32 bits\"\n#endif\n" +
|
||||
""
|
||||
@@ -0,0 +1,68 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be found
|
||||
// in the LICENSE file.
|
||||
|
||||
// +build ignore
|
||||
|
||||
// gen.go converts preamble.h to data.go.
|
||||
//
|
||||
// Invoke it via "go generate".
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"go/format"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
)
|
||||
|
||||
const columns = 1024
|
||||
|
||||
func main() {
|
||||
if err := main1(); err != nil {
|
||||
os.Stderr.WriteString(err.Error() + "\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func main1() error {
|
||||
const inFilename = "preamble.h"
|
||||
in, err := ioutil.ReadFile(inFilename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
const afterEditing = "// After editing this file,"
|
||||
if !bytes.HasPrefix(in, []byte(afterEditing)) {
|
||||
return fmt.Errorf("%s's contents do not start with %q", inFilename, afterEditing)
|
||||
}
|
||||
if i := bytes.Index(in, []byte("\n\n")); i >= 0 {
|
||||
in = in[i+2:]
|
||||
}
|
||||
|
||||
out := &bytes.Buffer{}
|
||||
out.WriteString("// Code generated by running \"go generate\". DO NOT EDIT.\n")
|
||||
out.WriteString("\n")
|
||||
out.WriteString("// Use of this source code is governed by a BSD-style license that can be found\n")
|
||||
out.WriteString("// in the LICENSE file.\n")
|
||||
out.WriteString("\n")
|
||||
out.WriteString("package cgen\n")
|
||||
out.WriteString("\n")
|
||||
out.WriteString("const preamble = \"\" +\n")
|
||||
for len(in) > 0 {
|
||||
s := in
|
||||
if len(s) > columns {
|
||||
s = s[:columns]
|
||||
}
|
||||
in = in[len(s):]
|
||||
fmt.Fprintf(out, "%q +\n", s)
|
||||
}
|
||||
out.WriteString("\"\"\n")
|
||||
|
||||
formatted, err := format.Source(out.Bytes())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ioutil.WriteFile("data.go", formatted, 0644)
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// After editing this file, run "go generate" in this directory.
|
||||
|
||||
// Use of this source code is governed by a BSD-style license that can be found
|
||||
// in the LICENSE file.
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#if __WORDSIZE < 32
|
||||
#error "Puffs requires a word size of at least 32 bits"
|
||||
#endif
|
||||
Reference in new issue
Block a user