build.zig 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. const std = @import("std");
  2. const page_size = 65536; // in bytes
  3. // initial and max memory must correspond to the memory size defined in script.js.
  4. const wasm_initial_memory = 10 * page_size;
  5. const wasm_max_memory = wasm_initial_memory;
  6. pub fn build(b: *std.build.Builder) void {
  7. // Adds the option -Drelease=[bool] to create a release build, which we set to be ReleaseSmall by default.
  8. b.setPreferredReleaseMode(.ReleaseSmall);
  9. // Standard release options allow the person running `zig build` to select
  10. // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
  11. const mode = b.standardReleaseOptions();
  12. const lib = b.addSharedLibrary("game", "src/main.zig", .unversioned);
  13. lib.setBuildMode(mode);
  14. lib.setTarget(.{
  15. .cpu_arch = .wasm32,
  16. .os_tag = .freestanding,
  17. .abi = .musl,
  18. });
  19. lib.setOutputDir(".");
  20. // https://github.com/ziglang/zig/issues/8633
  21. lib.import_memory = true; // import linear memory from the environment
  22. lib.initial_memory = wasm_initial_memory; // initial size of the linear memory (1 page = 64kB)
  23. lib.max_memory = wasm_initial_memory; // maximum size of the linear memory
  24. // lib.global_base = 6560; // offset in linear memory to place global data
  25. // Pass options from the build script to the files being compiled. This is awesome!
  26. const lib_options = b.addOptions();
  27. lib.addOptions("build_options", lib_options);
  28. lib_options.addOption(usize, "memory_size", wasm_max_memory);
  29. lib.install();
  30. const step = b.step("game", "Compiles src/main.zig");
  31. step.dependOn(&lib.step);
  32. }