sysaudio: rewrite in zig

removes libsoundio dependency
This commit is contained in:
Ali Chraghi 2022-12-16 14:55:46 +03:30 committed by Stephen Gutekanst
parent 8aa2c97079
commit 0f3e28bc2a
27 changed files with 4714 additions and 1344 deletions

View file

@ -2,17 +2,10 @@ const std = @import("std");
pub fn Sdk(comptime deps: anytype) type {
return struct {
const soundio_path = sdkPath("/upstream/soundio");
pub const pkg = std.build.Pkg{
.name = "sysaudio",
.source = .{ .path = sdkPath("/src/main.zig") },
.dependencies = &.{ deps.sysjs.pkg, soundio_pkg },
};
pub const soundio_pkg = std.build.Pkg{
.name = "soundio",
.source = .{ .path = sdkPath("/soundio/main.zig") },
.dependencies = &.{deps.sysjs.pkg},
};
pub const Options = struct {
@ -23,82 +16,32 @@ pub fn Sdk(comptime deps: anytype) type {
};
pub fn testStep(b: *std.build.Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) *std.build.RunStep {
const soundio_tests = b.addTestExe("soundio-tests", sdkPath("/soundio/main.zig"));
soundio_tests.setBuildMode(mode);
soundio_tests.setTarget(target);
link(b, soundio_tests, .{});
soundio_tests.install();
const main_tests = b.addTestExe("sysaudio-tests", sdkPath("/src/main.zig"));
main_tests.setBuildMode(mode);
main_tests.setTarget(target);
main_tests.addPackage(soundio_pkg);
link(b, main_tests, .{});
main_tests.install();
const main_tests_run = main_tests.run();
main_tests_run.step.dependOn(&soundio_tests.run().step);
return main_tests_run;
return main_tests.run();
}
pub fn link(b: *std.build.Builder, step: *std.build.LibExeObjStep, options: Options) void {
if (step.target.toTarget().cpu.arch != .wasm32) {
// TODO(build-system): pass system SDK options through
const soundio_lib = buildSoundIo(b, step.build_mode, step.target, options);
step.linkLibrary(soundio_lib);
step.addIncludePath(soundio_path);
deps.system_sdk.include(b, step, options.system_sdk);
deps.system_sdk.include(b, step, .{});
if (step.target.toTarget().isDarwin()) {
step.linkFramework("AudioToolbox");
step.linkFramework("CoreFoundation");
step.linkFramework("CoreAudio");
} else if (step.target.toTarget().os.tag == .linux) {
step.linkSystemLibrary("asound");
step.linkSystemLibrary("pulse");
step.linkSystemLibrary("jack");
step.linkLibC();
}
}
}
fn buildSoundIo(b: *std.build.Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget, options: Options) *std.build.LibExeObjStep {
// TODO(build-system): https://github.com/hexops/mach/issues/229#issuecomment-1100958939
ensureDependencySubmodule(b.allocator, "upstream") catch unreachable;
const config_base =
\\#ifndef SOUNDIO_CONFIG_H
\\#define SOUNDIO_CONFIG_H
\\#define SOUNDIO_VERSION_MAJOR 2
\\#define SOUNDIO_VERSION_MINOR 0
\\#define SOUNDIO_VERSION_PATCH 0
\\#define SOUNDIO_VERSION_STRING "2.0.0"
\\
;
var config_file = std.fs.cwd().createFile(soundio_path ++ "/src/config.h", .{}) catch unreachable;
defer config_file.close();
config_file.writeAll(config_base) catch unreachable;
const lib = b.addStaticLibrary("soundio", null);
lib.setBuildMode(mode);
lib.setTarget(target);
lib.linkLibC();
lib.addIncludePath(soundio_path);
lib.addCSourceFiles(soundio_sources, &.{});
deps.system_sdk.include(b, lib, options.system_sdk);
const target_info = (std.zig.system.NativeTargetInfo.detect(target) catch unreachable).target;
if (target_info.isDarwin()) {
lib.addCSourceFile(soundio_path ++ "/src/coreaudio.c", &.{});
lib.linkFramework("AudioToolbox");
lib.linkFramework("CoreFoundation");
lib.linkFramework("CoreAudio");
config_file.writeAll("#define SOUNDIO_HAVE_COREAUDIO\n") catch unreachable;
} else if (target_info.os.tag == .linux) {
lib.addCSourceFile(soundio_path ++ "/src/alsa.c", &.{});
lib.linkSystemLibrary("asound");
config_file.writeAll("#define SOUNDIO_HAVE_ALSA\n") catch unreachable;
} else if (target_info.os.tag == .windows) {
lib.addCSourceFile(soundio_path ++ "/src/wasapi.c", &.{});
lib.linkSystemLibrary("ole32");
config_file.writeAll("#define SOUNDIO_HAVE_WASAPI\n") catch unreachable;
if (options.install_libs) {
step.install();
}
config_file.writeAll("#endif\n") catch unreachable;
if (options.install_libs)
lib.install();
return lib;
}
fn ensureDependencySubmodule(allocator: std.mem.Allocator, path: []const u8) !void {
@ -121,14 +64,5 @@ pub fn Sdk(comptime deps: anytype) type {
break :blk root_dir ++ suffix;
};
}
const soundio_sources = &[_][]const u8{
soundio_path ++ "/src/soundio.c",
soundio_path ++ "/src/util.c",
soundio_path ++ "/src/os.c",
soundio_path ++ "/src/dummy.c",
soundio_path ++ "/src/channel_layout.c",
soundio_path ++ "/src/ring_buffer.c",
};
};
}