From 12d03f06b01bc4de4bf3fbf71ee9d46b7e561a06 Mon Sep 17 00:00:00 2001 From: Thomas Basler Date: Thu, 20 Apr 2023 23:13:18 +0200 Subject: [PATCH] Added functionality to apply custom patch files during building Usefull to patch existing libraries --- patches/esp32c3/EspAsyncWebserver.patch | 13 +++++ pio-scripts/patch_apply.py | 78 +++++++++++++++++++++++++ platformio.ini | 14 +++++ 3 files changed, 105 insertions(+) create mode 100644 patches/esp32c3/EspAsyncWebserver.patch create mode 100644 pio-scripts/patch_apply.py diff --git a/patches/esp32c3/EspAsyncWebserver.patch b/patches/esp32c3/EspAsyncWebserver.patch new file mode 100644 index 00000000..079c164d --- /dev/null +++ b/patches/esp32c3/EspAsyncWebserver.patch @@ -0,0 +1,13 @@ +diff --git a/.pio/libdeps/$$$env$$$/ESP Async WebServer/src/AsyncWebSocket.cpp b/.pio/libdeps/$$$env$$$/ESP Async WebServer/src/AsyncWebSocket.cpp +index 12be5f8..8505f73 100644 +--- a/.pio/libdeps/$$$env$$$/ESP Async WebServer/src/AsyncWebSocket.cpp ++++ b/.pio/libdeps/$$$env$$$/ESP Async WebServer/src/AsyncWebSocket.cpp +@@ -737,7 +737,7 @@ void AsyncWebSocketClient::binary(const __FlashStringHelper *data, size_t len) + IPAddress AsyncWebSocketClient::remoteIP() const + { + if (!_client) +- return IPAddress(0U); ++ return IPAddress((uint32_t)0); + + return _client->remoteIP(); + } diff --git a/pio-scripts/patch_apply.py b/pio-scripts/patch_apply.py new file mode 100644 index 00000000..17e25584 --- /dev/null +++ b/pio-scripts/patch_apply.py @@ -0,0 +1,78 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# +# Copyright (C) 2023 Thomas Basler and others +# +import os +import subprocess +import re + +Import("env") + +def getPatchPath(env): + return os.path.join(env["PROJECT_DIR"], "patches", env.GetProjectOption('custom_patches')) + +def is_tool(name): + """Check whether `name` is on PATH and marked as executable.""" + + # from whichcraft import which + from shutil import which + + return which(name) is not None + +def replaceInFile(in_file, out_file, text, subs, flags=0): + """ + Function for replacing content for the given file + Taken from https://www.studytonight.com/python-howtos/search-and-replace-a-text-in-a-file-in-python + """ + if os.path.exists(in_file): + with open(in_file, "rb") as infile: + with open(out_file, "wb") as outfile: + #read the file contents + file_contents = infile.read() + text_pattern = re.compile(re.escape(text), flags) + file_contents = text_pattern.sub(subs, file_contents.decode('utf-8')) + outfile.seek(0) + outfile.truncate() + outfile.write(file_contents.encode()) + +def main(): + if (env.GetProjectOption('custom_patches', '') == ''): + print('No custom_patches specified') + return + + if (not is_tool('git')): + print('Git not found. Will not apply custom patches!') + return + + directory = getPatchPath(env) + if (not os.path.isdir(directory)): + print('Patch directory not found: ' + directory) + return + + for file in os.listdir(directory): + if (not file.endswith('.patch')): + continue + + fullPath = os.path.join(directory, file) + preparePath = fullPath + '.prepare' + replaceInFile(fullPath, preparePath, '$$$env$$$', env['PIOENV']) + print('Working on patch: ' + fullPath + '... ', end='') + + # Check if patch was already applied + process = subprocess.run(['git', 'apply', '--reverse', '--check', preparePath], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + if (process.returncode == 0): + print('already applied') + os.remove(preparePath) + continue + + # Apply patch + process = subprocess.run(['git', 'apply', preparePath]) + if (process.returncode == 0): + print('applied') + else: + print('failed') + + os.remove(preparePath) + + +main() \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 97096a52..4df525c8 100644 --- a/platformio.ini +++ b/platformio.ini @@ -35,6 +35,7 @@ lib_deps = extra_scripts = pre:pio-scripts/auto_firmware_version.py + pre:pio-scripts/patch_apply.py board_build.partitions = partitions_custom.csv board_build.filesystem = littlefs @@ -58,6 +59,19 @@ build_flags = ${env.build_flags} -DHOYMILES_PIN_CS=5 +[env:generic_esp32c3] +board = esp32dev +board_build.mcu = esp32c3 +custom_patches = esp32c3 +build_flags = ${env.build_flags} + -DHOYMILES_PIN_MISO=9 + -DHOYMILES_PIN_MOSI=10 + -DHOYMILES_PIN_SCLK=8 + -DHOYMILES_PIN_IRQ=3 + -DHOYMILES_PIN_CE=4 + -DHOYMILES_PIN_CS=5 + + [env:olimex_esp32_poe] ; https://www.olimex.com/Products/IoT/ESP32/ESP32-POE/open-source-hardware board = esp32-poe