-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbmi_cache.cppm
More file actions
248 lines (217 loc) · 8.25 KB
/
bmi_cache.cppm
File metadata and controls
248 lines (217 loc) · 8.25 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// mcpp.bmi_cache — cross-project persistent BMI cache (M3.2).
//
// Layout (per docs/26-bmi-cache.md):
// $MCPP_HOME/bmi/<fingerprint>/deps/<indexName>/<pkgName>@<version>/
// {gcm,pcm}.cache/<module>.{gcm,pcm}
// obj/<file>.m.o + <file>.o
// manifest.txt (sentinel + file list)
//
// fingerprint already covers compiler / flags / stdlib / mcpp version /
// dep-lock hash etc. (docs/06), so different fingerprints get independent
// cache directories — no risk of stale-BMI cross-contamination.
//
// M4 #9: populate is wrapped in an advisory exclusive flock(2) on
// <cacheDir>/.lock so two concurrent mcpp builds can race to the same
// dep cache without trashing manifest.txt (docs/26 §5.4 V2).
module;
export module mcpp.bmi_cache;
import std;
import mcpp.platform;
export namespace mcpp::bmi_cache {
struct CacheKey {
std::filesystem::path mcppHome;
std::string fingerprint;
std::string indexName; // "mcpplibs" / "xim" / ...
std::string packageName; // "mcpplibs.cmdline"
std::string version; // "0.0.1"
std::string bmiDirName = "gcm.cache"; // "gcm.cache" | "pcm.cache"
std::string manifestTag = "gcm"; // "gcm" | "pcm"
std::filesystem::path dir() const {
return mcppHome / "bmi" / fingerprint / "deps"
/ indexName / std::format("{}@{}", packageName, version);
}
std::filesystem::path manifestFile() const { return dir() / "manifest.txt"; }
std::filesystem::path bmiDir() const { return dir() / bmiDirName; }
std::filesystem::path objDir() const { return dir() / "obj"; }
};
// File names (basename only) that belong to one dep package's cache entry.
struct DepArtifacts {
std::vector<std::string> bmiFiles; // basenames in bmiDir/
std::vector<std::string> objFiles; // basenames in obj/
};
// Cache hit if manifest.txt exists AND every listed file is present.
bool is_cached(const CacheKey& key);
// Read manifest.txt → DepArtifacts.
std::expected<DepArtifacts, std::string>
read_manifest(const CacheKey& key);
// Copy missing cached files into projectTarget/{bmiDirName,obj}. Existing
// project outputs are left untouched: BMIs may differ byte-for-byte between
// equivalent builds, and overwriting them would dirty downstream modules.
std::expected<DepArtifacts, std::string>
stage_into(const CacheKey& key,
const std::filesystem::path& projectTargetDir);
// Copy fresh build outputs from projectTarget/{bmiDirName,obj} → cache dir
// and write manifest.txt last (atomic-ish sentinel).
std::expected<void, std::string>
populate_from(const CacheKey& key,
const std::filesystem::path& projectTargetDir,
const DepArtifacts& artifacts);
} // namespace mcpp::bmi_cache
namespace mcpp::bmi_cache {
namespace {
void touch_now(const std::filesystem::path& p) {
std::error_code ec;
auto t = std::chrono::file_clock::now() + std::chrono::seconds(1);
std::filesystem::last_write_time(p, t, ec);
}
bool copy_one(const std::filesystem::path& from,
const std::filesystem::path& to,
std::error_code& ec)
{
std::filesystem::create_directories(to.parent_path(), ec);
std::filesystem::copy_file(from, to,
std::filesystem::copy_options::overwrite_existing, ec);
return !ec;
}
std::string serialize_manifest(std::string_view tag, const DepArtifacts& a) {
std::string out = "# Auto-generated by mcpp bmi_cache. Do not edit.\n";
for (auto& g : a.bmiFiles) out += std::format("{}: {}\n", tag, g);
for (auto& o : a.objFiles) out += std::format("obj: {}\n", o);
return out;
}
std::expected<DepArtifacts, std::string>
parse_manifest(const std::filesystem::path& p) {
std::ifstream is(p);
if (!is) return std::unexpected(std::format("cannot open '{}'", p.string()));
DepArtifacts a;
std::string line;
while (std::getline(is, line)) {
while (!line.empty() && (line.back() == '\r' || line.back() == ' ')) line.pop_back();
if (line.empty() || line[0] == '#') continue;
if (line.starts_with("gcm: ")) a.bmiFiles.push_back(line.substr(5));
else if (line.starts_with("pcm: ")) a.bmiFiles.push_back(line.substr(5));
else if (line.starts_with("obj: ")) a.objFiles.push_back(line.substr(5));
}
return a;
}
} // namespace
bool is_cached(const CacheKey& key) {
auto mf = key.manifestFile();
if (!std::filesystem::exists(mf)) return false;
auto arts = parse_manifest(mf);
if (!arts) return false;
// Verify every listed file actually exists on disk.
for (auto& g : arts->bmiFiles) {
if (!std::filesystem::exists(key.bmiDir() / g)) return false;
}
for (auto& o : arts->objFiles) {
if (!std::filesystem::exists(key.objDir() / o)) return false;
}
return true;
}
std::expected<DepArtifacts, std::string>
read_manifest(const CacheKey& key) {
return parse_manifest(key.manifestFile());
}
std::expected<DepArtifacts, std::string>
stage_into(const CacheKey& key,
const std::filesystem::path& projectTargetDir)
{
auto arts = parse_manifest(key.manifestFile());
if (!arts) return std::unexpected(arts.error());
auto projectBmi = projectTargetDir / key.bmiDirName;
auto projectObj = projectTargetDir / "obj";
std::error_code ec;
std::filesystem::create_directories(projectBmi, ec);
std::filesystem::create_directories(projectObj, ec);
for (auto& g : arts->bmiFiles) {
auto from = key.bmiDir() / g;
auto to = projectBmi / g;
if (std::filesystem::exists(to, ec)) {
ec.clear();
continue;
}
ec.clear();
if (!copy_one(from, to, ec)) {
return std::unexpected(std::format(
"stage bmi '{}': {}", g, ec.message()));
}
touch_now(to);
}
for (auto& o : arts->objFiles) {
auto from = key.objDir() / o;
auto to = projectObj / o;
if (std::filesystem::exists(to, ec)) {
ec.clear();
continue;
}
ec.clear();
if (!copy_one(from, to, ec)) {
return std::unexpected(std::format(
"stage obj '{}': {}", o, ec.message()));
}
touch_now(to);
}
return arts;
}
namespace {
} // namespace
std::expected<void, std::string>
populate_from(const CacheKey& key,
const std::filesystem::path& projectTargetDir,
const DepArtifacts& arts)
{
auto cacheDir = key.dir();
auto lock = mcpp::platform::fs::FileLock::try_acquire(cacheDir);
if (!lock) {
// Another writer holds the lock; treat as success (they'll do it).
return {};
}
auto cacheBmi = key.bmiDir();
auto cacheObj = key.objDir();
std::error_code ec;
std::filesystem::create_directories(cacheBmi, ec);
std::filesystem::create_directories(cacheObj, ec);
auto projectBmi = projectTargetDir / key.bmiDirName;
auto projectObj = projectTargetDir / "obj";
for (auto& g : arts.bmiFiles) {
auto from = projectBmi / g;
auto to = cacheBmi / g;
if (!std::filesystem::exists(from)) {
return std::unexpected(std::format(
"expected build output missing: {}", from.string()));
}
if (!copy_one(from, to, ec)) {
return std::unexpected(std::format(
"populate bmi '{}': {}", g, ec.message()));
}
}
for (auto& o : arts.objFiles) {
auto from = projectObj / o;
auto to = cacheObj / o;
if (!std::filesystem::exists(from)) {
return std::unexpected(std::format(
"expected build output missing: {}", from.string()));
}
if (!copy_one(from, to, ec)) {
return std::unexpected(std::format(
"populate obj '{}': {}", o, ec.message()));
}
}
// Write manifest.txt LAST as the sentinel — atomic via temp + rename.
{
auto tmp = key.manifestFile();
tmp += ".tmp";
{
std::ofstream os(tmp);
os << serialize_manifest(key.manifestTag, arts);
}
std::filesystem::rename(tmp, key.manifestFile(), ec);
if (ec) {
return std::unexpected(std::format(
"populate manifest rename: {}", ec.message()));
}
}
return {};
}
} // namespace mcpp::bmi_cache