forked from capy-ui/capy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.zig
109 lines (90 loc) · 4.58 KB
/
build.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const std = @import("std");
pub const install = @import("build_capy.zig").install;
pub const CapyBuildOptions = @import("build_capy.zig").CapyBuildOptions;
const LazyPath = std.Build.LazyPath;
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
var examplesDir = try if (@hasField(std.fs.Dir.OpenDirOptions, "iterate")) std.fs.cwd().openDir("examples", .{ .iterate = true }) else std.fs.cwd().openIterableDir("examples", .{}); // support zig 0.11 as well as current master
defer examplesDir.close();
const broken = switch (target.result.os.tag) {
.windows => &[_][]const u8{ "osm-viewer", "fade", "slide-viewer", "demo", "notepad", "dev-tools", "many-counters" },
else => &[_][]const u8{"many-counters"},
};
var walker = try examplesDir.walk(b.allocator);
defer walker.deinit();
while (try walker.next()) |entry| {
if (entry.kind == .file and std.mem.eql(u8, std.fs.path.extension(entry.path), ".zig")) {
const name = try std.mem.replaceOwned(u8, b.allocator, entry.path[0 .. entry.path.len - 4], std.fs.path.sep_str, "-");
defer b.allocator.free(name);
// it is not freed as the path is used later for building
const programPath = LazyPath.relative(b.pathJoin(&.{ "examples", entry.path }));
const exe: *std.Build.Step.Compile = if (target.result.isWasm())
b.addExecutable(.{ .name = name, .root_source_file = programPath, .target = target, .optimize = optimize })
else
b.addExecutable(.{ .name = name, .root_source_file = programPath, .target = target, .optimize = optimize });
const install_step = b.addInstallArtifact(exe, .{});
const working = blk: {
for (broken) |broken_name| {
if (std.mem.eql(u8, name, broken_name))
break :blk false;
}
break :blk true;
};
if (working) {
b.getInstallStep().dependOn(&install_step.step);
} else {
std.log.warn("'{s}' is broken (disabled by default)", .{name});
}
const run_cmd = try install(exe, .{});
const run_step = b.step(name, "Run this example");
run_step.dependOn(run_cmd);
}
}
const lib = b.addSharedLibrary(.{
.name = "capy",
.root_source_file = LazyPath.relative("src/c_api.zig"),
.version = std.SemanticVersion{ .major = 0, .minor = 4, .patch = 0 },
.target = target,
.optimize = optimize,
});
lib.linkLibC();
_ = try install(lib, .{});
// lib.emit_h = true;
const h_install = b.addInstallFile(lib.getEmittedH(), "headers");
b.getInstallStep().dependOn(&h_install.step);
const lib_install = b.addInstallArtifact(lib, .{});
b.getInstallStep().dependOn(&lib_install.step);
const buildc_step = b.step("shared", "Build capy as a shared library (with C ABI)");
buildc_step.dependOn(&lib_install.step);
const tests = b.addTest(.{
.root_source_file = LazyPath.relative("src/main.zig"),
.target = target,
.optimize = optimize,
});
const run_tests = try install(tests, .{});
const test_step = b.step("test", "Run unit tests and also generate the documentation");
test_step.dependOn(run_tests);
const docs = b.addTest(.{
.root_source_file = LazyPath.relative("src/main.zig"),
.target = target,
.optimize = optimize,
});
b.installDirectory(.{ .source_dir = docs.getEmittedDocs(), .install_dir = .{ .custom = "docs/" }, .install_subdir = "" });
const run_docs = try install(docs, .{});
const docs_step = b.step("docs", "Generate documentation and run unit tests");
docs_step.dependOn(run_docs);
const coverage_tests = b.addTest(.{
.root_source_file = LazyPath.relative("src/main.zig"),
.target = target,
.optimize = optimize,
});
coverage_tests.setExecCmd(&.{ "kcov", "--clean", "--include-pattern=src/", "kcov-output", null });
_ = try install(coverage_tests, .{});
const run_coverage_tests = b.addSystemCommand(&.{ "kcov", "--clean", "--include-pattern=src/", "kcov-output" });
run_coverage_tests.addArtifactArg(coverage_tests);
// const run_coverage_tests = b.addRunArtifact(coverage_tests);
// run_coverage_tests.has_side_effects = true;
const cov_step = b.step("coverage", "Perform code coverage of unit tests. This requires 'kcov' to be installed.");
cov_step.dependOn(&run_coverage_tests.step);
}