compile_java.py 4.6 KB

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