1 // Copyright (c) 2018 Cisco and/or its affiliates.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at:
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
25 "github.com/sirupsen/logrus"
27 "git.fd.io/govpp.git/binapigen"
28 "git.fd.io/govpp.git/binapigen/vppapi"
29 "git.fd.io/govpp.git/internal/version"
34 fmt.Fprintf(os.Stderr, "Usage: %s [OPTION] API_FILES\n", os.Args[0])
35 fmt.Fprintln(os.Stderr, "Parse API_FILES and generate Go bindings based on the options given:")
42 theApiDir = flag.String("input-dir", vppapi.DefaultDir, "Input directory containing API files.")
43 theInputFile = flag.String("input-file", "", "DEPRECATED: Use program arguments to define files to generate.")
44 theOutputDir = flag.String("output-dir", "binapi", "Output directory where code will be generated.")
45 importPrefix = flag.String("import-prefix", "", "Define import path prefix to be used to import types.")
46 generatorPlugins = flag.String("gen", "rpc", "List of generator plugins to run for files.")
48 printVersion = flag.Bool("version", false, "Prints version and exits.")
49 debugLog = flag.Bool("debug", false, "Enable verbose logging.")
50 noVersionInfo = flag.Bool("no-version-info", false, "Disable version info in generated files.")
51 noSourcePathInfo = flag.Bool("no-source-path-info", false, "Disable source path info in generated files.")
56 fmt.Fprintln(os.Stdout, version.Info())
61 logrus.SetLevel(logrus.DebugLevel)
64 var filesToGenerate []string
65 if *theInputFile != "" {
67 fmt.Fprintln(os.Stderr, "input-file cannot be combined with files to generate in arguments")
70 filesToGenerate = append(filesToGenerate, *theInputFile)
72 filesToGenerate = append(filesToGenerate, flag.Args()...)
75 opts := binapigen.Options{
76 ImportPrefix: *importPrefix,
77 OutputDir: *theOutputDir,
78 NoVersionInfo: *noVersionInfo,
79 NoSourcePathInfo: *noSourcePathInfo,
81 if opts.OutputDir == "binapi" {
82 if wd, _ := os.Getwd(); filepath.Base(wd) == "binapi" {
87 genPlugins := strings.FieldsFunc(*generatorPlugins, func(c rune) bool {
88 return !unicode.IsLetter(c) && !unicode.IsNumber(c)
91 binapigen.Run(apiDir, filesToGenerate, opts, func(gen *binapigen.Generator) error {
92 for _, file := range gen.Files {
96 binapigen.GenerateAPI(gen, file)
97 for _, p := range genPlugins {
98 if err := binapigen.RunPlugin(p, gen, file); err != nil {