nan-spec-runner.js 5.1 KB

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