diff --git a/tools/export/cces/__init__.py b/tools/export/cces/__init__.py index 2d90fa93e8..359042fcb8 100644 --- a/tools/export/cces/__init__.py +++ b/tools/export/cces/__init__.py @@ -408,6 +408,12 @@ class CCES(Exporter): print("CCES files generated.") + + @staticmethod + def clean(_): + os.remove('cces.json') + os.remove('README.md') + @staticmethod def build(project_name, log_name='build_log.txt', cleanup=True): """ @@ -436,6 +442,7 @@ class CCES(Exporter): # cleanup workspace if os.path.exists(workspace): shutil.rmtree(workspace, True) + CCES.clean(project_name) # check return code for failure if ret_code != 0: diff --git a/tools/export/cmake/__init__.py b/tools/export/cmake/__init__.py index 9c1638e9d1..a83f773abf 100644 --- a/tools/export/cmake/__init__.py +++ b/tools/export/cmake/__init__.py @@ -112,6 +112,15 @@ class CMake(Exporter): except TemplateNotFound: pass + @staticmethod + def clean(_): + remove("CMakeLists.txt") + # legacy .build directory cleaned if exists + if exists('.build'): + shutil.rmtree('.build') + if exists('BUILD'): + shutil.rmtree('BUILD') + @staticmethod def build(project_name, log_name="build_log.txt", cleanup=True): """ Build Make project """ @@ -162,13 +171,8 @@ class CMake(Exporter): # Cleanup the exported and built files if cleanup: - remove("CMakeLists.txt") remove(log_name) - # legacy .build directory cleaned if exists - if exists('.build'): - shutil.rmtree('.build') - if exists('BUILD'): - shutil.rmtree('BUILD') + CMake.clean(project_name) if ret_code != 0: # Seems like something went wrong. diff --git a/tools/export/exporters.py b/tools/export/exporters.py index c4d9e198ee..b9dc8c7591 100644 --- a/tools/export/exporters.py +++ b/tools/export/exporters.py @@ -50,6 +50,7 @@ class Exporter(object): NAME = None TARGETS = set() TOOLCHAIN = None + CLEAN_FILES = ("GettingStarted.html",) def __init__(self, target, export_dir, project_name, toolchain, @@ -217,12 +218,28 @@ class Exporter(object): Returns -1 on failure and 0 on success """ - raise NotImplemented("Implement in derived Exporter class.") + raise NotImplementedError("Implement in derived Exporter class.") + + @staticmethod + def clean(project_name): + """Clean a previously exported project + This method is assumed to be executed at the same level as exporter + project files and project source code. + See uvision/__init__.py, iar/__init__.py, and makefile/__init__.py for + example implemenation. + + Positional Arguments: + project_name - the name of the project to build; often required by + exporter's build command. + + Returns nothing. May raise exceptions + """ + raise NotImplementedError("Implement in derived Exporter class.") @abstractmethod def generate(self): """Generate an IDE/tool specific project file""" - raise NotImplemented("Implement a generate function in Exporter child class") + raise NotImplementedError("Implement a generate function in Exporter child class") @classmethod def is_target_supported(cls, target_name): diff --git a/tools/export/gnuarmeclipse/__init__.py b/tools/export/gnuarmeclipse/__init__.py index c0f05f03d7..99860dcd36 100644 --- a/tools/export/gnuarmeclipse/__init__.py +++ b/tools/export/gnuarmeclipse/__init__.py @@ -299,6 +299,17 @@ class GNUARMEclipse(Exporter): print print 'Done. Import the \'{0}\' project in Eclipse.'.format(self.project_name) + @staticmethod + def clean(_): + os.remove('.project') + os.remove('.cproject') + if exists('Debug'): + shutil.rmtree('Debug') + if exists('Release'): + shutil.rmtree('Release') + if exists('makefile.targets'): + os.remove('makefile.targets') + # override @staticmethod def build(project_name, log_name="build_log.txt", cleanup=True): @@ -366,14 +377,6 @@ class GNUARMEclipse(Exporter): if cleanup: if exists(log_name): os.remove(log_name) - os.remove('.project') - os.remove('.cproject') - if exists('Debug'): - shutil.rmtree('Debug') - if exists('Release'): - shutil.rmtree('Release') - if exists('makefile.targets'): - os.remove('makefile.targets') # Always remove the temporary folder. if exists(tmp_folder): diff --git a/tools/export/iar/__init__.py b/tools/export/iar/__init__.py index 641c142d02..69b53dbb96 100644 --- a/tools/export/iar/__init__.py +++ b/tools/export/iar/__init__.py @@ -138,6 +138,17 @@ class IAR(Exporter): self.gen_file('iar/ewd.tmpl', ctx, self.project_name + ".ewd") self.gen_file('iar/ewp.tmpl', ctx, self.project_name + ".ewp") + @staticmethod + def clean(project_name): + os.remove(project_name + ".ewp") + os.remove(project_name + ".ewd") + os.remove(project_name + ".eww") + # legacy output file location + if exists('.build'): + shutil.rmtree('.build') + if exists('BUILD'): + shutil.rmtree('BUILD') + @staticmethod def build(project_name, log_name="build_log.txt", cleanup=True): """ Build IAR project """ @@ -179,14 +190,7 @@ class IAR(Exporter): # Cleanup the exported and built files if cleanup: - os.remove(project_name + ".ewp") - os.remove(project_name + ".ewd") - os.remove(project_name + ".eww") - # legacy output file location - if exists('.build'): - shutil.rmtree('.build') - if exists('BUILD'): - shutil.rmtree('BUILD') + IAR.clean(project_name) if ret_code !=0: # Seems like something went wrong. diff --git a/tools/export/makefile/__init__.py b/tools/export/makefile/__init__.py index a717dfe168..3c0fc2a857 100644 --- a/tools/export/makefile/__init__.py +++ b/tools/export/makefile/__init__.py @@ -148,6 +148,15 @@ class Makefile(Exporter): return flags + @staticmethod + def clean(_): + remove("Makefile") + # legacy .build directory cleaned if exists + if exists('.build'): + shutil.rmtree('.build') + if exists('BUILD'): + shutil.rmtree('BUILD') + @staticmethod def build(project_name, log_name="build_log.txt", cleanup=True): """ Build Make project """ @@ -178,13 +187,8 @@ class Makefile(Exporter): # Cleanup the exported and built files if cleanup: - remove("Makefile") remove(log_name) - # legacy .build directory cleaned if exists - if exists('.build'): - shutil.rmtree('.build') - if exists('BUILD'): - shutil.rmtree('BUILD') + Makefile.clean(project_name) if ret_code != 0: # Seems like something went wrong. diff --git a/tools/export/mcuxpresso/__init__.py b/tools/export/mcuxpresso/__init__.py index f145c61cbd..8768d8537d 100644 --- a/tools/export/mcuxpresso/__init__.py +++ b/tools/export/mcuxpresso/__init__.py @@ -230,6 +230,17 @@ class MCUXpresso(GNUARMEclipse): print print 'Done. Import the \'{0}\' project in Eclipse.'.format(self.project_name) + @staticmethod + def clean(_): + remove('.project') + remove('.cproject') + if exists('Debug'): + shutil.rmtree('Debug') + if exists('Release'): + shutil.rmtree('Release') + if exists('makefile.targets'): + remove('makefile.targets') + # override @staticmethod def build(project_name, log_name="build_log.txt", cleanup=True): @@ -299,14 +310,7 @@ class MCUXpresso(GNUARMEclipse): if cleanup: if exists(log_name): remove(log_name) - remove('.project') - remove('.cproject') - if exists('Debug'): - shutil.rmtree('Debug') - if exists('Release'): - shutil.rmtree('Release') - if exists('makefile.targets'): - remove('makefile.targets') + MCUXpresso.clean(project_name) # Always remove the temporary folder. if exists(tmp_folder): diff --git a/tools/export/nb/__init__.py b/tools/export/nb/__init__.py index 8b9174bfa5..47a815f1d7 100644 --- a/tools/export/nb/__init__.py +++ b/tools/export/nb/__init__.py @@ -1,5 +1,6 @@ import os import copy +import shutil from os.path import relpath, join, exists, dirname, basename from os import makedirs @@ -275,6 +276,11 @@ class GNUARMNetbeans(Exporter): print print 'Done. Import the \'{0}\' project in Netbeans.'.format(self.project_name) + @staticmethod + def clean(_): + shutil.rmtree("nbproject") + remove("Makefile") + # ------------------------------------------------------------------------- @staticmethod diff --git a/tools/export/uvision/__init__.py b/tools/export/uvision/__init__.py index 77f2b8d948..749dffa6cd 100644 --- a/tools/export/uvision/__init__.py +++ b/tools/export/uvision/__init__.py @@ -238,6 +238,16 @@ class Uvision(Exporter): self.gen_file('uvision/uvision.tmpl', ctx, self.project_name+".uvprojx") self.gen_file('uvision/uvision_debug.tmpl', ctx, self.project_name + ".uvoptx") + @staticmethod + def clean(project_name): + os.remove(project_name + ".uvprojx") + os.remove(project_name + ".uvoptx") + # legacy .build directory cleaned if exists + if exists('.build'): + shutil.rmtree('.build') + if exists('BUILD'): + shutil.rmtree('BUILD') + @staticmethod def build(project_name, log_name='build_log.txt', cleanup=True): """ Build Uvision project """ @@ -257,13 +267,7 @@ class Uvision(Exporter): # Cleanup the exported and built files if cleanup: os.remove(log_name) - os.remove(project_name+".uvprojx") - os.remove(project_name+".uvoptx") - # legacy .build directory cleaned if exists - if exists('.build'): - shutil.rmtree('.build') - if exists('BUILD'): - shutil.rmtree('BUILD') + Uvision.clean(project_name) # Returns 0 upon success, 1 upon a warning, and neither upon an error if ret_code != 0 and ret_code != 1: diff --git a/tools/project.py b/tools/project.py index 65c65b17da..141dfe8783 100644 --- a/tools/project.py +++ b/tools/project.py @@ -3,13 +3,14 @@ supported IDEs or project structures. """ from __future__ import absolute_import, print_function import sys -from os.path import join, abspath, dirname, exists, basename +from os.path import (join, abspath, dirname, exists, basename, normpath, + realpath, basename) +from os import remove ROOT = abspath(join(dirname(__file__), "..")) sys.path.insert(0, ROOT) from shutil import move, rmtree from argparse import ArgumentParser -from os.path import normpath, realpath from tools.paths import EXPORT_DIR, MBED_HAL, MBED_LIBRARIES, MBED_TARGETS_PATH from tools.settings import BUILD_DIR @@ -247,7 +248,13 @@ def main(): args_error(parser, "%s not supported by %s"%(mcu,options.ide)) profile = extract_profile(parser, options, toolchain_name, fallback="debug") if options.clean: - rmtree(BUILD_DIR) + for cls in EXPORTERS.values(): + try: + cls.clean(basename(abspath(options.source_dir[0]))) + except (NotImplementedError, IOError, OSError): + pass + for f in EXPORTERS.values()[0].CLEAN_FILES: + remove(f) try: export(mcu, options.ide, build=options.build, src=options.source_dir, macros=options.macros,