-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathino2cpp.py
67 lines (54 loc) · 2.34 KB
/
ino2cpp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import glob
import clang.cindex
import shutil
import argparse
# Mac needs explicit reference to clang
import platform
if platform.system() == 'Darwin':
clang.cindex.Config.set_library_path('/Library/Developer/CommandLineTools/usr/lib/')
def filter_by_type(translation_unit, filter=None):
cursor = translation_unit.cursor
filtered_elts = []
for child in cursor.get_children():
if filter:
if child.kind == filter:
filtered_elts.append(child)
return filtered_elts
def convert(ino_file, targetdir=None):
if not targetdir:
targetdir = '.'
index = clang.cindex.Index.create()
# Necessary arguments for clang to parse the translation unit
compilation_arguments = ['-E', '-x', 'c++']
# Parse ino file to generate function signatures and populate the header definitions file
tu = index.parse(ino_file, compilation_arguments)
function_definitions = []
functions = []
functions = filter_by_type(tu, clang.cindex.CursorKind.FUNCTION_DECL)
for f in functions:
function_definitions.append('%s %s;' % (f.result_type.spelling, f.displayname))
# Write to header file
ino_file_name = ino_file.split('/')[-1].split('.')[0]
print('writing to %s/%s.cpp' % (targetdir, ino_file_name))
with open(targetdir + '/' + ino_file_name + '.h', 'w') as f:
f.writelines(['%s\n' % x for x in function_definitions])
# Copy INO file to targetdir
shutil.copy(ino_file, targetdir + '/' + ino_file_name + '.cpp')
# Insert #include <Arduino.h> and #include <self.h> in target CPP files)
with open(targetdir + '/' + ino_file_name + '.cpp', 'r+') as f:
content = f.read()
f.seek(0, 0)
f.write('#include <Arduino.h>\n#include \"%s\"\n%s' % (ino_file_name + '.h', content))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='ino2cpp converts arduino INO files to C++ files')
parser.add_argument('ino_file', nargs='+', help='INO file')
parser.add_argument('-o', '--output', help='Target directory')
args = parser.parse_args()
# Output directory
if args.output and not os.path.exists(args.output):
print('Creating output directory %s' % (args.output))
os.makedirs(args.output)
# Generate .cpp and .h files
for f in args.ino_file:
convert(f, args.output)