forked from Archive/PX4-Autopilot
Replaced awk with python script for bin_to_obj.
This commit is contained in:
parent
0286136c68
commit
1eddfde439
|
@ -0,0 +1,86 @@
|
|||
#!/usr/bin/env python
|
||||
"""
|
||||
This converts a binary imagge to an object file
|
||||
"""
|
||||
from __future__ import print_function
|
||||
import subprocess
|
||||
import argparse
|
||||
import re
|
||||
from subprocess import PIPE
|
||||
|
||||
#pylint: disable=invalid-name
|
||||
parser = argparse.ArgumentParser(description='Convert bin to obj.')
|
||||
parser.add_argument('--c_flags', required=True)
|
||||
parser.add_argument('--c_compiler', required=True)
|
||||
parser.add_argument('--nm', required=True)
|
||||
parser.add_argument('--ld', required=True)
|
||||
parser.add_argument('--objcopy', required=True)
|
||||
parser.add_argument('--bin', required=True)
|
||||
parser.add_argument('--obj', required=True)
|
||||
parser.add_argument('--var', required=True)
|
||||
args = parser.parse_args()
|
||||
|
||||
in_bin = args.bin
|
||||
c_flags = args.c_flags
|
||||
c_compiler = args.c_compiler
|
||||
nm = args.nm
|
||||
ld = args.ld
|
||||
obj = args.obj
|
||||
var = args.var
|
||||
objcopy = args.objcopy
|
||||
|
||||
sym = "_binary_" + in_bin.replace('/', '_').replace('.', '_').replace('-', '_')
|
||||
#print("sym: ", sym)
|
||||
|
||||
# write empty file
|
||||
with open('{obj:s}.c'.format(**locals()), 'w') as f:
|
||||
f.write("")
|
||||
|
||||
def run_cmd(cmd, d):
|
||||
cmd = cmd.format(**d)
|
||||
#print(cmd)
|
||||
proc = subprocess.Popen(cmd.split(), stdout=PIPE, stderr=PIPE)
|
||||
stdout, stderr = proc.communicate()
|
||||
if stderr != "":
|
||||
raise RuntimeError(stderr)
|
||||
return stdout
|
||||
|
||||
# do compile
|
||||
run_cmd("{c_compiler:s} {c_flags:s} -c {obj:s}.c -o {obj:s}.c.o",
|
||||
locals())
|
||||
|
||||
# link
|
||||
run_cmd("{ld:s} -r -o {obj:s}.bin.o {obj:s}.c.o -b binary {in_bin:s}",
|
||||
locals())
|
||||
|
||||
# get size of image
|
||||
stdout = run_cmd("{nm:s} -p --radix=x {obj:s}.bin.o", locals())
|
||||
re_size = re.compile("(^[0-9A-Fa-f]*) .*{sym:s}_size".format(
|
||||
**locals()))
|
||||
size_match = re.match(re_size, stdout)
|
||||
size = size_match.group(1)
|
||||
#print("romfs size: ", size)
|
||||
|
||||
# write size to file
|
||||
with open('{obj:s}.c'.format(**locals()), 'w') as f:
|
||||
f.write("const unsigned int {var:s}_len = 0x{size:s};".format(
|
||||
**locals()))
|
||||
|
||||
# do compile
|
||||
run_cmd("{c_compiler:s} {c_flags:s} -c {obj:s}.c -o {obj:s}.c.o",
|
||||
locals())
|
||||
|
||||
# link
|
||||
run_cmd("{ld:s} -r -o {obj:s} {obj:s}.c.o {obj:s}.bin.o",
|
||||
locals())
|
||||
|
||||
# obj copy
|
||||
run_cmd("""
|
||||
{objcopy:s} {obj:s}
|
||||
--redefine-sym {sym:s}_start={var:s}
|
||||
--strip-symbol {sym:s}_size
|
||||
--strip-symbol {sym:s}_end
|
||||
--rename-section .data=.rodata
|
||||
""", locals())
|
||||
|
||||
# vim: set et ft=python fenc= ff=unix sts=4 sw=4 ts=4 :
|
|
@ -41,7 +41,6 @@
|
|||
# * px4_nuttx_generate_builtin_commands
|
||||
# * px4_nuttx_add_export
|
||||
# * px4_nuttx_generate_romfs
|
||||
# * px4_bin_to_obj
|
||||
#
|
||||
# Required OS Inteface Functions
|
||||
#
|
||||
|
@ -234,65 +233,6 @@ function(px4_nuttx_add_export)
|
|||
|
||||
endfunction()
|
||||
|
||||
#=============================================================================
|
||||
#
|
||||
# px4_bin_to_obj
|
||||
#
|
||||
# The functions create an object file from a binary image.
|
||||
#
|
||||
# Usage:
|
||||
# px4_bin_to_boj(OBJ <out-obj> VAR <in-variable> BIN <in-bin>)
|
||||
#
|
||||
# Input:
|
||||
# BIN : the bin file
|
||||
# VAR : the variable name
|
||||
#
|
||||
# Output:
|
||||
# OBJ : the object file
|
||||
#
|
||||
# Example:
|
||||
# px4_bin_to_obj(OBJ my_obj VAR romfs BIN my_bin)
|
||||
#
|
||||
function(px4_bin_to_obj)
|
||||
|
||||
px4_parse_function_args(
|
||||
NAME px4_bin_to_obj
|
||||
ONE_VALUE BIN OBJ VAR
|
||||
REQUIRED BIN OBJ VAR
|
||||
ARGN ${ARGN})
|
||||
|
||||
string(REPLACE "/" "_" _tmp ${BIN})
|
||||
string(REPLACE "." "_" _tmp ${_tmp})
|
||||
string(REPLACE "-" "_" sym "_binary_${_tmp}")
|
||||
#message(STATUS "sym: ${sym}")
|
||||
|
||||
separate_arguments(CMAKE_C_FLAGS)
|
||||
|
||||
add_custom_command(OUTPUT ${OBJ}
|
||||
COMMAND ${ECHO} > ${OBJ}.c
|
||||
COMMAND ${CMAKE_C_COMPILER} ${CMAKE_C_FLAGS} -c ${OBJ}.c -o ${OBJ}.c.o
|
||||
COMMAND ${LD} -r -o ${OBJ}.bin.o ${OBJ}.c.o -b binary ${BIN}
|
||||
COMMAND ${NM} -p --radix=x ${OBJ}.bin.o
|
||||
| ${GREP} ${sym}_size
|
||||
| ${GREP} -o ^[0-9a-fA-F]*
|
||||
| ${AWK} "{print \"const unsigned int ${VAR}_len = 0x\"$1\";\"}" > ${OBJ}.c
|
||||
COMMAND ${CMAKE_C_COMPILER} ${CMAKE_C_FLAGS} -c ${OBJ}.c -o ${OBJ}.c.o
|
||||
COMMAND ${LD} -r -o ${OBJ} ${OBJ}.c.o ${OBJ}.bin.o
|
||||
COMMAND ${OBJCOPY} ${OBJ}
|
||||
--redefine-sym ${sym}_start=${VAR}
|
||||
--strip-symbol ${sym}_size
|
||||
--strip-symbol ${sym}_end
|
||||
--rename-section .data=.rodata
|
||||
# useful to comment remove statement when debugging
|
||||
COMMAND ${RM} ${OBJ}.c ${OBJ}.c.o ${OBJ}.bin.o
|
||||
DEPENDS ${BIN}
|
||||
VERBATIM
|
||||
)
|
||||
|
||||
set(${OBJ} ${OBJ} PARENT_SCOPE)
|
||||
|
||||
endfunction()
|
||||
|
||||
#=============================================================================
|
||||
#
|
||||
# px4_nuttx_generate_romfs
|
||||
|
@ -325,6 +265,7 @@ function(px4_nuttx_generate_romfs)
|
|||
|
||||
set(romfs_autostart ${CMAKE_SOURCE_DIR}/Tools/px_process_airframes.py)
|
||||
set(romfs_pruner ${CMAKE_SOURCE_DIR}/Tools/px_romfs_pruner.py)
|
||||
set(bin_to_obj ${CMAKE_SOURCE_DIR}/cmake/nuttx/bin_to_obj.py)
|
||||
|
||||
|
||||
#message(STATUS "temp_dir: ${romfs_temp_dir}")
|
||||
|
@ -349,9 +290,19 @@ function(px4_nuttx_generate_romfs)
|
|||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
||||
)
|
||||
|
||||
px4_bin_to_obj(OBJ ${OUT}
|
||||
BIN ${CMAKE_CURRENT_BINARY_DIR}/romfs.bin
|
||||
VAR romfs_img)
|
||||
add_custom_command(OUTPUT romfs.o
|
||||
COMMAND ${PYTHON_EXECUTABLE} ${bin_to_obj}
|
||||
--ld ${LD} --c_flags ${CMAKE_C_FLAGS}
|
||||
--c_compiler ${CMAKE_C_COMPILER}
|
||||
--nm ${NM} --objcopy ${OBJCOPY}
|
||||
--obj romfs.o
|
||||
--var romfs_img
|
||||
--bin romfs.bin
|
||||
DEPENDS romfs.bin
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
||||
)
|
||||
|
||||
set(${OUT} romfs.o PARENT_SCOPE)
|
||||
|
||||
endfunction()
|
||||
|
||||
|
|
|
@ -1,21 +0,0 @@
|
|||
#!/usr/bin/env python
|
||||
"""
|
||||
This converts a binary imagge to an object file
|
||||
"""
|
||||
from __future__ import print_function
|
||||
import subprocess
|
||||
import argparse
|
||||
import os
|
||||
|
||||
#pylint: disable=invalid-name
|
||||
parser = argparse.ArgumentParser(description='Convert bin to obj.')
|
||||
parser.add_argument('--c-flags', required=True)
|
||||
parser.add_argument('--c-compiler', required=True)
|
||||
parser.add_argument('--nm', required=True)
|
||||
args = parser.parse_args()
|
||||
|
||||
#TODO write function
|
||||
|
||||
exit(0)
|
||||
|
||||
# vim: set et ft=python fenc= ff=unix sts=4 sw=4 ts=4 :
|
Loading…
Reference in New Issue