Skip to content

Commit

Permalink
Merge pull request #9 from kassane/rework
Browse files Browse the repository at this point in the history
Update to Zig 0.11
  • Loading branch information
kassane committed Mar 24, 2023
2 parents 7eb3b36 + 41ff58c commit 67f7e1d
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 65 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/Darwin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ jobs:
with:
submodules: recursive
fetch-depth: 0
- uses: goto-bus-stop/setup-zig@v2
with:
version: master

- name: Install deps
env:
Expand All @@ -26,11 +29,8 @@ jobs:
sudo ln -s /Applications/VLC.app/Contents/MacOS/include/vlc /usr/local/include/ && \
sudo ln -s /Applications/VLC.app/Contents/MacOS/lib/* /usr/local/lib/
- name: Install zig
uses: goto-bus-stop/setup-zig@v2
with:
version: 0.10.0

- name: Build & Run
run: |
zig build run -freference-trace
- name: Build
run: zig build -Doptimize=ReleaseSafe -fsummary -freference-trace

- name: run print_version (example)
run: zig build run -Doptimize=ReleaseSafe
11 changes: 4 additions & 7 deletions .github/workflows/Linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,18 @@ jobs:
with:
submodules: recursive
fetch-depth: 0

- name: Install Zig
uses: goto-bus-stop/setup-zig@v2
- uses: goto-bus-stop/setup-zig@v2
with:
version: 0.10.0
version: master

- name: Install dependences
run: |
sudo apt update
sudo apt install libvlc-dev libsdl2-dev -y
- name: build
run: |
zig build -freference-trace
run: zig build -Doptimize=ReleaseSafe -fsummary -freference-trace

- name: run print_version (example)
run: |
zig build run -freference-trace
zig build run -Doptimize=ReleaseSafe
60 changes: 60 additions & 0 deletions .github/workflows/MinGW.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: MSYS2 (MingW)

on:
push:
paths:
- '**.zig'
schedule:
- cron: '0 0 * * *'
workflow_dispatch:

jobs:
build:
runs-on: windows-latest
strategy:
fail-fast: false
matrix:
include: [
{ msystem: CLANG64, arch: x86_64, prefix: /clang64 },
{ msystem: CLANG32, arch: i686, prefix: /clang32 }
]

steps:
- uses: actions/checkout@v3
with:
path: temp
submodules: recursive
fetch-depth: 0
- uses: goto-bus-stop/setup-zig@v2
with:
version: master
- uses: msys2/setup-msys2@v2
with:
msystem: ${{ matrix.msystem }}
path-type: inherit
location: D:\
install: git mingw-w64-clang-${{ matrix.arch }}-vlc
update: true

# - name: install windows sdk
# run: choco install windows-sdk-10.0

- name: Move Checkout
run: |
Copy-Item -Path ".\temp" -Destination "C:\_" -Recurse
- name: Build & Tests (x86)
shell: msys2 {0}
if: ${{ matrix.arch == 'i686' }}
run: |
cd /C/_
zig build -fsummary -freference-trace -Doptimize=ReleaseSafe -Dtarget=x86-windows-gnu
zig build run -Doptimize=ReleaseSafe -Dtarget=x86-windows-gnu
- name: Build & Tests (x86_x64)
shell: msys2 {0}
if: ${{ matrix.arch == 'x86_64' }}
run: |
cd /C/_
zig build -fsummary -freference-trace -Doptimize=ReleaseSafe -Dtarget=native-windows-gnu
zig build run -Doptimize=ReleaseSafe -Dtarget=native-windows-gnu
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<a href="https://github.com/kassane/libvlc-zig/actions/workflows/Darwin.yml">
<img alt="Build MacOS status" src="https://github.com/kassane/libvlc-zig/actions/workflows/Darwin.yml/badge.svg">
</a>
<a href="https://github.com/kassane/libvlc-zig/actions/workflows/MinGW.yml">
<img alt="Build MinGW status" src="https://github.com/kassane/libvlc-zig/actions/workflows/Darwin.yml/badge.svg">
</a>
<a href="https://opensource.org/licenses/BSD-2-Clause" rel="nofollow">
<img alt="BSD 2 Clause license" src="https://img.shields.io/github/license/kassane/libvlc-zig"/>
</a>
Expand Down
123 changes: 80 additions & 43 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,49 +1,66 @@
const std = @import("std");

const Options = struct {
sdl_enabled: bool,
// capy_enabled: bool, //TODO
};

pub fn build(b: *std.build.Builder) void {
b.prominent_compile_errors = true;
const mode = b.standardReleaseOptions();
pub fn build(b: *std.Build) void {
if (comptime !checkVersion())
@compileError("Please! Update zig toolchain >= 0.11!");
const target = b.standardTargetOptions(.{});

var op = Options{ .sdl_enabled = false };

testLib(b, mode);
const optimize = b.standardOptimizeOption(.{});

const examples = b.option([]const u8, "Example", "Build example: [print-version, cli-player, sdl2-player]") orelse "print-version";
if (std.mem.eql(u8, examples, "print-version"))
make_example(b, mode, target, "print_version", "examples/print_version.zig", op);
make_example(b, .{
.sdl_enabled = false,
.mode = optimize,
.target = target,
.name = "print_version",
.path = "examples/print_version.zig",
});

if (std.mem.eql(u8, examples, "cli-player"))
make_example(b, mode, target, "cli-player", "examples/cli_player.zig", op);
make_example(b, .{
.sdl_enabled = false,
.mode = optimize,
.target = target,
.name = "cli-player",
.path = "examples/cli_player.zig",
});

if (std.mem.eql(u8, examples, "sdl-player")) {
op.sdl_enabled = true;
make_example(b, mode, target, "sdl-player", "examples/sdl_player.zig", op);
make_example(b, .{
.sdl_enabled = true,
.mode = optimize,
.target = target,
.name = "sdl-player",
.path = "examples/sdl_player.zig",
});
}
}

fn make_example(b: *std.build.Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget, name: []const u8, path: []const u8, option: Options) void {
var bf: [100]u8 = undefined;

const example = b.addExecutable(name, path);
example.setBuildMode(mode);
example.setTarget(target);
example.addPackagePath("vlc", "src/vlc.zig");
if (option.sdl_enabled) {
// import SDL bindings
const sdl = @import("vendor/SDL2-zig/Sdk.zig");

const sdk = sdl.init(b);
example.addPackage(sdk.getNativePackage("sdl2"));
sdk.link(example, .dynamic);
fn make_example(b: *std.Build, info: BuildInfo) void {
const example = b.addExecutable(.{
.name = info.name,
.target = info.target,
.optimize = info.mode,
.root_source_file = .{ .path = info.path },
});

example.addAnonymousModule("vlc", .{
.source_file = .{
.path = "src/vlc.zig",
},
});

if (info.sdl_enabled) {
const libsdl_dep = b.dependency("libsdl", .{
.target = info.target,
.optimize = info.mode,
});
const libsdl = libsdl_dep.artifact("sdl");
example.linkLibrary(libsdl);
example.installLibraryHeaders(libsdl);
}

if (target.isDarwin()) {
if (info.target.isDarwin()) {
// Custom path
example.addIncludePath("/usr/local/include");
example.addLibraryPath("/usr/local/lib");
Expand All @@ -54,12 +71,21 @@ fn make_example(b: *std.build.Builder, mode: std.builtin.Mode, target: std.zig.C
// example.linkFramework("Sparkle");
// Link library
example.linkSystemLibrary("vlc");
} else if (target.isWindows()) {
example.linkSystemLibraryName("vlc");
} else if (info.target.isWindows()) {
// msys2/clang - CI
example.addIncludePath(switch (info.target.getCpuArch()) {
.x86_64 => "D:/msys64/clang64/include",
else => "D:/msys64/clang32/include",
});
example.addLibraryPath(switch (info.target.getCpuArch()) {
.x86_64 => "D:/msys64/clang64/lib",
else => "D:/msys64/clang32/lib",
});
example.linkSystemLibraryName("vlc.dll");
example.want_lto = false;
} else {
example.linkSystemLibrary("vlc");
}

example.linkLibC();
example.install();

Expand All @@ -69,17 +95,28 @@ fn make_example(b: *std.build.Builder, mode: std.builtin.Mode, target: std.zig.C
run_cmd.addArgs(args);
}

var descr = std.fmt.bufPrintZ(&bf, "Run the {s} example", .{name}) catch unreachable;
var descr = b.fmt("Run the {s} example", .{info.name});
const run_step = b.step("run", descr);
run_step.dependOn(&run_cmd.step);
}

fn testLib(b: *std.build.Builder, mode: std.builtin.Mode) void {
const tests = b.addTest("src/vlc.zig");
tests.setBuildMode(mode);
tests.linkSystemLibrary("vlc");
tests.linkLibC();
fn checkVersion() bool {
const builtin = @import("builtin");
if (!@hasDecl(builtin, "zig_version")) {
return false;
}

const needed_version = std.SemanticVersion.parse("0.11.0-dev.2191") catch unreachable;
const version = builtin.zig_version;
const order = version.order(needed_version);
return order != .lt;
}

const test_step = b.step("test", "Run library tests");
test_step.dependOn(&tests.step);
}
const BuildInfo = struct {
sdl_enabled: bool,
// capy_enabled: bool, //TODO
mode: std.builtin.Mode,
target: std.zig.CrossTarget,
name: []const u8,
path: []const u8,
};
10 changes: 10 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.{
.name = "libvlc",
.version = "0.1.0",
.dependencies = .{
.libsdl = .{
.url = "https://github.com/andrewrk/sdl/archive/73c38318443a98b80d7cd0cd1e18a190a85e842f.tar.gz",
.hash = "12202047e83332d37477811513bd6446c567efa6f03c4ae5e57585412b4ad8129cae",
},
},
}
35 changes: 35 additions & 0 deletions src/nanosleep.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const std = @import("std");
const builtin = @import("builtin");
const math = std.math;
const system = std.os.system;
const timespec = system.timespec;
const errno = system.getErrno;

const size = switch (builtin.os.tag) {
.windows => c_long,
else => isize,
};

pub export fn nanosleep(seconds: u64, nanoseconds: u64) void {
var req = timespec{
.tv_sec = math.cast(isize, seconds) orelse math.maxInt(isize),
.tv_nsec = math.cast(size, nanoseconds) orelse math.maxInt(size),
};
var rem: timespec = undefined;
while (true) {
switch (errno(system.nanosleep(&req, &rem))) {
.FAULT => unreachable,
.INVAL => {
// Sometimes Darwin returns EINVAL for no reason.
// We treat it as a spurious wakeup.
return;
},
.INTR => {
req = rem;
continue;
},
// This prong handles success as well as unexpected errors.
else => return,
}
}
}
6 changes: 3 additions & 3 deletions src/vlc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//! and/or other materials provided with the distribution.

const std = @import("std");
const builtin = @import("builtin");
const nanosleep = @import("nanosleep.zig").nanosleep;

const c = @cImport({
@cInclude("vlc/vlc.h");
Expand Down Expand Up @@ -57,7 +57,7 @@ pub const VideoEngine_t = c.libvlc_video_engine_t;
pub const vlc_log = std.log.scoped(.vlc);

pub fn sleep(time: usize) void {
std.os.nanosleep(time, time * std.time.ns_per_ms);
nanosleep(time, time * std.time.ns_per_ms);
}
pub fn getError() [*:0]const u8 {
return c.libvlc_errmsg();
Expand Down Expand Up @@ -575,4 +575,4 @@ pub const VideoMultiview_t = enum(c_int) {
test "ref all decls" {
const testing = std.testing;
testing.refAllDeclsRecursive(@This());
}
}
1 change: 0 additions & 1 deletion vendor/SDL2-zig
Submodule SDL2-zig deleted from 2fbd4b

0 comments on commit 67f7e1d

Please sign in to comment.