aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2017-01-22 23:35:25 +0100
committerHarald Welte <laforge@gnumonks.org>2017-01-24 17:09:26 +0100
commitd248204bb7f609d1f89ad36e70e471af78f5b2bb (patch)
treec649ef0fbdfac0eb1d9408c099a558ef43d2ae8e
parent78ced2084d7732bc2a37a9eb9bb37c9392372066 (diff)
python scripts: Generate file header with #include statements
-rwxr-xr-xtools/enum-create-valstr.py12
-rw-r--r--tools/value_string.py33
2 files changed, 34 insertions, 11 deletions
diff --git a/tools/enum-create-valstr.py b/tools/enum-create-valstr.py
index 1e78a40..ac24095 100755
--- a/tools/enum-create-valstr.py
+++ b/tools/enum-create-valstr.py
@@ -20,7 +20,7 @@
from optparse import OptionParser
import pprint
from pycparser import c_parser, c_ast, parse_file
-from value_string import export_value_str
+from value_string import *
class EnumValStrVisitor(c_ast.NodeVisitor):
"Generate a 'struct value_string' from an enum"
@@ -30,7 +30,7 @@ class EnumValStrVisitor(c_ast.NodeVisitor):
vdict = {}
for val in elist.enumerators:
vdict[val.name] = val.name
- export_value_str(name, vdict, sort_key=None)
+ wr.export_value_str(name, vdict, sort_key=None)
class EnumValuePrinter(c_ast.NodeVisitor):
def visit_Enum(self, node):
@@ -49,11 +49,19 @@ class EnumValuePrinter(c_ast.NodeVisitor):
parser = OptionParser()
+parser.add_option("-f", "--flavor", dest="flavor", default='osmocom',
+ help="Flavor of generated C (osmocom, wireshark)")
+parser.add_option("-w", "--weak-symbol", dest="weak", default=True,
+ help="Generate weak symbols")
(options, args) = parser.parse_args()
filename = args[0]
ast = parse_file(filename, use_cpp=True)
+wr = ValueStringWriter(flavor=options.flavor, weak=options.weak,
+ includes=[filename])
+wr.export_header()
+
v = EnumValStrVisitor()
#v = EnumValuePrinter()
v.visit(ast)
diff --git a/tools/value_string.py b/tools/value_string.py
index 2bdc499..31db752 100644
--- a/tools/value_string.py
+++ b/tools/value_string.py
@@ -17,13 +17,28 @@
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+class ValueStringWriter(object):
+ def __init__(self, flavor='osmocom', weak=False, includes=[]):
+ self.flavor = flavor
+ self.weak = weak
+ self.includes = includes
-def export_value_str(name, vals, flavor='osmocom', sort_key=int):
- if flavor == 'osmocom':
- print("const struct value_string %s[] = {" % name);
- elif flavor == 'wireshark':
- print("const value_string %s[] = {" % name);
- for v in sorted(vals.iterkeys(), key=sort_key):
- print("\t{ %s, \"%s\" }," % (v, vals[v]));
- print("\t{ 0, NULL }")
- print("};");
+ def export_value_str(self, name, vals, sort_key=int):
+ if self.weak:
+ print("__attribute__((weak))")
+ if self.flavor == 'osmocom':
+ print("const struct value_string %s[] = {" % name);
+ elif self.flavor == 'wireshark':
+ print("const value_string %s[] = {" % name);
+ for v in sorted(vals.iterkeys(), key=sort_key):
+ print("\t{ %s, \"%s\" }," % (v, vals[v]));
+ print("\t{ 0, NULL }")
+ print("};");
+
+ def export_header(self):
+ print("/* GENERATED FILE, DO NOT EDIT */")
+ if self.flavor == 'osmocom':
+ print("#include <osmocom/core/utils.h>")
+ for i in self.includes:
+ print('#include "%s"' % i)
+ print("")