1
0
Fork 0
mirror of https://github.com/opentx/opentx.git synced 2025-07-13 11:29:51 +03:00
opentx/radio/util/generate_datacopy.py
2016-03-26 15:48:42 +01:00

48 lines
1.8 KiB
Python

#!/usr/bin/env python
from __future__ import print_function
import sys
import clang.cindex
import time
import os
if sys.platform == "darwin":
clang.cindex.Config.set_library_file('/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libclang.dylib')
structs = []
def build_struct(cursor):
structs.append(cursor.spelling)
print("template <class A, class B>\nvoid copy%s(A * dest, B * src)\n{" % cursor.spelling)
for c in cursor.get_children():
if c.kind == clang.cindex.CursorKind.FIELD_DECL:
if c.type.get_array_size() > 0:
if c.type.get_array_element_type().spelling in structs:
print(" for (int i=0; i<%d; i++) {" % c.type.get_array_size())
print(" copy%s(&dest->%s[i], &src->%s[i]);" % (c.type.get_array_element_type().spelling, c.spelling, c.spelling))
print(" }")
else:
print(" memcpy(dest->%s, src->%s, sizeof(dest->%s));" % (c.spelling, c.spelling, c.spelling))
elif c.type.get_declaration().spelling in structs:
print(" copy%s(&dest->%s, &src->%s);" % (c.type.get_declaration().spelling, c.spelling, c.spelling))
else:
print(" dest->%s = src->%s;" % (c.spelling, c.spelling))
print("}\n")
def build(cursor):
result = []
for c in cursor.get_children():
if c.kind == clang.cindex.CursorKind.STRUCT_DECL:
build_struct(c)
return result
def header():
print("//This file was auto-generated by %s script on %s. Do not edit this file!\n\n\n" % (os.path.basename(sys.argv[0]), time.asctime()))
index = clang.cindex.Index.create()
translation_unit = index.parse(sys.argv[1], ['-x', 'c++', '-std=c++11'] + sys.argv[2:])
header()
build(translation_unit.cursor)