diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-19 17:26:31 +0200 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-19 17:26:37 +0200 |
| commit | 25fa85cbb622e1f9dc9818b0d18fe0de2ef7e5a3 (patch) | |
| tree | 2d6998f27c88ff046c56fad7420c447fa407cf3a | |
| parent | d1480f97e3c8ce82151d409e7c9d1545d44f3a99 (diff) | |
build(zig): test integrations with C
| -rw-r--r-- | build.zig | 57 | ||||
| -rw-r--r-- | examples/main.c | 25 | ||||
| -rw-r--r-- | include/typdown.h | 5 |
3 files changed, 57 insertions, 30 deletions
@@ -2,48 +2,45 @@ const std = @import("std"); pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); - //const optimize = b.standardOptimizeOption(.{}); + const optimize = b.standardOptimizeOption(.{}); const mod = b.addModule("typdown", .{ .root_source_file = b.path("src/root.zig"), .target = target, + .optimize = optimize, .link_libc = true, }); - - //const exe = b.addExecutable(.{ - // .name = "typdown", - // .root_module = b.createModule(.{ - // .root_source_file = b.path("src/main.zig"), - // .target = target, - // .optimize = optimize, - // .imports = &.{ - // .{ .name = "typdown", .module = mod }, - // }, - // }), - //}); - //b.installArtifact(exe); - - //const run_step = b.step("run", "Run the app"); - //const run_cmd = b.addRunArtifact(exe); - //run_step.dependOn(&run_cmd.step); - //run_cmd.step.dependOn(b.getInstallStep()); - - //if (b.args) |args| { - // run_cmd.addArgs(args); - //} + const lib = b.addLibrary(.{ + .name = "typdown", + .linkage = .dynamic, + .root_module = mod, + }); + b.installArtifact(lib); + + const example = b.addExecutable(.{ + .name = "example", + .root_module = b.createModule(.{ + .link_libc = true, + .target = target, + .optimize = optimize, + }), + }); + example.root_module.addCSourceFile(.{ + .file = b.path("examples/main.c"), + }); + example.root_module.linkLibrary(lib); + // manually writing headers because lib.getEmittedH() doesn't work. + example.root_module.addIncludePath(b.path("include")); const mod_tests = b.addTest(.{ .root_module = mod, }); const run_mod_tests = b.addRunArtifact(mod_tests); - //const exe_tests = b.addTest(.{ - // .root_module = exe.root_module, - //}); - - //const run_exe_tests = b.addRunArtifact(exe_tests); - const test_step = b.step("test", "Run tests"); test_step.dependOn(&run_mod_tests.step); - //test_step.dependOn(&run_exe_tests.step); + + const examples_step = b.step("examples", "Run examples"); + const example_run = b.addRunArtifact(example); + examples_step.dependOn(&example_run.step); } diff --git a/examples/main.c b/examples/main.c new file mode 100644 index 0000000..1409bd5 --- /dev/null +++ b/examples/main.c @@ -0,0 +1,25 @@ +#include <stdlib.h> +#include <stdio.h> +#include <stdint.h> +#include "typdown.h" + +void foo(char *v) { + uint8_t code; + char *res = parse(v, &code); + if (code == 0) { + printf("%s\n", res); + free(res); + } else printf("cannot parse '%s', error: %s (%d)\n", v, getErrorString(code), code); +} + +int main() { + // valid + foo("hello world"); + foo("he*ll*o world"); + foo("# he*ll*o world"); + + // invalid + foo("hello *world"); + foo("# hello :::"); + return 0; +} diff --git a/include/typdown.h b/include/typdown.h new file mode 100644 index 0000000..4b08aaa --- /dev/null +++ b/include/typdown.h @@ -0,0 +1,5 @@ +#pragma once +#include <stdint.h> + +char * getErrorString(uint8_t); +char * parse(char *, uint8_t *); |
