build.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/env node
  2. var cp = require('child_process'),
  3. fs = require('fs'),
  4. path = require('path');
  5. // Parse args
  6. var force = false,
  7. debug = false;
  8. var
  9. arch = process.arch,
  10. platform = process.platform,
  11. nodeV = /[0-9]+\.[0-9]+/.exec(process.versions.node)[0],
  12. nodeVM = /[0-9]+/.exec(process.versions.node)[0];
  13. var args = process.argv.slice(2).filter(function(arg) {
  14. if (arg === '-f') {
  15. force = true;
  16. return false;
  17. }
  18. else if (arg.substring(0, 13) === '--target_arch') {
  19. arch = arg.substring(14);
  20. }
  21. else if (arg === '--debug') {
  22. debug = true;
  23. }
  24. return true;
  25. });
  26. if (!{
  27. ia32: true,
  28. x64: true,
  29. arm: true,
  30. arm64: true,
  31. ppc64: true,
  32. ppc: true,
  33. s390x: true
  34. }.hasOwnProperty(arch)) {
  35. console.error('Unsupported (?) architecture: `' + arch + '`');
  36. process.exit(1);
  37. }
  38. // Test for pre-built library
  39. var modPath = platform + '-' + arch + '-node-' + nodeV;
  40. if (!force) {
  41. try {
  42. try{
  43. fs.statSync(path.join(__dirname, 'bin', modPath, 'deasync.node'));
  44. }
  45. catch(ex){
  46. modPath = platform + '-' + arch + '-node-' + nodeVM;
  47. fs.statSync(path.join(__dirname, 'bin', modPath, 'deasync.node'));
  48. }
  49. console.log('`' + modPath + '` exists; testing');
  50. cp.execFile(process.execPath, ['quick-test.js'], function(err, stdout, stderr) {
  51. if (err || stdout !== 'pass' || stderr) {
  52. console.log('Problem with the binary; manual build incoming');
  53. console.log('stdout=' + stdout);
  54. console.log('err=' + err);
  55. build();
  56. }
  57. else {
  58. console.log('Binary is fine; exiting');
  59. }
  60. });
  61. }
  62. catch (ex) {
  63. // Stat failed
  64. build();
  65. }
  66. }
  67. else {
  68. build();
  69. }
  70. // Build it
  71. function build() {
  72. cp.spawn(
  73. process.platform === 'win32' ? 'node-gyp.cmd' : 'node-gyp', ['rebuild'].concat(args), {
  74. stdio: 'inherit'
  75. })
  76. .on('exit', function(err) {
  77. if (err) {
  78. if (err === 127) {
  79. console.error(
  80. 'node-gyp not found! Please upgrade your install of npm! You need at least 1.1.5 (I think) ' +
  81. 'and preferably 1.1.30.'
  82. );
  83. }
  84. else {
  85. console.error('Build failed');
  86. }
  87. return process.exit(err);
  88. }
  89. afterBuild();
  90. });
  91. }
  92. // Move it to expected location
  93. function afterBuild() {
  94. var targetPath = path.join(__dirname, 'build', debug ? 'Debug' : 'Release', 'deasync.node');
  95. var installPath = path.join(__dirname, 'bin', modPath, 'deasync.node');
  96. try {
  97. fs.mkdirSync(path.join(__dirname, 'bin'));
  98. }
  99. catch (ex) {}
  100. try {
  101. fs.mkdirSync(path.join(__dirname, 'bin', modPath));
  102. }
  103. catch (ex) {}
  104. try {
  105. fs.statSync(targetPath);
  106. }
  107. catch (ex) {
  108. console.error('Build succeeded but target not found');
  109. process.exit(1);
  110. }
  111. fs.renameSync(targetPath, installPath);
  112. console.log('Installed in `' + installPath + '`');
  113. }