-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.zig
193 lines (167 loc) · 7.2 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const std = @import("std");
const builtin = @import("builtin");
pub fn generateHeaders(b: *std.Build, build_path: *[]const u8) void {
var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
std.mem.copyForwards(u8, buf[0..], build_path.*);
std.debug.print("Saving headers in {s}\n", .{buf[0..build_path.len]});
const popcnt = if (std.Target.x86.featureSetHas(builtin.cpu.features, .popcnt)) "1" else "0";
const sse4 = if (std.Target.x86.featureSetHas(builtin.cpu.features, .sse4_2)) "1" else "0";
const ssse3 = if (std.Target.x86.featureSetHas(builtin.cpu.features, .ssse3)) "1" else "0";
const avx2 = if (std.Target.x86.featureSetHas(builtin.cpu.features, .avx2)) "1" else "0";
const avx512 = if (std.Target.x86.featureSetHas(builtin.cpu.features, .avx512f)) "1" else "0";
//config.h
var hdr_path: []const u8 = "/config.h";
std.mem.copyForwards(u8, buf[build_path.len..], hdr_path);
var genConfigFile = std.fs.createFileAbsolute(buf[0..build_path.len + hdr_path.len], .{ .truncate = true }) catch unreachable;
defer genConfigFile.close();
const writer = genConfigFile.writer();
writer.print(
\\ /* Default config.h generated by build.zig */
\\#define HAVE_LIBBZ2 1
\\#define HAVE_LIBLZMA 1
\\#ifndef __APPLE__
\\#define HAVE_LZMA_H 1
\\#endif
\\#define HAVE_DRAND48 1
\\#define HAVE_LIBCURL 1
\\#define HAVE_POPCNT {s}
\\#define HAVE_SSE4_1 {s}
\\#define HAVE_SSSE3 {s}
\\#define HAVE_AVX2 {s}
\\#define HAVE_AVX512 {s}
\\#define UBSAN 0
, .{popcnt, sse4, ssse3, avx2, avx512}) catch unreachable;
//config_vars.h
hdr_path = "/config_vars.h";
std.mem.copyForwards(u8, buf[build_path.len..], hdr_path);
var genVarsFile = std.fs.createFileAbsolute(buf[0..build_path.len + hdr_path.len], .{ .truncate = true }) catch unreachable;
const vars_writer = genVarsFile.writer();
const CPPFLAGS: []const u8 = b.graph.env_map.get("CPPFLAGS") orelse "";
const CFLAGS: []const u8 = b.graph.env_map.get("CFLAGS") orelse "";
const LDFLAGS: []const u8 = b.graph.env_map.get("LDFLAGS") orelse "";
const LIBS: []const u8 = b.graph.env_map.get("LIBS") orelse "";
vars_writer.print(
\\#define HTS_CC "zig cc"
\\#define HTS_CPPFLAGS "{s}"
\\#define HTS_CFLAGS "{s} -g -Wall -O2 -fvisibility=hidden -fpic"
\\#define HTS_LDFLAGS "{s} -fvisibility=hidden "
\\#define HTS_LIBS "{s} -lz -lm -lbz2 -llzma -lcurl -lpthread"
, .{CPPFLAGS, CFLAGS, LDFLAGS, LIBS}) catch unreachable;
// version.h
const hts_version = "1.19.1";
hdr_path = "/version.h";
std.mem.copyForwards(u8, buf[build_path.len..], hdr_path);
var genVerFile = std.fs.createFileAbsolute(buf[0..build_path.len + hdr_path.len], .{ .truncate = true }) catch unreachable;
const ver_writer = genVerFile.writer();
ver_writer.print(
\\#define HTS_VERSION_TEXT "{s}"
, .{hts_version}) catch unreachable;
}
pub fn build(b: *std.Build) void {
const LIBHTS_SOVERSION = 3;
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{ .preferred_optimize_mode = std.builtin.OptimizeMode.ReleaseFast });
var arena_state = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena_state.deinit();
const arena = arena_state.allocator();
var cflags = std.ArrayList([]const u8).init(arena);
defer cflags.deinit();
cflags.appendSlice(&.{
"-g", "-Wall", "-O2", "-fvisibility=hidden", "-fpic", "-D_XOPEN_SOURCE=700"
}) catch unreachable;
// Platform specific build options. These dont seem to do anything at the moment?
const target_os = target.query.os_tag;
if (target_os == .macos) {
std.debug.print("Building for macOS\n", .{});
cflags.append("-dynamiclib") catch unreachable;
} else if (target_os == .linux) {
std.debug.print("Building for Linux\n", .{});
cflags.append("-shared") catch unreachable;
cflags.append("-Wl") catch unreachable;
const soname = std.fmt.allocPrint(arena, "-Wl,-soname,libhts.so.{}", .{LIBHTS_SOVERSION}) catch unreachable;
defer arena.free(soname);
cflags.append(soname) catch unreachable;
}
const htslib = b.addStaticLibrary(.{
// const htslib = b.addSharedLibrary(.{
.name = "hts",
.target = target,
.optimize = optimize,
});
const src_files = &[_][]const u8{
"htslib/kfunc.c",
"htslib/kstring.c",
"htslib/bcf_sr_sort.c",
"htslib/bgzf.c",
"htslib/errmod.c",
"htslib/faidx.c",
"htslib/header.c",
"htslib/hfile.c",
"htslib/hts.c",
"htslib/hts_expr.c",
"htslib/hts_os.c",
"htslib/md5.c",
"htslib/multipart.c",
"htslib/probaln.c",
"htslib/realn.c",
"htslib/regidx.c",
"htslib/region.c",
"htslib/sam.c",
"htslib/sam_mods.c",
"htslib/synced_bcf_reader.c",
"htslib/vcf_sweep.c",
"htslib/tbx.c",
"htslib/textutils.c",
"htslib/thread_pool.c",
"htslib/vcf.c",
"htslib/vcfutils.c",
"htslib/cram/cram_codecs.c",
"htslib/cram/cram_decode.c",
"htslib/cram/cram_encode.c",
"htslib/cram/cram_external.c",
"htslib/cram/cram_index.c",
"htslib/cram/cram_io.c",
"htslib/cram/cram_stats.c",
"htslib/cram/mFILE.c",
"htslib/cram/open_trace_file.c",
"htslib/cram/pooled_alloc.c",
"htslib/cram/string_alloc.c",
"htslib/htscodecs/htscodecs/arith_dynamic.c",
"htslib/htscodecs/htscodecs/fqzcomp_qual.c",
"htslib/htscodecs/htscodecs/htscodecs.c",
"htslib/htscodecs/htscodecs/pack.c",
"htslib/htscodecs/htscodecs/rANS_static4x16pr.c",
"htslib/htscodecs/htscodecs/rANS_static32x16pr_avx2.c",
"htslib/htscodecs/htscodecs/rANS_static32x16pr_avx512.c",
"htslib/htscodecs/htscodecs/rANS_static32x16pr_sse4.c",
"htslib/htscodecs/htscodecs/rANS_static32x16pr_neon.c",
"htslib/htscodecs/htscodecs/rANS_static32x16pr.c",
"htslib/htscodecs/htscodecs/rANS_static.c",
"htslib/htscodecs/htscodecs/rle.c",
"htslib/htscodecs/htscodecs/tokenise_name3.c",
"htslib/htscodecs/htscodecs/utils.c",
"htslib/hfile_libcurl.c",
};
for (src_files) |file| {
htslib.addCSourceFile(.{
.file = .{.path = file},
.flags = cflags.items,
});
}
var self_exe_dir_path: []const u8 = std.fs.selfExeDirPathAlloc(arena) catch unreachable;
htslib.addIncludePath(.{ .path = self_exe_dir_path });
generateHeaders(b, &self_exe_dir_path);
htslib.addIncludePath(.{ .path = "htslib" });
htslib.addIncludePath(.{ .path = "htslib/cram" });
htslib.addIncludePath(.{ .path = "htslib/htscodecs/htscodecs" });
htslib.addIncludePath(.{ .path = "htslib/htslib" });
htslib.addIncludePath(.{ .path = "htslib/os" });
htslib.linkLibC();
const libz_dep = b.dependency("zlib", .{.target = target, .optimize = optimize});
htslib.linkLibrary(libz_dep.artifact("z"));
htslib.linkSystemLibrary("bz2");
htslib.linkSystemLibrary("lzma");
htslib.linkSystemLibrary("curl");
b.default_step.dependOn(&htslib.step);
b.installArtifact(htslib);
}