Skip to content

Commit a263370

Browse files
backend over mcpplibs.os: no platform layer of its own; manifest symbols keep their text; a target with no toolchain offers no spec
Co-authored-by: speak-agent <248744407+speak-agent@users.noreply.github.com>
1 parent 6ea6178 commit a263370

18 files changed

Lines changed: 474 additions & 682 deletions

‎mcpp.lock‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@
55
# file must not change depending on which command ran last.
66
version = 2
77

8+
[package."os"]
9+
version = "os-0.1.0"
10+
source = "git+https://github.com/mcpplibs/os.git#branch=os-0.1.0@5476bb62613ea6b7e29bf268632e4bb6670835ed"
11+
hash = "fnv1a:0b8682dfa4c33f65"
12+
13+
[package."openkal"]
14+
version = "openkal-0.17.0"
15+
source = "git+https://github.com/mcpplibs/openkal.git#branch=openkal-0.17.0@18b36578808d2095fb6cf5f29f1e76463c2998b4"
16+
hash = "fnv1a:125ff2b9e8c59497"
17+
18+
[package."openkal-linux"]
19+
version = "openkal-0.17.0"
20+
source = "git+https://github.com/mcpplibs/openkal-linux.git#branch=openkal-0.17.0@ea4f39e239711166dcc46498a31726beea469e42"
21+
hash = "fnv1a:648b8e8438d4845f"
22+
823
[package."nlohmann.json"]
924
namespace = "nlohmann"
1025
version = "3.12.0"

‎mcpp.toml‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
# on its standard streams), the shared panels, and the editor adapters that host
33
# it (vscode/, jetbrains/). This file builds the backend.
44
#
5-
# The backend speaks POSIX and Win32 itself (src/platform). A Linux release is
6-
# linked statically against musl (`--target x86_64-linux-musl --static`), so
7-
# one file runs on every distribution; macOS and Windows use their system
8-
# toolchains.
5+
# The operating system is reached through mcpplibs.os (processes, the
6+
# environment, the standard streams, a request to end), which is openkal
7+
# beneath: the backend names no system header and no platform macro, and the
8+
# same source builds for a system (M1) and as an ok image (`<arch>-openkal`,
9+
# M2/M3), where the system it runs upon is answered when the image is loaded.
910
[package]
1011
name = "mcpp-ide"
1112
version = "0.1.0"
@@ -21,7 +22,7 @@ modules = true
2122
import_std = true
2223

2324
[build]
24-
sources = ["src/**/*.cppm", "src/**/*.cpp"]
25+
sources = ["src/*.cppm", "src/generated/*.cppm"]
2526
macos_deployment_target = "12.0"
2627

2728
[targets.mcpp-ide]
@@ -34,5 +35,8 @@ main = "src/main.cpp"
3435
kind = "bin"
3536
main = "src/bin/mockmcpp.cpp"
3637

38+
[dependencies]
39+
os = { git = "https://github.com/mcpplibs/os.git", branch = "os-0.1.0" }
40+
3741
[dependencies.nlohmann]
3842
json = "3.12.0"

