8000 Switch to convert to components and add example · realthunder/arduino-esp32@72b4eb6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 72b4eb6

Browse files
committed
Switch to convert to components and add example
1 parent de831e4 commit 72b4eb6

File tree

8 files changed

+233
-51
lines changed

8 files changed

+233
-51
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
build
2+
.cache
3+
components
4+
managed_components
5+
dependencies.lock
6+
sdkconfig
7+
sdkconfig.old
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# For more information about build system see
2+
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
3+
# The following five lines of boilerplate have to be in your project's
4+
# CMakeLists in this exact order for cmake to work correctly
5+
cmake_minimum_required(VERSION 3.17)
6+
7+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
8+
project(Hello_External_Libs)
9+
10+
# Download BufferUtils library
11+
include(FetchContent)
12+
FetchContent_Populate(BufferUtils
13+
URL https://github.com/bakercp/BufferUtils/archive/refs/tags/3.0.0.zip
14+
SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/components/BufferUtils
15+
)
16+
17+
# Convert the Arduino libraries that are missing 'CMakeLists.txt' into IDF components
18+
convert_arduino_libraries_to_components()
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 |
2+
| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- |
3+
4+
# _Hello world example_
5+
6+
This is the simplest buildable example made to be used as a template for new projects running Arduino-esp32 as an ESP-IDF component.
7+
See [Arduino-esp32](https://components.espressif.com/components/espressif/arduino-esp32) in ESP Registry.
8+
9+
## How to use example
10+
11+
To create a ESP-IDF project from this example with the latest release of Arduino-esp32, you can simply run command: `idf.py create-project-from-example "espressif/arduino-esp32:hello_world"`.
12+
ESP-IDF will download all dependencies needed from the component registry and setup the project for you.
13+
14+
If you want to use cloned Arduino-esp32 repository, you can build this example directly.
15+
Go to the example folder `arduino-esp32/idf_component_examples/Hello_world`.
16+
First you need to comment line 6 `pre_release: true` in examples `/main/idf_component.yml`.
17+
Then just run command: `idf.py build`.
18+
19+
## Example folder contents
20+
21+
The project **Hello_world** contains one source file in C++ language [main.cpp](main/main.cpp). The file is located in folder [main](main).
22+
23+
ESP-IDF projects are built using CMake. The project build configuration is contained in `CMakeLists.txt`
24+
files that provide set of directives and instructions describing the project's source files and targets
25+
(executable, library, or both).
26+
27+
Below is short explanation of remaining files in the project folder.
28+
29+
```
30+
├── CMakeLists.txt
31+
├── main
32+
│   ├── CMakeLists.txt
33+
│ ├── idf_component.yml
34+
│   └── main.cpp
35+
└── README.md This is the file you are currently reading
36+
```
37+
38+
## How to add Arduino libraries
39+
40+
In the project create folder `components/` and clone the library there.
41+
In the library folder create new CMakeLists.txt file, add lines shown below to the file and edit the SRCS to match the library source files.
42+
43+
```
44+
idf_component_register(SRCS "user_library.cpp" "another_source.c"
45+
INCLUDE_DIRS "."
46+
REQUIRES arduino-esp32
47+
)
48+
```
49+
50+
Below is structure of the project folder with the Arduino libraries.
51+
52+
```
53+
├── CMakeLists.txt
54+
├── components
55+
│   ├── user_library
56+
│   │   ├── CMakeLists.txt This needs to be added
57+
│   │   ├── ...
58+
├── main
59+
│   ├── CMakeLists.txt
60+
│ ├── idf_component.yml
61+
│   └── main.cpp
62+
└── README.md This is the file you are currently reading
63+
```
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
idf_component_register(SRCS "main.cpp"
2+
INCLUDE_DIRS ".")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## IDF Component Manager Manifest File
2+
dependencies:
3+
espressif/arduino-esp32:
4+
version: "*"
5+
override_path: "../../../"
6+
pre_release: true
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "Arduino.h"
2+
#include "CircularBuffer.h"
3+
4+
void setup() {
5+
Serial.begin(115200);
6+
7+
uint8_t data[10];
8+
CircularBuffer buffer(data, sizeof(data));
9+
buffer.put(1);
10+
}
11+
12+
void loop() {
13+
Serial.println("Hello world!");
14+
delay(1000);
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#
2+
# Arduino ESP32
3+
#
4+
CONFIG_AUTOSTART_ARDUINO=y
5+
# end of Arduino ESP32
6+
7+
#
8+
# FREERTOS
9+
#
10+
CONFIG_FREERTOS_HZ=1000
11+
# end of FREERTOS
12+
# end of Component config

project_include.cmake

Lines changed: 110 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,124 @@
1-
function(add_arduino_libraries libs_dir)
2-
# Construct the absolute path for libs_dir relative to the CMake project root
3-
set(libs_dir_abs "${CMAKE_SOURCE_DIR}/${libs_dir}")
1+
function(convert_arduino_libraries_to_components)
2+
# Base directory for libraries relative to the project root
3+
set(libs_dir_abs "${CMAKE_SOURCE_DIR}/components")
44

5-
# Verify if libs_dir_abs is a valid directory
65
if(NOT IS_DIRECTORY "${libs_dir_abs}")
7-
message(FATAL_ERROR "The specified libs_dir (${libs_dir_abs}) is not a valid directory.")
8-
return() # Stop processing if the directory is invalid
6+
message(FATAL_ERROR "The components dir (${libs_dir_abs}) is not a valid directory.")
7+
return()
98
endif()
109

11-
idf_component_get_property(arduino_esp32_lib arduino COMPONENT_LIB)
12-
13-
# Loop through each folder in the libs_dir
10+
# Get the name of the arduino component. It is the same as the name of the current directory.
11+
get_filename_component(arduino_component_name ${CMAKE_CURRENT_FUNCTION_LIST_DIR} NAME)
12+
message(STATUS "Arduino component name: ${arduino_component_name}")
13+
14+
# Try to figure out the main component name or skip it
15+
if(IS_DIRECTORY "${CMAKE_SOURCE_DIR}/main")
16+
set(main_component_name "main")
17+
elseif(IS_DIRECTORY "${CMAKE_SOURCE_DIR}/src")
18+
set(main_component_name "src")
19+
else()
20+
set(main_component_name "")
21+
endif()
22+
message(STATUS "Main component name: ${main_component_name}")
23+
1424
file(GLOB children RELATIVE ${libs_dir_abs} ${libs_dir_abs}/*)
1525
foreach(child ${children})
16-
if(IS_DIRECTORY "${libs_dir_abs}/${child}")
17-
# Check for library.properties and src directory
18-
if(EXISTS "${libs_dir_abs}/${child}/library.properties" AND IS_DIRECTORY "${libs_dir_abs}/${child}/src")
19-
message(STATUS "Processing ${child} in ${libs_dir_abs}")
20-
# Read the library.properties file to check for ldflags and precompiled
21-
file(STRINGS "${libs_dir_abs}/${child}/library.properties" properties)
22-
set(found_precompiled FALSE)
23-
foreach(property ${properties})
24-
# Process LD_FLAGS
25-
string(FIND "${property}" "ldflags=" ldflags_pos)
26-
if(NOT ${ldflags_pos} EQUAL -1)
27-
string(REPLACE "ldflags=" "" ldflags_value ${property})
28-
set_property(DIRECTORY APPEND PROPERTY LINK_FLAGS "${ldflags_value}")
29-
message(STATUS "Added linker flags from ${child}: ${ldflags_value}")
30-
endif()
31-
32-
# Process precompiled libraries
33-
string(FIND "${property}" "precompiled=" precompiled_pos)
34-
if(NOT ${precompiled_pos} EQUAL -1)
35-
string(REPLACE "precompiled=" "" precompiled_value ${property})
36-
set(found_precompiled TRUE)
37-
if(precompiled_value STREQUAL "true" OR precompiled_value STREQUAL "full")
38-
file(GLOB lib_files "${libs_dir_abs}/${child}/src/${IDF_TARGET}/lib*.a")
39-
foreach(lib_file ${lib_files})
40-
target_link_libraries(${arduino_esp32_lib} INTERFACE ${lib_file})
41-
message(STATUS "Linked precompiled library for ${child}: ${lib_file}")
42-
endforeach()
26+
set(child_dir "${libs_dir_abs}/${child}")
27+
set(properties_file "${child_dir}/library.properties")
28+
set(src_dir "${child_dir}/src")
29+
set(utility_dir "${child_dir}/utility")
30+
set(component_cmakelist "${child_dir}/CMakeLists.txt")
31+
32+
# Skip .DS_Store, files and folders that do not already contain CMakeLists.txt
33+
if(NOT "${child}" STREQUAL ".DS_Store" AND IS_DIRECTORY "${child_dir}" AND NOT EXISTS "${component_cmakelist}")
34+
# Require library.properties
35+
if(EXISTS "${properties_file}")
36+
# If we have src folder, then handle the new library structure
37+
if(IS_DIRECTORY "${src_dir}")
38+
message(STATUS "Processing 1.5+ compatible library '${child}' in '${libs_dir_abs}'")
39+
# Read properties
40+
set(ldflags "")
41+
set(precompiled "")
42+
file(STRINGS "${properties_file}" properties)
43+
foreach(property ${properties})
44+
string(REGEX MATCH "ldflags=(.*)" _ ${property})
45+
if(CMAKE_MATCH_COUNT GREATER 0)
46+
set(ldflags "${CMAKE_MATCH_1}")
4347
endif()
44-
if(precompiled_value STREQUAL "true")
45-
aux_source_directory("${libs_dir_abs}/${child}/src" src_files)
46-
target_sources(${arduino_esp32_lib} INTERFACE ${src_files})
47-
message(STATUS "Added source files for ${child}")
48+
string(REGEX MATCH "precompiled=(.*)" _ ${property})
49+
if(CMAKE_MATCH_COUNT GREATER 0)
50+
set(precompiled "${CMAKE_MATCH_1}")
4851
endif()
52+
endforeach()
53+
54+
# Sources are not compiled only if precompiled=full and precompiled library for our target exists
55+
file(WRITE ${component_cmakelist} "idf_component_register(SRCS ")
56+
if(NOT precompiled STREQUAL "full" OR NOT EXISTS "${src_dir}/${IDF_TARGET}/lib${child}.a")
57+
file(GLOB_RECURSE src_files "${src_dir}/*.c" "${src_dir}/*.cpp")
58+
foreach(src_file ${src_files})
59+
string(REPLACE "${child_dir}/" "" src_file "${src_file}")
60+
file(APPEND ${component_cmakelist} "\"${src_file}\" ")
61+
endforeach()
62+
else()
63+
file(APPEND ${component_cmakelist} "\"\" ")
4964
endif()
50-
endforeach()
51-
52-
# If no precompiled property found, add all sources
53-
if(NOT found_precompiled)
54-
aux_source_directory("${libs_dir_abs}/${child}/src" src_files)
55-
target_sources(${arduino_esp32_lib} INTERFACE ${src_files})
56-
message(STATUS "Added all source files from ${child} as no precompiled property was specified.")
57-
endif()
65+
file(APPEND ${component_cmakelist} "PRIV_REQUIRES ${arduino_component_name} ${main_component_name})\n")
66+
67+
# If this lib has precompiled libs and they match our target, get them
68+
if((precompiled STREQUAL "true" OR precompiled STREQUAL "full") AND IS_DIRECTORY "${src_dir}/${IDF_TARGET}")
69+
file(GLOB_RECURSE lib_files "${src_dir}/${IDF_TARGET}/lib*.a")
70+
file(APPEND ${component_cmakelist} "\"\")\n")
71+
foreach(lib_file ${lib_files})
72+
string(REPLACE "${child_dir}/" "" lib_file "${lib_file}")
73+
file(APPEND ${component_cmakelist} "set_property(TARGET \${COMPONENT_TARGET} APPEND_STRING PROPERTY INTERFACE_LINK_LIBRARIES \"${lib_file}\")\n")
74+
endforeach()
75+
endif()
76+
77+
# Add src folder to includes
78+
file(APPEND ${component_cmakelist} "target_include_directories(\${COMPONENT_TARGET} PUBLIC \"src\")\n")
5879

59-
target_include_directories(${arduino_esp32_lib} INTERFACE "${libs_dir_abs}/${child}/src")
60-
message(STATUS "Added include directory for ${child}")
80+
# Add any custom LD_FLAGS, if defined
81+
if(NOT "${ldflags}" STREQUAL "")
82+
file(APPEND ${component_cmakelist} "target_link_libraries(\${COMPONENT_TARGET} INTERFACE \"${ldflags}\")\n")
83+
endif()
84+
85+
message(STATUS "Created IDF component for ${child}")
86+
else()
87+
message(STATUS "Processing 1.0 compatible library '${child}' in '${libs_dir_abs}'")
88+
file(WRITE ${component_cmakelist} "idf_component_register(SRCS ")
89+
90+
# Add sources from the root folder
91+
file(GLOB src_files "${child_dir}/*.c" "${child_dir}/*.cpp")
92+
foreach(src_file ${src_files})
93+
string(REPLACE "${child_dir}/" "" src_file "${src_file}")
94+
file(APPEND ${component_cmakelist} "\"${src_file}\" ")
95+
endforeach()
96+
97+
# Add sources from the utility folder if it exists
98+
if(IS_DIRECTORY "${utility_dir}")
99+
file(GLOB utility_files "${utility_dir}/*.c" "${utility_dir}/*.cpp")
100+
foreach(utility_file ${utility_files})
101+
string(REPLACE "${child_dir}/" "" utility_file "${utility_file}")
102+
file(APPEND ${component_cmakelist} "\"${utility_file}\" ")
103+
endforeach()
104+
endif()
105+
106+
# Require arduino and main components to suceed in compilation
107+
file(APPEND ${component_cmakelist} "PRIV_REQUIRES ${arduino_component_name} ${main_component_name})\n")
108+
109+
# Add root folder to includes
110+
file(APPEND ${component_cmakelist} "target_include_directories(\${COMPONENT_TARGET} PUBLIC \".\"")
111+
112+
# Add the utility folder to includes if it exists
113+
if(IS_DIRECTORY "${utility_dir}")
114+
file(APPEND ${component_cmakelist} " \"utility\"")
115+
endif()
116+
117+
file(APPEND ${component_cmakelist} ")\n")
118+
message(STATUS "Created IDF component for ${child}")
119+
endif()
61120
else()
62-
message(WARNING "Skipped ${child}: library.properties missing or src directory not found")
121+
message(STATUS "Skipped ${child}: Required 'library.properties' not found")
63122
endif()
64123
endif()
65124
endforeach()

0 commit comments

Comments
 (0)
0