forked from openT2T/node-apphost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
397 lines (351 loc) · 13.8 KB
/
build.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
var taskman = require('./tools/taskman');
var fs = require('fs');
var path = require('path');
var isWindows = process.platform === 'win32';
var args = taskman.args;
function print_help(exit_code) {
var params = [
{option: "--help", text: "display available options"},
{option: "", text: ""}, // placeholder
{option: "--cid_node=[commit-id]", text: "Checkout a particular commit from the node repo"},
{option: "--cid_jxcore=[commit-id]", text: "Checkout a particular commit from jxcore repo"},
{option: "--url_node=[node repo url]", text: "URL for node.js Github repo"},
{option: "--url_jxcore=[jx repo url]", text: "URL for jxcore Github repo"},
{option: "--dest-cpu=[cpu_type]", text: "set target cpu (arm, ia32, x86, x64). i.e. --dest-cpu=ia32"},
{option: "--ndk-path=[path]", text: "path to android ndk. This option is required for platform=android"},
{option: "--platform=[target]", text: "set target platform. by default 'desktop'. (android, desktop, ios, windows-uwp)"},
{option: "--force-target=[jxcore or nodejs]", text: "Force target framework regardless from the platform"},
{option: "--release", text: "Build release binary. By default Debug"},
{option: "--reset", text: "Clone nodejs and jxcore repos once again"},
{option: "--test", text: "run tests after build"}
];
for(var i in params) {
var param = params[i];
var space = new Buffer(28 - param.option.length); space.fill(" ");
console.log(param.option, space + ": ", param.text);
}
process.exit(exit_code);
}
if (args.hasOwnProperty('--help')) {
print_help(0);
} else {
console.log('tip: try "--help" for other options');
}
var platform = args.hasOwnProperty('--platform') ? args['--platform'] : 'desktop';
var forced_target = args['--force-target'];
var release = args.hasOwnProperty('--release');
var cpu = "";
function createScript() {
if (!isWindows) {
if (args.hasOwnProperty('--dest-cpu')) {
cpu = "--dest-cpu=" + args['--dest-cpu'];
} else {
cpu = "--dest-cpu=x64"
}
if (platform == "ios" || platform == "android" || forced_target == 'jxcore') {
var script = 'cd jxcore\n';
if (platform == "android") {
if (!args.hasOwnProperty('--ndk-path')) {
console.error('forget "--ndk-path" ??\n');
print_help(1);
}
forced_target = 'jxcore';
script += "build_scripts/android-configure.sh " + args["--ndk-path"] + "\n"
+ "if [ $? != 0 ]; then\n"
+ " exit 1\n"
+ "fi\n"
+ "build_scripts/android_compile.sh " + args["--ndk-path"] + " --embed-leveldown\n"
+ "if [ $? != 0 ]; then\n"
+ " exit 1\n"
+ "fi\n";
return script;
} else if (platform == "ios") { // ios
forced_target = 'jxcore';
script += "build_scripts/ios_compile.sh\n"
+ "if [ $? != 0 ]; then\n"
+ " exit 1\n"
+ "fi\n";
return script;
} else {
forced_target = 'jxcore';
return "cd jxcore\n./configure --engine-mozilla --static-library "
+ (release ? ' ' : '--debug ') + cpu
+ '\nmake';
}
} else {
forced_target = 'nodejs';
return 'cd nodejs;./configure --enable-static '
+ (release ? ' ' : '--debug ') + cpu
+ ';make -j ' + require('os').cpus().length;
}
} else {
if (args.hasOwnProperty('--dest-cpu')){
cpu = args['--dest-cpu'];
} else {
cpu = 'x64';
}
if (platform == "windows-uwp" && forced_target == 'jxcore') {
console.error("JXCore does not have support for Windows UWP");
exit(1);
}
var batch = "";
if (forced_target == 'jxcore')
batch += 'cd jxcore\nvcbuild.bat --shared-library ' + cpu + ' ';
else {
forced_target = 'nodejs';
batch += 'cd nodejs\nvcbuild.bat nosign ' + cpu + ' ';
}
if (platform == "windows-uwp") {
batch += 'chakra uwp-dll '; // VCBuild flags for UWP target
}
var releaseType = (release ? 'release' : 'debug');
var logFileName = 'buildlog_' + cpu + '.log';
console.log('Build log file : ' + logFileName);
batch += releaseType + ' > ' + logFileName + '\n'
+ 'set EXIT_CODE=%errorlevel%\n'
+ 'cd ..\n'
+ 'exit /b %EXIT_CODE%\n';
return batch;
}
}
var stash = function(repo) {
return path.join(__dirname, "temp/clean.bat") + " " + repo;
};
var compile = function(repo) {
return path.join(__dirname, "temp/compile.bat") + " " + repo;
};
var createBatch = function() {
try {
fs.mkdirSync('./temp');
} catch(e) { }
// this needs to be called first
fs.writeFileSync('./temp/compile.bat', createScript());
if (isWindows) {
fs.writeFileSync('./temp/clean.bat', 'cd %1\ngit checkout -f');
fs.writeFileSync('./temp/copy.bat', ('del $TAPP*.lib \n'
+ 'del $TAPP*.dll \n'
+ 'copy $$TARGET\\$$MODE\\*.lib $TAPP\n'
+ 'copy $$TARGET\\$$MODE\\*.dll $TAPP\n')
.replace(/\$\$MODE/g, release ? "Release" : "Debug")
.replace(/\$TAPP/g, path.join(__dirname, 'winproj\\test_app/'))
.replace(/\$\$TARGET/g, path.join(__dirname, forced_target)));
} else {
fs.writeFileSync('./temp/clean.bat', 'cd $1;git checkout -f');
}
var node_base = (setup.node_url.indexOf('ms-iot/node') > 0 ? 'chakra-uwp' : 'master');
var br_node = args.hasOwnProperty('--cid_node') ? args['--cid_node'] : node_base;
var br_jxcore = args.hasOwnProperty('--cid_jxcore') ? args['--cid_jxcore'] : "master";
fs.writeFileSync('./temp/checkout_node.bat', taskman.checkout('nodejs', br_node));
fs.writeFileSync('./temp/checkout_jxcore.bat', taskman.checkout('jxcore', br_jxcore));
if (!isWindows) {
fs.writeFileSync('./temp/test.bat', 'cd tests\nnode runtests.js --target=' + forced_target
+ ' --dest-cpu=' + (args.hasOwnProperty('--dest-cpu') ? args['--dest-cpu'] : 'x64')
+ '\nexit $?');
} else {
fs.writeFileSync('./temp/test.bat', 'cd tests\nnode runtests.js --target=' + forced_target
+ ' --dest-cpu=' + (args.hasOwnProperty('--dest-cpu') ? args['--dest-cpu'] : 'x64')
+ ' --mode=' + (args.hasOwnProperty('--release') ? args['--release'] : 'Debug')
+ '\nexit %errorlevel%');
}
if (!isWindows) {
fs.chmodSync('./temp/test.bat', '0755');
fs.chmodSync('./temp/clean.bat', '0755');
fs.chmodSync('./temp/compile.bat', '0755');
fs.chmodSync('./temp/checkout_node.bat', '0755');
fs.chmodSync('./temp/checkout_jxcore.bat', '0755');
}
}
var setup = function(set_url_only) {
setup.node_url = 'https://github.com/' + (isWindows ? 'ms-iot/node' : 'nodejs/node');
if (args.hasOwnProperty('--url_node')) {
setup.node_url = args['--url_node'];
}
setup.jxcore_url = 'https://github.com/jxcore/jxcore';
if (args.hasOwnProperty('--url_jxcore')) {
setup.jxcore_url = args['--url_jxcore'];
}
if (!set_url_only) {
taskman.tasker = [
[taskman.rmdir('nodejs'), "deleting nodejs folder"],
[taskman.rmdir('jxcore'), "deleting jxcore folder"],
[taskman.clone(setup.node_url, 'nodejs'), "cloning nodejs"],
[taskman.clone(setup.jxcore_url, 'jxcore'), "cloning jxcore"],
[path.join(__dirname, 'temp/checkout_node.bat'), "checkout nodejs branch"],
[path.join(__dirname, 'temp/checkout_jxcore.bat'), "checkout jxcore branch"]
];
}
};
if (args.hasOwnProperty('--reset') || !fs.existsSync(path.resolve('nodejs'))) {
console.log("Setting Up! [ This will take some time.. ]");
setup(false);
} else {
setup(true);
taskman.tasker = [
[stash('nodejs'), "resetting nodejs folder"],
[stash('jxcore'), "resetting jxcore folder"]
];
}
var patch_nodejs = function() {
console.log("[ patching node.gyp ]");
{
var node_gyp = fs.readFileSync('./nodejs/node.gyp') + "";
var len_gyp = node_gyp.length;
// add patch source files
// todo: make this regex
node_gyp = node_gyp.replace("'sources': [",
"'sources': [\n'../patch/node/node_vfile.cc',\n'../patch/node/node_wrapper.cc',\n");
if (node_gyp.length == len_gyp) {
console.error('FATAL ERROR: could not patch node.gyp with cc files');
process.exit(1);
}
if (isWindows) {
node_gyp = node_gyp.replace(/shared_library/g, 'loadable_module');
len_gyp = node_gyp.length;
// todo: make this regex
node_gyp = node_gyp.replace("[ 'node_shared==\"false\"', {",
"['node_shared==\"true\"',{'msvs_settings':"
+ "{'VCCLCompilerTool':{'RuntimeLibrary': 3}}}],\n"
+ "[ 'node_shared==\"false\"', {");
if (node_gyp.length == len_gyp) {
console.error('FATAL ERROR: could not patch node.gyp with MSVS settings');
process.exit(1);
}
len_gyp = node_gyp.length;
node_gyp = node_gyp.replace(/node_module_version!=\"\"/g,
'node_module_version!="" and OS!="win"');
}
fs.writeFileSync('./nodejs/node.gyp', node_gyp);
}
if (isWindows) {
console.log("nodejs -> [ patching configure ]");
{
var node_configure = fs.readFileSync('./nodejs/configure') + "";
fs.writeFileSync('./nodejs/configure',
node_configure.replace('b(options.shared)',"'true'"));
}
}
console.log("nodejs -> [ patching node.cc ]");
{
var nodejs_cc = fs.readFileSync('./nodejs/src/node.cc') + "";
for(var n = nodejs_cc.length-1;n > 0; n--) {
var ch = nodejs_cc[n];
if (ch == '}') { // find namespace closing
var left = nodejs_cc.substr(0, n);
var right = nodejs_cc.substr(n);
fs.writeFileSync('./nodejs/src/node.cc', left
+ '\n#include "../../patch/node/node_internal_wrapper.cc"\n' + right);
break;
}
}
}
console.log("nodejs -> [ patching module.js ]");
{
var module_patch = fs.readFileSync('patch/node/module_patch.js') + "";
var module_js = fs.readFileSync('./nodejs/lib/module.js') + "";
fs.writeFileSync('./nodejs/lib/module.js', module_js + "\n" + module_patch);
}
console.log("nodejs -> [ patching fs.js ]");
{
var fs_patch = fs.readFileSync('patch/node/fs_patch.js') + "";
var fs_js = fs.readFileSync('./nodejs/lib/fs.js') + "";
fs.writeFileSync('./nodejs/lib/fs.js', fs_js + "\n" + fs_patch);
}
};
var patch_jxcore = function() {
console.log("[ patching jx.gyp ]")
var node_gyp = fs.readFileSync('./jxcore/jx.gyp') + "";
var len = node_gyp.length;
node_gyp = node_gyp.replace("'sources': [", "'sources': [\n'../patch/jxcore/jxcore_wrapper.cc',\n");
if (len == node_gyp.length) {
console.error("FAILED to patch jx.gyp");
process.exit(1);
}
fs.writeFileSync('./jxcore/jx.gyp', node_gyp);
};
function safeCreate(target) {
target = path.join(__dirname, target);
try {
fs.mkdirSync(target);
} catch(e) {
if (!fs.existsSync(target)) {
throw e;
}
}
}
var output_folder = "";
var createRelease = function() {
console.log("[ creating release folder ]");
safeCreate('out');
var dirs = {
target: '',
source: '',
types : []
};
var cpu_ = cpu.replace('--dest-cpu=', '');
if (platform == 'android') {
dirs.target = 'out' + path.sep + forced_target + '_android_' + cpu_;
dirs.source = 'jxcore/out_android/android/bin/';
dirs.types = [ '*.a' ];
} else if (platform == 'ios') {
dirs.target = 'out' + path.sep + forced_target + '_ios_' + cpu_;
dirs.source = 'jxcore/out_ios/ios/bin/';
dirs.types = [ '*.a' ];
} else if (platform == 'desktop') {
dirs.target = 'out' + path.sep + forced_target + '_desktop_' + cpu_;
if (isWindows) {
dirs.source = forced_target + path.sep
+ (args.hasOwnProperty('--release') ? 'Release' : 'Debug');
dirs.types = [ '*.dll', '*.lib' ];
} else {
dirs.source = forced_target + path.sep + 'out' + path.sep
+ (args.hasOwnProperty('--release') ? 'Release' : 'Debug')
+ path.sep;
dirs.types = [ '*.a' ];
}
} else if (platform == 'windows-uwp') {
dirs.source = forced_target + path.sep
+ (args.hasOwnProperty('--release') ? 'Release' : 'Debug');
dirs.types = [ '*.dll', '*.lib' ];
}
else {
throw new Error("Unsupported platform: " + platform);
}
var copy_script = "";
var source = path.join(__dirname, dirs.source);
var target = path.join(__dirname, dirs.target);
var copy = (isWindows ? "copy" : "cp");
for(var o in dirs.types) {
var tp = dirs.types[o];
copy_script += copy + " " + source + path.sep + tp + " " + target + path.sep + "\n";
}
copy_script += copy + " "
+ path.join(__dirname, "patch" + path.sep + "node" + path.sep + "node_wrapper.h")
+ " " + target + path.sep + "\n";
safeCreate(dirs.target);
var script_file = path.join(__dirname, "temp" + path.sep + "distro.bat");
fs.writeFileSync(script_file, copy_script);
if (!isWindows) {
fs.chmodSync(script_file, "0755");
}
output_folder = dirs.target;
return script_file
};
var finalize = function() {
console.log('done.');
};
// create batch files
createBatch();
var compiled_script_path = compile(forced_target);
var release_script = createRelease();
// tasker will be executing each one of them one by one
// if any of these actions fail, execution will stop
taskman.tasker.push(
[patch_nodejs],
[patch_jxcore],
[compiled_script_path, "building for " + forced_target + "@" + platform],
[isWindows ? path.join(__dirname, "temp/copy.bat") : null, "copying Windows binaries"],
[args.hasOwnProperty('--test') ? path.join(__dirname, "temp/test.bat") : null, "testing"],
[release_script, "copying binaries into " + output_folder],
[finalize]
);
taskman.runTasks();