build_java_codenav_index.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import sys
  2. import re
  3. import os
  4. import subprocess
  5. FAKE_ARCADIA_ROOT = 'fake_arcadia_root'
  6. FAKE_BUILD_ROOT = 'fake_build_root'
  7. def modify_sources_file(origin, target, source_roots_map):
  8. def _cut_source_root(src):
  9. for pref, fake_root in source_roots_map.items():
  10. if src.startswith(pref):
  11. return os.path.join(fake_root, os.path.relpath(src, pref))
  12. return src
  13. with open(origin) as o:
  14. srcs = [i for line in o for i in re.split('\\s+', line) if i]
  15. new_srcs = map(_cut_source_root, srcs)
  16. with open(target, 'w') as t:
  17. t.write(' '.join(new_srcs))
  18. def just_do_it(argv):
  19. corpus_name, build_root, arcadia_root, sources_file, javac_tail_cmd = argv[0], argv[1], argv[2], argv[3], argv[4:]
  20. fake_arcadia_root = os.path.join(build_root, FAKE_ARCADIA_ROOT)
  21. fake_build_root = os.path.join(build_root, FAKE_BUILD_ROOT)
  22. fake_source_roots = {
  23. arcadia_root: fake_arcadia_root,
  24. build_root: fake_build_root,
  25. }
  26. modify_sources_file(
  27. sources_file,
  28. os.path.join(os.path.dirname(sources_file), '_' + os.path.basename(sources_file)),
  29. fake_source_roots,
  30. )
  31. kindex_data_root = '{}/kindex'.format(os.path.join(build_root, os.path.dirname(corpus_name)))
  32. if not os.path.exists(kindex_data_root):
  33. os.makedirs(kindex_data_root)
  34. env = os.environ.copy()
  35. env['KYTHE_ROOT_DIRECTORY'] = build_root
  36. env['KYTHE_OUTPUT_DIRECTORY'] = kindex_data_root
  37. env['KYTHE_CORPUS'] = os.path.relpath(corpus_name, build_root)
  38. os.symlink(arcadia_root, fake_arcadia_root)
  39. os.symlink(build_root, fake_build_root)
  40. try:
  41. subprocess.check_call(javac_tail_cmd, env=env)
  42. finally:
  43. os.unlink(fake_arcadia_root)
  44. os.unlink(fake_build_root)
  45. if __name__ == '__main__':
  46. just_do_it(sys.argv[1:])