MakeHeader.py 2.51 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
#!/usr/bin/env python
Hisham Muhammad's avatar
Hisham Muhammad committed
2
import os, sys, string
3
4
5
6
try:
   from cStringIO import StringIO
except:
   from StringIO import StringIO
Hisham Muhammad's avatar
Hisham Muhammad committed
7
8
9
10
11
12
13

ANY=1
COPY=2
SKIP=3
SKIPONE=4

state = ANY
14
static = 0
Hisham Muhammad's avatar
Hisham Muhammad committed
15
16
17
18

file = open(sys.argv[1])
name = sys.argv[1][:-2]

19
out = StringIO()
20

Hisham Muhammad's avatar
Hisham Muhammad committed
21
22
selfheader = '#include "' + name + '.h"'

23
24
out.write( "/* Do not edit this file. It was automatically generated. */\n" )
out.write( "\n" )
Hisham Muhammad's avatar
Hisham Muhammad committed
25

26
27
out.write( "#ifndef HEADER_" + os.path.basename(name) + "\n")
out.write( "#define HEADER_" + os.path.basename(name) + "\n")
Hisham Muhammad's avatar
Hisham Muhammad committed
28
is_blank = False
Hisham Muhammad's avatar
Hisham Muhammad committed
29
30
31
32
33
34
35
for line in file.readlines():
   line = line[:-1]
   if state == ANY:
      if line == '/*{':
         state = COPY
      elif line == selfheader:
         pass
Hisham Muhammad's avatar
Hisham Muhammad committed
36
37
      elif line.find("#include") == 0:
         pass
38
      elif line.find("htop - ") == 0 and line[-2:] == ".c":
39
         out.write(line[:-2] + ".h\n")
40
41
42
43
44
45
46
47
48
49
      elif line.find("static ") != -1:
         if line[-1] == "{":
            state = SKIP
            static = 1
         else:
            state = SKIPONE
      elif len(line) > 1:
         static = 0
         equals = line.find(" = ")
         if line[-3:] == "= {":
50
            out.write( "extern " + line[:-4] + ";\n" )
51
52
            state = SKIP
         elif equals != -1:
53
            out.write("extern " + line[:equals] + ";\n" )
Hisham Muhammad's avatar
Hisham Muhammad committed
54
55
         elif line.startswith("typedef struct"):
            state = SKIP
56
         elif line[-1] == "{":
57
            out.write( line[:-2].replace("inline", "extern") + ";\n" )
58
59
            state = SKIP
         else:
60
            out.write( line + "\n")
Hisham Muhammad's avatar
Hisham Muhammad committed
61
62
63
         is_blank = False
      elif line == "":
         if not is_blank:
64
            out.write( line + "\n")
Hisham Muhammad's avatar
Hisham Muhammad committed
65
         is_blank = True
Hisham Muhammad's avatar
Hisham Muhammad committed
66
      else:
67
         out.write( line  + "\n")
Hisham Muhammad's avatar
Hisham Muhammad committed
68
         is_blank = False
Hisham Muhammad's avatar
Hisham Muhammad committed
69
   elif state == COPY:
Hisham Muhammad's avatar
Hisham Muhammad committed
70
      is_blank = False
Hisham Muhammad's avatar
Hisham Muhammad committed
71
72
73
      if line == "}*/":
         state = ANY
      else:
74
         out.write( line + "\n")
Hisham Muhammad's avatar
Hisham Muhammad committed
75
   elif state == SKIP:
Hisham Muhammad's avatar
Hisham Muhammad committed
76
      is_blank = False
Hisham Muhammad's avatar
Hisham Muhammad committed
77
      if len(line) >= 1 and line[0] == "}":
78
79
80
81
82
         if static == 1:
            state = SKIPONE
         else:
            state = ANY
         static = 0
Hisham Muhammad's avatar
Hisham Muhammad committed
83
   elif state == SKIPONE:
Hisham Muhammad's avatar
Hisham Muhammad committed
84
      is_blank = False
Hisham Muhammad's avatar
Hisham Muhammad committed
85
86
      state = ANY

87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
out.write( "\n" )
out.write( "#endif\n" )

# only write a new .h file if something changed.
# This prevents a lot of recompilation during development
out.seek(0)
try:
   with open(name + ".h", "r") as orig:
      origcontents = orig.readlines()
except:
   origcontents = ""
if origcontents != out.readlines():
   with open(name + ".h", "w") as new:
      print("Writing "+name+".h")
      new.write(out.getvalue())
out.close()