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.
22 "github.com/sirupsen/logrus"
24 "git.fd.io/govpp.git/binapigen"
25 "git.fd.io/govpp.git/binapigen/vppapi"
26 "git.fd.io/govpp.git/version"
31 fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [OPTION]... [API]...\n", os.Args[0])
32 fmt.Fprintln(flag.CommandLine.Output(), "Generate code for each API.")
33 fmt.Fprintf(flag.CommandLine.Output(), "Example: %s -output-dir=binapi acl interface l2\n", os.Args[0])
34 fmt.Fprintln(flag.CommandLine.Output())
35 fmt.Fprintln(flag.CommandLine.Output(), "Options:")
36 flag.CommandLine.PrintDefaults()
42 theInputFile = flag.String("input-file", "", "Input VPP API file. (DEPRECATED: Use program arguments to define VPP API files)")
43 theApiDir = flag.String("input-dir", vppapi.DefaultAPIDir, "Directory with VPP API files.")
44 theOutputDir = flag.String("output-dir", ".", "Output directory where code will be generated.")
46 importPrefix = flag.String("import-prefix", "", "Define import path prefix to be used to import types.")
47 importTypes = flag.Bool("import-types", false, "Generate packages for imported types.")
48 includeAPIVer = flag.Bool("include-apiver", true, "Include APIVersion constant for each module.")
49 includeServices = flag.Bool("include-services", true, "Include RPC service api and client implementation.")
50 includeComments = flag.Bool("include-comments", false, "Include JSON API source in comments for each object.")
51 includeBinapiNames = flag.Bool("include-binapi-names", true, "Include binary API names in struct tag.")
52 includeVppVersion = flag.Bool("include-vpp-version", true, "Include version of the VPP that provided input files.")
54 debugMode = flag.Bool("debug", os.Getenv("DEBUG_GOVPP") != "", "Enable debug mode.")
55 printVersion = flag.Bool("version", false, "Prints version and exits.")
60 fmt.Fprintln(os.Stdout, version.Info())
64 if flag.NArg() == 1 && flag.Arg(0) == "version" {
65 fmt.Fprintln(os.Stdout, version.Verbose())
69 var opts binapigen.Options
71 if *theInputFile != "" {
73 fmt.Fprintln(os.Stderr, "input-file cannot be combined with files to generate in arguments")
76 opts.FilesToGenerate = append(opts.FilesToGenerate, *theInputFile)
78 opts.FilesToGenerate = append(opts.FilesToGenerate, flag.Args()...)
82 if ver := os.Getenv("VPP_API_VERSION"); ver != "" {
83 // use version from env var if set
86 opts.VPPVersion = ResolveVppVersion(*theApiDir)
88 opts.IncludeAPIVersion = *includeAPIVer
89 opts.IncludeComments = *includeComments
90 opts.IncludeBinapiNames = *includeBinapiNames
91 opts.IncludeServices = *includeServices
92 opts.IncludeVppVersion = *includeVppVersion
93 opts.ImportPrefix = *importPrefix
94 opts.ImportTypes = *importTypes
97 logrus.SetLevel(logrus.DebugLevel)
98 logrus.Debug("debug mode enabled")
102 outputDir := *theOutputDir
104 binapigen.Run(apiDir, opts, func(g *binapigen.Generator) error {
105 for _, file := range g.Files {
109 binapigen.GenerateBinapiFile(g, file, outputDir)
110 if g.IncludeServices && file.Service != nil {
111 binapigen.GenerateRPC(g, file, outputDir)