Simplify Windows building

This commit is contained in:
cascade256 2016-01-07 22:01:29 -08:00
parent d45e74a0aa
commit 9561b4f710
3 changed files with 130 additions and 3 deletions

View File

@ -24,12 +24,12 @@
],
"libraries": [
"<!@(pkg-config --libs opencv)"
"<!@(node utils/find-opencv.js --libs)"
],
# For windows
"include_dirs": [
"<!@(pkg-config --cflags opencv)",
"<!@(node utils/find-opencv.js --cflags)",
"<!(node -e \"require('nan')\")"
],
@ -45,9 +45,11 @@
}],
[ "OS==\"win\"", {
"cflags": [
"<!@(pkg-config --cflags \"opencv >= 2.4.9\" )",
"-Wall"
],
"defines": [
"WIN"
],
"msvs_settings": {
"VCCLCompilerTool": {
"ExceptionHandling": "2",

View File

@ -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 <v8.h>
#include <node.h>
#include <node_object_wrap.h>

115
utils/find-opencv.js Normal file
View File

@ -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();