build.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. loongarch64: 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. }
  79. ).on('exit', function (err) {
  80. if (err) {
  81. if (err === 127) {
  82. console.error(
  83. 'node-gyp not found! Please upgrade your install of npm! You need at least 1.1.5 (I think) ' +
  84. 'and preferably 1.1.30.'
  85. );
  86. } else {
  87. console.error('Build failed');
  88. }
  89. return process.exit(err);
  90. }
  91. afterBuild();
  92. });
  93. }
  94. // Move it to expected location
  95. function afterBuild() {
  96. var targetPath = path.join(
  97. __dirname,
  98. 'build',
  99. debug ? 'Debug' : 'Release',
  100. 'deasync.node'
  101. );
  102. var installPath = path.join(__dirname, 'bin', modPath, 'deasync.node');
  103. try {
  104. fs.mkdirSync(path.join(__dirname, 'bin'));
  105. } catch (ex) {}
  106. try {
  107. fs.mkdirSync(path.join(__dirname, 'bin', modPath));
  108. } catch (ex) {}
  109. try {
  110. fs.statSync(targetPath);
  111. } catch (ex) {
  112. console.error('Build succeeded but target not found');
  113. process.exit(1);
  114. }
  115. fs.renameSync(targetPath, installPath);
  116. console.log('Installed in `' + installPath + '`');
  117. }