jni_swig.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import argparse
  2. import subprocess
  3. import re
  4. import os
  5. import tarfile
  6. def parse_args():
  7. parser = argparse.ArgumentParser(description='Wrapper script to invoke swig.')
  8. parser.add_argument('--swig', help='path to the swig executable')
  9. parser.add_argument('--default-module', type=str, help='swig -module argument value for inputs without %module statement')
  10. parser.add_argument('--package-by-file', help='path to file which dir must be converted to swig -package argument')
  11. parser.add_argument('--jsrc', help='jsrc output archive filename')
  12. parser.add_argument('--src', help='input .swg file path')
  13. parser.add_argument('--out-header', help='header file which must exist even if it was not generated by swig')
  14. parser.add_argument('args', nargs="*", help='regular swig arguments')
  15. return parser.parse_args()
  16. def path2pkg(path):
  17. return path.replace('/', '.').replace('-', '_')
  18. def main(args):
  19. package = path2pkg(os.path.dirname(args.package_by_file))
  20. outdir = None
  21. if args.jsrc:
  22. outdir = package.replace('.', '/')
  23. outdir_abs = os.path.join(os.path.dirname(args.jsrc), outdir)
  24. if not os.path.exists(outdir_abs):
  25. os.makedirs(outdir_abs)
  26. cmd = [args.swig, '-c++', '-java', '-package', package] + (['-outdir', outdir_abs] if outdir is not None else []) + args.args
  27. if '-module' not in args.args and args.default_module:
  28. with open(args.src, 'r') as f:
  29. if not re.search(r'(?m)^%module\b', f.read()):
  30. cmd += ['-module', args.default_module]
  31. subprocess.check_call(cmd + [args.src])
  32. if args.out_header and not os.path.exists(args.out_header):
  33. open(args.out_header, 'w').close()
  34. if args.jsrc:
  35. with tarfile.open(args.jsrc, 'a') as tf:
  36. tf.add(outdir_abs, arcname=outdir)
  37. if __name__ == '__main__':
  38. main(parse_args())