MakeHeader.py 2.59 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
15
16

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

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

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

22
out = StringIO()
23

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

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

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

90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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()