Made automatic payload generation much better
This commit is contained in:
@@ -9,21 +9,32 @@ set(PL_NAMES_SHORT
|
|||||||
task_sleep_test)
|
task_sleep_test)
|
||||||
|
|
||||||
foreach(NAME ${PL_NAMES_SHORT})
|
foreach(NAME ${PL_NAMES_SHORT})
|
||||||
list(APPEND PL_TARGETS "payload_${NAME}")
|
list(APPEND PL_TARGETS "payload_${NAME}")
|
||||||
list(APPEND PL_SRC_SHORT "${CMAKE_CURRENT_LIST_DIR}/pl/src/${NAME}.c")
|
list(APPEND PL_SRC_SHORT "${CMAKE_CURRENT_LIST_DIR}/pl/src/${NAME}.c")
|
||||||
endforeach(NAME)
|
endforeach(NAME)
|
||||||
|
|
||||||
foreach(NAME ${PL_TARGETS})
|
foreach(NAME ${PL_TARGETS})
|
||||||
list(APPEND PL_SRC_LONG "${CMAKE_CURRENT_BINARY_DIR}/lib/${NAME}.c")
|
list(APPEND PL_SRC_LONG "${CMAKE_CURRENT_BINARY_DIR}/lib/${NAME}.c")
|
||||||
|
list(APPEND PL_BIN "${CMAKE_CURRENT_BINARY_DIR}/pl/bin/${NAME}.bin")
|
||||||
|
list(APPEND PL_TARGETS_BIN ${NAME}_bin)
|
||||||
endforeach(NAME)
|
endforeach(NAME)
|
||||||
|
|
||||||
add_subdirectory(pl)
|
add_subdirectory(pl)
|
||||||
|
|
||||||
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
|
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
|
||||||
add_custom_command(OUTPUT ${PL_SRC_LONG}
|
add_custom_target(payload_sources
|
||||||
DEPENDS ${PL_TARGETS}
|
BYPRODUCTS ${PL_SRC_LONG}
|
||||||
|
DEPENDS ${PL_TARGETS_BIN}
|
||||||
|
COMMENT "Refreshing payload library"
|
||||||
COMMAND python3 ${CMAKE_CURRENT_LIST_DIR}/scripts/librarize.py
|
COMMAND python3 ${CMAKE_CURRENT_LIST_DIR}/scripts/librarize.py
|
||||||
${CMAKE_CURRENT_BINARY_DIR}/pl/bin
|
${CMAKE_CURRENT_BINARY_DIR}/pl/bin
|
||||||
${CMAKE_CURRENT_BINARY_DIR}/lib)
|
${CMAKE_CURRENT_BINARY_DIR}/lib)
|
||||||
|
|
||||||
add_library(payload ${PL_SRC_LONG})
|
add_library(payload ${PL_SRC_LONG})
|
||||||
|
add_dependencies(payload payload_sources)
|
||||||
|
|
||||||
|
add_custom_command(TARGET payload POST_BUILD
|
||||||
|
BYPRODUCTS ${CMAKE_SOURCE_DIR}/c8_remote/include/libpayload.h
|
||||||
|
COMMAND python3 ${CMAKE_CURRENT_LIST_DIR}/scripts/headerize.py
|
||||||
|
${CMAKE_CURRENT_BINARY_DIR}/lib
|
||||||
|
${CMAKE_SOURCE_DIR}/c8_remote/include)
|
||||||
@@ -19,4 +19,6 @@ foreach(PL ${PL_NAMES_SHORT})
|
|||||||
ARGS -O binary -j .text -j .payload_text -j .payload_data
|
ARGS -O binary -j .text -j .payload_text -j .payload_data
|
||||||
${CMAKE_CURRENT_BINARY_DIR}/payload_${PL}
|
${CMAKE_CURRENT_BINARY_DIR}/payload_${PL}
|
||||||
${CMAKE_CURRENT_BINARY_DIR}/bin/payload_${PL}.bin)
|
${CMAKE_CURRENT_BINARY_DIR}/bin/payload_${PL}.bin)
|
||||||
|
|
||||||
|
add_custom_target(payload_${PL}_bin DEPENDS payload_${PL})
|
||||||
endforeach(PL)
|
endforeach(PL)
|
||||||
36
c8_libpayload/scripts/headerize.py
Normal file
36
c8_libpayload/scripts/headerize.py
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
import sys
|
||||||
|
import os
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
if len(sys.argv) < 3:
|
||||||
|
print('Usage: headerize.py [lib names ...] [header dir]')
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
lib_names = []
|
||||||
|
hdr_dir = os.path.abspath(sys.argv[-1])
|
||||||
|
|
||||||
|
if os.path.isdir(sys.argv[1]):
|
||||||
|
lib_folder = os.path.abspath(sys.argv[1])
|
||||||
|
for lib_fname in os.listdir(lib_folder):
|
||||||
|
lib_names.append(lib_folder + '/' + lib_fname)
|
||||||
|
else:
|
||||||
|
for n in sys.argv[1:-1]:
|
||||||
|
lib_names.append(os.path.abspath(n))
|
||||||
|
|
||||||
|
header_lines = ['#ifndef CHECKM8_TOOL_LIBPAYLOAD_H\n',
|
||||||
|
'#define CHECKM8_TOOL_LIBPAYLOAD_H\n',
|
||||||
|
'\n']
|
||||||
|
|
||||||
|
for n in lib_names:
|
||||||
|
with open(n, 'r') as f:
|
||||||
|
line = f.readline() # looks like "const unsigned char PAYLOAD_NAME[PAYLOAD_SIZE] = "
|
||||||
|
name = line.split(' ')[3].split('[')[0]
|
||||||
|
size = line.split(' ')[3].split('[')[1][:-1]
|
||||||
|
|
||||||
|
header_lines.append('extern const unsigned char %s[%s];\n' % (name, size))
|
||||||
|
|
||||||
|
header_lines.append('\n')
|
||||||
|
header_lines.append('#endif //CHECKM8_TOOL_LIBPAYLOAD_H\n')
|
||||||
|
|
||||||
|
with open(hdr_dir + '/libpayload.h', 'w+') as f:
|
||||||
|
f.writelines(header_lines)
|
||||||
@@ -21,19 +21,11 @@ if __name__ == '__main__':
|
|||||||
bin_names.append(os.path.abspath(n))
|
bin_names.append(os.path.abspath(n))
|
||||||
|
|
||||||
source_lines = defaultdict(list)
|
source_lines = defaultdict(list)
|
||||||
header_lines = ['#ifndef CHECKM8_TOOL_LIBPAYLOAD_H\n',
|
|
||||||
'#define CHECKM8_TOOL_LIBPAYLOAD_H\n',
|
|
||||||
'\n']
|
|
||||||
|
|
||||||
for n in bin_names:
|
for n in bin_names:
|
||||||
payload_name = os.path.basename(n).split('.')[0]
|
payload_name = os.path.basename(n).split('.')[0]
|
||||||
with open(n, 'rb') as fbin:
|
with open(n, 'rb') as fbin:
|
||||||
fbytes = fbin.read()
|
fbytes = fbin.read()
|
||||||
|
|
||||||
header_lines.append('extern const unsigned char %s[%i];\n' % (payload_name, len(fbytes)))
|
|
||||||
|
|
||||||
source_lines[payload_name].append('#include "libpayload.h"\n')
|
|
||||||
source_lines[payload_name].append('\n')
|
|
||||||
source_lines[payload_name].append('const unsigned char %s[%i] =\n' % (payload_name, len(fbytes)))
|
source_lines[payload_name].append('const unsigned char %s[%i] =\n' % (payload_name, len(fbytes)))
|
||||||
source_lines[payload_name].append('\t{')
|
source_lines[payload_name].append('\t{')
|
||||||
|
|
||||||
@@ -48,10 +40,6 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
source_lines[payload_name].append('\t};\n')
|
source_lines[payload_name].append('\t};\n')
|
||||||
|
|
||||||
header_lines.append('\n')
|
|
||||||
header_lines.append('#endif //CHECKM8_TOOL_LIBPAYLOAD_H\n')
|
|
||||||
|
|
||||||
files_updated = False
|
|
||||||
for sname, lines in source_lines.items():
|
for sname, lines in source_lines.items():
|
||||||
sfname = lib_dir + '/' + sname + '.c'
|
sfname = lib_dir + '/' + sname + '.c'
|
||||||
|
|
||||||
@@ -63,9 +51,4 @@ if __name__ == '__main__':
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
with open(sfname, 'w+') as f:
|
with open(sfname, 'w+') as f:
|
||||||
files_updated = True
|
|
||||||
f.writelines(lines)
|
f.writelines(lines)
|
||||||
|
|
||||||
if files_updated:
|
|
||||||
with open(lib_dir + '/libpayload.h', 'w+') as f:
|
|
||||||
f.writelines(header_lines)
|
|
||||||
@@ -7,8 +7,7 @@
|
|||||||
#include "command.h"
|
#include "command.h"
|
||||||
#include "usb_helpers.h"
|
#include "usb_helpers.h"
|
||||||
|
|
||||||
// TODO: this is so ugly ...
|
#include "libpayload.h"
|
||||||
#include "../../cmake-build-debug/c8_libpayload/lib/libpayload.h"
|
|
||||||
|
|
||||||
struct payload
|
struct payload
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user