v8_version_check.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 tempfile
  18. import urllib.request
  19. from pathlib import Path
  20. CACHE = Path.home () / '.cache/lwe-v8-headers'
  21. # the files that include V8 headers
  22. PATTERNS = (
  23. '/Scripting/', 'Objects/CImage.cpp', 'Objects/CMesh.cpp', 'Testing/Cases/ModuleNamespaceTiming.cpp',
  24. 'Testing/Cases/ScriptWatchdog.cpp', 'Testing/Cases/ScriptLimits.cpp'
  25. )
  26. def headers (version):
  27. target = CACHE / version
  28. if not (target / 'include/node/v8.h').exists ():
  29. target.mkdir (parents = True, exist_ok = True)
  30. url = f'https://nodejs.org/dist/{version}/node-{version}-headers.tar.gz'
  31. archive = target / 'headers.tar.gz'
  32. urllib.request.urlretrieve (url, archive)
  33. with tarfile.open (archive) as tar:
  34. for member in tar.getmembers ():
  35. member.name = member.name.split ('/', 1)[-1]
  36. if member.name:
  37. tar.extract (member, target, filter = 'data')
  38. archive.unlink ()
  39. return target / 'include/node'
  40. def v8_include (command):
  41. for argument in command:
  42. if argument.startswith ('-I') or argument.startswith ('-isystem'):
  43. path = argument[2:] if argument.startswith ('-I') else argument[8:]
  44. if (Path (path) / 'v8.h').exists ():
  45. return path
  46. return None
  47. def check (entry, version, include):
  48. command = shlex.split (entry['command'])
  49. original = v8_include (command)
  50. swapped = []
  51. skip = False
  52. for argument in command:
  53. if skip:
  54. skip = False
  55. continue
  56. if argument == '-o':
  57. skip = True
  58. continue
  59. swapped.append (argument.replace (original, str (include)) if original else argument)
  60. # the file that derives from a V8 class is compiled for real: libnode has no RTTI, so a reference to the typeinfo of
  61. # one of V8's classes is a link error on the distributions whose headers need it (undefined reference to `typeinfo
  62. # for v8::ArrayBuffer::Allocator'), which -fsyntax-only never shows
  63. derives = entry['file'].endswith ('LimitedAllocator.cpp')
  64. if derives:
  65. with tempfile.TemporaryDirectory () as scratch:
  66. object_file = Path (scratch) / 'check.o'
  67. result = subprocess.run (
  68. swapped + ['-c', '-o', str (object_file)], cwd = entry['directory'], capture_output = True, text = True
  69. )
  70. symbols = subprocess.run (['nm', '-C', object_file], capture_output = True, text = True).stdout \
  71. if result.returncode == 0 else ''
  72. else:
  73. result = subprocess.run (swapped + ['-fsyntax-only'], cwd = entry['directory'], capture_output = True, text = True)
  74. symbols = ''
  75. problems = [line for line in result.stderr.splitlines () if 'error' in line or 'warning' in line]
  76. problems += [f'references {line.strip ()}' for line in symbols.splitlines () if 'typeinfo for v8::' in line]
  77. return version, entry['file'], result.returncode, problems
  78. def main ():
  79. parser = argparse.ArgumentParser (description = __doc__, formatter_class = argparse.RawDescriptionHelpFormatter)
  80. parser.add_argument ('build', type = Path, help = 'configured build directory')
  81. parser.add_argument ('versions', nargs = '+', help = 'Node.js releases, e.g. v22.23.1')
  82. args = parser.parse_args ()
  83. commands = json.loads ((args.build / 'compile_commands.json').read_text ())
  84. entries = {entry['file']: entry for entry in commands if any (pattern in entry['file'] for pattern in PATTERNS)}
  85. jobs = [(entry, version, headers (version)) for version in args.versions for entry in entries.values ()]
  86. failed = False
  87. with concurrent.futures.ThreadPoolExecutor (os.cpu_count ()) as pool:
  88. for version, file, code, problems in pool.map (lambda job: check (*job), jobs):
  89. if code != 0 or problems:
  90. failed = True
  91. print (f'{version} {file}')
  92. print ('\n'.join (' ' + line for line in problems[:20]))
  93. print (f'{len (jobs)} compiles, {"failures" if failed else "all clean"}')
  94. sys.exit (1 if failed else 0)
  95. if __name__ == '__main__':
  96. main ()