Skip to content

Commit

Permalink
Merge pull request #50 from lun-4/zigpkg
Browse files Browse the repository at this point in the history
port to zig package manager
  • Loading branch information
kivikakk authored Jun 10, 2024
2 parents 32671f5 + bfe7689 commit 605f22c
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 35 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/zig-*
koino.code-workspace
# https://github.com/kivikakk/htmlentities.zig/issues/10
src/entities.zig
12 changes: 0 additions & 12 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
[submodule "vendor/cmark-gfm"]
path = vendor/cmark-gfm
url = https://github.com/kivikakk/cmark-gfm.git
[submodule "vendor/htmlentities.zig"]
path = vendor/htmlentities.zig
url = https://github.com/kivikakk/htmlentities.zig
[submodule "vendor/libpcre.zig"]
path = vendor/libpcre.zig
url = https://github.com/kivikakk/libpcre.zig
[submodule "vendor/zunicode"]
path = vendor/zunicode
url = https://github.com/kivikakk/zunicode
[submodule "vendor/zig-clap"]
path = vendor/zig-clap
url = https://github.com/Hejsil/zig-clap
38 changes: 33 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,43 @@ Zig port of [Comrak](https://github.com/kivikakk/comrak). Maintains 100% spec-c

## Getting started

* Clone the repository with submodules, as we have quite a few dependencies.
## Using koino as a library

* Get zig 0.12+ https://ziglang.org/
* Start a new project with `zig init-exe` / `zig init-lib`
* Add koino via the zig package manager:
```console
$ git clone --recurse-submodules https://github.com/kivikakk/koino
$ zig fetch --save https://github.com/kivikakk/koino/archive/<commit hash>.tar.gz
```

* [Follow the `libpcre.zig` dependency install instructions](https://github.com/kivikakk/libpcre.zig/blob/main/README.md) for your operating system.
* Add the following to your `build.zig`'s `build` function:
```zig
const koino_pkg = b.dependency("koino", .{ .optimize = optimize, .target = target });
exe.root_module.addImport("koino", koino_pkg.module("koino"));
```

* Have a look at the bottom of [`parser.zig`](https://github.com/kivikakk/koino/blob/main/src/parser.zig) to see some test usage.

### Using it as a CLI executable

* Clone this repository:
```console
$ git clone https://github.com/kivikakk/koino
```
* Build
```console
$ zig build
```
* Use `./zig-out/bin/koino`

### For development purposes

* Clone this repository with submodules for the `cmark-gfm` dependency:
```console
$ git clone --recurse-submodules https://github.com/kivikakk/koino
$ cd koino
```

* Build and run the spec suite.

Expand All @@ -23,8 +53,6 @@ Zig port of [Comrak](https://github.com/kivikakk/comrak). Maintains 100% spec-c
$ make spec
```

* Have a look at the bottom of [`parser.zig`](https://github.com/kivikakk/koino/blob/main/src/parser.zig) to see some test usage.


## Usage

Expand Down
33 changes: 20 additions & 13 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
const std = @import("std");
const linkPcre = @import("vendor/libpcre.zig/build.zig").linkPcre;

pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

var deps = std.StringHashMap(*std.Build.Module).init(b.allocator);
const libpcre = b.addModule("libpcre", .{
.root_source_file = b.path("vendor/libpcre.zig/src/main.zig"),

const pcre_pkg = b.dependency("libpcre.zig", .{ .optimize = optimize, .target = target });
const htmlentities_pkg = b.dependency("htmlentities.zig", .{ .optimize = optimize, .target = target });
const zunicode_pkg = b.dependency("zunicode", .{ .optimize = optimize, .target = target });
const clap_pkg = b.dependency("clap", .{ .optimize = optimize, .target = target });

try deps.put("clap", clap_pkg.module("clap"));
try deps.put("libpcre", pcre_pkg.module("libpcre"));
try deps.put("zunicode", zunicode_pkg.module("zunicode"));
try deps.put("htmlentities", htmlentities_pkg.module("htmlentities"));

const mod = b.addModule("koino", .{
.root_source_file = b.path("src/koino.zig"),
.target = target,
.optimize = optimize,
});
try linkPcre(b, libpcre);
try deps.put("libpcre", libpcre);
try deps.put("htmlentities", b.addModule("htmlentities", .{ .root_source_file = b.path("vendor/htmlentities.zig/src/main.zig") }));
try deps.put("clap", b.addModule("clap", .{ .root_source_file = b.path("vendor/zig-clap/clap.zig") }));
try deps.put("zunicode", b.addModule("zunicode", .{ .root_source_file = b.path("vendor/zunicode/src/zunicode.zig") }));
try addCommonRequirements(mod, &deps);

const exe = b.addExecutable(.{
.name = "koino",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
try addCommonRequirements(exe, &deps);
try addCommonRequirements(&exe.root_module, &deps);
b.installArtifact(exe);

const run_cmd = b.addRunArtifact(exe);
Expand All @@ -40,15 +47,15 @@ pub fn build(b: *std.Build) !void {
.target = target,
.optimize = optimize,
});
try addCommonRequirements(test_exe, &deps);
try addCommonRequirements(&test_exe.root_module, &deps);
const test_step = b.step("test", "Run all the tests");
test_step.dependOn(&test_exe.step);
}

fn addCommonRequirements(cs: *std.Build.Step.Compile, deps: *const std.StringHashMap(*std.Build.Module)) !void {
fn addCommonRequirements(mod: *std.Build.Module, deps: *const std.StringHashMap(*std.Build.Module)) !void {
var it = deps.iterator();
while (it.next()) |entry| {
cs.root_module.addImport(entry.key_ptr.*, entry.value_ptr.*);
mod.addImport(entry.key_ptr.*, entry.value_ptr.*);
}
cs.linkLibC();
mod.linkSystemLibrary("c", .{});
}
32 changes: 32 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
.{
.name = "koino",
.version = "0.1.0",
.minimum_zig_version = "0.12.0",
.dependencies = .{
.@"libpcre.zig" = .{
.url = "https://github.com/kivikakk/libpcre.zig/archive/9522ad9bcce76ce608ac62097450d4eac7436d9c.tar.gz",
.hash = "1220fa274f41744bc60b284f9264705f772b98afd2ed219cebb6cec744422e8e2692",
},
.@"htmlentities.zig" = .{
.url = "https://github.com/kivikakk/htmlentities.zig/archive/9cc3600c53ae60565d839eaf93d5c519c21e27cc.tar.gz",
.hash = "1220efcce051d499ab04ab1d31d6d3fd242727f90a9287cd56e6c42ba912f782282c",
},
.zunicode = .{
.url = "https://github.com/kivikakk/zunicode/archive/711afa05416d1d1512bccf798a332be494d08f5f.tar.gz",
.hash = "1220d530a8c14f65f6184e695bf04d14c97b8e341cd391c579f9e85990c6262d2fec",
},
.clap = .{
.url = "https://github.com/Hejsil/zig-clap/archive/1d413d9ffcbd394904fa683ca975b5adbc19e615.tar.gz",
.hash = "1220949d4e88864579067b6d4cdad6476c6176f27e782782c2c39b7f2c4817a10efb",
},
},

.paths = .{
"build.zig",
"Makefile",
"build.zig.zon",
"src",
"LICENSE",
"README.md",
},
}
2 changes: 1 addition & 1 deletion src/parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ pub const Parser = struct {
var new_line: ?[]u8 = null;
if (input.len == 0 or !strings.isLineEndChar(input[input.len - 1])) {
new_line = try self.allocator.alloc(u8, input.len + 1);
@memcpy(new_line.?, input);
std.mem.copyForwards(u8, new_line.?, input);
new_line.?[input.len] = '\n';
line = new_line.?;
} else {
Expand Down
1 change: 0 additions & 1 deletion vendor/htmlentities.zig
Submodule htmlentities.zig deleted from 14a3eb
1 change: 0 additions & 1 deletion vendor/libpcre.zig
Submodule libpcre.zig deleted from 9522ad
1 change: 0 additions & 1 deletion vendor/zig-clap
Submodule zig-clap deleted from 1d413d
1 change: 0 additions & 1 deletion vendor/zunicode
Submodule zunicode deleted from de8c1e

0 comments on commit 605f22c

Please sign in to comment.