Skip to content

Commit

Permalink
feat!: rewrite for Zig 0.12/0.13 (#28)
Browse files Browse the repository at this point in the history
* refactor: rewrite for zig 0.12

* chore(build): switch to the zig package manager scheme

* ci: update ci for zig 0.12

* docs: update docs for zig 0.12
  • Loading branch information
tranzystorekk authored Oct 7, 2024
1 parent c091885 commit e18d8c1
Show file tree
Hide file tree
Showing 16 changed files with 113 additions and 112 deletions.
6 changes: 3 additions & 3 deletions .github/bors.toml
Original file line number Diff line number Diff line change
@@ -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: '--release=safe'",
"Build with args: '--release=fast'",
"Build with args: '--release=small'",
"Test",
"Check formatting",
]
Expand Down
7 changes: 2 additions & 5 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,22 @@ jobs:
steps:
- name: Checkout the repository
uses: actions/checkout@v3
with:
submodules: recursive

- name: Set the release version
run: echo "RELEASE_VERSION=${GITHUB_REF:11}" >> $GITHUB_ENV

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

- name: Show Zig version
run: |
zig version
zig env
- name: Build
run: zig build -Drelease-safe -Dtarget=${{ matrix.TARGET }}
run: zig build --release=safe -Dtarget=${{ matrix.TARGET }}

- name: Prepare release assets
shell: bash
Expand Down Expand Up @@ -124,7 +122,6 @@ jobs:
uses: actions/checkout@v3
with:
fetch-depth: 0
submodules: recursive

- name: Set the release version
run: echo "RELEASE_VERSION=${GITHUB_REF:11}" >> $GITHUB_ENV
Expand Down
18 changes: 6 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,18 @@ jobs:
OPTIMIZE:
[
"",
"-Drelease-safe",
"-Drelease-fast",
"-Drelease-small"
"--release=safe",
"--release=fast",
"--release=small"
]
steps:
- name: Checkout the repository
uses: actions/checkout@v3
with:
submodules: recursive

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

- name: Show Zig version
run: |
Expand All @@ -52,13 +50,11 @@ jobs:
steps:
- name: Checkout the repository
uses: actions/checkout@v3
with:
submodules: recursive

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

- name: Install kcov
run: |
Expand Down Expand Up @@ -86,13 +82,11 @@ jobs:
steps:
- name: Checkout the repository
uses: actions/checkout@v3
with:
submodules: recursive

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

- name: Check formatting
run: zig fmt --check .
4 changes: 1 addition & 3 deletions .github/workflows/pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ jobs:
steps:
- name: Checkout the repository
uses: actions/checkout@v3
with:
submodules: recursive

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

- name: Show Zig version
run: |
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Zig programming language

zig-cache/
.zig-cache/
zig-out/
build/
build-*/
Expand Down
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ RUN apk update
RUN apk add --no-cache git
WORKDIR /app
COPY . .
RUN git submodule update --init --recursive && \
zig build -Drelease-safe
RUN zig build --release=safe

FROM alpine:3.8
WORKDIR /app
Expand Down
12 changes: 3 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@

#### Prerequisites

- [Zig](https://ziglang.org/download/) (`0.10.1`)
- [Zig](https://ziglang.org/download/) (`0.12` or `0.13`)

#### Instructions

Expand All @@ -76,16 +76,10 @@
git clone https://github.com/orhun/linuxwave && cd linuxwave/
```

2. Update git submodules.
2. Build.

```sh
git submodule update --init --recursive
```

3. Build.

```sh
zig build -Drelease-safe
zig build --release=safe
```

Binary will be located at `zig-out/bin/linuxwave`. You can also run the binary directly via `zig build run`.
Expand Down
61 changes: 38 additions & 23 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,27 @@ 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");
fn addPackages(b: *std.Build, exe: *std.Build.Step.Compile) !void {
const clap = b.dependency("zig-clap", .{}).module("clap");
exe.root_module.addImport("clap", clap);
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);
const path = b.fmt("src/{s}.zig", .{package});
exe.root_module.addImport(package, b.createModule(.{
.root_source_file = b.path(path),
}));
}
}

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
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});

// Standard release options allow the person running `zig build` to select
// Standard optimization 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 @@ -36,27 +38,35 @@ 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 = b.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();
exe.addOptions("build_options", exe_options);
exe.root_module.addOptions("build_options", exe_options);
exe_options.addOption([]const u8, "version", version);
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);
Expand All @@ -68,20 +78,25 @@ 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| {
for ([_][]const u8{ "file", "gen", "wav" }) |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 = b.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(b.allocator, exe_tests);
exe_tests.addOptions("build_options", exe_options);
test_step.dependOn(&exe_tests.step);
try addPackages(b, exe_tests);
exe_tests.root_module.addOptions("build_options", exe_options);
const run_unit_tests = b.addRunArtifact(exe_tests);
test_step.dependOn(&run_unit_tests.step);
}
}
17 changes: 17 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.{
.name = "linuxwave",
.version = "0.1.5",
.paths = .{
"build.zig",
"build.zig.zon",
"src",
"LICENSE",
"README.md",
},
.dependencies = .{
.@"zig-clap" = .{
.url = "https://github.com/Hejsil/zig-clap/archive/refs/tags/0.9.1.tar.gz",
.hash = "122062d301a203d003547b414237229b09a7980095061697349f8bef41be9c30266b",
},
},
}
1 change: 0 additions & 1 deletion libs/zig-clap
Submodule zig-clap deleted from 749c43
2 changes: 1 addition & 1 deletion src/file.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn readBytes(
test "read bytes from the file" {
// Get the current directory.
var cwd_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
var cwd = try std.os.getcwd(&cwd_buffer);
const cwd = try std.posix.getcwd(&cwd_buffer);

// Concatenate the current directory with the builder file.
const allocator = std.testing.allocator;
Expand Down
11 changes: 6 additions & 5 deletions src/gen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ pub const Generator = struct {
// Calculate the frequency according to the equal temperament.
// Hertz = 440 * 2^(semitone distance / 12)
// (<http://en.wikipedia.org/wiki/Equal_temperament>)
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));
const tone_distance: f32 = @floatFromInt(self.config.scale[sample % self.config.scale.len]);
const increment: f32 = @floatFromInt(i);
var amp = @sin(self.config.note * std.math.pi * std.math.pow(f32, 2, tone_distance / 12) * (increment * 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));
const volume: f32 = @floatFromInt(self.config.volume);
amp = amp * volume / 100;
try buffer.append(@intFromFloat(amp));
}
return buffer.toOwnedSlice();
}
Expand Down
Loading

0 comments on commit e18d8c1

Please sign in to comment.