Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: opend-xpack-emscripten Support #42

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,52 @@ jobs:

- name: (Zig) Build Shaders
run: zig build -Dshaders
- name: (Zig) Running Test
if: runner.os != 'Windows'
run: zig build test -DzigCC
- name: (Zig) Build Native
run: zig build -Dimgui --summary all
# - name: (Zig + emsdk) Build Wasm
# run: zig build -Dimgui -DzigCC --summary all -Dtarget=wasm32-emscripten-none -Doptimize=ReleaseSmall

opend:
strategy:
fail-fast: false
matrix:
runs-on: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.runs-on }}
steps:
- uses: actions/checkout@v4
- uses: mlugg/setup-zig@v1
with:
version: master
- name: Install opend
run: |
if [ "$RUNNER_OS" == "Linux" ]; then
if [ "$RUNNER_ARCH" == "X64" ]; then
curl -sLO https://github.com/opendlang/opend/releases/download/CI/opend-latest-linux-x86_64.tar.xz
tar -xf opend-latest-linux-x86_64.tar.xz
echo "$PWD/opend-latest-linux-x86_64/bin" >> $GITHUB_PATH
$PWD/opend-latest-linux-x86_64/bin/opend install xpack-emscripten
fi
elif [ "$RUNNER_OS" == "macOS" ]; then
if [ "$RUNNER_ARCH" == "ARM64" ]; then
curl -sLO https://github.com/opendlang/opend/releases/download/CI/opend-latest-osx-arm64.tar.xz
tar -xf opend-latest-osx-arm64.tar.xz
echo "$PWD/opend-latest-osx-arm64/bin" >> $GITHUB_PATH
$PWD/opend-latest-osx-arm64/bin/opend install xpack-emscripten
fi
fi

- name: prepare-linux
if: runner.os == 'Linux'
run: |
sudo apt-get update
sudo apt-get install libglu1-mesa-dev mesa-common-dev xorg-dev libasound-dev

- name: (Zig) Build Shaders
run: zig build -Dshaders

- name: (Zig) Running Test
if: runner.os != 'Windows'
run: zig build test -DzigCC
Expand Down
74 changes: 71 additions & 3 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ pub fn build(b: *Build) !void {
.betterC = if (std.mem.eql(u8, example, "user-data")) false else opt_betterC,
.dflags = &.{
"-w",
"-preview=all",
"-preview=rvaluerefparam",
"-preview=dip1000",
},
// fixme: https://github.com/kassane/sokol-d/issues/1 - betterC works on darwin
.zig_cc = if (target.result.isDarwin() and !opt_betterC) false else opt_zigcc,
Expand Down Expand Up @@ -490,6 +491,7 @@ pub fn ldcBuildStep(b: *Build, options: DCompileStep) !*std.Build.Step.InstallDi
\\
\\ extern (C):
\\
\\ version(D_BetterC):
\\ version (Emscripten)
\\ {
\\ union fpos_t
Expand Down Expand Up @@ -690,8 +692,9 @@ pub fn ldcBuildStep(b: *Build, options: DCompileStep) !*std.Build.Step.InstallDi
.emsdk = options.emsdk orelse null,
.use_webgpu = backend == .wgpu,
.use_webgl2 = backend != .wgpu,
.use_emmalloc = true,
.use_emmalloc = options.betterC,
.use_filesystem = false,
.use_drt = !options.betterC and options.target.result.isWasm(),
.use_ubsan = options.artifact.?.root_module.sanitize_c orelse false,
.release_use_lto = options.artifact.?.want_lto orelse false,
.shell_file_path = b.path("src/sokol/web/shell.html"),
Expand Down Expand Up @@ -1003,6 +1006,7 @@ pub const EmLinkOptions = struct {
use_offset_converter: bool = false,
use_filesystem: bool = true,
use_ubsan: bool = false,
use_drt: bool = false,
shell_file_path: ?Build.LazyPath,
extra_args: []const []const u8 = &.{},
};
Expand Down Expand Up @@ -1064,6 +1068,18 @@ pub fn emLinkStep(b: *Build, options: EmLinkOptions) !*Build.Step.InstallDir {
emcc.addArg(arg);
}

if (options.use_drt) {
const libdruntime = fetch(b, .{
.url = "https://github.com/opendlang/opend/releases/download/CI/opend-latest-xpack-emscripten.tar.xz",
.file_name = "lib/libdruntime-ldc.a",
});
const libphobos2 = fetch(b, .{
.url = "https://github.com/opendlang/opend/releases/download/CI/opend-latest-xpack-emscripten.tar.xz",
.file_name = "lib/libphobos2-ldc.a",
});
emcc.addFileArg(libdruntime);
emcc.addFileArg(libphobos2);
}
// add the main lib, and then scan for library dependencies and add those too
emcc.addArtifactArg(options.lib_main);
for (options.lib_main.getCompileDependencies(false)) |item| {
Expand All @@ -1080,7 +1096,6 @@ pub fn emLinkStep(b: *Build, options: EmLinkOptions) !*Build.Step.InstallDir {
.install_subdir = "web",
});
install.step.dependOn(&emcc.step);

// get the emcc step to run on 'zig build'
b.getInstallStep().dependOn(&install.step);
return install;
Expand All @@ -1091,6 +1106,59 @@ pub fn emLinkStep(b: *Build, options: EmLinkOptions) !*Build.Step.InstallDir {
});
}

// Use 'zig fetch' to download and unpack the specified URL, optionally verifying the checksum.
fn fetch(b: *std.Build, options: struct {
url: []const u8,
file_name: []const u8,
hash: ?[]const u8 = null,
}) std.Build.LazyPath {
const copy_from_cache = b.addRunArtifact(b.addExecutable(.{
.name = "copy-from-cache",
.root_source_file = b.addWriteFiles().add("main.zig",
\\const std = @import("std");
\\const assert = std.debug.assert;
\\pub fn main() !void {
\\ var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
\\ const allocator = arena.allocator();
\\ const args = try std.process.argsAlloc(allocator);
\\ assert(args.len == 5 or args.len == 6);
\\
\\ const hash_and_newline = try std.fs.cwd().readFileAlloc(allocator, args[2], 128);
\\ assert(hash_and_newline[hash_and_newline.len - 1] == '\n');
\\ const hash = hash_and_newline[0..hash_and_newline.len - 1];
\\ if (args.len == 6 and !std.mem.eql(u8, args[5], hash)) {
\\ std.debug.panic(
\\ \\bad hash
\\ \\specified: {s}
\\ \\downloaded: {s}
\\ \\
\\ , .{args[5], hash, });
\\ }
\\ const source_path = try std.fs.path.join(allocator, &.{args[1], hash, args[3]});
\\ try std.fs.cwd().copyFile(
\\ source_path,
\\ std.fs.cwd(),
\\ args[4],
\\ .{},
\\ );
\\}
),
.target = b.graph.host,
}));
copy_from_cache.addArg(
b.graph.global_cache_root.join(b.allocator, &.{"p"}) catch @panic("OOM"),
);
copy_from_cache.addFileArg(
b.addSystemCommand(&.{ b.graph.zig_exe, "fetch", options.url }).captureStdOut(),
);
copy_from_cache.addArg(options.file_name);
const result = copy_from_cache.addOutputFileArg(options.file_name);
if (options.hash) |hash| {
copy_from_cache.addArg(hash);
}
return result;
}

// build a run step which uses the emsdk emrun command to run a build target in the browser
// NOTE: ideally this would go into a separate emsdk-zig package
pub const EmRunOptions = struct {
Expand Down
Loading
Loading