nan-spec-runner.js 5.0 KB

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