Skip to content

Commit 03b8ae1

Browse files
authored
Merge pull request #3200 from bridadan/exporter_build_subprocess_change
Using Popen for uVision and unifying the structure of the build function
2 parents 13676f0 + 9d91566 commit 03b8ae1

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
@@ -114,32 +114,41 @@ def build(project_name, log_name="build_log.txt", cleanup=True):
114114
""" Build Make project """
115115
# > Make -j
116116
cmd = ["make", "-j"]
117+
118+
# Build the project
117119
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
118-
ret = p.communicate()
119-
out, err = ret[0], ret[1]
120+
out, err = p.communicate()
120121
ret_code = p.returncode
121-
with open(log_name, 'w+') as f:
122-
f.write("=" * 10 + "OUT" + "=" * 10 + "\n")
123-
f.write(out)
124-
f.write("=" * 10 + "ERR" + "=" * 10 + "\n")
125-
f.write(err)
126-
if ret_code == 0:
127-
f.write("SUCCESS")
128-
else:
129-
f.write("FAILURE")
130-
with open(log_name, 'r') as f:
131-
print "\n".join(f.readlines())
132-
sys.stdout.flush()
133122

123+
out_string = "=" * 10 + "STDOUT" + "=" * 10 + "\n"
124+
out_string += out
125+
out_string += "=" * 10 + "STDERR" + "=" * 10 + "\n"
126+
out_string += err
127+
128+
if ret_code == 0:
129+
out_string += "SUCCESS"
130+
else:
131+
out_string += "FAILURE"
132+
133+
print out_string
134+
135+
if log_name:
136+
# Write the output to the log file
137+
with open(log_name, 'w+') as f:
138+
f.write(out_string)
139+
140+
# Cleanup the exported and built files
134141
if cleanup:
135142
remove("Makefile")
136143
remove(log_name)
137144
if exists('.build'):
138145
shutil.rmtree('.build')
146+
139147
if ret_code != 0:
140148
# Seems like something went wrong.
141149
return -1
142-
return 0
150+
else:
151+
return 0
143152

144153

145154
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)