From 654abddec3d24b64a19aedcc2033ef4d1ee3b616 Mon Sep 17 00:00:00 2001 From: Tomas Pilny Date: Mon, 24 Apr 2023 15:33:25 +0200 Subject: [PATCH 1/5] Iniital commit --- docs/source/esp-idf_component.rst | 86 +++++++++++++++++++++++++++++++ tools/add_lib.sh | 72 ++++++++++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100755 tools/add_lib.sh diff --git a/docs/source/esp-idf_component.rst b/docs/source/esp-idf_component.rst index fe27d52110f..01c1c42f557 100644 --- a/docs/source/esp-idf_component.rst +++ b/docs/source/esp-idf_component.rst @@ -163,3 +163,89 @@ Compilation Errors ------------------ As commits are made to esp-idf and submodules, the codebases can develop incompatibilities that cause compilation errors. If you have problems compiling, follow the instructions in `Issue #1142 `_ to roll esp-idf back to a different version. + +Adding arduino library +---------------------- + +There are few approaches: + +1. Add global library to ``components/arduino-esp32/libraries/new_library`` +2. Add local project library to ``examples/your_project/main/libraries/new_library`` + +1 Adding global library +*********************** + +Download the library: + +.. code-block:: bash + + cd ~/esp/esp-idf/components/arduino-esp32/ + git clone --recursive git@github.com:Author/new_library.git libraries/new_library + + +Edit file ``components/arduino-esp32/CMakeLists.txt`` + +Get the source file list with shell command: + +.. code-block:: bash + + find libraries/new_library/src/ -name '*.c' -o -name '*.cpp' + libraries/new_library/src/new_library.cpp + libraries/new_library/src/new_library_extra_file.c + +Locate block which starts with ``set(LIBRARY_SRCS`` and copy the list there. Now it should look something like this: + +.. code-block:: bash + + set(LIBRARY_SRCS + libraries/ArduinoOTA/src/ArduinoOTA.cpp + libraries/AsyncUDP/src/AsyncUDP.cpp + libraries/new_library/src/new_library.cpp + libraries/new_library/src/new_library_extra_file.c + + +After this add the library path to block which starts with ``set(includedirs``. It should look like this: + +.. code-block:: bash + + set(includedirs + variants/${CONFIG_ARDUINO_VARIANT}/ + cores/esp32/ + libraries/ArduinoOTA/src + libraries/AsyncUDP/src + libraries/new_library/src + + +2 Adding local library +********************** + +Download the library: + +.. code-block:: bash + + cd ~/esp/esp-idf/examples/your_project + mkdir components + git clone --recursive git@github.com:Author/new_library.git components/new_library + +??? Modify your main\CMakeLists.txt by adding the library sources: + +Create new CMakeists.txt in the library folder: ``components/new_library/CMakelists.txt`` + +.. code-block:: bash + + idf_component_register(SRCS "new_library.cpp" + INCLUDE_DIRS "." + ) + +You can read more about CMakelists in the IDF documentation regarding the `Build System `_ + +Tip +--- + +If you want to use arduino-esp32 both as an ESP-IDF component and with Arduino IDE you can simply create a symlink: + +.. code-block:: bash + + ln -s ~/Arduino/hardware/espressif/esp32 ~/esp/esp-idf/components/arduino-esp32 + +This will allow you to install new libraries as usual with Arduino IDE. To use them with IDF component, use ``add_lib.sh -e ~/Arduino/libraries/New_lib`` diff --git a/tools/add_lib.sh b/tools/add_lib.sh new file mode 100755 index 00000000000..977cc6a718d --- /dev/null +++ b/tools/add_lib.sh @@ -0,0 +1,72 @@ +#!/bin/bash +LIBRARY="" + +#TODO project library: -p path_to/project + + +# this is for new lib: [-n ] +# Get the directory name where this script is located +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" + +# Construct the absolute path to libraries folder +LIBS_PATH="$SCRIPT_DIR/../libraries" + +# Use $LIBS_PATH variable to clone new lib, regardless of where the script is run from. +echo "Cloning: git clone --recursive $1 $LIBS_PATH/Firebase-ESP32" +git clone --recursive $1 $LIBS_PATH/Firebase-ESP32 + +LIBRARY=$LIBS_PATH/Firebase-ESP32 + + + + +# get the CMakelists right + + + + + + + + + +# local: +#Modify your main\CMakeLists.txt to this: +# +#idf_component_register( +# SRC_DIRS "." +# "libraries/Firebase-ESP32/src" +# "libraries/Firebase-ESP32/src/addons" +# "libraries/Firebase-ESP32/src/json" +# "libraries/Firebase-ESP32/src/json/extras/print" +# "libraries/Firebase-ESP32/src/json/MB_JSON" +# "libraries/Firebase-ESP32/src/mbfs" +# "libraries/Firebase-ESP32/src/rtdb" +# "libraries/Firebase-ESP32/src/rtdb/stream" +# "libraries/Firebase-ESP32/src/session" +# "libraries/Firebase-ESP32/src/signer" +# "libraries/Firebase-ESP32/src/sslclient/esp32" +# "libraries/Firebase-ESP32/src/wcs" +# "libraries/Firebase-ESP32/src/wcs/base" +# "libraries/Firebase-ESP32/src/wcs/custom" +# "libraries/Firebase-ESP32/src/wcs/esp32" +# + # +# INCLUDE_DIRS "." +# "libraries/Firebase-ESP32/src" +# "libraries/Firebase-ESP32/src/addons" +# "libraries/Firebase-ESP32/src/json" +# "libraries/Firebase-ESP32/src/json/extras/print" +# "libraries/Firebase-ESP32/src/json/MB_JSON" +# "libraries/Firebase-ESP32/src/mbfs" +# "libraries/Firebase-ESP32/src/rtdb" +# "libraries/Firebase-ESP32/src/rtdb/stream" +# "libraries/Firebase-ESP32/src/session" +# "libraries/Firebase-ESP32/src/signer" +# "libraries/Firebase-ESP32/src/sslclient/esp32" +# "libraries/Firebase-ESP32/src/wcs" +# "libraries/Firebase-ESP32/src/wcs/base" +# "libraries/Firebase-ESP32/src/wcs/custom" +# "libraries/Firebase-ESP32/src/wcs/esp32" +#) + From f0d0ccb706d1d47493478284ca60adcc751965f2 Mon Sep 17 00:00:00 2001 From: Tomas Pilny Date: Tue, 25 Apr 2023 09:28:42 +0200 Subject: [PATCH 2/5] Fixed naming of the CMakeLists.txt file in the doc text --- docs/source/esp-idf_component.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/source/esp-idf_component.rst b/docs/source/esp-idf_component.rst index 01c1c42f557..72b6dcf1ff9 100644 --- a/docs/source/esp-idf_component.rst +++ b/docs/source/esp-idf_component.rst @@ -229,15 +229,17 @@ Download the library: ??? Modify your main\CMakeLists.txt by adding the library sources: -Create new CMakeists.txt in the library folder: ``components/new_library/CMakelists.txt`` +Create new CMakeists.txt in the library folder: ``components/new_library/CMakeLists.txt`` .. code-block:: bash idf_component_register(SRCS "new_library.cpp" INCLUDE_DIRS "." + REQUIRES arduino-esp32 + #PRIV_REQUIRES arduino-esp32, # ?? ) -You can read more about CMakelists in the IDF documentation regarding the `Build System `_ +You can read more about CMakeLists in the IDF documentation regarding the `Build System `_ Tip --- From 04d5289113f058cf8877838ecf5b5cf236ac8e66 Mon Sep 17 00:00:00 2001 From: Tomas Pilny Date: Tue, 25 Apr 2023 09:55:58 +0200 Subject: [PATCH 3/5] Finished the docmentation --- docs/source/esp-idf_component.rst | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/docs/source/esp-idf_component.rst b/docs/source/esp-idf_component.rst index 72b6dcf1ff9..efecff594f3 100644 --- a/docs/source/esp-idf_component.rst +++ b/docs/source/esp-idf_component.rst @@ -227,16 +227,13 @@ Download the library: mkdir components git clone --recursive git@github.com:Author/new_library.git components/new_library -??? Modify your main\CMakeLists.txt by adding the library sources: - Create new CMakeists.txt in the library folder: ``components/new_library/CMakeLists.txt`` .. code-block:: bash - idf_component_register(SRCS "new_library.cpp" + idf_component_register(SRCS "new_library.cpp" "another_source.c" INCLUDE_DIRS "." REQUIRES arduino-esp32 - #PRIV_REQUIRES arduino-esp32, # ?? ) You can read more about CMakeLists in the IDF documentation regarding the `Build System `_ From 3dc51909dcf9f6ae511ecaf913b8193b16d3f90a Mon Sep 17 00:00:00 2001 From: Tomas Pilny Date: Wed, 26 Apr 2023 12:09:00 +0200 Subject: [PATCH 4/5] Updated, but not yet tested script --- tools/add_lib.sh | 200 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 138 insertions(+), 62 deletions(-) diff --git a/tools/add_lib.sh b/tools/add_lib.sh index 977cc6a718d..db77c2089f2 100755 --- a/tools/add_lib.sh +++ b/tools/add_lib.sh @@ -1,72 +1,148 @@ #!/bin/bash -LIBRARY="" +HELP="This script help to add library when using arduino-esp32 as an ESP-IDF component +The script accepts up to three arguments: +-n NEW: URL address to new library on GIThub (cannot be combined with -e) +-l LOCAL: Path to the project where the library should be placed locally +-e EXISTING: path to existing libary- this will simply skip the download (cannot be combined with -n) -#TODO project library: -p path_to/project +Examples: +./add_lib.sh -n https://github.com/me-no-dev/ESPAsyncWebServer +./add_lib.sh -l ~/esp/esp-idf/examples/your_project +./add_lib.sh -e ~/Arduino/libraries/existing_library +./add_lib.sh -n https://github.com/me-no-dev/ESPAsyncWebServer -l ~/esp/esp-idf/examples/your_project +./add_lib.sh -e ~/Arduino/libraries/existing_library -l ~/esp/esp-idf/examples/your_project" -# this is for new lib: [-n ] # Get the directory name where this script is located SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" # Construct the absolute path to libraries folder -LIBS_PATH="$SCRIPT_DIR/../libraries" - -# Use $LIBS_PATH variable to clone new lib, regardless of where the script is run from. -echo "Cloning: git clone --recursive $1 $LIBS_PATH/Firebase-ESP32" -git clone --recursive $1 $LIBS_PATH/Firebase-ESP32 - -LIBRARY=$LIBS_PATH/Firebase-ESP32 - - - - -# get the CMakelists right - +ARDUINO_LIBS_PATH="$SCRIPT_DIR/../libraries" + +# Define the default values for the parameters +e_param="" +l_param="" +n_param="" + +# Parse the command-line arguments using getopts +while getopts "he:l:n:" opt; do + case $opt in + h) + echo "$HELP" + exit 0 + ;; + e) + #e_param="$OPTARG" + e_param="${OPTARG/#~/$HOME}" + ;; + l) + #l_param="$OPTARG" + l_param="${OPTARG/#~/$HOME}" + ;; + n) + n_param=$OPTARG + ;; + \?) + echo "Invalid option: -$OPTARG" >&2 + echo $HELP + exit 1 + ;; + :) + echo "Option -$OPTARG requires an argument." >&2 + echo $HELP + exit 1 + ;; + esac +done + +# Debug Print the parameters +echo "e_param: $e_param" +echo "l_param: $l_param" +echo "n_param: $n_param" + +# No parameter check +if [[ -z "$e_param" ]] && [[ -z "$e_param" ]] && [[ -z "$e_param" ]]; then + echo "Error: No parameters" >&2 + echo "$HELP" + exit 1 + +# Invalid combination check +if [[ ! -z $e_param ]] && [[ ! -z $n_param ]]; then + echo "ERROR: Cannot combine -n with -e" >&2 + echo "$HELP" + exit 1 +fi +fi + +# Check existing lib +if [[ ! -z "$e_param" ]]; then + echo "DEBUG: existing lib parameter supplied" + if [[ ! -d "${e_param/#~/$HOME}" ]]; then # this works! + echo "Error: existing library parameter - path does not exist" >&2 + exit 1 + fi +fi + +exit # Script is not yet finished and tested - use at your own risk! +LIBRARY="" - - - - - - -# local: -#Modify your main\CMakeLists.txt to this: -# -#idf_component_register( -# SRC_DIRS "." -# "libraries/Firebase-ESP32/src" -# "libraries/Firebase-ESP32/src/addons" -# "libraries/Firebase-ESP32/src/json" -# "libraries/Firebase-ESP32/src/json/extras/print" -# "libraries/Firebase-ESP32/src/json/MB_JSON" -# "libraries/Firebase-ESP32/src/mbfs" -# "libraries/Firebase-ESP32/src/rtdb" -# "libraries/Firebase-ESP32/src/rtdb/stream" -# "libraries/Firebase-ESP32/src/session" -# "libraries/Firebase-ESP32/src/signer" -# "libraries/Firebase-ESP32/src/sslclient/esp32" -# "libraries/Firebase-ESP32/src/wcs" -# "libraries/Firebase-ESP32/src/wcs/base" -# "libraries/Firebase-ESP32/src/wcs/custom" -# "libraries/Firebase-ESP32/src/wcs/esp32" -# - # -# INCLUDE_DIRS "." -# "libraries/Firebase-ESP32/src" -# "libraries/Firebase-ESP32/src/addons" -# "libraries/Firebase-ESP32/src/json" -# "libraries/Firebase-ESP32/src/json/extras/print" -# "libraries/Firebase-ESP32/src/json/MB_JSON" -# "libraries/Firebase-ESP32/src/mbfs" -# "libraries/Firebase-ESP32/src/rtdb" -# "libraries/Firebase-ESP32/src/rtdb/stream" -# "libraries/Firebase-ESP32/src/session" -# "libraries/Firebase-ESP32/src/signer" -# "libraries/Firebase-ESP32/src/sslclient/esp32" -# "libraries/Firebase-ESP32/src/wcs" -# "libraries/Firebase-ESP32/src/wcs/base" -# "libraries/Firebase-ESP32/src/wcs/custom" -# "libraries/Firebase-ESP32/src/wcs/esp32" -#) - +# Install new lib +if [ ! -z $n_param ]; then + INSTALL_TARGET="" + if [ -z $l_param ]; then + # If local path for project is not supplied - use as INSTALL_TARGET Arduino libraries path + INSTALL_TARGET=$($ARDUINO_LIBS_PATH/$(basename "$n_param")) + else + INSTALL_TARGET=$l_param/components/$(basename "$n_param") + fi + + # clone the new lib + echo "Cloning: git clone --recursive $n_param $INSTALL_TARGET" + git clone --recursive $n_param $INSTALL_TARGET + LIBRARY=$INSTALL_TARGET +fi + +# Copy existing lib to local project +if [[ ! -z $e_param ]] && [[ ! -z $l_param ]]; then + echo "Copy from $e_param to $l_param" + echo "cp -r $e_param $l_param/components/$(basename "$e_param")" + cp -r $e_param $l_param/components/$(basename "$e_param") + LIBRARY=$l_param/components/$(basename "$e_param") +fi + +# If Local target was supplied use that path, otherwise use Arduino path +#if [ ! -z $l_param ]; then +# echo "DEBUG: Local lib path" +# if [ ! -z $e_param ]; then +# echo "DEBUG: from existing" +# LIBRARY=$l_param/components/$(basename "$e_param") +# elif [ ! -z $n_param ]; then +# echo "DEBUG: from new" + #LIBRARY=$INSTALL_TARGET +# fi +#else +# echo "DEBUG: Arduino lib path" +# LIBRARY=$($ARDUINO_LIBS_PATH/$(basename "$n_param")) +#fi + +echo "DEBUG: LIBRARY: $LIBRARY" + + +if [ -z LIBRARY ]; then + echo "ERROR: No library -e" >&2 + exit 1 +fi + +# Generate CMakeLists.txt + +# 1. get the source list: +FILES=$(find $LIBRARY -name '*.c' -o -name '*.cpp' | xargs -I{} basename {}) +echo "DEBUG: source files:" +echo "$FILES" + +rm $LIBRARY/CMakeLists.txt # Fresh start +echo "idf_component_register(SRCS $(echo $FILES | sed -e 's/ /" "/g' | sed -e 's/^/"/' -e 's/$/"/')" >> $LIBRARY/CMakeLists.txt +echo " INCLUDE_DIRS \".\"" >> $LIBRARY/CMakeLists.txt +echo " REQUIRES \"arduino-esp32\"" >> $LIBRARY/CMakeLists.txt +echo " )" >> $LIBRARY/CMakeLists.txt From 1fe617dd7c760185175316589395145c4abd41cf Mon Sep 17 00:00:00 2001 From: Tomas Pilny Date: Thu, 27 Apr 2023 11:08:32 +0200 Subject: [PATCH 5/5] Finished script --- tools/add_lib.sh | 72 +++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 38 deletions(-) diff --git a/tools/add_lib.sh b/tools/add_lib.sh index db77c2089f2..4ec73c4f7f7 100755 --- a/tools/add_lib.sh +++ b/tools/add_lib.sh @@ -2,7 +2,7 @@ HELP="This script help to add library when using arduino-esp32 as an ESP-IDF component The script accepts up to three arguments: -n NEW: URL address to new library on GIThub (cannot be combined with -e) --l LOCAL: Path to the project where the library should be placed locally +-l LOCAL: Path to the project where the library should be placed locally (must be paired with -e or -n) -e EXISTING: path to existing libary- this will simply skip the download (cannot be combined with -n) Examples: @@ -55,16 +55,19 @@ while getopts "he:l:n:" opt; do esac done -# Debug Print the parameters -echo "e_param: $e_param" -echo "l_param: $l_param" -echo "n_param: $n_param" - # No parameter check -if [[ -z "$e_param" ]] && [[ -z "$e_param" ]] && [[ -z "$e_param" ]]; then +if [[ -z "$e_param" ]] && [[ -z "$l_param" ]] && [[ -z "$n_param" ]]; then echo "Error: No parameters" >&2 echo "$HELP" exit 1 +fi + +# Only local path check (not permitted) +if [[ -z "$e_param" ]] && [[ ! -z "$l_param" ]] && [[ -z "$n_param" ]]; then + echo "Error: -l parameter must be paired with -e or -n" >&2 + echo "$HELP" + exit 1 +fi # Invalid combination check if [[ ! -z $e_param ]] && [[ ! -z $n_param ]]; then @@ -72,31 +75,35 @@ if [[ ! -z $e_param ]] && [[ ! -z $n_param ]]; then echo "$HELP" exit 1 fi -fi # Check existing lib if [[ ! -z "$e_param" ]]; then - echo "DEBUG: existing lib parameter supplied" if [[ ! -d "${e_param/#~/$HOME}" ]]; then # this works! echo "Error: existing library parameter - path does not exist" >&2 exit 1 fi fi -exit # Script is not yet finished and tested - use at your own risk! - LIBRARY="" +# Only existing library was supplied +if [[ ! -z $e_param ]] && [[ -z $l_param ]] && [[ -z $n_param ]]; then + LIBRARY=$e_param +fi + # Install new lib if [ ! -z $n_param ]; then INSTALL_TARGET="" if [ -z $l_param ]; then # If local path for project is not supplied - use as INSTALL_TARGET Arduino libraries path - INSTALL_TARGET=$($ARDUINO_LIBS_PATH/$(basename "$n_param")) + INSTALL_TARGET=$ARDUINO_LIBS_PATH/$(basename "$n_param") else INSTALL_TARGET=$l_param/components/$(basename "$n_param") + if [ ! -d "$l_param/components" ]; then + echo "Folder components does not exist yet: mkdir -p "$l_param/components"" + mkdir -p "$l_param/components" + fi fi - # clone the new lib echo "Cloning: git clone --recursive $n_param $INSTALL_TARGET" git clone --recursive $n_param $INSTALL_TARGET @@ -105,43 +112,32 @@ fi # Copy existing lib to local project if [[ ! -z $e_param ]] && [[ ! -z $l_param ]]; then + if [ ! -d "$l_param/components" ]; then + echo "Folder components does not exist yet: mkdir -p "$l_param/components"" + mkdir -p "$l_param/components" + fi echo "Copy from $e_param to $l_param" echo "cp -r $e_param $l_param/components/$(basename "$e_param")" cp -r $e_param $l_param/components/$(basename "$e_param") LIBRARY=$l_param/components/$(basename "$e_param") fi -# If Local target was supplied use that path, otherwise use Arduino path -#if [ ! -z $l_param ]; then -# echo "DEBUG: Local lib path" -# if [ ! -z $e_param ]; then -# echo "DEBUG: from existing" -# LIBRARY=$l_param/components/$(basename "$e_param") -# elif [ ! -z $n_param ]; then -# echo "DEBUG: from new" - #LIBRARY=$INSTALL_TARGET -# fi -#else -# echo "DEBUG: Arduino lib path" -# LIBRARY=$($ARDUINO_LIBS_PATH/$(basename "$n_param")) -#fi - -echo "DEBUG: LIBRARY: $LIBRARY" - - -if [ -z LIBRARY ]; then - echo "ERROR: No library -e" >&2 + +if [ -z "$LIBRARY" ]; then + echo "ERROR: No library path" >&2 exit 1 fi -# Generate CMakeLists.txt - # 1. get the source list: FILES=$(find $LIBRARY -name '*.c' -o -name '*.cpp' | xargs -I{} basename {}) -echo "DEBUG: source files:" -echo "$FILES" -rm $LIBRARY/CMakeLists.txt # Fresh start +# Fresh start +if [ -f $LIBRARY/CMakeLists.txt ]; then + rm $LIBRARY/CMakeLists.txt + touch $LIBRARY/CMakeLists.txt +fi + +# Generate CMakeLists.txt echo "idf_component_register(SRCS $(echo $FILES | sed -e 's/ /" "/g' | sed -e 's/^/"/' -e 's/$/"/')" >> $LIBRARY/CMakeLists.txt echo " INCLUDE_DIRS \".\"" >> $LIBRARY/CMakeLists.txt echo " REQUIRES \"arduino-esp32\"" >> $LIBRARY/CMakeLists.txt