-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgen_vpplink.go
143 lines (119 loc) · 4.05 KB
/
gen_vpplink.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright (c) 2022 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"embed"
_ "embed"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/sirupsen/logrus"
"go.fd.io/govpp"
"go.fd.io/govpp/binapigen"
"go.fd.io/govpp/binapigen/vppapi"
"github.com/calico-vpp/vpplink/pkg/wrappergen"
)
const (
generateLogFname = "generate.log"
)
var (
//go:embed templates/*
templates embed.FS
parsedTemplates *wrappergen.Template
)
func init() {
// Trim off the "templates" prefix from the paths of our templates
templates, err := fs.Sub(templates, "templates")
if err != nil {
logrus.Fatalf("error creating subFS for 'templates'")
}
// Parse all the templates
parsedTemplates, err = wrappergen.ParseFS(templates, "*.tmpl.go")
if err != nil {
logrus.Fatalf("failed to ParseFS templates: %s", err)
}
}
func GenerateAll(gen *binapigen.Generator) []*binapigen.GenFile {
genOpts := gen.GetOpts()
logrus.Infof("[WRAPPERGEN] GenerateAll (opts: %+v)", genOpts)
// We output vpplink one directory higher than the regular bindings
basePkgName := filepath.Join(genOpts.ImportPrefix, "..")
outputDir := filepath.Join(genOpts.OutputDir, "..")
data := wrappergen.NewDataFromFiles(genOpts.ImportPrefix, filepath.Base(basePkgName), gen.Files)
// Execute all the templates
err := parsedTemplates.ExecuteAll(outputDir, data, gen)
if err != nil {
logrus.Fatalf("failed to execute template: %s", err)
}
if vppDir := os.Getenv("VPP_DIR"); vppDir != "" {
createGenerateLog(vppDir, filepath.Join(outputDir, generateLogFname))
}
return nil
}
func createGenerateLog(apiDir string, fname string) {
vppSrcDir, err := findGitRepoRootDir(apiDir)
if err != nil {
return
}
vppVersion, err := vppapi.GetVPPVersionRepo(vppSrcDir)
if err != nil {
logrus.Fatalf("Unable to get vpp version : %s", err)
}
cmd := exec.Command("bash", "-c", "git log --oneline -1 $(git log origin/master..HEAD --oneline | tail -1 | awk '{print $1}')")
cmd.Dir = vppSrcDir
cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil {
logrus.Fatalf("Unable to get vpp base commit : %s", out)
}
lastCommit := strings.TrimSpace(string(out))
cmd = exec.Command("git", "log", "origin/master..HEAD", "--pretty=%s")
cmd.Dir = vppSrcDir
cmd.Stderr = os.Stderr
out, err = cmd.Output()
if err != nil {
logrus.Fatalf("Unable to get vpp own branch commits : %s", out)
}
ownCommits := strings.TrimSpace(string(out))
value := fmt.Sprintf("VPP Version : %s\n", vppVersion)
value += fmt.Sprintf("Binapi-generator version : %s\n", govpp.Version())
value += fmt.Sprintf("VPP Base commit : %s\n", lastCommit)
value += fmt.Sprintf("------------------ Cherry picked commits --------------------\n")
value += fmt.Sprintf("%s\n", ownCommits)
value += fmt.Sprintf("-------------------------------------------------------------\n")
f, err := os.OpenFile(fname, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
logrus.Fatalf("Unable to open file %s %s", fname, err)
}
n, err := f.Write([]byte(value))
if err != nil || n < len(value) {
logrus.Fatalf("Unable to write to file %s %s", fname, err)
}
err = f.Close()
if err != nil {
logrus.Fatalf("Unable to close file %s %s", fname, err)
}
}
func findGitRepoRootDir(dir string) (string, error) {
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
cmd.Dir = dir
out, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("git command failed: %v\noutput: %s", err, out)
}
return strings.TrimSpace(string(out)), nil
}