diff --git a/.github/bors.toml b/.github/bors.toml index 8528af8..136f4f2 100644 --- a/.github/bors.toml +++ b/.github/bors.toml @@ -1,8 +1,8 @@ status = [ "Build with args: ''", - "Build with args: '-Drelease-safe'", - "Build with args: '-Drelease-fast'", - "Build with args: '-Drelease-small'", + "Build with args: '-Doptimize=ReleaseSafe'", + "Build with args: '-Doptimize=ReleaseFast'", + "Build with args: '-Doptimize=ReleaseSmall'", "Test", "Check formatting", ] diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 7b5c86a..e6d61e3 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -56,7 +56,7 @@ jobs: - name: Install Zig uses: goto-bus-stop/setup-zig@v2 with: - version: 0.10.1 + version: 0.11.0 - name: Show Zig version run: | diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f2aee1d..dc9e0e4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,9 +20,9 @@ jobs: OPTIMIZE: [ "", - "-Drelease-safe", - "-Drelease-fast", - "-Drelease-small" + "-Doptimize=ReleaseSafe", + "-Doptimize=ReleaseFast", + "-Doptimize=ReleaseSmall", ] steps: - name: Checkout the repository @@ -33,7 +33,7 @@ jobs: - name: Install Zig uses: goto-bus-stop/setup-zig@v2 with: - version: 0.10.1 + version: 0.11.0 - name: Show Zig version run: | @@ -58,7 +58,7 @@ jobs: - name: Install Zig uses: goto-bus-stop/setup-zig@v2 with: - version: 0.10.1 + version: 0.11.0 - name: Install kcov run: | @@ -69,7 +69,7 @@ jobs: kcov - name: Test - run: zig build test -Dtest-coverage + run: zig build --summary all test -Dtest-coverage - name: Upload coverage to Codecov uses: codecov/codecov-action@v3 @@ -92,7 +92,7 @@ jobs: - name: Install Zig uses: goto-bus-stop/setup-zig@v2 with: - version: 0.10.1 + version: 0.11.0 - name: Check formatting run: zig fmt --check . diff --git a/.github/workflows/pages.yml b/.github/workflows/pages.yml index 3f8d28c..4ced1c2 100644 --- a/.github/workflows/pages.yml +++ b/.github/workflows/pages.yml @@ -32,7 +32,7 @@ jobs: - name: Install Zig uses: goto-bus-stop/setup-zig@v2 with: - version: 0.10.1 + version: 0.11.0 - name: Show Zig version run: | diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d1a3f3d..fc6ac22 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -75,7 +75,7 @@ zig build run 6. Add your tests or update the existing tests according to the changes and check if the tests are passed. ```sh -zig build test +zig build --summary all test ``` 8. Push changes to your fork. diff --git a/Dockerfile b/Dockerfile index e2c735f..cee292f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,10 @@ -FROM euantorano/zig:0.10.1 as builder +FROM euantorano/zig:0.11.0 as builder RUN apk update RUN apk add --no-cache git WORKDIR /app COPY . . RUN git submodule update --init --recursive && \ - zig build -Drelease-safe + zig build -Doptimize=ReleaseSafe FROM alpine:3.8 WORKDIR /app diff --git a/README.md b/README.md index 3b9c808..c07dbf4 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ #### Prerequisites -- [Zig](https://ziglang.org/download/) (`0.10.1`) +- [Zig](https://ziglang.org/download/) (`0.11.0`) #### Instructions @@ -85,7 +85,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`. diff --git a/build.zig b/build.zig index dbaa2e5..774b682 100644 --- a/build.zig +++ b/build.zig @@ -9,16 +9,26 @@ const version = "0.1.5"; // 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.CompileStep) !void { + 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 { // Standard target options allows the person running `zig build` to choose // what target to build for. Here we do not override the defaults, which // means any target is allowed, and the default is native. Other options @@ -27,7 +37,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; @@ -36,18 +46,26 @@ 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; + const install_docs = b.addInstallDirectory(.{ + .source_dir = exe.getEmittedDocs(), + .install_dir = .prefix, + .install_subdir = "docs", + }); + b.getInstallStep().dependOn(&install_docs.step); } exe.pie = pie; exe.link_z_relro = relro; - exe.install(); + b.installArtifact(exe); // Add packages. - try addPackages(b.allocator, exe); + try addPackages(b, exe); // Add executable options. const exe_options = b.addOptions(); @@ -56,7 +74,7 @@ pub fn build(b: *std.build.Builder) !void { exe_options.addOption([]const u8, "exe_name", exe_name); // Create the run step and add arguments. - const run_cmd = exe.run(); + const run_cmd = b.addRunArtifact(exe); run_cmd.step.dependOn(b.getInstallStep()); if (b.args) |args| { run_cmd.addArgs(args); @@ -68,9 +86,16 @@ 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 = b.fmt("{s}-tests", .{module}); const test_module = b.fmt("src/{s}.zig", .{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", @@ -78,10 +103,9 @@ pub fn build(b: *std.build.Builder) !void { null, }); } - exe_tests.setTarget(target); - exe_tests.setBuildMode(mode); - try addPackages(b.allocator, exe_tests); + try addPackages(b, exe_tests); exe_tests.addOptions("build_options", exe_options); - test_step.dependOn(&exe_tests.step); + const run_unit_tests = b.addRunArtifact(exe_tests); + test_step.dependOn(&run_unit_tests.step); } } diff --git a/libs/zig-clap b/libs/zig-clap index 749c43f..f49b947 160000 --- a/libs/zig-clap +++ b/libs/zig-clap @@ -1 +1 @@ -Subproject commit 749c43f1f846adc950a5920ed61b40cbc3ec2c54 +Subproject commit f49b94700e0761b7514abdca0e4f0e7f3f938a93 diff --git a/release.sh b/release.sh index 191770c..4b617b7 100755 --- a/release.sh +++ b/release.sh @@ -12,7 +12,7 @@ echo "Preparing $1..." # update the version msg="\/\/ managed by release.sh" sed -E -i "s/^(const version = ).* $msg$/\1\"${1#v}\"; $msg/" build.zig -zig build test +zig build --summary all test # update the changelog git cliff --tag "$1" >CHANGELOG.md git add -A diff --git a/src/gen.zig b/src/gen.zig index f593146..9c9da0e 100644 --- a/src/gen.zig +++ b/src/gen.zig @@ -38,13 +38,13 @@ pub const Generator = struct { // Hertz = 440 * 2^(semitone distance / 12) // () var amp = @sin(self.config.note * std.math.pi * - std.math.pow(f32, 2, @intToFloat(f32, self.config.scale[sample % self.config.scale.len]) / 12) * - (@intToFloat(f64, i) * 0.0001)); + std.math.pow(f32, 2, @as(f32, @floatFromInt(self.config.scale[sample % self.config.scale.len])) / 12) * + (@as(f64, @floatFromInt(i)) * 0.0001)); // Scale the amplitude between 0 and 256. amp = (amp * std.math.maxInt(u8) / 2) + (std.math.maxInt(u8) / 2); // Apply the volume control. - amp = amp * @intToFloat(f64, self.config.volume) / 100; - try buffer.append(@floatToInt(u8, amp)); + amp = amp * @as(f64, @floatFromInt(self.config.volume)) / 100; + try buffer.append(@as(u8, @intFromFloat(amp))); } return buffer.toOwnedSlice(); } diff --git a/src/main.zig b/src/main.zig index ba4aedf..e98e8ad 100644 --- a/src/main.zig +++ b/src/main.zig @@ -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; } @@ -23,7 +23,7 @@ fn run(allocator: std.mem.Allocator, output: anytype) !void { // Create encoder configuration. const encoder_config = wav.EncoderConfig{ .num_channels = if (cli.args.channels) |channels| channels else defaults.channels, - .sample_rate = if (cli.args.rate) |rate| @floatToInt(usize, rate) else defaults.sample_rate, + .sample_rate = if (cli.args.rate) |rate| @as(usize, @intFromFloat(rate)) else defaults.sample_rate, .format = if (cli.args.format) |format| format else defaults.format, }; @@ -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{ @@ -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); } @@ -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 diff --git a/src/wav.zig b/src/wav.zig index 31c7b33..e3899b0 100644 --- a/src/wav.zig +++ b/src/wav.zig @@ -90,12 +90,12 @@ pub fn Encoder(comptime Writer: type) type { fn writeChunks(writer: Writer, config: EncoderConfig, opt_data: ?[]const u8) !void { // Chunk configuration. const bytes_per_sample = config.format.getNumBytes(); - const num_channels = @intCast(u16, config.num_channels); - const sample_rate = @intCast(u32, config.sample_rate); + const num_channels = @as(u16, @intCast(config.num_channels)); + const sample_rate = @as(u32, @intCast(config.sample_rate)); const byte_rate = sample_rate * @as(u32, num_channels) * bytes_per_sample; const block_align: u16 = num_channels * bytes_per_sample; const bits_per_sample: u16 = bytes_per_sample * 8; - const data_len = if (opt_data) |data| @intCast(u32, data.len) else 0; + const data_len = if (opt_data) |data| @as(u32, @intCast(data.len)) else 0; // Write the file header. try writer.writeAll(&RIFF); if (opt_data != null) {