Skip to content

Instantly share code, notes, and snippets.

@samhattangady
Created September 14, 2023 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samhattangady/e2b11d88bfef92e7b398086effaf8071 to your computer and use it in GitHub Desktop.
Save samhattangady/e2b11d88bfef92e7b398086effaf8071 to your computer and use it in GitHub Desktop.
build.zig for a game released on steam
const Builder = @import("std").build.Builder;
const std = @import("std");
// TODO (20 Jun 2021 sam): -Drelease-fast=true gives rise to a lld-link: error: undefined symbol: _tls_index
pub fn build(b: *Builder) void {
var load_libs = true;
var font_size: f32 = 26.0;
var web_build = false;
var testing = false;
var stbi_include = false;
var stbi_write_include = false;
var snapshot_mode = false;
var stage1 = false;
var DUMP_TEXT_DATA = false;
var noconsole = false;
var steam = true;
const mode = b.standardReleaseOptions();
// TODO (01 May 2021 sam): This feels like a clunky way of selecting. See if there's
// a better way.
const app_names = [_][]const u8{ "pirates", "sill", "solver", "kyabolta", "compile", "solution_test", "snap", "level_sort" };
const main_files = [_][]const u8{ "src/main.zig", "src/sill.zig", "src/solver.zig", "src/kyabolta.zig", "src/compile_data.zig", "src/solution_tester.zig", "src/snaps.zig", "src/level_sort.zig" };
var app_index: usize = 0;
const mond_option = b.option(bool, "sill", "Run the Sill Program");
if (mond_option) |run_app| {
if (run_app) app_index = 1;
font_size = 16.0;
stage1 = true;
}
const solver_option = b.option(bool, "solver", "Run the solver");
if (solver_option) |run_app| {
if (run_app) app_index = 2;
load_libs = false;
stage1 = true;
}
const web_build_option = b.option(bool, "web", "Build for web");
if (web_build_option) |run_app| {
if (run_app) {
app_index = 0;
web_build = true;
stage1 = false;
}
}
const bol_option = b.option(bool, "kyabolta", "Run the Kyabolta Pre-Program");
if (bol_option) |run_app| {
if (run_app) app_index = 3;
}
const comp_option = b.option(bool, "compile", "Run the compiler Pre-Program");
if (comp_option) |run_app| {
if (run_app) {
app_index = 4;
stbi_include = true;
stbi_write_include = true;
}
}
const soln_option = b.option(bool, "soln", "Run the solution tester");
if (soln_option) |run_app| {
if (run_app) {
app_index = 5;
load_libs = false;
testing = true;
}
}
const snap_option = b.option(bool, "snap", "Run the snapshot taker");
if (snap_option) |run_app| {
if (run_app) {
app_index = 6;
stbi_write_include = true;
snapshot_mode = true;
}
}
const sort_option = b.option(bool, "level_sort", "Run the level_sorter");
if (sort_option) |run_app| {
if (run_app) {
app_index = 7;
load_libs = false;
stage1 = false;
}
}
const font_bake_option = b.option(bool, "font_bake", "Bake the fonts");
if (font_bake_option) |run_app| {
if (run_app) {
app_index = 0;
DUMP_TEXT_DATA = true;
}
}
const noconsole_option = b.option(bool, "noconsole", "Build project without console");
if (noconsole_option) |option| {
noconsole = option;
}
const steam_api_option = b.option(bool, "steam", "Build project with steam api");
if (steam_api_option) |option| {
steam = option;
}
const windows_build = if (b.option(bool, "windows", "Build for windows")) |w| w else true;
var options = b.addOptions();
options.addOption(f32, "font_size", font_size);
options.addOption(bool, "web_build", web_build);
options.addOption(bool, "stbi_include", stbi_include);
options.addOption(bool, "stbi_write_include", stbi_write_include);
options.addOption(bool, "testing", testing);
options.addOption(bool, "snapshot_mode", snapshot_mode);
options.addOption(bool, "DUMP_TEXT_DATA", DUMP_TEXT_DATA);
options.addOption(bool, "use_steam_api", steam);
if (!web_build) {
const exe = b.addExecutable(app_names[app_index], main_files[app_index]);
exe.use_stage1 = stage1;
// TODO (23 Apr 2021 sam): Change the rules according to platform and things
// TODO (14 May 2021 sam): Figure out how to do subsystem=windows, so that the cmd does not
// pop up when opening the exectuable.
// TODO (14 May 2021 sam): Figure out whether we can add the icon for the executable here.
//const target = b.standardTargetOptions(.{});
const target = std.zig.CrossTarget.parse(.{ .arch_os_abi = "x86_64-windows-gnu" }) catch unreachable;
exe.setTarget(target);
exe.setBuildMode(mode);
exe.addSystemIncludePath("src");
exe.addOptions("build_options", options);
// exe.single_threaded = true;
if (load_libs) {
if (windows_build) exe.addSystemIncludePath("C:/SDL2/include");
exe.addSystemIncludePath("dependencies/gl");
exe.addSystemIncludePath("dependencies/minimp3");
if (stbi_include) exe.addSystemIncludePath("dependencies/stb_image-2.26");
if (stbi_write_include) exe.addSystemIncludePath("dependencies/stb_image_write-1.15");
exe.addSystemIncludePath("dependencies/stb_truetype-1.24");
exe.addCSourceFile("dependencies/gl/glad.c", &[_][]const u8{"-std=c99"});
exe.addCSourceFile("dependencies/minimp3/minimp3_decl.c", &[_][]const u8{"-std=c99"});
if (stbi_include) exe.addCSourceFile("dependencies/stb_image-2.26/stb_image_impl.c", &[_][]const u8{"-std=c99"});
if (stbi_write_include) exe.addCSourceFile("dependencies/stb_image_write-1.15/stb_image_write_impl.c", &[_][]const u8{"-std=c99"});
exe.addCSourceFile("dependencies/stb_truetype-1.24/stb_truetype_impl.c", &[_][]const u8{"-std=c99"});
if (windows_build) {
if (noconsole) exe.subsystem = .Windows;
exe.addLibraryPath("C:/SDL2/lib/x64");
exe.addLibraryPath("C:/Program Files (x86)/Windows Kits/10/Lib/10.0.18362.0/um/x64");
b.installBinFile("C:/SDL2/lib/x64/SDL2.dll", "SDL2.dll");
if (steam) {
exe.addObjectFile("dependencies/steamworks_sdk_155/win64/steam_api64.lib");
b.installBinFile("dependencies/steamworks_sdk_155/win64/steam_api64.dll", "steam_api64.dll");
}
exe.addObjectFile("icon.res.obj");
}
exe.linkSystemLibrary("sdl2");
if (windows_build) {
exe.linkSystemLibrary("OpenGL32");
} else {
exe.linkSystemLibrary("OpenGL");
}
}
exe.linkLibC();
exe.install();
const run_cmd = exe.run();
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
}
if (web_build) {
const target = std.zig.CrossTarget.parse(.{ .arch_os_abi = "wasm32-freestanding" }) catch unreachable;
const exe = b.addSharedLibrary(app_names[app_index], main_files[app_index], .unversioned);
exe.setTarget(target);
exe.addOptions("build_options", options);
exe.setBuildMode(mode);
exe.addSystemIncludePath("src");
exe.install();
// TODO (12 May 2022 sam): This runs before the build. Figure it out.
// b.updateFile("zig-out/lib/typeroo.wasm", "web/typeroo.wasm") catch unreachable;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment