168 lines
5.6 KiB
Zig
168 lines
5.6 KiB
Zig
const std = @import("std");
|
|
|
|
pub fn build(b: *std.Build) !void {
|
|
const target = b.standardTargetOptions(.{});
|
|
const optimize = b.standardOptimizeOption(.{});
|
|
|
|
const use_udev = b.option(
|
|
bool,
|
|
"use_udev",
|
|
"link and use udev (Linux only. Default: false)",
|
|
) orelse false;
|
|
|
|
const enable_logging = b.option(
|
|
bool,
|
|
"enable_logging",
|
|
"enable libusb's built-in logging (Default: false)",
|
|
) orelse false;
|
|
|
|
const enable_debug_logging = b.option(
|
|
bool,
|
|
"enable_debug_logging",
|
|
"enable libusb's debug logging (Default: false)",
|
|
) orelse false;
|
|
|
|
const libusb = b.addStaticLibrary(.{
|
|
.name = "usb",
|
|
.target = target,
|
|
.optimize = optimize,
|
|
.link_libc = true,
|
|
});
|
|
|
|
b.installArtifact(libusb);
|
|
|
|
libusb.addCSourceFiles(.{ .files = main_sources });
|
|
libusb.addIncludePath(b.path("libusb"));
|
|
libusb.installHeader(b.path("libusb/libusb.h"), "libusb-1.0/libusb.h");
|
|
|
|
switch (target.result.os.tag) {
|
|
.linux => {
|
|
libusb.addCSourceFiles(.{ .files = posix_sources });
|
|
libusb.addCSourceFiles(.{ .files = linux_sources });
|
|
if (use_udev) {
|
|
libusb.addCSourceFiles(.{ .files = linux_udev });
|
|
libusb.linkSystemLibrary("libudev");
|
|
} else {
|
|
libusb.addCSourceFiles(.{ .files = linux_netlink });
|
|
}
|
|
},
|
|
.windows => {
|
|
libusb.addCSourceFiles(.{ .files = windows_sources });
|
|
},
|
|
else => {
|
|
if (target.result.isDarwin()) {
|
|
libusb.addCSourceFiles(.{ .files = posix_sources });
|
|
libusb.addCSourceFiles(.{ .files = macos_sources });
|
|
libusb.linkFramework("CoreFoundation");
|
|
libusb.linkFramework("IOKit");
|
|
libusb.linkFramework("Security");
|
|
} else {
|
|
std.debug.print("Unsupported target OS {s}\n", .{@tagName(target.result.os.tag)});
|
|
return error.Unsupported;
|
|
}
|
|
},
|
|
}
|
|
|
|
const linux_target = target.result.os.tag == .linux;
|
|
const win_target = target.result.os.tag == .windows;
|
|
const mac_target = target.result.isDarwin();
|
|
|
|
const config_h = b.addConfigHeader(
|
|
.{ .style = .{ .autoconf = b.path("config.h.in") } },
|
|
.{
|
|
.DEFAULT_VISIBILITY = .@"__attribute__ ((visibility (\"default\")))",
|
|
.ENABLE_DEBUG_LOGGING = oneOrNull(enable_debug_logging),
|
|
.ENABLE_LOGGING = oneOrNull(enable_logging),
|
|
.HAVE_ASM_TYPES_H = null,
|
|
.HAVE_CLOCK_GETTIME = oneOrNull(linux_target),
|
|
.HAVE_DECL_EFD_CLOEXEC = oneOrNull(linux_target),
|
|
.HAVE_DECL_EFD_NONBLOCK = oneOrNull(linux_target),
|
|
.HAVE_DECL_TFD_CLOEXEC = oneOrNull(linux_target),
|
|
.HAVE_DECL_TFD_NONBLOCK = oneOrNull(linux_target),
|
|
.HAVE_DLFCN_H = oneOrNull(linux_target or mac_target),
|
|
.HAVE_EVENTFD = oneOrNull(linux_target),
|
|
.HAVE_INTTYPES_H = oneOrNull(linux_target or mac_target),
|
|
.HAVE_IOKIT_USB_IOUSBHOSTFAMILYDEFINITIONS_H = oneOrNull(mac_target),
|
|
.HAVE_LIBUDEV = oneOrNull(linux_target and use_udev),
|
|
.HAVE_NFDS_T = oneOrNull(linux_target or mac_target),
|
|
.HAVE_PIPE2 = oneOrNull(linux_target),
|
|
.HAVE_PTHREAD_CONDATTR_SETCLOCK = oneOrNull(linux_target),
|
|
.HAVE_PTHREAD_SETNAME_NP = oneOrNull(linux_target),
|
|
.HAVE_PTHREAD_THREADID_NP = oneOrNull(mac_target),
|
|
.HAVE_STDINT_H = 1,
|
|
.HAVE_STDIO_H = 1,
|
|
.HAVE_STDLIB_H = 1,
|
|
.HAVE_STRINGS_H = 1,
|
|
.HAVE_STRING_H = 1,
|
|
.HAVE_STRUCT_TIMESPEC = null,
|
|
.HAVE_SYSLOG = null,
|
|
.HAVE_SYS_STAT_H = 1,
|
|
.HAVE_SYS_TIME_H = 1,
|
|
.HAVE_SYS_TYPES_H = 1,
|
|
.HAVE_TIMERFD = oneOrNull(linux_target),
|
|
.HAVE_UNISTD_H = 1,
|
|
.LT_OBJDIR = null,
|
|
.PACKAGE = "libusb-1.0",
|
|
.PACKAGE_BUGREPORT = "libusb-devel@lists.sourceforge.net",
|
|
.PACKAGE_NAME = "libusb-1.0",
|
|
.PACKAGE_STRING = "libusb-1.0 1.0.27",
|
|
.PACKAGE_TARNAME = "libusb-1.0",
|
|
.PACKAGE_URL = "https://libusb.info",
|
|
.PACKAGE_VERSION = "1.0.27",
|
|
.PLATFORM_POSIX = oneOrNull(linux_target or mac_target),
|
|
.PLATFORM_WINDOWS = oneOrNull(win_target),
|
|
.STDC_HEADERS = 1,
|
|
.UMOCKDEV_HOTPLUG = null,
|
|
.USE_SYSTEM_LOGGING_FACILITY = null,
|
|
.VERSION = "1.0.27",
|
|
._GNU_SOURCE = 1,
|
|
._WIN32_WINNT = null,
|
|
.@"inline" = null, // or __attribute__((always_inline))
|
|
},
|
|
);
|
|
|
|
libusb.addConfigHeader(config_h);
|
|
}
|
|
|
|
fn oneOrNull(exp: bool) ?u1 {
|
|
return if (exp) 1 else null;
|
|
}
|
|
|
|
const main_sources: []const []const u8 = &.{
|
|
"libusb/core.c",
|
|
"libusb/descriptor.c",
|
|
"libusb/hotplug.c",
|
|
"libusb/io.c",
|
|
"libusb/strerror.c",
|
|
"libusb/sync.c",
|
|
};
|
|
|
|
const posix_sources: []const []const u8 = &.{
|
|
"libusb/os/events_posix.c",
|
|
"libusb/os/threads_posix.c",
|
|
};
|
|
|
|
const linux_sources: []const []const u8 = &.{
|
|
"libusb/os/linux_usbfs.c",
|
|
};
|
|
|
|
const linux_netlink: []const []const u8 = &.{
|
|
"libusb/os/linux_netlink.c",
|
|
};
|
|
|
|
const linux_udev: []const []const u8 = &.{
|
|
"libusb/os/linux_udev.c",
|
|
};
|
|
|
|
const macos_sources: []const []const u8 = &.{
|
|
"libusb/os/darwin_usb.c",
|
|
};
|
|
|
|
const windows_sources: []const []const u8 = &.{
|
|
"libusb/os/windows_common.c",
|
|
"libusb/os/windows_usbdk.c",
|
|
"libusb/os/windows_winusb.c",
|
|
"libusb/os/threads_windows.c",
|
|
"libusb/os/events_windows.c",
|
|
};
|