run_javac.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import sys
  2. import subprocess
  3. import optparse
  4. import re
  5. def parse_args():
  6. parser = optparse.OptionParser()
  7. parser.disable_interspersed_args()
  8. parser.add_option('--sources-list')
  9. parser.add_option('--verbose', default=False, action='store_true')
  10. parser.add_option('--remove-notes', default=False, action='store_true')
  11. parser.add_option('--ignore-errors', default=False, action='store_true')
  12. parser.add_option('--kotlin', default=False, action='store_true')
  13. return parser.parse_args()
  14. COLORING = {
  15. r'^(?P<path>.*):(?P<line>\d*): error: (?P<msg>.*)': lambda m: '[[unimp]]{path}[[rst]]:[[alt2]]{line}[[rst]]: [[c:light-red]]error[[rst]]: [[bad]]{msg}[[rst]]'.format(
  16. path=m.group('path'),
  17. line=m.group('line'),
  18. msg=m.group('msg'),
  19. ),
  20. r'^(?P<path>.*):(?P<line>\d*): warning: (?P<msg>.*)': lambda m: '[[unimp]]{path}[[rst]]:[[alt2]]{line}[[rst]]: [[c:light-yellow]]warning[[rst]]: {msg}'.format(
  21. path=m.group('path'),
  22. line=m.group('line'),
  23. msg=m.group('msg'),
  24. ),
  25. r'^warning: ': lambda m: '[[c:light-yellow]]warning[[rst]]: ',
  26. r'^error: (?P<msg>.*)': lambda m: '[[c:light-red]]error[[rst]]: [[bad]]{msg}[[rst]]'.format(msg=m.group('msg')),
  27. r'^Note: ': lambda m: '[[c:light-cyan]]Note[[rst]]: ',
  28. }
  29. def colorize(err):
  30. for regex, sub in COLORING.iteritems():
  31. err = re.sub(regex, sub, err, flags=re.MULTILINE)
  32. return err
  33. def remove_notes(err):
  34. return '\n'.join([line for line in err.split('\n') if not line.startswith('Note:')])
  35. def find_javac(cmd):
  36. if not cmd:
  37. return None
  38. if cmd[0].endswith('javac') or cmd[0].endswith('javac.exe'):
  39. return cmd[0]
  40. if len(cmd) > 2 and cmd[1].endswith('build_java_with_error_prone.py'):
  41. for javas in ('java', 'javac'):
  42. if cmd[2].endswith(javas) or cmd[2].endswith(javas + '.exe'):
  43. return cmd[2]
  44. return None
  45. # temporary, for jdk8/jdk9+ compatibility
  46. def fix_cmd(cmd):
  47. if not cmd:
  48. return cmd
  49. javac = find_javac(cmd)
  50. if not javac:
  51. return cmd
  52. p = subprocess.Popen([javac, '-version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  53. out, err = p.communicate()
  54. out, err = out.strip(), err.strip()
  55. for prefix in ('javac 1.8', 'java version "1.8'):
  56. for raw_out in ((out or ''), (err or '')):
  57. for line in raw_out.split('\n'):
  58. if line.startswith(prefix):
  59. res = []
  60. i = 0
  61. while i < len(cmd):
  62. for option in ('--add-exports', '--add-modules'):
  63. if cmd[i] == option:
  64. i += 1
  65. break
  66. elif cmd[i].startswith(option + '='):
  67. break
  68. else:
  69. res.append(cmd[i])
  70. i += 1
  71. return res
  72. return cmd
  73. def main():
  74. opts, cmd = parse_args()
  75. with open(opts.sources_list) as f:
  76. input_files = f.read().strip().split()
  77. if opts.kotlin:
  78. input_files = [i for i in input_files if i.endswith('.kt')]
  79. if not input_files:
  80. if opts.verbose:
  81. sys.stderr.write('No files to compile, javac is not launched.\n')
  82. else:
  83. p = subprocess.Popen(fix_cmd(cmd), stderr=subprocess.PIPE)
  84. _, err = p.communicate()
  85. rc = p.wait()
  86. if opts.remove_notes:
  87. err = remove_notes(err)
  88. try:
  89. err = colorize(err)
  90. except Exception:
  91. pass
  92. if opts.ignore_errors and rc:
  93. sys.stderr.write('error: javac actually failed with exit code {}\n'.format(rc))
  94. rc = 0
  95. sys.stderr.write(err)
  96. sys.exit(rc)
  97. if __name__ == '__main__':
  98. main()