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