Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clang/gcc and macOS compatibility fixes #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion codegen.pri
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Codegen.name = Codegen
Codegen.input = SOURCES
Codegen.output = $$CODEGEN_OUT_PATH
Codegen.variable_out = SOURCES
Codegen.commands = python $$CODEGEN_DIR/codegen.py -pwd $$CODEGEN_SRC_PATH -cmdinput -include $$INCLUDEPATH -sourcefiles $$SOURCES $$HEADERS -o $$CODEGEN_OUT_PATH
Codegen.commands = python3 $$CODEGEN_DIR/codegen.py -pwd $$CODEGEN_SRC_PATH -cmdinput -include $$INCLUDEPATH -sourcefiles $$SOURCES $$HEADERS -o $$CODEGEN_OUT_PATH
Codegen.CONFIG += target_predeps combine
QMAKE_EXTRA_COMPILERS += Codegen
DEFINES += WITH_CODEGEN
44 changes: 24 additions & 20 deletions codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,18 @@ def DebugPrint(self):

class ScopedDeclare:
""" Recursive class that represents a scoped forward declare in the generated source code """
def __init__(self, TypeName, Name):
def __init__(self, TypeName, Name, UnderlyingType=None):
self.TypeName = TypeName
self.Name = Name
self.Children = []

def AddChild(self, TypeName, Name):
self.UnderlyingType = UnderlyingType

def AddChild(self, TypeName, Name, UnderlyingType=None):
for Child in self.Children:
if Child.TypeName == TypeName and Child.Name == Name:
return Child
NewDeclare = ScopedDeclare(TypeName, Name)

NewDeclare = ScopedDeclare(TypeName, Name, UnderlyingType)
self.Children.append(NewDeclare)
return self.Children[-1]

Expand All @@ -131,8 +132,9 @@ def GenerateText(self, Indentation):
if self.Name:
IndentText = '\t' * Indentation
LineEnd = " {" if self.Children else ";"
OutText = "%s%s %s%s\n" % (IndentText, self.TypeName, self.Name, LineEnd)

UTSuffix = ": " + self.UnderlyingType if self.UnderlyingType else ""
OutText = "%s%s %s%s%s\n" % (IndentText, self.TypeName, self.Name, UTSuffix, LineEnd)

for Child in self.Children:
OutText += Child.GenerateText(Indentation + 1)

Expand Down Expand Up @@ -233,17 +235,16 @@ def CursorRecurse(self, Cursor, Depth):
break

Parent = Parent.semantic_parent

if DeclarationCursors:
Declaration = self.RootDeclare

for i in range(len(DeclarationCursors)-1, -1, -1):
Decl = DeclarationCursors[i]
TypeName = "namespace" if Decl.kind is clang.cindex.CursorKind.NAMESPACE else "struct"
Declaration = Declaration.AddChild(TypeName, Decl.spelling)

Declaration.AddChild("enum", NewEnum.Name)


Declaration = self.RootDeclare
for i in range(len(DeclarationCursors)-1, -1, -1):
Decl = DeclarationCursors[i]
TypeName = "namespace" if Decl.kind is clang.cindex.CursorKind.NAMESPACE else "struct"
Declaration = Declaration.AddChild(TypeName, Decl.spelling)

UnderlyingType = Child.enum_type.get_canonical().spelling
Declaration.AddChild("enum", NewEnum.Name, UnderlyingType)

# currently we're only testing enums - recurse for non-enum types to look for more enums
else:
self.CursorRecurse(Child, Depth + 1)
Expand Down Expand Up @@ -308,7 +309,10 @@ def RunCodegen():

# Clang index
#@todo - this definitely isn't portable??? how should I be setting this???
clang.cindex.Config.set_library_file('C:\\Program Files\\LLVM\\bin\\libclang.dll')
if sys.platform == "win32":
clang.cindex.Config.set_library_file('C:\\Program Files\\LLVM\\bin\\libclang.dll')
elif sys.platform == "darwin":
clang.cindex.Config.set_library_file('/usr/local/opt/llvm@6/lib/libclang.dylib')
ClangIndex = clang.cindex.Index.create()

# C++ environment
Expand Down Expand Up @@ -358,4 +362,4 @@ def RunCodegen():
if EnableProfiling:
cProfile.run('RunCodegen()')
else:
RunCodegen()
RunCodegen()
6 changes: 4 additions & 2 deletions template.mako
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ ${Decl.GenerateText(0)}
% endfor

% for Enum in Enums:
const CEnumNameMap TEnumReflection<enum ${Enum.FullName}>::skNameMap = {
template<>
const CEnumNameMap TEnumReflection<${Enum.FullName}>::skNameMap = {
<% ValueSet = set() %> \
% for Constant in Enum.Constants:
% if Constant.Value not in ValueSet:
Expand All @@ -16,6 +17,7 @@ const CEnumNameMap TEnumReflection<enum ${Enum.FullName}>::skNameMap = {
% endif
% endfor
};
const int TEnumReflection<enum ${Enum.FullName}>::skErrorValue = ${Enum.ErrorValue};
template<>
const int TEnumReflection<${Enum.FullName}>::skErrorValue = ${Enum.ErrorValue};

% endfor