Fix param scoping to use cmake for parsing.

This commit is contained in:
James Goppert
2017-02-17 11:26:46 +01:00
committed by Lorenz Meier
parent 44c8857354
commit 85f62f5da0
9 changed files with 171 additions and 166 deletions
+50 -94
View File
@@ -1,107 +1,63 @@
#!/usr/bin/env python
"""
Param source code generation script.
"""
from __future__ import print_function
import xml.etree.ElementTree as ET
import os
import re
import codecs
import argparse
from jinja2 import Environment, FileSystemLoader
import os
from px4params import scope, cmakeparser
def generate(xml_file, dest='.', modules=None):
"""
Generate px4 param source from xml.
if len(os.sys.argv) < 2:
print("Error in %s" % os.sys.argv[0])
print("Usage: %s <parameters.xml> [cmake-file-scoping] " % os.sys.argv[0])
raise SystemExit
@param xml_file: input parameter xml file
@param dest: Destination directory for generated files
@param modules: The list of px4 modules to search for params.
None means to scan everything.
"""
# pylint: disable=broad-except
tree = ET.parse(xml_file)
root = tree.getroot()
cmake_scope = 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.CMakeParser()
parser.Parse(cmake_scope, contents)
except:
contents = ''
print('Failed reading file: %s, skipping scoping.' % os.sys.argv[2])
pass
params = []
for group in root:
if group.tag == "group" and "no_code_generation" not in group.attrib:
for param in group:
scope_ = param.find('scope').text
if (modules is not None) and (not scope_ in modules):
continue
params.append(param)
fp_header = open("px4_parameters.h", "w")
fp_src = open("px4_parameters.c", "w")
params = sorted(params, key=lambda name: name.attrib["name"])
tree = ET.parse(os.sys.argv[1])
root = tree.getroot()
script_path = os.path.dirname(os.path.realpath(__file__))
# Generate the header file content
header = """
#include <stdint.h>
#include <systemlib/param/param.h>
# for jinja docs see: http://jinja.pocoo.org/docs/2.9/api/
env = Environment(
loader=FileSystemLoader(os.path.join(script_path, 'templates')))
// DO NOT EDIT
// This file is autogenerated from parameters.xml
if not os.path.isdir(dest):
os.path.mkdir(dest)
__BEGIN_DECLS
template_files = [
'px4_parameters.h.jinja',
'px4_parameters.c.jinja',
]
for template_file in template_files:
template = env.get_template(template_file)
with open(os.path.join(
dest, template_file.replace('.jinja','')), 'w') as fid:
fid.write(template.render(params=params))
struct px4_parameters_t {
"""
params = []
for group in root:
if group.tag == "group" and "no_code_generation" not in group.attrib:
for param in group:
scope_ = param.find('scope').text
if not cmake_scope.Has(scope_):
continue
params.append(param)
if __name__ == "__main__":
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument("--xml", help="parameter xml file")
arg_parser.add_argument("--modules", help="px4 module list", default=None)
arg_parser.add_argument("--dest", help="destination path", default=os.path.curdir)
args = arg_parser.parse_args()
generate(xml_file=args.xml, modules=args.modules, dest=args.dest)
params = sorted(params, key=lambda name: name.attrib["name"])
for param in params:
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
const
#ifndef __PX4_DARWIN
__attribute__((used, section("__param")))
#endif
struct px4_parameters_t px4_parameters = {
"""
i=0
for param in params:
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 = "
i+=1
src += """
{
"%s",
PARAM_TYPE_%s,
%s%s
},
""" % (param.attrib["name"], param.attrib["type"], val_str, param.attrib["default"])
src += """
%d
};
//extern const struct px4_parameters_t px4_parameters;
__END_DECLS
""" % i
fp_header.write(header)
fp_src.write(src)
fp_header.close()
fp_src.close()
# vim: set et fenc=utf-8 ff=unix sts=4 sw=4 ts=4 :