This commit is contained in:
agra
2026-03-02 17:18:47 +02:00
parent ba9c4d69ce
commit 2f4f898d54
20 changed files with 418 additions and 49 deletions

View File

@@ -303,6 +303,53 @@ pub fn loadCObjectsForJIT(
};
}
/// Compile C sources using emcc for Emscripten/WASM targets.
/// Shells out to `emcc -c` for each source file, returns temp object file paths.
pub fn compileCWithEmcc(
allocator: std.mem.Allocator,
io: std.Io,
infos: []const CImportInfo,
) ![]const []const u8 {
var paths = std.ArrayList([]const u8).empty;
var obj_idx: usize = 0;
for (infos) |info| {
if (info.sources.len == 0) continue;
for (info.sources) |src| {
const out_path = try std.fmt.allocPrint(allocator, "/tmp/sx_emcc_{d}.o", .{obj_idx});
obj_idx += 1;
var argv = std.ArrayList([]const u8).empty;
try argv.appendSlice(allocator, &.{ "emcc", "-c", "-O2", src, "-o", out_path });
// Add include paths
for (info.includes) |inc| {
try argv.append(allocator, try std.fmt.allocPrint(allocator, "-I{s}", .{dirName(inc)}));
}
for (info.defines) |def| {
try argv.append(allocator, try std.fmt.allocPrint(allocator, "-D{s}", .{def}));
}
for (info.flags) |flag| {
try argv.append(allocator, flag);
}
const argv_slice = try argv.toOwnedSlice(allocator);
var child = std.process.spawn(io, .{ .argv = argv_slice }) catch {
std.debug.print("error: failed to spawn emcc for '{s}'\n", .{src});
return error.CompileError;
};
const result = child.wait(io) catch return error.CompileError;
if (result != .exited or result.exited != 0) {
std.debug.print("error: emcc failed for '{s}'\n", .{src});
return error.CompileError;
}
try paths.append(allocator, out_path);
}
}
return try paths.toOwnedSlice(allocator);
}
/// For build mode: write .o buffers to temp files, return paths for the linker.
pub fn writeCObjectFiles(
allocator: std.mem.Allocator,