Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -25,16 +26,18 @@ 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
```

The executables will be available in `zig-out/bin/`.

## Usage

### wc Examples

<details>
<summary>wc Examples</summary>

Count lines, words, and characters in a file:
```bash
Expand All @@ -50,6 +53,7 @@ Count words from standard input:
```bash
cat file.txt | wc -w
```
</details>

## Contributing

Expand Down
86 changes: 55 additions & 31 deletions build.zig
Original file line number Diff line number Diff line change
@@ -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"));
}
3 changes: 3 additions & 0 deletions src/true/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
int main(void) {
return 0;
}