-
-
Notifications
You must be signed in to change notification settings - Fork 160
/
build.rs
1144 lines (1052 loc) · 35.8 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::{
borrow::Cow,
collections::HashSet,
env,
ffi::OsStr,
fs::{self, File},
io::{BufRead, BufReader},
iter::{self, FromIterator},
path::{Path, PathBuf},
process::Command,
};
use glob::glob;
use once_cell::sync::{Lazy, OnceCell};
use semver::{Version, VersionReq};
use shlex::Shlex;
#[cfg(feature = "buildtime-bindgen")]
mod generator {
use std::{
env,
ffi::OsStr,
fs::{self, DirEntry, File, OpenOptions},
io::{self, BufRead, BufReader, Write},
path::{Path, PathBuf},
process::Child,
sync::Arc,
thread,
time::Instant,
};
use glob::glob;
use super::{
file_copy_to_dir,
get_versioned_hub_dirs,
is_core_module,
MODULES,
OUT_DIR,
Result,
SRC_CPP_DIR,
SRC_DIR,
};
fn read_dir(path: &Path) -> Result<impl Iterator<Item=DirEntry>> {
Ok(path.read_dir()?.filter_map(|e| e.ok()))
}
fn copy_indent(mut read: impl BufRead, mut write: impl Write, indent: &str) -> Result<()> {
let mut line = Vec::with_capacity(100);
while read.read_until(b'\n', &mut line)? != 0 {
write.write(indent.as_bytes())?;
write.write(&line)?;
line.clear();
}
Ok(())
}
fn file_move_to_dir(src_file: &Path, target_dir: &Path) -> Result<PathBuf> {
if !target_dir.exists() {
fs::create_dir_all(&target_dir)?;
}
let src_filename = src_file.file_name()
.ok_or_else(|| "Can't calculate filename")?;
let target_file = target_dir.join(src_filename);
if fs::rename(&src_file, &target_file).is_err() {
fs::copy(&src_file, &target_file)?;
fs::remove_file(src_file)?;
}
Ok(target_file)
}
pub fn gen_wrapper(opencv_header_dir: &Path, generator_build: Option<Child>) -> Result<()> {
let out_dir_as_str = OUT_DIR.to_str().unwrap();
let (rust_hub_dir, cpp_hub_dir) = get_versioned_hub_dirs();
let module_dir = rust_hub_dir.join("hub");
let manual_dir = SRC_DIR.join("manual");
let opencv_dir = opencv_header_dir.join("opencv2");
eprintln!("=== Using OpenCV headers from: {}", opencv_dir.display());
eprintln!("=== Generating code in: {}", out_dir_as_str);
eprintln!("=== Placing generated bindings into: {}", rust_hub_dir.display());
let modules = MODULES.get().expect("MODULES not initialized");
for entry in read_dir(&OUT_DIR)? {
let path = entry.path();
if path.is_file() && path.extension().and_then(OsStr::to_str).map_or(true, |ext| !ext.eq_ignore_ascii_case("dll")) {
let _ = fs::remove_file(path);
}
}
let version = if cfg!(feature = "opencv-32") {
"3.2.0"
} else if cfg!(feature = "opencv-34") {
"3.4.10"
} else if cfg!(feature = "opencv-4") {
"4.3.0"
} else {
unreachable!();
};
let clang_stdlib_include_dir = Arc::new(env::var_os("OPENCV_CLANG_STDLIB_PATH")
.map(|p| PathBuf::from(p))
);
let num_jobs = env::var("NUM_JOBS").ok()
.and_then(|jobs| jobs.parse().ok())
.unwrap_or(2);
let job_server = jobserver::Client::new(num_jobs).expect("Can't create job server");
let mut join_handles = Vec::with_capacity(modules.len());
let start;
if cfg!(feature = "clang-runtime") {
let clang = clang::Clang::new().expect("Cannot initialize clang");
println!("=== Clang: {}", clang::get_version());
let gen = binding_generator::Generator::new(clang_stdlib_include_dir.as_deref(), &opencv_header_dir, &*SRC_CPP_DIR, clang);
eprintln!("=== Clang command line args: {:#?}", gen.build_clang_command_line_args());
start = Instant::now();
modules.iter().for_each(|module| {
let bindings_writer = binding_generator::writer::RustNativeBindingWriter::new(
&*SRC_CPP_DIR,
&*OUT_DIR,
&module,
version,
false,
);
gen.process_opencv_module(&module, bindings_writer);
println!("Generated: {}", module);
});
drop(generator_build); // fixme, prevent unused var warning
// fixme, https://github.com/twistedfall/opencv-rust/issues/145
// let status = generator_build.expect("Impossible").wait()?;
// if !status.success() {
// return Err("Failed to build the bindings generator".into());
// }
// let opencv_header_dir = Arc::new(opencv_header_dir.to_owned());
// start = Instant::now();
// modules.iter().for_each(|module| {
// let token = job_server.acquire().expect("Can't acquire token from job server");
// let join_handle = thread::spawn({
// let clang_stdlib_include_dir = Arc::clone(&clang_stdlib_include_dir);
// let opencv_header_dir = Arc::clone(&opencv_header_dir);
// move || {
// let clang_stdlib_include_dir = (*clang_stdlib_include_dir).as_ref()
// .and_then(|p| p.to_str())
// .unwrap_or("None");
// let mut bin_generator = std::process::Command::new(OUT_DIR.join("release/binding-generator"));
// bin_generator.arg(&*opencv_header_dir)
// .arg(&*SRC_CPP_DIR)
// .arg(&*OUT_DIR)
// .arg(&module)
// .arg(version)
// .arg(clang_stdlib_include_dir);
// let res = bin_generator.status().expect("Can't run bindings generator");
// if !res.success() {
// panic!("Failed to run the bindings generator");
// }
// println!("Generated: {}", module);
// drop(token); // needed to move the token to the thread
// }
// });
// join_handles.push(join_handle);
// });
} else {
let clang = clang::Clang::new().expect("Cannot initialize clang");
println!("=== Clang: {}", clang::get_version());
let gen = binding_generator::Generator::new(clang_stdlib_include_dir.as_deref(), &opencv_header_dir, &*SRC_CPP_DIR, clang);
eprintln!("=== Clang command line args: {:#?}", gen.build_clang_command_line_args());
let gen = Arc::new(gen);
start = Instant::now();
modules.iter().for_each(|module| {
let token = job_server.acquire().expect("Can't acquire token from job server");
let join_handle = thread::spawn({
let gen = Arc::clone(&gen);
move || {
let bindings_writer = binding_generator::writer::RustNativeBindingWriter::new(
&*SRC_CPP_DIR,
&*OUT_DIR,
&module,
version,
false,
);
gen.process_opencv_module(&module, bindings_writer);
println!("Generated: {}", module);
drop(token); // needed to move the token to the thread
}
});
join_handles.push(join_handle);
});
}
for join_handle in join_handles {
join_handle.join().expect("Can't join thread");
}
println!("Total binding generation time: {:?}", start.elapsed());
if !module_dir.exists() {
fs::create_dir_all(&module_dir)?;
}
for entry in read_dir(&module_dir)? {
let path = entry.path();
if path.extension().map_or(false, |e| e == "rs") {
let _ = fs::remove_file(path);
}
}
if !cpp_hub_dir.exists() {
fs::create_dir_all(&cpp_hub_dir)?;
}
for entry in read_dir(&cpp_hub_dir)? {
let path = entry.path();
if path.is_file() {
let _ = fs::remove_file(path);
}
}
for module in modules {
let module_cpp = OUT_DIR.join(format!("{}.cpp", module));
if module_cpp.is_file() {
file_copy_to_dir(&module_cpp, &cpp_hub_dir)?;
let module_types_cpp = OUT_DIR.join(format!("{}_types.hpp", module));
let mut module_types_file = OpenOptions::new().create(true).truncate(true).write(true).open(&module_types_cpp)?;
let mut type_files: Vec<PathBuf> = glob(&format!("{}/???-{}-*.type.cpp", out_dir_as_str, module))?
.collect::<Result<_, glob::GlobError>>()?;
type_files.sort_unstable();
for entry in type_files.into_iter() {
io::copy(&mut File::open(entry)?, &mut module_types_file)?;
}
file_copy_to_dir(&module_types_cpp, &cpp_hub_dir)?;
}
}
let add_manual = |file: &mut File, mod_name: &str| -> Result<bool> {
if manual_dir.join(format!("{}.rs", mod_name)).exists() {
writeln!(file, "pub use crate::manual::{}::*;", mod_name)?;
Ok(true)
} else {
Ok(false)
}
};
{
let mut hub_rs = File::create(rust_hub_dir.join("hub.rs"))?;
let mut types_rs = File::create(module_dir.join("types.rs"))?;
writeln!(&mut types_rs)?;
let mut sys_rs = File::create(module_dir.join("sys.rs"))?;
writeln!(&mut sys_rs, "use crate::{{mod_prelude_types::*, core}};")?;
writeln!(&mut sys_rs)?;
for module in modules {
let is_core_module = is_core_module(module.as_str());
let write_if_contrib = |write: &mut File| -> Result<()> {
if !is_core_module {
writeln!(write, r#"#[cfg(feature = "contrib")]"#)?;
}
Ok(())
};
// hub
write_if_contrib(&mut hub_rs)?;
writeln!(&mut hub_rs, "pub mod {};", module)?;
let module_filename = format!("{}.rs", module);
let target_file = file_move_to_dir(&OUT_DIR.join(&module_filename), &module_dir)?;
let mut f = OpenOptions::new().append(true).open(&target_file)?;
add_manual(&mut f, module)?;
// types
let mut write_header = true;
for entry in glob(&format!("{}/???-{}-*.type.rs", out_dir_as_str, module))? {
let entry = entry?;
if entry.metadata().map(|meta| meta.len()).unwrap_or(0) > 0 {
if write_header {
write_if_contrib(&mut types_rs)?;
writeln!(&mut types_rs, "mod {}_types {{", module)?;
writeln!(&mut types_rs, "\tuse crate::{{mod_prelude::*, core, types, sys}};")?;
writeln!(&mut types_rs)?;
write_header = false;
}
copy_indent(BufReader::new(File::open(&entry)?), &mut types_rs, "\t")?;
}
}
if !write_header {
writeln!(&mut types_rs, "}}")?;
write_if_contrib(&mut types_rs)?;
writeln!(&mut types_rs, "pub use {}_types::*;", module)?;
writeln!(&mut types_rs)?;
}
// sys
let path = OUT_DIR.join(format!("{}.externs.rs", module));
write_if_contrib(&mut sys_rs)?;
writeln!(&mut sys_rs, "mod {}_sys {{", module)?;
writeln!(&mut sys_rs, "\tuse super::*;")?;
writeln!(&mut sys_rs)?;
for entry in glob(&format!("{}/{}-*.rv.rs", out_dir_as_str, module))? {
let entry: PathBuf = entry?;
copy_indent(BufReader::new(File::open(entry)?), &mut sys_rs, "\t")?;
}
copy_indent(BufReader::new(File::open(&path)?), &mut sys_rs, "\t")?;
writeln!(&mut sys_rs, "}}")?;
write_if_contrib(&mut sys_rs)?;
writeln!(&mut sys_rs, "pub use {}_sys::*;", module)?;
writeln!(&mut sys_rs)?;
}
writeln!(&mut hub_rs, "pub mod types;")?;
writeln!(&mut hub_rs, "#[doc(hidden)]")?;
writeln!(&mut hub_rs, "pub mod sys;")?;
add_manual(&mut types_rs, "types")?;
add_manual(&mut sys_rs, "sys")?;
writeln!(&mut hub_rs, "pub mod hub_prelude {{")?;
for module in modules {
if !is_core_module(module.as_str()) {
writeln!(&mut hub_rs, r#" #[cfg(feature = "contrib")]"#)?;
}
writeln!(&mut hub_rs, r#" pub use super::{}::prelude::*;"#, module)?;
}
writeln!(&mut hub_rs, "}}")?;
}
Ok(())
}
}
type Result<T, E = Box<dyn std::error::Error>> = std::result::Result<T, E>;
static CORE_MODULES: Lazy<HashSet<&'static str>> = Lazy::new(|| HashSet::from_iter([
"calib3d",
"core",
#[cfg(not(feature = "opencv-32"))]
"dnn",
#[cfg(feature = "opencv-4")]
"dnn_superres",
"features2d",
"flann",
#[cfg(feature = "opencv-4")]
"gapi",
"highgui",
"imgcodecs",
"imgproc",
"ml",
"objdetect",
"photo",
#[cfg(any(feature = "opencv-32", feature = "opencv-34"))]
"shape",
"stitching",
#[cfg(any(feature = "opencv-32", feature = "opencv-34"))]
"superres",
"video",
"videoio",
#[cfg(any(feature = "opencv-32", feature = "opencv-34"))]
"videostab",
"viz",
].iter().copied()));
static DEBUG_MODULE: &str = "";
static MODULES: OnceCell<Vec<String>> = OnceCell::new();
static OUT_DIR: Lazy<PathBuf> = Lazy::new(|| PathBuf::from(env::var_os("OUT_DIR").expect("Can't read OUT_DIR env var")));
static MANIFEST_DIR: Lazy<PathBuf> = Lazy::new(|| PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").expect("Can't read CARGO_MANIFEST_DIR env var")));
static SRC_DIR: Lazy<PathBuf> = Lazy::new(|| MANIFEST_DIR.join("src"));
static SRC_CPP_DIR: Lazy<PathBuf> = Lazy::new(|| MANIFEST_DIR.join("src_cpp"));
static ENV_VARS: [&str; 15] = [
"OPENCV_HEADER_DIR",
"OPENCV_PACKAGE_NAME",
"OPENCV_PKGCONFIG_NAME",
"OPENCV_CMAKE_NAME",
"OPENCV_CMAKE_BIN",
"OPENCV_VCPKG_NAME",
"OPENCV_LINK_LIBS",
"OPENCV_LINK_PATHS",
"OPENCV_INCLUDE_PATHS",
"OPENCV_DISABLE_PROBES",
"OPENCV_CLANG_STDLIB_PATH",
"CMAKE_PREFIX_PATH",
"OpenCV_DIR",
"PKG_CONFIG_PATH",
"VCPKG_ROOT",
];
struct PackageName {}
impl PackageName {
pub fn env() -> Option<Cow<'static, str>> {
env::var("OPENCV_PACKAGE_NAME")
.ok()
.map(|x| x.into())
}
pub fn env_pkg_config() -> Option<Cow<'static, str>> {
env::var("OPENCV_PKGCONFIG_NAME")
.ok()
.map(|x| x.into())
}
pub fn env_cmake() -> Option<Cow<'static, str>> {
env::var("OPENCV_CMAKE_NAME")
.ok()
.map(|x| x.into())
}
pub fn env_vcpkg() -> Option<Cow<'static, str>> {
env::var("OPENCV_VCPKG_NAME")
.ok()
.map(|x| x.into())
}
pub fn pkg_config() -> Cow<'static, str> {
Self::env()
.or_else(Self::env_pkg_config)
.unwrap_or_else(|| if cfg!(feature = "opencv-32") || cfg!(feature = "opencv-34") {
"opencv".into()
} else if cfg!(feature = "opencv-4") {
"opencv4".into()
} else {
unreachable!("Feature flags should have been checked in main()");
})
}
pub fn cmake() -> Cow<'static, str> {
Self::env()
.or_else(Self::env_cmake)
.unwrap_or_else(|| "OpenCV".into())
}
pub fn vcpkg() -> Cow<'static, str> {
Self::env()
.or_else(Self::env_vcpkg)
.unwrap_or_else(|| if cfg!(feature = "opencv-32") || cfg!(feature = "opencv-34") {
"opencv3".into()
} else if cfg!(feature = "opencv-4") {
"opencv4".into()
} else {
unreachable!("Feature flags should have been checked in main()");
})
}
}
#[derive(Debug)]
struct Library {
pub include_paths: Vec<PathBuf>,
pub version: String,
pub cargo_metadata: Vec<String>,
}
impl Library {
fn strip_lib_file_decorations(path: &mut PathBuf) {
const LIB_EXTS: [&str; 7] = ["so", "a", "dll", "lib", "dylib", "framework", "tbd"];
// same, but with dots therearound
const LIB_EXTS_INNER: [&str; 7] = [".so.", ".a.", ".dll.", ".lib.", ".dylib.", ".framework.", ".tbd."];
if let Some(extension) = path.extension().and_then(OsStr::to_str) {
if LIB_EXTS.iter().any(|e| e.eq_ignore_ascii_case(extension)) {
path.set_extension("");
}
}
if let Some(mut file) = path.file_name().and_then(OsStr::to_str).map(str::to_owned) {
let orig_len = file.len();
const LIB_PREFIX: &str = "lib";
if file.starts_with(LIB_PREFIX) {
file.drain(..LIB_PREFIX.len());
}
LIB_EXTS_INNER.iter()
.for_each(|&inner_ext| if let Some(inner_ext_idx) = file.find(inner_ext) {
file.drain(inner_ext_idx..);
});
if orig_len != file.len() {
path.set_file_name(file);
}
}
}
fn process_library_list(libs: impl IntoIterator<Item=impl Into<PathBuf>>) -> impl Iterator<Item=String> {
libs.into_iter()
.map(|x| {
let mut path: PathBuf = x.into();
let is_framework = path.extension()
.and_then(OsStr::to_str)
.map_or(false, |e| e.eq_ignore_ascii_case("framework"));
Self::strip_lib_file_decorations(&mut path);
path.file_name()
.and_then(|f| f.to_str()
.map(|f| if is_framework {
format!("framework={}", f)
} else {
f.to_owned()
})
).expect("Invalid library name")
})
}
fn list(link_paths: &str) -> impl Iterator<Item=&str> {
link_paths.split(',')
.map(str::trim)
.filter(|&x| !x.is_empty())
}
fn version_from_include_paths(include_paths: impl Iterator<Item=impl AsRef<Path>>) -> Option<String> {
include_paths
.filter_map(|x| get_version_from_headers(x.as_ref()))
.next()
}
#[inline]
fn emit_link_search(path: &str, typ: Option<&str>) -> String {
format!("cargo:rustc-link-search={}{}", typ.map_or("".to_string(), |t| format!("{}=", t)), path)
}
#[inline]
fn emit_link_lib(path: &str, typ: Option<&str>) -> String {
format!("cargo:rustc-link-lib={}{}", typ.map_or("".to_string(), |t| format!("{}=", t)), path)
}
fn process_manual_link_search(cargo_metadata: &mut Vec<String>, link_paths: &str) {
cargo_metadata.extend(
Self::list(link_paths)
.map(|path| {
let out = iter::once(Self::emit_link_search(path, None));
#[cfg(target_os = "macos")] {
out.chain(
iter::once(Self::emit_link_search(path, Some("framework")))
)
}
#[cfg(not(target_os = "macos"))] {
out
}
})
.flatten()
);
}
fn process_manual_link_libs(cargo_metadata: &mut Vec<String>, link_libs: &str) {
cargo_metadata.extend(
Self::process_library_list(Self::list(&link_libs))
.map(|l| Self::emit_link_lib(&l, None))
);
}
pub fn probe_from_paths(include_paths: &str, link_paths: &str, link_libs: &str) -> Result<Self> {
eprintln!("=== Configuring OpenCV library from the environment:");
eprintln!("=== include_paths: {}", include_paths);
eprintln!("=== link_paths: {}", link_paths);
eprintln!("=== link_libs: {}", link_libs);
let mut cargo_metadata = Vec::with_capacity(64);
let include_paths: Vec<_> = Self::list(&include_paths)
.map(PathBuf::from)
.collect();
let version = Self::version_from_include_paths(include_paths.iter());
Self::process_manual_link_search(&mut cargo_metadata, link_paths);
Self::process_manual_link_libs(&mut cargo_metadata, link_libs);
Ok(Self {
include_paths,
version: version.unwrap_or_else(|| "0.0.0".to_owned()),
cargo_metadata,
})
}
pub fn probe_pkg_config(include_paths: Option<&str>, link_paths: Option<&str>, link_libs: Option<&str>) -> Result<Self> {
eprintln!("=== Probing OpenCV library using pkg_config");
let mut config = pkg_config::Config::new();
config.cargo_metadata(false);
let opencv = config.probe(&PackageName::pkg_config())?;
let mut cargo_metadata = Vec::with_capacity(64);
if let Some(link_paths) = link_paths {
Self::process_manual_link_search(&mut cargo_metadata, link_paths);
} else {
cargo_metadata.extend(
opencv.link_paths.into_iter()
.map(|link_path|
Self::emit_link_search(link_path.to_str().expect("Invalid link path"), Some("native"))
)
);
cargo_metadata.extend(
opencv.framework_paths.into_iter()
.map(|fw_path|
Self::emit_link_search(fw_path.to_str().expect("Invalid framework path"), Some("framework"))
)
);
}
if let Some(link_libs) = link_libs {
Self::process_manual_link_libs(&mut cargo_metadata, link_libs);
} else {
cargo_metadata.extend(
opencv.libs.into_iter()
.map(|lib| Self::emit_link_lib(&lib, None))
);
cargo_metadata.extend(
opencv.frameworks.into_iter()
.map(|fw| Self::emit_link_lib(&fw, Some("framework")))
);
}
let include_paths = include_paths.map_or(opencv.include_paths, |include_paths| {
Self::list(include_paths)
.map(PathBuf::from)
.collect()
});
Ok(Self {
include_paths,
version: opencv.version,
cargo_metadata,
})
}
pub fn probe_cmake(include_paths: Option<&str>, link_paths: Option<&str>, link_libs: Option<&str>) -> Result<Self> {
eprintln!("=== Probing OpenCV library using cmake");
let cmake_pkg = PackageName::cmake();
let cmake_bin = env::var_os("OPENCV_CMAKE_BIN").unwrap_or_else(|| "cmake".into());
let include_paths = include_paths
.map(|paths| Self::list(paths)
.map(PathBuf::from)
.collect::<Vec<_>>()
)
.ok_or_else(|| "Nobody is going to see that")
.or_else(|_| Command::new(&cmake_bin)
.current_dir(&*OUT_DIR)
.args(&[
"--find-package",
"-DCOMPILER_ID=GNU",
"-DLANGUAGE=CXX",
"-DMODE=COMPILE",
])
.arg(format!("-DNAME={}", cmake_pkg))
.output()
.map_err(Box::<dyn std::error::Error>::from)
.and_then(|output| {
if output.status.success() {
let mut include_paths = Vec::with_capacity(4);
let stdout = String::from_utf8(output.stdout)?;
eprintln!("=== cmake include arguments: {:#?}", stdout);
for mut arg in Shlex::new(stdout.trim()) {
const INCLUDE_PREFIX: &str = "-I";
if arg.starts_with(INCLUDE_PREFIX) {
arg.drain(..INCLUDE_PREFIX.len());
// todo possibly handle leading whitespace
include_paths.push(PathBuf::from(arg));
} else {
eprintln!("=== Unexpected cmake compile argument found: {}", arg);
}
}
Ok(include_paths)
} else {
Err(format!(
"cmake returned an error\n stdout: {:?}\n stderr: {:?}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
).into())
}
})
)?;
if let Some(version) = Self::version_from_include_paths(include_paths.iter()) {
let mut cargo_metadata = Vec::with_capacity(64);
link_paths.map(|link_paths| Self::process_manual_link_search(&mut cargo_metadata, link_paths));
link_libs.map(|link_libs| Self::process_manual_link_libs(&mut cargo_metadata, link_libs));
if link_paths.is_none() || link_libs.is_none() {
Command::new(&cmake_bin)
.current_dir(&*OUT_DIR)
.args(&[
"--find-package",
"-DCOMPILER_ID=GNU",
"-DLANGUAGE=CXX",
"-DMODE=LINK",
])
.arg(format!("-DNAME={}", cmake_pkg))
.output()
.map_err(Box::<dyn std::error::Error>::from)
.and_then(|output| if output.status.success() {
let mut cmake_link_paths = if link_paths.is_some() { HashSet::new() } else { HashSet::with_capacity(4) };
let stdout = String::from_utf8(output.stdout)?;
eprintln!("=== cmake link arguments: {:#?}", stdout);
for mut arg in Shlex::new(stdout.trim()) {
const RPATH_PREFIX: &str = "-Wl,-rpath,";
if arg.starts_with(RPATH_PREFIX) {
arg.drain(..RPATH_PREFIX.len());
cmake_link_paths.insert(PathBuf::from(arg));
} else if arg.starts_with("-") {
eprintln!("=== Unexpected cmake link argument found: {}", arg);
} else {
let mut path = PathBuf::from(arg);
if link_paths.is_none() {
if let Some(parent) = path.parent() {
cmake_link_paths.insert(parent.to_owned());
}
}
if link_libs.is_none() {
Self::strip_lib_file_decorations(&mut path);
if let Some(file) = path.file_name().and_then(OsStr::to_str) {
cargo_metadata.push(Self::emit_link_lib(file, None));
}
}
}
}
cargo_metadata.extend(
cmake_link_paths.into_iter()
.map(|link_path|
Self::emit_link_search(link_path.to_str().expect("Invalid link path"), Some("native"))
)
);
Ok(())
} else {
Err(format!(
"cmake returned an error\n stdout: {:?}\n stderr: {:?}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
).into())
})?
};
Ok(Self {
include_paths,
cargo_metadata,
version,
})
} else {
Err(format!("cmake discovery problem: OpenCV version not found in include paths: {:?}", include_paths).into())
}
}
pub fn probe_vcpkg() -> Result<Self> {
eprintln!("=== Probing OpenCV library using vcpkg");
let mut config = vcpkg::Config::new();
config.cargo_metadata(false);
let opencv = config.find_package(&PackageName::vcpkg())?;
let version = Self::version_from_include_paths(opencv.include_paths.iter());
Ok(Self {
include_paths: opencv.include_paths,
version: version.unwrap_or_else(|| "0.0.0".to_owned()),
cargo_metadata: opencv.cargo_metadata,
})
}
pub fn probe_system(include_paths: Option<&str>, link_paths: Option<&str>, link_libs: Option<&str>) -> Result<Self> {
let probe_pkg_config = || Self::probe_pkg_config(include_paths, link_paths, link_libs);
let probe_cmake = || Self::probe_cmake(include_paths, link_paths, link_libs);
let probe_vcpkg = || Self::probe_vcpkg();
let explicit_pkg_config = env::var_os("PKG_CONFIG_PATH").is_some() || env::var_os("OPENCV_PKGCONFIG_NAME").is_some();
let explicit_cmake = env::var_os("OpenCV_DIR").is_some()
|| env::var_os("OPENCV_CMAKE_NAME").is_some()
|| env::var_os("CMAKE_PREFIX_PATH").is_some()
|| env::var_os("OPENCV_CMAKE_BIN").is_some();
let explicit_vcpkg = env::var_os("VCPKG_ROOT").is_some() || cfg!(target_os = "windows");
let disabled_probes = env::var("OPENCV_DISABLE_PROBES");
let disabled_probes = disabled_probes.as_ref()
.map(|s| HashSet::from_iter(Self::list(s)))
.unwrap_or_else(|_| HashSet::new());
let mut probes = [
("pkg_config", &probe_pkg_config as &dyn Fn() -> Result<Self>),
("cmake", &probe_cmake),
("vcpkg", &probe_vcpkg),
];
if explicit_pkg_config {
if explicit_vcpkg {
probes.swap(1, 2);
}
} else if explicit_cmake {
probes.swap(0, 1);
if explicit_vcpkg {
probes.swap(1, 2);
}
} else if explicit_vcpkg {
probes.swap(1, 2);
probes.swap(0, 1);
}
let mut out = None;
for &(name, probe) in &probes {
if !disabled_probes.contains(name) {
match probe() {
Ok(lib) => {
match check_matching_version(&lib.version) {
Ok(..) => {
out = Some(lib);
break;
},
Err(e) => {
eprintln!("=== Wrong version: {} using {}, continuing: {:#?}", e, name, lib);
}
}
}
Err(e) => {
eprintln!("=== Can't probe using {}, continuing with other methods, error: {}", name, e);
}
}
} else {
eprintln!("=== Skipping probe {} because of the environment configuration", name);
}
}
out.ok_or_else(|| {
let methods = probes.iter()
.map(|&(name, _)| name)
.filter(|&name| !disabled_probes.contains(name))
.collect::<Vec<_>>()
.join(", ");
format!("Failed to find OpenCV package using probes: {}", methods).into()
})
}
pub fn probe() -> Result<Self> {
let include_paths = env::var("OPENCV_INCLUDE_PATHS").ok();
let link_paths = env::var("OPENCV_LINK_PATHS").ok();
let link_libs = env::var("OPENCV_LINK_LIBS").ok();
if let (Some(include_paths), Some(link_paths), Some(link_libs)) = (&include_paths, &link_paths, &link_libs) {
Self::probe_from_paths(include_paths, link_paths, link_libs)
} else {
Self::probe_system(include_paths.as_deref(), link_paths.as_deref(), link_libs.as_deref())
}
}
pub fn emit_cargo_metadata(&self) {
self.cargo_metadata.iter().for_each(|meta| {
println!("{}", meta);
});
}
}
fn file_copy_to_dir(src_file: &Path, target_dir: &Path) -> Result<PathBuf> {
if !target_dir.exists() {
fs::create_dir_all(&target_dir)?;
}
let src_filename = src_file.file_name()
.ok_or_else(|| "Can't calculate filename")?;
let target_file = target_dir.join(src_filename);
fs::copy(&src_file, &target_file)?;
Ok(target_file)
}
fn get_version_from_headers(header_dir: &Path) -> Option<String> {
let version_hpp = {
let out = header_dir.join("opencv2/core/version.hpp");
if out.is_file() {
out
} else {
let out = header_dir.join("Headers/core/version.hpp");
if out.is_file() {
out
} else {
return None;
}
}
};
let mut major = None;
let mut minor = None;
let mut revision = None;
let mut line = String::with_capacity(256);
let mut reader = BufReader::new(File::open(version_hpp).ok()?);
while let Ok(bytes_read) = reader.read_line(&mut line) {
if bytes_read == 0 {
break;
}
const PREFIX: &str = "#define CV_VERSION_";
if line.starts_with(PREFIX) {
let mut parts = line[PREFIX.len()..].split_whitespace();
if let (Some(ver_spec), Some(version)) = (parts.next(), parts.next()) {
match ver_spec {
"MAJOR" => {
major = Some(version.to_string());
}
"MINOR" => {
minor = Some(version.to_string());
}
"REVISION" => {
revision = Some(version.to_string());
}
_ => {}
}
}
if major.is_some() && minor.is_some() && revision.is_some() {
break;
}
}
line.clear();
}
if let (Some(major), Some(minor), Some(revision)) = (major, minor, revision) {
Some(format!("{}.{}.{}", major, minor, revision))
} else {
Some("0.0.0".to_string())
}
}
fn check_matching_version(version: &str) -> Result<()> {
#![allow(clippy::ifs_same_cond)] // false trigger
if cfg!(feature = "opencv-32") && !VersionReq::parse("~3.2")?.matches(&Version::parse(version)?) {
Err(format!("OpenCV version: {} must be from 3.2 branch because of the feature: opencv-32", version).into())
} else if cfg!(feature = "opencv-34") && !VersionReq::parse("~3.4")?.matches(&Version::parse(version)?) {
Err(format!("OpenCV version: {} must be from 3.4 branch because of the feature: opencv-34", version).into())
} else if cfg!(feature = "opencv-4") && !VersionReq::parse("~4")?.matches(&Version::parse(version)?) {
Err(format!("OpenCV version: {} must be from 4.x branch because of the feature: opencv-4", version).into())
} else {
Ok(())
}
}
fn get_versioned_hub_dirs() -> (PathBuf, PathBuf) {
let bindings_dir = MANIFEST_DIR.join("bindings");
let mut rust_hub_dir = bindings_dir.join("rust");
let mut cpp_hub_dir = bindings_dir.join("cpp");
if cfg!(feature = "opencv-32") {
rust_hub_dir.push("opencv_32");
cpp_hub_dir.push("opencv_32");
} else if cfg!(feature = "opencv-34") {
rust_hub_dir.push("opencv_34");
cpp_hub_dir.push("opencv_34");
} else if cfg!(feature = "opencv-4") {
rust_hub_dir.push("opencv_4");
cpp_hub_dir.push("opencv_4");
}
(rust_hub_dir, cpp_hub_dir)
}
fn make_modules(opencv_dir_as_string: &str) -> Result<()> {
if !DEBUG_MODULE.is_empty() {
MODULES.set(vec![DEBUG_MODULE.to_string()]).expect("Can't set debug MODULES cache");
return Ok(())
}
let ignore_modules: HashSet<&'static str> = HashSet::from_iter([
"core_detect",
"cudalegacy",
"cudev",
"gapi",
"opencv",
"opencv_modules",
].iter().copied());
let modules: Vec<String> = glob(&format!("{}/*.hpp", opencv_dir_as_string))?
.filter_map(|entry| {
let entry = entry.expect("Can't get path for module file");
let module = entry.file_stem()
.and_then(OsStr::to_str).expect("Can't calculate file stem");
if ignore_modules.contains(module) {
None
} else {
Some(module.to_string())
}
})
.collect();
MODULES.set(modules).expect("Can't set MODULES cache");
Ok(())
}
fn is_core_module(module: &str) -> bool {
CORE_MODULES.contains(module)
}
fn build_compiler(opencv: &Library) -> cc::Build {
let mut out = cc::Build::new();
out.cpp(true)
.include(&*SRC_CPP_DIR)
.include(&*OUT_DIR)
.include(".")
.flag_if_supported("-Wno-class-memaccess")
.flag_if_supported("-Wno-deprecated-declarations")
.flag_if_supported("-Wno-deprecated-copy")
.flag_if_supported("-Wno-unused-variable")
.flag_if_supported("-Wno-return-type-c-linkage")
;
opencv.include_paths.iter().for_each(|p| { out.include(p); });
if cfg!(target_env = "msvc") {
out.flag_if_supported("-std:c++latest")
.flag_if_supported("-wd4996")
.flag_if_supported("-wd5054") // deprecated between enumerations of different types
.flag_if_supported("-wd4190") // has C-linkage specified, but returns UDT 'Result<cv::Rect_<int>>' which is incompatible with C
.flag_if_supported("-EHsc")
.flag_if_supported("-bigobj")
;
} else {
out.flag("-std=c++11")
.flag_if_supported("-Wa,-mbig-obj")
;
}
out
}
fn build_wrapper(opencv: &Library) -> Result<()> {
for &v in ENV_VARS.iter() {
println!("cargo:rerun-if-env-changed={}", v);
}
let include_exts = &[OsStr::new("cpp"), OsStr::new("hpp")];
for entry in SRC_CPP_DIR.read_dir()?.map(|e| e.unwrap()) {
let path = entry.path();
if path.is_file() && path.extension().map_or(false, |e| include_exts.contains(&e)) {
if let Some(path) = path.to_str() {
println!("cargo:rerun-if-changed={}", path);
}
}
}