forked from peterbraden/node-opencv
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfind-opencv.js
81 lines (71 loc) · 2.44 KB
/
find-opencv.js
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
"use strict";
var exec = require("child_process").exec;
var fs = require("fs");
var flag = process.argv[2] || "--exists";
// Normally |pkg-config opencv ...| could report either OpenCV 2.x or OpenCV 3.y
// depending on what is installed. To enable both 2.x and 3.y to co-exist on
// the same machine, the opencv.pc for 3.y can be installed as opencv3.pc and
// then selected by |export PKG_CONFIG_OPENCV3=1| before building node-opencv.
var opencv = process.env.PKG_CONFIG_OPENCV3 === "1" ? "opencv3" : '"opencv >= 2.3.1"';
function main(){
//Try using pkg-config, but if it fails and it is on Windows, try the fallback
exec("pkg-config " + opencv + " " + flag, function(error, stdout, stderr){
if(error){
if(process.platform === "win32"){
fallback();
}
else{
throw new Error("ERROR: failed to run: pkg-config", opencv, flag);
}
}
else{
console.log(stdout);
}
});
}
//======================Windows Specific=======================================
function fallback(){
exec("echo %OPENCV_DIR%", function(error, stdout, stderr){
stdout = cleanupEchoOutput(stdout);
if(error){
throw new Error("ERROR: There was an error reading OPENCV_DIR");
}
else if(stdout === "%OPENCV_DIR%") {
throw new Error("ERROR: OPENCV_DIR doesn't seem to be defined");
}
else {
printPaths(stdout);
}
});
}
function printPaths(opencvPath){
if(flag === "--cflags") {
console.log("\"" + opencvPath + "\\..\\..\\include\"");
console.log("\"" + opencvPath + "\\..\\..\\include\\opencv\"");
}
else if(flag === "--libs") {
var libPath = opencvPath + "\\lib\\";
fs.readdir(libPath, function(err, files){
if(err){
throw new Error("ERROR: couldn't read the lib directory " + err);
}
var libs = "";
for(var i = 0; i < files.length; i++){
if(getExtension(files[i]) === "lib"){
libs = libs + " \"" + libPath + files[i] + "\" \r\n ";
}
}
console.log(libs);
});
}
else {
throw new Error("Error: unknown argument '" + flag + "'");
}
}
function cleanupEchoOutput(s){
return s.slice(0, s.length - 2);
}
function getExtension(s){
return s.substr(s.lastIndexOf(".") + 1);
}
main();