|
| 1 | +#! /usr/bin/env python2 |
| 2 | +""" |
| 3 | +mbed SDK |
| 4 | +Copyright (c) 2019 ARM Limited |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +
|
| 18 | +LIBRARIES BUILD |
| 19 | +""" |
| 20 | +from __future__ import print_function, division, absolute_import |
| 21 | + |
| 22 | +import sys |
| 23 | +from os.path import join, abspath, dirname, normpath |
| 24 | +import json |
| 25 | +from shutil import copyfile |
| 26 | +from argparse import ArgumentParser |
| 27 | +from copy import copy |
| 28 | + |
| 29 | +# Be sure that the tools directory is in the search path |
| 30 | +ROOT = abspath(join(dirname(__file__), "../../../")) |
| 31 | +sys.path.insert(0, ROOT) |
| 32 | + |
| 33 | +from tools.build_api import merge_region_list, UPDATE_WHITELIST |
| 34 | +from tools.notifier.term import TerminalNotifier |
| 35 | +from tools.config import Region |
| 36 | +from tools.utils import split_path, run_cmd_ext, generate_update_filename |
| 37 | + |
| 38 | + |
| 39 | +if __name__ == "__main__": |
| 40 | + parser = ArgumentParser() |
| 41 | + |
| 42 | + parser.add_argument( |
| 43 | + "toolchain_path", |
| 44 | + help="Path to the Keil folder" |
| 45 | + ) |
| 46 | + |
| 47 | + parser.add_argument( |
| 48 | + "linker_output", |
| 49 | + help="Path to the built axf file" |
| 50 | + ) |
| 51 | + |
| 52 | + options = parser.parse_args() |
| 53 | + axf_file = normpath(options.linker_output) |
| 54 | + output_directory, output_name, output_ext = split_path(axf_file) |
| 55 | + hex_file = join(output_directory, output_name + ".hex") |
| 56 | + combined_hex_file = join(output_directory, output_name + "_combined.hex") |
| 57 | + |
| 58 | + command = [ |
| 59 | + join(normpath(options.toolchain_path), "ARM/ARMCC/bin/fromelf.exe"), |
| 60 | + "--i32", "--output", hex_file, axf_file |
| 61 | + ] |
| 62 | + stdout, stderr, retcode = run_cmd_ext(command) |
| 63 | + |
| 64 | + if retcode: |
| 65 | + err_msg = ( |
| 66 | + "Failed to convert axf to hex.\r\n" |
| 67 | + "Command: {}\r\n" |
| 68 | + "retcode: {}\r\n" |
| 69 | + "stdout: {}\r\n" |
| 70 | + "stderr: {}" |
| 71 | + ).format(command, retcode, stdout, stderr) |
| 72 | + raise Exception(err_msg) |
| 73 | + |
| 74 | + with open(join("export_info.json"), "r") as export_info_file: |
| 75 | + export_info_data = json.load(export_info_file) |
| 76 | + |
| 77 | + region_list = [Region(*r) for r in export_info_data.get("region_list", [])] |
| 78 | + |
| 79 | + for index, region in enumerate(copy(region_list)): |
| 80 | + if region.name == "application": |
| 81 | + region_data = region._asdict() |
| 82 | + region_data["filename"] = hex_file |
| 83 | + region_list[index] = Region(**region_data) |
| 84 | + break |
| 85 | + else: |
| 86 | + raise Exception("No application region found") |
| 87 | + |
| 88 | + notify = TerminalNotifier() |
| 89 | + restrict_size = export_info_data.get("target", {}).get("restrict_size") |
| 90 | + merge_region_list(region_list, combined_hex_file, notify, restrict_size) |
| 91 | + |
| 92 | + update_regions = [ |
| 93 | + r for r in region_list if r.name in UPDATE_WHITELIST |
| 94 | + ] |
| 95 | + |
| 96 | + if update_regions: |
| 97 | + update_res = normpath( |
| 98 | + join( |
| 99 | + output_directory, |
| 100 | + generate_update_filename(output_name, None) |
| 101 | + ) |
| 102 | + ) |
| 103 | + merge_region_list(update_regions, update_res, notify, restrict_size) |
| 104 | + |
| 105 | + sys.exit(0) |
0 commit comments