f2c.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from __future__ import print_function
  2. import sys
  3. import subprocess
  4. import argparse
  5. import os
  6. header = '''\
  7. #ifdef __GNUC__
  8. #pragma GCC diagnostic push
  9. #pragma GCC diagnostic ignored "-Wunused-parameter"
  10. #pragma GCC diagnostic ignored "-Wmissing-braces"
  11. #pragma GCC diagnostic ignored "-Wuninitialized"
  12. #pragma GCC diagnostic ignored "-Wreturn-type"
  13. #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
  14. #endif
  15. '''
  16. footer = '''
  17. #ifdef __GNUC__
  18. #pragma GCC diagnostic pop
  19. #endif
  20. '''
  21. def mkdir_p(directory):
  22. if not os.path.exists(directory):
  23. os.makedirs(directory)
  24. if __name__ == '__main__':
  25. parser = argparse.ArgumentParser()
  26. parser.add_argument('-t', '--tool')
  27. parser.add_argument('-c', '--input')
  28. parser.add_argument('-o', '--output')
  29. args = parser.parse_args()
  30. tmpdir = args.output + '.f2c'
  31. mkdir_p(tmpdir)
  32. # should parse includes, really
  33. p = subprocess.Popen(
  34. [args.tool, '-w', '-R', '-a', '-I' + os.path.dirname(args.input), '-T' + tmpdir],
  35. stdout=subprocess.PIPE,
  36. stderr=subprocess.PIPE,
  37. stdin=subprocess.PIPE,
  38. )
  39. stdout, stderr = p.communicate(input=open(args.input).read())
  40. ret = p.wait()
  41. if ret:
  42. print('f2c failed: %s, %s' % (stderr, ret), file=sys.stderr)
  43. sys.exit(ret)
  44. if 'Error' in stderr:
  45. print(stderr, file=sys.stderr)
  46. with open(args.output, 'w') as f:
  47. f.write(header)
  48. f.write(stdout)
  49. f.write(footer)