]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/tool/mbed/mbed-sdk/workspace_tools/export/iar.py
Merge commit 'fdc38ef3f92af7adeeb4de49550d8838c8a39b5c'
[qmk_firmware.git] / tmk_core / tool / mbed / mbed-sdk / workspace_tools / export / iar.py
1 """
2 mbed SDK
3 Copyright (c) 2011-2013 ARM Limited
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9     http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 """
17 from workspace_tools.export.exporters import Exporter
18 import re
19 import os
20 class IAREmbeddedWorkbench(Exporter):
21     """
22     Exporter class for IAR Systems.
23     """
24     NAME = 'IAR'
25     TOOLCHAIN = 'IAR'
26
27     TARGETS = [
28         'LPC1768',
29         'LPC1347',
30         'LPC11U24',
31         'LPC11U35_401',
32         'LPC11U35_501',
33         #Removed LPCCAPPUCCINO linker file and startup file missing
34         #'LPCCAPPUCCINO',
35         'LPC1114',
36         'LPC1549',
37         'LPC812',
38         'LPC4088',
39         'LPC4088_DM',
40         'LPC824',
41         'UBLOX_C027',
42         'ARCH_PRO',
43         'K20D50M',
44         'KL05Z',
45         'KL25Z',
46         'KL46Z',
47         'K22F',
48         'K64F',
49         'NUCLEO_F030R8',
50         'NUCLEO_F070RB',
51         'NUCLEO_F072RB',
52         'NUCLEO_F091RC',
53         'NUCLEO_F103RB',
54         'NUCLEO_F302R8',
55         'NUCLEO_F303RE',
56         'NUCLEO_F334R8',
57         'NUCLEO_F401RE',
58         'NUCLEO_F411RE',
59         'NUCLEO_L053R8',
60         'NUCLEO_L073RZ',
61         'NUCLEO_L152RE',
62         #'STM32F407', Fails to build same for GCC
63         'MAXWSNENV',
64         'MAX32600MBED',
65         'MTS_MDOT_F405RG',
66         'MTS_MDOT_F411RE',
67         'MTS_DRAGONFLY_F411RE',
68         'NRF51822',
69         'NRF51_DK',
70         'NRF51_DONGLE',
71         'DELTA_DFCM_NNN40',
72         'SEEED_TINY_BLE',
73         'HRM1017',
74         'ARCH_BLE',
75         'MOTE_L152RC',
76     ]
77
78     def generate(self):
79         """
80         Generates the project files
81         """
82         sources = []
83         sources += self.resources.c_sources
84         sources += self.resources.cpp_sources
85         sources += self.resources.s_sources
86
87         iar_files = IarFolder("", "", [])
88         for source in sources:
89             iar_files.insert_file(source)
90
91         ctx = {
92             'name': self.program_name,
93             'include_paths': self.resources.inc_dirs,
94             'linker_script': self.resources.linker_script,
95             'object_files': self.resources.objects,
96             'libraries': self.resources.libraries,
97             'symbols': self.get_symbols(),
98             'source_files': iar_files.__str__(),
99             'binary_files': self.resources.bin_files,
100         }
101         self.gen_file('iar_%s.ewp.tmpl' % self.target.lower(), ctx, '%s.ewp' % self.program_name)
102         self.gen_file('iar.eww.tmpl', ctx, '%s.eww' % self.program_name)
103         self.gen_file('iar_%s.ewd.tmpl' % self.target.lower(), ctx, '%s.ewd' % self.program_name)
104
105 class IarFolder():
106     """
107     This is a recursive folder object.
108     To present the folder structure in the IDE as it is presented on the disk.
109     This can be used for uvision as well if you replace the __str__ method.
110     Example:
111     files: ./main.cpp, ./apis/I2C.h, ./mbed/common/I2C.cpp
112     in the project this would look like:
113     main.cpp
114     common/I2C.cpp
115     input:
116     folder_level : folder path to current folder
117     folder_name : name of current folder
118     source_files : list of source_files (all must be in same directory)
119     """
120     def __init__(self, folder_level, folder_name, source_files):
121         self.folder_level = folder_level
122         self.folder_name = folder_name
123         self.source_files = source_files
124         self.sub_folders = {}
125
126     def __str__(self):
127         """
128         converts the folder structue to IAR project format.
129         """
130         group_start = ""
131         group_end = ""
132         if self.folder_name != "":
133             group_start = "<group>\n<name>%s</name>\n" %(self.folder_name)
134             group_end = "</group>\n"
135
136         str_content = group_start
137         #Add files in current folder
138         if self.source_files:
139             for src in self.source_files:
140                 str_content += "<file>\n<name>$PROJ_DIR$/%s</name>\n</file>\n" % src
141         #Add sub folders
142         if self.sub_folders:
143             for folder_name in self.sub_folders.iterkeys():
144                 str_content += self.sub_folders[folder_name].__str__()
145
146         str_content += group_end
147         return str_content
148
149     def insert_file(self, source_input):
150         """
151         Inserts a source file into the folder tree
152         """
153         if self.source_files:
154             #All source_files in a IarFolder must be in same directory.
155             dir_sources = IarFolder.get_directory(self.source_files[0])
156             #Check if sources are already at their deepest level.
157             if not self.folder_level == dir_sources:
158                 _reg_exp = r"^" + re.escape(self.folder_level) + r"[/\\]?([^/\\]+)"
159                 folder_name = re.match(_reg_exp, dir_sources).group(1)
160                 self.sub_folders[folder_name] = IarFolder(os.path.join(self.folder_level, folder_name), folder_name, self.source_files)
161                 self.source_files = []
162
163         dir_input = IarFolder.get_directory(source_input)
164         if dir_input == self.folder_level:
165             self.source_files.append(source_input)
166         else:
167             _reg_exp = r"^" + re.escape(self.folder_level) + r"[/\\]?([^/\\]+)"
168             folder_name = re.match(_reg_exp, dir_input).group(1)
169             if self.sub_folders.has_key(folder_name):
170                 self.sub_folders[folder_name].insert_file(source_input)
171             else:
172                 if self.folder_level == "":
173                     #Top level exception
174                     self.sub_folders[folder_name] = IarFolder(folder_name, folder_name, [source_input])
175                 else:
176                     self.sub_folders[folder_name] = IarFolder(os.path.join(self.folder_level, folder_name), folder_name, [source_input])
177
178     @staticmethod
179     def get_directory(file_path):
180         """
181         Returns the directory of the file
182         """
183         return os.path.dirname(file_path)