-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.zig
110 lines (96 loc) · 3.38 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
110
const std = @import("std");
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const build_options = b.addOptions();
build_options.step.name = "Zigrad build options";
const build_options_module = build_options.createModule();
build_options.addOption(
std.log.Level,
"log_level",
b.option(std.log.Level, "log_level", "The Log Level to be used.") orelse .info,
);
const zigrad = b.addModule("zigrad", .{
.root_source_file = b.path("src/zigrad.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "build_options", .module = build_options_module },
},
});
switch (target.result.os.tag) {
.linux => {
zigrad.linkSystemLibrary("blas", .{});
},
.macos => zigrad.linkFramework("Accelerate", .{}),
else => @panic("Os not supported."),
}
const tracy_enable = b.option(bool, "tracy_enable", "Enable profiling") orelse false;
const tracy = b.lazyDependency("tracy", .{
.target = target,
.optimize = optimize,
.tracy_enable = tracy_enable,
});
if (tracy_enable) zigrad.addImport("tracy", tracy.?.module("tracy"));
const lib = b.addStaticLibrary(.{
.name = "zigrad",
.root_source_file = zigrad.root_source_file.?,
.target = target,
.optimize = optimize,
});
link(target, lib);
if (tracy_enable) add_tracy(lib, tracy.?);
b.installArtifact(lib);
const exe = b.addExecutable(.{
.name = "zigrad",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("zigrad", zigrad);
link(target, exe);
if (tracy_enable) add_tracy(exe, tracy.?);
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
// Arg passthru (`zig build run -- arg1 arg2 etc`)
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
const unit_tests = b.addTest(.{
.root_source_file = b.path("src/zigrad.zig"),
.target = target,
.optimize = optimize,
});
const run_unit_tests = b.addRunArtifact(unit_tests);
link(target, unit_tests);
if (tracy_enable) add_tracy(unit_tests, tracy.?);
const test_step = b.step("test", "Run all tests");
test_step.dependOn(&run_unit_tests.step);
// doc gen
const docs_step = b.addInstallDirectory(.{
.source_dir = lib.getEmittedDocs(),
.install_dir = .prefix,
.install_subdir = "docs",
});
docs_step.step.dependOn(&exe.step);
const docs = b.step("docs", "Generate documentation");
docs.dependOn(&docs_step.step);
}
fn link(target: std.Build.ResolvedTarget, exe: *std.Build.Step.Compile) void {
switch (target.result.os.tag) {
.linux => {
exe.linkSystemLibrary("blas");
exe.linkLibC();
},
.macos => exe.linkFramework("Accelerate"),
else => @panic("Os not supported."),
}
}
fn add_tracy(exe: *std.Build.Step.Compile, tracy: *std.Build.Dependency) void {
exe.root_module.addImport("tracy", tracy.module("tracy"));
exe.linkLibrary(tracy.artifact("tracy"));
exe.linkLibCpp();
}