Skip to content

Commit 376260d

Browse files
committed
Add Zig Extension
1 parent e92faf3 commit 376260d

File tree

5 files changed

+217
-0
lines changed

5 files changed

+217
-0
lines changed

zig/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.zig-cache/
2+
zig-out/
3+
main
4+
root

zig/build.zig

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
const std = @import("std");
2+
3+
// Although this function looks imperative, note that its job is to
4+
// declaratively construct a build graph that will be executed by an external
5+
// runner.
6+
pub fn build(b: *std.Build) void {
7+
// Standard target options allows the person running `zig build` to choose
8+
// what target to build for. Here we do not override the defaults, which
9+
// means any target is allowed, and the default is native. Other options
10+
// for restricting supported target set are available.
11+
const target = b.standardTargetOptions(.{});
12+
13+
// Standard optimization options allow the person running `zig build` to select
14+
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
15+
// set a preferred release mode, allowing the user to decide how to optimize.
16+
const optimize = b.standardOptimizeOption(.{});
17+
18+
const lib = b.addStaticLibrary(.{
19+
.name = "zig",
20+
// In this case the main source file is merely a path, however, in more
21+
// complicated build scripts, this could be a generated file.
22+
.root_source_file = b.path("src/root.zig"),
23+
.target = target,
24+
.optimize = optimize,
25+
});
26+
27+
// This declares intent for the library to be installed into the standard
28+
// location when the user invokes the "install" step (the default step when
29+
// running `zig build`).
30+
b.installArtifact(lib);
31+
32+
const exe = b.addExecutable(.{
33+
.name = "zig",
34+
.root_source_file = b.path("src/main.zig"),
35+
.target = target,
36+
.optimize = optimize,
37+
});
38+
39+
exe.addLibraryPath(b.path("../")); // Equivalent to `-L/path`
40+
exe.linkSystemLibrary("c");
41+
exe.linkSystemLibrary("erbx");
42+
43+
// This declares intent for the executable to be installed into the
44+
// standard location when the user invokes the "install" step (the default
45+
// step when running `zig build`).
46+
b.installArtifact(exe);
47+
48+
// This *creates* a Run step in the build graph, to be executed when another
49+
// step is evaluated that depends on it. The next line below will establish
50+
// such a dependency.
51+
const run_cmd = b.addRunArtifact(exe);
52+
53+
// By making the run step depend on the install step, it will be run from the
54+
// installation directory rather than directly from within the cache directory.
55+
// This is not necessary, however, if the application depends on other installed
56+
// files, this ensures they will be present and in the expected location.
57+
run_cmd.step.dependOn(b.getInstallStep());
58+
59+
// This allows the user to pass arguments to the application in the build
60+
// command itself, like this: `zig build run -- arg1 arg2 etc`
61+
if (b.args) |args| {
62+
run_cmd.addArgs(args);
63+
}
64+
65+
// This creates a build step. It will be visible in the `zig build --help` menu,
66+
// and can be selected like this: `zig build run`
67+
// This will evaluate the `run` step rather than the default, which is "install".
68+
const run_step = b.step("run", "Run the app");
69+
run_step.dependOn(&run_cmd.step);
70+
71+
// Creates a step for unit testing. This only builds the test executable
72+
// but does not run it.
73+
const lib_unit_tests = b.addTest(.{
74+
.root_source_file = b.path("src/root.zig"),
75+
.target = target,
76+
.optimize = optimize,
77+
});
78+
79+
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
80+
81+
const exe_unit_tests = b.addTest(.{
82+
.root_source_file = b.path("src/main.zig"),
83+
.target = target,
84+
.optimize = optimize,
85+
});
86+
87+
exe_unit_tests.addLibraryPath(b.path("../")); // Equivalent to `-L/path`
88+
exe_unit_tests.linkSystemLibrary("c");
89+
exe_unit_tests.linkSystemLibrary("erbx");
90+
91+
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
92+
93+
// Similar to creating the run step earlier, this exposes a `test` step to
94+
// the `zig build --help` menu, providing a way for the user to request
95+
// running the unit tests.
96+
const test_step = b.step("test", "Run unit tests");
97+
test_step.dependOn(&run_lib_unit_tests.step);
98+
test_step.dependOn(&run_exe_unit_tests.step);
99+
}

