Repository navigation
Expand file tree
/
Copy pathrust_binary.go
More file actions
165 lines (145 loc) · 5.04 KB
/
Copy pathrust_binary.go
File metadata and controls
165 lines (145 loc) · 5.04 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package rustbin
import (
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
opservice "github.com/ethereum-optimism/optimism/op-service"
"github.com/ethereum-optimism/optimism/op-service/log"
)
// Spec describes a Rust binary to be built and located.
type Spec struct {
SrcDir string // directory name relative to monorepo root, e.g. "rust/kona"
Package string // cargo package name, e.g. "kona-node"
Binary string // binary name, e.g. "kona-node"
}
// EnsureExists locates or builds a Rust binary as needed.
//
// Env var overrides (suffix derived from binary name, e.g. "kona-node" -> "KONA_NODE"):
// - RUST_BINARY_PATH_<BINARY>: absolute path to pre-built binary (skips build, must exist)
// - RUST_SRC_DIR_<BINARY>: overrides SrcDir (absolute path to cargo project root)
//
// Build behavior:
// - RUST_JIT_BUILD=1: runs cargo build in debug mode (letting cargo handle rebuild detection)
// - Otherwise: only checks binary exists, errors if missing
func (s Spec) EnsureExists(ctx context.Context, logger log.Logger) (string, error) {
envSuffix := toEnvVarSuffix(s.Binary)
// Check for explicit binary path override
if pathOverride := os.Getenv("RUST_BINARY_PATH_" + envSuffix); pathOverride != "" {
if _, err := os.Stat(pathOverride); os.IsNotExist(err) {
return "", fmt.Errorf("%s binary not found at overridden path %s", s.Binary, pathOverride)
}
logger.Info("Using overridden binary path", "binary", s.Binary, "path", pathOverride)
return pathOverride, nil
}
// Determine source root
srcRoot, err := resolveSrcRoot(s.SrcDir, envSuffix)
if err != nil {
return "", err
}
jitBuild := os.Getenv("RUST_JIT_BUILD") != ""
if jitBuild {
logger.Info("Building Rust binary (JIT)", "binary", s.Binary, "dir", srcRoot)
if err := buildRustBinary(ctx, srcRoot, s.Package, s.Binary); err != nil {
return "", err
}
}
binaryPath, err := resolveBuiltRustBinaryPath(srcRoot, s.Binary)
if err != nil {
return "", fmt.Errorf("%s binary not found; run 'cd %s && just build-%s-debug' (or just build-%s for release) or set RUST_JIT_BUILD=1: %w",
s.Binary, s.SrcDir, s.Binary, s.Binary, err)
}
return binaryPath, nil
}
// resolveSrcRoot determines the cargo project root, checking for env var override first.
func resolveSrcRoot(defaultSrcDir, envSuffix string) (string, error) {
if srcOverride := os.Getenv("RUST_SRC_DIR_" + envSuffix); srcOverride != "" {
return srcOverride, nil
}
rootDir, err := os.Getwd()
if err != nil {
return "", err
}
monorepoRoot, err := opservice.FindMonorepoRoot(rootDir)
if err != nil {
return "", err
}
return filepath.Join(monorepoRoot, defaultSrcDir), nil
}
// toEnvVarSuffix converts a binary name to an env var suffix.
// e.g. "kona-node" -> "KONA_NODE"
func toEnvVarSuffix(binary string) string {
return strings.ToUpper(strings.ReplaceAll(binary, "-", "_"))
}
func buildRustBinary(ctx context.Context, root, pkg, bin string) error {
cmd := exec.CommandContext(ctx, "cargo", "build", "-p", pkg, "--bin", bin)
cmd.Dir = root
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
type cargoMetadata struct {
TargetDirectory string `json:"target_directory"`
}
func resolveBuiltRustBinaryPath(srcRoot, binary string) (string, error) {
targetDir, err := cargoTargetDirectory(srcRoot)
if err != nil {
return "", err
}
return selectNewestBinary(targetDir, binary)
}
// selectNewestBinary locates the built binary under targetDir, preferring the
// most recently modified when several profiles or target triples are present.
// Picking by mtime avoids silently shadowing a freshly built release binary with
// a stale debug leftover (alphabetical order would prefer "debug" over "release").
func selectNewestBinary(targetDir, binary string) (string, error) {
candidates := []string{
filepath.Join(targetDir, "release", binary),
filepath.Join(targetDir, "debug", binary),
}
globMatches, err := filepath.Glob(filepath.Join(targetDir, "*", "release", binary))
if err == nil {
candidates = append(candidates, globMatches...)
}
seen := make(map[string]struct{}, len(candidates))
var newest string
var newestMod time.Time
for _, candidate := range candidates {
if _, dup := seen[candidate]; dup {
continue
}
seen[candidate] = struct{}{}
info, err := os.Stat(candidate)
if err != nil {
continue
}
if newest == "" || info.ModTime().After(newestMod) {
newest = candidate
newestMod = info.ModTime()
}
}
if newest == "" {
return "", fmt.Errorf("no built binary found under target dir %s", targetDir)
}
return newest, nil
}
func cargoTargetDirectory(srcRoot string) (string, error) {
cmd := exec.Command("cargo", "metadata", "--no-deps", "--format-version", "1")
cmd.Dir = srcRoot
out, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("cargo metadata: %w", err)
}
var meta cargoMetadata
if err := json.Unmarshal(out, &meta); err != nil {
return "", fmt.Errorf("parse cargo metadata: %w", err)
}
if meta.TargetDirectory == "" {
return "", fmt.Errorf("cargo metadata returned empty target directory")
}
return meta.TargetDirectory, nil
}