forked from washingtonpost/FastFEC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
125 lines (116 loc) · 4.35 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const std = @import("std");
const builtin = @import("builtin");
const CrossTarget = std.zig.CrossTarget;
pub fn linkPcre(vendored_pcre: bool, libExe: *std.build.LibExeObjStep) void {
if (vendored_pcre) {
libExe.addCSourceFiles(&pcreSources, &buildOptions);
} else {
if (builtin.os.tag == .windows) {
libExe.linkSystemLibrary("pcre");
} else {
libExe.linkSystemLibrary("libpcre");
}
}
}
pub fn build(b: *std.build.Builder) !void {
b.setPreferredReleaseMode(.ReleaseFast);
const target = b.standardTargetOptions(.{});
const mode = b.standardReleaseOptions();
const lib_only: bool = b.option(bool, "lib-only", "Only compile the library") orelse false;
const skip_lib: bool = b.option(bool, "skip-lib", "Skip compiling the library") orelse false;
const wasm: bool = b.option(bool, "wasm", "Compile the wasm library") orelse false;
const vendored_pcre: bool = b.option(bool, "vendored-pcre", "Use vendored pcre") orelse true;
// Main build step
if (!lib_only and !wasm) {
const fastfec_cli = b.addExecutable("fastfec", null);
fastfec_cli.setTarget(target);
fastfec_cli.setBuildMode(mode);
fastfec_cli.install();
fastfec_cli.linkLibC();
fastfec_cli.addCSourceFiles(&libSources, &buildOptions);
linkPcre(vendored_pcre, fastfec_cli);
fastfec_cli.addCSourceFiles(&.{
"src/cli.c",
"src/main.c",
}, &buildOptions);
}
if (!wasm and !skip_lib) {
// Library build step
const fastfec_lib = b.addSharedLibrary("fastfec", null, .unversioned);
fastfec_lib.setTarget(target);
fastfec_lib.setBuildMode(mode);
fastfec_lib.install();
fastfec_lib.linkLibC();
fastfec_lib.addCSourceFiles(&libSources, &buildOptions);
linkPcre(vendored_pcre, fastfec_lib);
} else if (wasm) {
// Wasm library build step
const fastfec_wasm = b.addSharedLibrary("fastfec", null, .unversioned);
const wasm_target = CrossTarget{ .cpu_arch = .wasm32, .os_tag = .wasi };
fastfec_wasm.setTarget(wasm_target);
fastfec_wasm.setBuildMode(mode);
fastfec_wasm.install();
fastfec_wasm.linkLibC();
fastfec_wasm.addCSourceFiles(&libSources, &buildOptions);
linkPcre(vendored_pcre, fastfec_wasm);
fastfec_wasm.addCSourceFile("src/wasm.c", &buildOptions);
}
// Test step
var prev_test_step: ?*std.build.Step = null;
for (tests) |test_file| {
const base_file = std.fs.path.basename(test_file);
const subtest_exe = b.addExecutable(base_file, null);
subtest_exe.linkLibC();
subtest_exe.addCSourceFiles(&testIncludes, &buildOptions);
linkPcre(vendored_pcre, subtest_exe);
subtest_exe.addCSourceFile(test_file, &buildOptions);
const subtest_cmd = subtest_exe.run();
if (prev_test_step != null) {
subtest_cmd.step.dependOn(prev_test_step.?);
}
prev_test_step = &subtest_cmd.step;
}
const test_steps = prev_test_step.?;
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(test_steps);
}
const libSources = [_][]const u8{
"src/buffer.c",
"src/memory.c",
"src/encoding.c",
"src/csv.c",
"src/writer.c",
"src/fec.c",
};
const pcreSources = [_][]const u8{
"src/pcre/pcre_chartables.c",
"src/pcre/pcre_byte_order.c",
"src/pcre/pcre_compile.c",
"src/pcre/pcre_config.c",
"src/pcre/pcre_dfa_exec.c",
"src/pcre/pcre_exec.c",
"src/pcre/pcre_fullinfo.c",
"src/pcre/pcre_get.c",
"src/pcre/pcre_globals.c",
"src/pcre/pcre_jit_compile.c",
"src/pcre/pcre_maketables.c",
"src/pcre/pcre_newline.c",
"src/pcre/pcre_ord2utf8.c",
"src/pcre/pcre_refcount.c",
"src/pcre/pcre_string_utils.c",
"src/pcre/pcre_study.c",
"src/pcre/pcre_tables.c",
"src/pcre/pcre_ucd.c",
"src/pcre/pcre_valid_utf8.c",
"src/pcre/pcre_version.c",
"src/pcre/pcre_xclass.c",
};
const tests = [_][]const u8{ "src/buffer_test.c", "src/csv_test.c", "src/writer_test.c", "src/cli_test.c" };
const testIncludes = [_][]const u8{ "src/buffer.c", "src/memory.c", "src/encoding.c", "src/csv.c", "src/writer.c", "src/cli.c" };
const buildOptions = [_][]const u8{
"-std=c11",
"-pedantic",
"-Wall",
"-W",
"-Wno-missing-field-initializers",
};