Skip to content

Commit 9d91566

Browse files
committed
Using Popen for uvision and unifying the structure of the build function
across exporters
1 parent f044786 commit 9d91566

File tree

3 files changed

+73
-41
lines changed

3 files changed

+73
-41
lines changed

tools/export/iar/__init__.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,11 @@ def generate(self):
122122
self.gen_file(self.get_ewp_template(), ctx, self.project_name + ".ewp")
123123

124124
@staticmethod
125-
def build(project_name, cleanup=True):
125+
def build(project_name, log_name="build_log.txt", cleanup=True):
126126
""" Build IAR project """
127127
# > IarBuild [project_path] -build [project_name]
128-
129128
proj_file = project_name + ".ewp"
130-
cmd = ["IarBuild.exe", proj_file, '-build', project_name]
129+
cmd = ["IarBuild", proj_file, '-build', project_name]
131130

132131
# IAR does not support a '0' option to automatically use all
133132
# available CPUs, so we use Python's multiprocessing library
@@ -139,23 +138,38 @@ def build(project_name, cleanup=True):
139138
if jobs:
140139
cmd += ['-parallel', str(jobs)]
141140

141+
# Build the project
142142
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
143-
num_errors = 0
144-
#Parse the output for printing and errors
145-
for line in p.stdout.readlines():
146-
sys.stdout.write(line)
147-
error_re = '\s*Total number of errors:\s*(\d+)\s*'
148-
m = re.match(error_re, line)
149-
if m is not None:
150-
num_errors = int(m.group(1))
143+
out, err = p.communicate()
144+
ret_code = p.returncode
145+
146+
out_string = "=" * 10 + "STDOUT" + "=" * 10 + "\n"
147+
out_string += out
148+
out_string += "=" * 10 + "STDERR" + "=" * 10 + "\n"
149+
out_string += err
150+
151+
if ret_code == 0:
152+
out_string += "SUCCESS"
153+
else:
154+
out_string += "FAILURE"
155+
156+
print out_string
157+
158+
if log_name:
159+
# Write the output to the log file
160+
with open(log_name, 'w+') as f:
161+
f.write(out_string)
151162

163+
# Cleanup the exported and built files
152164
if cleanup:
153165
os.remove(project_name + ".ewp")
154166
os.remove(project_name + ".ewd")
155167
os.remove(project_name + ".eww")
156-
shutil.rmtree('.build')
168+
if exists('.build'):
169+
shutil.rmtree('.build')
157170

158-
if num_errors !=0:
171+
if ret_code !=0:
159172
# Seems like something went wrong.
160173
return -1
161-
return 0
174+
else:
175+
return 0

tools/export/makefile/__init__.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,32 +110,41 @@ def build(project_name, log_name="build_log.txt", cleanup=True):
110110
""" Build Make project """
111111
# > Make -j
112112
cmd = ["make", "-j"]
113+
114+
# Build the project
113115
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
114-
ret = p.communicate()
115-
out, err = ret[0], ret[1]
116+
out, err = p.communicate()
116117
ret_code = p.returncode
117-
with open(log_name, 'w+') as f:
118-
f.write("=" * 10 + "OUT" + "=" * 10 + "\n")
119-
f.write(out)
120-
f.write("=" * 10 + "ERR" + "=" * 10 + "\n")
121-
f.write(err)
122-
if ret_code == 0:
123-
f.write("SUCCESS")
124-
else:
125-
f.write("FAILURE")
126-
with open(log_name, 'r') as f:
127-
print "\n".join(f.readlines())
128-
sys.stdout.flush()
129118

119+
out_string = "=" * 10 + "STDOUT" + "=" * 10 + "\n"
120+
out_string += out
121+
out_string += "=" * 10 + "STDERR" + "=" * 10 + "\n"
122+
out_string += err
123+
124+
if ret_code == 0:
125+
out_string += "SUCCESS"
126+
else:
127+
out_string += "FAILURE"
128+
129+
print out_string
130+
131+
if log_name:
132+
# Write the output to the log file
133+
with open(log_name, 'w+') as f:
134+
f.write(out_string)
135+
136+
# Cleanup the exported and built files
130137
if cleanup:
131138
remove("Makefile")
132139
remove(log_name)
133140
if exists('.build'):
134141
shutil.rmtree('.build')
142+
135143
if ret_code != 0:
136144
# Seems like something went wrong.
137145
return -1
138-
return 0
146+
else:
147+
return 0
139148

140149

141150
class GccArm(Makefile):

tools/export/uvision/__init__.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import copy
55
from collections import namedtuple
66
import shutil
7-
import subprocess
7+
from subprocess import Popen, PIPE
88
import re
99

1010
from tools.arm_pack_manager import Cache
@@ -208,21 +208,30 @@ def generate(self):
208208
@staticmethod
209209
def build(project_name, log_name='build_log.txt', cleanup=True):
210210
""" Build Uvision project """
211-
# > UV4.exe -r -j0 -o [log_name] [project_name].uvprojx
212-
success = 0
213-
warn = 1
214-
cmd = ["UV4.exe", '-r', '-j0', '-o', log_name, project_name+".uvprojx"]
215-
ret_code = subprocess.call(cmd)
216-
with open(log_name, 'r') as build_log:
217-
print build_log.read()
211+
# > UV4 -r -j0 -o [log_name] [project_name].uvprojx
212+
proj_file = project_name + ".uvprojx"
213+
cmd = ['UV4', '-r', '-j0', '-o', log_name, proj_file]
214+
215+
# Build the project
216+
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
217+
out, err = p.communicate()
218+
ret_code = p.returncode
219+
220+
# Print the log file to stdout
221+
with open(log_name, 'r') as f:
222+
print f.read()
223+
224+
# Cleanup the exported and built files
218225
if cleanup:
219226
os.remove(log_name)
220227
os.remove(project_name+".uvprojx")
221228
os.remove(project_name+".uvoptx")
222-
shutil.rmtree(".build")
223-
229+
if exists('.build'):
230+
shutil.rmtree(".build")
224231

225-
if ret_code != success and ret_code != warn:
232+
# Returns 0 upon success, 1 upon a warning, and neither upon an error
233+
if ret_code != 0 and ret_code != 1:
226234
# Seems like something went wrong.
227235
return -1
228-
return 0
236+
else:
237+
return 0

0 commit comments

Comments
 (0)