From 5b4af52d048418fd0d2db69f0b67f0422aaae7b6 Mon Sep 17 00:00:00 2001
From: Ivan Kravets <me@ikravets.com>
Date: Sat, 6 Aug 2022 09:17:46 +0300
Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20Fix=20a=20PlatformIO=20debug=20i?=
 =?UTF-8?q?ssue=20(#24569)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../PlatformIO/scripts/common-cxxflags.py     | 65 ++++++++++---------
 .../share/PlatformIO/scripts/simulator.py     |  1 +
 2 files changed, 37 insertions(+), 29 deletions(-)

diff --git a/buildroot/share/PlatformIO/scripts/common-cxxflags.py b/buildroot/share/PlatformIO/scripts/common-cxxflags.py
index 1e8f0dcb055..22a0665e05d 100644
--- a/buildroot/share/PlatformIO/scripts/common-cxxflags.py
+++ b/buildroot/share/PlatformIO/scripts/common-cxxflags.py
@@ -2,38 +2,45 @@
 # common-cxxflags.py
 # Convenience script to apply customizations to CPP flags
 #
+
 import pioutil
 if pioutil.is_pio_build():
-	Import("env")
+    Import("env")
 
-	cxxflags = [
-		#"-Wno-incompatible-pointer-types",
-		#"-Wno-unused-const-variable",
-		#"-Wno-maybe-uninitialized",
-		#"-Wno-sign-compare"
-	]
-	if "teensy" not in env['PIOENV']:
-		cxxflags += ["-Wno-register"]
-	env.Append(CXXFLAGS=cxxflags)
+    cxxflags = [
+        # "-Wno-incompatible-pointer-types",
+        # "-Wno-unused-const-variable",
+        # "-Wno-maybe-uninitialized",
+        # "-Wno-sign-compare"
+    ]
+    if "teensy" not in env["PIOENV"]:
+        cxxflags += ["-Wno-register"]
+    env.Append(CXXFLAGS=cxxflags)
 
-	#
-	# Add CPU frequency as a compile time constant instead of a runtime variable
-	#
-	def add_cpu_freq():
-		if 'BOARD_F_CPU' in env:
-			env['BUILD_FLAGS'].append('-DBOARD_F_CPU=' + env['BOARD_F_CPU'])
+    #
+    # Add CPU frequency as a compile time constant instead of a runtime variable
+    #
+    def add_cpu_freq():
+        if "BOARD_F_CPU" in env:
+            env["BUILD_FLAGS"].append("-DBOARD_F_CPU=" + env["BOARD_F_CPU"])
 
-	# Useful for JTAG debugging
-	#
-	# It will separate release and debug build folders.
-	# It useful to keep two live versions: a debug version for debugging and another for
-	# release, for flashing when upload is not done automatically by jlink/stlink.
-	# Without this, PIO needs to recompile everything twice for any small change.
-	if env.GetBuildType() == "debug" and env.get('UPLOAD_PROTOCOL') not in ['jlink', 'stlink', 'custom']:
-		env['BUILD_DIR'] = '$PROJECT_BUILD_DIR/$PIOENV/debug'
+    # Useful for JTAG debugging
+    #
+    # It will separate release and debug build folders.
+    # It useful to keep two live versions: a debug version for debugging and another for
+    # release, for flashing when upload is not done automatically by jlink/stlink.
+    # Without this, PIO needs to recompile everything twice for any small change.
+    if env.GetBuildType() == "debug" and env.get("UPLOAD_PROTOCOL") not in ["jlink", "stlink", "custom"]:
+        env["BUILD_DIR"] = "$PROJECT_BUILD_DIR/$PIOENV/debug"
 
-	# On some platform, F_CPU is a runtime variable. Since it's used to convert from ns
-	# to CPU cycles, this adds overhead preventing small delay (in the order of less than
-	# 30 cycles) to be generated correctly. By using a compile time constant instead
-	# the compiler will perform the computation and this overhead will be avoided
-	add_cpu_freq()
+        def on_program_ready(source, target, env):
+            import shutil
+            shutil.copy(target[0].get_abspath(), env.subst("$PROJECT_BUILD_DIR/$PIOENV"))
+
+        env.AddPostAction("$PROGPATH", on_program_ready)
+
+    # On some platform, F_CPU is a runtime variable. Since it's used to convert from ns
+    # to CPU cycles, this adds overhead preventing small delay (in the order of less than
+    # 30 cycles) to be generated correctly. By using a compile time constant instead
+    # the compiler will perform the computation and this overhead will be avoided
+    add_cpu_freq()
diff --git a/buildroot/share/PlatformIO/scripts/simulator.py b/buildroot/share/PlatformIO/scripts/simulator.py
index 2961d2826d4..1767f83d32d 100644
--- a/buildroot/share/PlatformIO/scripts/simulator.py
+++ b/buildroot/share/PlatformIO/scripts/simulator.py
@@ -2,6 +2,7 @@
 # simulator.py
 # PlatformIO pre: script for simulator builds
 #
+
 import pioutil
 if pioutil.is_pio_build():
 	# Get the environment thus far for the build