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