-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.rs
318 lines (281 loc) · 10.5 KB
/
build.rs
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright 2014 Johannes Köster.
// Licensed under the MIT license (http://opensource.org/licenses/MIT)
// This file may not be copied, modified, or distributed
// except according to those terms.
#[cfg(feature = "serde")]
use bindgen;
use fs_utils::copy::copy_directory;
use glob::glob;
use std::env;
use std::fs;
use std::io::Write;
use std::path::PathBuf;
// these need to be kept in sync with the htslib Makefile
const FILES: &[&str] = &[
"kfunc.c",
"kstring.c",
"bcf_sr_sort.c",
"bgzf.c",
"errmod.c",
"faidx.c",
"header.c",
"hfile.c",
"hts.c",
"hts_expr.c",
"hts_os.c",
"md5.c",
"multipart.c",
"probaln.c",
"realn.c",
"regidx.c",
"region.c",
"sam.c",
"sam_mods.c",
"synced_bcf_reader.c",
"vcf_sweep.c",
"tbx.c",
"textutils.c",
"thread_pool.c",
"vcf.c",
"vcfutils.c",
"cram/cram_codecs.c",
"cram/cram_decode.c",
"cram/cram_encode.c",
"cram/cram_external.c",
"cram/cram_index.c",
"cram/cram_io.c",
"cram/cram_stats.c",
"cram/mFILE.c",
"cram/open_trace_file.c",
"cram/pooled_alloc.c",
"cram/string_alloc.c",
"htscodecs/htscodecs/arith_dynamic.c",
"htscodecs/htscodecs/fqzcomp_qual.c",
"htscodecs/htscodecs/htscodecs.c",
"htscodecs/htscodecs/pack.c",
"htscodecs/htscodecs/rANS_static4x16pr.c",
"htscodecs/htscodecs/rANS_static32x16pr_avx2.c",
"htscodecs/htscodecs/rANS_static32x16pr_avx512.c",
"htscodecs/htscodecs/rANS_static32x16pr_sse4.c",
"htscodecs/htscodecs/rANS_static32x16pr_neon.c",
"htscodecs/htscodecs/rANS_static32x16pr.c",
"htscodecs/htscodecs/rANS_static.c",
"htscodecs/htscodecs/rle.c",
"htscodecs/htscodecs/tokenise_name3.c",
"htscodecs/htscodecs/utils.c",
];
fn main() {
let out = PathBuf::from(env::var("OUT_DIR").unwrap());
let htslib_copy = out.join("htslib");
if htslib_copy.exists() {
std::fs::remove_dir_all(htslib_copy).unwrap();
}
copy_directory("htslib", &out).unwrap();
// In build.rs cfg(target_os) does not give the target when cross-compiling;
// instead use the environment variable supplied by cargo, which does.
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
let mut cfg = cc::Build::new();
let mut lib_list = "".to_string();
// default files
let out_htslib = out.join("htslib");
let htslib = PathBuf::from("htslib");
for f in FILES {
let c_file = out_htslib.join(f);
cfg.file(&c_file);
println!("cargo:rerun-if-changed={}", htslib.join(f).display());
}
cfg.include(out.join("htslib"));
let want_static = cfg!(feature = "static") || env::var("HTS_STATIC").is_ok();
if want_static {
cfg.warnings(false).static_flag(true).pic(true);
} else {
cfg.warnings(false).static_flag(false).pic(true);
}
if let Ok(z_inc) = env::var("DEP_Z_INCLUDE") {
cfg.include(z_inc);
lib_list += " -lz";
}
// We build a config.h ourselves, rather than rely on Makefile or ./configure
let mut config_lines = vec![
"/* Default config.h generated by build.rs */",
"#define HAVE_DRAND48 1",
];
let use_bzip2 = env::var("CARGO_FEATURE_BZIP2").is_ok();
if use_bzip2 {
if let Ok(inc) = env::var("DEP_BZIP2_ROOT")
.map(PathBuf::from)
.map(|path| path.join("include"))
{
cfg.include(inc);
lib_list += " -lbz2";
config_lines.push("#define HAVE_LIBBZ2 1");
}
}
let use_libdeflate = env::var("CARGO_FEATURE_LIBDEFLATE").is_ok();
if use_libdeflate {
if let Ok(inc) = env::var("DEP_LIBDEFLATE_INCLUDE").map(PathBuf::from) {
cfg.include(inc);
lib_list += " -ldeflate";
config_lines.push("#define HAVE_LIBDEFLATE 1");
} else {
panic!("no DEP_LIBDEFLATE_INCLUDE");
}
}
let use_lzma = env::var("CARGO_FEATURE_LZMA").is_ok();
if use_lzma {
if let Ok(inc) = env::var("DEP_LZMA_INCLUDE").map(PathBuf::from) {
cfg.include(inc);
lib_list += " -llzma";
config_lines.push("#define HAVE_LIBBZ2 1");
config_lines.push("#ifndef __APPLE__");
config_lines.push("#define HAVE_LZMA_H 1");
config_lines.push("#endif");
}
}
let use_curl = env::var("CARGO_FEATURE_CURL").is_ok();
if use_curl {
if let Ok(inc) = env::var("DEP_CURL_INCLUDE").map(PathBuf::from) {
cfg.include(inc);
lib_list += " -lcurl";
config_lines.push("#define HAVE_LIBCURL 1");
cfg.file("htslib/hfile_libcurl.c");
println!("cargo:rerun-if-changed=htslib/hfile_libcurl.c");
if target_os == "macos" {
// Use builtin MacOS CommonCrypto HMAC
config_lines.push("#define HAVE_COMMONCRYPTO 1");
} else if let Ok(inc) = env::var("DEP_OPENSSL_INCLUDE").map(PathBuf::from) {
// Must use hmac from libcrypto in openssl
cfg.include(inc);
config_lines.push("#define HAVE_HMAC 1");
} else {
panic!("No OpenSSL dependency -- need OpenSSL includes");
}
}
}
let use_gcs = env::var("CARGO_FEATURE_GCS").is_ok();
if use_gcs {
config_lines.push("#define ENABLE_GCS 1");
cfg.file("htslib/hfile_gcs.c");
println!("cargo:rerun-if-changed=htslib/hfile_gcs.c");
}
let use_s3 = env::var("CARGO_FEATURE_S3").is_ok();
if use_s3 {
config_lines.push("#define ENABLE_S3 1");
cfg.file("htslib/hfile_s3.c");
println!("cargo:rerun-if-changed=htslib/hfile_s3.c");
cfg.file("htslib/hfile_s3_write.c");
println!("cargo:rerun-if-changed=htslib/hfile_s3_write.c");
}
// pass through target-feature flags to enable special instruction
// support in htscodecs build.
// TODO: add AVX512 support once Rust has it as a target feature.
if let Ok(targets) = env::var("CARGO_CFG_TARGET_FEATURE") {
if targets.contains("avx2") {
cfg.flag("-mavx2");
config_lines.push("#define HAVE_AVX2 1");
}
if targets.contains("sse4.1") {
cfg.flag("-msse4.1");
config_lines.push("#define HAVE_SSE4_1 1");
}
if targets.contains("ssse3") {
cfg.flag("-mssse3");
config_lines.push("#define HAVE_SSSE3 1");
}
if targets.contains("popcnt") {
cfg.flag("-mpopcnt");
config_lines.push("#define HAVE_POPCNT 1");
}
if targets.contains("neon") {
config_lines.push("#define __ARM_NEON 1");
}
}
// write out config.h which controls the options htslib will use
{
let mut f = std::fs::File::create(out.join("htslib").join("config.h")).unwrap();
for l in config_lines {
writeln!(&mut f, "{}", l).unwrap();
}
}
// write out version.h
{
let version = std::process::Command::new(out.join("htslib").join("version.sh"))
.output()
.expect("failed to execute process");
let version_str = std::str::from_utf8(&version.stdout).unwrap().trim();
let mut f = std::fs::File::create(out.join("htslib").join("version.h")).unwrap();
writeln!(&mut f, "#define HTS_VERSION_TEXT \"{}\"", version_str).unwrap();
}
// write out htscodecs/htscodecs/version.h
{
let mut f = std::fs::File::create(
out.join("htslib")
.join("htscodecs")
.join("htscodecs")
.join("version.h"),
)
.unwrap();
// FIXME, using a dummy. Not sure why this should be a separate version from htslib itself.
writeln!(&mut f, "#define HTSCODECS_VERSION_TEXT \"rust-htslib\"").unwrap();
}
// write out config_vars.h which is used to expose compiler parameters via
// hts_test_feature() in hts.c. We partially fill in these values.
{
let tool = cfg.get_compiler();
let mut f = std::fs::File::create(out.join("htslib").join("config_vars.h")).unwrap();
writeln!(&mut f, "#define HTS_CC {:?}", tool.cc_env()).unwrap();
writeln!(&mut f, "#define HTS_CPPFLAGS \"\"").unwrap();
writeln!(&mut f, "#define HTS_CFLAGS {:?}", tool.cflags_env()).unwrap();
writeln!(&mut f, "#define HTS_LDFLAGS \"\"").unwrap();
writeln!(&mut f, "#define HTS_LIBS \"{}\"", lib_list).unwrap();
}
cfg.file("wrapper.c");
cfg.compile("hts");
// If bindgen is enabled, use it
#[cfg(feature = "bindgen")]
{
bindgen::Builder::default()
.header("wrapper.h")
.generate_comments(false)
.blocklist_function("strtold")
.blocklist_type("max_align_t")
.generate()
.expect("Unable to generate bindings.")
.write_to_file(out.join("bindings.rs"))
.expect("Could not write bindings.");
}
// If no bindgen, use pre-built bindings
#[cfg(not(feature = "bindgen"))]
if target_os == "macos" {
fs::copy("osx_prebuilt_bindings.rs", out.join("bindings.rs"))
.expect("couldn't copy prebuilt bindings");
println!("cargo:rerun-if-changed=osx_prebuilt_bindings.rs");
} else {
fs::copy("linux_prebuilt_bindings.rs", out.join("bindings.rs"))
.expect("couldn't copy prebuilt bindings");
println!("cargo:rerun-if-changed=linux_prebuilt_bindings.rs");
}
let include = out.join("include");
fs::create_dir_all(&include).unwrap();
if include.join("htslib").exists() {
fs::remove_dir_all(include.join("htslib")).expect("remove exist include dir");
}
copy_directory(out.join("htslib").join("htslib"), &include).unwrap();
println!("cargo:root={}", out.display());
println!("cargo:include={}", include.display());
println!("cargo:libdir={}", out.display());
println!("cargo:rerun-if-changed=wrapper.c");
println!("cargo:rerun-if-changed=wrapper.h");
let globs = std::iter::empty()
.chain(glob("htslib/*.[h]").unwrap())
.chain(glob("htslib/cram/*.[h]").unwrap())
.chain(glob("htslib/htslib/*.h").unwrap())
.chain(glob("htslib/os/*.[h]").unwrap())
.filter_map(Result::ok);
for htsfile in globs {
println!("cargo:rerun-if-changed={}", htsfile.display());
}
// Note: config.h is a function of the cargo features. Any feature change will
// cause build.rs to re-run, so don't re-run on that change.
//println!("cargo:rerun-if-changed=htslib/config.h");
}