Fix codec fallback and generate type imports
[govpp.git] / cmd / binapi-generator / main.go
1 // Copyright (c) 2018 Cisco and/or its affiliates.
2 //
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:
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 package main
16
17 import (
18         "flag"
19         "fmt"
20         "os"
21
22         "github.com/sirupsen/logrus"
23
24         "git.fd.io/govpp.git/binapigen"
25         "git.fd.io/govpp.git/binapigen/vppapi"
26         "git.fd.io/govpp.git/version"
27 )
28
29 func init() {
30         flag.Usage = func() {
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()
37         }
38 }
39
40 func main() {
41         var (
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.")
45
46                 importPrefix       = flag.String("import-prefix", "", "Define import path prefix to be used to import types.")
47                 importTypes        = flag.Bool("import-types", true, "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.")
53
54                 debugMode    = flag.Bool("debug", os.Getenv("DEBUG_GOVPP") != "", "Enable debug mode.")
55                 printVersion = flag.Bool("version", false, "Prints version and exits.")
56         )
57         flag.Parse()
58         if *printVersion {
59                 fmt.Fprintln(os.Stdout, version.Info())
60                 os.Exit(0)
61         }
62         if flag.NArg() == 1 && flag.Arg(0) == "version" {
63                 fmt.Fprintln(os.Stdout, version.Verbose())
64                 os.Exit(0)
65         }
66
67         // prepare options
68         var opts binapigen.Options
69         if *theInputFile != "" {
70                 if flag.NArg() > 0 {
71                         fmt.Fprintln(os.Stderr, "input-file cannot be combined with files to generate in arguments")
72                         os.Exit(1)
73                 }
74                 opts.FilesToGenerate = append(opts.FilesToGenerate, *theInputFile)
75         } else {
76                 opts.FilesToGenerate = append(opts.FilesToGenerate, flag.Args()...)
77         }
78         if ver := os.Getenv("VPP_API_VERSION"); ver != "" {
79                 // use version from env var if set
80                 opts.VPPVersion = ver
81         } else {
82                 opts.VPPVersion = ResolveVppVersion(*theApiDir)
83         }
84         opts.IncludeAPIVersion = *includeAPIVer
85         opts.IncludeComments = *includeComments
86         opts.IncludeBinapiNames = *includeBinapiNames
87         opts.IncludeServices = *includeServices
88         opts.IncludeVppVersion = *includeVppVersion
89         opts.ImportPrefix = *importPrefix
90         opts.ImportTypes = *importTypes
91
92         if *debugMode {
93                 logrus.SetLevel(logrus.DebugLevel)
94                 logrus.Debug("debug mode enabled")
95         }
96
97         apiDir := *theApiDir
98         outputDir := *theOutputDir
99
100         binapigen.Run(apiDir, opts, func(g *binapigen.Generator) error {
101                 for _, file := range g.Files {
102                         if !file.Generate {
103                                 continue
104                         }
105                         binapigen.GenerateBinapi(g, file, outputDir)
106                         if g.IncludeServices && file.Service != nil {
107                                 binapigen.GenerateRPC(g, file, outputDir)
108                         }
109                 }
110                 return nil
111         })
112 }