‎src/bin/mockmcpp.cpp‎

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,113 @@
1+
// A stand-in for mcpp in the tests: answers mcpp's control contract from the
2+
// golden fixtures (protocol/fixtures, captured from a real mcpp by
3+
// tools/capture-fixtures.sh). `@ROOT@` in a fixture becomes the directory the
4+
// command runs in, `@HOME@` the home.
5+
//
6+
// MCPPIDE_FIXTURES the fixtures directory (required)
7+
// MOCK_MCPP_FAIL "build": the build fails with the compiler's diagnostic;
8+
// "manifest": it fails on the manifest
9+
// MOCK_MCPP_SLOW_MS the build waits this long before it ends, so a test can
10+
// cancel it; SIGTERM then leaves the record `cancelled`
11+
// MOCK_MCPP_LOG every argv is appended here, one line each
112
import std;
2-
int main() { std::println("mock"); }
13+
import mcpplibs.os;
14+
15+
namespace {
16+
17+
std::string env(const char* name) {
18+
const char* v = std::getenv(name);
19+
return v ? std::string(v) : std::string{};
20+
}
21+
22+
std::string slurp(const std::filesystem::path& p) {
23+
std::ifstream in(p, std::ios::binary);
24+
return std::string(std::istreambuf_iterator<char>(in), {});
25+
}
26+
27+
std::string replace_all(std::string s, std::string_view from, std::string_view to) {
28+
for (auto pos = s.find(from); pos != std::string::npos; pos = s.find(from, pos + to.size())) s.replace(pos, from.size(), to);
29+
return s;
30+
}
31+
32+
std::string fixture(std::string_view name) {
33+
const auto dir = env("MCPPIDE_FIXTURES");
34+
auto text = slurp(std::filesystem::path(dir) / name);
35+
std::error_code ec;
36+
text = replace_all(std::move(text), "@ROOT@", std::filesystem::current_path(ec).generic_string());
37+
text = replace_all(std::move(text), "@HOME@", env("HOME"));
38+
return text;
39+
}
40+
41+
void write_record(std::string_view status) {
42+
std::error_code ec;
43+
const auto root = std::filesystem::current_path(ec);
44+
const auto out = root / "target" / "mock-config";
45+
std::filesystem::create_directories(out, ec);
46+
auto record = fixture("result-ok.json");
47+
record = replace_all(record, "\"status\": \"ok\"", std::format("\"status\": \"{}\"", status));
48+
std::ofstream(out / "result.json", std::ios::binary) << record;
49+
std::ofstream(root / "target" / ".last-result.json", std::ios::binary)
50+
<< std::format(R"({{"schemaVersion":1,"kind":"mcpp.last-result","kindVersion":1,"data":{{"result":"{}","status":"{}","finishedAt":null}}}})",
51+
(out / "result.json").generic_string(), status);
52+
}
53+
54+
// As mcpp does: a build asked to end leaves its record `cancelled`.
55+
void on_term() {
56+
write_record("cancelled");
57+
std::quick_exit(143);
58+
}
59+
60+
int build(bool message) {
61+
const auto fail = env("MOCK_MCPP_FAIL");
62+
std::string stream = fixture(fail == "build" ? "build-failed.ndjson"
63+
: fail == "manifest" ? "build-manifest-error.ndjson" : "build-ok.ndjson");
64+
std::println(std::cerr, " Compiling app v0.1.0 (.)");
65+
if (const auto slow = env("MOCK_MCPP_SLOW_MS"); !slow.empty()) {
66+
write_record("running");
67+
// A request to end (SIGTERM, a console control event) is taken over and answered as mcpp
68+
// answers it: the record says `cancelled` (mcpplibs.os.interrupt).
69+
if (mcpplibs::os::interrupt::wait(std::chrono::milliseconds(std::stoi(slow)))) on_term();
70+
}
71+
if (message) std::cout << stream << std::flush;
72+
if (fail.empty()) { write_record("ok"); std::println(std::cerr, " Finished dev [unoptimized + debuginfo] in 0.01s"); return 0; }
73+
if (fail == "build") write_record("failed");
74+
return 1;
75+
}
76+
77+
} // namespace
78+
79+
int main(int argc, char** argv) {
80+
std::vector<std::string> args(argv + 1, argv + argc);
81+
if (const auto log = env("MOCK_MCPP_LOG"); !log.empty()) {
82+
std::ofstream out(log, std::ios::app);
83+
for (const auto& a : args) out << a << ' ';
84+
out << '\n';
85+
}
86+
auto has = [&](std::string_view a) { return std::ranges::find(args, a) != args.end(); };
87+
auto first = [&](std::size_t i) { return i < args.size() ? args[i] : std::string{}; };
88+
if (args.empty()) return 2;
89+
if (first(0) == "--version") { std::cout << fixture("version.txt"); return 0; }
90+
if (first(0) == "--protocol-version") { std::cout << fixture("protocol-version.json"); return 0; }
91+
if (first(0) == "build") return build(has("--message-format"));
92+
if (first(0) == "test") { std::cout << fixture("test.ndjson"); return 0; }
93+
if (first(0) == "run" && has("--print-launch")) { std::cout << fixture("launch.json"); return 0; }
94+
if (first(0) == "search") { std::cout << fixture("search.json"); return 0; }
95+
if (first(0) == "index" && first(1) == "list") { std::cout << fixture("index-list.json"); return 0; }
96+
if (first(0) == "pack" && has("--list-formats")) { std::cout << fixture("pack-formats.json"); return 0; }
97+
if (first(0) == "toolchain" && first(1) == "list") { std::cout << fixture("toolchain-list.json"); return 0; }
98+
if (first(0) == "cache" && first(1) == "list") { std::cout << fixture("cache-list.json"); return 0; }
99+
if (first(0) == "emit") {
100+
if (env("MOCK_MCPP_FAIL") == "manifest") {
101+
std::cout << replace_all(fixture("build-manifest-error.ndjson"), "\"mcpp.build\"", "\"mcpp.build-database\"");
102+
return 1;
103+
}
104+
std::cout << fixture("build-database.json");
105+
return 0;
106+
}
107+
if (first(0) == "self" && first(1) == "env") { std::cout << fixture("self-env.json"); return 0; }
108+
// Everything else is a command a person reads: say what ran.
109+
std::string line;
110+
for (const auto& a : args) { if (!line.empty()) line += ' '; line += a; }
111+
std::println(std::cerr, "mock mcpp: {}", line);
112+
return 0;
113+
}

‎src/jobs.cppm‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export module mcppide.jobs;
1616

1717
import std;
1818
import nlohmann.json;
19-
import mcppide.platform.process;
19+
import mcpplibs.os;
2020
import mcppide.mcpp;
2121

