| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# Copyright (c) 2022 Firebuild Inc. |
| 4 |
# All rights reserved. |
| 5 |
# Free for personal use and commercial trial. |
| 6 |
# Non-trial commercial use requires licenses available from https://firebuild.com. |
| 7 |
# Modification and redistribution are permitted, but commercial use of |
| 8 |
# derivative works is subject to the same requirements of this license |
| 9 |
# |
| 10 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 11 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 12 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 13 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 14 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 15 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 16 |
# SOFTWARE. |
| 17 |
|
| 18 |
import os |
| 19 |
import sys |
| 20 |
from jinja2 import Environment, FileSystemLoader |
| 21 |
|
| 22 |
if len(sys.argv) != 3: |
| 23 |
print("Usage: generate_fbb namespace outputdir", file=sys.stderr) |
| 24 |
print(" Processes the input file \"<namespace>\".def from the current directory", file=sys.stderr) |
| 25 |
exit(1) |
| 26 |
fbbdir = os.path.dirname(sys.argv[0]) # wherever generate_fbb and tpl.[ch] reside |
| 27 |
namespace = sys.argv[1] |
| 28 |
outdir = sys.argv[2] |
| 29 |
|
| 30 |
env = Environment(loader=FileSystemLoader(fbbdir), |
| 31 |
line_statement_prefix='###', |
| 32 |
trim_blocks=True, |
| 33 |
lstrip_blocks=True, |
| 34 |
keep_trailing_newline=True, |
| 35 |
extensions=['jinja2.ext.do']) |
| 36 |
|
| 37 |
# Use a practically unique temporary filename, see #314 |
| 38 |
tmpsuffix = ".tmp." + str(os.getpid()) |
| 39 |
|
| 40 |
# Symbolic constants to reduce the chance of typos |
| 41 |
REQUIRED = "required" |
| 42 |
OPTIONAL = "optional" |
| 43 |
ARRAY = "array" |
| 44 |
STRING = "string" |
| 45 |
FBB = "fbb" |
| 46 |
|
| 47 |
def gen_fbb(params): |
| 48 |
msgs = params.get("tags") |
| 49 |
|
| 50 |
# Python can easily unpack tuples whose size isn't known in advance. Jinja cannot. I don't want to |
| 51 |
# bloat the FBB definitions by requiring a None for all the rarely used debugging-related 4th |
| 52 |
# fields. Alter the big msgs object here so that all its tuples contain all 4 fields. |
| 53 |
for (msg, fields) in msgs: |
| 54 |
for (index, (req, type, var, *args)) in enumerate(fields): |
| 55 |
debugger_method = args[0] if len(args) > 0 else None |
| 56 |
fields[index] = (req, type, var, debugger_method) |
| 57 |
|
| 58 |
for (msg, fields) in msgs: |
| 59 |
for (req, type, var, dbgfn) in fields: |
| 60 |
if req not in [REQUIRED, OPTIONAL, ARRAY]: |
| 61 |
print("Unknown value instead of REQUIRED, OPTIONAL or ARRAY", file=sys.stderr) |
| 62 |
exit(1) |
| 63 |
|
| 64 |
for extension in [".c", ".h", "_decode.c"]: |
| 65 |
template = env.get_template("tpl" + extension) |
| 66 |
rendered = template.render(ns=namespace, |
| 67 |
msgs=msgs, |
| 68 |
types_with_custom_debugger=params.get("types_with_custom_debugger", []), |
| 69 |
varnames_with_custom_debugger=params.get("varnames_with_custom_debugger", []), |
| 70 |
extra_c=params.get("extra_c", ""), |
| 71 |
extra_h=params.get("extra_h", ""), |
| 72 |
REQUIRED=REQUIRED, |
| 73 |
OPTIONAL=OPTIONAL, |
| 74 |
ARRAY=ARRAY, |
| 75 |
STRING=STRING, |
| 76 |
FBB=FBB) |
| 77 |
if rendered: |
| 78 |
filename = outdir + "/" + namespace + extension |
| 79 |
with open(filename + tmpsuffix, "w") as f: |
| 80 |
f.write(rendered) |
| 81 |
os.rename(filename + tmpsuffix, filename) |
| 82 |
|
| 83 |
# Generate the same file with both .c and .cc extension, so that cmake can |
| 84 |
# easily compile the C and C++ variants separately. |
| 85 |
if extension == ".c": |
| 86 |
extension = ".cc" |
| 87 |
filename = outdir + "/" + namespace + extension |
| 88 |
with open(filename + tmpsuffix, "w") as f: |
| 89 |
f.write(rendered) |
| 90 |
os.rename(filename + tmpsuffix, filename) |
| 91 |
|
| 92 |
with open("" + namespace + ".def", "r") as f: |
| 93 |
params = eval(f.read()) |
| 94 |
gen_fbb(params) |