mirror of https://github.com/ARMmbed/mbed-os.git
				
				
				
			Merge pull request #3200 from bridadan/exporter_build_subprocess_change
Using Popen for uVision and unifying the structure of the build functionpull/3214/head^2
						commit
						03b8ae1811
					
				| 
						 | 
				
			
			@ -122,12 +122,11 @@ class IAR(Exporter):
 | 
			
		|||
        self.gen_file(self.get_ewp_template(), ctx, self.project_name + ".ewp")
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def build(project_name, cleanup=True):
 | 
			
		||||
    def build(project_name, log_name="build_log.txt", cleanup=True):
 | 
			
		||||
        """ Build IAR project """
 | 
			
		||||
        # > IarBuild [project_path] -build [project_name]
 | 
			
		||||
 | 
			
		||||
        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
 | 
			
		||||
        # available CPUs, so we use Python's multiprocessing library
 | 
			
		||||
| 
						 | 
				
			
			@ -139,23 +138,38 @@ class IAR(Exporter):
 | 
			
		|||
        if jobs:
 | 
			
		||||
            cmd += ['-parallel', str(jobs)]
 | 
			
		||||
 | 
			
		||||
        # Build the project
 | 
			
		||||
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
 | 
			
		||||
        num_errors = 0
 | 
			
		||||
        #Parse the output for printing and errors
 | 
			
		||||
        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, 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:
 | 
			
		||||
            os.remove(project_name + ".ewp")
 | 
			
		||||
            os.remove(project_name + ".ewd")
 | 
			
		||||
            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.
 | 
			
		||||
            return -1
 | 
			
		||||
        return 0
 | 
			
		||||
        else:
 | 
			
		||||
            return 0
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -114,32 +114,41 @@ class Makefile(Exporter):
 | 
			
		|||
        """ Build Make project """
 | 
			
		||||
        # > 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:
 | 
			
		||||
            remove("Makefile")
 | 
			
		||||
            remove(log_name)
 | 
			
		||||
            if exists('.build'):
 | 
			
		||||
                shutil.rmtree('.build')
 | 
			
		||||
 | 
			
		||||
        if ret_code != 0:
 | 
			
		||||
            # Seems like something went wrong.
 | 
			
		||||
            return -1
 | 
			
		||||
        return 0
 | 
			
		||||
        else:
 | 
			
		||||
            return 0
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class GccArm(Makefile):
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -4,7 +4,7 @@ import ntpath
 | 
			
		|||
import copy
 | 
			
		||||
from collections import namedtuple
 | 
			
		||||
import shutil
 | 
			
		||||
import subprocess
 | 
			
		||||
from subprocess import Popen, PIPE
 | 
			
		||||
import re
 | 
			
		||||
 | 
			
		||||
from tools.arm_pack_manager import Cache
 | 
			
		||||
| 
						 | 
				
			
			@ -208,21 +208,30 @@ class Uvision(Exporter):
 | 
			
		|||
    @staticmethod
 | 
			
		||||
    def build(project_name, log_name='build_log.txt', cleanup=True):
 | 
			
		||||
        """ Build Uvision project """
 | 
			
		||||
        # > UV4.exe -r -j0 -o [log_name] [project_name].uvprojx
 | 
			
		||||
        success = 0
 | 
			
		||||
        warn  = 1
 | 
			
		||||
        cmd = ["UV4.exe", '-r', '-j0', '-o', log_name, project_name+".uvprojx"]
 | 
			
		||||
        ret_code = subprocess.call(cmd)
 | 
			
		||||
        with open(log_name, 'r') as build_log:
 | 
			
		||||
            print build_log.read()
 | 
			
		||||
        # > UV4 -r -j0 -o [log_name] [project_name].uvprojx
 | 
			
		||||
        proj_file = project_name + ".uvprojx"
 | 
			
		||||
        cmd = ['UV4', '-r', '-j0', '-o', log_name, proj_file]
 | 
			
		||||
 | 
			
		||||
        # Build the project
 | 
			
		||||
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
 | 
			
		||||
        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:
 | 
			
		||||
            os.remove(log_name)
 | 
			
		||||
            os.remove(project_name+".uvprojx")
 | 
			
		||||
            os.remove(project_name+".uvoptx")
 | 
			
		||||
            shutil.rmtree(".build")
 | 
			
		||||
            if exists('.build'):
 | 
			
		||||
                shutil.rmtree(".build")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        if ret_code != success and ret_code != warn:
 | 
			
		||||
        # Returns 0 upon success, 1 upon a warning, and neither upon an error
 | 
			
		||||
        if ret_code != 0 and ret_code != 1:
 | 
			
		||||
            # Seems like something went wrong.
 | 
			
		||||
            return -1
 | 
			
		||||
        return 0
 | 
			
		||||
        else:
 | 
			
		||||
            return 0
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue