gpu-dawn: remove xcode_frameworks submodule; Sdk type

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2023-07-06 23:55:28 -07:00
parent 5aa06c2234
commit 2caffc8d35
4 changed files with 1682 additions and 1594 deletions

View file

@ -3,11 +3,7 @@ const builtin = @import("builtin");
const freetype = @import("libs/freetype/build.zig");
const glfw = @import("libs/glfw/build.zig");
const sysaudio = @import("mach_sysaudio");
pub const gpu_dawn = @import("libs/gpu-dawn/build.zig").Sdk(.{
// TODO(build-system): This cannot be imported with the Zig package manager
// error: TarUnsupportedFileType
.xcode_frameworks = @import("libs/gpu-dawn/libs/xcode-frameworks/build.zig"),
});
pub const gpu_dawn = @import("libs/gpu-dawn/build.zig"); // TODO(build-system): make this private
const gpu = @import("libs/gpu/build.zig").Sdk(.{
.gpu_dawn = gpu_dawn,
});

View file

@ -1,11 +1,7 @@
const std = @import("std");
const builtin = @import("builtin");
const glfw = @import("libs/mach-glfw/build.zig");
const gpu_dawn = @import("libs/mach-gpu-dawn/build.zig").Sdk(.{
// TODO(build-system): This cannot be imported with the Zig package manager
// error: TarUnsupportedFileType
.xcode_frameworks = @import("libs/mach-gpu-dawn/libs/xcode-frameworks/build.zig"),
});
const gpu_dawn = @import("libs/mach-gpu-dawn/build.zig");
const gpu = @import("libs/mach-gpu/build.zig").Sdk(.{
.gpu_dawn = gpu_dawn,
});

View file

