Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/Benvie/node-mnm into Benv…
Browse files Browse the repository at this point in the history
…ie-master
  • Loading branch information
joeferner committed Feb 22, 2012
2 parents 8a8977f + c4f9819 commit fee15ad
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions lib/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ var fs = require('fs');
var childProcess = require('child_process');
var ansi = require("./ansi.js");

var exists = fs.existsSync || path.existsSync;
var isArray = Array.isArray;


function Builder() {
this.flagGroups = {};
this.target = "native_bindings";
Expand Down Expand Up @@ -102,16 +106,18 @@ function Builder() {
}

Builder.prototype.appendLinkerLibrary = function(lib) {
if (isArray(lib)) return lib.forEach(this.appendLinkerLibrary.bind(this));
var flag;
if(process.platform == 'win32') {
flag = lib + '.lib';
flag = lib.replace(/(?:\.lib)?$/,'.lib');
} else {
flag = '-l' + lib;
}
this.appendUnique('LINKFLAGS', flag);
}

Builder.prototype.appendLinkerSearchDir = function(dir) {
if (isArray(dir)) return dir.forEach(this.appendLinkerSearchDir.bind(this));
var flag;
if(process.platform == 'win32') {
flag = '-LIBPATH:' + dir;
Expand All @@ -121,6 +127,11 @@ Builder.prototype.appendLinkerSearchDir = function(dir) {
this.appendUnique('LINKFLAGS', flag);
}

Builder.prototype.appendIncludeDir = function(dir) {
if (isArray(dir)) return dir.forEach(this.appendIncludeDir.bind(this));
this.appendUnique('CXXFLAGS', '-I' + dir);
}

Builder.prototype.getFlags = function(flagGroupName) {
var flags = this.flagGroups[flagGroupName];
if(!flags) {
Expand All @@ -130,7 +141,7 @@ Builder.prototype.getFlags = function(flagGroupName) {
}

Builder.prototype.appendUnique = function(flagGroupName, newFlags) {
if(!(newFlags instanceof Array)) newFlags = [ newFlags ];
newFlags = isArray(newFlags) ? newFlags : [newFlags];
var flags = this.getFlags(flagGroupName);

for(var i=0; i<newFlags.length; i++) {
Expand All @@ -141,10 +152,12 @@ Builder.prototype.appendUnique = function(flagGroupName, newFlags) {
}

Builder.prototype.appendSource = function(fileName) {
if (isArray(fileName)) return fileName.forEach(this.appendSource.bind(this));
this.sourceFiles.push(fileName);
}

Builder.prototype.appendSourceDir = function(dirName) {
if (isArray(dirName)) return dirName.forEach(this.appendSourceDir.bind(this));
var files = fs.readdirSync(dirName);
for(var i=0; i<files.length; i++) {
var fileName = files[i];
Expand Down Expand Up @@ -187,10 +200,10 @@ Builder.prototype.getLinkerArgs = function(outFileName) {

Builder.prototype.createDir = function(dirName) {
var parent = path.dirname(dirName);
if(!path.existsSync(parent)) {
if(!exists(parent)) {
this.createDir(parent);
}
if(!path.existsSync(dirName)) {
if(!exists(dirName)) {
fs.mkdirSync(dirName);
}
}
Expand Down Expand Up @@ -268,8 +281,9 @@ Builder.prototype._compile = function(curFileIdx, callback) {
var self = this;
var fileName = path.resolve(this.sourceFiles[curFileIdx]);
var outFileName = path.join(this.ouputDir, path.relative(this.projectDir, fileName));
outFileName = outFileName.replace(/\.cpp$/, '.o');
var depFileName = outFileName.replace(/\.o$/, '.d');
var outExt = process.platform == 'win32' ? '.obj' : '.o';
outFileName = outFileName.replace(/\.cpp$/, outExt);
var depFileName = outFileName.replace(new RegExp('\\'+outExt+'$'), '.d');
this.objectFiles.push(outFileName);

if(this._shouldCompile(outFileName, depFileName)) {
Expand Down Expand Up @@ -312,13 +326,11 @@ Builder.prototype.compile = function(callback) {
}

// need to append these last to reduce conflicts
this.appendUnique('CXXFLAGS', '-I' + this.nodeIncludeDir);
this.appendIncludeDir(this.nodeIncludeDir);

if(process.platform == 'win32') {
this.appendUnique('CXXFLAGS', [
'-I' + this.v8IncludeDir,
'-I' + this.uvIncludeDir
]);
this.appendIncludeDir(this.v8IncludeDir);
this.appendIncludeDir(this.uvIncludeDir);
}

// no source then fail
Expand Down Expand Up @@ -473,7 +485,7 @@ Builder.prototype.printHelp = function() {

Builder.prototype.failIfNotExists = function(dirName, message) {
dirName = path.resolve(dirName);
if(!path.existsSync(dirName)) {
if(!exists(dirName)) {
message = message || "Could not find '%s'.";
this.fail(message, dirName);
}
Expand Down

0 comments on commit fee15ad

Please sign in to comment.