From aba3c41422a04466a0edcc287589cfc8ab29ffd3 Mon Sep 17 00:00:00 2001 From: bugobliterator Date: Wed, 5 Apr 2023 17:05:05 +1000 Subject: [PATCH] generator: add support for relative path to lua mavlink module --- generator/mavgen_lua.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/generator/mavgen_lua.py b/generator/mavgen_lua.py index 717f5ca8d..357a35f2a 100644 --- a/generator/mavgen_lua.py +++ b/generator/mavgen_lua.py @@ -35,7 +35,19 @@ def generate(basename, xml): print("Generating %s/mavlink_msgs.lua" % basename) + # create the output directory if needed + if not os.path.exists(basename): + os.makedirs(basename) outf = open("%s/mavlink_msgs.lua" % basename, "w") + if 'modules' not in basename: + print("ERROR: mavlink_msgs.lua must be generated in the modules directory or subdirectory") + return + # find the path relative to module directory + module_root_rel = basename.split('modules')[1] + module_root_rel = module_root_rel.strip('/') + if module_root_rel != '': + module_root_rel += '/' + # dump the actual symbol table outf.write("local mavlink_msgs = {}\n") for m in msgs: @@ -55,10 +67,10 @@ def generate(basename, xml): mavlink_msg_file.close() outf.write( """ -local mavlink_msgs = {} +local mavlink_msgs = {{}} function mavlink_msgs.get_msgid(msgname) - local message_map = require("mavlink_msg_" .. msgname) + local message_map = require("{module_root_rel}mavlink_msg_" .. msgname) if not message_map then error("Unknown MAVLink message " .. msgname) end @@ -67,7 +79,7 @@ def generate(basename, xml): function mavlink_msgs.decode_header(message) -- build up a map of the result - local result = {} + local result = {{}} local read_marker = 3 @@ -97,7 +109,7 @@ def generate(basename, xml): function mavlink_msgs.decode(message, msg_map) local result, offset = mavlink_msgs.decode_header(message) - local message_map = require("mavlink_msg_" .. msg_map[result.msgid]) + local message_map = require("{module_root_rel}mavlink_msg_" .. msg_map[result.msgid]) if not message_map then -- we don't know how to decode this message, bail on it return nil @@ -106,7 +118,7 @@ def generate(basename, xml): -- map all the fields out for i,v in ipairs(message_map.fields) do if v[3] then - result[v[1]] = {} + result[v[1]] = {{}} for j=1,v[3] do result[v[1]][j], offset = string.unpack(v[2], message, offset) end @@ -121,14 +133,14 @@ def generate(basename, xml): end function mavlink_msgs.encode(msgname, message) - local message_map = require("mavlink_msg_" .. msgname) + local message_map = require("{module_root_rel}mavlink_msg_" .. msgname) if not message_map then -- we don't know how to encode this message, bail on it error("Unknown MAVLink message " .. msgname) end local packString = "<" - local packedTable = {} + local packedTable = {{}} local packedIndex = 1 for i,v in ipairs(message_map.fields) do if v[3] then @@ -150,7 +162,7 @@ def generate(basename, xml): end return mavlink_msgs -""") +""".format(module_root_rel=module_root_rel)) outf.close() print("Generated %s OK" % basename)