2015-09-11 06:33:42 -03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
import os
|
2015-11-05 17:48:35 -04:00
|
|
|
import re
|
|
|
|
import codecs
|
|
|
|
|
|
|
|
class Scope(object):
|
|
|
|
"""
|
|
|
|
Single parameter group
|
|
|
|
"""
|
2015-11-10 15:16:48 -04:00
|
|
|
re_deep_lines = re.compile(r'.*\/.*\/')
|
2015-11-05 17:48:35 -04:00
|
|
|
def __init__(self, ):
|
|
|
|
self.scope = set()
|
|
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.scope.__str__()
|
|
|
|
|
|
|
|
def Add(self, scope):
|
|
|
|
"""
|
|
|
|
Add Scope to set
|
|
|
|
"""
|
|
|
|
self.scope.add(scope)
|
|
|
|
|
|
|
|
def Has(self, scope):
|
|
|
|
"""
|
|
|
|
Check for existance
|
|
|
|
"""
|
|
|
|
if len(self.scope) == 0:
|
|
|
|
return True
|
2015-11-10 15:16:48 -04:00
|
|
|
# Anything in the form xxxxx/yyyyy/zzzzz....
|
|
|
|
# is treated as xxxxx/yyyyy
|
|
|
|
while (self.re_deep_lines.match(scope)):
|
|
|
|
scope = os.path.dirname(scope)
|
2015-11-05 17:48:35 -04:00
|
|
|
return scope in self.scope
|
2015-11-10 15:16:48 -04:00
|
|
|
|
2015-11-05 17:48:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
class CMakeParser(object):
|
|
|
|
"""
|
|
|
|
Parses provided data and stores all found paths in scope.
|
|
|
|
"""
|
|
|
|
re_split_lines = re.compile(r'[\r\n]+')
|
|
|
|
re_comment = re.compile(r'^\#')
|
|
|
|
re_start = re.compile(r'set\s*\(\s*config_module_list')
|
|
|
|
re_end = re.compile(r'\)\s*')
|
|
|
|
|
|
|
|
def Parse(self, scope, contents):
|
|
|
|
"""
|
|
|
|
Incrementally parse cmake file contents and append all found path scope
|
|
|
|
to scope.
|
|
|
|
"""
|
|
|
|
# This code is essentially a comment-parsing grammar. "state"
|
|
|
|
# represents parser state. It contains human-readable state
|
|
|
|
# names.
|
|
|
|
state = None
|
|
|
|
for line in self.re_split_lines.split(contents):
|
|
|
|
line = line.strip()
|
|
|
|
# Ignore empty lines
|
|
|
|
if line == "":
|
|
|
|
continue
|
|
|
|
if self.re_comment.match(line):
|
|
|
|
continue
|
|
|
|
elif self.re_start.match(line):
|
|
|
|
state = "gather"
|
|
|
|
continue
|
|
|
|
elif state is not None and state == "gather":
|
|
|
|
if self.re_end.match(line):
|
|
|
|
return True
|
|
|
|
scope.Add(line)
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
if len(os.sys.argv) < 2:
|
2015-10-06 16:48:13 -03:00
|
|
|
print("Error in %s" % os.sys.argv[0])
|
2015-11-05 17:48:35 -04:00
|
|
|
print("Usage: %s <parameters.xml> [cmake-file-scoping] " % os.sys.argv[0])
|
2015-09-11 06:33:42 -03:00
|
|
|
raise SystemExit
|
|
|
|
|
2015-11-05 17:48:35 -04:00
|
|
|
|
|
|
|
scope = Scope()
|
|
|
|
if len(os.sys.argv) == 3:
|
|
|
|
with codecs.open(os.sys.argv[2], 'r', 'utf-8') as f:
|
|
|
|
try:
|
|
|
|
contents = f.read()
|
|
|
|
f.close()
|
|
|
|
parser = CMakeParser()
|
|
|
|
parser.Parse(scope, contents)
|
|
|
|
except:
|
|
|
|
contents = ''
|
|
|
|
print('Failed reading file: %s, skipping scoping.' % os.sys.argv[2])
|
|
|
|
pass
|
|
|
|
|
2015-09-11 16:41:00 -03:00
|
|
|
fp_header = open("px4_parameters.h", "w")
|
|
|
|
fp_src = open("px4_parameters.c", "w")
|
2015-09-11 06:33:42 -03:00
|
|
|
|
|
|
|
tree = ET.parse(os.sys.argv[1])
|
|
|
|
root = tree.getroot()
|
2015-09-11 16:41:00 -03:00
|
|
|
|
|
|
|
# Generate the header file content
|
|
|
|
header = """
|
2015-09-11 06:33:42 -03:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <systemlib/param/param.h>
|
|
|
|
|
|
|
|
// DO NOT EDIT
|
2015-09-12 06:26:20 -03:00
|
|
|
// This file is autogenerated from parameters.xml
|
|
|
|
|
|
|
|
__BEGIN_DECLS
|
2015-09-11 06:33:42 -03:00
|
|
|
|
2015-09-11 16:41:00 -03:00
|
|
|
struct px4_parameters_t {
|
2015-09-11 06:33:42 -03:00
|
|
|
"""
|
|
|
|
start_name = ""
|
|
|
|
end_name = ""
|
|
|
|
|
|
|
|
for group in root:
|
2015-11-09 04:31:43 -04:00
|
|
|
if group.tag == "group" and "no_code_generation" not in group.attrib:
|
2015-11-05 17:48:35 -04:00
|
|
|
section = """
|
2015-09-11 16:41:00 -03:00
|
|
|
/*****************************************************************
|
|
|
|
* %s
|
|
|
|
****************************************************************/""" % group.attrib["name"]
|
2015-09-11 06:33:42 -03:00
|
|
|
for param in group:
|
2015-11-05 17:48:35 -04:00
|
|
|
scope_ = param.find('scope').text
|
|
|
|
if not scope.Has(scope_):
|
|
|
|
continue
|
2015-09-11 06:33:42 -03:00
|
|
|
if not start_name:
|
|
|
|
start_name = param.attrib["name"]
|
|
|
|
end_name = param.attrib["name"]
|
2015-11-05 17:48:35 -04:00
|
|
|
header += section
|
|
|
|
section =""
|
2015-09-11 16:41:00 -03:00
|
|
|
header += """
|
|
|
|
const struct param_info_s __param__%s;""" % param.attrib["name"]
|
|
|
|
header += """
|
|
|
|
const unsigned int param_count;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern const struct px4_parameters_t px4_parameters;
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Generate the C file content
|
|
|
|
src = """
|
|
|
|
#include <px4_parameters.h>
|
|
|
|
|
|
|
|
// DO NOT EDIT
|
|
|
|
// This file is autogenerated from paramaters.xml
|
|
|
|
|
2015-09-12 06:26:20 -03:00
|
|
|
const
|
2015-09-11 16:58:32 -03:00
|
|
|
#ifndef __PX4_DARWIN
|
2015-09-11 06:33:42 -03:00
|
|
|
__attribute__((used, section("__param")))
|
2015-09-11 16:58:32 -03:00
|
|
|
#endif
|
2015-09-12 06:26:20 -03:00
|
|
|
struct px4_parameters_t px4_parameters = {
|
2015-09-11 16:41:00 -03:00
|
|
|
"""
|
|
|
|
i=0
|
|
|
|
for group in root:
|
2015-11-09 04:31:43 -04:00
|
|
|
if group.tag == "group" and "no_code_generation" not in group.attrib:
|
2015-11-05 17:48:35 -04:00
|
|
|
section = """
|
2015-09-11 16:41:00 -03:00
|
|
|
/*****************************************************************
|
|
|
|
* %s
|
|
|
|
****************************************************************/""" % group.attrib["name"]
|
|
|
|
for param in group:
|
2015-11-05 17:48:35 -04:00
|
|
|
scope_ = param.find('scope').text
|
|
|
|
if not scope.Has(scope_):
|
|
|
|
continue
|
2015-09-11 16:41:00 -03:00
|
|
|
if not start_name:
|
|
|
|
start_name = param.attrib["name"]
|
|
|
|
end_name = param.attrib["name"]
|
2015-09-13 19:16:49 -03:00
|
|
|
val_str = "#error UNKNOWN PARAM TYPE, FIX px_generate_params.py"
|
|
|
|
if (param.attrib["type"] == "FLOAT"):
|
|
|
|
val_str = ".val.f = "
|
|
|
|
elif (param.attrib["type"] == "INT32"):
|
|
|
|
val_str = ".val.i = "
|
2015-09-11 16:41:00 -03:00
|
|
|
i+=1
|
2015-11-05 17:48:35 -04:00
|
|
|
src += section
|
|
|
|
section =""
|
2015-09-11 16:41:00 -03:00
|
|
|
src += """
|
|
|
|
{
|
|
|
|
"%s",
|
|
|
|
PARAM_TYPE_%s,
|
2015-09-13 19:16:49 -03:00
|
|
|
%s%s
|
2015-09-11 16:41:00 -03:00
|
|
|
},
|
2015-09-13 19:16:49 -03:00
|
|
|
""" % (param.attrib["name"], param.attrib["type"], val_str, param.attrib["default"])
|
2015-09-11 16:41:00 -03:00
|
|
|
src += """
|
|
|
|
%d
|
2015-09-11 06:33:42 -03:00
|
|
|
};
|
2015-09-11 16:41:00 -03:00
|
|
|
|
2015-09-12 06:26:20 -03:00
|
|
|
//extern const struct px4_parameters_t px4_parameters;
|
|
|
|
|
|
|
|
__END_DECLS
|
2015-09-11 16:58:32 -03:00
|
|
|
|
2015-09-11 16:41:00 -03:00
|
|
|
""" % i
|
|
|
|
|
|
|
|
fp_header.write(header)
|
|
|
|
fp_src.write(src)
|
2015-11-05 17:48:35 -04:00
|
|
|
fp_header.close()
|
|
|
|
fp_src.close()
|