Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-sdk-tools by
__init__.py
00001 import os 00002 from os.path import sep, join, exists 00003 from collections import namedtuple 00004 from subprocess import Popen, PIPE 00005 import shutil 00006 import re 00007 import sys 00008 00009 from tools.targets import TARGET_MAP 00010 from tools.export.exporters import Exporter, TargetNotSupportedException 00011 import json 00012 from tools.export.cmsis import DeviceCMSIS 00013 from multiprocessing import cpu_count 00014 00015 class IAR(Exporter): 00016 NAME = 'iar' 00017 TOOLCHAIN = 'IAR' 00018 00019 #iar_definitions.json location 00020 def_loc = os.path.join( 00021 os.path.dirname(os.path.abspath(__file__)), '..', '..', '..', 00022 'tools','export', 'iar', 'iar_definitions.json') 00023 00024 #create a dictionary of the definitions 00025 with open(def_loc, 'r') as f: 00026 IAR_DEFS = json.load(f) 00027 00028 #supported targets have a device name and corresponding definition in 00029 #iar_definitions.json 00030 TARGETS = [target for target, obj in TARGET_MAP.iteritems() 00031 if hasattr(obj, 'device_name') and 00032 obj.device_name in IAR_DEFS.keys() and "IAR" in obj.supported_toolchains] 00033 00034 def iar_groups(self, grouped_src): 00035 """Return a namedtuple of group info 00036 Positional Arguments: 00037 grouped_src: dictionary mapping a group(str) to sources 00038 within it (list of file names) 00039 Relevant part of IAR template 00040 {% for group in groups %} 00041 <group> 00042 <name>group.name</name> 00043 {% for file in group.files %} 00044 <file> 00045 <name>$PROJ_DIR${{file}}</name> 00046 </file> 00047 {% endfor %} 00048 </group> 00049 {% endfor %} 00050 """ 00051 IARgroup = namedtuple('IARgroup', ['name','files']) 00052 groups = [] 00053 for name, files in grouped_src.items(): 00054 groups.append(IARgroup(name,files)) 00055 return groups 00056 00057 def iar_device(self): 00058 """Retrieve info from iar_definitions.json""" 00059 device_name = TARGET_MAP[self.target].device_name 00060 device_info = self.IAR_DEFS[device_name] 00061 iar_defaults ={ 00062 "OGChipSelectEditMenu": "", 00063 "CoreVariant": '', 00064 "GFPUCoreSlave": '', 00065 "GFPUCoreSlave2": 40, 00066 "GBECoreSlave": 35, 00067 "FPU2": 0, 00068 "NrRegs": 0, 00069 } 00070 00071 iar_defaults.update(device_info) 00072 IARdevice = namedtuple('IARdevice', iar_defaults.keys()) 00073 return IARdevice(**iar_defaults) 00074 00075 def format_file(self, file): 00076 """Make IAR compatible path""" 00077 return join('$PROJ_DIR$',file) 00078 00079 def format_src(self, srcs): 00080 """Group source files""" 00081 grouped = self.group_project_files(srcs) 00082 for group, files in grouped.items(): 00083 grouped[group] = [self.format_file(src) for src in files] 00084 return grouped 00085 00086 def generate(self): 00087 """Generate the .eww, .ewd, and .ewp files""" 00088 srcs = self.resources.headers + self.resources.s_sources + \ 00089 self.resources.c_sources + self.resources.cpp_sources + \ 00090 self.resources.objects + self.resources.libraries 00091 flags = self.flags 00092 flags['c_flags'] = list(set(flags['common_flags'] 00093 + flags['c_flags'] 00094 + flags['cxx_flags'])) 00095 if '--vla' in flags['c_flags']: 00096 flags['c_flags'].remove('--vla') 00097 if '--no_static_destruction' in flags['c_flags']: 00098 flags['c_flags'].remove('--no_static_destruction') 00099 #Optimizations 00100 if '-Oh' in flags['c_flags']: 00101 flags['c_flags'].remove('-Oh') 00102 00103 try: 00104 debugger = DeviceCMSIS(self.target).debug.replace('-','').upper() 00105 except TargetNotSupportedException: 00106 debugger = "CMSISDAP" 00107 00108 ctx = { 00109 'name': self.project_name, 00110 'groups': self.iar_groups(self.format_src(srcs)), 00111 'linker_script': self.format_file(self.resources.linker_script), 00112 'include_paths': [self.format_file(src) for src in self.resources.inc_dirs], 00113 'device': self.iar_device(), 00114 'ewp': sep+self.project_name + ".ewp", 00115 'debugger': debugger 00116 } 00117 ctx.update(flags) 00118 00119 self.gen_file('iar/eww.tmpl', ctx, self.project_name + ".eww") 00120 self.gen_file('iar/ewd.tmpl', ctx, self.project_name + ".ewd") 00121 self.gen_file('iar/ewp.tmpl', ctx, self.project_name + ".ewp") 00122 00123 @staticmethod 00124 def build(project_name, log_name="build_log.txt", cleanup=True): 00125 """ Build IAR project """ 00126 # > IarBuild [project_path] -build [project_name] 00127 proj_file = project_name + ".ewp" 00128 cmd = ["IarBuild", proj_file, '-build', project_name] 00129 00130 # IAR does not support a '0' option to automatically use all 00131 # available CPUs, so we use Python's multiprocessing library 00132 # to detect the number of CPUs available 00133 cpus_available = cpu_count() 00134 jobs = cpus_available if cpus_available else None 00135 00136 # Only add the parallel flag if we're using more than one CPU 00137 if jobs: 00138 cmd += ['-parallel', str(jobs)] 00139 00140 # Build the project 00141 p = Popen(cmd, stdout=PIPE, stderr=PIPE) 00142 out, err = p.communicate() 00143 ret_code = p.returncode 00144 00145 out_string = "=" * 10 + "STDOUT" + "=" * 10 + "\n" 00146 out_string += out 00147 out_string += "=" * 10 + "STDERR" + "=" * 10 + "\n" 00148 out_string += err 00149 00150 if ret_code == 0: 00151 out_string += "SUCCESS" 00152 else: 00153 out_string += "FAILURE" 00154 00155 print out_string 00156 00157 if log_name: 00158 # Write the output to the log file 00159 with open(log_name, 'w+') as f: 00160 f.write(out_string) 00161 00162 # Cleanup the exported and built files 00163 if cleanup: 00164 os.remove(project_name + ".ewp") 00165 os.remove(project_name + ".ewd") 00166 os.remove(project_name + ".eww") 00167 # legacy output file location 00168 if exists('.build'): 00169 shutil.rmtree('.build') 00170 if exists('BUILD'): 00171 shutil.rmtree('BUILD') 00172 00173 if ret_code !=0: 00174 # Seems like something went wrong. 00175 return -1 00176 else: 00177 return 0
Generated on Tue Jul 12 2022 21:14:58 by
1.7.2
