cgo1_wrapper.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import argparse
  2. import shutil
  3. import subprocess
  4. import sys
  5. CGO1_SUFFIX = '.cgo1.go'
  6. def call(cmd, cwd, env=None):
  7. # sys.stderr.write('{}\n'.format(' '.join(cmd)))
  8. return subprocess.call(cmd, stdin=None, stderr=sys.stderr, stdout=sys.stdout, cwd=cwd, env=env)
  9. def process_file(source_root, source_prefix, build_root, build_prefix, src_path, comment_prefix):
  10. dst_path = '{}.tmp'.format(src_path)
  11. with open(src_path, 'r') as src_file, open(dst_path, 'w') as dst_file:
  12. for line in src_file:
  13. if line.startswith(comment_prefix):
  14. dst_file.write(line.replace(source_root, source_prefix).replace(build_root, build_prefix))
  15. else:
  16. dst_file.write(line)
  17. shutil.move(dst_path, src_path)
  18. if __name__ == '__main__':
  19. parser = argparse.ArgumentParser()
  20. parser.add_argument('--build-prefix', default='__ARCADIA_BUILD_ROOT_PREFIX__')
  21. parser.add_argument('--build-root', required=True)
  22. parser.add_argument('--cgo1-files', nargs='+', required=True)
  23. parser.add_argument('--cgo2-files', nargs='+', required=True)
  24. parser.add_argument('--source-prefix', default='__ARCADIA_SOURCE_ROOT_PREFIX__')
  25. parser.add_argument('--source-root', required=True)
  26. parser.add_argument('cgo1_cmd', nargs='*')
  27. args = parser.parse_args()
  28. exit_code = call(args.cgo1_cmd, args.source_root)
  29. if exit_code != 0:
  30. sys.exit(exit_code)
  31. for src_path in args.cgo1_files:
  32. process_file(args.source_root, args.source_prefix, args.build_root, args.build_prefix, src_path, '//')
  33. for src_path in args.cgo2_files:
  34. process_file(args.source_root, args.source_prefix, args.build_root, args.build_prefix, src_path, '#line')