Skip to content

Commit f19a4c5

Browse files
authored
Move miniaudio and stb_vorbis dependencies to build.zig.zon (#10)
Progress towards PixelGuys/Cubyz#2346 The stb_vorbis lib is included with the official miniaudio repo, so I include it from there. Also, miniaudio internally uses stb_vorbis for decoding ogg vorbis files, but the stb_vorbis header must be defined first to enable that, so a small wrapper file is created on the fly. This is necessary if we want to be using miniaudio's high level API (without rewriting all the interfaces for a custom decoder.)
1 parent eaafd9b commit f19a4c5

7 files changed

Lines changed: 62 additions & 99077 deletions

File tree

build.zig

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,25 @@ fn addPackageCSourceFiles(exe: *std.Build.Step.Compile, dep: *std.Build.Dependen
2020
});
2121
}
2222

23+
/// Helper to run the file_replace tool
24+
fn patchFile(b: *std.Build, tool: *std.Build.Step.Compile, replacements: []const ReplacementPair, filePath: []const u8, dependency: *std.Build.Step) *std.Build.Step {
25+
var step = dependency;
26+
27+
for(replacements) |pair| {
28+
const cmd = b.addRunArtifact(tool);
29+
cmd.addArgs(&.{pair.find, pair.replace});
30+
cmd.addFileArg(b.path(filePath));
31+
cmd.step.dependOn(step);
32+
step = &cmd.step;
33+
}
34+
35+
return step;
36+
}
37+
const ReplacementPair = struct {
38+
find: []const u8,
39+
replace: []const u8,
40+
};
41+
2342
const freetypeSources = [_][]const u8{
2443
"src/autofit/autofit.c",
2544
"src/base/ftbase.c",
@@ -127,12 +146,11 @@ pub fn addVulkanApple(b: *std.Build, step: *std.Build.Step, c_lib: *std.Build.St
127146
const moltenVkJsonInstall = b.addInstallLibFile(moltenVkJsonPath, b.fmt("{s}/MoltenVK_icd.json", .{name}));
128147
step.dependOn(&moltenVkJsonInstall.step);
129148

130-
const replaceMoltenvkLibPath = b.addRunArtifact(replace_tool);
131-
replaceMoltenvkLibPath.addArgs(&.{"./libMoltenVK.dylib", "libMoltenVK.dylib"});
132-
replaceMoltenvkLibPath.addFileArg(b.path(b.fmt("zig-out/lib/{s}/MoltenVK_icd.json", .{name})));
149+
const jsonPath = b.fmt("zig-out/lib{s}/MoltenVK_icd.json", .{name});
150+
const replacements: []const ReplacementPair = &.{.{.find = "./libMoltenVK.dylib", .replace = "libMoltenVK.dylib"}};
151+
const replaceMoltenvkLibPathStep = patchFile(b, replace_tool, replacements, jsonPath, &moltenVkJsonInstall.step);
133152

134-
replaceMoltenvkLibPath.step.dependOn(&moltenVkJsonInstall.step);
135-
step.dependOn(&replaceMoltenvkLibPath.step);
153+
step.dependOn(replaceMoltenvkLibPathStep);
136154
}
137155
}
138156

@@ -421,18 +439,13 @@ pub fn makeVulkanLayers(b: *std.Build, parentStep: *std.Build.Step, name: []cons
421439
parentStep.dependOn(&libInstall.step);
422440

423441
// NOTE(blackedout): Replace the layer name and lib path placeholders in the layer manifest JSON file AFTER it has been installed
424-
const jsonPath = b.path(b.fmt("zig-out/lib/{s}/VkLayer_khronos_validation.json", .{name}));
425-
const replaceLayerName = b.addRunArtifact(replace_tool);
426-
replaceLayerName.addArgs(&.{"@JSON_LAYER_NAME@", "VK_LAYER_KHRONOS_validation"});
427-
replaceLayerName.addFileArg(jsonPath);
428-
const replaceLayerLibPath = b.addRunArtifact(replace_tool);
429-
replaceLayerLibPath.addArgs(&.{"@JSON_LIBRARY_PATH@", "libVkLayer_khronos_validation.dylib"});
430-
replaceLayerLibPath.addFileArg(jsonPath);
431-
432-
replaceLayerName.step.dependOn(&jsonInstall.step);
433-
replaceLayerLibPath.step.dependOn(&replaceLayerName.step);
434-
435-
parentStep.dependOn(&replaceLayerLibPath.step);
442+
const jsonPath = b.fmt("zig-out/lib/{s}/VkLayer_khronos_validation.json", .{name});
443+
const replacements: []const ReplacementPair = &.{
444+
.{.find = "@JSON_LAYER_NAME@", .replace = "VK_LAYER_KHRONOS_validation"},
445+
.{.find = "@JSON_LIBRARY_PATH@", .replace = "libVkLayer_khronos_validation.dylib"},
446+
};
447+
const replacementStep = patchFile(b, replace_tool, replacements, jsonPath, &jsonInstall.step);
448+
parentStep.dependOn(replacementStep);
436449
}
437450

