alsa-lib ported to zig-build

This commit is contained in:
Emil Lerch 2025-09-09 17:01:24 -07:00
parent 165abc7fbe
commit e91610bc54
Signed by: lobo
GPG key ID: A7B62D657EF764F8
6 changed files with 224 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
.zig-cache/
zig-out/

11
.mise.toml Normal file
View file

@ -0,0 +1,11 @@
[tools]
zig = "0.15.1"
zls = "0.15.0"
pre-commit = "latest"
"ubi:DonIsaac/zlint" = "latest"
[hooks]
enter = 'echo use "nix develop" if you want to build'
[settings]
experimental = true

21
LICENSE Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2025 Emil Lerch
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

0
README.md Normal file
View file

172
build.zig Normal file
View file

@ -0,0 +1,172 @@
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const upstream = b.dependency("alsa-lib", .{});
// Create config header matching autotools output
const config = b.addConfigHeader(.{
.style = .blank,
.include_path = "config.h",
}, .{
.PACKAGE_NAME = "alsa-lib",
.VERSION = "1.2.14",
.HAVE_USELOCALE = 1,
.HAVE_EACCESS = 1,
.HAVE_ENDIAN_H = 1,
.HAVE_SYS_SHM_H = 1,
.HAVE_MALLOC_H = 1,
.HAVE_LFS = 1,
.HAVE_LIBDL = 1,
.PIC = 1,
.BUILD_PCM = "1",
.BUILD_MIXER = "1",
.BUILD_HWDEP = "1",
.ALSA_DEVICE_DIRECTORY = "/dev/snd/",
.ALSA_CONFIG_DIR = "/usr/share/alsa",
.ALSA_PLUGIN_DIR = "/usr/lib/alsa-lib",
.SND_MAX_CARDS = 32,
.__SYMBOL_PREFIX = "",
._GNU_SOURCE = 1,
.HAVE_GNU_LD = 1,
.HAVE_ELF = 1,
.HAVE_ASM_PREVIOUS_DIRECTIVE = 1,
});
// Create version.h
const version_h = b.addWriteFiles();
_ = version_h.add("version.h",
\\/*
\\ * version.h
\\ */
\\
\\#define SND_LIB_MAJOR 1 /**< major number of library version */
\\#define SND_LIB_MINOR 2 /**< minor number of library version */
\\#define SND_LIB_SUBMINOR 14 /**< subminor number of library version */
\\#define SND_LIB_EXTRAVER 1000000 /**< extra version number, used mainly for betas */
\\/** library version */
\\#define SND_LIB_VER(maj, min, sub) (((maj)<<16)|((min)<<8)|(sub))
\\#define SND_LIB_VERSION SND_LIB_VER(SND_LIB_MAJOR, SND_LIB_MINOR, SND_LIB_SUBMINOR)
\\/** library version (string) */
\\#define SND_LIB_VERSION_STR "1.2.14"
\\
);
// Generate asoundlib.h
const asoundlib_h = b.addWriteFiles();
_ = asoundlib_h.add("asoundlib.h",
\\/*
\\ * ALSA library - main header file
\\ */
\\
\\#ifndef __ALSA_ASOUNDLIB_H
\\#define __ALSA_ASOUNDLIB_H
\\
\\#include <stdio.h>
\\#include <stdlib.h>
\\#include <string.h>
\\#include <fcntl.h>
\\#include <assert.h>
\\#include <endian.h>
\\#include <sys/poll.h>
\\#include <errno.h>
\\#include <stdint.h>
\\#include <sys/types.h>
\\#include <unistd.h>
\\
\\#include <alsa/asoundef.h>
\\#include <alsa/version.h>
\\#include <alsa/global.h>
\\#include <alsa/input.h>
\\#include <alsa/output.h>
\\#include <alsa/error.h>
\\#include <alsa/conf.h>
\\#include <alsa/control.h>
\\#include <alsa/pcm.h>
\\
\\#endif /* __ALSA_ASOUNDLIB_H */
\\
);
const lib = b.addLibrary(.{
.name = "asound",
.linkage = .static,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
}),
});
lib.addConfigHeader(config);
lib.addIncludePath(upstream.path("include"));
lib.addIncludePath(version_h.getDirectory());
lib.addIncludePath(asoundlib_h.getDirectory());
// Create alsa as symlink for the include directory. Not sure why upstream
// does this, but since we're not using their scripts, we need to do that
// as well
const create_symlink = b.addSystemCommand(&.{ "sh", "-c", "rm -f alsa; ln -sf . alsa" });
create_symlink.setCwd(upstream.path("include"));
lib.step.dependOn(&create_symlink.step);
lib.addCSourceFiles(.{
.root = upstream.path(""),
.files = &.{
"src/conf.c",
"src/confeval.c",
"src/confmisc.c",
"src/input.c",
"src/output.c",
"src/async.c",
"src/error.c",
"src/dlmisc.c",
"src/socket.c",
"src/shmarea.c",
"src/userfile.c",
"src/names.c",
"src/control/control.c",
"src/control/control_hw.c",
"src/control/cards.c",
"src/control/namehint.c",
"src/control/hcontrol.c",
"src/control/tlv.c",
"src/control/setup.c",
"src/control/ctlparse.c",
"src/control/eld.c",
"src/pcm/pcm.c",
"src/pcm/pcm_hw.c",
"src/pcm/pcm_params.c",
"src/pcm/pcm_misc.c",
"src/pcm/pcm_mmap.c",
"src/pcm/mask.c",
"src/pcm/interval.c",
},
.flags = &.{
"-D_GNU_SOURCE",
"-DALSA_LIBRARY_BUILD",
},
});
lib.installHeadersDirectory(upstream.path("include"), "alsa", .{
.include_extensions = &.{
"asoundef.h",
"version.h",
"global.h",
"input.h",
"output.h",
"error.h",
"conf.h",
"control.h",
"pcm.h",
},
});
// Install generated asoundlib.h and version.h
lib.installHeader(asoundlib_h.getDirectory().path(b, "asoundlib.h"), "alsa/asoundlib.h");
lib.installHeader(version_h.getDirectory().path(b, "version.h"), "alsa/version.h");
b.installArtifact(lib);
}

18
build.zig.zon Normal file
View file

@ -0,0 +1,18 @@
.{
.name = .alsa,
.version = "1.2.14",
.minimum_zig_version = "0.15.1",
.fingerprint = 0xddb406c516c1eb8d,
.dependencies = .{
.@"alsa-lib" = .{
.url = "git+https://github.com/alsa-project/alsa-lib?ref=v1.2.14#81d70a27136840fa8fb1bec6eb8e04fc1475a380",
.hash = "N-V-__8AACPMQgBfqlZq9Kx2qUwCMrZVkl-5dDd5bVDiF06E",
},
},
.paths = .{
"LICENSE",
"README.md",
"build.zig",
"build.zig.zon",
},
}