Improve doc & fix import ordering
[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         "path/filepath"
22         "strings"
23         "unicode"
24
25         "github.com/sirupsen/logrus"
26
27         "git.fd.io/govpp.git/binapigen"
28         "git.fd.io/govpp.git/binapigen/vppapi"
29         "git.fd.io/govpp.git/internal/version"
30 )
31
32 func init() {
33         flag.Usage = func() {
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")
39                 flag.PrintDefaults()
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")
46         }
47 }
48
49 func printErrorAndExit(msg string) {
50         fmt.Fprintf(os.Stderr, "Error: %s\n\n", msg)
51         flag.Usage()
52         os.Exit(1)
53 }
54
55 func main() {
56         var (
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.")
62
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.")
67         )
68         flag.Parse()
69
70         if *printVersion {
71                 fmt.Fprintln(os.Stdout, version.Info())
72                 os.Exit(0)
73         }
74
75         if *debugLog {
76                 logrus.SetLevel(logrus.DebugLevel)
77         }
78
79         var filesToGenerate []string
80         if *theInputFile != "" {
81                 if flag.NArg() > 0 {
82                         printErrorAndExit("input-file cannot be combined with files to generate in arguments")
83                 }
84                 filesToGenerate = append(filesToGenerate, *theInputFile)
85         } else {
86                 filesToGenerate = append(filesToGenerate, flag.Args()...)
87         }
88
89         opts := binapigen.Options{
90                 ImportPrefix:     *importPrefix,
91                 OutputDir:        *theOutputDir,
92                 NoVersionInfo:    *noVersionInfo,
93                 NoSourcePathInfo: *noSourcePathInfo,
94         }
95         if opts.OutputDir == "binapi" {
96                 if wd, _ := os.Getwd(); filepath.Base(wd) == "binapi" {
97                         opts.OutputDir = "."
98                 }
99         }
100         apiDir := *theApiDir
101         genPlugins := strings.FieldsFunc(*generatorPlugins, func(c rune) bool {
102                 return !unicode.IsLetter(c) && !unicode.IsNumber(c)
103         })
104
105         binapigen.Run(apiDir, filesToGenerate, opts, func(gen *binapigen.Generator) error {
106                 for _, file := range gen.Files {
107                         if !file.Generate {
108                                 continue
109                         }
110                         binapigen.GenerateAPI(gen, file)
111                         for _, p := range genPlugins {
112                                 if err := binapigen.RunPlugin(p, gen, file); err != nil {
113                                         return err
114                                 }
115                         }
116                 }
117                 return nil
118         })
119 }