From d49b06fcacb53a74377bd98b984af8eb3391a005 Mon Sep 17 00:00:00 2001 From: ciao Date: Sun, 13 Apr 2025 09:43:05 +0800 Subject: [PATCH 1/2] feat: implement true command --- build.zig | 86 ++++++++++++++++++++++++++++++----------------- src/true/main.cpp | 3 ++ 2 files changed, 58 insertions(+), 31 deletions(-) create mode 100644 src/true/main.cpp diff --git a/build.zig b/build.zig index 92c337b..547f2b1 100644 --- a/build.zig +++ b/build.zig @@ -1,47 +1,71 @@ const std = @import("std"); const zcc = @import("compile_commands"); +const Executable = struct { + name: []const u8, + cpp_sources: []const []const u8, + include_paths: []const []const u8, + + fn init(name: []const u8, cpp_sources: []const []const u8, include_paths: []const []const u8) Executable { + return Executable{ + .name = name, + .cpp_sources = cpp_sources, + .include_paths = include_paths, + }; + } + + fn build(self: Executable, b: *std.Build, optimize: std.builtin.Mode, target: std.Build.ResolvedTarget, targets: *std.ArrayList(*std.Build.Step.Compile)) void { + const exe = b.addExecutable(.{ + .name = self.name, + .target = target, + .optimize = optimize, + }); + targets.append(exe) catch @panic("OOM"); + for (self.cpp_sources) |source| { + exe.addCSourceFile(.{ + .file = b.path(source), + .flags = &[_][]const u8{"-std=c++20"}, + }); + } + exe.linkLibCpp(); + for (self.include_paths) |include_path| { + exe.addIncludePath(b.path(include_path)); + } + b.installArtifact(exe); + + const run_cmd_name = std.fmt.allocPrint(b.allocator, "run-{s}", .{self.name}) catch @panic("OOM"); + const run_cmd_info = std.fmt.allocPrint(b.allocator, "Run the {s} app", .{self.name}) catch @panic("OOM"); + const run_step = b.step(run_cmd_name, run_cmd_info); + const run_cmd = b.addRunArtifact(exe); + run_cmd.step.dependOn(b.getInstallStep()); + + if (b.args) |args| { + run_cmd.addArgs(args); + } + run_step.dependOn(&run_cmd.step); + } +}; + pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); var targets = std.ArrayList(*std.Build.Step.Compile).init(b.allocator); defer targets.deinit(); - const wc_exe = b.addExecutable(.{ - .name = "wc", - .target = target, - .optimize = optimize, - }); - targets.append(wc_exe) catch @panic("OOM"); - - const cpp_sources = [_][]const u8{ + Executable.init("wc", &[_][]const u8{ "src/wc/main.cpp", "src/wc/wc.cpp", "src/wc/options.cpp", "src/wc/params.cpp", - }; - - for (cpp_sources) |source| { - wc_exe.addCSourceFile(.{ - .file = b.path(source), - .flags = &[_][]const u8{"-std=c++20"}, - }); - } - - wc_exe.linkLibCpp(); - wc_exe.addIncludePath(b.path("src/wc")); - - b.installArtifact(wc_exe); - - const run_step = b.step("run-wc", "Run the wc app"); - - const run_cmd = b.addRunArtifact(wc_exe); - run_cmd.step.dependOn(b.getInstallStep()); - - if (b.args) |args| { - run_cmd.addArgs(args); - } + }, &[_][]const u8{ + "src/wc", + }).build(b, optimize, target, &targets); + + Executable.init("true", &[_][]const u8{ + "src/true/main.cpp", + }, &[_][]const u8{ + "src/true", + }).build(b, optimize, target, &targets); - run_step.dependOn(&run_cmd.step); zcc.createStep(b, "cdb", targets.toOwnedSlice() catch @panic("OOM")); } diff --git a/src/true/main.cpp b/src/true/main.cpp new file mode 100644 index 0000000..a51103c --- /dev/null +++ b/src/true/main.cpp @@ -0,0 +1,3 @@ +int main(void) { + return 0; +} \ No newline at end of file From fc126ffb919c56aba5a2a780f6a05a34ca86bc2a Mon Sep 17 00:00:00 2001 From: ciao Date: Sun, 13 Apr 2025 09:48:35 +0800 Subject: [PATCH 2/2] fix: update readme --- README.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7df0667..03d9c8a 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ A project to implement GNU coreutils utilities in `C++` and `Zig`. This is an on ## Implemented Utilities - `wc`: Word, line, and byte counting utility +- `true`: Do nothing, successfully (exit with status 0) ## Planned Utilities @@ -25,8 +26,8 @@ The project aims to implement all core utilities, including but not limited to: Clone the repository and build using Zig's build system: ```bash -git clone https://github.com/guuzaa/wc.git -cd wc +git clone https://github.com/guuzaa/coreutils.git +cd coreutils zig build ``` @@ -34,7 +35,9 @@ The executables will be available in `zig-out/bin/`. ## Usage -### wc Examples + +
+wc Examples Count lines, words, and characters in a file: ```bash @@ -50,6 +53,7 @@ Count words from standard input: ```bash cat file.txt | wc -w ``` +
## Contributing