User | Revision | Line number | New contents of line |
nexpaq |
0:6c56fb4bc5f0
|
1
|
"""
|
nexpaq |
0:6c56fb4bc5f0
|
2
|
mbed SDK
|
nexpaq |
0:6c56fb4bc5f0
|
3
|
Copyright (c) 2011-2016 ARM Limited
|
nexpaq |
0:6c56fb4bc5f0
|
4
|
|
nexpaq |
0:6c56fb4bc5f0
|
5
|
Licensed under the Apache License, Version 2.0 (the "License");
|
nexpaq |
0:6c56fb4bc5f0
|
6
|
you may not use this file except in compliance with the License.
|
nexpaq |
0:6c56fb4bc5f0
|
7
|
You may obtain a copy of the License at
|
nexpaq |
0:6c56fb4bc5f0
|
8
|
|
nexpaq |
0:6c56fb4bc5f0
|
9
|
http://www.apache.org/licenses/LICENSE-2.0
|
nexpaq |
0:6c56fb4bc5f0
|
10
|
|
nexpaq |
0:6c56fb4bc5f0
|
11
|
Unless required by applicable law or agreed to in writing, software
|
nexpaq |
0:6c56fb4bc5f0
|
12
|
distributed under the License is distributed on an "AS IS" BASIS,
|
nexpaq |
0:6c56fb4bc5f0
|
13
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
nexpaq |
0:6c56fb4bc5f0
|
14
|
See the License for the specific language governing permissions and
|
nexpaq |
0:6c56fb4bc5f0
|
15
|
limitations under the License.
|
nexpaq |
0:6c56fb4bc5f0
|
16
|
"""
|
nexpaq |
0:6c56fb4bc5f0
|
17
|
import re
|
nexpaq |
0:6c56fb4bc5f0
|
18
|
import os
|
nexpaq |
0:6c56fb4bc5f0
|
19
|
from project_generator_definitions.definitions import ProGenDef
|
nexpaq |
0:6c56fb4bc5f0
|
20
|
|
nexpaq |
0:6c56fb4bc5f0
|
21
|
from tools.export.exporters import Exporter, ExporterTargetsProperty
|
nexpaq |
0:6c56fb4bc5f0
|
22
|
from tools.targets import TARGET_MAP, TARGET_NAMES
|
nexpaq |
0:6c56fb4bc5f0
|
23
|
|
nexpaq |
0:6c56fb4bc5f0
|
24
|
# If you wish to add a new target, add it to project_generator_definitions, and then
|
nexpaq |
0:6c56fb4bc5f0
|
25
|
# define progen_target name in the target class (`` self.progen_target = 'my_target_name' ``)
|
nexpaq |
0:6c56fb4bc5f0
|
26
|
class IAREmbeddedWorkbench(Exporter):
|
nexpaq |
0:6c56fb4bc5f0
|
27
|
"""
|
nexpaq |
0:6c56fb4bc5f0
|
28
|
Exporter class for IAR Systems. This class uses project generator.
|
nexpaq |
0:6c56fb4bc5f0
|
29
|
"""
|
nexpaq |
0:6c56fb4bc5f0
|
30
|
# These 2 are currently for exporters backward compatiblity
|
nexpaq |
0:6c56fb4bc5f0
|
31
|
NAME = 'iar_arm'
|
nexpaq |
0:6c56fb4bc5f0
|
32
|
TOOLCHAIN = 'IAR'
|
nexpaq |
0:6c56fb4bc5f0
|
33
|
# PROGEN_ACTIVE contains information for exporter scripts that this is using progen
|
nexpaq |
0:6c56fb4bc5f0
|
34
|
PROGEN_ACTIVE = True
|
nexpaq |
0:6c56fb4bc5f0
|
35
|
|
nexpaq |
0:6c56fb4bc5f0
|
36
|
MBED_CONFIG_HEADER_SUPPORTED = True
|
nexpaq |
0:6c56fb4bc5f0
|
37
|
|
nexpaq |
0:6c56fb4bc5f0
|
38
|
@ExporterTargetsProperty
|
nexpaq |
0:6c56fb4bc5f0
|
39
|
def TARGETS(cls):
|
nexpaq |
0:6c56fb4bc5f0
|
40
|
if not hasattr(cls, "_targets_supported"):
|
nexpaq |
0:6c56fb4bc5f0
|
41
|
cls._targets_supported = []
|
nexpaq |
0:6c56fb4bc5f0
|
42
|
progendef = ProGenDef('iar')
|
nexpaq |
0:6c56fb4bc5f0
|
43
|
for target in TARGET_NAMES:
|
nexpaq |
0:6c56fb4bc5f0
|
44
|
try:
|
nexpaq |
0:6c56fb4bc5f0
|
45
|
if (progendef.is_supported(str(TARGET_MAP[target])) or
|
nexpaq |
0:6c56fb4bc5f0
|
46
|
progendef.is_supported(TARGET_MAP[target].progen['target'])):
|
nexpaq |
0:6c56fb4bc5f0
|
47
|
cls._targets_supported.append(target)
|
nexpaq |
0:6c56fb4bc5f0
|
48
|
except AttributeError:
|
nexpaq |
0:6c56fb4bc5f0
|
49
|
# target is not supported yet
|
nexpaq |
0:6c56fb4bc5f0
|
50
|
continue
|
nexpaq |
0:6c56fb4bc5f0
|
51
|
return cls._targets_supported
|
nexpaq |
0:6c56fb4bc5f0
|
52
|
|
nexpaq |
0:6c56fb4bc5f0
|
53
|
def generate(self):
|
nexpaq |
0:6c56fb4bc5f0
|
54
|
""" Generates the project files """
|
nexpaq |
0:6c56fb4bc5f0
|
55
|
project_data = self.progen_get_project_data()
|
nexpaq |
0:6c56fb4bc5f0
|
56
|
try:
|
nexpaq |
0:6c56fb4bc5f0
|
57
|
if TARGET_MAP[self.target].progen['iar']['template']:
|
nexpaq |
0:6c56fb4bc5f0
|
58
|
project_data['template']=TARGET_MAP[self.target].progen['iar']['template']
|
nexpaq |
0:6c56fb4bc5f0
|
59
|
except KeyError:
|
nexpaq |
0:6c56fb4bc5f0
|
60
|
# use default template
|
nexpaq |
0:6c56fb4bc5f0
|
61
|
# by the mbed projects
|
nexpaq |
0:6c56fb4bc5f0
|
62
|
project_data['template']=[os.path.join(os.path.dirname(__file__), 'iar_template.ewp.tmpl')]
|
nexpaq |
0:6c56fb4bc5f0
|
63
|
|
nexpaq |
0:6c56fb4bc5f0
|
64
|
project_data['misc'] = self.flags
|
nexpaq |
0:6c56fb4bc5f0
|
65
|
# VLA is enabled via template IccAllowVLA
|
nexpaq |
0:6c56fb4bc5f0
|
66
|
project_data['misc']['c_flags'].remove("--vla")
|
nexpaq |
0:6c56fb4bc5f0
|
67
|
project_data['misc']['asm_flags'] = list(set(project_data['misc']['asm_flags']))
|
nexpaq |
0:6c56fb4bc5f0
|
68
|
project_data['build_dir'] = os.path.join(project_data['build_dir'], 'iar_arm')
|
nexpaq |
0:6c56fb4bc5f0
|
69
|
self.progen_gen_file(project_data)
|
nexpaq |
0:6c56fb4bc5f0
|
70
|
|
nexpaq |
0:6c56fb4bc5f0
|
71
|
# Currently not used, we should reuse folder_name to create virtual folders
|
nexpaq |
0:6c56fb4bc5f0
|
72
|
class IarFolder():
|
nexpaq |
0:6c56fb4bc5f0
|
73
|
"""
|
nexpaq |
0:6c56fb4bc5f0
|
74
|
This is a recursive folder object.
|
nexpaq |
0:6c56fb4bc5f0
|
75
|
To present the folder structure in the IDE as it is presented on the disk.
|
nexpaq |
0:6c56fb4bc5f0
|
76
|
This can be used for uvision as well if you replace the __str__ method.
|
nexpaq |
0:6c56fb4bc5f0
|
77
|
Example:
|
nexpaq |
0:6c56fb4bc5f0
|
78
|
files: ./main.cpp, ./apis/I2C.h, ./mbed/common/I2C.cpp
|
nexpaq |
0:6c56fb4bc5f0
|
79
|
in the project this would look like:
|
nexpaq |
0:6c56fb4bc5f0
|
80
|
main.cpp
|
nexpaq |
0:6c56fb4bc5f0
|
81
|
common/I2C.cpp
|
nexpaq |
0:6c56fb4bc5f0
|
82
|
input:
|
nexpaq |
0:6c56fb4bc5f0
|
83
|
folder_level : folder path to current folder
|
nexpaq |
0:6c56fb4bc5f0
|
84
|
folder_name : name of current folder
|
nexpaq |
0:6c56fb4bc5f0
|
85
|
source_files : list of source_files (all must be in same directory)
|
nexpaq |
0:6c56fb4bc5f0
|
86
|
"""
|
nexpaq |
0:6c56fb4bc5f0
|
87
|
def __init__(self, folder_level, folder_name, source_files):
|
nexpaq |
0:6c56fb4bc5f0
|
88
|
self.folder_level = folder_level
|
nexpaq |
0:6c56fb4bc5f0
|
89
|
self.folder_name = folder_name
|
nexpaq |
0:6c56fb4bc5f0
|
90
|
self.source_files = source_files
|
nexpaq |
0:6c56fb4bc5f0
|
91
|
self.sub_folders = {}
|
nexpaq |
0:6c56fb4bc5f0
|
92
|
|
nexpaq |
0:6c56fb4bc5f0
|
93
|
def __str__(self):
|
nexpaq |
0:6c56fb4bc5f0
|
94
|
"""
|
nexpaq |
0:6c56fb4bc5f0
|
95
|
converts the folder structue to IAR project format.
|
nexpaq |
0:6c56fb4bc5f0
|
96
|
"""
|
nexpaq |
0:6c56fb4bc5f0
|
97
|
group_start = ""
|
nexpaq |
0:6c56fb4bc5f0
|
98
|
group_end = ""
|
nexpaq |
0:6c56fb4bc5f0
|
99
|
if self.folder_name != "":
|
nexpaq |
0:6c56fb4bc5f0
|
100
|
group_start = "<group>\n<name>%s</name>\n" %(self.folder_name)
|
nexpaq |
0:6c56fb4bc5f0
|
101
|
group_end = "</group>\n"
|
nexpaq |
0:6c56fb4bc5f0
|
102
|
|
nexpaq |
0:6c56fb4bc5f0
|
103
|
str_content = group_start
|
nexpaq |
0:6c56fb4bc5f0
|
104
|
#Add files in current folder
|
nexpaq |
0:6c56fb4bc5f0
|
105
|
if self.source_files:
|
nexpaq |
0:6c56fb4bc5f0
|
106
|
for src in self.source_files:
|
nexpaq |
0:6c56fb4bc5f0
|
107
|
str_content += "<file>\n<name>$PROJ_DIR$/%s</name>\n</file>\n" % src
|
nexpaq |
0:6c56fb4bc5f0
|
108
|
#Add sub folders
|
nexpaq |
0:6c56fb4bc5f0
|
109
|
if self.sub_folders:
|
nexpaq |
0:6c56fb4bc5f0
|
110
|
for folder_name in self.sub_folders.iterkeys():
|
nexpaq |
0:6c56fb4bc5f0
|
111
|
str_content += self.sub_folders[folder_name].__str__()
|
nexpaq |
0:6c56fb4bc5f0
|
112
|
|
nexpaq |
0:6c56fb4bc5f0
|
113
|
str_content += group_end
|
nexpaq |
0:6c56fb4bc5f0
|
114
|
return str_content
|
nexpaq |
0:6c56fb4bc5f0
|
115
|
|
nexpaq |
0:6c56fb4bc5f0
|
116
|
def insert_file(self, source_input):
|
nexpaq |
0:6c56fb4bc5f0
|
117
|
"""
|
nexpaq |
0:6c56fb4bc5f0
|
118
|
Inserts a source file into the folder tree
|
nexpaq |
0:6c56fb4bc5f0
|
119
|
"""
|
nexpaq |
0:6c56fb4bc5f0
|
120
|
if self.source_files:
|
nexpaq |
0:6c56fb4bc5f0
|
121
|
#All source_files in a IarFolder must be in same directory.
|
nexpaq |
0:6c56fb4bc5f0
|
122
|
dir_sources = IarFolder.get_directory(self.source_files[0])
|
nexpaq |
0:6c56fb4bc5f0
|
123
|
#Check if sources are already at their deepest level.
|
nexpaq |
0:6c56fb4bc5f0
|
124
|
if not self.folder_level == dir_sources:
|
nexpaq |
0:6c56fb4bc5f0
|
125
|
_reg_exp = r"^" + re.escape(self.folder_level) + r"[/\\]?([^/\\]+)"
|
nexpaq |
0:6c56fb4bc5f0
|
126
|
folder_name = re.match(_reg_exp, dir_sources).group(1)
|
nexpaq |
0:6c56fb4bc5f0
|
127
|
self.sub_folders[folder_name] = IarFolder(os.path.join(self.folder_level, folder_name), folder_name, self.source_files)
|
nexpaq |
0:6c56fb4bc5f0
|
128
|
self.source_files = []
|
nexpaq |
0:6c56fb4bc5f0
|
129
|
|
nexpaq |
0:6c56fb4bc5f0
|
130
|
dir_input = IarFolder.get_directory(source_input)
|
nexpaq |
0:6c56fb4bc5f0
|
131
|
if dir_input == self.folder_level:
|
nexpaq |
0:6c56fb4bc5f0
|
132
|
self.source_files.append(source_input)
|
nexpaq |
0:6c56fb4bc5f0
|
133
|
else:
|
nexpaq |
0:6c56fb4bc5f0
|
134
|
_reg_exp = r"^" + re.escape(self.folder_level) + r"[/\\]?([^/\\]+)"
|
nexpaq |
0:6c56fb4bc5f0
|
135
|
folder_name = re.match(_reg_exp, dir_input).group(1)
|
nexpaq |
0:6c56fb4bc5f0
|
136
|
if self.sub_folders.has_key(folder_name):
|
nexpaq |
0:6c56fb4bc5f0
|
137
|
self.sub_folders[folder_name].insert_file(source_input)
|
nexpaq |
0:6c56fb4bc5f0
|
138
|
else:
|
nexpaq |
0:6c56fb4bc5f0
|
139
|
if self.folder_level == "":
|
nexpaq |
0:6c56fb4bc5f0
|
140
|
#Top level exception
|
nexpaq |
0:6c56fb4bc5f0
|
141
|
self.sub_folders[folder_name] = IarFolder(folder_name, folder_name, [source_input])
|
nexpaq |
0:6c56fb4bc5f0
|
142
|
else:
|
nexpaq |
0:6c56fb4bc5f0
|
143
|
self.sub_folders[folder_name] = IarFolder(os.path.join(self.folder_level, folder_name), folder_name, [source_input])
|
nexpaq |
0:6c56fb4bc5f0
|
144
|
|
nexpaq |
0:6c56fb4bc5f0
|
145
|
@staticmethod
|
nexpaq |
0:6c56fb4bc5f0
|
146
|
def get_directory(file_path):
|
nexpaq |
0:6c56fb4bc5f0
|
147
|
"""
|
nexpaq |
0:6c56fb4bc5f0
|
148
|
Returns the directory of the file
|
nexpaq |
0:6c56fb4bc5f0
|
149
|
"""
|
nexpaq |
0:6c56fb4bc5f0
|
150
|
return os.path.dirname(file_path)
|