platformio / platformio-docs

PlatformIO Documentation
https://docs.platformio.org
Apache License 2.0
234 stars 323 forks source link

PLatformIO's external scripts How to use them, middleware functions are a bit problematic #326

Closed GPTKEY closed 8 months ago

GPTKEY commented 8 months ago

I want to replace the library files under the kernel package with files from the project Lib library. I need to use an external script

I have repeatedly passed xxx.c directly to an external script using env.AddBuildMiddleware(replace_node_with_another, lib_file) and only see AddBuildMiddleware executed once at compile time, is this a bug? The replace_node_with_another() function does not return None.

I've added extra_scripts to the ini file = pre:extra_script.py

I've been testing for a day and I've determined that there is a problem with this way of handling kernel and framework library headers.

valeros commented 8 months ago

Hi @GPTKEY, do you mind sharing your platformio.ini and the code from the extra script here?

GPTKEY commented 8 months ago
[env:bluepill_f103c8]
board = bluepill_f103c8
platform = ststm32
framework = arduino
extra_scripts = pre:extra_script.py
build_flags = -I./lib/uart/
lib_deps = 
    bxparks/AceButton@^1.9.2
    arduino-libraries/ArduinoRS485@^1.0.2
    arduino-libraries/ArduinoModbus@^1.0.7
debug_tool = jlink
upload_protocol = jlink
; platform_packages = framework-arduinoststm32
middleware_functions = [
    "**/uart.c",
    "**/uart.h"
]

for file_path in middleware_functions :
    env.AddBuildMiddleware(replace_node_with_another, file_path)

I tested it many times and finally implemented AddBuildMiddleware() to run successfully. But it didn't work the way I wanted it to when I compiled it later. Because when I replace the header file, it doesn't work. Maybe the header file is not supported, or there are some parameter variations that prevent the header file from being fully replaced.

valeros commented 8 months ago

Unfortunately, only source files can be used with AddBuildMiddleware helper. Not sure if it's applicable in your case, but there is a possible alternative for overriding default packages described here https://docs.platformio.org/en/latest/scripting/examples/override_package_files.html