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.
34 "github.com/sirupsen/logrus"
36 "git.fd.io/govpp.git/binapigen/vppapi"
39 type Generator struct {
41 FilesByName map[string]*File
44 apifiles []*vppapi.File
50 enumsByName map[string]*Enum
51 aliasesByName map[string]*Alias
52 structsByName map[string]*Struct
53 unionsByName map[string]*Union
54 messagesByName map[string]*Message
57 func New(opts Options, apifiles []*vppapi.File, filesToGen []string) (*Generator, error) {
59 FilesByName: make(map[string]*File),
62 filesToGen: filesToGen,
63 enumsByName: map[string]*Enum{},
64 aliasesByName: map[string]*Alias{},
65 structsByName: map[string]*Struct{},
66 unionsByName: map[string]*Union{},
67 messagesByName: map[string]*Message{},
70 // Normalize API files
71 SortFilesByImports(gen.apifiles)
72 for _, apifile := range apifiles {
73 RemoveImportedTypes(gen.apifiles, apifile)
74 SortFileObjectsByName(apifile)
77 // prepare package names and import paths
78 packageNames := make(map[string]GoPackageName)
79 importPaths := make(map[string]GoImportPath)
80 for _, apifile := range gen.apifiles {
81 filename := getFilename(apifile)
82 packageNames[filename] = cleanPackageName(apifile.Name)
83 importPaths[filename] = GoImportPath(path.Join(gen.opts.ImportPrefix, baseName(apifile.Name)))
86 logrus.Debugf("adding %d VPP API files to generator", len(gen.apifiles))
88 for _, apifile := range gen.apifiles {
89 filename := getFilename(apifile)
91 if _, ok := gen.FilesByName[apifile.Name]; ok {
92 return nil, fmt.Errorf("duplicate file: %q", apifile.Name)
95 file, err := newFile(gen, apifile, packageNames[filename], importPaths[filename])
97 return nil, fmt.Errorf("loading file %s failed: %w", apifile.Name, err)
99 gen.Files = append(gen.Files, file)
100 gen.FilesByName[apifile.Name] = file
102 logrus.Debugf("added file %q (path: %v)", apifile.Name, apifile.Path)
105 // mark files for generation
106 if len(gen.filesToGen) > 0 {
107 logrus.Debugf("Checking %d files to generate: %v", len(gen.filesToGen), gen.filesToGen)
108 for _, genfile := range gen.filesToGen {
109 file, ok := gen.FilesByName[genfile]
111 return nil, fmt.Errorf("no API file found for: %v", genfile)
114 // generate all imported files
115 for _, impFile := range file.importedFiles(gen) {
116 impFile.Generate = true
120 logrus.Debugf("Files to generate not specified, marking all %d files for generate", len(gen.Files))
121 for _, file := range gen.Files {
129 func getFilename(file *vppapi.File) string {
136 func (g *Generator) Generate() error {
137 if len(g.genfiles) == 0 {
138 return fmt.Errorf("no files to generate")
141 logrus.Infof("Generating %d files", len(g.genfiles))
143 for _, genfile := range g.genfiles {
144 content, err := genfile.Content()
148 if err := writeSourceTo(genfile.filename, content); err != nil {
149 return fmt.Errorf("writing source package %s failed: %v", genfile.filename, err)
155 type GenFile struct {
159 goImportPath GoImportPath
161 manualImports map[GoImportPath]bool
162 packageNames map[GoImportPath]GoPackageName
165 func (g *Generator) NewGenFile(filename string, importPath GoImportPath) *GenFile {
169 goImportPath: importPath,
170 manualImports: make(map[GoImportPath]bool),
171 packageNames: make(map[GoImportPath]GoPackageName),
173 g.genfiles = append(g.genfiles, f)
177 func (g *GenFile) Write(p []byte) (n int, err error) {
178 return g.buf.Write(p)
181 func (g *GenFile) Import(importPath GoImportPath) {
182 g.manualImports[importPath] = true
185 func (g *GenFile) GoIdent(ident GoIdent) string {
186 if ident.GoImportPath == g.goImportPath {
189 if packageName, ok := g.packageNames[ident.GoImportPath]; ok {
190 return string(packageName) + "." + ident.GoName
192 packageName := cleanPackageName(baseName(string(ident.GoImportPath)))
193 g.packageNames[ident.GoImportPath] = packageName
194 return string(packageName) + "." + ident.GoName
197 func (g *GenFile) P(v ...interface{}) {
198 for _, x := range v {
199 switch x := x.(type) {
201 fmt.Fprint(&g.buf, g.GoIdent(x))
203 fmt.Fprint(&g.buf, x)
209 func (g *GenFile) Content() ([]byte, error) {
210 if !strings.HasSuffix(g.filename, ".go") {
211 return g.buf.Bytes(), nil
213 return g.injectImports(g.buf.Bytes())
216 func (g *GenFile) injectImports(original []byte) ([]byte, error) {
218 fset := token.NewFileSet()
219 file, err := parser.ParseFile(fset, "", original, parser.ParseComments)
222 s := bufio.NewScanner(bytes.NewReader(original))
223 for line := 1; s.Scan(); line++ {
224 fmt.Fprintf(&src, "%5d\t%s\n", line, s.Bytes())
226 return nil, fmt.Errorf("%v: unparsable Go source: %v\n%v", g.filename, err, src.String())
232 // Prepare list of all imports
233 var importPaths []Import
234 for importPath := range g.packageNames {
235 importPaths = append(importPaths, Import{
236 Name: string(g.packageNames[GoImportPath(importPath)]),
237 Path: string(importPath),
240 for importPath := range g.manualImports {
241 if _, ok := g.packageNames[importPath]; ok {
244 importPaths = append(importPaths, Import{
246 Path: string(importPath),
249 // Sort imports by import path
250 sort.Slice(importPaths, func(i, j int) bool {
251 return importPaths[i].Path < importPaths[j].Path
253 // Inject new import block into parsed AST
254 if len(importPaths) > 0 {
255 // Find import block position
257 tokFile := fset.File(file.Package)
258 pkgLine := tokFile.Line(file.Package)
259 for _, c := range file.Comments {
260 if tokFile.Line(c.Pos()) > pkgLine {
265 // Prepare the import block
266 impDecl := &ast.GenDecl{Tok: token.IMPORT, TokPos: pos, Lparen: pos, Rparen: pos}
267 for _, importPath := range importPaths {
269 if importPath.Name == "_" || strings.Contains(importPath.Path, ".") {
270 name = &ast.Ident{Name: importPath.Name, NamePos: pos}
272 impDecl.Specs = append(impDecl.Specs, &ast.ImportSpec{
274 Path: &ast.BasicLit{Kind: token.STRING, Value: strconv.Quote(importPath.Path), ValuePos: pos},
279 file.Decls = append([]ast.Decl{impDecl}, file.Decls...)
281 // Reformat source code
283 cfg := &printer.Config{
284 Mode: printer.TabIndent | printer.UseSpaces,
287 if err = cfg.Fprint(&out, fset, file); err != nil {
288 return nil, fmt.Errorf("%v: can not reformat Go source: %v", g.filename, err)
290 return out.Bytes(), nil
293 // GoIdent is a Go identifier, consisting of a name and import path.
294 // The name is a single identifier and may not be a dot-qualified selector.
295 type GoIdent struct {
297 GoImportPath GoImportPath
300 func (id GoIdent) String() string {
301 return fmt.Sprintf("%q.%v", id.GoImportPath, id.GoName)
304 func newGoIdent(f *File, fullName string) GoIdent {
305 name := strings.TrimPrefix(fullName, string(f.PackageName)+".")
307 GoName: camelCaseName(name),
308 GoImportPath: f.GoImportPath,
312 // GoImportPath is a Go import path for a package.
313 type GoImportPath string
315 func (p GoImportPath) String() string {
316 return strconv.Quote(string(p))
319 func (p GoImportPath) Ident(s string) GoIdent {
320 return GoIdent{GoName: s, GoImportPath: p}
323 type GoPackageName string
325 func cleanPackageName(name string) GoPackageName {
326 return GoPackageName(sanitizedName(name))
329 func sanitizedName(name string) string {
340 // baseName returns the last path element of the name, with the last dotted suffix removed.
341 func baseName(name string) string {
342 // First, find the last element
343 if i := strings.LastIndex(name, "/"); i >= 0 {
346 // Now drop the suffix
347 if i := strings.LastIndex(name, "."); i >= 0 {
353 func writeSourceTo(outputFile string, b []byte) error {
354 // create output directory
355 packageDir := filepath.Dir(outputFile)
356 if err := os.MkdirAll(packageDir, 0775); err != nil {
357 return fmt.Errorf("creating output dir %s failed: %v", packageDir, err)
360 // format generated source code
361 gosrc, err := format.Source(b)
363 _ = ioutil.WriteFile(outputFile, b, 0666)
364 return fmt.Errorf("formatting source code failed: %v", err)
367 // write generated code to output file
368 if err := ioutil.WriteFile(outputFile, gosrc, 0666); err != nil {
369 return fmt.Errorf("writing to output file %s failed: %v", outputFile, err)
372 lines := bytes.Count(gosrc, []byte("\n"))
373 logf("wrote %d lines (%d bytes) to: %q", lines, len(gosrc), outputFile)