browser wasm
This commit is contained in:
parent
28fab2509b
commit
e8630528af
24 changed files with 2490 additions and 100 deletions
79
examples/SimpleClient/project.cpp
Normal file
79
examples/SimpleClient/project.cpp
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
import std;
|
||||
import Crafter.Build;
|
||||
namespace fs = std::filesystem;
|
||||
using namespace Crafter;
|
||||
|
||||
extern "C" Configuration CrafterBuildProject(std::span<const std::string_view> 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<std::string> 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<fs::path, 0> ifaces = {};
|
||||
std::array<fs::path, 1> 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<fs::path, 0> ifaces = {};
|
||||
std::array<fs::path, 1> impls = { "main" };
|
||||
cfg.GetInterfacesAndImplementations(ifaces, impls);
|
||||
|
||||
return cfg;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue