gpu-dawn: update to latest Zig build API

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2023-02-08 01:22:57 -07:00 committed by Stephen Gutekanst
parent 46a805bc90
commit 0f71895634
2 changed files with 84 additions and 59 deletions

View file

@ -1,11 +1,11 @@
const std = @import("std"); const std = @import("std");
const Builder = std.build.Builder; const Build = std.Build;
const glfw = @import("libs/mach-glfw/build.zig"); const glfw = @import("libs/mach-glfw/build.zig");
const system_sdk = @import("libs/mach-glfw/system_sdk.zig"); const system_sdk = @import("libs/mach-glfw/system_sdk.zig");
const gpu_dawn_sdk = @import("sdk.zig"); const gpu_dawn_sdk = @import("sdk.zig");
pub fn build(b: *Builder) !void { pub fn build(b: *Build) !void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const gpu_dawn = gpu_dawn_sdk.Sdk(.{ const gpu_dawn = gpu_dawn_sdk.Sdk(.{
.glfw_include_dir = "libs/mach-glfw/upstream/glfw/include", .glfw_include_dir = "libs/mach-glfw/upstream/glfw/include",
@ -19,11 +19,14 @@ pub fn build(b: *Builder) !void {
// Just to demonstrate/test linking. This is not a functional example, see the mach/gpu examples // Just to demonstrate/test linking. This is not a functional example, see the mach/gpu examples
// or Dawn C++ examples for functional example code. // or Dawn C++ examples for functional example code.
const example = b.addExecutable("dawn-example", "src/main.zig"); const example = b.addExecutable(.{
example.setBuildMode(mode); .name = "dawn-example",
example.setTarget(target); .root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
try gpu_dawn.link(b, example, options); try gpu_dawn.link(b, example, options);
try glfw.link(b, example, .{ .system_sdk = .{ .set_sysroot = false } }); try glfw.link(b, example, .{ .system_sdk = .{ .set_sysroot = false } });
example.addPackage(glfw.pkg); example.addModule("glfw", glfw.module(b));
example.install(); example.install();
} }

View file

@ -1,5 +1,5 @@
const std = @import("std"); const std = @import("std");
const Builder = std.build.Builder; const Build = std.Build;
pub fn Sdk(comptime deps: anytype) type { pub fn Sdk(comptime deps: anytype) type {
return struct { return struct {
@ -59,7 +59,7 @@ pub fn Sdk(comptime deps: anytype) type {
} }
}; };
pub fn link(b: *Builder, step: *std.build.LibExeObjStep, options: Options) !void { pub fn link(b: *Build, step: *std.build.CompileStep, options: Options) !void {
const opt = options.detectDefaults(step.target_info.target); const opt = options.detectDefaults(step.target_info.target);
try if (options.from_source) try if (options.from_source)
@ -68,7 +68,7 @@ pub fn Sdk(comptime deps: anytype) type {
linkFromBinary(b, step, opt); linkFromBinary(b, step, opt);
} }
fn linkFromSource(b: *Builder, step: *std.build.LibExeObjStep, options: Options) !void { fn linkFromSource(b: *Build, step: *std.build.CompileStep, options: Options) !void {
try ensureGitRepoCloned(b.allocator, "https://github.com/hexops/dawn", "generated-2023-01-28.1674950134", sdkPath("/libs/dawn")); try ensureGitRepoCloned(b.allocator, "https://github.com/hexops/dawn", "generated-2023-01-28.1674950134", sdkPath("/libs/dawn"));
// branch: mach // branch: mach
@ -113,9 +113,11 @@ pub fn Sdk(comptime deps: anytype) type {
return; return;
} }
const lib_dawn = b.addStaticLibrary("dawn", null); const lib_dawn = b.addStaticLibrary(.{
lib_dawn.setBuildMode(if (options.debug) .Debug else .ReleaseFast); .name = "dawn",
lib_dawn.setTarget(step.target); .target = step.target,
.optimize = if (options.debug) .Debug else .ReleaseFast,
});
lib_dawn.linkLibCpp(); lib_dawn.linkLibCpp();
if (options.install_libs) if (options.install_libs)
lib_dawn.install(); lib_dawn.install();
@ -214,7 +216,7 @@ pub fn Sdk(comptime deps: anytype) type {
} }
} }
pub fn linkFromBinary(b: *Builder, step: *std.build.LibExeObjStep, options: Options) !void { pub fn linkFromBinary(b: *Build, step: *std.build.CompileStep, options: Options) !void {
const target = step.target_info.target; const target = step.target_info.target;
const binaries_available = switch (target.os.tag) { const binaries_available = switch (target.os.tag) {
.windows => target.abi.isGnu(), .windows => target.abi.isGnu(),
@ -553,7 +555,7 @@ pub fn Sdk(comptime deps: anytype) type {
}; };
} }
pub fn appendFlags(step: *std.build.LibExeObjStep, flags: *std.ArrayList([]const u8), debug_symbols: bool, is_cpp: bool) !void { pub fn appendFlags(step: *std.build.CompileStep, flags: *std.ArrayList([]const u8), debug_symbols: bool, is_cpp: bool) !void {
if (debug_symbols) try flags.append("-g1") else try flags.append("-g0"); if (debug_symbols) try flags.append("-g1") else try flags.append("-g0");
if (is_cpp) try flags.append("-std=c++17"); if (is_cpp) try flags.append("-std=c++17");
if (isLinuxDesktopLike(step.target_info.target.os.tag)) { if (isLinuxDesktopLike(step.target_info.target.os.tag)) {
@ -562,11 +564,13 @@ pub fn Sdk(comptime deps: anytype) type {
} }
} }
fn buildLibMachDawnNative(b: *Builder, step: *std.build.LibExeObjStep, options: Options) !*std.build.LibExeObjStep { fn buildLibMachDawnNative(b: *Build, step: *std.build.CompileStep, options: Options) !*std.build.CompileStep {
const lib = if (!options.separate_libs) step else blk: { const lib = if (!options.separate_libs) step else blk: {
const separate_lib = b.addStaticLibrary("dawn-native-mach", null); const separate_lib = b.addStaticLibrary(.{
separate_lib.setBuildMode(if (options.debug) .Debug else .ReleaseFast); .name = "dawn-native-mach",
separate_lib.setTarget(step.target); .target = step.target,
.optimize = if (options.debug) .Debug else .ReleaseFast,
});
separate_lib.linkLibCpp(); separate_lib.linkLibCpp();
if (options.install_libs) if (options.install_libs)
separate_lib.install(); separate_lib.install();
@ -594,11 +598,13 @@ pub fn Sdk(comptime deps: anytype) type {
} }
// Builds common sources; derived from src/common/BUILD.gn // Builds common sources; derived from src/common/BUILD.gn
fn buildLibDawnCommon(b: *Builder, step: *std.build.LibExeObjStep, options: Options) !*std.build.LibExeObjStep { fn buildLibDawnCommon(b: *Build, step: *std.build.CompileStep, options: Options) !*std.build.CompileStep {
const lib = if (!options.separate_libs) step else blk: { const lib = if (!options.separate_libs) step else blk: {
const separate_lib = b.addStaticLibrary("dawn-common", null); const separate_lib = b.addStaticLibrary(.{
separate_lib.setBuildMode(if (options.debug) .Debug else .ReleaseFast); .name = "dawn-common",
separate_lib.setTarget(step.target); .target = step.target,
.optimize = if (options.debug) .Debug else .ReleaseFast,
});
separate_lib.linkLibCpp(); separate_lib.linkLibCpp();
if (options.install_libs) if (options.install_libs)
separate_lib.install(); separate_lib.install();
@ -646,11 +652,13 @@ pub fn Sdk(comptime deps: anytype) type {
} }
// Build dawn platform sources; derived from src/dawn/platform/BUILD.gn // Build dawn platform sources; derived from src/dawn/platform/BUILD.gn
fn buildLibDawnPlatform(b: *Builder, step: *std.build.LibExeObjStep, options: Options) !*std.build.LibExeObjStep { fn buildLibDawnPlatform(b: *Build, step: *std.build.CompileStep, options: Options) !*std.build.CompileStep {
const lib = if (!options.separate_libs) step else blk: { const lib = if (!options.separate_libs) step else blk: {
const separate_lib = b.addStaticLibrary("dawn-platform", null); const separate_lib = b.addStaticLibrary(.{
separate_lib.setBuildMode(if (options.debug) .Debug else .ReleaseFast); .name = "dawn-platform",
separate_lib.setTarget(step.target); .target = step.target,
.optimize = if (options.debug) .Debug else .ReleaseFast,
});
separate_lib.linkLibCpp(); separate_lib.linkLibCpp();
if (options.install_libs) if (options.install_libs)
separate_lib.install(); separate_lib.install();
@ -717,11 +725,13 @@ pub fn Sdk(comptime deps: anytype) type {
}; };
// Builds dawn native sources; derived from src/dawn/native/BUILD.gn // Builds dawn native sources; derived from src/dawn/native/BUILD.gn
fn buildLibDawnNative(b: *Builder, step: *std.build.LibExeObjStep, options: Options) !*std.build.LibExeObjStep { fn buildLibDawnNative(b: *Build, step: *std.build.CompileStep, options: Options) !*std.build.CompileStep {
const lib = if (!options.separate_libs) step else blk: { const lib = if (!options.separate_libs) step else blk: {
const separate_lib = b.addStaticLibrary("dawn-native", null); const separate_lib = b.addStaticLibrary(.{
separate_lib.setBuildMode(if (options.debug) .Debug else .ReleaseFast); .name = "dawn-native",
separate_lib.setTarget(step.target); .target = step.target,
.optimize = if (options.debug) .Debug else .ReleaseFast,
});
separate_lib.linkLibCpp(); separate_lib.linkLibCpp();
if (options.install_libs) if (options.install_libs)
separate_lib.install(); separate_lib.install();
@ -999,11 +1009,13 @@ pub fn Sdk(comptime deps: anytype) type {
} }
// Builds tint sources; derived from src/tint/BUILD.gn // Builds tint sources; derived from src/tint/BUILD.gn
fn buildLibTint(b: *Builder, step: *std.build.LibExeObjStep, options: Options) !*std.build.LibExeObjStep { fn buildLibTint(b: *Build, step: *std.build.CompileStep, options: Options) !*std.build.CompileStep {
const lib = if (!options.separate_libs) step else blk: { const lib = if (!options.separate_libs) step else blk: {
const separate_lib = b.addStaticLibrary("tint", null); const separate_lib = b.addStaticLibrary(.{
separate_lib.setBuildMode(if (options.debug) .Debug else .ReleaseFast); .name = "tint",
separate_lib.setTarget(step.target); .target = step.target,
.optimize = if (options.debug) .Debug else .ReleaseFast,
});
separate_lib.linkLibCpp(); separate_lib.linkLibCpp();
if (options.install_libs) if (options.install_libs)
separate_lib.install(); separate_lib.install();
@ -1152,11 +1164,13 @@ pub fn Sdk(comptime deps: anytype) type {
} }
// Builds third_party/vulkan-deps/spirv-tools sources; derived from third_party/vulkan-deps/spirv-tools/src/BUILD.gn // Builds third_party/vulkan-deps/spirv-tools sources; derived from third_party/vulkan-deps/spirv-tools/src/BUILD.gn
fn buildLibSPIRVTools(b: *Builder, step: *std.build.LibExeObjStep, options: Options) !*std.build.LibExeObjStep { fn buildLibSPIRVTools(b: *Build, step: *std.build.CompileStep, options: Options) !*std.build.CompileStep {
const lib = if (!options.separate_libs) step else blk: { const lib = if (!options.separate_libs) step else blk: {
const separate_lib = b.addStaticLibrary("spirv-tools", null); const separate_lib = b.addStaticLibrary(.{
separate_lib.setBuildMode(if (options.debug) .Debug else .ReleaseFast); .name = "spirv-tools",
separate_lib.setTarget(step.target); .target = step.target,
.optimize = if (options.debug) .Debug else .ReleaseFast,
});
separate_lib.linkLibCpp(); separate_lib.linkLibCpp();
if (options.install_libs) if (options.install_libs)
separate_lib.install(); separate_lib.install();
@ -1219,11 +1233,13 @@ pub fn Sdk(comptime deps: anytype) type {
// $ find third_party/abseil-cpp/absl | grep '\.cc' | grep -v 'test' | grep -v 'benchmark' | grep -v gaussian_distribution_gentables | grep -v print_hash_of | grep -v chi_square // $ find third_party/abseil-cpp/absl | grep '\.cc' | grep -v 'test' | grep -v 'benchmark' | grep -v gaussian_distribution_gentables | grep -v print_hash_of | grep -v chi_square
// ``` // ```
// //
fn buildLibAbseilCpp(b: *Builder, step: *std.build.LibExeObjStep, options: Options) !*std.build.LibExeObjStep { fn buildLibAbseilCpp(b: *Build, step: *std.build.CompileStep, options: Options) !*std.build.CompileStep {
const lib = if (!options.separate_libs) step else blk: { const lib = if (!options.separate_libs) step else blk: {
const separate_lib = b.addStaticLibrary("abseil-cpp-common", null); const separate_lib = b.addStaticLibrary(.{
separate_lib.setBuildMode(if (options.debug) .Debug else .ReleaseFast); .name = "abseil-cpp-common",
separate_lib.setTarget(step.target); .target = step.target,
.optimize = if (options.debug) .Debug else .ReleaseFast,
});
separate_lib.linkLibCpp(); separate_lib.linkLibCpp();
if (options.install_libs) if (options.install_libs)
separate_lib.install(); separate_lib.install();
@ -1280,11 +1296,13 @@ pub fn Sdk(comptime deps: anytype) type {
} }
// Buids dawn wire sources; derived from src/dawn/wire/BUILD.gn // Buids dawn wire sources; derived from src/dawn/wire/BUILD.gn
fn buildLibDawnWire(b: *Builder, step: *std.build.LibExeObjStep, options: Options) !*std.build.LibExeObjStep { fn buildLibDawnWire(b: *Build, step: *std.build.CompileStep, options: Options) !*std.build.CompileStep {
const lib = if (!options.separate_libs) step else blk: { const lib = if (!options.separate_libs) step else blk: {
const separate_lib = b.addStaticLibrary("dawn-wire", null); const separate_lib = b.addStaticLibrary(.{
separate_lib.setBuildMode(if (options.debug) .Debug else .ReleaseFast); .name = "dawn-wire",
separate_lib.setTarget(step.target); .target = step.target,
.optimize = if (options.debug) .Debug else .ReleaseFast,
});
separate_lib.linkLibCpp(); separate_lib.linkLibCpp();
if (options.install_libs) if (options.install_libs)
separate_lib.install(); separate_lib.install();
@ -1316,11 +1334,13 @@ pub fn Sdk(comptime deps: anytype) type {
} }
// Builds dawn utils sources; derived from src/dawn/utils/BUILD.gn // Builds dawn utils sources; derived from src/dawn/utils/BUILD.gn
fn buildLibDawnUtils(b: *Builder, step: *std.build.LibExeObjStep, options: Options) !*std.build.LibExeObjStep { fn buildLibDawnUtils(b: *Build, step: *std.build.CompileStep, options: Options) !*std.build.CompileStep {
const lib = if (!options.separate_libs) step else blk: { const lib = if (!options.separate_libs) step else blk: {
const separate_lib = b.addStaticLibrary("dawn-utils", null); const separate_lib = b.addStaticLibrary(.{
separate_lib.setBuildMode(if (options.debug) .Debug else .ReleaseFast); .name = "dawn-utils",
separate_lib.setTarget(step.target); .target = step.target,
.optimize = if (options.debug) .Debug else .ReleaseFast,
});
separate_lib.linkLibCpp(); separate_lib.linkLibCpp();
if (options.install_libs) if (options.install_libs)
separate_lib.install(); separate_lib.install();
@ -1389,11 +1409,13 @@ pub fn Sdk(comptime deps: anytype) type {
} }
// Buids dxcompiler sources; derived from libs/DirectXShaderCompiler/CMakeLists.txt // Buids dxcompiler sources; derived from libs/DirectXShaderCompiler/CMakeLists.txt
fn buildLibDxcompiler(b: *Builder, step: *std.build.LibExeObjStep, options: Options) !*std.build.LibExeObjStep { fn buildLibDxcompiler(b: *Build, step: *std.build.CompileStep, options: Options) !*std.build.CompileStep {
const lib = if (!options.separate_libs) step else blk: { const lib = if (!options.separate_libs) step else blk: {
const separate_lib = b.addStaticLibrary("dxcompiler", null); const separate_lib = b.addStaticLibrary(.{
separate_lib.setBuildMode(if (options.debug) .Debug else .ReleaseFast); .name = "dxcompiler",
separate_lib.setTarget(step.target); .target = step.target,
.optimize = if (options.debug) .Debug else .ReleaseFast,
});
separate_lib.linkLibCpp(); separate_lib.linkLibCpp();
if (options.install_libs) if (options.install_libs)
separate_lib.install(); separate_lib.install();
@ -1489,8 +1511,8 @@ pub fn Sdk(comptime deps: anytype) type {
} }
fn appendLangScannedSources( fn appendLangScannedSources(
b: *Builder, b: *Build,
step: *std.build.LibExeObjStep, step: *std.build.CompileStep,
args: struct { args: struct {
debug_symbols: bool = false, debug_symbols: bool = false,
flags: []const []const u8, flags: []const []const u8,
@ -1525,7 +1547,7 @@ pub fn Sdk(comptime deps: anytype) type {
}); });
} }
fn appendScannedSources(b: *Builder, step: *std.build.LibExeObjStep, args: struct { fn appendScannedSources(b: *Build, step: *std.build.CompileStep, args: struct {
flags: []const []const u8, flags: []const []const u8,
rel_dirs: []const []const u8 = &.{}, rel_dirs: []const []const u8 = &.{},
extensions: []const []const u8, extensions: []const []const u8,
@ -1543,7 +1565,7 @@ pub fn Sdk(comptime deps: anytype) type {
/// listed in the excluded list. /// listed in the excluded list.
/// Results are appended to the dst ArrayList. /// Results are appended to the dst ArrayList.
fn scanSources( fn scanSources(
b: *Builder, b: *Build,
dst: *std.ArrayList([]const u8), dst: *std.ArrayList([]const u8),
rel_dir: []const u8, rel_dir: []const u8,
extensions: []const []const u8, extensions: []const []const u8,