v8_version_check.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env python3
  2. """Compiles the scripting sources against the V8 headers of several Node.js releases (-fsyntax-only), since
  3. distributions ship different V8 versions in libnode (Ubuntu 24.04: 10.2, Fedora 44: 12.4/13.6, current Node: 14.x).
  4. tools/v8_version_check.py build v18.19.1 v22.23.1 v24.18.0 v26.10.0
  5. Takes the compile commands of an already configured build (CMAKE_EXPORT_COMPILE_COMMANDS is on), swaps the V8
  6. include directory for each release's headers (downloaded from nodejs.org into ~/.cache/lwe-v8-headers) and prints
  7. every error or warning. Exits 1 when anything fails.
  8. """
  9. import argparse
  10. import concurrent.futures
  11. import json
  12. import os
  13. import shlex
  14. import subprocess
  15. import sys
  16. import tarfile
  17. import urllib.request
  18. from pathlib import Path
  19. CACHE = Path.home () / '.cache/lwe-v8-headers'
  20. # the files that include V8 headers
  21. PATTERNS = ('/Scripting/', 'Objects/CImage.cpp', 'Objects/CMesh.cpp', 'Testing/Cases/ModuleNamespaceTiming.cpp')
  22. def headers (version):
  23. target = CACHE / version
  24. if not (target / 'include/node/v8.h').exists ():
  25. target.mkdir (parents = True, exist_ok = True)
  26. url = f'https://nodejs.org/dist/{version}/node-{version}-headers.tar.gz'
  27. archive = target / 'headers.tar.gz'
  28. urllib.request.urlretrieve (url, archive)
  29. with tarfile.open (archive) as tar:
  30. for member in tar.getmembers ():
  31. member.name = member.name.split ('/', 1)[-1]
  32. if member.name:
  33. tar.extract (member, target, filter = 'data')
  34. archive.unlink ()
  35. return target / 'include/node'
  36. def v8_include (command):
  37. for argument in command:
  38. if argument.startswith ('-I') or argument.startswith ('-isystem'):
  39. path = argument[2:] if argument.startswith ('-I') else argument[8:]
  40. if (Path (path) / 'v8.h').exists ():
  41. return path
  42. return None
  43. def check (entry, version, include):
  44. command = shlex.split (entry['command'])
  45. original = v8_include (command)
  46. swapped = []
  47. skip = False
  48. for argument in command:
  49. if skip:
  50. skip = False
  51. continue
  52. if argument == '-o':
  53. skip = True
  54. continue
  55. swapped.append (argument.replace (original, str (include)) if original else argument)
  56. result = subprocess.run (swapped + ['-fsyntax-only'], cwd = entry['directory'], capture_output = True, text = True)
  57. problems = [line for line in result.stderr.splitlines () if 'error' in line or 'warning' in line]
  58. return version, entry['file'], result.returncode, problems
  59. def main ():
  60. parser = argparse.ArgumentParser (description = __doc__, formatter_class = argparse.RawDescriptionHelpFormatter)
  61. parser.add_argument ('build', type = Path, help = 'configured build directory')
  62. parser.add_argument ('versions', nargs = '+', help = 'Node.js releases, e.g. v22.23.1')
  63. args = parser.parse_args ()
  64. commands = json.loads ((args.build / 'compile_commands.json').read_text ())
  65. entries = {entry['file']: entry for entry in commands if any (pattern in entry['file'] for pattern in PATTERNS)}
  66. jobs = [(entry, version, headers (version)) for version in args.versions for entry in entries.values ()]
  67. failed = False
  68. with concurrent.futures.ThreadPoolExecutor (os.cpu_count ()) as pool:
  69. for version, file, code, problems in pool.map (lambda job: check (*job), jobs):
  70. if code != 0 or problems:
  71. failed = True
  72. print (f'{version} {file}')
  73. print ('\n'.join (' ' + line for line in problems[:20]))
  74. print (f'{len (jobs)} compiles, {"failures" if failed else "all clean"}')
  75. sys.exit (1 if failed else 0)
  76. if __name__ == '__main__':
  77. main ()