build.js 2.9 KB

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