nan-spec-runner.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. const cp = require('node:child_process');
  2. const fs = require('node:fs');
  3. const path = require('node:path');
  4. const BASE = path.resolve(__dirname, '../..');
  5. const NAN_DIR = path.resolve(BASE, 'third_party', 'nan');
  6. const NPX_CMD = process.platform === 'win32' ? 'npx.cmd' : 'npx';
  7. const utils = require('./lib/utils');
  8. const { YARN_VERSION } = require('./yarn');
  9. if (!require.main) {
  10. throw new Error('Must call the nan spec runner directly');
  11. }
  12. const args = require('minimist')(process.argv.slice(2), {
  13. string: ['only']
  14. });
  15. async function main () {
  16. const outDir = utils.getOutDir({ shouldLog: true });
  17. const nodeDir = path.resolve(BASE, 'out', outDir, 'gen', 'node_headers');
  18. const env = {
  19. ...process.env,
  20. npm_config_nodedir: nodeDir,
  21. npm_config_msvs_version: '2019',
  22. npm_config_arch: process.env.NPM_CONFIG_ARCH,
  23. npm_config_yes: 'true'
  24. };
  25. const clangDir = path.resolve(BASE, 'third_party', 'llvm-build', 'Release+Asserts', 'bin');
  26. const cc = path.resolve(clangDir, 'clang');
  27. const cxx = path.resolve(clangDir, 'clang++');
  28. const ld = path.resolve(clangDir, 'lld');
  29. const platformFlags = [];
  30. if (process.platform === 'darwin') {
  31. const sdkPath = path.resolve(BASE, 'out', outDir, 'sdk', 'xcode_links');
  32. const sdks = (await fs.promises.readdir(sdkPath)).filter(fileName => fileName.endsWith('.sdk'));
  33. const sdkToUse = sdks[0];
  34. if (!sdkToUse) {
  35. console.error('Could not find an SDK to use for the NAN tests');
  36. process.exit(1);
  37. }
  38. if (sdks.length) {
  39. console.warn(`Multiple SDKs found in the xcode_links directory - using ${sdkToUse}`);
  40. }
  41. platformFlags.push(
  42. `-isysroot ${path.resolve(sdkPath, sdkToUse)}`
  43. );
  44. }
  45. // TODO(ckerr) this is cribbed from read obj/electron/electron_app.ninja.
  46. // Maybe it would be better to have this script literally open up that
  47. // file and pull cflags_cc from it instead of using bespoke code here?
  48. // I think it's unlikely to work; but if it does, it would be more futureproof
  49. const cxxflags = [
  50. '-std=c++20',
  51. '-Wno-trigraphs',
  52. '-fno-exceptions',
  53. '-fno-rtti',
  54. '-nostdinc++',
  55. `-isystem "${path.resolve(BASE, 'buildtools', 'third_party', 'libc++')}"`,
  56. `-isystem "${path.resolve(BASE, 'third_party', 'libc++', 'src', 'include')}"`,
  57. `-isystem "${path.resolve(BASE, 'third_party', 'libc++abi', 'src', 'include')}"`,
  58. ' -fvisibility-inlines-hidden',
  59. '-fPIC',
  60. '-D_LIBCPP_ABI_NAMESPACE=Cr',
  61. ...platformFlags
  62. ].join(' ');
  63. const ldflags = [
  64. '-stdlib=libc++',
  65. '-fuse-ld=lld',
  66. `-L"${path.resolve(BASE, 'out', outDir, 'obj', 'buildtools', 'third_party', 'libc++abi')}"`,
  67. `-L"${path.resolve(BASE, 'out', outDir, 'obj', 'buildtools', 'third_party', 'libc++')}"`,
  68. '-lc++abi',
  69. ...platformFlags
  70. ].join(' ');
  71. if (process.platform !== 'win32') {
  72. env.CC = cc;
  73. env.CFLAGS = cxxflags;
  74. env.CXX = cxx;
  75. env.LD = ld;
  76. env.CXXFLAGS = cxxflags;
  77. env.LDFLAGS = ldflags;
  78. }
  79. const { status: buildStatus } = cp.spawnSync(NPX_CMD, ['node-gyp', 'rebuild', '--verbose', '--directory', 'test', '-j', 'max'], {
  80. env,
  81. cwd: NAN_DIR,
  82. stdio: 'inherit'
  83. });
  84. if (buildStatus !== 0) {
  85. console.error('Failed to build nan test modules');
  86. return process.exit(buildStatus);
  87. }
  88. const { status: installStatus } = cp.spawnSync(NPX_CMD, [`yarn@${YARN_VERSION}`, 'install'], {
  89. env,
  90. cwd: NAN_DIR,
  91. stdio: 'inherit'
  92. });
  93. if (installStatus !== 0) {
  94. console.error('Failed to install nan node_modules');
  95. return process.exit(installStatus);
  96. }
  97. const onlyTests = args.only && args.only.split(',');
  98. const DISABLED_TESTS = new Set([
  99. 'nannew-test.js',
  100. 'buffer-test.js'
  101. ]);
  102. const testsToRun = fs.readdirSync(path.resolve(NAN_DIR, 'test', 'js'))
  103. .filter(test => !DISABLED_TESTS.has(test))
  104. .filter(test => {
  105. return !onlyTests || onlyTests.includes(test) || onlyTests.includes(test.replace('.js', '')) || onlyTests.includes(test.replace('-test.js', ''));
  106. })
  107. .map(test => `test/js/${test}`);
  108. const testChild = cp.spawn(utils.getAbsoluteElectronExec(), ['node_modules/.bin/tap', ...testsToRun], {
  109. env: {
  110. ...process.env,
  111. ELECTRON_RUN_AS_NODE: 'true'
  112. },
  113. cwd: NAN_DIR,
  114. stdio: 'inherit'
  115. });
  116. testChild.on('exit', (testCode) => {
  117. process.exit(testCode);
  118. });
  119. }
  120. main().catch((err) => {
  121. console.error('An unhandled error occurred in the nan spec runner', err);
  122. process.exit(1);
  123. });