MakeHeader.py 2.87 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
try:
   from cStringIO import StringIO
guoci's avatar
guoci committed
5
6
7
8
9
except ImportError:
   try:
      from StringIO import StringIO
   except ImportError:
      from io import StringIO
Hisham Muhammad's avatar
Hisham Muhammad committed
10
11
12
13
14

ANY=1
COPY=2
SKIP=3
SKIPONE=4
15
COPYDEFINE=5
Hisham Muhammad's avatar
Hisham Muhammad committed
16
17

state = ANY
18
static = 0
Hisham Muhammad's avatar
Hisham Muhammad committed
19
20
21
22

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

23
out = StringIO()
24

Hisham Muhammad's avatar
Hisham Muhammad committed
25
26
selfheader = '#include "' + name + '.h"'

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

30
31
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
32
is_blank = False
Hisham Muhammad's avatar
Hisham Muhammad committed
33
34
35
36
37
38
39
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
40
41
      elif line.find("#include") == 0:
         pass
42
      elif line.find("htop - ") == 0 and line[-2:] == ".c":
43
         out.write(line[:-2] + ".h\n")
44
45
46
47
48
49
50
51
52
      elif line.find("static ") != -1:
         if line[-1] == "{":
            state = SKIP
            static = 1
         else:
            state = SKIPONE
      elif len(line) > 1:
         static = 0
         equals = line.find(" = ")
53
54
55
56
57
         if line[:7] == "#define":
            if line[-1:] == "\\":
               state = COPYDEFINE
            out.write( line + "\n")
         elif line[-3:] == "= {":
58
            out.write( "extern " + line[:-4] + ";\n" )
59
60
            state = SKIP
         elif equals != -1:
61
            out.write("extern " + line[:equals] + ";\n" )
Hisham Muhammad's avatar
Hisham Muhammad committed
62
63
         elif line.startswith("typedef struct"):
            state = SKIP
64
         elif line[-1] == "{":
65
            out.write( line[:-2].replace("inline", "extern") + ";\n" )
66
67
            state = SKIP
         else:
68
            out.write( line + "\n" )
Hisham Muhammad's avatar
Hisham Muhammad committed
69
70
71
         is_blank = False
      elif line == "":
         if not is_blank:
72
            out.write( line + "\n")
Hisham Muhammad's avatar
Hisham Muhammad committed
73
         is_blank = True
Hisham Muhammad's avatar
Hisham Muhammad committed
74
      else:
75
         out.write( line  + "\n")
Hisham Muhammad's avatar
Hisham Muhammad committed
76
         is_blank = False
77
78
79
80
81
   elif state == COPYDEFINE:
      is_blank = False
      out.write( line + "\n")
      if line[-1:] != "\\":
         state = ANY
Hisham Muhammad's avatar
Hisham Muhammad committed
82
   elif state == COPY:
Hisham Muhammad's avatar
Hisham Muhammad committed
83
      is_blank = False
Hisham Muhammad's avatar
Hisham Muhammad committed
84
85
86
      if line == "}*/":
         state = ANY
      else:
87
         out.write( line + "\n")
Hisham Muhammad's avatar
Hisham Muhammad committed
88
   elif state == SKIP:
Hisham Muhammad's avatar
Hisham Muhammad committed
89
      is_blank = False
Hisham Muhammad's avatar
Hisham Muhammad committed
90
      if len(line) >= 1 and line[0] == "}":
91
92
93
94
95
         if static == 1:
            state = SKIPONE
         else:
            state = ANY
         static = 0
Hisham Muhammad's avatar
Hisham Muhammad committed
96
   elif state == SKIPONE:
Hisham Muhammad's avatar
Hisham Muhammad committed
97
      is_blank = False
Hisham Muhammad's avatar
Hisham Muhammad committed
98
99
      state = ANY

100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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()