f2c.py 1.4 KB

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