diff --git a/binding.gyp b/binding.gyp index 8cd66bf..8cde338 100755 --- a/binding.gyp +++ b/binding.gyp @@ -24,12 +24,12 @@ ], "libraries": [ - "= 2.4.9\" )", "-Wall" ], + "defines": [ + "WIN" + ], "msvs_settings": { "VCCLCompilerTool": { "ExceptionHandling": "2", diff --git a/src/OpenCV.h b/src/OpenCV.h index 0d8eb90..0bb2b96 100755 --- a/src/OpenCV.h +++ b/src/OpenCV.h @@ -1,6 +1,16 @@ #ifndef __NODE_OPENCV_H__ #define __NODE_OPENCV_H__ +#ifdef WIN + /* + This is needed on Windows for Visual Studio to not throw an error in the + build/include/opencv2/flann/any.h file in OpenCV. + */ + namespace std{ typedef type_info type_info; } +#endif + + + #include #include #include diff --git a/utils/find-opencv.js b/utils/find-opencv.js new file mode 100644 index 0000000..c9a700a --- /dev/null +++ b/utils/find-opencv.js @@ -0,0 +1,115 @@ +var exec = require("child_process").exec; +var fs = require("fs"); +var flag = process.argv[2]; + +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{ + console.log(stdout); + } + }); +} + +//======================Windows Specific======================================= + +function fallback(){ + exec("echo %OPENCV_DIR%", function(error, stdout, stderr){ + stdout = cleanupEchoOutput(stdout); + if(error){ + console.log("ERROR: There was an error reading OPENCV_DIR"); + } + else if(stdout === "%OPENCV_DIR%") { + console.log("ERROR: OPENCV_DIR doesn't seem to be defined"); + } + else { + getVisualStudioVersion(function(version){ + getProcessArch(function(bits){ + printPaths(stdout, version, bits); + }); + }); + } + }); +} + +function printPaths(opencvPath, version, bits){ + if(flag === "--cflags") { + console.log("\"" + opencvPath + "include\""); + console.log("\"" + opencvPath + "include\\opencv\""); + } + else if(flag === "--libs") { + var libPath = opencvPath + bits + "\\vc" + version + "\\lib\\"; + + fs.readdir(libPath, function(err, files){ + if(err){ + console.log("ERROR: couldn't read the lib directory"); + console.log(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 { + console.log("Error: unknown argument '" + flag + "'"); + } +} + +//This gets the architecture of the NodeJS that is running this script, +//either x86 or x64 +function getProcessArch(cb){ + exec("echo %PROCESSOR_ARCHITECTURE%", function(error, stdout, stderr) { + if(!error) { + var arch = cleanupEchoOutput(stdout); + if(arch === "AMD64"){ + cb("x64"); + } + else if(arch === "x86"){ + cb("x86"); + } + else { + console.log("ERROR: Unrecognized architecture"); + } + } + else { + console.log("ERROR: There was an error getting the architecture"); + } + }); +} + +function getVisualStudioVersion(cb, version){ + if(typeof(version) === "undefined") version = 11; + exec("reg query HKEY_CLASSES_ROOT\\VisualStudio.DTE." + version + ".0", + function(error, stdout, stderr){ + if(!error){ + cb(version); + } + else if(version < 13) { + //Try the next version + getVisualStudioVersion(cb, version + 1); + } + else { + console.log("ERROR: Can't find Visual Studio"); + } + }); +} + +function cleanupEchoOutput(s){ + return s.slice(0, s.length - 2); +} + +function getExtension(s){ + return s.substr(s.lastIndexOf(".") + 1); +} +main();