zig/build.zig.zon

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
.{
2+
// This is the default name used by packages depending on this one. For
3+
// example, when a user runs `zig fetch --save <url>`, this field is used
4+
// as the key in the `dependencies` table. Although the user can choose a
5+
// different name, most users will stick with this provided value.
6+
//
7+
// It is redundant to include "zig" in this name because it is already
8+
// within the Zig package namespace.
9+
.name = "zig",
10+
11+
// This is a [Semantic Version](https://semver.org/).
12+
// In a future version of Zig it will be used for package deduplication.
13+
.version = "0.0.0",
14+
15+
// This field is optional.
16+
// This is currently advisory only; Zig does not yet do anything
17+
// with this value.
18+
//.minimum_zig_version = "0.11.0",
19+
20+
// This field is optional.
21+
// Each dependency must either provide a `url` and `hash`, or a `path`.
22+
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
23+
// Once all dependencies are fetched, `zig build` no longer requires
24+
// internet connectivity.
25+
.dependencies = .{
26+
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
27+
//.example = .{
28+
// // When updating this field to a new URL, be sure to delete the corresponding
29+
// // `hash`, otherwise you are communicating that you expect to find the old hash at
30+
// // the new URL.
31+
// .url = "https://example.com/foo.tar.gz",
32+
//
33+
// // This is computed from the file contents of the directory of files that is
34+
// // obtained after fetching `url` and applying the inclusion rules given by
35+
// // `paths`.
36+
// //
37+
// // This field is the source of truth; packages do not come from a `url`; they
38+
// // come from a `hash`. `url` is just one of many possible mirrors for how to
39+
// // obtain a package matching this `hash`.
40+
// //
41+
// // Uses the [multihash](https://multiformats.io/multihash/) format.
42+
// .hash = "...",
43+
//
44+
// // When this is provided, the package is found in a directory relative to the
45+
// // build root. In this case the package's hash is irrelevant and therefore not
46+
// // computed. This field and `url` are mutually exclusive.
47+
// .path = "foo",
48+
49+
// // When this is set to `true`, a package is declared to be lazily
50+
// // fetched. This makes the dependency only get fetched if it is
51+
// // actually used.
52+
// .lazy = false,
53+
//},
54+
},
55+
56+
// Specifies the set of files and directories that are included in this package.
57+
// Only files and directories listed here are included in the `hash` that
58+
// is computed for this package. Only files listed here will remain on disk
59+
// when using the zig package manager. As a rule of thumb, one should list
60+
// files required for compilation plus any license(s).
61+
// Paths are relative to the build root. Use the empty string (`""`) to refer to
62+
// the build root itself.
63+
// A directory listed here means that all files within, recursively, are included.
64+
.paths = .{
65+
"build.zig",
66+
"build.zig.zon",
67+
"src",
68+
// For example...
69+
//"LICENSE",
70+
//"README.md",
71+
},
72+
}

zig/src/main.zig

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const std = @import("std");
2+
3+
const erbx = @cImport({
4+
@cInclude("/Users/marcoroth/Development/erbx/src/include/erbx.h");
5+
});
6+
7+
pub fn main() !void {
8+
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
9+
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
10+
}
11+
12+
// const c_html: [*c]const u8 = &"<img required />"[0]; // Get a C-compatible pointer
13+
14+
test "erbx" {
15+
const html: *const [13:0]u8 = "<html></html>"; // String literal, null-terminated
16+
const c_html: [*c]u8 = html[0]; // Get C-style pointer
17+
18+
const buffer: [*c]erbx.buffer_T = undefined; // Use C struct type
19+
20+
const init = erbx.buffer_init(buffer); // Explicit cast
21+
22+
if (init) {
23+
erbx.erbx_lex_to_buffer(c_html, buffer);
24+
}
25+
}
26+
27+
test "simple test" {
28+
var list = std.ArrayList(i32).init(std.testing.allocator);
29+
defer list.deinit(); // try commenting this out and see if zig detects the memory leak!
30+
try list.append(42);
31+
try std.testing.expectEqual(@as(i32, 42), list.pop());
32+
}

zig/src/root.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const std = @import("std");
2+
const testing = std.testing;
3+
4+
export fn add(a: i32, b: i32) i32 {
5+
return a + b;
6+
}
7+
8+
test "basic add functionality" {
9+
try testing.expect(add(3, 7) == 10);
10+
}

0 commit comments

Comments
 (0)