-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGlobalPackages.cpp
More file actions
356 lines (285 loc) · 9.22 KB
/
Copy pathGlobalPackages.cpp
File metadata and controls
356 lines (285 loc) · 9.22 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/**
*
* @file GlobalPackages.cpp
* @author Gaspard Kirira
*
* Copyright 2025, Gaspard Kirira. All rights reserved.
* https://github.com/vixcpp/vix
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*/
#include <vix/cli/cmake/GlobalPackages.hpp>
#include <vix/utils/Env.hpp>
#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <optional>
#include <sstream>
#include <string>
#include <vector>
#include <nlohmann/json.hpp>
namespace vix::cli::build
{
namespace
{
static std::optional<std::string> home_dir()
{
#ifdef _WIN32
const char *home = vix::utils::vix_getenv("USERPROFILE");
#else
const char *home = vix::utils::vix_getenv("HOME");
#endif
if (!home || std::string(home).empty())
return std::nullopt;
return std::string(home);
}
static fs::path vix_root()
{
if (const auto home = home_dir(); home)
return fs::path(*home) / ".vix";
return fs::path(".vix");
}
static fs::path global_manifest_path()
{
return vix_root() / "global" / "installed.json";
}
static fs::path artifact_cache_root()
{
return vix_root() / "cache" / "build";
}
static std::string dep_id_to_dir(std::string depId)
{
depId.erase(std::remove(depId.begin(), depId.end(), '@'), depId.end());
std::replace(depId.begin(), depId.end(), '/', '.');
return depId;
}
static std::string cmake_quote(const std::string &value)
{
std::string out;
out.reserve(value.size() + 8);
out.push_back('"');
for (char c : value)
{
if (c == '\\')
out += "\\\\";
else if (c == '"')
out += "\\\"";
else
out.push_back(c);
}
out.push_back('"');
return out;
}
static bool is_header_only_package(const GlobalPackage &pkg)
{
return pkg.type == "header-only";
}
static fs::path package_include_path(const GlobalPackage &pkg)
{
return pkg.installedPath / pkg.includeDir;
}
static fs::path package_cmake_lists_path(const GlobalPackage &pkg)
{
return pkg.installedPath / "CMakeLists.txt";
}
static std::string package_binary_dir_name(const GlobalPackage &pkg)
{
std::string name = pkg.pkgDir;
if (name.empty())
name = "pkg";
for (char &c : name)
{
if (c == '.' || c == '/' || c == '\\' || c == ':' || c == ' ')
c = '_';
}
return name;
}
static std::string package_cache_leaf_name(const GlobalPackage &pkg)
{
std::string name = pkg.pkgDir;
if (name.empty())
name = dep_id_to_dir(pkg.id);
for (char &c : name)
{
if (c == '.' || c == '/' || c == '\\' || c == ':' || c == ' ')
c = '_';
}
return name;
}
static bool looks_like_artifact_prefix(const fs::path &root)
{
return fs::exists(root / "manifest.json") &&
fs::exists(root / "include");
}
static std::vector<fs::path> find_cached_artifact_prefixes(const GlobalPackage &pkg)
{
std::vector<fs::path> prefixes;
const fs::path cacheRoot = artifact_cache_root();
if (!fs::exists(cacheRoot) || !fs::is_directory(cacheRoot))
return prefixes;
const std::string expectedLeaf = package_cache_leaf_name(pkg) + "@local";
std::error_code ec;
for (const auto &targetDir : fs::directory_iterator(cacheRoot, ec))
{
if (ec || !targetDir.is_directory())
continue;
for (const auto &compilerDir : fs::directory_iterator(targetDir.path(), ec))
{
if (ec || !compilerDir.is_directory())
continue;
for (const auto &buildTypeDir : fs::directory_iterator(compilerDir.path(), ec))
{
if (ec || !buildTypeDir.is_directory())
continue;
for (const auto &packageDir : fs::directory_iterator(buildTypeDir.path(), ec))
{
if (ec || !packageDir.is_directory())
continue;
const std::string dirName = packageDir.path().filename().string();
if (dirName != expectedLeaf &&
dirName != (package_cache_leaf_name(pkg) + "@unknown") &&
dirName != package_cache_leaf_name(pkg))
{
continue;
}
for (const auto &fingerprintDir : fs::directory_iterator(packageDir.path(), ec))
{
if (ec || !fingerprintDir.is_directory())
continue;
if (looks_like_artifact_prefix(fingerprintDir.path()))
prefixes.push_back(fingerprintDir.path());
}
}
}
}
}
std::sort(prefixes.begin(), prefixes.end());
prefixes.erase(std::unique(prefixes.begin(), prefixes.end()), prefixes.end());
return prefixes;
}
static std::string make_prefix_path_block(const GlobalPackage &pkg)
{
std::ostringstream out;
const auto prefixes = find_cached_artifact_prefixes(pkg);
if (prefixes.empty())
return "";
out << "# Cached artifact prefixes for " << pkg.id << "\n";
for (const auto &prefix : prefixes)
{
out << "if (EXISTS " << cmake_quote(prefix.string()) << ")\n";
out << " list(PREPEND CMAKE_PREFIX_PATH "
<< cmake_quote(prefix.string()) << ")\n";
out << "endif()\n";
}
out << "\n";
return out.str();
}
static std::string make_header_only_block(const GlobalPackage &pkg)
{
std::ostringstream out;
const fs::path includePath = package_include_path(pkg);
if (!pkg.includeDir.empty())
{
out << "if (EXISTS " << cmake_quote(includePath.string()) << ")\n";
out << " include_directories(" << cmake_quote(includePath.string()) << ")\n";
out << "endif()\n";
}
return out.str();
}
static std::string make_source_fallback_block(const GlobalPackage &pkg)
{
std::ostringstream out;
const fs::path cmakeLists = package_cmake_lists_path(pkg);
if (!fs::exists(cmakeLists))
return "";
const std::string binDir =
"${CMAKE_BINARY_DIR}/_vix_global/" + package_binary_dir_name(pkg);
out << "if (EXISTS " << cmake_quote(cmakeLists.string()) << ")\n";
out << " add_subdirectory(\n";
out << " " << cmake_quote(pkg.installedPath.string()) << "\n";
out << " " << cmake_quote(binDir) << "\n";
out << " EXCLUDE_FROM_ALL\n";
out << " )\n";
out << "endif()\n";
return out.str();
}
} // namespace
std::vector<GlobalPackage> load_global_packages()
{
std::vector<GlobalPackage> out;
const fs::path manifestPath = global_manifest_path();
if (!fs::exists(manifestPath))
return out;
std::ifstream ifs(manifestPath);
if (!ifs)
return out;
nlohmann::json root;
ifs >> root;
if (!root.is_object())
return out;
if (!root.contains("packages") || !root["packages"].is_array())
return out;
for (const auto &item : root["packages"])
{
if (!item.is_object())
continue;
if (!item.contains("id") || !item["id"].is_string())
continue;
if (!item.contains("installed_path") || !item["installed_path"].is_string())
continue;
GlobalPackage pkg;
pkg.id = item["id"].get<std::string>();
pkg.pkgDir = dep_id_to_dir(pkg.id);
pkg.installedPath = fs::path(item["installed_path"].get<std::string>());
if (item.contains("include") && item["include"].is_string())
pkg.includeDir = item["include"].get<std::string>();
if (item.contains("type") && item["type"].is_string())
pkg.type = item["type"].get<std::string>();
out.push_back(std::move(pkg));
}
return out;
}
std::string make_global_packages_cmake(const std::vector<GlobalPackage> &packages)
{
std::ostringstream out;
out << "# Auto-generated by Vix\n";
out << "# Global packages integration for vix build\n";
out << "# Strategy:\n";
out << "# 1. inject cached compiled artifact prefixes when available\n";
out << "# 2. keep header-only include paths\n";
out << "# 3. fallback to add_subdirectory for source packages\n\n";
if (packages.empty())
{
out << "# No global packages installed\n";
return out.str();
}
for (const auto &pkg : packages)
{
if (pkg.installedPath.empty())
continue;
out << "# Package: " << pkg.id << "\n";
const std::string prefixBlock = make_prefix_path_block(pkg);
if (!prefixBlock.empty())
out << prefixBlock;
if (is_header_only_package(pkg))
{
out << make_header_only_block(pkg);
}
else
{
const fs::path includePath = package_include_path(pkg);
if (!pkg.includeDir.empty())
{
out << "if (EXISTS " << cmake_quote(includePath.string()) << ")\n";
out << " include_directories(" << cmake_quote(includePath.string()) << ")\n";
out << "endif()\n";
}
out << make_source_fallback_block(pkg);
}
out << "\n";
}
return out.str();
}
} // namespace vix::cli::build