blob: f1c653e60323c3f3b923b9843738da65e22b3a7a [file] [log] [blame]
#!/usr/bin/env python3
# Copyright lowRISC contributors.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0
"""Script used to generate additional output files for Meson embedded targets.
This script takes the input ELF file generated by the build system, and
generates BIN and VMEM files. The BIN file is generated using objcopy, whereas
the VMEM file is generated using srec_cat.
"""
import argparse
import os
from pathlib import Path
import subprocess
import sys
def run_objcopy(objcopy, input, basename, outdir):
filename = basename + '.bin'
output = outdir / filename
cmd = [
objcopy, '-O', 'binary', input, output
]
subprocess.run(cmd, check=True)
return output
def run_srec_cat(srec_cat, input, basename, outdir):
# TODO: Replace command for objcopy once this bug is fixed and released.
# https://sourceware.org/bugzilla/show_bug.cgi?id=19921
filename = basename + '.vmem'
output = outdir / filename
cmd = [
srec_cat, input, '-binary', '-offset', '0x0', '-byte-swap', '4', '-o',
output, '-vmem'
]
subprocess.run(cmd, check=True)
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'--basename',
'-b',
required=True,
help='File basename used as preffix in output filenames.')
parser.add_argument(
'--input',
'-i',
required=True,
help="Input file")
parser.add_argument(
'--objcopy',
'-c',
required=True,
help='Absolute path to objcopy tool.')
parser.add_argument(
'--outdir',
'-d',
required=True,
help="Output directory.")
parser.add_argument(
'--srec_cat',
'-s',
required=True,
help='Absolute path to srec_cat tool.')
args = parser.parse_args()
if not os.path.exists(args.outdir):
os.makedirs(args.outdir)
outdir = Path(args.outdir)
bin_file = run_objcopy(args.objcopy, args.input, args.basename, outdir)
run_srec_cat(args.srec_cat, bin_file, args.basename, outdir)
if __name__ == "__main__":
main()