Generate .h files too, not just .c files.

This commit is contained in:
Nigel Tao committed 2017-05-26 10:56:59 +10:00
1 parent 3642dd3d0e
commit 5d5381ea17
11 files changed
+105 -51

No files matched your search

+51
View File
@@ -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
// in the LICENSE file.
package main
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" +
@@ -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("// in the LICENSE file.\n")
out.WriteString("\n")
out.WriteString("package main\n")
out.WriteString("package cgen\n")
out.WriteString("\n")
out.WriteString("const preamble = \"\" +\n")
for len(in) > 0 {
File renamed without changes.
+2 -37
View File
@@ -1,8 +1,6 @@
// 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
// puffs-gen-c transpiles a Puffs program to a C program.
//
// The command line arguments list the source Puffs files. If no arguments are
@@ -12,48 +10,15 @@
package main
import (
"bytes"
"errors"
"os"
"os/exec"
"github.com/google/puffs/cmd/internal/cgen"
"github.com/google/puffs/lang/generate"
)
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.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
}
+24
View File
@@ -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
View File
@@ -22,7 +22,7 @@ import (
func doGen(puffsRoot string, args []string) error {
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 {
return err
}
@@ -114,11 +114,15 @@ func genDir(puffsRoot string, dirname string, filenames []string, langs []string
return err
}
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) {
fmt.Println("gen unchanged: ", outFilename)
continue
}
if err := os.MkdirAll(outDirname, 0755); err != nil {
return err
}
if err := ioutil.WriteFile(outFilename, out, 0644); err != nil {
return err
}
+1 -1
View File
@@ -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) {
ret := []string(nil)
-8
View File
@@ -8,11 +8,3 @@
#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
+18
View File
@@ -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
+1 -1
View File
@@ -23,7 +23,7 @@ var (
type Generator interface {
WritePreamble(w *bytes.Buffer) error
WritePostamble(w *bytes.Buffer) error
Format([]byte) ([]byte, error)
Format(rawSource []byte) ([]byte, error)
}
func Main(g Generator) error {