14
14
See the License for the specific language governing permissions and
15
15
limitations under the License.
16
16
"""
17
- from exporters import Exporter
17
+ from workspace_tools . export . exporters import Exporter
18
18
import re
19
-
19
+ import os
20
20
class IAREmbeddedWorkbench (Exporter ):
21
+ """
22
+ Exporter class for IAR Systems.
23
+ """
21
24
NAME = 'IAR'
22
25
TOOLCHAIN = 'IAR'
23
26
@@ -70,15 +73,18 @@ class IAREmbeddedWorkbench(Exporter):
70
73
]
71
74
72
75
def generate (self ):
76
+ """
77
+ Generates the project files
78
+ """
73
79
sources = []
74
80
sources += self .resources .c_sources
75
81
sources += self .resources .cpp_sources
76
82
sources += self .resources .s_sources
77
-
78
- iar_files = IAR_FOLDER ("" ,"" ,[])
83
+
84
+ iar_files = IarFolder ("" , "" , [])
79
85
for source in sources :
80
86
iar_files .insert_file (source )
81
-
87
+
82
88
ctx = {
83
89
'name' : self .program_name ,
84
90
'include_paths' : self .resources .inc_dirs ,
@@ -93,18 +99,31 @@ def generate(self):
93
99
self .gen_file ('iar.eww.tmpl' , ctx , '%s.eww' % self .program_name )
94
100
self .gen_file ('iar_%s.ewd.tmpl' % self .target .lower (), ctx , '%s.ewd' % self .program_name )
95
101
96
- class IAR_FOLDER :
97
- #input:
98
- #folder_level : folder path to current folder
99
- #folder_name : name of current folder
100
- #source_files : list of source_files (all must be in same directory)
102
+ class IarFolder ():
103
+ """
104
+ This is a recursive folder object.
105
+ To present the folder structure in the IDE as it is presented on the disk.
106
+ This can be used for uvision as well if you replace the __str__ method.
107
+ Example:
108
+ files: ./main.cpp, ./apis/I2C.h, ./mbed/common/I2C.cpp
109
+ in the project this would look like:
110
+ main.cpp
111
+ common/I2C.cpp
112
+ input:
113
+ folder_level : folder path to current folder
114
+ folder_name : name of current folder
115
+ source_files : list of source_files (all must be in same directory)
116
+ """
101
117
def __init__ (self , folder_level , folder_name , source_files ):
102
118
self .folder_level = folder_level
103
119
self .folder_name = folder_name
104
120
self .source_files = source_files
105
- self .sub_folders = {};
106
-
121
+ self .sub_folders = {}
122
+
107
123
def __str__ (self ):
124
+ """
125
+ converts the folder structue to IAR project format.
126
+ """
108
127
group_start = ""
109
128
group_end = ""
110
129
if self .folder_name != "" :
@@ -115,45 +134,47 @@ def __str__(self):
115
134
#Add files in current folder
116
135
if self .source_files :
117
136
for src in self .source_files :
118
- str_content += "<file>\n <name>$PROJ_DIR$\\ %s</name>\n </file>\n " % src
119
- ## Add sub folders
137
+ str_content += "<file>\n <name>$PROJ_DIR$/ %s</name>\n </file>\n " % src
138
+ #Add sub folders
120
139
if self .sub_folders :
121
140
for folder_name in self .sub_folders .iterkeys ():
122
141
str_content += self .sub_folders [folder_name ].__str__ ()
123
-
142
+
124
143
str_content += group_end
125
144
return str_content
126
-
127
-
145
+
128
146
def insert_file (self , source_input ):
147
+ """
148
+ Inserts a source file into the folder tree
149
+ """
129
150
if self .source_files :
130
- dir_sources = IAR_FOLDER .get_directory (self .source_files [0 ]) ##All source_files in a IAR_FOLDER must be in same directory.
131
- if not self .folder_level == dir_sources : ## Check if sources are already at their deepest level.
151
+ #All source_files in a IarFolder must be in same directory.
152
+ dir_sources = IarFolder .get_directory (self .source_files [0 ])
153
+ #Check if sources are already at their deepest level.
154
+ if not self .folder_level == dir_sources :
132
155
_reg_exp = r"^" + re .escape (self .folder_level ) + r"[/\\]?([^/\\]+)"
133
- folder_name = re .match ( _reg_exp , dir_sources ).group (1 )
134
- self .sub_folders [folder_name ] = IAR_FOLDER ( self .folder_level + " \\ " + folder_name , folder_name , self .source_files )
156
+ folder_name = re .match (_reg_exp , dir_sources ).group (1 )
157
+ self .sub_folders [folder_name ] = IarFolder ( os . path . join ( self .folder_level , folder_name ) , folder_name , self .source_files )
135
158
self .source_files = []
136
-
137
- dir_input = IAR_FOLDER .get_directory (source_input )
159
+
160
+ dir_input = IarFolder .get_directory (source_input )
138
161
if dir_input == self .folder_level :
139
162
self .source_files .append (source_input )
140
163
else :
141
164
_reg_exp = r"^" + re .escape (self .folder_level ) + r"[/\\]?([^/\\]+)"
142
- folder_name = re .match ( _reg_exp , dir_input ).group (1 )
165
+ folder_name = re .match (_reg_exp , dir_input ).group (1 )
143
166
if self .sub_folders .has_key (folder_name ):
144
167
self .sub_folders [folder_name ].insert_file (source_input )
145
168
else :
146
- if self .folder_level == "" : #Top level exception
147
- self .sub_folders [folder_name ] = IAR_FOLDER (folder_name , folder_name , [source_input ])
169
+ if self .folder_level == "" :
170
+ #Top level exception
171
+ self .sub_folders [folder_name ] = IarFolder (folder_name , folder_name , [source_input ])
148
172
else :
149
- self .sub_folders [folder_name ] = IAR_FOLDER ( self .folder_level + " \\ " + folder_name , folder_name , [source_input ])
173
+ self .sub_folders [folder_name ] = IarFolder ( os . path . join ( self .folder_level , folder_name ) , folder_name , [source_input ])
150
174
151
- @staticmethod
175
+ @staticmethod
152
176
def get_directory (file_path ):
153
- dir_Match = re .match ( r'(.*)[/\\][^/\\]+' , file_path )
154
- if dir_Match is not None :
155
- return dir_Match .group (1 )
156
- else :
157
- return ""
158
-
159
-
177
+ """
178
+ Returns the directory of the file
179
+ """
180
+ return os .path .dirname (file_path )
0 commit comments