package main
import (
- "bufio"
- "encoding/json"
"flag"
"fmt"
- "io/ioutil"
"os"
- "os/exec"
"path/filepath"
"strings"
+ "unicode"
- "github.com/bennyscetbun/jsongo"
"github.com/sirupsen/logrus"
-)
-var (
- inputFile = flag.String("input-file", "", "Input file with VPP API in JSON format.")
- inputDir = flag.String("input-dir", ".", "Input directory with VPP API files in JSON format.")
- outputDir = flag.String("output-dir", ".", "Output directory where package folders will be generated.")
- includeAPIVer = flag.Bool("include-apiver", false, "Include APIVersion constant for each module.")
- includeComments = flag.Bool("include-comments", false, "Include JSON API source in comments for each object.")
- includeBinapiNames = flag.Bool("include-binapi-names", false, "Include binary API names in struct tag.")
- continueOnError = flag.Bool("continue-onerror", false, "Continue with next file on error.")
- debug = flag.Bool("debug", debugMode, "Enable debug mode.")
+ "git.fd.io/govpp.git/binapigen"
+ "git.fd.io/govpp.git/binapigen/vppapi"
+ "git.fd.io/govpp.git/internal/version"
)
-var debugMode = os.Getenv("DEBUG_BINAPI_GENERATOR") != ""
-
-var log = logrus.Logger{
- Level: logrus.InfoLevel,
- Formatter: &logrus.TextFormatter{},
- Out: os.Stdout,
+func init() {
+ flag.Usage = func() {
+ fmt.Fprintf(os.Stderr, "Usage: %s [OPTION] API_FILES\n", os.Args[0])
+ fmt.Fprintln(os.Stderr, "Provide API_FILES by file name, or with full path including extension.")
+ fmt.Fprintln(os.Stderr, "Parse API_FILES and generate Go bindings based on the options given:")
+ flag.PrintDefaults()
+ }
}
func main() {
+ var (
+ theApiDir = flag.String("input-dir", vppapi.DefaultDir, "Input directory containing API files.")
+ theInputFile = flag.String("input-file", "", "DEPRECATED: Use program arguments to define files to generate.")
+ theOutputDir = flag.String("output-dir", "binapi", "Output directory where code will be generated.")
+ importPrefix = flag.String("import-prefix", "", "Define import path prefix to be used to import types.")
+ generatorPlugins = flag.String("gen", "rpc", "List of generator plugins to run for files.")
+
+ printVersion = flag.Bool("version", false, "Prints version and exits.")
+ debugLog = flag.Bool("debug", false, "Enable verbose logging.")
+ noVersionInfo = flag.Bool("no-version-info", false, "Disable version info in generated files.")
+ noSourcePathInfo = flag.Bool("no-source-path-info", false, "Disable source path info in generated files.")
+ )
flag.Parse()
- if *debug {
- logrus.SetLevel(logrus.DebugLevel)
+
+ if *printVersion {
+ fmt.Fprintln(os.Stdout, version.Info())
+ os.Exit(0)
}
- if *inputFile == "" && *inputDir == "" {
- fmt.Fprintln(os.Stderr, "ERROR: input-file or input-dir must be specified")
- os.Exit(1)
+ if *debugLog {
+ logrus.SetLevel(logrus.DebugLevel)
}
- if *inputFile != "" {
- // process one input file
- if err := generateFromFile(*inputFile, *outputDir); err != nil {
- fmt.Fprintf(os.Stderr, "ERROR: code generation from %s failed: %v\n", *inputFile, err)
+ var filesToGenerate []string
+ if *theInputFile != "" {
+ if flag.NArg() > 0 {
+ fmt.Fprintln(os.Stderr, "input-file cannot be combined with files to generate in arguments")
os.Exit(1)
}
+ filesToGenerate = append(filesToGenerate, *theInputFile)
} else {
- // process all files in specified directory
- files, err := getInputFiles(*inputDir)
- if err != nil {
- fmt.Fprintf(os.Stderr, "ERROR: code generation failed: %v\n", err)
- os.Exit(1)
- }
- for _, file := range files {
- if err := generateFromFile(file, *outputDir); err != nil {
- fmt.Fprintf(os.Stderr, "ERROR: code generation from %s failed: %v\n", file, err)
- if *continueOnError {
- continue
- }
- os.Exit(1)
- }
- }
+ filesToGenerate = append(filesToGenerate, flag.Args()...)
}
-}
-// getInputFiles returns all input files located in specified directory
-func getInputFiles(inputDir string) (res []string, err error) {
- files, err := ioutil.ReadDir(inputDir)
- if err != nil {
- return nil, fmt.Errorf("reading directory %s failed: %v", inputDir, err)
+ opts := binapigen.Options{
+ ImportPrefix: *importPrefix,
+ OutputDir: *theOutputDir,
+ NoVersionInfo: *noVersionInfo,
+ NoSourcePathInfo: *noSourcePathInfo,
}
- for _, f := range files {
- if strings.HasSuffix(f.Name(), inputFileExt) {
- res = append(res, filepath.Join(inputDir, f.Name()))
+ if opts.OutputDir == "binapi" {
+ if wd, _ := os.Getwd(); filepath.Base(wd) == "binapi" {
+ opts.OutputDir = "."
}
}
- return res, nil
-}
-
-// generateFromFile generates Go package from one input JSON file
-func generateFromFile(inputFile, outputDir string) error {
- logf("generating from file: %q", inputFile)
- defer logf("--------------------------------------")
-
- ctx, err := getContext(inputFile, outputDir)
- if err != nil {
- return err
- }
-
- ctx.includeAPIVersionCrc = *includeAPIVer
- ctx.includeComments = *includeComments
- ctx.includeBinapiNames = *includeBinapiNames
-
- // read input file contents
- ctx.inputData, err = readFile(inputFile)
- if err != nil {
- return err
- }
- // parse JSON data into objects
- jsonRoot, err := parseJSON(ctx.inputData)
- if err != nil {
- return err
- }
- ctx.packageData, err = parsePackage(ctx, jsonRoot)
- if err != nil {
- return err
- }
-
- // create output directory
- packageDir := filepath.Dir(ctx.outputFile)
- if err := os.MkdirAll(packageDir, 0777); err != nil {
- return fmt.Errorf("creating output directory %q failed: %v", packageDir, err)
- }
- // open output file
- f, err := os.Create(ctx.outputFile)
- if err != nil {
- return fmt.Errorf("creating output file %q failed: %v", ctx.outputFile, err)
- }
- defer f.Close()
+ apiDir := *theApiDir
+ genPlugins := strings.FieldsFunc(*generatorPlugins, func(c rune) bool {
+ return !unicode.IsLetter(c) && !unicode.IsNumber(c)
+ })
- // generate Go package code
- w := bufio.NewWriter(f)
- if err := generatePackage(ctx, w); err != nil {
- return err
- }
-
- // go format the output file (fail probably means the output is not compilable)
- cmd := exec.Command("gofmt", "-w", ctx.outputFile)
- if output, err := cmd.CombinedOutput(); err != nil {
- return fmt.Errorf("gofmt failed: %v\n%s", err, string(output))
- }
-
- // count number of lines in generated output file
- cmd = exec.Command("wc", "-l", ctx.outputFile)
- if output, err := cmd.CombinedOutput(); err != nil {
- log.Warnf("wc command failed: %v\n%s", err, string(output))
- } else {
- logf("generated lines: %s", output)
- }
-
- return nil
-}
-
-// readFile reads content of a file into memory
-func readFile(inputFile string) ([]byte, error) {
- inputData, err := ioutil.ReadFile(inputFile)
- if err != nil {
- return nil, fmt.Errorf("reading data from file failed: %v", err)
- }
-
- return inputData, nil
-}
-
-// parseJSON parses a JSON data into an in-memory tree
-func parseJSON(inputData []byte) (*jsongo.JSONNode, error) {
- root := jsongo.JSONNode{}
-
- if err := json.Unmarshal(inputData, &root); err != nil {
- return nil, fmt.Errorf("unmarshalling JSON failed: %v", err)
- }
-
- return &root, nil
-}
-
-func logf(f string, v ...interface{}) {
- if *debug {
- logrus.Debugf(f, v...)
- }
+ binapigen.Run(apiDir, filesToGenerate, opts, func(gen *binapigen.Generator) error {
+ for _, file := range gen.Files {
+ if !file.Generate {
+ continue
+ }
+ binapigen.GenerateAPI(gen, file)
+ for _, p := range genPlugins {
+ if err := binapigen.RunPlugin(p, gen, file); err != nil {
+ return err
+ }
+ }
+ }
+ return nil
+ })
}