Build platform optionally
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"os": "windows", | |
"libraries": [ | |
"c", | |
"d3d12", | |
"dxgi", | |
"d3dcompiler", | |
"dxguid" | |
], | |
"include_dirs": [], | |
"libraries_dirs": [] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Builder = @import("std").build.Builder; | |
const std = @import("std"); | |
var gpa = std.heap.GeneralPurposeAllocator(.{ .safety = true }){}; | |
var gpa_allocator = &gpa.allocator; | |
const PlatformInfo = struct { | |
os: std.Target.Os.Tag, | |
libraries: [][]const u8, | |
include_dirs: [][]const u8, | |
libraries_dirs: [][]const u8, | |
}; | |
const PlatformArray = std.ArrayList(PlatformInfo); | |
pub fn parsePlatformBuild() !PlatformArray { | |
const cwd = std.fs.cwd(); | |
const platform_dir = try cwd.openDir("src/platforms", .{ .iterate = true }); | |
var platform_array = PlatformArray.init(gpa_allocator); | |
var platform_iterator = platform_dir.iterate(); | |
while (try platform_iterator.next()) |entry| { | |
const child_dir = try platform_dir.openDir(entry.name, .{}); | |
const json_file = try child_dir.openFile("build.json", .{}); | |
const json_content = try json_file.inStream().readAllAlloc(gpa_allocator, std.math.maxInt(u64)); | |
defer gpa_allocator.free(json_content); | |
var json_token_stream = std.json.TokenStream.init(json_content); | |
const platform_info = try std.json.parse(PlatformInfo, &json_token_stream, std.json.ParseOptions{ .allocator = gpa_allocator }); | |
try platform_array.append(platform_info); | |
} | |
return platform_array; | |
} | |
pub fn build(b: *Builder) !void { | |
const platforms = try parsePlatformBuild(); | |
defer platforms.deinit(); | |
const target = b.standardTargetOptions(.{}); | |
const mode = b.standardReleaseOptions(); | |
const exe = b.addExecutable("SomeGameEngine", "src/main.zig"); | |
const platform_info: PlatformInfo = blk: { | |
for (platforms.items) |platform| { | |
if (platform.os == target.getOsTag()) { | |
break :blk platform; | |
} | |
} | |
return error.PlatformInfoNotFound; | |
}; | |
for (platform_info.include_dirs) |include_dir| { | |
exe.addSystemIncludeDir(include_dir); | |
} | |
for (platform_info.libraries_dirs) |lib| { | |
exe.addLibPath(lib); | |
} | |
for (platform_info.libraries) |lib| { | |
exe.linkSystemLibrary(lib); | |
} | |
exe.setTarget(target); | |
exe.setBuildMode(mode); | |
exe.install(); | |
const run_cmd = exe.run(); | |
run_cmd.step.dependOn(b.getInstallStep()); | |
const run_step = b.step("run", "Run the app"); | |
run_step.dependOn(&run_cmd.step); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment