Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zig 0.11 #8

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ git submodule update --init --recursive
3. Build.

```sh
zig build -Drelease-safe
zig build -Doptimize=ReleaseSafe
```

Binary will be located at `zig-out/bin/linuxwave`. You can also run the binary directly via `zig build run`.
Expand Down
52 changes: 35 additions & 17 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,26 @@ const version = "0.1.2"; // managed by release.sh
/// Adds the required packages to the given executable.
///
/// This is used for providing the dependencies for main executable as well as the tests.
fn addPackages(allocator: std.mem.Allocator, exe: *std.build.LibExeObjStep) !void {
exe.addPackagePath("clap", "libs/zig-clap/clap.zig");
for ([_][]const u8{ "file", "gen", "wav" }) |package| {
const path = try std.fmt.allocPrint(allocator, "src/{s}.zig", .{package});
defer allocator.free(path);
exe.addPackagePath(package, path);
}
fn addPackages(b: *std.Build, exe: *std.build.LibExeObjStep) !void {
orhun marked this conversation as resolved.
Show resolved Hide resolved
exe.addModule("clap", b.createModule(.{
.source_file = .{ .path = "libs/zig-clap/clap.zig" },
.dependencies = &.{},
}));
exe.addModule("file", b.createModule(.{
.source_file = .{ .path = "src/file.zig" },
.dependencies = &.{},
}));
exe.addModule("gen", b.createModule(.{
.source_file = .{ .path = "src/gen.zig" },
.dependencies = &.{},
}));
exe.addModule("wav", b.createModule(.{
.source_file = .{ .path = "src/wav.zig" },
.dependencies = &.{},
}));
}

pub fn build(b: *std.build.Builder) !void {
pub fn build(b: *std.Build) !void {
// Create an allocator.
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
orhun marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -31,7 +41,7 @@ pub fn build(b: *std.build.Builder) !void {

// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();
const optimize = b.standardOptimizeOption(.{});

// Add custom options.
const pie = b.option(bool, "pie", "Build a Position Independent Executable") orelse true;
Expand All @@ -40,9 +50,12 @@ pub fn build(b: *std.build.Builder) !void {
const documentation = b.option(bool, "docs", "Generate documentation") orelse false;

// Add main executable.
const exe = b.addExecutable(exe_name, "src/main.zig");
exe.setTarget(target);
exe.setBuildMode(mode);
const exe = b.addExecutable(.{
.name = exe_name,
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
if (documentation) {
exe.emit_docs = .emit;
}
Expand All @@ -51,7 +64,7 @@ pub fn build(b: *std.build.Builder) !void {
exe.install();
orhun marked this conversation as resolved.
Show resolved Hide resolved

// Add packages.
try addPackages(allocator, exe);
try addPackages(b, exe);

// Add executable options.
const exe_options = b.addOptions();
Expand All @@ -73,19 +86,24 @@ pub fn build(b: *std.build.Builder) !void {
// Add tests.
const test_step = b.step("test", "Run tests");
for ([_][]const u8{ "main", "wav", "file", "gen" }) |module| {
const test_name = try std.fmt.allocPrint(allocator, "{s}-tests", .{module});
defer allocator.free(test_name);
const test_module = try std.fmt.allocPrint(allocator, "src/{s}.zig", .{module});
orhun marked this conversation as resolved.
Show resolved Hide resolved
defer allocator.free(test_module);
var exe_tests = b.addTest(test_module);
var exe_tests = b.addTest(.{
.name = test_name,
.root_source_file = .{ .path = test_module },
.target = target,
.optimize = optimize,
});
if (coverage) {
exe_tests.setExecCmd(&[_]?[]const u8{
"kcov",
"kcov-output",
null,
});
}
exe_tests.setTarget(target);
exe_tests.setBuildMode(mode);
try addPackages(allocator, exe_tests);
try addPackages(b, exe_tests);
exe_tests.addOptions("build_options", exe_options);
test_step.dependOn(&exe_tests.step);
}
Expand Down
10 changes: 5 additions & 5 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ fn run(allocator: std.mem.Allocator, output: anytype) !void {
// Parse command-line arguments.
const cli = try clap.parse(clap.Help, &args.params, args.parsers, .{});
defer cli.deinit();
if (cli.args.help) {
if (cli.args.help != 0) {
try output.print("{s}\n", .{args.banner});
return clap.help(output, clap.Help, &args.params, args.help_options);
} else if (cli.args.version) {
} else if (cli.args.version != 0) {
try output.print("{s} {s}\n", .{ build_options.exe_name, build_options.version });
return;
}
Expand All @@ -34,7 +34,7 @@ fn run(allocator: std.mem.Allocator, output: anytype) !void {
while (splits.next()) |chunk| {
try scale.append(try std.fmt.parseInt(u8, chunk, 0));
}
break :s scale.toOwnedSlice();
break :s try scale.toOwnedSlice();
};
defer allocator.free(scale);
const generator_config = gen.GeneratorConfig{
Expand Down Expand Up @@ -83,7 +83,7 @@ fn run(allocator: std.mem.Allocator, output: anytype) !void {
break :w out_file.writer();
}
};
const wav_data = data.toOwnedSlice();
const wav_data = try data.toOwnedSlice();
defer allocator.free(wav_data);
try wav.Encoder(@TypeOf(writer)).encode(writer, wav_data, encoder_config);
}
Expand All @@ -103,7 +103,7 @@ test "run" {
var buffer = std.ArrayList(u8).init(allocator);
const output = buffer.writer();
try run(allocator, output);
const result = buffer.toOwnedSlice();
const result = try buffer.toOwnedSlice();
defer allocator.free(result);
try std.testing.expectEqualStrings(
\\Reading 96 bytes from /dev/urandom
Expand Down