Arrow / Mbed OS DAPLink Reset
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbedcli_compile.py Source File

mbedcli_compile.py

00001 #
00002 # DAPLink Interface Firmware
00003 # Copyright (c) 2009-2018, ARM Limited, All Rights Reserved
00004 # SPDX-License-Identifier: Apache-2.0
00005 #
00006 # Licensed under the Apache License, Version 2.0 (the "License"); you may
00007 # not use this file except in compliance with the License.
00008 # You may obtain a copy of the License at
00009 #
00010 # http://www.apache.org/licenses/LICENSE-2.0
00011 #
00012 # Unless required by applicable law or agreed to in writing, software
00013 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00014 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 # See the License for the specific language governing permissions and
00016 # limitations under the License.
00017 #
00018 
00019 import argparse
00020 import os
00021 import sys
00022 import shutil
00023 import yaml
00024 import mbedcli_tools
00025 from pre_build_script import generate_version_file
00026 from package_release_files import package_release_files
00027 from post_build_script import post_build_script
00028 
00029 self_path = os.path.abspath(__file__)
00030 tools_dir = os.path.dirname(self_path)
00031 daplink_dir = os.path.dirname(tools_dir)
00032 test_dir = os.path.join(daplink_dir, "test")
00033 sys.path.append(test_dir)
00034 
00035 import info
00036 
00037 PROJECTS_YAML = "projects.yaml"
00038 VERSION_YAML = "version.yaml"
00039 
00040 def main():
00041     """python -u tools/mbedcli_compile.py kl26z_microbit_if kl26z_bl --clean"""
00042     #parse project yaml file for project list
00043     project_list = []
00044     with open(PROJECTS_YAML, 'r') as top_yaml:
00045         try:
00046             topdict = yaml.load(top_yaml)
00047             for dict_key in topdict:
00048                 if dict_key == 'projects':
00049                     for project in topdict[dict_key]:
00050                         project_list.append(project)
00051                     break
00052         except yaml.YAMLError as ex:
00053             print("Found yaml parse error", ex)
00054     #parse the arguments
00055     projects = "List of supported projects\n\n" + ", ".join(project_list) #
00056     parser = argparse.ArgumentParser(description='mbedcli compile support for DAPLink', epilog=projects, formatter_class=argparse.RawDescriptionHelpFormatter)
00057     parser.add_argument('projects', help='Selectively compile only the firmware specified otherwise all projects',
00058                         nargs='*', type=str, default=[])
00059     parser.add_argument("--board-id", type=str, help="board id to for the target in hex")
00060     parser.add_argument("--family-id", type=str, help="family id to for the target in hex")
00061     parser.add_argument("--bin-offset", type=str, help="binary offset in hex")
00062     parser.add_argument('--release', dest='release', action='store_true', help='Create a release with the yaml version file')
00063     parser.add_argument('--build-folder', type=str, default='BUILD', help='Release directory to grab files from')
00064     parser.add_argument('--release-folder', type=str, default='firmware', help='Directory to create and place files in')
00065     parser.add_argument('--toolchain', type=str, default='ARM', help='Toolchain directory if present')
00066     parser.add_argument('--clean', dest='clean', action='store_true', help='Rebuild or delete build folder before compile')
00067     parser.add_argument('-v', dest='verbosity', action='count', help='Pass verbosity level to mbed compile -vv for more')
00068     parser.set_defaults(clean=False)
00069     parser.set_defaults(release=False)
00070     args = parser.parse_args()
00071     self_path = os.path.abspath(__file__)
00072     tools_dir = os.path.dirname(self_path)
00073     daplink_dir = os.path.dirname(tools_dir)
00074     if os.path.basename(tools_dir) != "tools":
00075         print("Error - this script must be run from the tools directory")
00076         exit(-1)
00077     version_git_dir = os.path.join(daplink_dir, "source", "daplink")
00078     generate_version_file(version_git_dir)
00079     if not args.projects == []: 
00080         for project in args.projects:
00081             print("Compiling %s" % project)
00082             (cli_hex_output,crc_file_output) = mbedcli_tools.mbedcli_run(daplink_dir, args.build_folder, project, args.toolchain, args.clean, args.verbosity)
00083             print("Creating crc padded binaries %s" % os.path.basename(cli_hex_output))
00084             post_build_script(cli_hex_output, crc_file_output, args.board_id, args.family_id, args.bin_offset)
00085     else:
00086         print("compiling all firmware")
00087         #generate a dictionary of board ID, and family ID
00088         id_map = {}
00089         for board_id, family_id, firmware, bootloader, target in info.SUPPORTED_CONFIGURATIONS:
00090             if firmware in id_map:
00091                 id_map[firmware].append((hex(board_id), hex(family_id)))
00092             else:
00093                 id_map[firmware] = [(hex(board_id), hex(family_id))]
00094         for project in project_list:
00095             print("Compiling %s" % project)
00096             (cli_hex_output,crc_file_output) = mbedcli_tools.mbedcli_run(daplink_dir, args.build_folder, project, args.toolchain, args.clean, args.verbosity)
00097             print("Creating crc padded binaries %s" % os.path.basename(cli_hex_output))
00098             #can be a legacy build or 0 board_id and family_id
00099             post_build_script(cli_hex_output, crc_file_output)
00100             #do a build with board_id and family_id
00101             if project in id_map:
00102                 for (boardid, familyid) in id_map[project]:
00103                     print(project, boardid, familyid)
00104                     post_build_script(cli_hex_output, crc_file_output, boardid, familyid)
00105     if args.release is True:
00106         release_version = 0
00107         with open(os.path.join("records","tools", VERSION_YAML), 'r') as ver_yaml:
00108             try:
00109                 verdict = yaml.load(ver_yaml)
00110                 release_version = int(verdict['common']['macros'][0].split('=')[1])
00111             except yaml.YAMLError as ex:
00112                 print("Found yaml parse error", ex)
00113 
00114         release_dir = args.release_folder + "_%04i" % release_version 
00115         if os.path.exists(release_dir):
00116             print("Deleting %s" % release_dir)
00117             shutil.rmtree(release_dir, ignore_errors=True)
00118         print("Releasing directory: " + release_dir)
00119         toolchain_dir = args.toolchain+"-CUSTOM_PROFILE"
00120         package_release_files(args.build_folder, release_dir, release_version, toolchain_dir)
00121 
00122 main()