Skip to content

Commit

Permalink
Generate hwdefs.qrc via a script
Browse files Browse the repository at this point in the history
  • Loading branch information
elecpower committed Jan 19, 2024
1 parent fb3a87a commit 05c79a2
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 12 deletions.
24 changes: 12 additions & 12 deletions companion/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,21 +138,21 @@ else()
message(STATUS "Qt lupdate not found, 'translations' target will not be availabe.")
endif()

############# Radio hardware definitions ###############
############# Import radio hardware definitions ###############

# Find all the json files
set(HWDEFS_DIR "${CMAKE_CURRENT_BINARY_DIR}/../../radio/src")
file(GLOB hwdefs_json "${HWDEFS_DIR}/*.json")
set(HWDEFS_TMPL "${COMPANION_SRC_DIRECTORY}/hwdefs.qrc.in")
set(HWDEFS_PHDR "HWDEF_JSON_LIST")
set(HWDEFS_QRC "${CMAKE_CURRENT_BINARY_DIR}/hwdefs.qrc")

# Dynamically update companion.qrc file (XML) adding collected hwdef json files.
foreach(hwdef_json ${hwdefs_json})
get_filename_component(hwdef_json_name ${hwdef_json} NAME)
message(STATUS "Hardware definition found: ${hwdef_json_name}")
# Add file with full path and file name (w/out path) as alias to be used in actual code
set(HWDEF_JSON_LIST "${HWDEF_JSON_LIST} <file alias=\"${hwdef_json_name}\">${HWDEFS_DIR}/${hwdef_json_name}</file>\n")
endforeach()
configure_file(${COMPANION_SRC_DIRECTORY}/hwdefs.qrc.in ${HWDEFS_QRC} @ONLY)
set(HWDEFS_CMD "${COMPANION_SRC_DIRECTORY}/../util/generate_hwdefs_qrc.py")

add_custom_command(OUTPUT ${HWDEFS_QRC}
COMMAND ${HWDEFS_CMD} -d ${HWDEFS_DIR}
-t ${HWDEFS_TMPL}
-p ${HWDEFS_PHDR}
-o ${HWDEFS_QRC}
DEPENDS ${HWDEFS_TMPL}
)

############# Common lib ###############

Expand Down
51 changes: 51 additions & 0 deletions companion/util/generate_hwdefs_qrc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python3

import argparse
import glob
from os import path

def main(jsondir, template, placeholder, output):
if not path.isdir(jsondir):
raise Exception(jsondir + " is not a directory")

try:
tmplt = open(template, "r")
except:
raise Exception("Unable to open template file " + template)

try:
qrc = open(output, "w")
except:
raise Exception("Unable to open " + output + " for writing")

line = tmplt.readline()

while placeholder not in line and line != '':
qrc.write(line)
line = tmplt.readline()

for f in sorted(glob.iglob(path.join(jsondir, '*.json'), recursive = False)):
fname = path.basename(f)
qrc.write(' <file alias="' + fname + '">' + f + '</file>' + '\n')

line = tmplt.readline()

while line != '':
qrc.write(line)
line = tmplt.readline()

tmplt.close()
qrc.close()


if __name__ == "__main__":

parser = argparse.ArgumentParser(description='Generate hardware definitions resource file')
parser.add_argument('-d', metavar='json files directory', required=True)
parser.add_argument('-t', metavar='template file', required=True)
parser.add_argument('-p', metavar='placeholder', required=True)
parser.add_argument('-o', metavar='qrc file', required=True)

args = parser.parse_args()

main(args.d, args.t, args.p, args.o)

0 comments on commit 05c79a2

Please sign in to comment.