automatically create zip file for wasm build
This commit is contained in:
parent
099c3c0607
commit
fb0c1eebda
44
build.zig
44
build.zig
@ -2,12 +2,19 @@ const std = @import("std");
|
|||||||
const Build = std.Build;
|
const Build = std.Build;
|
||||||
const sokol = @import("sokol");
|
const sokol = @import("sokol");
|
||||||
|
|
||||||
|
const game_name = "sokol_template_v2";
|
||||||
|
|
||||||
pub fn build(b: *std.Build) !void {
|
pub fn build(b: *std.Build) !void {
|
||||||
const target = b.standardTargetOptions(.{});
|
const target = b.standardTargetOptions(.{});
|
||||||
const optimize = b.standardOptimizeOption(.{});
|
const optimize = b.standardOptimizeOption(.{});
|
||||||
|
|
||||||
const dep_stb = b.dependency("stb", .{});
|
const dep_stb = b.dependency("stb", .{});
|
||||||
const ctx = Context.init(b, dep_stb.module("stb_image"));
|
const dep_miniz = b.dependency("miniz", .{
|
||||||
|
.target = b.graph.host,
|
||||||
|
});
|
||||||
|
const ctx = Context.init(b, dep_stb.module("stb_image"), dep_miniz.module("miniz"));
|
||||||
|
|
||||||
|
const pkg_install_dir: Build.InstallDir = .{ .custom = "pkg" };
|
||||||
|
|
||||||
var run_cmd_step: *std.Build.Step = undefined;
|
var run_cmd_step: *std.Build.Step = undefined;
|
||||||
switch (try buildProject(ctx, target, optimize)) {
|
switch (try buildProject(ctx, target, optimize)) {
|
||||||
@ -26,13 +33,19 @@ pub fn build(b: *std.Build) !void {
|
|||||||
test_step.dependOn(&run_exe_tests.step);
|
test_step.dependOn(&run_exe_tests.step);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
.wasm => |build_step| {
|
.wasm => |install_dir| {
|
||||||
b.getInstallStep().dependOn(build_step);
|
b.getInstallStep().dependOn(&install_dir.step);
|
||||||
|
|
||||||
|
var create_zip_run = b.addRunArtifact(ctx.create_zip);
|
||||||
|
create_zip_run.addDirectoryArg(install_dir.options.source_dir);
|
||||||
|
const wasm_zip = create_zip_run.addOutputFileArg("html.zip");
|
||||||
|
var html_install_file = b.addInstallFileWithDir(wasm_zip, pkg_install_dir, b.fmt("{s}_html.zip", .{game_name}));
|
||||||
|
b.getInstallStep().dependOn(&html_install_file.step);
|
||||||
|
|
||||||
const dep_sokol = b.dependency("sokol", .{});
|
const dep_sokol = b.dependency("sokol", .{});
|
||||||
const dep_emsdk = dep_sokol.builder.dependency("emsdk", .{});
|
const dep_emsdk = dep_sokol.builder.dependency("emsdk", .{});
|
||||||
const emrun_step = sokol.emRunStep(b, .{ .name = "index", .emsdk = dep_emsdk });
|
const emrun_step = sokol.emRunStep(b, .{ .name = "index", .emsdk = dep_emsdk });
|
||||||
emrun_step.step.dependOn(build_step);
|
emrun_step.step.dependOn(&install_dir.step);
|
||||||
run_cmd_step = &emrun_step.step;
|
run_cmd_step = &emrun_step.step;
|
||||||
|
|
||||||
// add an emsdk install step
|
// add an emsdk install step
|
||||||
@ -48,14 +61,12 @@ pub fn build(b: *std.Build) !void {
|
|||||||
|
|
||||||
const ProjectResult = union(enum) {
|
const ProjectResult = union(enum) {
|
||||||
native: *std.Build.Step.Compile,
|
native: *std.Build.Step.Compile,
|
||||||
wasm: *std.Build.Step,
|
wasm: *std.Build.Step.InstallDir,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn buildProject(ctx: Context, target: Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) !ProjectResult {
|
fn buildProject(ctx: Context, target: Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) !ProjectResult {
|
||||||
const b = ctx.b;
|
const b = ctx.b;
|
||||||
|
|
||||||
const name = "sokol_template_v2";
|
|
||||||
|
|
||||||
const assets_dir = b.path("assets");
|
const assets_dir = b.path("assets");
|
||||||
const asset_files = [_][]const u8{
|
const asset_files = [_][]const u8{
|
||||||
"sokoban/sokoban_tilesheet.png",
|
"sokoban/sokoban_tilesheet.png",
|
||||||
@ -183,7 +194,7 @@ fn buildProject(ctx: Context, target: Build.ResolvedTarget, optimize: std.builti
|
|||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
const exe = b.addExecutable(.{
|
const exe = b.addExecutable(.{
|
||||||
.name = name,
|
.name = game_name,
|
||||||
.root_module = exe_mod,
|
.root_module = exe_mod,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -245,8 +256,9 @@ const Context = struct {
|
|||||||
b: *std.Build,
|
b: *std.Build,
|
||||||
png_to_icon: *Build.Step.Compile,
|
png_to_icon: *Build.Step.Compile,
|
||||||
create_win32_resource: *Build.Step.Compile,
|
create_win32_resource: *Build.Step.Compile,
|
||||||
|
create_zip: *Build.Step.Compile,
|
||||||
|
|
||||||
pub fn init(b: *std.Build, stb_image: *Build.Module) Context {
|
pub fn init(b: *std.Build, stb_image: *Build.Module, miniz: *Build.Module) Context {
|
||||||
return Context{
|
return Context{
|
||||||
.b = b,
|
.b = b,
|
||||||
.png_to_icon = b.addExecutable(.{
|
.png_to_icon = b.addExecutable(.{
|
||||||
@ -265,6 +277,16 @@ const Context = struct {
|
|||||||
.target = b.graph.host,
|
.target = b.graph.host,
|
||||||
.root_source_file = b.path("scripts/create-win32-resource.zig"),
|
.root_source_file = b.path("scripts/create-win32-resource.zig"),
|
||||||
})
|
})
|
||||||
|
}),
|
||||||
|
.create_zip = b.addExecutable(.{
|
||||||
|
.name = "create-zip",
|
||||||
|
.root_module = b.createModule(.{
|
||||||
|
.target = b.graph.host,
|
||||||
|
.root_source_file = b.path("scripts/create-zip.zig"),
|
||||||
|
.imports = &.{
|
||||||
|
.{ .name = "miniz", .module = miniz }
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -294,7 +316,7 @@ const BuildWasmOptions = struct {
|
|||||||
dep_sokol: *std.Build.Dependency,
|
dep_sokol: *std.Build.Dependency,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn buildWasm(b: *std.Build, opts: BuildWasmOptions) *std.Build.Step {
|
fn buildWasm(b: *std.Build, opts: BuildWasmOptions) *std.Build.Step.InstallDir {
|
||||||
opts.root_module.sanitize_c = .off;
|
opts.root_module.sanitize_c = .off;
|
||||||
|
|
||||||
// build the main file into a library, this is because the WASM 'exe'
|
// build the main file into a library, this is because the WASM 'exe'
|
||||||
@ -329,7 +351,7 @@ fn buildWasm(b: *std.Build, opts: BuildWasmOptions) *std.Build.Step {
|
|||||||
.extra_args = &.{ "-sALLOW_MEMORY_GROWTH", "-sENVIRONMENT=web" }
|
.extra_args = &.{ "-sALLOW_MEMORY_GROWTH", "-sENVIRONMENT=web" }
|
||||||
}) catch unreachable;
|
}) catch unreachable;
|
||||||
|
|
||||||
return &link_step.step;
|
return link_step;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn patchWasmIncludeDirs(
|
fn patchWasmIncludeDirs(
|
||||||
|
|||||||
@ -21,6 +21,9 @@
|
|||||||
.tracy = .{
|
.tracy = .{
|
||||||
.path = "./libs/tracy",
|
.path = "./libs/tracy",
|
||||||
},
|
},
|
||||||
|
.miniz = .{
|
||||||
|
.path = "./libs/miniz",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
.paths = .{
|
.paths = .{
|
||||||
"build.zig",
|
"build.zig",
|
||||||
|
|||||||
41
libs/miniz/build.zig
Normal file
41
libs/miniz/build.zig
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
pub fn build(b: *std.Build) void {
|
||||||
|
const target = b.standardTargetOptions(.{});
|
||||||
|
const optimize = b.standardOptimizeOption(.{});
|
||||||
|
|
||||||
|
const src_dep = b.dependency("src", .{});
|
||||||
|
|
||||||
|
// TODO: This is a workaround to avoid a translate-c bug.
|
||||||
|
// Remove this when fixed, likely when 0.17 is released.
|
||||||
|
const remove_pragma_step = b.addRunArtifact(b.addExecutable(.{
|
||||||
|
.name = "remove pragma",
|
||||||
|
.root_module = b.createModule(.{
|
||||||
|
.root_source_file = b.path("scripts/remove-pragma.zig"),
|
||||||
|
.target = b.graph.host
|
||||||
|
})
|
||||||
|
}));
|
||||||
|
remove_pragma_step.addFileArg(src_dep.path("miniz.h"));
|
||||||
|
var clean_miniz_h = remove_pragma_step.addOutputFileArg("miniz.h");
|
||||||
|
|
||||||
|
const mod = b.addModule("miniz", .{
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
.root_source_file = b.path("src/root.zig"),
|
||||||
|
.link_libc = true
|
||||||
|
});
|
||||||
|
mod.addIncludePath(clean_miniz_h.dirname());
|
||||||
|
mod.addCSourceFiles(.{
|
||||||
|
.root = src_dep.path("."),
|
||||||
|
.files = &.{ "miniz.c" },
|
||||||
|
});
|
||||||
|
|
||||||
|
const write_file_step = b.addWriteFiles();
|
||||||
|
const translate_c = b.addTranslateC(.{
|
||||||
|
.root_source_file = write_file_step.add("c.h", "#include \"miniz.h\""),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
translate_c.addIncludePath(clean_miniz_h.dirname());
|
||||||
|
mod.addImport("c", translate_c.createModule());
|
||||||
|
}
|
||||||
13
libs/miniz/build.zig.zon
Normal file
13
libs/miniz/build.zig.zon
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
.{
|
||||||
|
.name = .miniz,
|
||||||
|
.version = "0.0.1",
|
||||||
|
.dependencies = .{
|
||||||
|
.src = .{
|
||||||
|
.url = "https://github.com/richgel999/miniz/releases/download/3.1.2/miniz-3.1.2.zip",
|
||||||
|
.hash = "N-V-__8AACZ9BwBtpix1JS569yNZcDzDf9abe3Zimx8glq9u",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
.minimum_zig_version = "0.17.0-dev.2264+230c63650",
|
||||||
|
.paths = .{""},
|
||||||
|
.fingerprint = 0xa708528eb541dff2,
|
||||||
|
}
|
||||||
48
libs/miniz/scripts/remove-pragma.zig
Normal file
48
libs/miniz/scripts/remove-pragma.zig
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
const Io = std.Io;
|
||||||
|
|
||||||
|
pub fn main(init: std.process.Init) !void {
|
||||||
|
const io = init.io;
|
||||||
|
const arena = init.arena.allocator();
|
||||||
|
const args = try init.minimal.args.toSlice(arena);
|
||||||
|
|
||||||
|
if (args.len != 3) {
|
||||||
|
std.debug.print("Usage: ./remove-pragma <input> <output>", .{});
|
||||||
|
std.process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
const input_path = args[1];
|
||||||
|
const output_path = args[2];
|
||||||
|
|
||||||
|
const cwd = Io.Dir.cwd();
|
||||||
|
|
||||||
|
var input_file = try cwd.openFile(io, input_path, .{ .mode = .read_only });
|
||||||
|
defer input_file.close(io);
|
||||||
|
|
||||||
|
var input_buffer: [4*4096]u8 = undefined;
|
||||||
|
var input_file_reader = input_file.reader(io, &input_buffer);
|
||||||
|
const reader = &input_file_reader.interface;
|
||||||
|
|
||||||
|
var output_file = try cwd.createFile(io, output_path, .{ });
|
||||||
|
defer output_file.close(io);
|
||||||
|
|
||||||
|
var output_buffer: [4*4096]u8 = undefined;
|
||||||
|
var output_file_writer = output_file.writer(io, &output_buffer);
|
||||||
|
const writer = &output_file_writer.interface;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
const line = reader.takeDelimiterInclusive('\n') catch |err| switch (err) {
|
||||||
|
error.EndOfStream => break,
|
||||||
|
else => return err,
|
||||||
|
};
|
||||||
|
|
||||||
|
const trimmed_line = std.mem.trimStart(u8, line, &std.ascii.whitespace);
|
||||||
|
if (std.mem.startsWith(u8, trimmed_line, "#pragma ")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
try writer.writeAll(line);
|
||||||
|
}
|
||||||
|
|
||||||
|
try writer.flush();
|
||||||
|
}
|
||||||
37
libs/miniz/src/root.zig
Normal file
37
libs/miniz/src/root.zig
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
const c = @import("c");
|
||||||
|
|
||||||
|
pub const Archive = struct {
|
||||||
|
archive: c.mz_zip_archive,
|
||||||
|
|
||||||
|
pub fn initFile(self: *Archive, output: [*c]const u8) !void {
|
||||||
|
self.* = Archive{
|
||||||
|
.archive = .{}
|
||||||
|
};
|
||||||
|
if (c.mz_zip_writer_init_file(&self.archive, output, 0) == 0) {
|
||||||
|
return error.InitFileFailed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn end(self: *Archive) void {
|
||||||
|
_ = c.mz_zip_writer_end(&self.archive);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn addFile(self: *Archive, name_in_archive: [*c]const u8, path: [*c]const u8) !void {
|
||||||
|
if (c.mz_zip_writer_add_file(
|
||||||
|
&self.archive,
|
||||||
|
name_in_archive,
|
||||||
|
path,
|
||||||
|
null,
|
||||||
|
0,
|
||||||
|
@bitCast(c.MZ_DEFAULT_COMPRESSION),
|
||||||
|
) == 0) {
|
||||||
|
return error.AddFileFailed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn finalize(self: *Archive) !void {
|
||||||
|
if (c.mz_zip_writer_finalize_archive(&self.archive) == 0) {
|
||||||
|
return error.FinalizeFailed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
42
scripts/create-zip.zig
Normal file
42
scripts/create-zip.zig
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
const Io = std.Io;
|
||||||
|
|
||||||
|
const miniz = @import("miniz");
|
||||||
|
|
||||||
|
pub fn main(init: std.process.Init) !void {
|
||||||
|
const io = init.io;
|
||||||
|
const arena = init.arena.allocator();
|
||||||
|
const args = try init.minimal.args.toSlice(arena);
|
||||||
|
|
||||||
|
if (args.len != 3) {
|
||||||
|
std.debug.print("Usage: ./create-zip <directory> <output>", .{});
|
||||||
|
std.process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
const input_directory_path = args[1];
|
||||||
|
const output_zip_path = args[2];
|
||||||
|
const cwd = Io.Dir.cwd();
|
||||||
|
|
||||||
|
var archive: miniz.Archive = undefined;
|
||||||
|
try archive.initFile(output_zip_path);
|
||||||
|
defer archive.end();
|
||||||
|
|
||||||
|
var input_dir = try cwd.openDir(io, input_directory_path, .{ .iterate = true });
|
||||||
|
defer input_dir.close(io);
|
||||||
|
|
||||||
|
var input_dir_iter = input_dir.iterate();
|
||||||
|
while (try input_dir_iter.next(io)) |entry| {
|
||||||
|
if (entry.kind == .file) {
|
||||||
|
var path_buff: [std.fs.max_path_bytes+1]u8 = undefined;
|
||||||
|
const path_len = try input_dir.realPathFile(io, entry.name, path_buff[0..(path_buff.len-1)]);
|
||||||
|
path_buff[path_len] = 0;
|
||||||
|
|
||||||
|
var namez_buff: [std.fs.max_name_bytes+1]u8 = undefined;
|
||||||
|
@memcpy(namez_buff[0..entry.name.len], entry.name);
|
||||||
|
namez_buff[entry.name.len] = 0;
|
||||||
|
try archive.addFile(namez_buff[0..entry.name.len :0], path_buff[0..path_len :0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try archive.finalize();
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user