From ee78496058613d49847877fad465a2645689e06c Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Fri, 8 Oct 2021 19:02:45 +0300 Subject: [PATCH] Clean a build environment and installed library dependencies using a new ``cleanall`` target // Resolve #4062 --- HISTORY.rst | 3 +- platformio/builder/main.py | 8 +++-- platformio/builder/tools/piotarget.py | 47 ++++++++++++++++++--------- 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index a3f404ff..c5bfb57b 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -11,7 +11,8 @@ PlatformIO Core 5 5.2.1 (2021-??-??) ~~~~~~~~~~~~~~~~~~ -- Allowed to override a default library builder via a new ``builder`` field in a ``build`` group of `library.json `__ manifest (`issue #3957 `_) +- Clean a build environment and installed library dependencies using a new ``cleanall`` target (`issue #4062 `_) +- Override a default library builder via a new ``builder`` field in a ``build`` group of `library.json `__ manifest (`issue #3957 `_) - Updated `Cppcheck `__ v2.6 with new checks, increased reliability of advanced addons (MISRA/CERT) and various improvements - Handle the "test" folder as a part of CLion project (`issue #4005 `_) - Improved handling of a library root based on "Conan" or "CMake" build systems (`issue #3887 `_) diff --git a/platformio/builder/main.py b/platformio/builder/main.py index 04d6e342..7f719cca 100644 --- a/platformio/builder/main.py +++ b/platformio/builder/main.py @@ -149,10 +149,12 @@ if int(ARGUMENTS.get("ISATTY", 0)): # pylint: disable=protected-access click._compat.isatty = lambda stream: True -if env.GetOption("clean"): - env.PioClean(env.subst("$BUILD_DIR")) +is_clean_all = "cleanall" in COMMAND_LINE_TARGETS +if env.GetOption("clean") or is_clean_all: + env.PioClean(is_clean_all) env.Exit(0) -elif not int(ARGUMENTS.get("PIOVERBOSE", 0)): + +if not int(ARGUMENTS.get("PIOVERBOSE", 0)): click.echo("Verbose mode can be enabled via `-v, --verbose` option") # Dynamically load dependent tools diff --git a/platformio/builder/tools/piotarget.py b/platformio/builder/tools/piotarget.py index 70b7c41b..6ebe1f99 100644 --- a/platformio/builder/tools/piotarget.py +++ b/platformio/builder/tools/piotarget.py @@ -29,7 +29,7 @@ def VerboseAction(_, act, actstr): return Action(act, actstr) -def PioClean(env, clean_dir): +def PioClean(env, clean_all=False): def _relpath(path): if compat.IS_WINDOWS: prefix = os.getcwd()[:2].lower() @@ -41,21 +41,30 @@ def PioClean(env, clean_dir): return path return os.path.relpath(path) - if not os.path.isdir(clean_dir): + def _clean_dir(path): + clean_rel_path = _relpath(path) + for root, _, files in os.walk(path): + for f in files: + dst = os.path.join(root, f) + os.remove(dst) + print( + "Removed %s" + % (dst if not clean_rel_path.startswith(".") else _relpath(dst)) + ) + + build_dir = env.subst("$BUILD_DIR") + libdeps_dir = env.subst("$PROJECT_LIBDEPS_DIR") + if os.path.isdir(build_dir): + _clean_dir(build_dir) + fs.rmtree(build_dir) + else: print("Build environment is clean") - env.Exit(0) - clean_rel_path = _relpath(clean_dir) - for root, _, files in os.walk(clean_dir): - for f in files: - dst = os.path.join(root, f) - os.remove(dst) - print( - "Removed %s" - % (dst if not clean_rel_path.startswith(".") else _relpath(dst)) - ) + + if clean_all and os.path.isdir(libdeps_dir): + _clean_dir(libdeps_dir) + fs.rmtree(libdeps_dir) + print("Done cleaning") - fs.rmtree(clean_dir) - env.Exit(0) def AddTarget( # pylint: disable=too-many-arguments @@ -65,7 +74,7 @@ def AddTarget( # pylint: disable=too-many-arguments actions, title=None, description=None, - group="Generic", + group="General", always_build=True, ): if "__PIO_TARGETS" not in env: @@ -101,7 +110,13 @@ def DumpTargets(env): description="Generate compilation database `compile_commands.json`", group="Advanced", ) - targets["clean"] = dict(name="clean", title="Clean", group="Generic") + targets["clean"] = dict(name="clean", title="Clean", group="General") + targets["cleanall"] = dict( + name="cleanall", + title="Clean All", + group="General", + description="Clean a build environment and installed library dependencies", + ) return list(targets.values())