build_java_with_error_prone2.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import sys
  2. import os
  3. import re
  4. import subprocess
  5. import platform
  6. ERROR_PRONE_FLAGS = [
  7. '-Xep:FunctionalInterfaceMethodChanged:WARN',
  8. '-Xep:ReturnValueIgnored:WARN',
  9. ]
  10. JAVA10_EXPORTS = [
  11. '--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED',
  12. '--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED',
  13. '--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED',
  14. '--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED',
  15. '--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED',
  16. '--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED',
  17. '--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED',
  18. '--add-exports=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED',
  19. ]
  20. def get_java_version(exe):
  21. p = subprocess.Popen([exe, '-version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  22. out, err = p.communicate()
  23. for line in (out or '').strip().split("\n") + (err or '').strip().split("\n"):
  24. m = re.match(r'java version "(.+)"', line)
  25. if m:
  26. parts = m.groups()[0].split(".")
  27. return parts[1] if parts[0] == "1" else parts[0]
  28. m = re.match(r'openjdk version "(\d+).*"', line)
  29. if m:
  30. parts = m.groups()[0].split(".")
  31. return parts[0]
  32. return None
  33. def get_classpath(cmd):
  34. for i, part in enumerate(cmd):
  35. if part == '-classpath':
  36. i += 1
  37. if i < len(cmd):
  38. return cmd[i]
  39. else:
  40. return None
  41. return None
  42. def parse_args(argv):
  43. parsed = []
  44. for i in range(len(argv)):
  45. if not argv[i].startswith('-'):
  46. parsed.append(argv[i])
  47. if len(parsed) >= 3:
  48. break
  49. return parsed + [argv[i + 1 :]]
  50. def just_do_it(argv):
  51. java, javac, error_prone_tool, javac_cmd = parse_args(argv)
  52. ver = get_java_version(java)
  53. if not ver:
  54. raise Exception("Can't determine java version")
  55. if int(ver) >= 10:
  56. for f in javac_cmd:
  57. if f.startswith('-Xep'):
  58. ERROR_PRONE_FLAGS.append(f)
  59. for f in ERROR_PRONE_FLAGS:
  60. if f in javac_cmd:
  61. javac_cmd.remove(f)
  62. if '-processor' in javac_cmd:
  63. classpath = get_classpath(javac_cmd)
  64. if classpath:
  65. error_prone_tool = error_prone_tool + os.pathsep + classpath
  66. cmd = (
  67. [javac]
  68. + JAVA10_EXPORTS
  69. + ['-processorpath', error_prone_tool, '-XDcompilePolicy=byfile']
  70. + [(' '.join(['-Xplugin:ErrorProne'] + ERROR_PRONE_FLAGS))]
  71. + javac_cmd
  72. )
  73. else:
  74. cmd = (
  75. [java, '-Xbootclasspath/p:' + error_prone_tool, 'com.google.errorprone.ErrorProneCompiler']
  76. + ERROR_PRONE_FLAGS
  77. + javac_cmd
  78. )
  79. if platform.system() == 'Windows':
  80. sys.exit(subprocess.Popen(cmd).wait())
  81. else:
  82. os.execv(cmd[0], cmd)
  83. if __name__ == '__main__':
  84. just_do_it(sys.argv[1:])