aboutsummaryrefslogtreecommitdiffstats
path: root/build/scripts/f2c.py
blob: 243d3965a4b02d0d3f40cbc4f8b3a56f3b30061b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import sys 
import subprocess 
import argparse 
import os 
 

header = '''\
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wmissing-braces"
#pragma GCC diagnostic ignored "-Wuninitialized"
#pragma GCC diagnostic ignored "-Wreturn-type"
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#endif

'''

footer = '''
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
'''


def mkdir_p(directory):
    if not os.path.exists(directory):
        os.makedirs(directory)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
 
    parser.add_argument('-t', '--tool')
    parser.add_argument('-c', '--input')
    parser.add_argument('-o', '--output')
 
    args = parser.parse_args()
    tmpdir = args.output + '.f2c'
    mkdir_p(tmpdir)
    # should parse includes, really
    p = subprocess.Popen(
        [args.tool, '-w', '-R', '-a', '-I' + os.path.dirname(args.input), '-T' + tmpdir],
        stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
    stdout, stderr = p.communicate(input=open(args.input).read())
    ret = p.wait()
 
    if ret:
        print >>sys.stderr, 'f2c failed: %s, %s' % (stderr, ret)
        sys.exit(ret)
 
    if 'Error' in stderr:
        print >>sys.stderr, stderr
 
    with open(args.output, 'w') as f:
        f.write(header)
        f.write(stdout)
        f.write(footer)