-
Notifications
You must be signed in to change notification settings - Fork 13
/
build.rs
238 lines (185 loc) · 6.59 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
use std::env;
use std::path::PathBuf;
// Configure for minimap2
fn configure(mut cc: &mut cc::Build) {
println!("cargo:rerun-if-changed=minimap2/*.c");
cc.include("minimap2");
cc.opt_level(2);
#[cfg(feature = "sse2only")]
sse2only(&mut cc);
#[cfg(feature = "simde")]
simde(&mut cc);
cc.include("minimap2/");
let files: Vec<_> = std::fs::read_dir("minimap2")
.unwrap()
.map(|f| f.unwrap().path())
.collect();
assert!(files.len() != 0, "No files found in minimap2 directory -- Did you forget to clone the submodule? git submodule init --recursive");
for file in files {
// Skip "main.c" and "example.c"
if file.file_name().unwrap() == "main.c" || file.file_name().unwrap() == "example.c" {
continue;
}
// Ignore all "neon"
if file.file_name().unwrap().to_str().unwrap().contains("neon") {
continue;
}
// Ignore all "ksw"
if file.file_name().unwrap().to_str().unwrap().contains("ksw") {
continue;
}
if let Some(x) = file.extension() {
if x == "c" {
println!("Compiling: {:?}", file);
cc.file(file);
}
}
}
cc.file("minimap2/ksw2_ll_sse.c");
#[cfg(not(feature = "noopt"))]
target_specific(&mut cc);
}
fn target_specific(cc: &mut cc::Build) {
// let host = env::var("HOST").unwrap();
let target = env::var("TARGET").unwrap();
if target.contains("aarch64") | target.contains("arm") {
cc.include("minimap2/sse2neon/");
// For aarch64 targets with neon
// Add the following files:
// ksw2_extz2_neon.o ksw2_extd2_neon.o ksw2_exts2_neon.o
cc.file("minimap2/ksw2_extz2_sse.c");
cc.file("minimap2/ksw2_extd2_sse.c");
cc.file("minimap2/ksw2_exts2_sse.c");
cc.flag("-DKSW_SSE2_ONLY");
// CFLAGS+=-D_FILE_OFFSET_BITS=64 -fsigned-char -Isse2neon -D__SSE2__
cc.flag("-D_FILE_OFFSET_BITS=64");
cc.flag("-fsigned-char");
cc.flag("-Isse2neon");
cc.flag("-D__SSE2__");
} else if target.contains("x86_64") {
#[cfg(all(
target_feature = "sse4.1",
not(feature = "simde"),
not(feature = "sse2only")
))]
cc.flag("-msse4.1");
if target.contains("aarch64") {
cc.include("minimap2/sse2neon/");
// Include appropriate NEON files
cc.file("minimap2/ksw2_extz2_neon.c");
cc.file("minimap2/ksw2_extd2_neon.c");
cc.file("minimap2/ksw2_exts2_neon.c");
cc.flag("-D_FILE_OFFSET_BITS=64");
cc.flag("-fsigned-char");
cc.flag("-Isse2neon");
cc.flag("-D__SSE2__");
}
#[cfg(all(not(target_feature = "sse4.1"), target_feature = "sse2",))]
{
cc.flag("-msse2");
}
#[cfg(all(not(target_feature = "sse4.1"), target_feature = "sse2"))]
cc.flag("-DKSW_SSE2_ONLY");
// #[cfg(all(not(target_feature = "sse4.1"), target_feature = "sse2"))]
// cc.flag("-DKSW_CPU_DISPATCH");
#[cfg(all(not(target_feature = "sse4.1"), target_feature = "sse2",))]
cc.flag("-mno-sse4.1");
// OBJS+=ksw2_extz2_sse41.o ksw2_extd2_sse41.o ksw2_exts2_sse41.o ksw2_extz2_sse2.o ksw2_extd2_sse2.o ksw2_exts2_sse2.o ksw2_dispatch.o
cc.file("minimap2/ksw2_extz2_sse.c");
cc.file("minimap2/ksw2_extd2_sse.c");
cc.file("minimap2/ksw2_exts2_sse.c");
// cc.file("minimap2/ksw2_dispatch.c");
}
}
#[cfg(feature = "simde")]
fn simde(cc: &mut cc::Build) {
cc.include("minimap2/lib/simde");
cc.flag("-DSIMDE_ENABLE_NATIVE_ALIASES");
cc.flag("-DUSE_SIMDE");
cc.flag("-std=c99");
}
fn compile() {
let mut cc = cc::Build::new();
let out_path = PathBuf::from(env::var_os("OUT_DIR").unwrap());
let _host = env::var("HOST").unwrap();
let _target = env::var("TARGET").unwrap();
println!("{}", _target);
println!("cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR");
println!("cargo:rustc-link-lib=m");
println!("cargo:rustc-link-lib=z");
if !env::var("TARGET").unwrap().contains("android") {
println!("cargo:rustc-link-lib=pthread");
}
if !env::var("TARGET").unwrap().contains("android") {
cc.flag("-lpthread");
}
cc.flag("-lm");
cc.flag("-lz");
let mut cc = cc::Build::new();
cc.warnings(false);
cc.flag("-Wc++-compat");
cc.out_dir(&out_path);
configure(&mut cc);
cc.flag("-DHAVE_KALLOC");
#[cfg(feature = "static")]
cc.static_flag(true);
println!("cargo:rustc-cfg=link_libz");
if let Some(include) = std::env::var_os("DEP_Z_INCLUDE") {
println!("-------------FOUND ZLIB INCLUDE: {:?}", include);
cc.include(include);
// Use env DEP_Z_ROOT to find the library
if let Some(lib) = std::env::var_os("DEP_Z_ROOT") {
let lib = lib.to_str().unwrap();
println!("cargo:rustc-link-search=native={}", lib);
println!("cargo:rustc-link-lib=static=z");
}
}
// Debugging, print out the entire environment
for (key, value) in std::env::vars() {
println!("{}: {}", key, value);
}
if let Ok(lib) = pkg_config::find_library("zlib") {
for path in &lib.include_paths {
cc.include(path);
}
}
cc.compile("libminimap");
}
#[cfg(feature = "sse2only")]
fn sse2only(cc: &mut cc::Build) {
#[cfg(all(target_feature = "sse2", not(target_feature = "sse4.1")))]
cc.flag("-DKSW_SSE2_ONLY");
#[cfg(all(target_feature = "sse2", not(target_feature = "sse4.1")))]
cc.flag("-mno-sse4.1");
let target = env::var("TARGET").unwrap_or_default();
if target.contains("x86_64") {
#[cfg(all(
not(target_feature = "sse4.1"),
target_feature = "sse2",
not(target_arch = "aarch64")
))]
{
cc.flag("-msse2");
}
}
}
#[cfg(feature = "bindgen")]
fn gen_bindings() {
let out_path = PathBuf::from(env::var_os("OUT_DIR").unwrap());
let mut bindgen = bindgen::Builder::default()
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.rustfmt_bindings(true);
let mut bindgen = bindgen.header("minimap2.h");
bindgen
.generate_cstr(true)
.generate()
.expect("Couldn't write bindings!")
.write_to_file(out_path.join("bindings.rs"))
.expect("Unable to create bindings");
}
#[cfg(not(feature = "bindgen"))]
fn gen_bindings() {}
fn main() {
compile();
gen_bindings();
}