-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.go
More file actions
100 lines (81 loc) · 2.08 KB
/
state.go
File metadata and controls
100 lines (81 loc) · 2.08 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
package main
import (
"encoding/json"
"os"
"path/filepath"
"time"
)
// BuildState tracks the build state for incremental builds
type BuildState struct {
URL string `json:"url"`
ConfigHash string `json:"config_hash"`
BuildTime time.Time `json:"build_time"`
lib *Library
buildRoot string
statePath string
}
// NewBuildState creates a new build state tracker
func NewBuildState(lib *Library, buildRoot string) *BuildState {
buildDir := filepath.Join(buildRoot, "build", lib.Name)
statePath := filepath.Join(buildDir, ".build-state")
state := &BuildState{
lib: lib,
buildRoot: buildRoot,
statePath: statePath,
}
// Try to load existing state
state.Load()
return state
}
// Load loads the build state from disk
func (s *BuildState) Load() error {
data, err := os.ReadFile(s.statePath)
if err != nil {
return err // file doesn't exist or can't be read
}
return json.Unmarshal(data, s)
}
// Save saves the current build state to disk
func (s *BuildState) Save() error {
s.URL = s.lib.URL
s.ConfigHash = s.lib.ConfigHash()
s.BuildTime = time.Now()
// Ensure directory exists
if err := os.MkdirAll(filepath.Dir(s.statePath), 0755); err != nil {
return err
}
data, err := json.MarshalIndent(s, "", " ")
if err != nil {
return err
}
return os.WriteFile(s.statePath, data, 0644)
}
// CanSkip checks if we can skip building this library
func (s *BuildState) CanSkip(installDir string) bool {
// No state file = must build
if s.URL == "" {
return false
}
// URL changed = must rebuild
if s.URL != s.lib.URL {
return false
}
// Config changed = must rebuild
if s.ConfigHash != s.lib.ConfigHash() {
return false
}
// Check if outputs exist
// For header-only libraries (LinkLibs == nil), we can skip if we built before
if s.lib.LinkLibs == nil {
return true
}
// For libraries with LinkLibs, check that all expected .a files exist
libDir := filepath.Join(installDir, "lib")
for _, libName := range s.lib.LinkLibs {
libPath := filepath.Join(libDir, libName+".a")
if !fileExists(libPath) {
return false
}
}
return true
}