|
| 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}") |
| 4 | + |
| 5 | + # Verify if libs_dir_abs is a valid directory |
| 6 | + 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 |
| 9 | + endif() |
| 10 | + |
| 11 | + idf_component_get_property(arduino_esp32_lib arduino COMPONENT_LIB) |
| 12 | + |
| 13 | + # Loop through each folder in the libs_dir |
| 14 | + file(GLOB children RELATIVE ${libs_dir_abs} ${libs_dir_abs}/*) |
| 15 | + 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() |
| 43 | + 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 | + endif() |
| 49 | + 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() |
| 58 | + |
| 59 | + target_include_directories(${arduino_esp32_lib} INTERFACE "${libs_dir_abs}/${child}/src") |
| 60 | + message(STATUS "Added include directory for ${child}") |
| 61 | + else() |
| 62 | + message(WARNING "Skipped ${child}: library.properties missing or src directory not found") |
| 63 | + endif() |
| 64 | + endif() |
| 65 | + endforeach() |
| 66 | +endfunction() |
0 commit comments