Brian Daniels / mbed-tools

Fork of mbed-tools by Morpheus

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers iar.py Source File

iar.py

00001 """
00002 mbed SDK
00003 Copyright (c) 2011-2015 ARM Limited
00004 
00005 Licensed under the Apache License, Version 2.0 (the "License");
00006 you may not use this file except in compliance with the License.
00007 You may obtain a copy of the License at
00008 
00009     http://www.apache.org/licenses/LICENSE-2.0
00010 
00011 Unless required by applicable law or agreed to in writing, software
00012 distributed under the License is distributed on an "AS IS" BASIS,
00013 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014 See the License for the specific language governing permissions and
00015 limitations under the License.
00016 """
00017 import re
00018 import os
00019 from project_generator_definitions.definitions import ProGenDef
00020 
00021 from tools.export.exporters import Exporter
00022 from tools.targets import TARGET_MAP, TARGET_NAMES
00023 
00024 # If you wish to add a new target, add it to project_generator_definitions, and then
00025 # define progen_target name in the target class (`` self.progen_target = 'my_target_name' ``)
00026 class IAREmbeddedWorkbench (Exporter):
00027     """
00028     Exporter class for IAR Systems. This class uses project generator.
00029     """
00030     # These 2 are currently for exporters backward compatiblity
00031     NAME = 'IAR'
00032     TOOLCHAIN = 'IAR'
00033     # PROGEN_ACTIVE contains information for exporter scripts that this is using progen
00034     PROGEN_ACTIVE = True
00035 
00036     # backward compatibility with our scripts
00037     TARGETS = []
00038     for target in TARGET_NAMES:
00039         try:
00040             if (ProGenDef('iar').is_supported(str(TARGET_MAP[target])) or
00041                 ProGenDef('iar').is_supported(TARGET_MAP[target].progen['target'])):
00042                 TARGETS.append(target)
00043         except AttributeError:
00044             # target is not supported yet
00045             continue
00046 
00047     def generate (self):
00048         """ Generates the project files """
00049         project_data = self.progen_get_project_data()
00050         tool_specific = {}
00051         # Expand tool specific settings by IAR specific settings which are required
00052         try:
00053             if TARGET_MAP[self.target].progen['iar']['template']:
00054                 tool_specific['iar'] = TARGET_MAP[self.target].progen['iar']
00055         except KeyError:
00056             # use default template
00057             # by the mbed projects
00058             tool_specific['iar'] = {
00059                     # We currently don't use misc, template sets those for us
00060                     # 'misc': {
00061                     #     'cxx_flags': ['--no_rtti', '--no_exceptions'],
00062                     #     'c_flags': ['--diag_suppress=Pa050,Pa084,Pa093,Pa082'],
00063                     #     'ld_flags': ['--skip_dynamic_initialization'],
00064                     # },
00065                     'template': [os.path.join(os.path.dirname(__file__),  'iar_template.ewp.tmpl')],
00066             }
00067 
00068         project_data['tool_specific'] = {}
00069         project_data['tool_specific'].update(tool_specific)
00070         self.progen_gen_file('iar_arm', project_data)
00071 
00072 # Currently not used, we should reuse folder_name to create virtual folders
00073 class IarFolder ():
00074     """
00075     This is a recursive folder object.
00076     To present the folder structure in the IDE as it is presented on the disk.
00077     This can be used for uvision as well if you replace the __str__ method.
00078     Example:
00079     files: ./main.cpp, ./apis/I2C.h, ./mbed/common/I2C.cpp
00080     in the project this would look like:
00081     main.cpp
00082     common/I2C.cpp
00083     input:
00084     folder_level : folder path to current folder
00085     folder_name : name of current folder
00086     source_files : list of source_files (all must be in same directory)
00087     """
00088     def __init__(self, folder_level, folder_name, source_files):
00089         self.folder_level  = folder_level
00090         self.folder_name  = folder_name
00091         self.source_files  = source_files
00092         self.sub_folders  = {}
00093 
00094     def __str__ (self):
00095         """
00096         converts the folder structue to IAR project format.
00097         """
00098         group_start = ""
00099         group_end = ""
00100         if self.folder_name  != "":
00101             group_start = "<group>\n<name>%s</name>\n" %(self.folder_name )
00102             group_end = "</group>\n"
00103 
00104         str_content = group_start
00105         #Add files in current folder
00106         if self.source_files :
00107             for src in self.source_files :
00108                 str_content += "<file>\n<name>$PROJ_DIR$/%s</name>\n</file>\n" % src
00109         #Add sub folders
00110         if self.sub_folders :
00111             for folder_name in self.sub_folders .iterkeys():
00112                 str_content += self.sub_folders [folder_name].__str__()
00113 
00114         str_content += group_end
00115         return str_content
00116 
00117     def insert_file (self, source_input):
00118         """
00119         Inserts a source file into the folder tree
00120         """
00121         if self.source_files :
00122             #All source_files in a IarFolder must be in same directory.
00123             dir_sources = IarFolder.get_directory(self.source_files [0])
00124             #Check if sources are already at their deepest level.
00125             if not self.folder_level  == dir_sources:
00126                 _reg_exp = r"^" + re.escape(self.folder_level ) + r"[/\\]?([^/\\]+)"
00127                 folder_name = re.match(_reg_exp, dir_sources).group(1)
00128                 self.sub_folders [folder_name] = IarFolder(os.path.join(self.folder_level , folder_name), folder_name, self.source_files )
00129                 self.source_files  = []
00130 
00131         dir_input = IarFolder.get_directory(source_input)
00132         if dir_input == self.folder_level :
00133             self.source_files .append(source_input)
00134         else:
00135             _reg_exp = r"^" + re.escape(self.folder_level ) + r"[/\\]?([^/\\]+)"
00136             folder_name = re.match(_reg_exp, dir_input).group(1)
00137             if self.sub_folders .has_key(folder_name):
00138                 self.sub_folders [folder_name].insert_file(source_input)
00139             else:
00140                 if self.folder_level  == "":
00141                     #Top level exception
00142                     self.sub_folders [folder_name] = IarFolder(folder_name, folder_name, [source_input])
00143                 else:
00144                     self.sub_folders [folder_name] = IarFolder(os.path.join(self.folder_level , folder_name), folder_name, [source_input])
00145 
00146     @staticmethod
00147     def get_directory (file_path):
00148         """
00149         Returns the directory of the file
00150         """
00151         return os.path.dirname(file_path)