import std; import Crafter.Build; namespace fs = std::filesystem; using namespace Crafter; extern "C" Configuration CrafterBuildProject(std::span args) { // Dispatch on --target: wasm32-* → browser client; anything else → native server. // Plain `crafter-build` (no --target) defaults to the browser client so the // primary use case keeps working without flags. bool isBrowser = true; for (const auto& a : args) { if (a.starts_with("--target=")) { isBrowser = (a.find("wasm") != std::string_view::npos); break; } } std::vector netArgs(args.begin(), args.end()); if (isBrowser) { // Force wasm32-wasip1 when no --target was supplied. bool hasTarget = false; for (const auto& a : netArgs) { if (a.starts_with("--target=")) { hasTarget = true; break; } } if (!hasTarget) netArgs.emplace_back("--target=wasm32-wasip1"); Configuration* network = LocalProject({ .projectFile = "../../project.cpp", .args = netArgs, }); Configuration cfg; cfg.path = "./"; cfg.name = "SimpleClient"; cfg.outputName = "SimpleClient"; cfg.type = ConfigurationType::Executable; cfg.target = "wasm32-wasip1"; // Mirror CRAFTER_NETWORK_BROWSER so main.cpp sees the same API surface // as the wasm .pcm (Crafter.Build does not propagate defines from deps). cfg.defines.push_back({"CRAFTER_NETWORK_BROWSER", ""}); ApplyStandardArgs(cfg, args); cfg.dependencies = { network }; cfg.files = { "cert-hash.txt" }; std::array ifaces = {}; std::array impls = { "main" }; cfg.GetInterfacesAndImplementations(ifaces, impls); // Crafter.Network ships additional/network-env.js via cfg.files; // Crafter.Build propagates it to the output dir alongside the .wasm. // EnableWasiBrowserRuntime wires it into the generated index.html. EnableWasiBrowserRuntime(cfg); return cfg; } // Native server — echo server on port 4443. Configuration* network = LocalProject({ .projectFile = "../../project.cpp", .args = netArgs, }); Configuration cfg; cfg.path = "./"; cfg.name = "SimpleServer"; cfg.outputName = "SimpleServer"; cfg.type = ConfigurationType::Executable; ApplyStandardArgs(cfg, args); cfg.dependencies = { network }; cfg.linkFlags.push_back("-Wl,--export-dynamic"); cfg.linkFlags.push_back("-ldl"); std::array ifaces = {}; std::array impls = { "main" }; cfg.GetInterfacesAndImplementations(ifaces, impls); return cfg; }