From 2b3734a087ac6e3b682adb486e1fb45045448aff Mon Sep 17 00:00:00 2001 From: David Allemang Date: Wed, 15 Jan 2025 16:47:59 -0500 Subject: [PATCH] Initial Commit --- .gitignore | 3 +++ .tool-versions | 1 + README.md | 18 +++++++++++++ build.zig | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ build.zig.zon | 16 ++++++++++++ src/main.zig | 35 +++++++++++++++++++++++++ src/root.zig | 14 ++++++++++ 7 files changed, 157 insertions(+) create mode 100644 .gitignore create mode 100644 .tool-versions create mode 100644 README.md create mode 100644 build.zig create mode 100644 build.zig.zon create mode 100644 src/main.zig create mode 100644 src/root.zig diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6182617 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.zig-cache/ +zig-out/ +.idea/ diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..fe42a60 --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +zig 0.14.0-dev.2647+5322459a0 diff --git a/README.md b/README.md new file mode 100644 index 0000000..e469a5b --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +### Configuring CLion for debugging + +Use https://github.com/asdf-vm/asdf with https://github.com/allemangd/asdf-zig for the versioned zig master in `.tool-versions`. + +Build ZLS with that zig and point ZigBrains to it. + +Custom Build Application configurations: + +- `zig-shape` uses custom target `zig build -fincremental` and executes `zig-out/bin/zig-shape`. +- `exe-unit-tests` uses custom target `zig build -fincremental build-tests` and executes `zig-out/dev/exe-unit-tests` +- `lib-unit-tests` uses custom target `zig build -fincremental build-tests` and executes `zig-out/dev/lib-unit-tests` + +Then native "build", "run", and "debug" buttons will work. + +Optionally create a "clean" target that executes `rm -r .zig-cache zig-out` + +Switching between debug and release build is more difficult. Probably easier to run release builds +only via CLI, like `zig build --release=fast` or `zig build --release=fast test`. diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..acc7311 --- /dev/null +++ b/build.zig @@ -0,0 +1,70 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + const lib_mod = b.createModule(.{ + .root_source_file = b.path("src/root.zig"), + .target = target, + .optimize = optimize, + }); + + const exe_mod = b.createModule(.{ + .root_source_file = b.path("src/main.zig"), + .target = target, + .optimize = optimize, + }); + + exe_mod.addImport("zig-shape-lib", lib_mod); + + const lib = b.addStaticLibrary(.{ + .name = "zig-shape", + .root_module = lib_mod, + }); + + b.installArtifact(lib); + + const exe = b.addExecutable(.{ + .name = "zig-shape", + .root_module = exe_mod, + }); + + b.installArtifact(exe); + + const run_cmd = b.addRunArtifact(exe); + + run_cmd.step.dependOn(b.getInstallStep()); + if (b.args) |args| { + run_cmd.addArgs(args); + } + + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); + + const lib_unit_tests = b.addTest(.{ + .root_module = lib_mod, + .name = "lib-unit-tests", + }); + + const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests); + + const exe_unit_tests = b.addTest(.{ + .root_module = exe_mod, + .name = "exe-unit-tests", + }); + + const build_test_step = b.step("build-test", "Build unit tests"); + + const install_lib_test = b.addInstallArtifact(lib_unit_tests, .{ .dest_dir = .{ .override = .{ .custom = "dev" } } }); + build_test_step.dependOn(&install_lib_test.step); + + const install_exe_test = b.addInstallArtifact(exe_unit_tests, .{ .dest_dir = .{ .override = .{ .custom = "dev" } } }); + build_test_step.dependOn(&install_exe_test.step); + + const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); + + const test_step = b.step("test", "Run unit tests"); + test_step.dependOn(&run_lib_unit_tests.step); + test_step.dependOn(&run_exe_unit_tests.step); +} diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000..9101812 --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,16 @@ +.{ + .name = "zig-shape", + .version = "0.0.0", + + .minimum_zig_version = "0.14.0", + + .dependencies = .{ + // See `zig fetch --save ` for a command-line interface for adding dependencies. + }, + + .paths = .{ + "build.zig", + "build.zig.zon", + "src", + }, +} diff --git a/src/main.zig b/src/main.zig new file mode 100644 index 0000000..deacd15 --- /dev/null +++ b/src/main.zig @@ -0,0 +1,35 @@ +const std = @import("std"); +const lib = @import("zig-shape-lib"); + +pub fn main() !void { + std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); + + const stdout_file = std.io.getStdOut().writer(); + var bw = std.io.bufferedWriter(stdout_file); + const stdout = bw.writer(); + + try stdout.print("Run `zig build test` to run the tests.\n", .{}); + + try bw.flush(); +} + +test "simple test" { + var list = std.ArrayList(i32).init(std.testing.allocator); + defer list.deinit(); + try list.append(42); + try std.testing.expectEqual(@as(i32, 42), list.pop()); +} + +test "use other module" { + try std.testing.expectEqual(@as(i32, 150), lib.add(100, 50)); +} + +test "fuzz example" { + const global = struct { + fn testOne(input: []const u8) anyerror!void { + // Try passing `--fuzz` to `zig build test` and see if it manages to fail this test case! + try std.testing.expect(!std.mem.eql(u8, "canyoufindme", input)); + } + }; + try std.testing.fuzz(global.testOne, .{}); +} diff --git a/src/root.zig b/src/root.zig new file mode 100644 index 0000000..0c5c75e --- /dev/null +++ b/src/root.zig @@ -0,0 +1,14 @@ +//! By convention, root.zig is the root source file when making a library. If +//! you are making an executable, the convention is to delete this file and +//! start with main.zig instead. + +const std = @import("std"); +const testing = std.testing; + +pub export fn add(a: i32, b: i32) i32 { + return a + b; +} + +test "basic add functionality" { + try testing.expect(add(3, 7) == 10); +}