-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathrapidgame.js
1762 lines (1608 loc) · 48.2 KB
/
rapidgame.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Part of the [RapidGame](https://github.com/natweiss/rapidgame) project.
// See the `LICENSE` file for the license governing this code.
// Developed by Nathanael Weiss.
//
var http = require("http"),
path = require("path-extra"),
fs = require("fs"),
os = require("os"),
cmd = require("commander"),
replace = require("replace"),
glob = require("glob"),
wrench = require("wrench"),
child_process = require("child_process"),
packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, "package.json"))),
cmdName = packageJson.name,
version = packageJson.version,
category,
engines = [],
templates = [],
builds = [],
orientations = ["landscape", "portrait"],
platforms = ["headers", "ios", "mac", "android", "windows", "linux"],
copyCount = 0,
msBuildExePath,
libExePath,
vcTargetsPath,
doJSB = false,
doPhysics = false,
doNavmesh = false,
doWebp = false,
defaults = {
engine: "cocos2dx",
template: "TwoScene",
package: "org.mycompany.mygame",
dest: process.cwd(),
prefix: path.join(path.homedir(), ".rapidgame"),
src: path.join(path.homedir(), ".rapidgame", "src", "cocos2d-x"),
dest: path.join(path.homedir(), ".rapidgame", "lib"),
orientation: orientations[0]
};
//
// list directories (path.join is called on all arguments)
//
var listDirectories = function() {
var i, src, dirs;
for (i = 0; i < arguments.length; i += 1) {
src = (src ? path.join(src, arguments[i]) : arguments[i]);
}
dirs = glob.sync(src);
for (i = 0; i < dirs.length; i++) {
dirs[i] = path.basename(dirs[i]);
}
return dirs;
};
//
// get engines and templates
//
engines = listDirectories(__dirname, "templates", "*");
templates = listDirectories(__dirname, "templates", "cocos2dx", "*");
//
// Main run method.
//
var run = function(args) {
var i, commands = [], commandFound = false;
//checkUpdate();
args = args || process.argv;
cmd
.version(version)
.option("-s, --src <path>", "cocos2d-x source path [" + defaults.src + "]", defaults.src)
.option("-d, --dest <name>", "destination libraries path [" + defaults.dest + "]", defaults.dest)
.option("-p, --prefix <path>", "rapidgame home [" + defaults.prefix + "]", defaults.prefix)
.option("-t, --template <name>", "template [" + defaults.template + "]", defaults.template)
.option("-f, --folder <path>", "output folder of created project [" + defaults.dest + "]", defaults.dest)
.option("-v, --verbose", "be verbose", false)
.option("--minimal", "prebuild only debug libraries and use minimal architectures", false)
//.option("--i386", "on iphonesimulator, build i386 instead of x86_64", false)
.option("--nostrip", "do not strip the prebuilt libraries", false);
cmd
.command("show")
.description(" Show where static libraries and headers reside")
.action(showPrefix);
commands.push("show");
cmd
.command("prebuild <platform>")
.description(" Prebuild cocos2d-x static libraries and headers")
.action(prebuild);
commands.push("prebuild");
cmd
.command("clean")
.description(" Clean the temporary build files")
.action(clean);
commands.push("clean");
cmd
.command("init <directory>")
.description(" Create a symlink named 'lib' to the static libraries")
.action(init);
commands.push("init");
cmd
.command("create <engine> <project-name> <package-name>")
.description(" Create a new cross-platform game project")
.action(createProject);
commands.push("create");
cmd.on("--help", usageExamples);
cmd
.parse(args)
.name = cmdName;
if (!cmd.args.length) {
usage();
} else {
// Check if command exists
for (i = 0; i < commands.length; i += 1) {
if (args[2] === commands[i]) {
commandFound = true;
break;
}
}
if (!commandFound) {
console.log("Command '" + args[2] + "' not found");
usage();
}
}
};
//
// check that prefix directory is writeable
//
var checkPrefix = function() {
return true;
};
//
// Resolve dirs.
//
var resolveDirs = function() {
// Resolve prefix.
if (cmd.prefix !== defaults.prefix) {
cmd.prefix = path.resolve(cmd.prefix);
}
if (!isWriteableDir(cmd.prefix)) {
// Complain if specified prefix dir.
if (cmd.prefix !== defaults.prefix) {
logErr("Cannot write files to prefix directory: " + cmd.prefix);
return false;
}
// Make dir
wrench.mkdirSyncRecursive(defaults.prefix);
if (!isWriteableDir(defaults.prefix)) {
logErr("Cannot write files to default prefix directory: " + defaults.prefix);
return false;
}
// Success.
cmd.prefix = defaults.prefix;
if (cmd.verbose) {
console.log("Can successfully write files to prefix directory: " + cmd.prefix);
}
}
if (!dirExists(cmd.prefix)) {
logBuild("Invalid prefix dir: " + cmd.prefix, true);
return false;
}
// Resolve source.
if (cmd.src !== defaults.src) {
cmd.src = path.resolve(cmd.src);
}
if (!dirExists(cmd.src)) {
if (cmd.src !== defaults.src) {
logBuild("Invalid src dir: " + cmd.src, true);
} else {
logBuild("Please specify a source directory with the -s option.", true);
//usage();
}
return false;
}
// Resolve dest.
if (cmd.dest !== defaults.dest) {
cmd.dest = path.resolve(cmd.dest);
}
return true;
};
//
// Initialize the given directory.
//
var init = function(directory) {
var src, dest;
if (!resolveDirs()) {return 1;}
if (!dirExists(directory)) {
console.log("Output directory must exist: " + directory);
return 1;
}
// Create lib symlink
// Windows "EPERM: operation not permitted" probably means user needs to Run As Administrator
// http://stackoverflow.com/questions/4051883/batch-script-how-to-check-for-admin-rights
src = cmd.dest;
dest = path.join(directory, "lib");
console.log("Symlinking" + (cmd.verbose ? ": " + dest + " -> " + src : " lib folder"));
try {
fs.symlinkSync(src, dest);
} catch(e) {
logErr("Error creating symlink");
if (process.platform === "win32") {
logErr("\nPlease 'Run As Administrator'.\n");
}
}
};
//
// Clean the temp build files.
//
var clean = function(directory) {
var i, j, dest, files = [], globbed = [],
configs = ["Debug", "Release"];
if (!resolveDirs()) {return 1;}
// List dirs to clean.
for (i = 0; i < configs.length; i += 1) {
globbed = glob.sync(path.join(cmd.src, "build", configs[i] + "-*"));
for (j = 0; j < globbed.length; j += 1) {
files.push(globbed[j]);
}
}
// Add Windows dirs to list.
globbed = glob.sync(path.join(cmd.src, "**", "Debug.win32"));
for (j = 0; j < globbed.length; j += 1) {
files.push(globbed[j]);
}
globbed = glob.sync(path.join(cmd.src, "**", "Release.win32"));
for (j = 0; j < globbed.length; j += 1) {
files.push(globbed[j]);
}
// Remove dirs.
for (i = 0; i < files.length; i += 1) {
if (dirExists(files[i])) {
console.log("Cleaning: " + files[i]);
try {
wrench.rmdirSyncRecursive(files[i], true);
} catch(e) {
logErr(e, cmd.verbose);
}
}
}
};
//
// Show prefix.
//
var showPrefix = function(directory) {
if (!resolveDirs()) {return 1;}
console.log("Rapidgame lives here: " + cmd.prefix);
console.log("Prebuilt headers and libs path: " + cmd.dest);
console.log("Headers have been copied: " + (dirExists(path.join(cmd.dest, "cocos2d", "x", "include")) ? "YES" : "NO"));
console.log("Libraries have been built: " + (dirExists(path.join(cmd.dest, "cocos2d", "x", "lib")) ? "YES" : "NO"));
if (cmd.src != defaults.src) {
console.log("src: " + cmd.src);
}
};
//
// Create project.
//
var createProject = function(engine, name, package) {
var dir = path.join(process.cwd(), name),
src,
dest,
fileCount,
i,
onFinished,
files,
isCocos2d = false,
packageSrc = "com.wizardfu." + cmd.template.toLowerCase();
cmd.engine = engine.toString().toLowerCase();
if (!resolveDirs()) {return 1;}
category = "createProject";
// Check engine and name
if (!cmd.engine || !name || !package) {
console.log("Engine, project name and package name are required, for example: " + cmdName + " cocos2dx \"HeckYeah\" com.mycompany.heckyeah");
usage();
return 1;
}
// Check if dirs exist
if (dirExists(dir) || fileExists(dir)) {
console.log("Output directory already exists: " + dir);
return 1;
}
// Check engine
if (engines.indexOf(cmd.engine) < 0) {
console.log("Engine '" + cmd.engine + "' not found");
console.log("Available engines are: " + engines.join(", "));
usage();
return 1;
}
// Check template
src = path.join(__dirname, "templates", cmd.engine, cmd.template);
if (!dirExists(src)) {
console.log("Missing template directory: " + src);
files = listDirectories(__dirname, "templates", cmd.engine, "*");
if (files.length > 0) {
console.log("Available templates for " + cmd.engine + " are: " + files.join(", ") + ".");
}
usage();
return 1;
}
// Start
logReport("start", cmd.engine + "/" + cmd.template);
console.log("Rapidly creating a game");
console.log("Engine: " + cmd.engine.charAt(0).toUpperCase() + cmd.engine.slice(1));
console.log("Template: " + cmd.template + (cmd.verbose ? " " + packageSrc : ""));
isCocos2d = (cmd.engine.indexOf("cocos") >= 0);
// Copy all template files to destination
dest = dir;
console.log("Copying project files" + (cmd.verbose ? " from " + src + " to " + dest : ""));
fileCount = copyRecursive(src, dest, true);
if (cmd.verbose) {
console.log("Successfully copied " + fileCount + " files");
}
// Replace project name
console.log("Setting project name: " + name);
replace({
regex: cmd.template,
replacement: name,
paths: [dest],
include: "*.js,*.plist,*.cpp,*.md,*.lua,*.html,*.json,*.xml,*.xib,*.pbxproj,*.xcscheme,*.xcworkspacedata,*.xccheckout,*.sh,*.cmd,*.py,*.rc,*.sln,*.txt,.classpath,.project,.cproject,makefile,manifest,*.vcxproj,*.user,*.filters,.name",
recursive: true,
silent: !cmd.verbose
});
// Replace package name
console.log("Setting package name: " + package);
replace({
regex: packageSrc,
replacement: package,
paths: [dest],
include: "*.js,*.plist,*.xml,makefile,manifest,*.settings,*.lua,.project,.identifier",
recursive: true,
silent: !cmd.verbose
});
// Rename files & dirs
from = path.join(dest, "**", cmd.template + ".*");
if (cmd.verbose) {
console.log("Renaming all " + from + " files");
}
files = glob.sync(from);
for (i = 0; i < files.length; i++) {
from = files[i];
to = path.join(path.dirname(from), path.basename(from).replace(cmd.template, name));
if (cmd.verbose) {
console.log("Moving " + from + " to " + to);
}
try {
fs.renameSync(from, to);
} catch(e) {
logErr("Error moving file " + e);
}
}
// Symlink
if (isCocos2d) {
init(dir);
}
// Npm install
i = null;
dest = path.join(dir, "server");
onFinished = function(){
// Show readme
console.log("Done");
try {
var text = fs.readFileSync(path.join(dir, "README.md")).toString();
console.log("");
console.log(text);
console.log("");
} catch(e) {
}
logReport("done");
// Auto prebuild
if (isCocos2d && !dirExists(cmd.dest)) {
console.log("");
console.log("Static libraries must be prebuilt");
prebuild();
}
};
if (dirExists(dest) && !dirExists(path.join(dest, "node_modules"))) {
console.log("Installing node modules");
try {
child_process.exec("npm install", {cwd: dest, env: process.env}, function(a, b, c){
execCallback(a, b, c);
onFinished();
});
} catch(e) {
logErr("Error installing node modules: " + e);
}
} else {
onFinished();
}
};
//
// run the prebuild command
//
var prebuild = function(platform, config, arch) {
if (!resolveDirs()) {return 1;}
category = "prebuild";
platform = (platform || "");
platform = platform.toString().toLowerCase();
config = config || "";
arch = arch || "";
if (platforms.indexOf(platform) < 0) {
platform = "";
}
// initialize build log
cmd.buildLog = path.join(cmd.prefix, "build-" + process.platform + ".log");
try {
fs.writeFileSync(cmd.buildLog, "");
logBuild("Happily prebuilding: " + platform, true);
console.log("Writing build log to: " + cmd.buildLog);
} catch(e) {
cmd.buildLog = "";
}
getToolPaths(function(success) {
if (!success) {
return;
}
logReport("start");
copySrcFiles(function() {
setupPrebuild(platform, function() {
runPrebuild(platform, config, arch, function() {
logReport("done");
});
});
});
});
};
//
// copy src directory to prefix
//
var copySrcFiles = function(callback) {
var src = path.join(__dirname, "src"),
dest = path.join(cmd.prefix, "src");
// Synchronously copy src directory to dest
if (src !== dest && !dirExists(dest)) {
logBuild("Copying " + src + " to " + dest, true);
copyRecursive(src, dest, true);
}
callback();
};
//
// prebuild setup (copies headers, java files, etc.)
//
var setupPrebuild = function(platform, callback) {
var dir, src, dest, i, files;
// Return if we are prebuilding a specific platform.
if (platform && platform !== "headers") {
logBuild("Not building headers", true);
callback();
return;
}
// Return if we have already prebuilt headers.
if (!platform && dirExists(path.join(cmd.dest, "cocos2d", "x", "java", "mk"))) {
logBuild("Already built headers", true);
callback();
return;
}
logBuild("Checking if symlinks can be created", true);
// symlink ./latest -> ./version
src = path.basename(cmd.dest);
dest = path.join(cmd.prefix, "latest");
try {
fs.unlinkSync(dest);
} catch(e) {
//logErr("Error deleting symlink: " + e);
}
try {
fs.symlinkSync(src, dest);
logErr("Ok");
} catch(e) {
logErr("Error creating symlink " + dest + " => " + src);
if (process.platform === "win32") {
logErr("\nPlease 'Run As Administrator'. Proceeding with unpredictable results.\n");
}
}
logBuild("Copying header files...", true);
// reset cocos2d dir
dest = path.join(cmd.dest, "cocos2d");
files = ["html", path.join("x", "include"), path.join("x", "cmake"), path.join("x", "java"), path.join("x", "script")];
try {
for (i = 0; i < files.length; i += 1) {
src = path.join(dest, files[i]);
logBuild("rm -r " + src, cmd.verbose);
wrench.rmdirSyncRecursive(src, true);
logBuild("mkdir " + src, cmd.verbose);
wrench.mkdirSyncRecursive(src);
}
} catch(e) {
logBuild("Error cleaning destination", cmd.verbose);
logBuild(e, cmd.verbose);
}
// copy cmake files
dest = path.join(cmd.dest, "cocos2d", "x", "cmake");
src = path.join(cmd.src, "cmake");
copyRecursive(src, dest, false, true);
// copy cocos2d-html5
dest = path.join(cmd.dest, "cocos2d", "html");
src = path.join(cmd.src, "web");
copyRecursive(src, dest, false, true);
// copy headers
dir = dest = path.join(cmd.dest, "cocos2d", "x", "include");
src = cmd.src;
copyGlobbed(src, dest, '*.h');
copyGlobbed(src, dest, '*.hpp');
copyGlobbed(src, dest, '*.msg');
copyGlobbed(src, dest, '*.inl');
// remove unneeded
files = ["docs", "build", "tests", "samples", "templates", "tools",
path.join("plugin", "samples"), path.join("plugin", "plugins"), path.join("extensions", "proj.win32")];
for (i = 0; i < files.length; i += 1) {
wrench.rmdirSyncRecursive(path.join(dir, files[i]), true);
}
// jsb
dest = path.join(cmd.dest, "cocos2d", "x", "script");
src = path.join(cmd.src, "cocos", "scripting", "js-bindings", "script");
copyGlobbed(src, dest, '*.js');
// java
dir = path.join(cmd.dest, "cocos2d", "x", "java");
dest = path.join(dir, "cocos2d-x");
src = path.join(cmd.src, "cocos", "platform", "android", "java");
copyRecursive(src, dest);
// mk
dest = path.join(cmd.dest, "cocos2d", "x", "java", "mk");
copyGlobbed(cmd.src, dest, "*.mk");
// clean up
files = ["proj.android", "cocos2d-js", path.join("cocos2d-x", "tools"), path.join("cocos2d-x", "templates"), path.join("cocos2d-x", "tests")];
for (i = 0; i < files.length; i += 1) {
wrench.rmdirSyncRecursive(path.join(dest, files[i]), true);
}
files = glob.sync(path.join(dir, "*", "bin"));
for (i = 0; i < files.length; i += 1) {
wrench.rmdirSyncRecursive(files[i], true);
}
files = glob.sync(path.join(dir, "*", "gen"));
for (i = 0; i < files.length; i += 1) {
wrench.rmdirSyncRecursive(files[i], true);
}
// find ${dir} | xargs xattr -c >> ${logFile} 2>&1
callback();
};
//
// run the prebuild command
//
var runPrebuild = function(platform, config, arch, callback) {
config = "";
arch = "";
// check whether to prebuild javascript bindings
try{
var configDest = path.join(cmd.dest, "cocos2d", "x", "include", "cocos", "base", "ccConfig.h"),
ccConfig = fs.readFileSync(configDest).toString().trim();
doJSB = (ccConfig.indexOf("CC_ENABLE_SCRIPT_BINDING 1") >= 0);
doPhysics = (ccConfig.indexOf("CC_USE_PHYSICS 1") >= 0);
doNavmesh = (ccConfig.indexOf("CC_USE_NAVMESH 1") >= 0);
doWebp = (ccConfig.indexOf("CC_USE_WEBP 1") >= 0);
console.log("Prebuild options:");
console.log(" Javascript bindings: " + (doJSB ? "yes" : "no"));
console.log(" Physics: " + (doPhysics ? "yes" : "no"));
console.log(" WebP: " + (doWebp ? "yes" : "no"));
} catch(e) {
}
if (platform === "headers") {
callback();
} else if (platform === "mac") {
prebuildMac("Mac", config, arch, callback);
} else if (platform === "ios") {
prebuildMac("iOS", config, arch, callback);
} else if (platform === "linux") {
prebuildLinux("Linux", config, arch, callback);
} else if (platform === "windows") {
prebuildWin("Windows", config, arch, callback);
} else if (platform === "android") {
prebuildAndroid("Android", config, arch, callback);
} else {
prebuildMac("Mac", config, arch, function(){
prebuildMac("iOS", config, arch, function(){
prebuildLinux("Linux", config, arch, function(){
prebuildWin("Windows", config, arch, function(){
prebuildAndroid("Android", config, arch, function(){
callback();
});
});
});
});
});
}
};
//
// launch the next build
//
var nextBuild = function(platform, callback){
// Show remaining builds.
if (cmd.verbose) {
logBuild("Remaining builds: ", true);
for (i = 0; i < builds.length; i+=1) {
logBuild(" Config: " + builds[i][0] + "\n Dir: " + builds[i][2] + "\n Command: " + builds[i][1] + " " + builds[i][3].join(" ") + "\n", true);
}
}
// Launch this build.
if (builds.length) {
startBuild(platform, callback, builds.shift());
} else {
callback();
}
};
//
// start a given build
//
var startBuild = function(platform, callback, settings) {
var i,
config = settings[0],
command = settings[1],
dir = settings[2],
args = settings[3],
func = settings[4],
funcArg = settings[5];
logBuild("Building: " + platform +
(config ? " " + config : "") +
(command ? " " + (cmd.verbose ? command : path.basename(command)) : "") +
((cmd.verbose && dir) ? " " + dir : ""),
true);
spawn(command, args, {cwd: path.resolve(dir), env: process.env}, function(err){
var onFinished = function(){
logBuild("Succeeded.", true);
nextBuild(platform, callback);
};
if (!err){
// Run callback.
if (typeof func === "function") {
func(funcArg, onFinished);
} else {
onFinished();
}
} else {
// Failed.
if (!cmd.verbose) {
console.log("Build failed. Please run with --verbose or check the build log: " + cmd.buildLog);
}
}
});
};
//
// prebuild mac
//
var prebuildMac = function(platform, config, arch, callback) {
var i, j, k, dir, project, func, funcArg, derivedDir, dest,
sdks = (platform === "Mac" ? ["macosx"] : ["iphoneos", "iphonesimulator"]),
configs = (config ? [config] : (cmd.minimal ? ["Debug"] : ["Debug", "Release"])),
projs = [
path.join(cmd.src, "build", "cocos2d_libs.xcodeproj")
];
if (doJSB !== false) {
projs.push(path.join(cmd.src, "cocos", "scripting", "js-bindings", "proj.ios_mac", "cocos2d_js_bindings.xcodeproj"));
}
// Bail if not on Mac.
if (process.platform !== "darwin") {
logBuild("Can only build " + platform + " on Mac", true);
callback();
return;
}
// Create builds array.
for (i = 0; i < configs.length; i += 1) {
for (j = 0; j < sdks.length; j += 1) {
for (k = 0; k < projs.length; k += 1) {
command = "xcodebuild";
dir = path.dirname(projs[k]);
derivedDir = path.join(cmd.src, "build", configs[i] + "-" + sdks[j]);
args = [
"-project", path.basename(projs[k]),
"-configuration", configs[i],
"-sdk", sdks[j],
"-derivedDataPath", path.resolve(derivedDir)
];
if (k == 0) { // first proj is libcocos2d
//"-scheme", "\"libcocos2d " + platform + "\"", // this doesn't spawn correctly
args = args.concat(["-scheme", "libcocos2d " + platform]);
} else { // second proj is libjscocos2d
args = args.concat(["-scheme", "libjscocos2d " + platform]);
}
if (sdks[j] === "iphoneos") {
if (cmd.minimal) {
args = args.concat(["-arch", "armv7"]);
} /*else {
args = args.concat([
"-destination", "platform=iOS,name=iPhone 6s",
"-destination-timeout", "5"
]);
}*/
} else if (sdks[j] === "iphonesimulator") {
// des it need "generic/" in the destination? is that why it doesn't run correctly on 5s?
args = args.concat([
"-destination", "platform=iphonesimulator,name=iPhone 6s",
"-destination-timeout", "5"
]);
/*if (cmd.i386) {
args = args.concat(["-arch", "i386"]);
} else {
// why doesn't this make iphonesimulator libs have both i386 and x86_64? is one being stripped away?
//args = args.concat(["-arch", "x86_64", "-arch", "i386"]);
args = args.concat(["-arch", "x86_64"]);
}*/
}
// final bit of command (xcode settings)
args.push("GCC_SYMBOLS_PRIVATE_EXTERN=NO");
args.push("OTHER_CPLUSPLUSFLAGS=-w");
if (!cmd.nostrip) {
args.push("DEPLOYMENT_POSTPROCESSING=YES");
args.push("STRIP_INSTALLED_PRODUCT=YES");
args.push("STRIP_STYLE=non-global");
}
// Post-build function.
func = linkMac;
funcArg = configs[i] + "-" + sdks[j];
// Push this build.
builds.push([configs[i], command, dir, args, func, funcArg]);
// Prepare link command.
command = "libtool";
dir = path.join(path.resolve(derivedDir), "Build", "Products");
dest = path.join(cmd.dest, "cocos2d", "x", "lib", configs[i] + "-" + platform, sdks[j]);
wrench.mkdirSyncRecursive(dest);
dest = path.join(dest, "libcocos2dx-prebuilt.a");
// Link.
args = [
"-static",
"-o", dest,
"-filelist", path.join(dir, "list.txt")
// this doesn't use derived data:
// xcodebuild -project src/cocos2d-x/build/cocos2d_libs.xcodeproj -target "libcocos2d Mac" -showBuildSettings | grep BUILD_DIR
];
builds.push([configs[i], command, dir, args, false, false]);
}
}
}
nextBuild(platform, callback);
};
//
// link mac
//
var linkMac = function(configPlatform, callback) {
var i, txt,
d = path.join(cmd.src, "build", configPlatform, "Build", "Products"),
files = [];
files = glob.sync(path.join(d, "**", "*.a"));
d = path.join(d, "list.txt");
txt = files.join("\n") + "\n";
logBuild("Writing file list:\n " + d + "\n " + files.join("\n "));
fs.writeFileSync(d, txt);
callback();
};
//
// prebuild linux
//
var prebuildLinux = function(platform, config, arch, callback) {
var i, j, dir, args, func, funcArg,
archs = [/*"i386", */"x86_64"], // a second arch would currently clobber the first arch's intermediate files
configs = (config ? [config] : (cmd.minimal ? ["Debug"] : ["Debug", "Release"]));
// Bail if not on Linux.
if (process.platform !== "linux") {
logBuild("Can only build " + platform + " on Linux", true);
callback();
return;
}
// create builds array
builds = [];
for (j = 0; j < archs.length; j += 1) {
for (i = 0; i < configs.length; i += 1) {
dir = path.join(cmd.src, "build", configs[i] + "-linux");
funcArg = configs[i] + "-" + archs[j];
args = [
path.join("..", ".."),
"-DDEBUG_MODE=" + (configs[i] === "Debug" ? "ON" : "OFF"),
"-DBUILD_SHARED_LIBS=OFF",
"-DBUILD_EXTENSIONS=OFF",
"-DBUILD_EDITOR_SPINE=OFF",
"-DBUILD_EDITOR_COCOSTUDIO=OFF",
"-DBUILD_EDITOR_COCOSBUILDER=OFF",
"-DBUILD_CPP_TESTS=OFF",
"-DBUILD_LUA_LIBS=OFF",
"-DBUILD_LUA_TESTS=OFF",
"-DBUILD_JS_TESTS=OFF",
"-DUSE_BOX2D=OFF"
// USE_PREBUILT_LIBS=ON (use default here)
];
if (doJSB !== false) {
args.push("-DBUILD_JS_LIBS=ON");
} else {
args.push("-DBUILD_JS_LIBS=OFF");
}
if (doPhysics !== false) {
args.push("-DUSE_CHIPMUNK=ON");
args.push("-DUSE_BULLET=ON");
} else {
args.push("-DUSE_CHIPMUNK=OFF");
args.push("-DUSE_BULLET=OFF");
}
if (doNavmesh !== false) {
args.push("-DUSE_RECAST=ON");
} else {
args.push("-DUSE_RECAST=OFF");
}
if (doWebp !== false) {
args.push("-DUSE_WEBP=ON");
} else {
args.push("-DUSE_WEBP=OFF");
}
wrench.mkdirSyncRecursive(dir);
builds.push([configs[i], "cmake", dir, args, false, false]);
builds.push([configs[i], "make", dir, ["-j", parseInt(Math.max(4, os.cpus.length * 1.5))], linkLinux, funcArg]);
}
}
nextBuild(platform, callback);
};
//
// link linux
//
var linkLinux = function(configArch, callback) {
var a, src, dest, config = "", arch = "", bits = "";
// Copy these prebuilt files.
a = configArch.split("-");
if (a.length >= 2) {
config = a[0];
arch = a[1];
bits = (arch === "x86_64" ? "64-bit" : "32-bit");
// copy libcocos2d.a
src = path.join(cmd.src, "build", config + "-linux", "lib");
dest = path.join(cmd.dest, "cocos2d", "x", "lib", config + "-Linux", arch);
logBuild("Copying Linux libraries from " + src + " to " + dest, false);
copyGlobbed(src, dest, "*.a");
// copy other prebuilt libraries
src = path.join(cmd.src, "external");
logBuild("Copying Linux libraries from " + src + " to " + dest, false);
copyGlobbed(src, dest, "*.a", path.join("prebuilt", "linux", bits), "none");
src = path.join(cmd.src, "external");
logBuild("Copying Linux libraries from " + src + " to " + dest, false);
copyGlobbed(src, dest, "*.so", path.join("prebuilt", bits), "none");
} else {
logBuild("Failed to link Linux because couldn't get config and arch from: " + configArch, true);
}
// remember to call callback when finished
callback();
};
//
// prebuild windows
//
var prebuildWin = function(platform, config, arch, callback) {
var i, j, command, dir, args, func, funcArg, targets, projs = [],
configs = (config ? [config] : (cmd.minimal ? ["Debug"] : ["Debug", "Release"]));
// Bail if not on Windows.
if (process.platform !== "win32") {
logBuild("Can only build win32 on Windows", true);
callback();
return;
}
// set vc targets path
process.env["VCTargetsPath"] = vcTargetsPath;
// create builds
builds = [];
for (i = 0; i < configs.length; i += 1) {
command = msBuildExePath;
dir = path.homedir();// cmd.prefix;
func = linkWin;
funcArg = configs[i];
targets = ["libcocos2d"];
if (doJSB) {
targets.push("libjscocos2d");
}
args = [
path.join(cmd.src, "build", "cocos2d-win32.sln"),
"/nologo",
"/maxcpucount:4",
"/t:" + targets.join(";"),
//"/p:VisualStudioVersion=12.0",
//"/p:PlatformTarget=x86",
//"/verbosity:diag",
//"/clp:ErrorsOnly",
//"/p:nowarn=4005",
//'/p:WarningLevel=0',
"/p:configuration=" + configs[i] + ";platform=Win32"
];
// push this build
builds.push([configs[i], command, dir, args, func, funcArg]);
}
// start
nextBuild("Windows", callback);
};
//
// link windows
//
var linkWin = function(config, callback) {
var i,
command = path.basename(libExePath),
src = path.join(cmd.src, "build", config + ".win32"),
dest = path.join(cmd.dest, "cocos2d", "x", "lib", config + "-win32", "x86"),
args = [
'/NOLOGO',
'/IGNORE:4006',
//'/OPT:REF',
//'/OPT:ICF',
//'/OUT:"' + path.join(dest, "libcocos2dx-prebuilt.lib") + '"',
//'"' + path.join(src, "*.lib") + '"'
'/OUT:' + path.join(dest, "libcocos2dx-prebuilt.lib"),
path.join(src, "*.lib")
];
// make output dir