438451
pub fn addFreetypeAndHarfbuzz(b: *std.Build, c_lib: *std.Build.Step.Compile, target: std.Build.ResolvedTarget, flags: []const []const u8) void {
@@ -513,16 +526,34 @@ pub inline fn addGLFWSources(b: *std.Build, c_lib: *std.Build.Step.Compile, targ
513526
}
514527
}
515528

529+
pub fn addMiniaudioAndStbVorbis(b: *std.Build, c_lib: *std.Build.Step.Compile, flags: []const []const u8, replace_tool: *std.Build.Step.Compile) void {
530+
const miniaudio = b.dependency("miniaudio", .{});
531+
c_lib.addIncludePath(miniaudio.path(""));
532+
533+
c_lib.installHeader(miniaudio.path("extras/stb_vorbis.c"), "stb/stb_vorbis.h");
534+
const miniaudioHeaderInstall = b.addInstallFile(miniaudio.path("extras/miniaudio_split/miniaudio.h"), "include/miniaudio.h");
535+
536+
// Patch miniaudio.h to avoid "dependency loop" issues (see: https://github.com/ziglang/zig/issues/12325)
537+
const miniaudioHeaderPath = "zig-out/include/miniaudio.h";
538+
const replacements: []const ReplacementPair = &.{
539+
.{.find = "proc)(ma_device*", .replace = "proc)(void*"},
540+
.{.find = "const ma_device_notification*", .replace = "const void*"},
541+
};
542+
const replacementStep = patchFile(b, replace_tool, replacements, miniaudioHeaderPath, &miniaudioHeaderInstall.step);
543+
c_lib.step.dependOn(replacementStep);
544+
545+
c_lib.addCSourceFile(.{.file = b.path("lib/miniaudio_stbvorbis.c"), .flags = flags});
546+
}
547+
516548
pub inline fn addHeaderOnlyLibs(b: *std.Build, c_lib: *std.Build.Step.Compile, flags: []const []const u8) void {
517549
const cgltf = b.dependency("cgltf", .{});
518550

519551
c_lib.addIncludePath(cgltf.path(""));
520552
c_lib.installHeader(cgltf.path("cgltf.h"), "cgltf.h");
521553
c_lib.installHeader(b.path("include/stb/stb_image_write.h"), "stb/stb_image_write.h");
522554
c_lib.installHeader(b.path("include/stb/stb_image.h"), "stb/stb_image.h");
523-
c_lib.installHeader(b.path("include/stb/stb_vorbis.h"), "stb/stb_vorbis.h");
524-
c_lib.installHeader(b.path("include/miniaudio.h"), "miniaudio.h");
525-
c_lib.addCSourceFiles(.{.files = &[_][]const u8{"lib/cgltf.c", "lib/stb_image.c", "lib/stb_image_write.c", "lib/stb_vorbis.c", "lib/miniaudio.c"}, .flags = flags});
555+
556+
c_lib.addCSourceFiles(.{.files = &[_][]const u8{"lib/cgltf.c", "lib/stb_image.c", "lib/stb_image_write.c"}, .flags = flags});
526557
}
527558

528559
pub inline fn makeCubyzLibs(b: *std.Build, step: *std.Build.Step, name: []const u8, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, flags: []const []const u8, replace_tool: *std.Build.Step.Compile) !*std.Build.Step.Compile {
@@ -553,6 +584,7 @@ pub inline fn makeCubyzLibs(b: *std.Build, step: *std.Build.Step, name: []const
553584

554585
addHeaderOnlyLibs(b, c_lib, flags);
555586
addFreetypeAndHarfbuzz(b, c_lib, target, flags);
587+
addMiniaudioAndStbVorbis(b, c_lib, flags, replace_tool);
556588
if(target.result.os.tag == .macos) {
557589
try addVulkanApple(b, step, c_lib, name, target, flags, replace_tool);
558590
}

build.zig.zon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,9 @@
4848
.url = "https://github.com/jkuhlmann/cgltf/archive/refs/tags/v1.15.tar.gz",
4949
.hash = "N-V-__8AAKFjBABZzn84N4iiLQyNQSMVsv5XBJSvZTPcQL2H",
5050
},
51+
.miniaudio = .{
52+
.url = "https://github.com/mackron/miniaudio/archive/refs/tags/0.11.23.tar.gz",
53+
.hash = "N-V-__8AAPu4vwC_P5AHlL_kEON58uGgrDcJSjGq9Hc5yadh",
54+
},
5155
},
5256
}

0 commit comments

Comments
 (0)