Clone of official tools
export/iar/__init__.py@36:96847d42f010, 2017-06-22 (annotated)
- Committer:
- The Other Jimmy
- Date:
- Thu Jun 22 11:12:28 2017 -0500
- Revision:
- 36:96847d42f010
- Parent:
- 35:da9c89f8be7d
- Child:
- 38:399953da035d
Tools release 5.5.1
Who changed what in which revision?
User | Revision | Line number | New 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 |
31:8ea194f6145b | 16 | class IAR(Exporter): |
The Other Jimmy |
31:8ea194f6145b | 17 | NAME = 'iar' |
The Other Jimmy |
31:8ea194f6145b | 18 | TOOLCHAIN = 'IAR' |
The Other Jimmy |
31:8ea194f6145b | 19 | |
The Other Jimmy |
31:8ea194f6145b | 20 | #iar_definitions.json location |
The Other Jimmy |
31:8ea194f6145b | 21 | def_loc = os.path.join( |
The Other Jimmy |
36:96847d42f010 | 22 | os.path.dirname(os.path.abspath(__file__)), '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 |
36:96847d42f010 | 81 | "CExtraOptionsCheck": 0, |
The Other Jimmy |
36:96847d42f010 | 82 | "CExtraOptions": "", |
The Other Jimmy |
36:96847d42f010 | 83 | "CMSISDAPJtagSpeedList": 0, |
The Other Jimmy |
31:8ea194f6145b | 84 | } |
The Other Jimmy |
31:8ea194f6145b | 85 | |
The Other Jimmy |
31:8ea194f6145b | 86 | iar_defaults.update(device_info) |
The Other Jimmy |
31:8ea194f6145b | 87 | IARdevice = namedtuple('IARdevice', iar_defaults.keys()) |
The Other Jimmy |
31:8ea194f6145b | 88 | return IARdevice(**iar_defaults) |
The Other Jimmy |
31:8ea194f6145b | 89 | |
The Other Jimmy |
31:8ea194f6145b | 90 | def format_file(self, file): |
The Other Jimmy |
31:8ea194f6145b | 91 | """Make IAR compatible path""" |
The Other Jimmy |
31:8ea194f6145b | 92 | return join('$PROJ_DIR$',file) |
The Other Jimmy |
31:8ea194f6145b | 93 | |
The Other Jimmy |
31:8ea194f6145b | 94 | def format_src(self, srcs): |
The Other Jimmy |
31:8ea194f6145b | 95 | """Group source files""" |
The Other Jimmy |
31:8ea194f6145b | 96 | grouped = self.group_project_files(srcs) |
The Other Jimmy |
31:8ea194f6145b | 97 | for group, files in grouped.items(): |
The Other Jimmy |
31:8ea194f6145b | 98 | grouped[group] = [self.format_file(src) for src in files] |
The Other Jimmy |
31:8ea194f6145b | 99 | return grouped |
The Other Jimmy |
31:8ea194f6145b | 100 | |
The Other Jimmy |
31:8ea194f6145b | 101 | def generate(self): |
The Other Jimmy |
31:8ea194f6145b | 102 | """Generate the .eww, .ewd, and .ewp files""" |
The Other Jimmy |
36:96847d42f010 | 103 | if not self.resources.linker_script: |
The Other Jimmy |
36:96847d42f010 | 104 | raise NotSupportedException("No linker script found.") |
The Other Jimmy |
31:8ea194f6145b | 105 | srcs = self.resources.headers + self.resources.s_sources + \ |
The Other Jimmy |
31:8ea194f6145b | 106 | self.resources.c_sources + self.resources.cpp_sources + \ |
The Other Jimmy |
31:8ea194f6145b | 107 | self.resources.objects + self.resources.libraries |
The Other Jimmy |
31:8ea194f6145b | 108 | flags = self.flags |
The Other Jimmy |
35:da9c89f8be7d | 109 | c_flags = list(set(flags['common_flags'] |
The Other Jimmy |
31:8ea194f6145b | 110 | + flags['c_flags'] |
The Other Jimmy |
31:8ea194f6145b | 111 | + flags['cxx_flags'])) |
The Other Jimmy |
35:da9c89f8be7d | 112 | # Flags set in template to be set by user in IDE |
The Other Jimmy |
35:da9c89f8be7d | 113 | template = ["--vla", "--no_static_destruction"] |
The Other Jimmy |
35:da9c89f8be7d | 114 | # Flag invalid if set in template |
The Other Jimmy |
35:da9c89f8be7d | 115 | # Optimizations are also set in template |
The Other Jimmy |
36:96847d42f010 | 116 | invalid_flag = lambda x: x in template or re.match("-O(\d|time|n|hz?)", x) |
The Other Jimmy |
35:da9c89f8be7d | 117 | flags['c_flags'] = [flag for flag in c_flags if not invalid_flag(flag)] |
The Other Jimmy |
31:8ea194f6145b | 118 | |
The Other Jimmy |
31:8ea194f6145b | 119 | try: |
The Other Jimmy |
31:8ea194f6145b | 120 | debugger = DeviceCMSIS(self.target).debug.replace('-','').upper() |
The Other Jimmy |
31:8ea194f6145b | 121 | except TargetNotSupportedException: |
The Other Jimmy |
31:8ea194f6145b | 122 | debugger = "CMSISDAP" |
The Other Jimmy |
31:8ea194f6145b | 123 | |
The Other Jimmy |
31:8ea194f6145b | 124 | ctx = { |
The Other Jimmy |
31:8ea194f6145b | 125 | 'name': self.project_name, |
The Other Jimmy |
31:8ea194f6145b | 126 | 'groups': self.iar_groups(self.format_src(srcs)), |
The Other Jimmy |
31:8ea194f6145b | 127 | 'linker_script': self.format_file(self.resources.linker_script), |
The Other Jimmy |
31:8ea194f6145b | 128 | 'include_paths': [self.format_file(src) for src in self.resources.inc_dirs], |
The Other Jimmy |
31:8ea194f6145b | 129 | 'device': self.iar_device(), |
The Other Jimmy |
31:8ea194f6145b | 130 | 'ewp': sep+self.project_name + ".ewp", |
The Other Jimmy |
31:8ea194f6145b | 131 | 'debugger': debugger |
The Other Jimmy |
31:8ea194f6145b | 132 | } |
The Other Jimmy |
31:8ea194f6145b | 133 | ctx.update(flags) |
The Other Jimmy |
31:8ea194f6145b | 134 | |
The Other Jimmy |
31:8ea194f6145b | 135 | self.gen_file('iar/eww.tmpl', ctx, self.project_name + ".eww") |
The Other Jimmy |
31:8ea194f6145b | 136 | self.gen_file('iar/ewd.tmpl', ctx, self.project_name + ".ewd") |
The Other Jimmy |
31:8ea194f6145b | 137 | self.gen_file('iar/ewp.tmpl', ctx, self.project_name + ".ewp") |
The Other Jimmy |
31:8ea194f6145b | 138 | |
The Other Jimmy |
31:8ea194f6145b | 139 | @staticmethod |
The Other Jimmy |
31:8ea194f6145b | 140 | def build(project_name, log_name="build_log.txt", cleanup=True): |
The Other Jimmy |
31:8ea194f6145b | 141 | """ Build IAR project """ |
The Other Jimmy |
31:8ea194f6145b | 142 | # > IarBuild [project_path] -build [project_name] |
The Other Jimmy |
31:8ea194f6145b | 143 | proj_file = project_name + ".ewp" |
The Other Jimmy |
31:8ea194f6145b | 144 | cmd = ["IarBuild", proj_file, '-build', project_name] |
The Other Jimmy |
31:8ea194f6145b | 145 | |
The Other Jimmy |
31:8ea194f6145b | 146 | # IAR does not support a '0' option to automatically use all |
The Other Jimmy |
31:8ea194f6145b | 147 | # available CPUs, so we use Python's multiprocessing library |
The Other Jimmy |
31:8ea194f6145b | 148 | # to detect the number of CPUs available |
The Other Jimmy |
31:8ea194f6145b | 149 | cpus_available = cpu_count() |
The Other Jimmy |
31:8ea194f6145b | 150 | jobs = cpus_available if cpus_available else None |
The Other Jimmy |
31:8ea194f6145b | 151 | |
The Other Jimmy |
31:8ea194f6145b | 152 | # Only add the parallel flag if we're using more than one CPU |
The Other Jimmy |
31:8ea194f6145b | 153 | if jobs: |
The Other Jimmy |
31:8ea194f6145b | 154 | cmd += ['-parallel', str(jobs)] |
The Other Jimmy |
31:8ea194f6145b | 155 | |
The Other Jimmy |
31:8ea194f6145b | 156 | # Build the project |
The Other Jimmy |
31:8ea194f6145b | 157 | p = Popen(cmd, stdout=PIPE, stderr=PIPE) |
The Other Jimmy |
31:8ea194f6145b | 158 | out, err = p.communicate() |
The Other Jimmy |
31:8ea194f6145b | 159 | ret_code = p.returncode |
The Other Jimmy |
31:8ea194f6145b | 160 | |
The Other Jimmy |
31:8ea194f6145b | 161 | out_string = "=" * 10 + "STDOUT" + "=" * 10 + "\n" |
The Other Jimmy |
31:8ea194f6145b | 162 | out_string += out |
The Other Jimmy |
31:8ea194f6145b | 163 | out_string += "=" * 10 + "STDERR" + "=" * 10 + "\n" |
The Other Jimmy |
31:8ea194f6145b | 164 | out_string += err |
The Other Jimmy |
31:8ea194f6145b | 165 | |
The Other Jimmy |
31:8ea194f6145b | 166 | if ret_code == 0: |
The Other Jimmy |
31:8ea194f6145b | 167 | out_string += "SUCCESS" |
The Other Jimmy |
31:8ea194f6145b | 168 | else: |
The Other Jimmy |
31:8ea194f6145b | 169 | out_string += "FAILURE" |
The Other Jimmy |
31:8ea194f6145b | 170 | |
The Other Jimmy |
31:8ea194f6145b | 171 | print out_string |
The Other Jimmy |
31:8ea194f6145b | 172 | |
The Other Jimmy |
31:8ea194f6145b | 173 | if log_name: |
The Other Jimmy |
31:8ea194f6145b | 174 | # Write the output to the log file |
The Other Jimmy |
31:8ea194f6145b | 175 | with open(log_name, 'w+') as f: |
The Other Jimmy |
31:8ea194f6145b | 176 | f.write(out_string) |
The Other Jimmy |
31:8ea194f6145b | 177 | |
The Other Jimmy |
31:8ea194f6145b | 178 | # Cleanup the exported and built files |
The Other Jimmy |
31:8ea194f6145b | 179 | if cleanup: |
The Other Jimmy |
31:8ea194f6145b | 180 | os.remove(project_name + ".ewp") |
The Other Jimmy |
31:8ea194f6145b | 181 | os.remove(project_name + ".ewd") |
The Other Jimmy |
31:8ea194f6145b | 182 | os.remove(project_name + ".eww") |
The Other Jimmy |
31:8ea194f6145b | 183 | # legacy output file location |
The Other Jimmy |
31:8ea194f6145b | 184 | if exists('.build'): |
The Other Jimmy |
31:8ea194f6145b | 185 | shutil.rmtree('.build') |
The Other Jimmy |
31:8ea194f6145b | 186 | if exists('BUILD'): |
The Other Jimmy |
31:8ea194f6145b | 187 | shutil.rmtree('BUILD') |
The Other Jimmy |
31:8ea194f6145b | 188 | |
The Other Jimmy |
31:8ea194f6145b | 189 | if ret_code !=0: |
The Other Jimmy |
31:8ea194f6145b | 190 | # Seems like something went wrong. |
The Other Jimmy |
31:8ea194f6145b | 191 | return -1 |
The Other Jimmy |
31:8ea194f6145b | 192 | else: |
The Other Jimmy |
31:8ea194f6145b | 193 | return 0 |