1 // Copyright (c) 2020 Cisco and/or its affiliates.
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:
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
26 "github.com/sirupsen/logrus"
28 "git.fd.io/govpp.git/binapigen/vppapi"
32 OutputDir string // output directory for generated files
33 ImportPrefix string // prefix for import paths
34 NoVersionInfo bool // disables generating version info
35 NoSourcePathInfo bool // disables the 'source: /path' comment
38 func Run(apiDir string, filesToGenerate []string, opts Options, f func(*Generator) error) {
39 if err := run(apiDir, filesToGenerate, opts, f); err != nil {
40 fmt.Fprintf(os.Stderr, "%s: %v\n", filepath.Base(os.Args[0]), err)
45 func run(apiDir string, filesToGenerate []string, opts Options, fn func(*Generator) error) error {
46 apiFiles, err := vppapi.ParseDir(apiDir)
51 if opts.ImportPrefix == "" {
52 opts.ImportPrefix, err = resolveImportPath(opts.OutputDir)
54 return fmt.Errorf("cannot resolve import path for output dir %s: %w", opts.OutputDir, err)
56 logrus.Debugf("resolved import path prefix: %s", opts.ImportPrefix)
59 gen, err := New(opts, apiFiles, filesToGenerate)
64 gen.vppVersion = vppapi.ResolveVPPVersion(apiDir)
65 if gen.vppVersion == "" {
66 gen.vppVersion = "unknown"
72 if err := fn(gen); err != nil {
76 if err = gen.Generate(); err != nil {
83 func GenerateDefault(gen *Generator) {
84 for _, file := range gen.Files {
88 GenerateAPI(gen, file)
89 GenerateRPC(gen, file)
93 var Logger = logrus.New()
96 if debug := os.Getenv("DEBUG_GOVPP"); strings.Contains(debug, "binapigen") {
97 Logger.SetLevel(logrus.DebugLevel)
98 logrus.SetLevel(logrus.DebugLevel)
102 func logf(f string, v ...interface{}) {
103 Logger.Debugf(f, v...)
106 // resolveImportPath tries to resolve import path for a directory.
107 func resolveImportPath(dir string) (string, error) {
108 absPath, err := filepath.Abs(dir)
112 modRoot := findGoModuleRoot(absPath)
116 modPath, err := readModulePath(path.Join(modRoot, "go.mod"))
120 relDir, err := filepath.Rel(modRoot, absPath)
124 return filepath.Join(modPath, relDir), nil
127 // findGoModuleRoot looks for enclosing Go module.
128 func findGoModuleRoot(dir string) (root string) {
129 dir = filepath.Clean(dir)
131 if fi, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil && !fi.IsDir() {
134 d := filepath.Dir(dir)
143 var modulePathRE = regexp.MustCompile(`module[ \t]+([^ \t\r\n]+)`)
145 // readModulePath reads module path from go.mod file.
146 func readModulePath(gomod string) (string, error) {
147 data, err := ioutil.ReadFile(gomod)
151 m := modulePathRE.FindSubmatch(data)
155 return string(m[1]), nil