@ -5,13 +5,7 @@ pub fn build(b: *Build) !void {
const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{});
const glfw = @import("libs/mach-glfw/build.zig");
const gpu_dawn = Sdk(.{
// TODO(build-system): This cannot be imported with the Zig package manager
// error: TarUnsupportedFileType
.xcode_frameworks = @import("libs/xcode-frameworks/build.zig"),
});
const options = gpu_dawn.Options{
const options = Options{
.install_libs = true,
.from_source = true,
};
@ -24,15 +18,13 @@ pub fn build(b: *Build) !void {
.target = target,
.optimize = optimize,
});
try gpu_dawn.link(b, example, options);
try link(b, example, options);
try glfw.link(b, example, .{});
example.addModule("glfw", glfw.module(b));
b.installArtifact(example);
}
pub fn Sdk(comptime deps: anytype) type {
return struct {
pub const Options = struct {
pub const Options = struct {
/// Defaults to true on Windows
d3d12: ?bool = null,
@ -86,9 +78,9 @@ pub fn Sdk(comptime deps: anytype) type {
return options;
}
};
};
pub fn link(b: *Build, step: *std.build.CompileStep, options: Options) !void {
pub fn link(b: *Build, step: *std.build.CompileStep, options: Options) !void {
const opt = options.detectDefaults(step.target_info.target);
if (step.target_info.target.os.tag == .windows) @import("direct3d_headers").addLibraryPath(step);
@ -101,16 +93,16 @@ pub fn Sdk(comptime deps: anytype) type {
// .optimize = step.optimize,
// }).artifact("xcode-frameworks"));
// @import("xcode_frameworks").addPaths(step);
deps.xcode_frameworks.addPaths(step);
xcode_frameworks.addPaths(b, step);
}
try if (options.from_source)
linkFromSource(b, step, opt)
else
linkFromBinary(b, step, opt);
}
}
fn linkFromSource(b: *Build, step: *std.build.CompileStep, 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-06-30.1688174725", sdkPath("/libs/dawn"));
// branch: mach
@ -159,9 +151,9 @@ pub fn Sdk(comptime deps: anytype) type {
_ = try buildLibSPIRVTools(b, lib_dawn, options);
_ = try buildLibTint(b, lib_dawn, options);
if (options.d3d12.?) _ = try buildLibDxcompiler(b, lib_dawn, options);
}
}
fn ensureGitRepoCloned(allocator: std.mem.Allocator, clone_url: []const u8, revision: []const u8, dir: []const u8) !void {
fn ensureGitRepoCloned(allocator: std.mem.Allocator, clone_url: []const u8, revision: []const u8, dir: []const u8) !void {
if (isEnvVarTruthy(allocator, "NO_ENSURE_SUBMODULES") or isEnvVarTruthy(allocator, "NO_ENSURE_GIT")) {
return;
}
@ -188,22 +180,22 @@ pub fn Sdk(comptime deps: anytype) type {
},
else => err,
};
}
}
fn exec(allocator: std.mem.Allocator, argv: []const []const u8, cwd: []const u8) !void {
fn exec(allocator: std.mem.Allocator, argv: []const []const u8, cwd: []const u8) !void {
var child = std.ChildProcess.init(argv, allocator);
child.cwd = cwd;
_ = try child.spawnAndWait();
}
}
fn getCurrentGitRevision(allocator: std.mem.Allocator, cwd: []const u8) ![]const u8 {
fn getCurrentGitRevision(allocator: std.mem.Allocator, cwd: []const u8) ![]const u8 {
const result = try std.ChildProcess.exec(.{ .allocator = allocator, .argv = &.{ "git", "rev-parse", "HEAD" }, .cwd = cwd });
allocator.free(result.stderr);
if (result.stdout.len > 0) return result.stdout[0 .. result.stdout.len - 1]; // trim newline
return result.stdout;
}
}
fn ensureGit(allocator: std.mem.Allocator) void {
fn ensureGit(allocator: std.mem.Allocator) void {
const argv = &[_][]const u8{ "git", "--version" };
const result = std.ChildProcess.exec(.{
.allocator = allocator,
@ -221,9 +213,9 @@ pub fn Sdk(comptime deps: anytype) type {
std.log.err("mach: error: 'git --version' failed. Is git not installed?", .{});
std.process.exit(1);
}
}
}
fn isEnvVarTruthy(allocator: std.mem.Allocator, name: []const u8) bool {
fn isEnvVarTruthy(allocator: std.mem.Allocator, name: []const u8) bool {
if (std.process.getEnvVarOwned(allocator, name)) |truthy| {
defer allocator.free(truthy);
if (std.mem.eql(u8, truthy, "true")) return true;
@ -231,18 +223,18 @@ pub fn Sdk(comptime deps: anytype) type {
} else |_| {
return false;
}
}
}
fn getGitHubBaseURLOwned(allocator: std.mem.Allocator) ![]const u8 {
fn getGitHubBaseURLOwned(allocator: std.mem.Allocator) ![]const u8 {
if (std.process.getEnvVarOwned(allocator, "MACH_GITHUB_BASE_URL")) |base_url| {
std.log.info("mach: respecting MACH_GITHUB_BASE_URL: {s}\n", .{base_url});
return base_url;
} else |_| {
return allocator.dupe(u8, "https://github.com");
}
}
}
pub fn linkFromBinary(b: *Build, step: *std.build.CompileStep, options: Options) !void {
pub fn linkFromBinary(b: *Build, step: *std.build.CompileStep, options: Options) !void {
const target = step.target_info.target;
const binaries_available = switch (target.os.tag) {
.windows => target.abi.isGnu(),
@ -313,15 +305,15 @@ pub fn Sdk(comptime deps: anytype) type {
linkLibAbseilCppDependencies(b, step, options);
linkLibDawnWireDependencies(b, step, options);
linkLibDxcompilerDependencies(b, step, options);
}
}
pub fn ensureBinaryDownloaded(
pub fn ensureBinaryDownloaded(
allocator: std.mem.Allocator,
zig_triple: []const u8,
is_debug: bool,
is_windows: bool,
version: []const u8,
) !void {
) !void {
// If zig-cache/mach/gpu-dawn/<git revision> does not exist:
// If on a commit in the main branch => rm -r zig-cache/mach/gpu-dawn/
// else => noop
@ -364,9 +356,9 @@ pub fn Sdk(comptime deps: anytype) type {
std.log.err("mach/gpu-dawn: prebuilt binary download failed: {s}", .{@errorName(err)});
std.process.exit(1);
};
}
}
fn downloadBinary(
fn downloadBinary(
allocator: std.mem.Allocator,
commit_cache_dir: []const u8,
release_tag: []const u8,
@ -374,7 +366,7 @@ pub fn Sdk(comptime deps: anytype) type {
zig_triple: []const u8,
is_windows: bool,
version: []const u8,
) !void {
) !void {
ensureCanDownloadFiles(allocator);
const download_dir = try std.fs.path.join(allocator, &.{ target_cache_dir, "download" });
@ -442,9 +434,9 @@ pub fn Sdk(comptime deps: anytype) type {
}
try std.fs.deleteTreeAbsolute(download_dir);
}
}
fn extractHeaders(allocator: std.mem.Allocator, json_file: []const u8, out_dir: []const u8) !void {
fn extractHeaders(allocator: std.mem.Allocator, json_file: []const u8, out_dir: []const u8) !void {
const contents = try std.fs.cwd().readFileAlloc(allocator, json_file, std.math.maxInt(usize));
defer allocator.free(contents);
@ -461,15 +453,15 @@ pub fn Sdk(comptime deps: anytype) type {
defer new_file.close();
try new_file.writeAll(f.value_ptr.*.string);
}
}
}
fn dirExists(path: []const u8) bool {
fn dirExists(path: []const u8) bool {
var dir = std.fs.openDirAbsolute(path, .{}) catch return false;
dir.close();
return true;
}
}
fn gzipDecompress(allocator: std.mem.Allocator, src_absolute_path: []const u8, dst_absolute_path: []const u8) !void {
fn gzipDecompress(allocator: std.mem.Allocator, src_absolute_path: []const u8, dst_absolute_path: []const u8) !void {
var file = try std.fs.openFileAbsolute(src_absolute_path, .{ .mode = .read_only });
defer file.close();
@ -485,9 +477,9 @@ pub fn Sdk(comptime deps: anytype) type {
defer new_file.close();
try new_file.writeAll(buf);
}
}
fn gitBranchContainsCommit(allocator: std.mem.Allocator, branch: []const u8, commit: []const u8) !bool {
fn gitBranchContainsCommit(allocator: std.mem.Allocator, branch: []const u8, commit: []const u8) !bool {
const result = try std.ChildProcess.exec(.{
.allocator = allocator,
.argv = &.{ "git", "branch", branch, "--contains", commit },
@ -498,9 +490,9 @@ pub fn Sdk(comptime deps: anytype) type {
allocator.free(result.stderr);
}
return result.term.Exited == 0;
}
}
fn getCurrentGitCommit(allocator: std.mem.Allocator) ![]const u8 {
fn getCurrentGitCommit(allocator: std.mem.Allocator) ![]const u8 {
const result = try std.ChildProcess.exec(.{
.allocator = allocator,
.argv = &.{ "git", "rev-parse", "HEAD" },
@ -509,9 +501,9 @@ pub fn Sdk(comptime deps: anytype) type {
defer allocator.free(result.stderr);
if (result.stdout.len > 0) return result.stdout[0 .. result.stdout.len - 1]; // trim newline
return result.stdout;
}
}
fn gitClone(allocator: std.mem.Allocator, repository: []const u8, dir: []const u8) !bool {
fn gitClone(allocator: std.mem.Allocator, repository: []const u8, dir: []const u8) !bool {
const result = try std.ChildProcess.exec(.{
.allocator = allocator,
.argv = &.{ "git", "clone", repository, dir },
@ -522,9 +514,9 @@ pub fn Sdk(comptime deps: anytype) type {
allocator.free(result.stderr);
}
return result.term.Exited == 0;
}
}
fn downloadFile(allocator: std.mem.Allocator, target_file: []const u8, url: []const u8) !void {
fn downloadFile(allocator: std.mem.Allocator, target_file: []const u8, url: []const u8) !void {
std.debug.print("downloading {s}..\n", .{url});
// Some Windows users experience `SSL certificate problem: unable to get local issuer certificate`
@ -537,9 +529,9 @@ pub fn Sdk(comptime deps: anytype) type {
child.stderr = std.io.getStdErr();
child.stdout = std.io.getStdOut();
_ = try child.spawnAndWait();
}
}
fn ensureCanDownloadFiles(allocator: std.mem.Allocator) void {
fn ensureCanDownloadFiles(allocator: std.mem.Allocator) void {
const result = std.ChildProcess.exec(.{
.allocator = allocator,
.argv = &.{ "curl", "--version" },
@ -556,9 +548,9 @@ pub fn Sdk(comptime deps: anytype) type {
std.log.err("mach: error: 'curl --version' failed. Is curl not installed?", .{});
std.process.exit(1);
}
}
}
fn isLinuxDesktopLike(tag: std.Target.Os.Tag) bool {
fn isLinuxDesktopLike(tag: std.Target.Os.Tag) bool {
return switch (tag) {
.linux,
.freebsd,
@ -568,18 +560,18 @@ pub fn Sdk(comptime deps: anytype) type {
=> true,
else => false,
};
}
}
pub fn appendFlags(step: *std.build.CompileStep, 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 (is_cpp) try flags.append("-std=c++17");
if (isLinuxDesktopLike(step.target_info.target.os.tag)) {
try flags.append("-DDAWN_USE_X11");
try flags.append("-DDAWN_USE_WAYLAND");
}
}
}
fn linkLibDawnCommonDependencies(b: *Build, step: *std.build.CompileStep, options: Options) void {
fn linkLibDawnCommonDependencies(b: *Build, step: *std.build.CompileStep, options: Options) void {
_ = b;
_ = options;
step.linkLibCpp();
@ -592,15 +584,15 @@ pub fn Sdk(comptime deps: anytype) type {
// .optimize = step.optimize,
// }).artifact("xcode-frameworks"));
// @import("xcode_frameworks").addPaths(step);
deps.xcode_frameworks.addPaths(step);
xcode_frameworks.addPaths(b, step);
step.linkSystemLibraryName("objc");
step.linkFramework("Foundation");
}
}
}
// Builds common sources; derived from src/common/BUILD.gn
fn buildLibDawnCommon(b: *Build, step: *std.build.CompileStep, options: Options) !*std.build.CompileStep {
// Builds common sources; derived from src/common/BUILD.gn
fn buildLibDawnCommon(b: *Build, step: *std.build.CompileStep, options: Options) !*std.build.CompileStep {
const lib = if (!options.separate_libs) step else b.addStaticLibrary(.{
.name = "dawn-common",
.target = step.target,
@ -650,16 +642,16 @@ pub fn Sdk(comptime deps: anytype) type {
try appendFlags(step, &cpp_flags, options.debug, true);
lib.addCSourceFiles(cpp_sources.items, cpp_flags.items);
return lib;
}
}
fn linkLibDawnPlatformDependencies(b: *Build, step: *std.build.CompileStep, options: Options) void {
fn linkLibDawnPlatformDependencies(b: *Build, step: *std.build.CompileStep, options: Options) void {
_ = b;
_ = options;
step.linkLibCpp();
}
}
// Build dawn platform sources; derived from src/dawn/platform/BUILD.gn
fn buildLibDawnPlatform(b: *Build, step: *std.build.CompileStep, options: Options) !*std.build.CompileStep {
// Build dawn platform sources; derived from src/dawn/platform/BUILD.gn
fn buildLibDawnPlatform(b: *Build, step: *std.build.CompileStep, options: Options) !*std.build.CompileStep {
const lib = if (!options.separate_libs) step else b.addStaticLibrary(.{
.name = "dawn-platform",
.target = step.target,
@ -689,9 +681,9 @@ pub fn Sdk(comptime deps: anytype) type {
lib.addCSourceFiles(cpp_sources.items, cpp_flags.items);
return lib;
}
}
fn appendDawnEnableBackendTypeFlags(flags: *std.ArrayList([]const u8), options: Options) !void {
fn appendDawnEnableBackendTypeFlags(flags: *std.ArrayList([]const u8), options: Options) !void {
const d3d12 = "-DDAWN_ENABLE_BACKEND_D3D12";
const metal = "-DDAWN_ENABLE_BACKEND_METAL";
const vulkan = "-DDAWN_ENABLE_BACKEND_VULKAN";
@ -706,9 +698,9 @@ pub fn Sdk(comptime deps: anytype) type {
if (options.vulkan.?) try flags.append(vulkan);
if (options.desktop_gl.?) try flags.appendSlice(&.{ opengl, desktop_gl });
if (options.opengl_es.?) try flags.appendSlice(&.{ opengl, opengl_es });
}
}
const dawn_d3d12_flags = &[_][]const u8{
const dawn_d3d12_flags = &[_][]const u8{
"-DDAWN_NO_WINDOWS_UI",
"-D__EMULATE_UUID=1",
"-Wno-nonportable-include-path",
@ -725,9 +717,9 @@ pub fn Sdk(comptime deps: anytype) type {
"-DWIN32_LEAN_AND_MEAN",
"-DD3D10_ARBITRARY_HEADER_ORDERING",
"-DNOMINMAX",
};
};
fn linkLibDawnNativeDependencies(b: *Build, step: *std.build.CompileStep, options: Options) void {
fn linkLibDawnNativeDependencies(b: *Build, step: *std.build.CompileStep, options: Options) void {
step.linkLibCpp();
if (options.d3d12.?) {
step.linkLibrary(b.dependency("direct3d_headers", .{
@ -744,7 +736,7 @@ pub fn Sdk(comptime deps: anytype) type {
// .optimize = step.optimize,
// }).artifact("xcode-frameworks"));
// @import("xcode_frameworks").addPaths(step);
deps.xcode_frameworks.addPaths(step);
xcode_frameworks.addPaths(b, step);
step.linkSystemLibraryName("objc");
step.linkFramework("Metal");
@ -758,10 +750,10 @@ pub fn Sdk(comptime deps: anytype) type {
if (isLinuxDesktopLike(tag)) {
step.linkSystemLibraryName("X11");
}
}
}
// Builds dawn native sources; derived from src/dawn/native/BUILD.gn
fn buildLibDawnNative(b: *Build, step: *std.build.CompileStep, options: Options) !*std.build.CompileStep {
// Builds dawn native sources; derived from src/dawn/native/BUILD.gn
fn buildLibDawnNative(b: *Build, step: *std.build.CompileStep, options: Options) !*std.build.CompileStep {
const lib = if (!options.separate_libs) step else b.addStaticLibrary(.{
.name = "dawn-native",
.target = step.target,
@ -1043,16 +1035,16 @@ pub fn Sdk(comptime deps: anytype) type {
try appendFlags(step, &cpp_flags, options.debug, true);
lib.addCSourceFiles(cpp_sources.items, cpp_flags.items);
return lib;
}
}
fn linkLibTintDependencies(b: *Build, step: *std.build.CompileStep, options: Options) void {
fn linkLibTintDependencies(b: *Build, step: *std.build.CompileStep, options: Options) void {
_ = b;
_ = options;
step.linkLibCpp();
}
}
// Builds tint sources; derived from src/tint/BUILD.gn
fn buildLibTint(b: *Build, step: *std.build.CompileStep, options: Options) !*std.build.CompileStep {
// Builds tint sources; derived from src/tint/BUILD.gn
fn buildLibTint(b: *Build, step: *std.build.CompileStep, options: Options) !*std.build.CompileStep {
const lib = if (!options.separate_libs) step else b.addStaticLibrary(.{
.name = "tint",
.target = step.target,
@ -1210,16 +1202,16 @@ pub fn Sdk(comptime deps: anytype) type {
try appendFlags(step, &cpp_flags, options.debug, true);
lib.addCSourceFiles(cpp_sources.items, cpp_flags.items);
return lib;
}
}
fn linkLibSPIRVToolsDependencies(b: *Build, step: *std.build.CompileStep, options: Options) void {
fn linkLibSPIRVToolsDependencies(b: *Build, step: *std.build.CompileStep, options: Options) void {
_ = b;
_ = options;
step.linkLibCpp();
}
}
// Builds third_party/vulkan-deps/spirv-tools sources; derived from third_party/vulkan-deps/spirv-tools/src/BUILD.gn
fn buildLibSPIRVTools(b: *Build, step: *std.build.CompileStep, options: Options) !*std.build.CompileStep {
// Builds third_party/vulkan-deps/spirv-tools sources; derived from third_party/vulkan-deps/spirv-tools/src/BUILD.gn
fn buildLibSPIRVTools(b: *Build, step: *std.build.CompileStep, options: Options) !*std.build.CompileStep {
const lib = if (!options.separate_libs) step else b.addStaticLibrary(.{
.name = "spirv-tools",
.target = step.target,
@ -1276,9 +1268,9 @@ pub fn Sdk(comptime deps: anytype) type {
.excluding_contains = &.{ "test", "benchmark" },
});
return lib;
}
}
fn linkLibAbseilCppDependencies(b: *Build, step: *std.build.CompileStep, options: Options) void {
fn linkLibAbseilCppDependencies(b: *Build, step: *std.build.CompileStep, options: Options) void {
_ = b;
_ = options;
step.linkLibCpp();
@ -1292,21 +1284,21 @@ pub fn Sdk(comptime deps: anytype) type {
// .optimize = step.optimize,
// }).artifact("xcode-frameworks"));
// @import("xcode_frameworks").addPaths(step);
deps.xcode_frameworks.addPaths(step);
xcode_frameworks.addPaths(b, step);
step.linkSystemLibraryName("objc");
step.linkFramework("CoreFoundation");
}
if (target.os.tag == .windows) step.linkSystemLibraryName("bcrypt");
}
}
// Builds third_party/abseil sources; derived from:
//
// ```
// $ 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: *Build, step: *std.build.CompileStep, options: Options) !*std.build.CompileStep {
// Builds third_party/abseil sources; derived from:
//
// ```
// $ 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: *Build, step: *std.build.CompileStep, options: Options) !*std.build.CompileStep {
const lib = if (!options.separate_libs) step else b.addStaticLibrary(.{
.name = "abseil",
.target = step.target,
@ -1364,16 +1356,16 @@ pub fn Sdk(comptime deps: anytype) type {
.excluding_contains = &.{ "_test", "_testing", "benchmark", "print_hash_of.cc", "gaussian_distribution_gentables.cc" },
});
return lib;
}
}
fn linkLibDawnWireDependencies(b: *Build, step: *std.build.CompileStep, options: Options) void {
fn linkLibDawnWireDependencies(b: *Build, step: *std.build.CompileStep, options: Options) void {
_ = b;
_ = options;
step.linkLibCpp();
}
}
// Buids dawn wire sources; derived from src/dawn/wire/BUILD.gn
fn buildLibDawnWire(b: *Build, step: *std.build.CompileStep, options: Options) !*std.build.CompileStep {
// Buids dawn wire sources; derived from src/dawn/wire/BUILD.gn
fn buildLibDawnWire(b: *Build, step: *std.build.CompileStep, options: Options) !*std.build.CompileStep {
const lib = if (!options.separate_libs) step else b.addStaticLibrary(.{
.name = "dawn-wire",
.target = step.target,
@ -1404,9 +1396,9 @@ pub fn Sdk(comptime deps: anytype) type {
.excluding_contains = &.{ "test", "benchmark", "mock" },
});
return lib;
}
}
fn linkLibDxcompilerDependencies(b: *Build, step: *std.build.CompileStep, options: Options) void {
fn linkLibDxcompilerDependencies(b: *Build, step: *std.build.CompileStep, options: Options) void {
if (options.d3d12.?) {
step.linkLibCpp();
step.linkLibrary(b.dependency("direct3d_headers", .{
@ -1418,10 +1410,10 @@ pub fn Sdk(comptime deps: anytype) type {
step.linkSystemLibraryName("ole32");
step.linkSystemLibraryName("dbghelp");
}
}
}
// Buids dxcompiler sources; derived from libs/DirectXShaderCompiler/CMakeLists.txt
fn buildLibDxcompiler(b: *Build, step: *std.build.CompileStep, options: Options) !*std.build.CompileStep {
// Buids dxcompiler sources; derived from libs/DirectXShaderCompiler/CMakeLists.txt
fn buildLibDxcompiler(b: *Build, step: *std.build.CompileStep, options: Options) !*std.build.CompileStep {
const lib = if (!options.separate_libs) step else b.addStaticLibrary(.{
.name = "dxcompiler",
.target = step.target,
@ -1509,9 +1501,9 @@ pub fn Sdk(comptime deps: anytype) type {
},
});
return lib;
}
}
fn appendLangScannedSources(
fn appendLangScannedSources(
b: *Build,
step: *std.build.CompileStep,
args: struct {
@ -1522,7 +1514,7 @@ pub fn Sdk(comptime deps: anytype) type {
excluding: []const []const u8 = &.{},
excluding_contains: []const []const u8 = &.{},
},
) !void {
) !void {
var cpp_flags = std.ArrayList([]const u8).init(b.allocator);
try cpp_flags.appendSlice(args.flags);
try appendFlags(step, &cpp_flags, args.debug_symbols, true);
@ -1546,33 +1538,33 @@ pub fn Sdk(comptime deps: anytype) type {
.excluding = args.excluding,
.excluding_contains = args.excluding_contains,
});
}
}
fn appendScannedSources(b: *Build, step: *std.build.CompileStep, args: struct {
fn appendScannedSources(b: *Build, step: *std.build.CompileStep, args: struct {
flags: []const []const u8,
rel_dirs: []const []const u8 = &.{},
extensions: []const []const u8,
excluding: []const []const u8 = &.{},
excluding_contains: []const []const u8 = &.{},
}) !void {
}) !void {
var sources = std.ArrayList([]const u8).init(b.allocator);
for (args.rel_dirs) |rel_dir| {
try scanSources(b, &sources, rel_dir, args.extensions, args.excluding, args.excluding_contains);
}
step.addCSourceFiles(sources.items, args.flags);
}
}
/// Scans rel_dir for sources ending with one of the provided extensions, excluding relative paths
/// listed in the excluded list.
/// Results are appended to the dst ArrayList.
fn scanSources(
/// Scans rel_dir for sources ending with one of the provided extensions, excluding relative paths
/// listed in the excluded list.
/// Results are appended to the dst ArrayList.
fn scanSources(
b: *Build,
dst: *std.ArrayList([]const u8),
rel_dir: []const u8,
extensions: []const []const u8,
excluding: []const []const u8,
excluding_contains: []const []const u8,
) !void {
) !void {
const abs_dir = try std.fs.path.join(b.allocator, &.{ sdkPath("/"), rel_dir });
defer b.allocator.free(abs_dir);
var dir = std.fs.openIterableDirAbsolute(abs_dir, .{}) catch |err| {
@ -1613,18 +1605,126 @@ pub fn Sdk(comptime deps: anytype) type {
try dst.append(abs_path);
}
}
}
fn include(comptime rel: []const u8) []const u8 {
fn include(comptime rel: []const u8) []const u8 {
return comptime "-I" ++ sdkPath("/" ++ rel);
}
fn sdkPath(comptime suffix: []const u8) []const u8 {
if (suffix[0] != '/') @compileError("suffix must be an absolute path");
return comptime blk: {
const root_dir = std.fs.path.dirname(@src().file) orelse ".";
break :blk root_dir ++ suffix;
};
}
// TODO(build-system): This is a workaround that we copy anywhere xcode_frameworks needs to be used.
// With the Zig package manager, it should be possible to remove this entirely and instead just
// write:
//
// ```
// step.linkLibrary(b.dependency("xcode_frameworks", .{
// .target = step.target,
// .optimize = step.optimize,
// }).artifact("xcode-frameworks"));
// @import("xcode_frameworks").addPaths(step);
// ```
//
// However, today this package cannot be imported with the Zig package manager due to `error: TarUnsupportedFileType`
// which would be fixed by https://github.com/ziglang/zig/pull/15382 - so instead for now you must
// copy+paste this struct into your `build.zig` and write:
//
// ```
// try xcode_frameworks.addPaths(b, step);
// ```
const xcode_frameworks = struct {
pub fn addPaths(b: *std.Build, step: *std.build.CompileStep) void {
// branch: mach
ensureGitRepoCloned(b.allocator, "https://github.com/hexops/xcode-frameworks", "723aa55e9752c8c6c25d3413722b5fe13d72ac4f", "zig-cache/xcode_frameworks") catch |err| @panic(@errorName(err));
step.addFrameworkPath("zig-cache/xcode_frameworks/Frameworks");
step.addSystemIncludePath("zig-cache/xcode_frameworks/include");
step.addLibraryPath("zig-cache/xcode_frameworks/lib");
}
fn sdkPath(comptime suffix: []const u8) []const u8 {
fn xcodeSdkPath(comptime suffix: []const u8) []const u8 {
if (suffix[0] != '/') @compileError("suffix must be an absolute path");
return comptime blk: {
const root_dir = std.fs.path.dirname(@src().file) orelse ".";
break :blk root_dir ++ suffix;
};
}
fn ensureGitRepoCloned(allocator: std.mem.Allocator, clone_url: []const u8, revision: []const u8, rel_dir: []const u8) !void {
if (isEnvVarTruthy(allocator, "NO_ENSURE_SUBMODULES") or isEnvVarTruthy(allocator, "NO_ENSURE_GIT")) {
return;
}
ensureGit(allocator);
if (std.fs.cwd().realpathAlloc(allocator, rel_dir)) |dir| {
const current_revision = try getCurrentGitRevision(allocator, dir);
if (!std.mem.eql(u8, current_revision, revision)) {
// Reset to the desired revision
exec(allocator, &[_][]const u8{ "git", "fetch" }, dir) catch |err| std.debug.print("warning: failed to 'git fetch' in {s}: {s}\n", .{ dir, @errorName(err) });
try exec(allocator, &[_][]const u8{ "git", "checkout", "--quiet", "--force", revision }, dir);
try exec(allocator, &[_][]const u8{ "git", "submodule", "update", "--init", "--recursive" }, dir);
}
return;
} else |err| return switch (err) {
error.FileNotFound => {
std.log.info("cloning required dependency..\ngit clone {s} {s}..\n", .{ clone_url, rel_dir });
try exec(allocator, &[_][]const u8{ "git", "clone", "-c", "core.longpaths=true", clone_url, rel_dir }, xcodeSdkPath("/"));
try exec(allocator, &[_][]const u8{ "git", "checkout", "--quiet", "--force", revision }, rel_dir);
try exec(allocator, &[_][]const u8{ "git", "submodule", "update", "--init", "--recursive" }, rel_dir);
return;
},
else => err,
};
}
}
fn exec(allocator: std.mem.Allocator, argv: []const []const u8, cwd: []const u8) !void {
var child = std.ChildProcess.init(argv, allocator);
child.cwd = cwd;
_ = try child.spawnAndWait();
}
fn getCurrentGitRevision(allocator: std.mem.Allocator, cwd: []const u8) ![]const u8 {
const result = try std.ChildProcess.exec(.{ .allocator = allocator, .argv = &.{ "git", "rev-parse", "HEAD" }, .cwd = cwd });
allocator.free(result.stderr);
if (result.stdout.len > 0) return result.stdout[0 .. result.stdout.len - 1]; // trim newline
return result.stdout;
}
fn ensureGit(allocator: std.mem.Allocator) void {
const argv = &[_][]const u8{ "git", "--version" };
const result = std.ChildProcess.exec(.{
.allocator = allocator,
.argv = argv,
.cwd = ".",
}) catch { // e.g. FileNotFound
std.log.err("mach: error: 'git --version' failed. Is git not installed?", .{});
std.process.exit(1);
};
defer {
allocator.free(result.stderr);
allocator.free(result.stdout);
}
if (result.term.Exited != 0) {
std.log.err("mach: error: 'git --version' failed. Is git not installed?", .{});
std.process.exit(1);
}
}
fn isEnvVarTruthy(allocator: std.mem.Allocator, name: []const u8) bool {
if (std.process.getEnvVarOwned(allocator, name)) |truthy| {
defer allocator.free(truthy);
if (std.mem.eql(u8, truthy, "true")) return true;
return false;
} else |_| {
return false;
}
}
};

View file

@ -4,11 +4,7 @@ pub fn build(b: *std.Build) !void {
const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{});
const glfw = @import("libs/mach-glfw/build.zig");
const gpu_dawn = @import("libs/mach-gpu-dawn/build.zig").Sdk(.{
// TODO(build-system): This cannot be imported with the Zig package manager
// error: TarUnsupportedFileType
.xcode_frameworks = @import("libs/mach-gpu-dawn/libs/xcode-frameworks/build.zig"),
});
const gpu_dawn = @import("libs/mach-gpu-dawn/build.zig");
const gpu = Sdk(.{
.gpu_dawn = gpu_dawn,
});