CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/Tools/ardupilotwaf/mavgen.py
Views: 1798
1
# encoding: utf-8
2
# (c) Siddharth Bharat Purohit, 3DRobotics Inc.
3
4
"""
5
The **mavgen.py** program is a code generator which creates mavlink header files.
6
"""
7
8
from waflib import Logs, Task, Utils, Node
9
from waflib.TaskGen import feature, before_method, extension
10
import os, sys
11
import os.path
12
from xml.etree import ElementTree as et
13
14
class mavgen(Task.Task):
15
"""generate mavlink header files"""
16
color = 'BLUE'
17
before = 'cxx c'
18
19
def scan(self):
20
nodes = []
21
names = []
22
23
entry_point = self.inputs[0]
24
queue = [entry_point]
25
head = 0
26
27
while head < len(queue):
28
node = queue[head]
29
head += 1
30
31
tree = et.parse(node.abspath())
32
root = tree.getroot()
33
includes = root.findall('include')
34
for i in includes:
35
path = i.text.strip()
36
n = node.parent.find_node(path)
37
if n:
38
nodes.append(n)
39
if n not in queue:
40
queue.append(n)
41
continue
42
43
path = os.path.join(
44
node.parent.path_from(entry_point.parent),
45
path
46
)
47
if path not in names:
48
names.append(path)
49
50
return nodes, names
51
52
def run(self):
53
sys.path.insert(0,self.env.get_flat('MAVLINK_DIR'))
54
from pymavlink.generator import mavgen
55
class mavgen_options:
56
language = 'C'
57
wire_protocol = '2.0'
58
validate = False
59
output = self.env.get_flat('OUTPUT_DIR')
60
xml = self.inputs[0].abspath()
61
if mavgen.mavgen(mavgen_options(), [xml]):
62
return 0
63
return 1
64
65
def post_run(self):
66
super(mavgen, self).post_run()
67
for header in self.generator.output_dir.ant_glob("*.h **/*.h", remove=False):
68
header.sig = header.cache_sig = self.cache_sig
69
70
def options(opt):
71
opt.load('python')
72
73
@feature('mavgen')
74
@before_method('process_source')
75
def process_mavgen(self):
76
if not hasattr(self, 'output_dir'):
77
self.bld.fatal('mavgen: missing option output_dir')
78
79
inputs = self.to_nodes(self.bld.srcnode.find_node(self.source))
80
outputs = []
81
82
self.source = []
83
84
if not isinstance(self.output_dir, Node.Node):
85
self.output_dir = self.bld.bldnode.find_or_declare(self.output_dir)
86
87
task = self.create_task('mavgen', inputs, outputs)
88
task.env['OUTPUT_DIR'] = self.output_dir.abspath()
89
90
task.env.env = dict(os.environ)
91
task.env.env['PYTHONPATH'] = task.env.MAVLINK_DIR
92
93
def configure(cfg):
94
"""
95
setup environment for mavlink header generator
96
"""
97
env = cfg.env
98
env.MAVLINK_DIR = cfg.srcnode.make_node('modules/mavlink/').abspath()
99
100