Expose version info control flags
[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: %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:")
36                 flag.PrintDefaults()
37         }
38 }
39
40 func main() {
41         var (
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.")
47
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.")
52         )
53         flag.Parse()
54
55         if *printVersion {
56                 fmt.Fprintln(os.Stdout, version.Info())
57                 os.Exit(0)
58         }
59
60         if *debugLog {
61                 logrus.SetLevel(logrus.DebugLevel)
62         }
63
64         var filesToGenerate []string
65         if *theInputFile != "" {
66                 if flag.NArg() > 0 {
67                         fmt.Fprintln(os.Stderr, "input-file cannot be combined with files to generate in arguments")
68                         os.Exit(1)
69                 }
70                 filesToGenerate = append(filesToGenerate, *theInputFile)
71         } else {
72                 filesToGenerate = append(filesToGenerate, flag.Args()...)
73         }
74
75         opts := binapigen.Options{
76                 ImportPrefix:     *importPrefix,
77                 OutputDir:        *theOutputDir,
78                 NoVersionInfo:    *noVersionInfo,
79                 NoSourcePathInfo: *noSourcePathInfo,
80         }
81         if opts.OutputDir == "binapi" {
82                 if wd, _ := os.Getwd(); filepath.Base(wd) == "binapi" {
83                         opts.OutputDir = "."
84                 }
85         }
86         apiDir := *theApiDir
87         genPlugins := strings.FieldsFunc(*generatorPlugins, func(c rune) bool {
88                 return !unicode.IsLetter(c) && !unicode.IsNumber(c)
89         })
90
91         binapigen.Run(apiDir, filesToGenerate, opts, func(gen *binapigen.Generator) error {
92                 for _, file := range gen.Files {
93                         if !file.Generate {
94                                 continue
95                         }
96                         binapigen.GenerateAPI(gen, file)
97                         for _, p := range genPlugins {
98                                 if err := binapigen.RunPlugin(p, gen, file); err != nil {
99                                         return err
100                                 }
101                         }
102                 }
103                 return nil
104         })
105 }