feat: part of the story api exposed via c bindings
This commit is contained in:
parent
a065e5bf46
commit
3afbbb6ec2
6 changed files with 748 additions and 16 deletions
142
build.zig
142
build.zig
|
|
@ -1,8 +1,7 @@
|
|||
const std = @import("std");
|
||||
|
||||
pub fn build(b: *std.Build) void {
|
||||
pub fn build(b: *std.Build) !void {
|
||||
const use_llvm = true;
|
||||
|
||||
const target = b.standardTargetOptions(.{});
|
||||
const optimize = b.standardOptimizeOption(.{});
|
||||
const mod = b.addModule("ink", .{
|
||||
|
|
@ -21,11 +20,71 @@ pub fn build(b: *std.Build) void {
|
|||
}),
|
||||
// Added because of a switch on a corrupt value on git hash
|
||||
// a8a6ce8d0f14c1dd5b3ea42e46f3226bcbe11a87
|
||||
.use_llvm = true,
|
||||
.use_llvm = use_llvm,
|
||||
.use_lld = use_llvm,
|
||||
});
|
||||
const lib = b.addLibrary(.{
|
||||
.linkage = .static,
|
||||
.name = "ink",
|
||||
.root_module = b.createModule(.{
|
||||
.root_source_file = b.path("src/c_api.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
.imports = &.{
|
||||
.{ .name = "ink", .module = mod },
|
||||
},
|
||||
}),
|
||||
.use_llvm = use_llvm,
|
||||
.use_lld = use_llvm,
|
||||
});
|
||||
|
||||
const pc: *std.Build.Step.InstallFile = pc: {
|
||||
const file = b.addWriteFile("libink.pc", b.fmt(
|
||||
\\prefix={s}
|
||||
\\includedir=${{prefix}}/include
|
||||
\\libdir=${{prefix}}/lib
|
||||
\\
|
||||
\\Name: libink
|
||||
\\URL: https://codeberg.org/haxolotl/ink
|
||||
\\Description: TODO
|
||||
\\Version: 0.1.0
|
||||
\\Cflags: -I${{includedir}}
|
||||
\\Libs: -L${{libdir}} -link
|
||||
, .{b.install_prefix}));
|
||||
break :pc b.addInstallFileWithDir(
|
||||
file.getDirectory().path(b, "libink.pc"),
|
||||
.prefix,
|
||||
"share/pkgconfig/libink.pc",
|
||||
);
|
||||
};
|
||||
|
||||
const c_header = b.addInstallFileWithDir(
|
||||
b.path("include/ink.h"),
|
||||
.header,
|
||||
"ink/ink.h",
|
||||
);
|
||||
|
||||
const examples = try buildExamples(b, target, optimize, lib);
|
||||
|
||||
b.getInstallStep().dependOn(&lib.step);
|
||||
b.getInstallStep().dependOn(&c_header.step);
|
||||
b.getInstallStep().dependOn(&pc.step);
|
||||
b.installArtifact(lib);
|
||||
b.installArtifact(exe);
|
||||
|
||||
for (examples) |example_exe| {
|
||||
b.getInstallStep().dependOn(&b.addInstallArtifact(
|
||||
example_exe,
|
||||
.{
|
||||
.dest_dir = .{
|
||||
.override = .{
|
||||
.custom = "bin/example",
|
||||
},
|
||||
},
|
||||
},
|
||||
).step);
|
||||
}
|
||||
|
||||
const run_step = b.step("run", "Run the app");
|
||||
const run_cmd = b.addRunArtifact(exe);
|
||||
run_step.dependOn(&run_cmd.step);
|
||||
|
|
@ -42,16 +101,8 @@ pub fn build(b: *std.Build) void {
|
|||
});
|
||||
const run_mod_tests = b.addRunArtifact(mod_tests);
|
||||
|
||||
const exe_tests = b.addTest(.{
|
||||
.root_module = exe.root_module,
|
||||
.use_llvm = use_llvm,
|
||||
.use_lld = use_llvm,
|
||||
});
|
||||
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 run_cover = b.addSystemCommand(&.{
|
||||
"kcov",
|
||||
|
|
@ -63,3 +114,72 @@ pub fn build(b: *std.Build) void {
|
|||
const cover_step = b.step("cover", "Generate test coverage report");
|
||||
cover_step.dependOn(&run_cover.step);
|
||||
}
|
||||
|
||||
fn buildExamples(
|
||||
b: *std.Build,
|
||||
target: std.Build.ResolvedTarget,
|
||||
optimize: std.builtin.OptimizeMode,
|
||||
c_lib_: ?*std.Build.Step.Compile,
|
||||
) ![]const *std.Build.Step.Compile {
|
||||
const alloc = b.allocator;
|
||||
var steps: std.ArrayList(*std.Build.Step.Compile) = .empty;
|
||||
defer steps.deinit(alloc);
|
||||
|
||||
var dir = try std.fs.cwd().openDir(try b.build_root.join(
|
||||
b.allocator,
|
||||
&.{"examples"},
|
||||
), .{ .iterate = true });
|
||||
defer dir.close();
|
||||
|
||||
var it = dir.iterate();
|
||||
while (try it.next()) |entry| {
|
||||
const index = std.mem.lastIndexOfScalar(u8, entry.name, '.') orelse continue;
|
||||
if (index == 0) continue;
|
||||
|
||||
const name = entry.name[0..index];
|
||||
const is_zig = std.mem.eql(u8, entry.name[index + 1 ..], "zig");
|
||||
const exe: *std.Build.Step.Compile = if (is_zig) exe: {
|
||||
const exe = b.addExecutable(.{
|
||||
.name = name,
|
||||
.root_module = b.createModule(.{
|
||||
.root_source_file = b.path(b.fmt(
|
||||
"examples/{s}",
|
||||
.{entry.name},
|
||||
)),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
}),
|
||||
});
|
||||
exe.root_module.addImport("ink", b.modules.get("ink").?);
|
||||
break :exe exe;
|
||||
} else exe: {
|
||||
const c_lib = c_lib_ orelse return error.UnsupportedPlatform;
|
||||
const exe = b.addExecutable(.{
|
||||
.name = name,
|
||||
.root_module = b.createModule(.{
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
}),
|
||||
});
|
||||
exe.linkLibC();
|
||||
exe.addIncludePath(b.path("include"));
|
||||
exe.addCSourceFile(.{
|
||||
.file = b.path(b.fmt(
|
||||
"examples/{s}",
|
||||
.{entry.name},
|
||||
)),
|
||||
.flags = &[_][]const u8{
|
||||
"-Wall",
|
||||
"-Wextra",
|
||||
"-pedantic",
|
||||
"-std=c99",
|
||||
"-D_POSIX_C_SOURCE=199309L",
|
||||
},
|
||||
});
|
||||
exe.linkLibrary(c_lib);
|
||||
break :exe exe;
|
||||
};
|
||||
try steps.append(alloc, exe);
|
||||
}
|
||||
return steps.toOwnedSlice(alloc);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue