compile_java.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import argparse
  2. import contextlib
  3. from shutil import copytree
  4. import os
  5. import shutil
  6. import subprocess as sp
  7. import tarfile
  8. import zipfile
  9. import sys
  10. import process_command_files as pcf
  11. import java_command_file as jcf
  12. def parse_args(args):
  13. parser = argparse.ArgumentParser(description='Wrapper to invoke Java compilation from ya make build')
  14. parser.add_argument('--javac-bin', help='path to javac')
  15. parser.add_argument('--jar-bin', help='path to jar tool')
  16. parser.add_argument('--java-bin', help='path to java binary')
  17. parser.add_argument('--kotlin-compiler', help='path to kotlin compiler jar file')
  18. parser.add_argument('--vcs-mf', help='path to VCS info manifest snippet')
  19. parser.add_argument('--package-prefix', help='package prefix for resource files')
  20. parser.add_argument('--jar-output', help='jar file with compiled classes destination path')
  21. parser.add_argument('--srcs-jar-output', help='jar file with sources destination path')
  22. parser.add_argument('srcs', nargs="*")
  23. args = parser.parse_args(args)
  24. return args, args.srcs
  25. def mkdir_p(directory):
  26. if not os.path.exists(directory):
  27. os.makedirs(directory)
  28. def split_cmd_by_delim(cmd, delim='DELIM'):
  29. result = [[]]
  30. for arg in cmd:
  31. if arg == delim:
  32. result.append([])
  33. else:
  34. result[-1].append(arg)
  35. return result
  36. def main():
  37. loaded_args = pcf.get_args(sys.argv[1:])
  38. cmd_parts = split_cmd_by_delim(loaded_args)
  39. assert len(cmd_parts) == 4
  40. args, javac_opts, peers, ktc_opts = cmd_parts
  41. opts, jsrcs = parse_args(args)
  42. jsrcs += list(filter(lambda x: x.endswith('.jsrc'), peers))
  43. peers = list(filter(lambda x: not x.endswith('.jsrc'), peers))
  44. sources_dir = 'src'
  45. mkdir_p(sources_dir)
  46. for s in jsrcs:
  47. if s.endswith('.jsrc'):
  48. with contextlib.closing(tarfile.open(s, 'r')) as tf:
  49. tf.extractall(sources_dir)
  50. srcs = []
  51. for r, _, files in os.walk(sources_dir):
  52. for f in files:
  53. srcs.append(os.path.join(r, f))
  54. srcs += jsrcs
  55. ktsrcs = list(filter(lambda x: x.endswith('.kt'), srcs))
  56. srcs = list(filter(lambda x: x.endswith('.java'), srcs))
  57. classes_dir = 'cls'
  58. mkdir_p(classes_dir)
  59. classpath = os.pathsep.join(peers)
  60. if srcs:
  61. temp_sources_file = 'temp.sources.list'
  62. with open(temp_sources_file, 'w') as ts:
  63. ts.write(' '.join(srcs))
  64. if ktsrcs:
  65. kt_classes_dir = 'kt_cls'
  66. mkdir_p(kt_classes_dir)
  67. jcf.call_java_with_command_file(
  68. [
  69. opts.java_bin,
  70. '-Didea.max.content.load.filesize=30720',
  71. '-jar',
  72. opts.kotlin_compiler,
  73. '-classpath',
  74. classpath,
  75. '-d',
  76. kt_classes_dir,
  77. ]
  78. + ktc_opts,
  79. wrapped_args=ktsrcs + srcs,
  80. )
  81. classpath = os.pathsep.join([kt_classes_dir, classpath])
  82. if srcs:
  83. jcf.call_java_with_command_file(
  84. [opts.javac_bin, '-nowarn', '-g', '-classpath', classpath, '-encoding', 'UTF-8', '-d', classes_dir]
  85. + javac_opts,
  86. wrapped_args=srcs,
  87. )
  88. for s in jsrcs:
  89. if s.endswith('-sources.jar'):
  90. with zipfile.ZipFile(s) as zf:
  91. zf.extractall(sources_dir)
  92. elif s.endswith('.jar'):
  93. with zipfile.ZipFile(s) as zf:
  94. zf.extractall(classes_dir)
  95. if ktsrcs:
  96. copytree(kt_classes_dir, classes_dir, dirs_exist_ok=True)
  97. if opts.vcs_mf:
  98. sp.check_call([opts.jar_bin, 'cfm', opts.jar_output, opts.vcs_mf, os.curdir], cwd=classes_dir)
  99. else:
  100. sp.check_call([opts.jar_bin, 'cfM', opts.jar_output, os.curdir], cwd=classes_dir)
  101. if opts.srcs_jar_output:
  102. for s in jsrcs:
  103. if s.endswith('.java'):
  104. if opts.package_prefix:
  105. d = os.path.join(sources_dir, *(opts.package_prefix.split('.') + [os.path.basename(s)]))
  106. else:
  107. d = os.path.join(sources_dir, os.path.basename(s))
  108. shutil.copyfile(s, d)
  109. if opts.vcs_mf:
  110. sp.check_call([opts.jar_bin, 'cfm', opts.srcs_jar_output, opts.vcs_mf, os.curdir], cwd=sources_dir)
  111. else:
  112. sp.check_call([opts.jar_bin, 'cfM', opts.srcs_jar_output, os.curdir], cwd=sources_dir)
  113. if __name__ == '__main__':
  114. main()