const std = @import("std"); pub fn build(b: *std.Build) !void { const use_llvm = true; const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); const mod = b.addModule("ink", .{ .root_source_file = b.path("src/root.zig"), .target = target, }); const exe = b.addExecutable(.{ .name = "inkc", .root_module = b.createModule(.{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "ink", .module = mod }, }, }), // Added because of a switch on a corrupt value on git hash // a8a6ce8d0f14c1dd5b3ea42e46f3226bcbe11a87 .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); run_cmd.step.dependOn(b.getInstallStep()); if (b.args) |args| { run_cmd.addArgs(args); } const mod_tests = b.addTest(.{ .root_module = mod, .use_llvm = use_llvm, .use_lld = use_llvm, }); const run_mod_tests = b.addRunArtifact(mod_tests); const test_step = b.step("test", "Run tests"); test_step.dependOn(&run_mod_tests.step); const run_cover = b.addSystemCommand(&.{ "kcov", "--clean", "--include-pattern=src/", b.pathJoin(&.{ b.install_path, "cover" }), }); run_cover.addArtifactArg(mod_tests); 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 gpa = b.allocator; const io = b.graph.io; var steps: std.ArrayList(*std.Build.Step.Compile) = .empty; defer steps.deinit(gpa); var dir = try std.Io.Dir.cwd().openDir(io, try b.build_root.join( gpa, &.{"examples"}, ), .{ .iterate = true }); defer dir.close(io); var it = dir.iterate(); while (try it.next(io)) |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, .link_libc = true, }), }); exe.root_module.addIncludePath(b.path("include")); exe.root_module.addCSourceFile(.{ .file = b.path(b.fmt( "examples/{s}", .{entry.name}, )), .flags = &[_][]const u8{ "-Wall", "-Wextra", "-pedantic", "-std=c99", "-D_POSIX_C_SOURCE=199309L", }, }); exe.root_module.linkLibrary(c_lib); break :exe exe; }; try steps.append(gpa, exe); } return steps.toOwnedSlice(gpa); }