Using Popen for uvision and unifying the structure of the build function

across exporters
pull/3200/head
Brian Daniels 2016-11-03 13:13:50 -05:00
parent f044786f22
commit 9d91566927
3 changed files with 75 additions and 43 deletions

View File

@ -122,12 +122,11 @@ class IAR(Exporter):
self.gen_file(self.get_ewp_template(), ctx, self.project_name + ".ewp") self.gen_file(self.get_ewp_template(), ctx, self.project_name + ".ewp")
@staticmethod @staticmethod
def build(project_name, cleanup=True): def build(project_name, log_name="build_log.txt", cleanup=True):
""" Build IAR project """ """ Build IAR project """
# > IarBuild [project_path] -build [project_name] # > IarBuild [project_path] -build [project_name]
proj_file = project_name + ".ewp" proj_file = project_name + ".ewp"
cmd = ["IarBuild.exe", proj_file, '-build', project_name] cmd = ["IarBuild", proj_file, '-build', project_name]
# IAR does not support a '0' option to automatically use all # IAR does not support a '0' option to automatically use all
# available CPUs, so we use Python's multiprocessing library # available CPUs, so we use Python's multiprocessing library
@ -139,23 +138,38 @@ class IAR(Exporter):
if jobs: if jobs:
cmd += ['-parallel', str(jobs)] cmd += ['-parallel', str(jobs)]
# Build the project
p = Popen(cmd, stdout=PIPE, stderr=PIPE) p = Popen(cmd, stdout=PIPE, stderr=PIPE)
num_errors = 0 out, err = p.communicate()
#Parse the output for printing and errors ret_code = p.returncode
for line in p.stdout.readlines():
sys.stdout.write(line)
error_re = '\s*Total number of errors:\s*(\d+)\s*'
m = re.match(error_re, line)
if m is not None:
num_errors = int(m.group(1))
out_string = "=" * 10 + "STDOUT" + "=" * 10 + "\n"
out_string += out
out_string += "=" * 10 + "STDERR" + "=" * 10 + "\n"
out_string += err
if ret_code == 0:
out_string += "SUCCESS"
else:
out_string += "FAILURE"
print out_string
if log_name:
# Write the output to the log file
with open(log_name, 'w+') as f:
f.write(out_string)
# Cleanup the exported and built files
if cleanup: if cleanup:
os.remove(project_name + ".ewp") os.remove(project_name + ".ewp")
os.remove(project_name + ".ewd") os.remove(project_name + ".ewd")
os.remove(project_name + ".eww") os.remove(project_name + ".eww")
shutil.rmtree('.build') if exists('.build'):
shutil.rmtree('.build')
if num_errors !=0: if ret_code !=0:
# Seems like something went wrong. # Seems like something went wrong.
return -1 return -1
return 0 else:
return 0

View File

@ -110,32 +110,41 @@ class Makefile(Exporter):
""" Build Make project """ """ Build Make project """
# > Make -j # > Make -j
cmd = ["make", "-j"] cmd = ["make", "-j"]
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
ret = p.communicate()
out, err = ret[0], ret[1]
ret_code = p.returncode
with open(log_name, 'w+') as f:
f.write("=" * 10 + "OUT" + "=" * 10 + "\n")
f.write(out)
f.write("=" * 10 + "ERR" + "=" * 10 + "\n")
f.write(err)
if ret_code == 0:
f.write("SUCCESS")
else:
f.write("FAILURE")
with open(log_name, 'r') as f:
print "\n".join(f.readlines())
sys.stdout.flush()
# Build the project
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
out, err = p.communicate()
ret_code = p.returncode
out_string = "=" * 10 + "STDOUT" + "=" * 10 + "\n"
out_string += out
out_string += "=" * 10 + "STDERR" + "=" * 10 + "\n"
out_string += err
if ret_code == 0:
out_string += "SUCCESS"
else:
out_string += "FAILURE"
print out_string
if log_name:
# Write the output to the log file
with open(log_name, 'w+') as f:
f.write(out_string)
# Cleanup the exported and built files
if cleanup: if cleanup:
remove("Makefile") remove("Makefile")
remove(log_name) remove(log_name)
if exists('.build'): if exists('.build'):
shutil.rmtree('.build') shutil.rmtree('.build')
if ret_code != 0: if ret_code != 0:
# Seems like something went wrong. # Seems like something went wrong.
return -1 return -1
return 0 else:
return 0
class GccArm(Makefile): class GccArm(Makefile):

View File

@ -4,7 +4,7 @@ import ntpath
import copy import copy
from collections import namedtuple from collections import namedtuple
import shutil import shutil
import subprocess from subprocess import Popen, PIPE
import re import re
from tools.arm_pack_manager import Cache from tools.arm_pack_manager import Cache
@ -208,21 +208,30 @@ class Uvision(Exporter):
@staticmethod @staticmethod
def build(project_name, log_name='build_log.txt', cleanup=True): def build(project_name, log_name='build_log.txt', cleanup=True):
""" Build Uvision project """ """ Build Uvision project """
# > UV4.exe -r -j0 -o [log_name] [project_name].uvprojx # > UV4 -r -j0 -o [log_name] [project_name].uvprojx
success = 0 proj_file = project_name + ".uvprojx"
warn = 1 cmd = ['UV4', '-r', '-j0', '-o', log_name, proj_file]
cmd = ["UV4.exe", '-r', '-j0', '-o', log_name, project_name+".uvprojx"]
ret_code = subprocess.call(cmd) # Build the project
with open(log_name, 'r') as build_log: p = Popen(cmd, stdout=PIPE, stderr=PIPE)
print build_log.read() out, err = p.communicate()
ret_code = p.returncode
# Print the log file to stdout
with open(log_name, 'r') as f:
print f.read()
# Cleanup the exported and built files
if cleanup: if cleanup:
os.remove(log_name) os.remove(log_name)
os.remove(project_name+".uvprojx") os.remove(project_name+".uvprojx")
os.remove(project_name+".uvoptx") os.remove(project_name+".uvoptx")
shutil.rmtree(".build") if exists('.build'):
shutil.rmtree(".build")
# Returns 0 upon success, 1 upon a warning, and neither upon an error
if ret_code != success and ret_code != warn: if ret_code != 0 and ret_code != 1:
# Seems like something went wrong. # Seems like something went wrong.
return -1 return -1
return 0 else:
return 0