|
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") |
4 | 4 |
|
5 |
| - # Verify if libs_dir_abs is a valid directory |
6 | 5 | 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() |
9 | 8 | endif()
|
10 | 9 |
|
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 | + |
14 | 24 | file(GLOB children RELATIVE ${libs_dir_abs} ${libs_dir_abs}/*)
|
15 | 25 | 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}") |
43 | 47 | 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}") |
48 | 51 | 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} "\"\" ") |
49 | 64 | 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") |
58 | 79 |
|
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() |
61 | 120 | else()
|
62 |
| - message(WARNING "Skipped ${child}: library.properties missing or src directory not found") |
| 121 | + message(STATUS "Skipped ${child}: Required 'library.properties' not found") |
63 | 122 | endif()
|
64 | 123 | endif()
|
65 | 124 | endforeach()
|
|
0 commit comments