build_java_with_error_prone2.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_classpath(cmd):
  21. for i, part in enumerate(cmd):
  22. if part == '-classpath':
  23. i += 1
  24. if i < len(cmd):
  25. return cmd[i]
  26. else:
  27. return None
  28. return None
  29. def parse_args(argv):
  30. parsed = []
  31. for i in range(len(argv)):
  32. if not argv[i].startswith('-'):
  33. parsed.append(argv[i])
  34. if len(parsed) >= 3:
  35. break
  36. return parsed + [argv[i + 1 :]]
  37. def fix_cmd_line(error_prone_tool, cmd):
  38. if not error_prone_tool:
  39. return cmd
  40. for f in cmd:
  41. if f.startswith('-Xep'):
  42. ERROR_PRONE_FLAGS.append(f)
  43. for f in ERROR_PRONE_FLAGS:
  44. if f in cmd:
  45. cmd.remove(f)
  46. if '-processor' in cmd:
  47. classpath = get_classpath(cmd)
  48. if classpath:
  49. error_prone_tool = error_prone_tool + os.pathsep + classpath
  50. return (
  51. cmd
  52. + JAVA10_EXPORTS
  53. + ['-processorpath', error_prone_tool, '-XDcompilePolicy=byfile']
  54. + [(' '.join(['-Xplugin:ErrorProne'] + ERROR_PRONE_FLAGS))]
  55. )
  56. # NOTE: legacy, only for "devtools/ya/jbuild"
  57. def just_do_it(argv):
  58. java, javac, error_prone_tool, javac_cmd = parse_args(argv)
  59. for f in javac_cmd:
  60. if f.startswith('-Xep'):
  61. ERROR_PRONE_FLAGS.append(f)
  62. for f in ERROR_PRONE_FLAGS:
  63. if f in javac_cmd:
  64. javac_cmd.remove(f)
  65. if '-processor' in javac_cmd:
  66. classpath = get_classpath(javac_cmd)
  67. if classpath:
  68. error_prone_tool = error_prone_tool + os.pathsep + classpath
  69. cmd = (
  70. [javac]
  71. + JAVA10_EXPORTS
  72. + ['-processorpath', error_prone_tool, '-XDcompilePolicy=byfile']
  73. + [(' '.join(['-Xplugin:ErrorProne'] + ERROR_PRONE_FLAGS))]
  74. + javac_cmd
  75. )
  76. if platform.system() == 'Windows':
  77. sys.exit(subprocess.Popen(cmd).wait())
  78. else:
  79. os.execv(cmd[0], cmd)
  80. if __name__ == '__main__':
  81. just_do_it(sys.argv[1:])