2222
export namespace mcppide::jobs {
@@ -70,7 +70,7 @@ private:
7070
std::string id;
7171
Request request;
7272
std::string state = "waiting"; // waiting | running | done
73-
std::shared_ptr<platform::Process> process;
73+
std::shared_ptr<mcpplibs::os::process::process> process;
7474
bool cancelled = false;
7575
};
7676
struct Lane {
@@ -254,14 +254,15 @@ void Jobs::execute(const std::shared_ptr<Job>& job) {
254254
for (const auto& x : job->request.args) a.push_back(x);
255255
return a;
256256
}()}});
257-
auto spawned = platform::Process::spawn(client.spawn_options(o));
257+
auto spawned = mcpplibs::os::process::process::spawn(client.spawn_options(o));
258258
if (!spawned) {
259-
if (sink_.narration) sink_.narration(job->id, spawned.error() + "\n");
259+
const auto why = spawned.error().message();
260+
if (sink_.narration) sink_.narration(job->id, why + "\n");
260261
if (sink_.finished) sink_.finished(job->id, Json{{"status", "failed"}, {"exitCode", nullptr},
261-
{"error", spawned.error()}});
262+
{"error", why}});
262263
return;
263264
}
264-
auto process = std::make_shared<platform::Process>(std::move(*spawned));
265+
auto process = std::make_shared<mcpplibs::os::process::process>(std::move(*spawned));
265266
{
266267
std::lock_guard lock(mutex_);
267268
job->process = process;
@@ -270,7 +271,7 @@ void Jobs::execute(const std::shared_ptr<Job>& job) {
270271
// Standard error: the narration, as it comes.
271272
std::jthread errors([&] {
272273
for (;;) {
273-
auto chunk = process->read_error();
274+
auto chunk = process->read_error().value_or(std::string{});
274275
if (chunk.empty()) break;
275276
if (sink_.narration) sink_.narration(job->id, chunk);
276277
}
@@ -280,7 +281,7 @@ void Jobs::execute(const std::shared_ptr<Job>& job) {
280281
Json envelope = nullptr;
281282
std::string pending;
282283
for (;;) {
283-
auto chunk = process->read_output();
284+
auto chunk = process->read_output().value_or(std::string{});
284285
if (chunk.empty()) break;
285286
if (job->request.mode == Mode::narration) {
286287
if (sink_.narration) sink_.narration(job->id, chunk);
@@ -312,7 +313,9 @@ void Jobs::execute(const std::shared_ptr<Job>& job) {
312313
}
313314
if (!pending.empty() && sink_.narration) sink_.narration(job->id, pending);
314315
errors.join();
315-
const int code = process->wait();
316+
// The status as a shell reports it (128 + N for a program its system ended).
317+
const auto ended = process->wait();
318+
const int code = ended ? ended->shell_code() : -1;
316319
const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - started);
317320
std::string status = code == 0 ? "ok" : (code == 5 ? "denied" : "failed");
318321
{

‎src/main.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import std;
77
import nlohmann.json;
88
import mcppide.rpc;
99
import mcppide.server;
10-
import mcppide.platform.stdio;
10+
import mcpplibs.os;
1111

1212
int main(int argc, char** argv) {
1313
std::vector<std::string_view> args(argv + 1, argv + argc);
@@ -43,6 +43,6 @@ int main(int argc, char** argv) {
4343
for (const auto& c : report["checks"]) bad = bad || c.value("status", "") == "error";
4444
return bad ? 1 : 0;
4545
}
46-
platform::write_stderr(std::format("mcpp-ide: unknown command '{}' (serve, self-check, --version)\n", args[0]));
46+
(void)mcpplibs::os::stdio::write_error(std::format("mcpp-ide: unknown command '{}' (serve, self-check, --version)\n", args[0]));
4747
return 2;
4848
}

‎src/manifest.cppm‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,9 @@ Json hover(const Json& p) {
311311
}
312312

313313
Json symbols(const Json& p) {
314-
const auto lines = split_lines(p.value("text", ""));
314+
// The views name the text, so the text outlives them.
315+
const std::string text = p.value("text", std::string{});
316+
const auto lines = split_lines(text);
315317
Json out = Json::array();
316318
Json* current = nullptr;
317319
for (int i = 0; i < static_cast<int>(lines.size()); ++i) {
@@ -333,7 +335,9 @@ Json symbols(const Json& p) {
333335
}
334336

335337
Json buildscript_complete(const Json& p) {
336-
const auto lines = split_lines(p.value("text", ""));
338+
// The views name the text, so the text outlives them.
339+
const std::string text = p.value("text", std::string{});
340+
const auto lines = split_lines(text);
337341
const int line = p.value("line", 0), character = p.value("character", 0);
338342
if (line < 0 || line >= static_cast<int>(lines.size())) return Json{{"items", Json::array()}};
339343
const auto cur = lines[line];

0 commit comments

Comments
 (0)