@@ -493,73 +493,99 @@ endfunction()
493493# Internal utility functions that find Arduino components and configurations.
494494# ======================================================================================================================
495495
496+ set (__ARDUINO_PROPERTIES_EXPANDED_DESCRIPTION "expanded build properties" )
497+ set (__ARDUINO_PROPERTIES_EXPANDED_FILENAME "properties-expanded.txt" )
498+ set (__ARDUINO_PROPERTIES_EXPANDED_COMMAND board details "--fqbn=${ARDUINO_BOARD} " --format=text --show-properties=expanded)
499+
500+ set (__ARDUINO_PROPERTIES_UNEXPANDED_DESCRIPTION "unexpanded build properties" )
501+ set (__ARDUINO_PROPERTIES_UNEXPANDED_FILENAME "properties-unexpanded.txt" )
502+ set (__ARDUINO_PROPERTIES_UNEXPANDED_COMMAND board details "--fqbn=${ARDUINO_BOARD} " --format=text --show-properties=unexpanded)
503+
504+ set (__ARDUINO_INSTALLED_LIBRARIES_DESCRIPTION "installed Arduino libraries" )
505+ set (__ARDUINO_INSTALLED_LIBRARIES_FILENAME "libraries.json" )
506+ set (__ARDUINO_INSTALLED_LIBRARIES_COMMAND lib list "--fqbn=${ARDUINO_BOARD} " --format=json)
507+
496508# ----------------------------------------------------------------------------------------------------------------------
497- # Collects build properties for the current board from arduino-cli,
498- # and stores them in prefixed CMake variables.
499- #
500- # See `arduino_get_property()` and `__arduino_property_to_variable()`.
509+ # Reads information from `arduino-cli` into `__ARDUINO_${TOPIC}` and tries to cache it.
501510# ----------------------------------------------------------------------------------------------------------------------
502- function (__arduino_find_properties MODE)
503- if (MODE STREQUAL UNEXPANDED) # <---------------------------------------------------------- parse function arguments
504- set (_property_mode UNEXPANDED)
505- elseif (MODE STREQUAL EXPANDED)
506- set (_property_mode)
507- else ()
508- message (FATAL_ERROR "Unsupported mode: ${MODE} " )
511+ function (__arduino_read_cached_setting TOPIC)
512+ if (NOT DEFINED __ARDUINO_${TOPIC} _DESCRIPTION
513+ OR NOT DEFINED __ARDUINO_${TOPIC} _FILENAME
514+ OR NOT DEFINED __ARDUINO_${TOPIC} _COMMAND)
515+ message (FATAL_ERROR "Invalid topic: ${TOPIC} " )
509516 return ()
510517 endif ()
511518
512- string (TOLOWER " ${MODE} " _mode )
519+ set (_use_cache YES )
513520
514- set (_use_property_cache YES ) # <----------------------------------------- figure out if a cached version can be used
515- set (_cachefile_variable "__ARDUINO_PROPERTIES_${MODE} _CACHE" )
516-
517- if (DEFINED "${_cachefile_variable} " )
518- set (_arduino_cache_filepath "${${_cachefile_variable} }" )
519- set (_cmake_dump_filepath) # do not dump already cached variables
521+ if (__ARDUINO_${TOPIC} _CACHE)
522+ set (_cache_filepath "${__ARDUINO_${TOPIC} _CACHE}" )
520523 else ()
521- set (_arduino_cache_filepath "${CMAKE_BINARY_DIR} /ArduinoFiles/properties-${_mode} .txt" )
522- set (_cmake_dump_filepath "${CMAKE_BINARY_DIR} /ArduinoFiles/properties-${_mode} .cmake" )
524+ set (_cache_filepath "${CMAKE_BINARY_DIR} /ArduinoFiles/${__ARDUINO_${TOPIC} _FILENAME}" )
523525
524- if (NOT EXISTS "${_arduino_cache_filepath } "
526+ if (NOT EXISTS "${_cache_filepath } "
525527 OR NOT EXISTS "${CMAKE_BINARY_DIR} /CMakeCache.txt"
526- OR NOT "${_arduino_cache_filepath } " IS_NEWER_THAN "${CMAKE_BINARY_DIR} /CMakeCache.txt" )
527- set (_use_property_cache NO )
528+ OR NOT "${_cache_filepath } " IS_NEWER_THAN "${CMAKE_BINARY_DIR} /CMakeCache.txt" )
529+ set (_use_cache NO )
528530 endif ()
529531 endif ()
530532
531- set ("${_cachefile_variable} " "${_arduino_cache_filepath} " PARENT_SCOPE)
533+ if (_use_cache) # <------------------------------------------------------------------ try to read cached information
534+ message (STATUS "Reading ${__ARDUINO_${TOPIC} _DESCRIPTION} from ${_cache_filepath} " )
535+ set ("__ARDUINO_${TOPIC} _ORIGIN" cache PARENT_SCOPE)
532536
533- if (_use_property_cache) # <---------------------------------------------------------- try to read cached properties
534- message (STATUS "Reading ${_mode} build properties from ${_arduino_cache_filepath} " )
535- file (READ "${_arduino_cache_filepath} " _properties)
537+ file (READ "${_cache_filepath} " _content)
536538 else () # <------------------------------------------------------------------------------------- run arduino-cli tool
537- message (STATUS "Running android-cli to read ${_mode} build properties..." )
539+ message (STATUS "Running android-cli to read ${__ARDUINO_${TOPIC} _DESCRIPTION}" )
540+ set ("__ARDUINO_${TOPIC} _ORIGIN" cli PARENT_SCOPE)
538541
539542 execute_process (
540- COMMAND "${ARDUINO_CLI_EXECUTABLE} "
541- board details "--fqbn=${ARDUINO_BOARD} "
542- --show-properties=${_mode} --format=text
543+ COMMAND "${ARDUINO_CLI_EXECUTABLE} " ${__ARDUINO_${TOPIC} _COMMAND}
543544
544545 ENCODING UTF-8
545546 COMMAND_ERROR_IS_FATAL ANY
546547 OUTPUT_STRIP_TRAILING_WHITESPACE
547- OUTPUT_VARIABLE _properties )
548+ OUTPUT_VARIABLE _content )
548549
549- file (WRITE "${_arduino_cache_filepath } " "${_properties } " )
550+ file (WRITE "${_cache_filepath } " "${_content } " )
550551 endif ()
551552
552- if (NOT _property_list)
553- string (REPLACE ";" "\\ ;" _properties "${_properties} " ) # <------------------ split into lines; preserving semicolons
554- string (REGEX REPLACE "[ \t\r ]*\n " ";" _property_list "${_properties} " )
553+ set ("__ARDUINO_${TOPIC} " "${_content} " PARENT_SCOPE)
554+ set ("__ARDUINO_${TOPIC} _CACHE" "${_cache_filepath} " PARENT_SCOPE)
555+ endfunction ()
556+
557+ # ----------------------------------------------------------------------------------------------------------------------
558+ # Collects build properties for the current board from arduino-cli,
559+ # and stores them in prefixed CMake variables.
560+ #
561+ # See `arduino_get_property()` and `__arduino_property_to_variable()`.
562+ # ----------------------------------------------------------------------------------------------------------------------
563+ function (__arduino_find_properties MODE)
564+ if (MODE STREQUAL UNEXPANDED) # <---------------------------------------------------------- parse function arguments
565+ set (_property_mode UNEXPANDED)
566+ elseif (MODE STREQUAL EXPANDED)
567+ set (_property_mode)
568+ else ()
569+ message (FATAL_ERROR "Unsupported mode: ${MODE} " )
570+ return ()
555571 endif ()
556572
557- if (NOT _use_property_cache)
573+ __arduino_read_cached_setting("PROPERTIES_${MODE} " )
574+ set (_properties "${__ARDUINO_PROPERTIES_${MODE} }" )
575+
576+ string (REPLACE ";" "\\ ;" _properties "${_properties} " ) # <------------------ split into lines; preserving semicolons
577+ string (REGEX REPLACE "[ \t\r ]*\n " ";" _property_list "${_properties} " )
578+
579+ if (__ARDUINO_PROPERTIES_${MODE} _ORIGIN STREQUAL "cli" )
558580 list (LENGTH _property_list _count)
559581 message (STATUS " ${_count} properties found" )
582+
583+ string (REPLACE ".txt" ".cmake" _cmake_cache_filepath "${__ARDUINO_PROPERTIES_${MODE} _CACHE}" )
584+ else ()
585+ unset (_cmake_cache_filepath) # do not dump already cached variables
560586 endif ()
561587
562- set (_variable_dump "" )
588+ unset (_cmake_variable_cache )
563589
564590 foreach (_property IN LISTS _property_list) # <--------------------------------- set CMake variables from properties
565591 if (_property MATCHES "([^=]+)=(.*)" )
@@ -573,63 +599,41 @@ function(__arduino_find_properties MODE)
573599 endif ()
574600
575601 set ("${_variable} " "${_property_value} " PARENT_SCOPE)
576- string (APPEND _property_dump "${_variable} =${_property_value} \n " )
602+
603+ string (REPLACE "\\ " "\\\\ " _escaped_value "${_property_value} " )
604+ string (REPLACE "\" " "\\\" " _escaped_value "${_escaped_value} " )
605+ string (APPEND _cmake_variable_cache "set(${_variable} \" ${_escaped_value} \" )\n " )
577606 elseif (_property)
578607 message (FATAL_ERROR "Unexpected output from arduino-cli tool: ${_property} " )
579608 return ()
580609 endif ()
581610 endforeach ()
582611
583- if (" ${_cmake_dump_filepath} " ) # <---------------------------------------------- only dump after running arduino-cli
584- file (WRITE "${_cmake_dump_filepath } " "${_property_dump } " )
612+ if (_cmake_cache_filepath ) # <------------------------------------------------ -- only dump after running arduino-cli
613+ file (WRITE "${_cmake_cache_filepath } " "${_cmake_variable_cache } " )
585614 endif ()
615+
616+ set ("__ARDUINO_PROPERTIES_${MODE} " "${__ARDUINO_PROPERTIES_${MODE} }" PARENT_SCOPE)
617+ set ("__ARDUINO_PROPERTIES_${MODE} _CACHE" "${__ARDUINO_PROPERTIES_${MODE} _CACHE}" PARENT_SCOPE)
586618endfunction ()
587619
588620# ----------------------------------------------------------------------------------------------------------------------
589621# Collects available libraries for the current board from arduino-cli.
590622# ----------------------------------------------------------------------------------------------------------------------
591623function (__arduino_find_libraries)
592- set (_use_library_cache YES ) # <------------------------------------------ figure out if a cached version can be used
593-
594- if (__ARDUINO_INSTALLED_LIBRARIES_CACHE)
595- set (_arduino_cache_filepath "${__ARDUINO_INSTALLED_LIBRARIES_CACHE} " )
596- else ()
597- set (_arduino_cache_filepath "${CMAKE_BINARY_DIR} /ArduinoFiles/libraries.json" )
598-
599- if (NOT EXISTS "${_arduino_cache_filepath} "
600- OR NOT EXISTS "${CMAKE_BINARY_DIR} /CMakeCache.txt"
601- OR NOT "${_arduino_cache_filepath} " IS_NEWER_THAN "${CMAKE_BINARY_DIR} /CMakeCache.txt" )
602- set (_use_library_cache NO )
603- endif ()
604- endif ()
605-
606- if (_use_library_cache) # <------------------------------------------------------------ try to read cached libraries
607- message (STATUS "Reading installed Arduino libraries from ${_arduino_cache_filepath} " )
608- file (READ "${_arduino_cache_filepath} " _json)
609- else () # <------------------------------------------------------------------------------------- run arduino-cli tool
610- message (STATUS "Running android-cli to read installed Arduino libraries..." )
611-
612- execute_process (
613- COMMAND "${ARDUINO_CLI_EXECUTABLE} "
614- lib list "--fqbn=${ARDUINO_BOARD} " --format=json
615-
616- ENCODING UTF-8
617- COMMAND_ERROR_IS_FATAL ANY
618- OUTPUT_STRIP_TRAILING_WHITESPACE
619- OUTPUT_VARIABLE _json)
620-
621- file (WRITE "${_arduino_cache_filepath} " "${_json} " )
622- endif ()
624+ __arduino_read_cached_setting(INSTALLED_LIBRARIES)
623625
624- string (JSON _installed_libraries GET "${_json} " installed_libraries) # <-------------- parse the library information
626+ string ( # <--------------------------------------------------------------------------- parse the library information
627+ JSON _installed_libraries
628+ GET "${__ARDUINO_INSTALLED_LIBRARIES} " installed_libraries)
625629
626- if (NOT _use_library_cache )
630+ if (__ARDUINO_INSTALLED_LIBRARIES_ORIGIN STREQUAL "cli" )
627631 string (JSON _count LENGTH "${_installed_libraries} " )
628632 message (STATUS " ${_count} libraries found" )
629633 endif ()
630634
631- set (__ARDUINO_INSTALLED_LIBRARIES "${_installed_libraries} " PARENT_SCOPE)
632- set (__ARDUINO_INSTALLED_LIBRARIES_CACHE "${_arduino_cache_filepath } " PARENT_SCOPE)
635+ set (__ARDUINO_INSTALLED_LIBRARIES "${_installed_libraries} " PARENT_SCOPE)
636+ set (__ARDUINO_INSTALLED_LIBRARIES_CACHE "${__ARDUINO_INSTALLED_LIBRARIES_CACHE } " PARENT_SCOPE)
633637endfunction ()
634638
635639# ======================================================================================================================
0 commit comments