-
Notifications
You must be signed in to change notification settings - Fork 2
/
node-builder.js
executable file
·214 lines (189 loc) · 7.05 KB
/
node-builder.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
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
#!/usr/bin/env node
/* node-builder
* Copyright 2011 Jaakko-Heikki Heusala <jheusala@iki.fi>
*/
function load_module(name) {
try {
return require(name);
} catch(e) {
}
}
function error(msg) {
console.log("ERROR: " + msg);
return;
}
function main() {
function no_module_msg(name) {
return "Could not load module " + name + "\n\nTo install it use:\n npm install " + name + "\n";
}
// Prepare modules
var fs = load_module('fs');
if(!fs) return error(no_module_msg('fs'));
var optimist = load_module('optimist');
if(!optimist) return error(no_module_msg('optimist'));
var path = load_module('path');
if(!path) return error(no_module_msg('path'));
var child_process = load_module('child_process');
if(!child_process) return error(no_module_msg('child_process'));
// Parse arguments
var args = optimist.usage('Usage: $0 [-q] [--tmpdir=DIR] -o FILE [file(s)]')
.default('o', 'a.out')
.default('tmpdir', './tmp')
.default('distfile', 'http://nodejs.org/dist/v0.8.15/node-v0.8.15.tar.gz')
.demand(['o'])
.argv;
var source_files = args._;
if(source_files.length === 0) return error("You need to specify at least one script file.");
var quiet = args.q;
/* Async preparation for directory */
function prep_dir (name, next) {
if(!quiet) console.log("Preparing directory " + name );
fs.exists(name, function(exists) {
if(exists) return next();
fs.mkdir(name, "0700", function(err) { next(err); });
});
}
/* Async preparation for remote files */
function prep_distfiles (distfile, tofile, next) {
if(!quiet) console.log("Preparing distfile " + distfile + " to " + tofile);
fs.exists(tofile, function(exists) {
if(exists) return next();
if(!quiet) console.log("Downloading " + distfile + " to " + tofile);
var wget = child_process.spawn('wget', ['-O', tofile, distfile]);
wget.stdin.end();
if(!quiet) wget.stdout.on('data', function (data) { console.log(""+data); });
if(!quiet) wget.stderr.on('data', function (data) { console.log(""+data); });
wget.on('exit', function (code) {
if(code !== 0) next(new TypeError('wget exited with code ' + code));
next();
});
});
}
/* Async check file md5sum */
function check_md5sum (name, sum, next) {
if(!quiet) console.log("Checking md5sum for " + name + " ... [NOT IMPLEMENTED!]" );
next();
}
/* For make & make install etc */
function doexec (name, cmdargs, next) {
if(!quiet) console.log("Running "+name+" " + cmdargs.join(" ") + "..." );
var c = child_process.spawn(name, cmdargs);
c.stdin.end();
if(!quiet) c.stdout.on('data', function (data) { console.log(""+data); });
if(!quiet) c.stderr.on('data', function (data) { console.log(""+data); });
c.on('exit', function (code) {
if(code !== 0) next(new TypeError(name+' exited with code ' + code));
next();
});
}
/* Async unpack tar.gz */
function prep_unpack_tgz (name, dir, next) {
if(!quiet) console.log("Checking " + dir + "/configure..." );
fs.exists(dir+"/configure", function(exists) {
if(exists) return next();
if(!quiet) console.log("Unpacking " + name + " to " + dir);
var tar = child_process.spawn('tar', ['--strip-components=1', '-C', dir, '-zxf', name]);
if(!quiet) tar.stdout.on('data', function (data) { console.log(""+data); });
if(!quiet) tar.stderr.on('data', function (data) { console.log(""+data); });
tar.on('exit', function (code) {
if(code !== 0) next(new TypeError('tar exited with code ' + code));
next();
});
});
}
/* Async preparation for source files */
function prep_source_files (srcdir, files, next) {
if(!quiet) console.log("Preparing source files into " + srcdir + "..." );
var file = files[0];
if(!file) next("no files!");
fs.readFile(srcdir+'/node.gyp', 'utf8', function(err, data) {
if(err) return error('Could not read node.gyp: ' + err);
data = data.replace(
"'lib/_linklist.js',",
"'lib/_linklist.js', 'lib/_third_party_main.js',"
);
fs.writeFile(srcdir+'/node.gyp', data, 'utf8', function(err) {
if(err) return error('Could not write node.gyp: ' + err);
doexec("cp", ["-f", file, srcdir+"/lib/_third_party_main.js"], function(err) {
if(err) return error('Could not prepare _third_party_main.js: ' + err);
next();
});
});
});
}
/* Change directory */
function chdir(dir, next) {
if(!quiet) console.log('Changing to directory: ' + dir);
try {
process.chdir(dir);
next();
} catch (err) {
next('chdir: ' + err);
}
}
/* Compile node */
function compile_node (srcdir, installdir, prefix, next) {
if(!quiet) console.log("Preparing destdir... ");
doexec("mkdir", ["-p", installdir+prefix], function(err) {
if(err) return error('Could prepare destdir: ' + err);
if(!quiet) console.log("Changing to "+ srcdir +"... ");
chdir(srcdir, function(err) {
if(err) return error('Could change directory: ' + err);
doexec("./configure", ["--prefix="+prefix], function(err) {
if(err) return error('Could not configure: ' + err);
doexec("make", [], function(err) {
if(err) return error('Could not make: ' + err);
if(!quiet) console.log("Installing to "+ installdir +"... ");
doexec("make", ["DESTDIR="+installdir, "install"], function(err) {
if(err) return error('Could not make install: ' + err);
next();
});
});
});
});
});
}
/* Async preparation for output file */
function prep_output_file (bindir, outfile, next) {
if(!quiet) console.log("Installing to " + outfile + "..." );
doexec("cp", ["-f", bindir+"/node", outfile], function(err) {
if(err) return next('Could not install '+outfile+': ' + err);
next();
});
}
/* Async builder cycle */
if(!quiet) console.log("Building... ");
var outputfile = path.resolve(path.normalize(args.o));
var prefix = "/opt/node";
var tmpdir = path.resolve(path.normalize(args.tmpdir));
prep_dir(tmpdir, function(err) {
if(err) return error('Could not prepare: ' + tmpdir + ': ' + err);
var sourcefile = tmpdir + '/node.tar.gz';
prep_distfiles(args.distfile, sourcefile, function(err) {
if(err) return error('Could not prepare: ' + args.distfile + ' to ' + sourcefile + ': ' + err);
check_md5sum(sourcefile, "a2a6a6699e275a30f6047b1f33281a77", function(err) {
if(err) return error('Hash failed for ' + sourcefile);
var distdir = tmpdir + "/node";
var installdir = tmpdir + "/destdir";
prep_dir(distdir, function(err) {
if(err) return error('Could not prepare: ' + distdir + ': ' + err);
prep_unpack_tgz(sourcefile, distdir, function(err) {
if(err) return error('Unpack failed for ' + sourcefile + ": " + err);
prep_source_files(distdir, source_files, function(err) {
if(err) return error('Preparing for source files failed: ' + err);
compile_node(distdir, installdir, prefix, function(err) {
if(err) return error('Compiling node failed: ' + err);
prep_output_file(installdir+prefix+"/bin", outputfile, function(err) {
if(err) return error('Installing binary failed: ' + err);
if(!quiet) console.log("OK!");
});
});
});
});
});
});
});
});
}
main();
/* EOF */