tools/gen: clang-format generated .cc, .h and .inl files

Change-Id: I5a79cc0b5da1967632d9df02e058a8e3e5073d2d
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/97441
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
This commit is contained in:
Ben Clayton
2022-08-01 19:29:14 +00:00
committed by Dawn LUCI CQ
parent 4aa360db5d
commit 92f2cb79b5
6 changed files with 123 additions and 231 deletions

View File

@@ -23,9 +23,11 @@ import (
"io/ioutil"
"math/rand"
"os"
"os/exec"
"path/filepath"
"reflect"
"regexp"
"runtime"
"strconv"
"strings"
"text/template"
@@ -72,6 +74,12 @@ optional flags:`)
func run() error {
projectRoot := fileutils.ProjectRoot()
// Find clang-format
clangFormatPath := findClangFormat(projectRoot)
if clangFormatPath == "" {
return fmt.Errorf("cannot find clang-format in <dawn>/buildtools nor PATH")
}
// Recursively find all the template files in the <tint>/src directory
files, err := glob.Scan(projectRoot, glob.MustParseConfig(`{
"paths": [{"include": [
@@ -132,6 +140,15 @@ func run() error {
if body := sb.String(); body != "" {
_, tmplFileName := filepath.Split(tmplPath)
outFileName := strings.TrimSuffix(tmplFileName, ".tmpl")
switch filepath.Ext(outFileName) {
case ".cc", ".h", ".inl":
body, err = clangFormat(body, clangFormatPath)
if err != nil {
return err
}
}
if err := writeFile(outFileName, body); err != nil {
return err
}
@@ -486,3 +503,37 @@ func pascalCase(s string) string {
}
return b.String()
}
// Invokes the clang-format executable at 'exe' to format the file content 'in'.
// Returns the formatted file.
func clangFormat(in, exe string) (string, error) {
cmd := exec.Command(exe)
cmd.Stdin = strings.NewReader(in)
out, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("clang-format failed:\n%v\n%v", string(out), err)
}
return string(out), nil
}
// Looks for clang-format in the 'buildtools' directory, falling back to PATH
func findClangFormat(projectRoot string) string {
var path string
switch runtime.GOOS {
case "linux":
path = filepath.Join(projectRoot, "buildtools/linux64/clang-format")
case "darwin":
path = filepath.Join(projectRoot, "buildtools/mac/clang-format")
case "windows":
path = filepath.Join(projectRoot, "buildtools/win/clang-format.exe")
}
if fileutils.IsExe(path) {
return path
}
var err error
path, err = exec.LookPath("clang-format")
if err == nil {
return path
}
return ""
}