af6062838fd1d6ea7e8ab6bed25febff4c98378d
[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, "Provide API_FILES by file name, or with full path including extension.")
36                 fmt.Fprintln(os.Stderr, "Parse API_FILES and generate Go bindings based on the options given:")
37                 flag.PrintDefaults()
38         }
39 }
40
41 func main() {
42         var (
43                 theApiDir        = flag.String("input-dir", vppapi.DefaultDir, "Input directory containing API files.")
44                 theInputFile     = flag.String("input-file", "", "DEPRECATED: Use program arguments to define files to generate.")
45                 theOutputDir     = flag.String("output-dir", "binapi", "Output directory where code will be generated.")
46                 importPrefix     = flag.String("import-prefix", "", "Define import path prefix to be used to import types.")
47                 generatorPlugins = flag.String("gen", "rpc", "List of generator plugins to run for files.")
48
49                 printVersion     = flag.Bool("version", false, "Prints version and exits.")
50                 debugLog         = flag.Bool("debug", false, "Enable verbose logging.")
51                 noVersionInfo    = flag.Bool("no-version-info", false, "Disable version info in generated files.")
52                 noSourcePathInfo = flag.Bool("no-source-path-info", false, "Disable source path info in generated files.")
53         )
54         flag.Parse()
55
56         if *printVersion {
57                 fmt.Fprintln(os.Stdout, version.Info())
58                 os.Exit(0)
59         }
60
61         if *debugLog {
62                 logrus.SetLevel(logrus.DebugLevel)
63         }
64
65         var filesToGenerate []string
66         if *theInputFile != "" {
67                 if flag.NArg() > 0 {
68                         fmt.Fprintln(os.Stderr, "input-file cannot be combined with files to generate in arguments")
69                         os.Exit(1)
70                 }
71                 filesToGenerate = append(filesToGenerate, *theInputFile)
72         } else {
73                 filesToGenerate = append(filesToGenerate, flag.Args()...)
74         }
75
76         opts := binapigen.Options{
77                 ImportPrefix:     *importPrefix,
78                 OutputDir:        *theOutputDir,
79                 NoVersionInfo:    *noVersionInfo,
80                 NoSourcePathInfo: *noSourcePathInfo,
81         }
82         if opts.OutputDir == "binapi" {
83                 if wd, _ := os.Getwd(); filepath.Base(wd) == "binapi" {
84                         opts.OutputDir = "."
85                 }
86         }
87         apiDir := *theApiDir
88         genPlugins := strings.FieldsFunc(*generatorPlugins, func(c rune) bool {
89                 return !unicode.IsLetter(c) && !unicode.IsNumber(c)
90         })
91
92         binapigen.Run(apiDir, filesToGenerate, opts, func(gen *binapigen.Generator) error {
93                 for _, file := range gen.Files {
94                         if !file.Generate {
95                                 continue
96                         }
97                         binapigen.GenerateAPI(gen, file)
98                         for _, p := range genPlugins {
99                                 if err := binapigen.RunPlugin(p, gen, file); err != nil {
100                                         return err
101                                 }
102                         }
103                 }
104                 return nil
105         })
106 }