nan-spec-runner.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. '-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE',
  62. ...platformFlags
  63. ].join(' ');
  64. const ldflags = [
  65. '-stdlib=libc++',
  66. '-fuse-ld=lld',
  67. `-L"${path.resolve(BASE, 'out', outDir, 'obj', 'buildtools', 'third_party', 'libc++abi')}"`,
  68. `-L"${path.resolve(BASE, 'out', outDir, 'obj', 'buildtools', 'third_party', 'libc++')}"`,
  69. '-lc++abi',
  70. ...platformFlags
  71. ].join(' ');
  72. if (process.platform !== 'win32') {
  73. env.CC = cc;
  74. env.CFLAGS = cxxflags;
  75. env.CXX = cxx;
  76. env.LD = ld;
  77. env.CXXFLAGS = cxxflags;
  78. env.LDFLAGS = ldflags;
  79. }
  80. const { status: buildStatus } = cp.spawnSync(NPX_CMD, ['node-gyp', 'rebuild', '--verbose', '--directory', 'test', '-j', 'max'], {
  81. env,
  82. cwd: NAN_DIR,
  83. stdio: 'inherit'
  84. });
  85. if (buildStatus !== 0) {
  86. console.error('Failed to build nan test modules');
  87. return process.exit(buildStatus);
  88. }
  89. const { status: installStatus } = cp.spawnSync(NPX_CMD, [`yarn@${YARN_VERSION}`, 'install'], {
  90. env,
  91. cwd: NAN_DIR,
  92. stdio: 'inherit'
  93. });
  94. if (installStatus !== 0) {
  95. console.error('Failed to install nan node_modules');
  96. return process.exit(installStatus);
  97. }
  98. const onlyTests = args.only && args.only.split(',');
  99. const DISABLED_TESTS = new Set([
  100. 'nannew-test.js',
  101. 'buffer-test.js'
  102. ]);
  103. const testsToRun = fs.readdirSync(path.resolve(NAN_DIR, 'test', 'js'))
  104. .filter(test => !DISABLED_TESTS.has(test))
  105. .filter(test => {
  106. return !onlyTests || onlyTests.includes(test) || onlyTests.includes(test.replace('.js', '')) || onlyTests.includes(test.replace('-test.js', ''));
  107. })
  108. .map(test => `test/js/${test}`);
  109. const testChild = cp.spawn(utils.getAbsoluteElectronExec(), ['node_modules/.bin/tap', ...testsToRun], {
  110. env: {
  111. ...process.env,
  112. ELECTRON_RUN_AS_NODE: 'true'
  113. },
  114. cwd: NAN_DIR,
  115. stdio: 'inherit'
  116. });
  117. testChild.on('exit', (testCode) => {
  118. process.exit(testCode);
  119. });
  120. }
  121. main().catch((err) => {
  122. console.error('An unhandled error occurred in the nan spec runner', err);
  123. process.exit(1);
  124. });