compile_java.py 4.5 KB

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