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\n")
35 fmt.Fprintf(os.Stderr, " Parse API_FILES and generate Go bindings\n")
36 fmt.Fprintf(os.Stderr, " Provide API_FILES by file name, or with full path including extension.\n")
37 fmt.Fprintf(os.Stderr, " %s [OPTION] API_FILES\n\n", os.Args[0])
38 fmt.Fprintf(os.Stderr, "OPTIONS\n")
40 fmt.Fprintf(os.Stderr, "\nEXAMPLES:\n")
41 fmt.Fprintf(os.Stderr, " %s \\\n", os.Args[0])
42 fmt.Fprintf(os.Stderr, " --input-dir=$VPP/build-root/install-vpp-native/vpp/share/vpp/api/ \\\n")
43 fmt.Fprintf(os.Stderr, " --output-dir=~/output \\\n")
44 fmt.Fprintf(os.Stderr, " interface ip\n")
45 fmt.Fprintf(os.Stderr, " Assuming --input-dir contains interface.api.json & ip.api.json\n")
49 func printErrorAndExit(msg string) {
50 fmt.Fprintf(os.Stderr, "Error: %s\n\n", msg)
57 theApiDir = flag.String("input-dir", vppapi.DefaultDir, "Input directory containing API files. (e.g. )")
58 theOutputDir = flag.String("output-dir", "binapi", "Output directory where code will be generated.")
59 importPrefix = flag.String("import-prefix", "", "Prefix imports in the generated go code. \nE.g. other API Files (e.g. api_file.ba.go) will be imported with :\nimport (\n api_file \"<import-prefix>/api_file\"\n)")
60 generatorPlugins = flag.String("gen", "rpc", "List of generator plugins to run for files.")
61 theInputFile = flag.String("input-file", "", "DEPRECATED: Use program arguments to define files to generate.")
63 printVersion = flag.Bool("version", false, "Prints version and exits.")
64 debugLog = flag.Bool("debug", false, "Enable verbose logging.")
65 noVersionInfo = flag.Bool("no-version-info", false, "Disable version info in generated files.")
66 noSourcePathInfo = flag.Bool("no-source-path-info", false, "Disable source path info in generated files.")
71 fmt.Fprintln(os.Stdout, version.Info())
76 logrus.SetLevel(logrus.DebugLevel)
79 var filesToGenerate []string
80 if *theInputFile != "" {
82 printErrorAndExit("input-file cannot be combined with files to generate in arguments")
84 filesToGenerate = append(filesToGenerate, *theInputFile)
86 filesToGenerate = append(filesToGenerate, flag.Args()...)
89 opts := binapigen.Options{
90 ImportPrefix: *importPrefix,
91 OutputDir: *theOutputDir,
92 NoVersionInfo: *noVersionInfo,
93 NoSourcePathInfo: *noSourcePathInfo,
95 if opts.OutputDir == "binapi" {
96 if wd, _ := os.Getwd(); filepath.Base(wd) == "binapi" {
101 genPlugins := strings.FieldsFunc(*generatorPlugins, func(c rune) bool {
102 return !unicode.IsLetter(c) && !unicode.IsNumber(c)
105 binapigen.Run(apiDir, filesToGenerate, opts, func(gen *binapigen.Generator) error {
106 for _, file := range gen.Files {
110 binapigen.GenerateAPI(gen, file)
111 for _, p := range genPlugins {
112 if err := binapigen.RunPlugin(p, gen, file); err != nil {