Skip to content

Commit 64fa120

Browse files
Add Arduino lib to arduino as IDF component (#8721)
* Iniital commit * Fixed naming of the CMakeLists.txt file in the doc text * Finished the docmentation * Updated, but not yet tested script * Finished script --------- Co-authored-by: Tomas Pilny <tomas.pilny@espressif.com>
1 parent 1a504a6 commit 64fa120

File tree

2 files changed

+229
-0
lines changed

2 files changed

+229
-0
lines changed

docs/source/esp-idf_component.rst

+85
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,88 @@ Compilation Errors
163163
------------------
164164

165165
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 <https://github.com/espressif/arduino-esp32/issues/1142>`_ to roll esp-idf back to a different version.
166+
167+
Adding arduino library
168+
----------------------
169+
170+
There are few approaches:
171+
172+
1. Add global library to ``components/arduino-esp32/libraries/new_library``
173+
2. Add local project library to ``examples/your_project/main/libraries/new_library``
174+
175+
1 Adding global library
176+
***********************
177+
178+
Download the library:
179+
180+
.. code-block:: bash
181+
182+
cd ~/esp/esp-idf/components/arduino-esp32/
183+
git clone --recursive git@github.com:Author/new_library.git libraries/new_library
184+
185+
186+
Edit file ``components/arduino-esp32/CMakeLists.txt``
187+
188+
Get the source file list with shell command:
189+
190+
.. code-block:: bash
191+
192+
find libraries/new_library/src/ -name '*.c' -o -name '*.cpp'
193+
libraries/new_library/src/new_library.cpp
194+
libraries/new_library/src/new_library_extra_file.c
195+
196+
Locate block which starts with ``set(LIBRARY_SRCS`` and copy the list there. Now it should look something like this:
197+
198+
.. code-block:: bash
199+
200+
set(LIBRARY_SRCS
201+
libraries/ArduinoOTA/src/ArduinoOTA.cpp
202+
libraries/AsyncUDP/src/AsyncUDP.cpp
203+
libraries/new_library/src/new_library.cpp
204+
libraries/new_library/src/new_library_extra_file.c
205+
206+
207+
After this add the library path to block which starts with ``set(includedirs``. It should look like this:
208+
209+
.. code-block:: bash
210+
211+
set(includedirs
212+
variants/${CONFIG_ARDUINO_VARIANT}/
213+
cores/esp32/
214+
libraries/ArduinoOTA/src
215+
libraries/AsyncUDP/src
216+
libraries/new_library/src
217+
218+
219+
2 Adding local library
220+
**********************
221+
222+
Download the library:
223+
224+
.. code-block:: bash
225+
226+
cd ~/esp/esp-idf/examples/your_project
227+
mkdir components
228+
git clone --recursive git@github.com:Author/new_library.git components/new_library
229+
230+
Create new CMakeists.txt in the library folder: ``components/new_library/CMakeLists.txt``
231+
232+
.. code-block:: bash
233+
234+
idf_component_register(SRCS "new_library.cpp" "another_source.c"
235+
INCLUDE_DIRS "."
236+
REQUIRES arduino-esp32
237+
)
238+
239+
You can read more about CMakeLists in the IDF documentation regarding the `Build System <https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/build-system.html>`_
240+
241+
Tip
242+
---
243+
244+
If you want to use arduino-esp32 both as an ESP-IDF component and with Arduino IDE you can simply create a symlink:
245+
246+
.. code-block:: bash
247+
248+
ln -s ~/Arduino/hardware/espressif/esp32 ~/esp/esp-idf/components/arduino-esp32
249+
250+
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``

tools/add_lib.sh

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
#!/bin/bash
2+
HELP="This script help to add library when using arduino-esp32 as an ESP-IDF component
3+
The script accepts up to three arguments:
4+
-n NEW: URL address to new library on GIThub (cannot be combined with -e)
5+
-l LOCAL: Path to the project where the library should be placed locally (must be paired with -e or -n)
6+
-e EXISTING: path to existing libary- this will simply skip the download (cannot be combined with -n)
7+
8+
Examples:
9+
./add_lib.sh -n https://github.com/me-no-dev/ESPAsyncWebServer
10+
./add_lib.sh -l ~/esp/esp-idf/examples/your_project
11+
./add_lib.sh -e ~/Arduino/libraries/existing_library
12+
13+
./add_lib.sh -n https://github.com/me-no-dev/ESPAsyncWebServer -l ~/esp/esp-idf/examples/your_project
14+
./add_lib.sh -e ~/Arduino/libraries/existing_library -l ~/esp/esp-idf/examples/your_project"
15+
16+
# Get the directory name where this script is located
17+
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
18+
19+
# Construct the absolute path to libraries folder
20+
ARDUINO_LIBS_PATH="$SCRIPT_DIR/../libraries"
21+
22+
# Define the default values for the parameters
23+
e_param=""
24+
l_param=""
25+
n_param=""
26+
27+
# Parse the command-line arguments using getopts
28+
while getopts "he:l:n:" opt; do
29+
case $opt in
30+
h)
31+
echo "$HELP"
32+
exit 0
33+
;;
34+
e)
35+
#e_param="$OPTARG"
36+
e_param="${OPTARG/#~/$HOME}"
37+
;;
38+
l)
39+
#l_param="$OPTARG"
40+
l_param="${OPTARG/#~/$HOME}"
41+
;;
42+
n)
43+
n_param=$OPTARG
44+
;;
45+
\?)
46+
echo "Invalid option: -$OPTARG" >&2
47+
echo $HELP
48+
exit 1
49+
;;
50+
:)
51+
echo "Option -$OPTARG requires an argument." >&2
52+
echo $HELP
53+
exit 1
54+
;;
55+
esac
56+
done
57+
58+
# No parameter check
59+
if [[ -z "$e_param" ]] && [[ -z "$l_param" ]] && [[ -z "$n_param" ]]; then
60+
echo "Error: No parameters" >&2
61+
echo "$HELP"
62+
exit 1
63+
fi
64+
65+
# Only local path check (not permitted)
66+
if [[ -z "$e_param" ]] && [[ ! -z "$l_param" ]] && [[ -z "$n_param" ]]; then
67+
echo "Error: -l parameter must be paired with -e or -n" >&2
68+
echo "$HELP"
69+
exit 1
70+
fi
71+
72+
# Invalid combination check
73+
if [[ ! -z $e_param ]] && [[ ! -z $n_param ]]; then
74+
echo "ERROR: Cannot combine -n with -e" >&2
75+
echo "$HELP"
76+
exit 1
77+
fi
78+
79+
# Check existing lib
80+
if [[ ! -z "$e_param" ]]; then
81+
if [[ ! -d "${e_param/#~/$HOME}" ]]; then # this works!
82+
echo "Error: existing library parameter - path does not exist" >&2
83+
exit 1
84+
fi
85+
fi
86+
87+
LIBRARY=""
88+
89+
# Only existing library was supplied
90+
if [[ ! -z $e_param ]] && [[ -z $l_param ]] && [[ -z $n_param ]]; then
91+
LIBRARY=$e_param
92+
fi
93+
94+
# Install new lib
95+
if [ ! -z $n_param ]; then
96+
INSTALL_TARGET=""
97+
if [ -z $l_param ]; then
98+
# If local path for project is not supplied - use as INSTALL_TARGET Arduino libraries path
99+
INSTALL_TARGET=$ARDUINO_LIBS_PATH/$(basename "$n_param")
100+
else
101+
INSTALL_TARGET=$l_param/components/$(basename "$n_param")
102+
if [ ! -d "$l_param/components" ]; then
103+
echo "Folder components does not exist yet: mkdir -p "$l_param/components""
104+
mkdir -p "$l_param/components"
105+
fi
106+
fi
107+
# clone the new lib
108+
echo "Cloning: git clone --recursive $n_param $INSTALL_TARGET"
109+
git clone --recursive $n_param $INSTALL_TARGET
110+
LIBRARY=$INSTALL_TARGET
111+
fi
112+
113+
# Copy existing lib to local project
114+
if [[ ! -z $e_param ]] && [[ ! -z $l_param ]]; then
115+
if [ ! -d "$l_param/components" ]; then
116+
echo "Folder components does not exist yet: mkdir -p "$l_param/components""
117+
mkdir -p "$l_param/components"
118+
fi
119+
echo "Copy from $e_param to $l_param"
120+
echo "cp -r $e_param $l_param/components/$(basename "$e_param")"
121+
cp -r $e_param $l_param/components/$(basename "$e_param")
122+
LIBRARY=$l_param/components/$(basename "$e_param")
123+
fi
124+
125+
126+
if [ -z "$LIBRARY" ]; then
127+
echo "ERROR: No library path" >&2
128+
exit 1
129+
fi
130+
131+
# 1. get the source list:
132+
FILES=$(find $LIBRARY -name '*.c' -o -name '*.cpp' | xargs -I{} basename {})
133+
134+
# Fresh start
135+
if [ -f $LIBRARY/CMakeLists.txt ]; then
136+
rm $LIBRARY/CMakeLists.txt
137+
touch $LIBRARY/CMakeLists.txt
138+
fi
139+
140+
# Generate CMakeLists.txt
141+
echo "idf_component_register(SRCS $(echo $FILES | sed -e 's/ /" "/g' | sed -e 's/^/"/' -e 's/$/"/')" >> $LIBRARY/CMakeLists.txt
142+
echo " INCLUDE_DIRS \".\"" >> $LIBRARY/CMakeLists.txt
143+
echo " REQUIRES \"arduino-esp32\"" >> $LIBRARY/CMakeLists.txt
144+
echo " )" >> $LIBRARY/CMakeLists.txt

0 commit comments

Comments
 (0)