Clone of official tools

Committer:
theotherjimmy
Date:
Tue Oct 10 16:56:30 2017 -0500
Revision:
40:7d3fa6b99b2b
Parent:
38:399953da035d
Child:
43:2a7da56ebd24
Update to tools release 5.6.1

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