diff --git a/cmd/internal/cgen/cgen.go b/cmd/internal/cgen/cgen.go new file mode 100644 index 00000000..d61ede2f --- /dev/null +++ b/cmd/internal/cgen/cgen.go @@ -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 +} diff --git a/cmd/puffs-gen-c/data.go b/cmd/internal/cgen/data.go similarity index 96% rename from cmd/puffs-gen-c/data.go rename to cmd/internal/cgen/data.go index 7fd3133c..e354ecf4 100644 --- a/cmd/puffs-gen-c/data.go +++ b/cmd/internal/cgen/data.go @@ -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 \n\n#if __WORDSIZE < 32\n#error \"Puffs requires a word size of at least 32 bits\"\n#endif\n" + diff --git a/cmd/puffs-gen-c/gen.go b/cmd/internal/cgen/gen.go similarity index 97% rename from cmd/puffs-gen-c/gen.go rename to cmd/internal/cgen/gen.go index ef1dd754..6161b3d3 100644 --- a/cmd/puffs-gen-c/gen.go +++ b/cmd/internal/cgen/gen.go @@ -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 { diff --git a/cmd/puffs-gen-c/preamble.h b/cmd/internal/cgen/preamble.h similarity index 100% rename from cmd/puffs-gen-c/preamble.h rename to cmd/internal/cgen/preamble.h diff --git a/cmd/puffs-gen-c/main.go b/cmd/puffs-gen-c/main.go index dcdb080f..414f9527 100644 --- a/cmd/puffs-gen-c/main.go +++ b/cmd/puffs-gen-c/main.go @@ -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 -} diff --git a/cmd/puffs-gen-h/main.go b/cmd/puffs-gen-h/main.go new file mode 100644 index 00000000..f0c5c0b1 --- /dev/null +++ b/cmd/puffs-gen-h/main.go @@ -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) + } +} diff --git a/cmd/puffs/gen.go b/cmd/puffs/gen.go index b02f999e..776dea78 100644 --- a/cmd/puffs/gen.go +++ b/cmd/puffs/gen.go @@ -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 } diff --git a/cmd/puffs/main.go b/cmd/puffs/main.go index 3f0071fd..9b119b2f 100644 --- a/cmd/puffs/main.go +++ b/cmd/puffs/main.go @@ -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) diff --git a/gen/c/gif.c b/gen/c/gif.c index 7e827606..d65bba04 100644 --- a/gen/c/gif.c +++ b/gen/c/gif.c @@ -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 diff --git a/gen/h/gif.h b/gen/h/gif.h new file mode 100644 index 00000000..77b83abe --- /dev/null +++ b/gen/h/gif.h @@ -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 + +#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 diff --git a/lang/generate/generate.go b/lang/generate/generate.go index 5c1dd701..31ce124a 100644 --- a/lang/generate/generate.go +++ b/lang/generate/generate.go @@ -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 {