Merge pull request #614 from dkgroot/EnhanceMakeHeader

Enh: scripts/MakeHeader script
This commit is contained in:
Hisham Muhammad 2017-04-24 23:30:42 -03:00 committed by GitHub
commit 975e22144e
1 changed files with 33 additions and 24 deletions

View File

@ -1,6 +1,9 @@
#!/usr/bin/env python #!/usr/bin/env python
import os, sys, string import os, sys, string
try:
from cStringIO import StringIO
except:
from StringIO import StringIO
ANY=1 ANY=1
COPY=2 COPY=2
@ -13,23 +16,15 @@ static = 0
file = open(sys.argv[1]) file = open(sys.argv[1])
name = sys.argv[1][:-2] name = sys.argv[1][:-2]
out = open(name + ".h", "w") out = StringIO()
class writer:
def __init__(self, file):
self.file = file
def write(self, text):
self.file.write(text + "\n")
out = writer(out)
print("Generating "+name+".h")
selfheader = '#include "' + name + '.h"' selfheader = '#include "' + name + '.h"'
out.write( "/* Do not edit this file. It was automatically generated. */" ) out.write( "/* Do not edit this file. It was automatically generated. */\n" )
out.write( "" ) out.write( "\n" )
out.write( "#ifndef HEADER_" + os.path.basename(name) ) out.write( "#ifndef HEADER_" + os.path.basename(name) + "\n")
out.write( "#define HEADER_" + os.path.basename(name) ) out.write( "#define HEADER_" + os.path.basename(name) + "\n")
is_blank = False is_blank = False
for line in file.readlines(): for line in file.readlines():
line = line[:-1] line = line[:-1]
@ -41,7 +36,7 @@ for line in file.readlines():
elif line.find("#include") == 0: elif line.find("#include") == 0:
pass pass
elif line.find("htop - ") == 0 and line[-2:] == ".c": elif line.find("htop - ") == 0 and line[-2:] == ".c":
out.write(line[:-2] + ".h") out.write(line[:-2] + ".h\n")
elif line.find("static ") != -1: elif line.find("static ") != -1:
if line[-1] == "{": if line[-1] == "{":
state = SKIP state = SKIP
@ -52,31 +47,31 @@ for line in file.readlines():
static = 0 static = 0
equals = line.find(" = ") equals = line.find(" = ")
if line[-3:] == "= {": if line[-3:] == "= {":
out.write( "extern " + line[:-4] + ";" ) out.write( "extern " + line[:-4] + ";\n" )
state = SKIP state = SKIP
elif equals != -1: elif equals != -1:
out.write("extern " + line[:equals] + ";" ) out.write("extern " + line[:equals] + ";\n" )
elif line.startswith("typedef struct"): elif line.startswith("typedef struct"):
state = SKIP state = SKIP
elif line[-1] == "{": elif line[-1] == "{":
out.write( line[:-2].replace("inline", "extern") + ";" ) out.write( line[:-2].replace("inline", "extern") + ";\n" )
state = SKIP state = SKIP
else: else:
out.write( line ) out.write( line + "\n")
is_blank = False is_blank = False
elif line == "": elif line == "":
if not is_blank: if not is_blank:
out.write( line ) out.write( line + "\n")
is_blank = True is_blank = True
else: else:
out.write( line ) out.write( line + "\n")
is_blank = False is_blank = False
elif state == COPY: elif state == COPY:
is_blank = False is_blank = False
if line == "}*/": if line == "}*/":
state = ANY state = ANY
else: else:
out.write( line ) out.write( line + "\n")
elif state == SKIP: elif state == SKIP:
is_blank = False is_blank = False
if len(line) >= 1 and line[0] == "}": if len(line) >= 1 and line[0] == "}":
@ -89,5 +84,19 @@ for line in file.readlines():
is_blank = False is_blank = False
state = ANY state = ANY
out.write( "" ) out.write( "\n" )
out.write( "#endif" ) 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()