aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/MakeHeader.py
blob: 4841bdac6a46079707cd4421a77a7af01a65fd62 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python
import os, sys, string
try:
   from cStringIO import StringIO
except ImportError:
   try:
      from StringIO import StringIO
   except ImportError:
      from io import StringIO

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

state = ANY
static = 0

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

out = StringIO()

selfheader = '#include "' + name + '.h"'

out.write( "/* Do not edit this file. It was automatically generated. */\n" )
out.write( "\n" )

out.write( "#ifndef HEADER_" + os.path.basename(name) + "\n")
out.write( "#define HEADER_" + os.path.basename(name) + "\n")
is_blank = False
for line in file.readlines():
   line = line[:-1]
   if state == ANY:
      if line == '/*{':
         state = COPY
      elif line == selfheader:
         pass
      elif line.find("#include") == 0:
         pass
      elif line.find("htop - ") == 0 and line[-2:] == ".c":
         out.write(line[:-2] + ".h\n")
      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:] == "= {":
            out.write( "extern " + line[:-4] + ";\n" )
            state = SKIP
         elif equals != -1:
            out.write("extern " + line[:equals] + ";\n" )
         elif line.startswith("typedef struct"):
            state = SKIP
         elif line[-1] == "{":
            out.write( line[:-2].replace("inline", "extern") + ";\n" )
            state = SKIP
         else:
            out.write( line + "\n")
         is_blank = False
      elif line == "":
         if not is_blank:
            out.write( line + "\n")
         is_blank = True
      else:
         out.write( line  + "\n")
         is_blank = False
   elif state == COPY:
      is_blank = False
      if line == "}*/":
         state = ANY
      else:
         out.write( line + "\n")
   elif state == SKIP:
      is_blank = False
      if len(line) >= 1 and line[0] == "}":
         if static == 1:
            state = SKIPONE
         else:
            state = ANY
         static = 0
   elif state == SKIPONE:
      is_blank = False
      state = ANY

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()

© 2014-2024 Faster IT GmbH | imprint | privacy policy