Skip to content

Instantly share code, notes, and snippets.

@DylanLukes
Created December 31, 2023 04:27
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 DylanLukes/066f5c443e60e5b3b924f4fdc0f23d90 to your computer and use it in GitHub Desktop.
Save DylanLukes/066f5c443e60e5b3b924f4fdc0f23d90 to your computer and use it in GitHub Desktop.
Minimal build.zig for building bgfx, and using it in a Zig project.
const std = @import("std");
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const is_debug = optimize == .Debug;
// BGFX
// ----
const bgfx = b.addStaticLibrary(.{
.name = "bgfx",
.target = target,
.optimize = optimize,
});
bgfx.defineCMacro("BX_CONFIG_DEBUG", if (is_debug) "1" else "0");
bgfx.linkSystemLibrary("c++");
bgfx.addIncludePath(.{ .path = "dep/bx/include" });
bgfx.addIncludePath(.{ .path = "dep/bx/3rdparty" });
bgfx.addIncludePath(.{ .path = "dep/bgfx/include" });
bgfx.addIncludePath(.{ .path = "dep/bimg/include" });
bgfx.addIncludePath(.{ .path = "dep/bimg/3rdparty" });
bgfx.addIncludePath(.{ .path = "dep/bimg/3rdparty/astc-encoder/include" });
bgfx.addIncludePath(.{ .path = "dep/bimg/3rdparty/tinyexr/deps/miniz" });
switch (target.getOsTag()) {
.freebsd => bgfx.addIncludePath(.{ .path = "dep/bx/include/compat/freebsd" }),
.linux => bgfx.addIncludePath(.{ .path = "dep/bx/include/compat/linux" }),
.ios => bgfx.addIncludePath(.{ .path = "dep/bx/include/compat/ios" }),
.macos => bgfx.addIncludePath(.{ .path = "dep/bx/include/compat/osx" }),
.windows => switch (target.getAbi()) {
.gnu => bgfx.addIncludePath(.{ .path = "dep/bx/include/compat/mingw" }),
.msvc => bgfx.addIncludePath(.{ .path = "dep/bx/include/compat/msvc" }),
else => {},
},
else => {},
}
bgfx.addCSourceFiles(.{
.files = &[_][]const u8{
"dep/bx/src/amalgamated.cpp",
"dep/bgfx/src/amalgamated.cpp",
"dep/bimg/src/image.cpp",
"dep/bimg/src/image_gnf.cpp",
"dep/bimg/3rdparty/astc-encoder/source/astcenc_averages_and_directions.cpp",
"dep/bimg/3rdparty/astc-encoder/source/astcenc_block_sizes.cpp",
"dep/bimg/3rdparty/astc-encoder/source/astcenc_color_quantize.cpp",
"dep/bimg/3rdparty/astc-encoder/source/astcenc_color_unquantize.cpp",
"dep/bimg/3rdparty/astc-encoder/source/astcenc_compress_symbolic.cpp",
"dep/bimg/3rdparty/astc-encoder/source/astcenc_compute_variance.cpp",
"dep/bimg/3rdparty/astc-encoder/source/astcenc_decompress_symbolic.cpp",
"dep/bimg/3rdparty/astc-encoder/source/astcenc_diagnostic_trace.cpp",
"dep/bimg/3rdparty/astc-encoder/source/astcenc_entry.cpp",
"dep/bimg/3rdparty/astc-encoder/source/astcenc_find_best_partitioning.cpp",
"dep/bimg/3rdparty/astc-encoder/source/astcenc_ideal_endpoints_and_weights.cpp",
"dep/bimg/3rdparty/astc-encoder/source/astcenc_image.cpp",
"dep/bimg/3rdparty/astc-encoder/source/astcenc_integer_sequence.cpp",
"dep/bimg/3rdparty/astc-encoder/source/astcenc_mathlib_softfloat.cpp",
"dep/bimg/3rdparty/astc-encoder/source/astcenc_mathlib.cpp",
"dep/bimg/3rdparty/astc-encoder/source/astcenc_partition_tables.cpp",
"dep/bimg/3rdparty/astc-encoder/source/astcenc_percentile_tables.cpp",
"dep/bimg/3rdparty/astc-encoder/source/astcenc_pick_best_endpoint_format.cpp",
"dep/bimg/3rdparty/astc-encoder/source/astcenc_quantization.cpp",
"dep/bimg/3rdparty/astc-encoder/source/astcenc_symbolic_physical.cpp",
"dep/bimg/3rdparty/astc-encoder/source/astcenc_weight_align.cpp",
"dep/bimg/3rdparty/astc-encoder/source/astcenc_weight_quant_xfer_tables.cpp",
"dep/bimg/3rdparty/tinyexr/deps/miniz/miniz.c",
},
});
if (bgfx.target.isDarwin()) {
bgfx.defineCMacro("BGFX_CONFIG_RENDERER_VULKAN", "0"); // until we have a vulkan driver
bgfx.linkSystemLibrary("objc");
bgfx.linkFramework("Foundation");
bgfx.addCSourceFiles(.{
.files = &[_][]const u8{
"dep/bgfx/src/renderer_mtl.mm",
},
});
}
const bgfx_mod = b.addModule("bgfx", .{
.source_file = .{ .path = "dep/bgfx/bindings/zig/bgfx.zig" },
});
// Main Target using BGFX
// ----------------------
const lib = b.addStaticLibrary(.{
.name = "mylib",
.root_source_file = .{ .path = "src/mylib/root.zig" },
.target = target,
.optimize = optimize,
});
lib.linkLibrary(bgfx); // Link symbols
lib.addModule("bgfx", bgfx_mod); // Expose @import("bgfx")
b.installArtifact(b) // Produce zig-out/libmylib.a
// Testing, etc ...
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment