-
Notifications
You must be signed in to change notification settings - Fork 5
/
compile.go
138 lines (119 loc) · 3.12 KB
/
compile.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
package cli
import (
"context"
"crypto/sha256"
"errors"
"os"
"path"
"path/filepath"
plugin_compiler "github.com/aperturerobotics/controllerbus/plugin/compiler"
b58 "github.com/mr-tron/base58/base58"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
// setupCompiler setups and creates the compiler.
func (c *CompilerArgs) setupCompiler(
ctx context.Context,
le *logrus.Entry,
paks []string,
) (*plugin_compiler.Analysis, *plugin_compiler.ModuleCompiler, func(), error) {
rel := func() {}
args := paks
err := c.Validate()
if err != nil {
return nil, nil, rel, err
}
packageSearchPath, err := os.Getwd()
if err == nil {
packageSearchPath, err = filepath.Abs(packageSearchPath)
}
if err != nil {
return nil, nil, rel, err
}
// deterministic prefix gen
var buildUid string
{
hs := sha256.New()
_, _ = hs.Write([]byte(packageSearchPath))
for _, p := range args {
_, _ = hs.Write([]byte(p))
}
buildUid = b58.Encode(hs.Sum(nil))
}
if c.CodegenDir == "" {
// cannot use /tmp for this, need ~/.cache dir
userCacheDir, err := os.UserCacheDir()
if err != nil {
return nil, nil, rel, err
}
c.CodegenDir = filepath.Join(userCacheDir, "cbus-codegen-"+buildUid)
le.Debugf("created tmpdir for code-gen process: %s", c.CodegenDir)
f := rel
rel = func() {
defer os.RemoveAll(c.CodegenDir)
f()
}
}
codegenDirPath, err := filepath.Abs(c.CodegenDir)
if err != nil {
return nil, nil, rel, err
}
if err := os.MkdirAll(codegenDirPath, 0o755); err != nil {
return nil, nil, rel, err
}
buildPrefix := c.BuildPrefix
if buildPrefix == "" {
buildPrefix = "cbus-plugin-" + (buildUid[:8])
}
pluginBinaryID := c.PluginBinaryID
if pluginBinaryID == "" {
pluginBinaryID = "cbus-plugin-" + buildUid[:8]
}
le.
WithField("plugin-binary-id", pluginBinaryID).
WithField("build-prefix", buildPrefix).
Infof("creating compiler for plugin with packages: %v", args)
hc, err := plugin_compiler.NewModuleCompiler(ctx, le, c.BuildPrefix, codegenDirPath, c.PluginBinaryID)
if err != nil {
return nil, nil, rel, err
}
le.Infof("analyzing %d packages for plugin", len(args))
an, err := plugin_compiler.AnalyzePackages(ctx, le, packageSearchPath, args)
if err != nil {
return nil, nil, rel, err
}
return an, hc, rel, nil
}
func (c *CompilerArgs) runCompileOnce(cctx *cli.Context) error {
ctx := context.Background()
log := logrus.New()
log.SetLevel(logrus.DebugLevel)
le := logrus.NewEntry(log)
args := cctx.Args().Slice()
if len(args) == 0 {
return errors.New("specify list of packages as arguments")
}
an, modCompiler, cleanup, err := c.setupCompiler(ctx, le, args)
if err != nil {
return err
}
if !c.NoCleanup {
defer cleanup()
defer modCompiler.Cleanup()
}
pluginBinaryVersion := c.PluginBinaryVersion
if pluginBinaryVersion == "" {
pluginBinaryVersion = "cbus-plugin-{buildHash}"
}
if err := modCompiler.GenerateModules(an, pluginBinaryVersion); err != nil {
return err
}
outputPath, err := filepath.Abs(c.OutputPath)
if err == nil {
err = os.MkdirAll(path.Dir(outputPath), 0o755)
}
if err != nil {
return err
}
return modCompiler.CompilePlugin(outputPath)
}