mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-26 17:01:35 +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
|
||||||
|
}
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
// Use of this source code is governed by a BSD-style license that can be found
|
// Use of this source code is governed by a BSD-style license that can be found
|
||||||
// in the LICENSE file.
|
// in the LICENSE file.
|
||||||
|
|
||||||
package main
|
package cgen
|
||||||
|
|
||||||
const preamble = "" +
|
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" +
|
"// 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" +
|
||||||
@@ -47,7 +47,7 @@ func main1() error {
|
|||||||
out.WriteString("// Use of this source code is governed by a BSD-style license that can be found\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("// in the LICENSE file.\n")
|
||||||
out.WriteString("\n")
|
out.WriteString("\n")
|
||||||
out.WriteString("package main\n")
|
out.WriteString("package cgen\n")
|
||||||
out.WriteString("\n")
|
out.WriteString("\n")
|
||||||
out.WriteString("const preamble = \"\" +\n")
|
out.WriteString("const preamble = \"\" +\n")
|
||||||
for len(in) > 0 {
|
for len(in) > 0 {
|
||||||
File renamed without changes.
+2
-37
@@ -1,8 +1,6 @@
|
|||||||
// Use of this source code is governed by a BSD-style license that can be found
|
// Use of this source code is governed by a BSD-style license that can be found
|
||||||
// in the LICENSE file.
|
// in the LICENSE file.
|
||||||
|
|
||||||
//go:generate go run gen.go
|
|
||||||
|
|
||||||
// puffs-gen-c transpiles a Puffs program to a C program.
|
// puffs-gen-c transpiles a Puffs program to a C program.
|
||||||
//
|
//
|
||||||
// The command line arguments list the source Puffs files. If no arguments are
|
// The command line arguments list the source Puffs files. If no arguments are
|
||||||
@@ -12,48 +10,15 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"errors"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
|
|
||||||
|
"github.com/google/puffs/cmd/internal/cgen"
|
||||||
"github.com/google/puffs/lang/generate"
|
"github.com/google/puffs/lang/generate"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if err := generate.Main(gen{}); err != nil {
|
if err := generate.Main(&cgen.Generator{Extension: 'c'}); err != nil {
|
||||||
os.Stderr.WriteString(err.Error() + "\n")
|
os.Stderr.WriteString(err.Error() + "\n")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type gen struct{}
|
|
||||||
|
|
||||||
func (gen) WritePreamble(w *bytes.Buffer) error {
|
|
||||||
w.WriteString("// Code generated by puffs-gen-c. DO NOT EDIT.\n\n")
|
|
||||||
w.WriteString(preamble)
|
|
||||||
w.WriteString("\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gen) WritePostamble(w *bytes.Buffer) error {
|
|
||||||
w.WriteString("\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gen) Format(in []byte) ([]byte, error) {
|
|
||||||
cmd := exec.Command("clang-format", "-style=Chromium")
|
|
||||||
cmd.Stdin = bytes.NewReader(in)
|
|
||||||
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,24 @@
|
|||||||
|
// Use of this source code is governed by a BSD-style license that can be found
|
||||||
|
// in the LICENSE file.
|
||||||
|
|
||||||
|
// puffs-gen-h transpiles a Puffs program to a C header.
|
||||||
|
//
|
||||||
|
// The command line arguments list the source Puffs files. If no arguments are
|
||||||
|
// given, it reads from stdin.
|
||||||
|
//
|
||||||
|
// The generated header is written to stdout.
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/google/puffs/cmd/internal/cgen"
|
||||||
|
"github.com/google/puffs/lang/generate"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if err := generate.Main(&cgen.Generator{Extension: 'h'}); err != nil {
|
||||||
|
os.Stderr.WriteString(err.Error() + "\n")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
+6
-2
@@ -22,7 +22,7 @@ import (
|
|||||||
|
|
||||||
func doGen(puffsRoot string, args []string) error {
|
func doGen(puffsRoot string, args []string) error {
|
||||||
flags := flag.NewFlagSet("gen", flag.ExitOnError)
|
flags := flag.NewFlagSet("gen", flag.ExitOnError)
|
||||||
langsStr := flags.String("langs", "c", langsUsage)
|
langsStr := flags.String("langs", "c,h", langsUsage)
|
||||||
if err := flags.Parse(args); err != nil {
|
if err := flags.Parse(args); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -114,11 +114,15 @@ func genDir(puffsRoot string, dirname string, filenames []string, langs []string
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
out := stdout.Bytes()
|
out := stdout.Bytes()
|
||||||
outFilename := filepath.Join(puffsRoot, "gen", lang, filepath.Base(dirname)+"."+lang)
|
outDirname := filepath.Join(puffsRoot, "gen", lang)
|
||||||
|
outFilename := filepath.Join(outDirname, filepath.Base(dirname)+"."+lang)
|
||||||
if existing, err := ioutil.ReadFile(outFilename); err == nil && bytes.Equal(existing, out) {
|
if existing, err := ioutil.ReadFile(outFilename); err == nil && bytes.Equal(existing, out) {
|
||||||
fmt.Println("gen unchanged: ", outFilename)
|
fmt.Println("gen unchanged: ", outFilename)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if err := os.MkdirAll(outDirname, 0755); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if err := ioutil.WriteFile(outFilename, out, 0644); err != nil {
|
if err := ioutil.WriteFile(outFilename, out, 0644); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -101,7 +101,7 @@ The commands are:
|
|||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const langsUsage = `comma-separated list of target languages, e.g. "c,go,rs"`
|
const langsUsage = `comma-separated list of target languages (file extensions), e.g. "c,h,go,rs"`
|
||||||
|
|
||||||
func parseLangs(commaSeparated string) ([]string, error) {
|
func parseLangs(commaSeparated string) ([]string, error) {
|
||||||
ret := []string(nil)
|
ret := []string(nil)
|
||||||
|
|||||||
@@ -8,11 +8,3 @@
|
|||||||
#if __WORDSIZE < 32
|
#if __WORDSIZE < 32
|
||||||
#error "Puffs requires a word size of at least 32 bits"
|
#error "Puffs requires a word size of at least 32 bits"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
} // extern "C"
|
|
||||||
#endif
|
|
||||||
+18
@@ -0,0 +1,18 @@
|
|||||||
|
// Code generated by puffs-gen-h. DO NOT EDIT.
|
||||||
|
|
||||||
|
// 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
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
} // extern "C"
|
||||||
|
#endif
|
||||||
@@ -23,7 +23,7 @@ var (
|
|||||||
type Generator interface {
|
type Generator interface {
|
||||||
WritePreamble(w *bytes.Buffer) error
|
WritePreamble(w *bytes.Buffer) error
|
||||||
WritePostamble(w *bytes.Buffer) error
|
WritePostamble(w *bytes.Buffer) error
|
||||||
Format([]byte) ([]byte, error)
|
Format(rawSource []byte) ([]byte, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Main(g Generator) error {
|
func Main(g Generator) error {
|
||||||
|
|||||||
Reference in new issue
Block a user