Clone of official tools

Committer:
The Other Jimmy
Date:
Wed Feb 15 13:53:18 2017 -0600
Revision:
35:da9c89f8be7d
Parent:
31:8ea194f6145b
Child:
36:96847d42f010
Update tools to mbed-os 5.3.5

Who changed what in which revision?

UserRevisionLine numberNew contents of line
The Other Jimmy 31:8ea194f6145b 1 import os
The Other Jimmy 31:8ea194f6145b 2 from os.path import sep, join, exists
The Other Jimmy 31:8ea194f6145b 3 from collections import namedtuple
The Other Jimmy 31:8ea194f6145b 4 from subprocess import Popen, PIPE
The Other Jimmy 31:8ea194f6145b 5 import shutil
The Other Jimmy 31:8ea194f6145b 6 import re
The Other Jimmy 31:8ea194f6145b 7 import sys
The Other Jimmy 31:8ea194f6145b 8
The Other Jimmy 31:8ea194f6145b 9 from tools.targets import TARGET_MAP
The Other Jimmy 31:8ea194f6145b 10 from tools.export.exporters import Exporter, TargetNotSupportedException
The Other Jimmy 31:8ea194f6145b 11 import json
The Other Jimmy 31:8ea194f6145b 12 from tools.export.cmsis import DeviceCMSIS
The Other Jimmy 31:8ea194f6145b 13 from multiprocessing import cpu_count
The Other Jimmy 31:8ea194f6145b 14
The Other Jimmy 31:8ea194f6145b 15 class IAR(Exporter):
The Other Jimmy 31:8ea194f6145b 16 NAME = 'iar'
The Other Jimmy 31:8ea194f6145b 17 TOOLCHAIN = 'IAR'
The Other Jimmy 31:8ea194f6145b 18
The Other Jimmy 31:8ea194f6145b 19 #iar_definitions.json location
The Other Jimmy 31:8ea194f6145b 20 def_loc = os.path.join(
The Other Jimmy 31:8ea194f6145b 21 os.path.dirname(os.path.abspath(__file__)), '..', '..', '..',
The Other Jimmy 31:8ea194f6145b 22 'tools','export', 'iar', 'iar_definitions.json')
The Other Jimmy 31:8ea194f6145b 23
The Other Jimmy 31:8ea194f6145b 24 #create a dictionary of the definitions
The Other Jimmy 31:8ea194f6145b 25 with open(def_loc, 'r') as f:
The Other Jimmy 31:8ea194f6145b 26 IAR_DEFS = json.load(f)
The Other Jimmy 31:8ea194f6145b 27
The Other Jimmy 35:da9c89f8be7d 28 def _iar_support(tgt, iar_targets):
The Other Jimmy 35:da9c89f8be7d 29 if "IAR" not in tgt.supported_toolchains:
The Other Jimmy 35:da9c89f8be7d 30 return False
The Other Jimmy 35:da9c89f8be7d 31 if hasattr(tgt, 'device_name') and tgt.device_name in iar_targets:
The Other Jimmy 35:da9c89f8be7d 32 return True
The Other Jimmy 35:da9c89f8be7d 33 if tgt.name in iar_targets:
The Other Jimmy 35:da9c89f8be7d 34 return True
The Other Jimmy 35:da9c89f8be7d 35 return False
The Other Jimmy 35:da9c89f8be7d 36
The Other Jimmy 35:da9c89f8be7d 37 #supported targets have a name or device_name which maps to a definition
The Other Jimmy 35:da9c89f8be7d 38 #in iar_definitions.json
The Other Jimmy 35:da9c89f8be7d 39 TARGETS = [target for target, obj in TARGET_MAP.iteritems() if
The Other Jimmy 35:da9c89f8be7d 40 _iar_support(obj, IAR_DEFS.keys())]
The Other Jimmy 31:8ea194f6145b 41
The Other Jimmy 31:8ea194f6145b 42 def iar_groups(self, grouped_src):
The Other Jimmy 31:8ea194f6145b 43 """Return a namedtuple of group info
The Other Jimmy 31:8ea194f6145b 44 Positional Arguments:
The Other Jimmy 31:8ea194f6145b 45 grouped_src: dictionary mapping a group(str) to sources
The Other Jimmy 31:8ea194f6145b 46 within it (list of file names)
The Other Jimmy 31:8ea194f6145b 47 Relevant part of IAR template
The Other Jimmy 31:8ea194f6145b 48 {% for group in groups %}
The Other Jimmy 31:8ea194f6145b 49 <group>
The Other Jimmy 31:8ea194f6145b 50 <name>group.name</name>
The Other Jimmy 31:8ea194f6145b 51 {% for file in group.files %}
The Other Jimmy 31:8ea194f6145b 52 <file>
The Other Jimmy 31:8ea194f6145b 53 <name>$PROJ_DIR${{file}}</name>
The Other Jimmy 31:8ea194f6145b 54 </file>
The Other Jimmy 31:8ea194f6145b 55 {% endfor %}
The Other Jimmy 31:8ea194f6145b 56 </group>
The Other Jimmy 31:8ea194f6145b 57 {% endfor %}
The Other Jimmy 31:8ea194f6145b 58 """
The Other Jimmy 31:8ea194f6145b 59 IARgroup = namedtuple('IARgroup', ['name','files'])
The Other Jimmy 31:8ea194f6145b 60 groups = []
The Other Jimmy 31:8ea194f6145b 61 for name, files in grouped_src.items():
The Other Jimmy 31:8ea194f6145b 62 groups.append(IARgroup(name,files))
The Other Jimmy 31:8ea194f6145b 63 return groups
The Other Jimmy 31:8ea194f6145b 64
The Other Jimmy 31:8ea194f6145b 65 def iar_device(self):
The Other Jimmy 31:8ea194f6145b 66 """Retrieve info from iar_definitions.json"""
The Other Jimmy 35:da9c89f8be7d 67 tgt = TARGET_MAP[self.target]
The Other Jimmy 35:da9c89f8be7d 68 device_name = (tgt.device_name if hasattr(tgt, "device_name") else
The Other Jimmy 35:da9c89f8be7d 69 tgt.name)
The Other Jimmy 31:8ea194f6145b 70 device_info = self.IAR_DEFS[device_name]
The Other Jimmy 31:8ea194f6145b 71 iar_defaults ={
The Other Jimmy 31:8ea194f6145b 72 "OGChipSelectEditMenu": "",
The Other Jimmy 31:8ea194f6145b 73 "CoreVariant": '',
The Other Jimmy 31:8ea194f6145b 74 "GFPUCoreSlave": '',
The Other Jimmy 31:8ea194f6145b 75 "GFPUCoreSlave2": 40,
The Other Jimmy 31:8ea194f6145b 76 "GBECoreSlave": 35,
The Other Jimmy 35:da9c89f8be7d 77 "GBECoreSlave2": '',
The Other Jimmy 31:8ea194f6145b 78 "FPU2": 0,
The Other Jimmy 31:8ea194f6145b 79 "NrRegs": 0,
The Other Jimmy 35:da9c89f8be7d 80 "NEON": '',
The Other Jimmy 31:8ea194f6145b 81 }
The Other Jimmy 31:8ea194f6145b 82
The Other Jimmy 31:8ea194f6145b 83 iar_defaults.update(device_info)
The Other Jimmy 31:8ea194f6145b 84 IARdevice = namedtuple('IARdevice', iar_defaults.keys())
The Other Jimmy 31:8ea194f6145b 85 return IARdevice(**iar_defaults)
The Other Jimmy 31:8ea194f6145b 86
The Other Jimmy 31:8ea194f6145b 87 def format_file(self, file):
The Other Jimmy 31:8ea194f6145b 88 """Make IAR compatible path"""
The Other Jimmy 31:8ea194f6145b 89 return join('$PROJ_DIR$',file)
The Other Jimmy 31:8ea194f6145b 90
The Other Jimmy 31:8ea194f6145b 91 def format_src(self, srcs):
The Other Jimmy 31:8ea194f6145b 92 """Group source files"""
The Other Jimmy 31:8ea194f6145b 93 grouped = self.group_project_files(srcs)
The Other Jimmy 31:8ea194f6145b 94 for group, files in grouped.items():
The Other Jimmy 31:8ea194f6145b 95 grouped[group] = [self.format_file(src) for src in files]
The Other Jimmy 31:8ea194f6145b 96 return grouped
The Other Jimmy 31:8ea194f6145b 97
The Other Jimmy 31:8ea194f6145b 98 def generate(self):
The Other Jimmy 31:8ea194f6145b 99 """Generate the .eww, .ewd, and .ewp files"""
The Other Jimmy 31:8ea194f6145b 100 srcs = self.resources.headers + self.resources.s_sources + \
The Other Jimmy 31:8ea194f6145b 101 self.resources.c_sources + self.resources.cpp_sources + \
The Other Jimmy 31:8ea194f6145b 102 self.resources.objects + self.resources.libraries
The Other Jimmy 31:8ea194f6145b 103 flags = self.flags
The Other Jimmy 35:da9c89f8be7d 104 c_flags = list(set(flags['common_flags']
The Other Jimmy 31:8ea194f6145b 105 + flags['c_flags']
The Other Jimmy 31:8ea194f6145b 106 + flags['cxx_flags']))
The Other Jimmy 35:da9c89f8be7d 107 # Flags set in template to be set by user in IDE
The Other Jimmy 35:da9c89f8be7d 108 template = ["--vla", "--no_static_destruction"]
The Other Jimmy 35:da9c89f8be7d 109 # Flag invalid if set in template
The Other Jimmy 35:da9c89f8be7d 110 # Optimizations are also set in template
The Other Jimmy 35:da9c89f8be7d 111 invalid_flag = lambda x: x in template or re.match("-O(\d|time|n)", x)
The Other Jimmy 35:da9c89f8be7d 112 flags['c_flags'] = [flag for flag in c_flags if not invalid_flag(flag)]
The Other Jimmy 31:8ea194f6145b 113
The Other Jimmy 31:8ea194f6145b 114 try:
The Other Jimmy 31:8ea194f6145b 115 debugger = DeviceCMSIS(self.target).debug.replace('-','').upper()
The Other Jimmy 31:8ea194f6145b 116 except TargetNotSupportedException:
The Other Jimmy 31:8ea194f6145b 117 debugger = "CMSISDAP"
The Other Jimmy 31:8ea194f6145b 118
The Other Jimmy 31:8ea194f6145b 119 ctx = {
The Other Jimmy 31:8ea194f6145b 120 'name': self.project_name,
The Other Jimmy 31:8ea194f6145b 121 'groups': self.iar_groups(self.format_src(srcs)),
The Other Jimmy 31:8ea194f6145b 122 'linker_script': self.format_file(self.resources.linker_script),
The Other Jimmy 31:8ea194f6145b 123 'include_paths': [self.format_file(src) for src in self.resources.inc_dirs],
The Other Jimmy 31:8ea194f6145b 124 'device': self.iar_device(),
The Other Jimmy 31:8ea194f6145b 125 'ewp': sep+self.project_name + ".ewp",
The Other Jimmy 31:8ea194f6145b 126 'debugger': debugger
The Other Jimmy 31:8ea194f6145b 127 }
The Other Jimmy 31:8ea194f6145b 128 ctx.update(flags)
The Other Jimmy 31:8ea194f6145b 129
The Other Jimmy 31:8ea194f6145b 130 self.gen_file('iar/eww.tmpl', ctx, self.project_name + ".eww")
The Other Jimmy 31:8ea194f6145b 131 self.gen_file('iar/ewd.tmpl', ctx, self.project_name + ".ewd")
The Other Jimmy 31:8ea194f6145b 132 self.gen_file('iar/ewp.tmpl', ctx, self.project_name + ".ewp")
The Other Jimmy 31:8ea194f6145b 133
The Other Jimmy 31:8ea194f6145b 134 @staticmethod
The Other Jimmy 31:8ea194f6145b 135 def build(project_name, log_name="build_log.txt", cleanup=True):
The Other Jimmy 31:8ea194f6145b 136 """ Build IAR project """
The Other Jimmy 31:8ea194f6145b 137 # > IarBuild [project_path] -build [project_name]
The Other Jimmy 31:8ea194f6145b 138 proj_file = project_name + ".ewp"
The Other Jimmy 31:8ea194f6145b 139 cmd = ["IarBuild", proj_file, '-build', project_name]
The Other Jimmy 31:8ea194f6145b 140
The Other Jimmy 31:8ea194f6145b 141 # IAR does not support a '0' option to automatically use all
The Other Jimmy 31:8ea194f6145b 142 # available CPUs, so we use Python's multiprocessing library
The Other Jimmy 31:8ea194f6145b 143 # to detect the number of CPUs available
The Other Jimmy 31:8ea194f6145b 144 cpus_available = cpu_count()
The Other Jimmy 31:8ea194f6145b 145 jobs = cpus_available if cpus_available else None
The Other Jimmy 31:8ea194f6145b 146
The Other Jimmy 31:8ea194f6145b 147 # Only add the parallel flag if we're using more than one CPU
The Other Jimmy 31:8ea194f6145b 148 if jobs:
The Other Jimmy 31:8ea194f6145b 149 cmd += ['-parallel', str(jobs)]
The Other Jimmy 31:8ea194f6145b 150
The Other Jimmy 31:8ea194f6145b 151 # Build the project
The Other Jimmy 31:8ea194f6145b 152 p = Popen(cmd, stdout=PIPE, stderr=PIPE)
The Other Jimmy 31:8ea194f6145b 153 out, err = p.communicate()
The Other Jimmy 31:8ea194f6145b 154 ret_code = p.returncode
The Other Jimmy 31:8ea194f6145b 155
The Other Jimmy 31:8ea194f6145b 156 out_string = "=" * 10 + "STDOUT" + "=" * 10 + "\n"
The Other Jimmy 31:8ea194f6145b 157 out_string += out
The Other Jimmy 31:8ea194f6145b 158 out_string += "=" * 10 + "STDERR" + "=" * 10 + "\n"
The Other Jimmy 31:8ea194f6145b 159 out_string += err
The Other Jimmy 31:8ea194f6145b 160
The Other Jimmy 31:8ea194f6145b 161 if ret_code == 0:
The Other Jimmy 31:8ea194f6145b 162 out_string += "SUCCESS"
The Other Jimmy 31:8ea194f6145b 163 else:
The Other Jimmy 31:8ea194f6145b 164 out_string += "FAILURE"
The Other Jimmy 31:8ea194f6145b 165
The Other Jimmy 31:8ea194f6145b 166 print out_string
The Other Jimmy 31:8ea194f6145b 167
The Other Jimmy 31:8ea194f6145b 168 if log_name:
The Other Jimmy 31:8ea194f6145b 169 # Write the output to the log file
The Other Jimmy 31:8ea194f6145b 170 with open(log_name, 'w+') as f:
The Other Jimmy 31:8ea194f6145b 171 f.write(out_string)
The Other Jimmy 31:8ea194f6145b 172
The Other Jimmy 31:8ea194f6145b 173 # Cleanup the exported and built files
The Other Jimmy 31:8ea194f6145b 174 if cleanup:
The Other Jimmy 31:8ea194f6145b 175 os.remove(project_name + ".ewp")
The Other Jimmy 31:8ea194f6145b 176 os.remove(project_name + ".ewd")
The Other Jimmy 31:8ea194f6145b 177 os.remove(project_name + ".eww")
The Other Jimmy 31:8ea194f6145b 178 # legacy output file location
The Other Jimmy 31:8ea194f6145b 179 if exists('.build'):
The Other Jimmy 31:8ea194f6145b 180 shutil.rmtree('.build')
The Other Jimmy 31:8ea194f6145b 181 if exists('BUILD'):
The Other Jimmy 31:8ea194f6145b 182 shutil.rmtree('BUILD')
The Other Jimmy 31:8ea194f6145b 183
The Other Jimmy 31:8ea194f6145b 184 if ret_code !=0:
The Other Jimmy 31:8ea194f6145b 185 # Seems like something went wrong.
The Other Jimmy 31:8ea194f6145b 186 return -1
The Other Jimmy 31:8ea194f6145b 187 else:
The Other Jimmy 31:8ea194f6145b 188 return 0