Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sztomi committed Feb 17, 2014
0 parents commit 5c3ba7c
Show file tree
Hide file tree
Showing 13 changed files with 3,716 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
@@ -0,0 +1,8 @@
build
obj
*.make
Makefile
nohup.out
*.pyc
.ycm_extra_conf.py
**/generated
8 changes: 8 additions & 0 deletions LICENSE
@@ -0,0 +1,8 @@
Copyright (C) 2014 Tamás Szelei


Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15 changes: 15 additions & 0 deletions README.md
@@ -0,0 +1,15 @@
# Implementing a code generator with libclang

This repository contains the full source code for my article about
[implementing a code generator with libclang](http://szelei.me/code-generator).
Please refer to the article for details.

## Building

The following is required to build this project:

* LLVM 3.2+
* Python 2.7
* Premake4 (http://industriousone.com/premake)

To build the project and run the code generator at the same time, run ./build.sh
6 changes: 6 additions & 0 deletions build.sh
@@ -0,0 +1,6 @@
#!/bin/sh
cd src
./boost_python_gen.py textcomponent.h
cd ..
premake4 gmake
make
23 changes: 23 additions & 0 deletions premake4.lua
@@ -0,0 +1,23 @@
-- this is a premake4 script
-- see http://industriousone.com/premake

solution "codegen"
language "c++"
includedirs { "./src", "/usr/include/python2.7" }
buildoptions { "-std=c++11" }
links { "boost_python", "boost_filesystem", "boost_system", "python2.7" }

configurations { "debug", "release" }

configuration "debug"
targetdir "build/debug"
flags "Symbols"
defines "DEBUG"

configuration "release"
targetdir "build/release"
defines { "RELEASE", "NDEBUG" }

project "codegen"
kind "ConsoleApp"
files "./src/**cc"
23 changes: 23 additions & 0 deletions src/bind.mako
@@ -0,0 +1,23 @@
#include <boost/python.hpp>
#include "${include_file}"

using namespace boost::python;

BOOST_PYTHON_MODULE(${module_name})
{
% for c in classes:
class_<${c.name}>("${c.name}")
% for f in c.functions:
% if not "hidden" in f.annotations:
.def("${f.name}", &${c.name}::${f.name})
% endif
% endfor
;
% endfor
}

void init_bindings()
{
Py_Initialize();
init${module_name}();
}
66 changes: 66 additions & 0 deletions src/boost_python_gen.py
@@ -0,0 +1,66 @@
#!/usr/bin/python
# vim: set fileencoding=utf-8

import sys
import os
import clang.cindex
import itertools
from mako.template import Template

def get_annotations(node):
return [c.displayname for c in node.get_children()
if c.kind == clang.cindex.CursorKind.ANNOTATE_ATTR]

class Function(object):
def __init__(self, cursor):
self.name = cursor.spelling
self.annotations = get_annotations(cursor)
self.access = cursor.access_specifier

class Class(object):
def __init__(self, cursor):
self.name = cursor.spelling
self.functions = []
self.annotations = get_annotations(cursor)

for c in cursor.get_children():
if (c.kind == clang.cindex.CursorKind.CXX_METHOD and
c.access_specifier == clang.cindex.AccessSpecifier.PUBLIC):
f = Function(c)
self.functions.append(f)

def build_classes(cursor):
result = []
for c in cursor.get_children():
if (c.kind == clang.cindex.CursorKind.CLASS_DECL
and c.location.file.name == sys.argv[1]):
a_class = Class(c)
result.append(a_class)
elif c.kind == clang.cindex.CursorKind.NAMESPACE:
child_classes = build_classes(c)
result.extend(child_classes)

return result


if len(sys.argv) != 2:
print("Usage: boost_python_gen.py [header file name]")
sys.exit()

clang.cindex.Config.set_library_file('/usr/local/lib/libclang.so')
index = clang.cindex.Index.create()
translation_unit = index.parse(sys.argv[1], ['-x', 'c++', '-std=c++11', '-D__CODE_GENERATOR__'])

classes = build_classes(translation_unit.cursor)
tpl = Template(filename='bind.mako')
rendered = tpl.render(
classes=classes,
module_name='CodegenExample',
include_file=sys.argv[1])

OUTPUT_DIR = 'generated'

if not os.path.isdir(OUTPUT_DIR): os.mkdir(OUTPUT_DIR)

with open("generated/{}.bind.cc".format(sys.argv[1]), "w") as f:
f.write(rendered)

0 comments on commit 5c3ba7c

Please sign in to comment.