diff --git a/HISTORY.rst b/HISTORY.rst index be4dcdc0..6b9aacb6 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -13,7 +13,7 @@ PlatformIO Core 6 **A professional collaborative platform for declarative, safety-critical, and test-driven embedded development.** -6.0.3 (2022-??-??) +6.1.0 (2022-??-??) ~~~~~~~~~~~~~~~~~~ * **Device Manager** @@ -36,9 +36,14 @@ PlatformIO Core 6 * **Build System** + - Significantly improved support for `Pre & Post Actions `__ + + * Allowed to declare actions in the `PRE-type scripts `__ even if the target is not ready yet + * Allowed library maintainers to use Pre & Post Actions in the library `extraScript `__ + + - Allowed to ``Import("projenv")`` in a library extra script (`issue #4305 `_) - Documented `Stringification `__ – converting a macro argument into a string constant (`issue #4310 `_) - Added ``env.StringifyMacro(value)`` helper function for the `Advanced Scripting `__ - - Allowed to ``Import("projenv")`` in a library extra script (`issue #4305 `_) - Fixed an issue when the `build_unflags `__ operation ignores a flag value (`issue #4309 `_) - Fixed an issue when the `build_unflags `__ option was not applied to the ``ASPPFLAGS`` scope - Fixed an issue on Windows OS when flags were wrapped to the temporary file while generating the `Compilation database "compile_commands.json" `__ diff --git a/docs b/docs index d24f9a64..54d25a92 160000 --- a/docs +++ b/docs @@ -1 +1 @@ -Subproject commit d24f9a649c849448e96fb7ee4aaa3aa6ffa7ba55 +Subproject commit 54d25a92c921f9e2e000898cfbfcd8637154bff3 diff --git a/platformio/builder/main.py b/platformio/builder/main.py index e46dd079..e2482625 100644 --- a/platformio/builder/main.py +++ b/platformio/builder/main.py @@ -53,6 +53,7 @@ DEFAULT_ENV_OPTIONS = dict( "cc", "c++", "link", + "piohooks", "pioasm", "platformio", "pioproject", @@ -221,6 +222,8 @@ env.AddPreAction( AlwaysBuild(env.Alias("__debug", DEFAULT_TARGETS)) AlwaysBuild(env.Alias("__test", DEFAULT_TARGETS)) +env.ProcessDelayedActions() + ############################################################################## if "envdump" in COMMAND_LINE_TARGETS: diff --git a/platformio/builder/tools/piohooks.py b/platformio/builder/tools/piohooks.py new file mode 100644 index 00000000..653a7f30 --- /dev/null +++ b/platformio/builder/tools/piohooks.py @@ -0,0 +1,42 @@ +# Copyright (c) 2014-present PlatformIO +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import + + +def AddActionWrapper(handler): + def wraps(env, files, action): + nodes = env.arg2nodes(files, env.fs.Entry) + unknown_nodes = [node for node in nodes if not node.exists()] + if unknown_nodes: + env.Append(**{"_PIO_DELAYED_ACTIONS": [(handler, unknown_nodes, action)]}) + return handler([node for node in nodes if node.exists()], action) + + return wraps + + +def ProcessDelayedActions(env): + for func, nodes, action in env.get("_PIO_DELAYED_ACTIONS", []): + func(nodes, action) + + +def generate(env): + env.Replace(**{"_PIO_DELAYED_ACTIONS": []}) + env.AddMethod(AddActionWrapper(env.AddPreAction), "AddPreAction") + env.AddMethod(AddActionWrapper(env.AddPostAction), "AddPostAction") + env.AddMethod(ProcessDelayedActions) + + +def exists(_): + return True diff --git a/tests/commands/test_run.py b/tests/commands/test_run.py index 3a2817ba..c91594e2 100644 --- a/tests/commands/test_run.py +++ b/tests/commands/test_run.py @@ -17,7 +17,7 @@ from pathlib import Path from platformio.run.cli import cli as cmd_run -def test_build_flags(clirunner, validate_cliresult, tmpdir): +def test_generic_build(clirunner, validate_cliresult, tmpdir): build_flags = [ ("-D TEST_INT=13", "-DTEST_INT=13"), ("-DTEST_SINGLE_MACRO", "-DTEST_SINGLE_MACRO"), @@ -28,7 +28,9 @@ def test_build_flags(clirunner, validate_cliresult, tmpdir): """ [env:native] platform = native -extra_scripts = extra.py +extra_scripts = + pre:pre_script.py + post_script.py lib_ldf_mode = deep+ build_src_flags = -DI_AM_ONLY_SRC_FLAG build_flags = @@ -38,7 +40,17 @@ build_flags = % " ".join([f[0] for f in build_flags]) ) - tmpdir.join("extra.py").write( + tmpdir.join("pre_script.py").write( + """ +Import("env") + +def post_prog_action(source, target, env): + print("post_prog_action is called") + +env.AddPostAction("$PROG_PATH", post_prog_action) + """ + ) + tmpdir.join("post_script.py").write( """ Import("projenv") @@ -102,6 +114,7 @@ void dummy(void ) {}; result = clirunner.invoke(cmd_run, ["--project-dir", str(tmpdir), "--verbose"]) validate_cliresult(result) + assert "post_prog_action is called" in result.output build_output = result.output[result.output.find("Scanning dependencies...") :] for flag in build_flags: assert flag[1] in build_output, flag