@@ -202,7 +202,7 @@ function(__arduino_property_to_variable PROPERTY_NAME OUTPUT_VARIABLE)
202202 set (_host_variable "${_name} _${_host_suffix} " )
203203
204204 # Must check for empty _host_suffix because it will be empty initially
205- # while filling the alias variables in __arduino_find_board_details ().
205+ # while filling the alias variables in __arduino_find_properties ().
206206 if (_host_suffix AND DEFINED "${_host_variable} " )
207207 set ("${OUTPUT_VARIABLE} " "${_host_variable} " PARENT_SCOPE)
208208 else ()
@@ -499,7 +499,7 @@ endfunction()
499499#
500500# See `arduino_get_property()` and `__arduino_property_to_variable()`.
501501# ----------------------------------------------------------------------------------------------------------------------
502- function (__arduino_find_board_details MODE)
502+ function (__arduino_find_properties MODE)
503503 if (MODE STREQUAL UNEXPANDED) # <---------------------------------------------------------- parse function arguments
504504 set (_property_mode UNEXPANDED)
505505 elseif (MODE STREQUAL EXPANDED)
@@ -696,52 +696,80 @@ endfunction()
696696# if not hundreds of out-of-tree sources the library is built separately and later gets pulled as IMPORT library.
697697# ----------------------------------------------------------------------------------------------------------------------
698698function (__arduino_add_import_library NAME SOURCE_DIR) # [SOURCE_DIR...]
699+ string (TOUPPER "ARDUINO_${NAME} " _prefix)
700+
699701 set (_libname "Arduino${NAME} " )
700702 set (_target "Arduino::${NAME} " )
701703
702- set (_library_binary_dir "${CMAKE_BINARY_DIR} /Arduino/${NAME} " )
703- set (_library_source_dir "${CMAKE_BINARY_DIR} /ArduinoFiles/${NAME} " )
704- set (_library_template "${ARDUINO_TOOLCHAIN_DIR} /Templates/ArduinoLibraryCMakeLists.txt.in" )
705- set (_library_filepath "${_library_binary_dir} /lib${_libname} .a" )
704+ if (${_prefix} _FILEPATH) # <--------------------------------- check if there already is a version that can be reused
705+ message (STATUS "Using ${_target} from ${${_prefix} _FILEPATH}" )
706706
707- set (_library_directories "${SOURCE_DIR} " ${ARGN} )
708- list (FILTER _library_directories EXCLUDE REGEX "^ *\$ " )
709- list (REMOVE_DUPLICATES _library_directories)
710- list (SORT _library_directories)
707+ set (_library_binary_dir "${${_prefix} _BINARY_DIR}" )
708+ set (_library_source_dir "${${_prefix} _BINARY_DIR}" )
709+ set (_library_directories "${${_prefix} _INCLUDE_DIRS}" )
710+ set (_library_filepath "${${_prefix} _FILEPATH}" )
711+ else () # <--------------------------------------------------------------------------------------- build from scratch
712+ set (_library_binary_dir "${CMAKE_BINARY_DIR} /Arduino/${NAME} " )
713+ set (_library_source_dir "${CMAKE_BINARY_DIR} /ArduinoFiles/${NAME} " )
714+ set (_library_filepath "${_library_binary_dir} /lib${_libname} .a" )
711715
712- __arduino_collect_source_files(_library_sources ${_library_directories} ) # <----- collect the library's source files
716+ message ( STATUS "Building ${_target} at ${_library_binary_dir} " )
713717
714- list ( LENGTH _library_sources _source_file_count)
715- list (LENGTH _library_directories _source_dir_count )
716- message ( STATUS " ${_source_file_count} source files found for ${_target} in ${_source_dir_count} directories" )
718+ set (_library_directories " ${SOURCE_DIR} " ${ARGN} ) # <---------------- normalize the library's source directories
719+ list (FILTER _library_directories EXCLUDE REGEX "^ * \$ " )
720+ list ( REMOVE_DUPLICATES _library_directories )
717721
718- list (JOIN _library_sources "\"\n \" " _quoted_library_sources) # <--------- prepare CMake to build out of tree
719- list (JOIN _library_directories "\"\n \" " _quoted_library_directories)
720- configure_file ("${_library_template} " "${_library_source_dir} /CMakeLists.txt" )
722+ __arduino_collect_source_files(_library_sources ${_library_directories} ) # <--------- find the library's sources
721723
722- add_custom_command (
723- OUTPUT " ${_library_binary_dir} /CMakeCache.txt"
724- DEPENDS " ${_library_template} " " ${CMAKE_CURRENT_LIST_FILE} "
725- COMMENT "Configuring ${_target} library"
726- WORKING_DIRECTORY " ${_library_binary_dir} "
724+ list ( LENGTH _library_sources _source_file_count)
725+ message ( STATUS " ${_source_file_count} source files found for ${_target} " )
726+
727+ list (JOIN _library_sources " \"\n \" " _quoted_library_sources) # <----- prepare CMake to build out of tree
728+ list (JOIN _library_directories " \"\n \" " _quoted_library_directories)
727729
728- COMMAND "${CMAKE_COMMAND} "
730+ set (_library_template "${ARDUINO_TOOLCHAIN_DIR} /Templates/ArduinoLibraryCMakeLists.txt.in" )
731+ configure_file ("${_library_template} " "${_library_source_dir} /CMakeLists.txt" )
732+
733+ add_custom_command (
734+ OUTPUT "${_library_binary_dir} /CMakeCache.txt"
735+ DEPENDS "${_library_template} " "${CMAKE_CURRENT_LIST_FILE} "
736+ COMMENT "Configuring ${_target} library"
737+ WORKING_DIRECTORY "${_library_binary_dir} "
738+
739+ COMMAND "${CMAKE_COMMAND} "
729740 --toolchain "${CMAKE_CURRENT_FUNCTION_LIST_FILE} "
730741 -G "${CMAKE_GENERATOR} " -S "${_library_source_dir} "
742+ -D "__ARDUINO_IMPORTED_TARGET_CACHE=${__ARDUINO_IMPORTED_TARGET_CACHE} "
731743 -D "ARDUINO_BOARD:STRING=${ARDUINO_BOARD} " )
732744
733- add_custom_command (
734- OUTPUT "${_library_filepath} "
735- DEPENDS ${_library_sources} "${_library_binary_dir} /CMakeCache.txt"
736- COMMENT "Building ${_target} library"
737- WORKING_DIRECTORY "${_library_binary_dir} "
745+ add_custom_command (
746+ OUTPUT "${_library_filepath} "
747+ DEPENDS ${_library_sources} "${_library_binary_dir} /CMakeCache.txt"
748+ COMMENT "Building ${_target} library"
749+ WORKING_DIRECTORY "${_library_binary_dir} "
738750
739- COMMAND "${CMAKE_COMMAND} " --build "${_library_binary_dir} " )
751+ COMMAND "${CMAKE_COMMAND} " --build "${_library_binary_dir} " )
740752
741- add_custom_target ("${_libname} _compile" DEPENDS "${_library_filepath} " )
753+ add_custom_target ("${_libname} _compile" DEPENDS "${_library_filepath} " )
754+
755+ set (${_prefix} _BINARY_DIR "${_library_binary_dir} " PARENT_SCOPE) # <------------------- set cache variables
756+ set (${_prefix} _SOURCE_DIR "${_library_source_dir} " PARENT_SCOPE)
757+ set (${_prefix} _FILEPATH "${_library_filepath} " PARENT_SCOPE)
758+ set (${_prefix} _INCLUDE_DIRS "${_library_directories} " PARENT_SCOPE)
759+
760+ file (
761+ APPEND "${__ARDUINO_IMPORTED_TARGET_CACHE} "
762+ "set(${_prefix} _BINARY_DIR \" ${_library_binary_dir} \" )\n "
763+ "set(${_prefix} _SOURCE_DIR \" ${_library_source_dir} \" )\n "
764+ "set(${_prefix} _FILEPATH \" ${_library_filepath} \" )\n "
765+ "set(${_prefix} _INCLUDE_DIRS \" ${_library_directories} \" )\n " )
766+ endif ()
742767
743768 add_library ("${_target} " STATIC IMPORTED ) # <-------------------- define import library for the built static library
744- add_dependencies ("${_target} " "${_libname} _compile" )
769+
770+ if (TARGET "${_libname} _compile" )
771+ add_dependencies ("${_target} " "${_libname} _compile" )
772+ endif ()
745773
746774 if (NOT NAME STREQUAL "Core" )
747775 target_link_libraries ("${_target} " INTERFACE Arduino::Core)
@@ -761,11 +789,22 @@ endfunction()
761789# Creates an import library for Arduino's core library.
762790# ----------------------------------------------------------------------------------------------------------------------
763791function (__arduino_add_arduino_core_library)
764- set (_library_directories
765- "${ARDUINO_PROPERTIES_EXPANDED_BUILD_CORE_PATH} "
766- "${ARDUINO_PROPERTIES_EXPANDED_BUILD_VARIANT_PATH} " )
792+ cmake_path(
793+ CONVERT "${ARDUINO_PROPERTIES_EXPANDED_BUILD_CORE_PATH} "
794+ TO_CMAKE_PATH_LIST _core_dirpath
795+ NORMALIZE)
796+
797+ cmake_path(
798+ CONVERT "${ARDUINO_PROPERTIES_EXPANDED_BUILD_VARIANT_PATH} "
799+ TO_CMAKE_PATH_LIST _variant_dirpath
800+ NORMALIZE)
767801
802+ set (_library_directories "${_core_dirpath} " "${_variant_dirpath} " )
768803 __arduino_add_import_library(Core ${_library_directories} )
804+
805+ foreach (_suffix IN ITEMS BINARY_DIR SOURCE_DIR FILEPATH )
806+ set ("ARDUINO_CORE_${_suffix} " "${ARDUINO_CORE_${_suffix} }" PARENT_SCOPE)
807+ endforeach ()
769808endfunction ()
770809
771810# ----------------------------------------------------------------------------------------------------------------------
@@ -976,8 +1015,8 @@ find_program( # <---------------------------------------------------------------
9761015 [HKLM/SOFTWARE/Arduino CLI;InstallDir]
9771016 "$ENV{PROGRAMFILES} /Arduino CLI" )
9781017
979- __arduino_find_board_details (EXPANDED) # <----------------------------------- collect properties and installed libraries
980- __arduino_find_board_details (UNEXPANDED)
1018+ __arduino_find_properties (EXPANDED) # <--- ----------------------------------- collect properties and installed libraries
1019+ __arduino_find_properties (UNEXPANDED)
9811020__arduino_find_libraries()
9821021
9831022find_program ( # <----------------------------------------------------------------------- find ctags from Arduino runtime
@@ -1038,14 +1077,23 @@ list( # <-----------------------------------------------------------------------
10381077 ARDUINO_BOARD # make it just work
10391078 __ARDUINO_PROPERTIES_EXPANDED_CACHE # make it MUCH faster
10401079 __ARDUINO_PROPERTIES_UNEXPANDED_CACHE
1041- __ARDUINO_INSTALLED_LIBRARIES_CACHE)
1080+ __ARDUINO_INSTALLED_LIBRARIES_CACHE
1081+ __ARDUINO_IMPORTED_TARGET_CACHE)
10421082
10431083set (CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) # try_compile() doesn't provide setup() and loop()
10441084
10451085set (CMAKE_USER_MAKE_RULES_OVERRIDE # <------------------ align object and library filenames with Arduino for convenience
10461086 "${ARDUINO_TOOLCHAIN_DIR} /Arduino/RulesOverride.cmake" )
10471087
10481088if (CMAKE_PARENT_LIST_FILE MATCHES "CMakeSystem\\ .cmake$" ) # <----------------- define additonal API, additional targets
1089+ if (__ARDUINO_IMPORTED_TARGET_CACHE)
1090+ message (STATUS "Using library cache from ${__ARDUINO_IMPORTED_TARGET_CACHE} " )
1091+ include ("${__ARDUINO_IMPORTED_TARGET_CACHE} " )
1092+ else ()
1093+ set (__ARDUINO_IMPORTED_TARGET_CACHE "${CMAKE_BINARY_DIR} /ArduinoFiles/ArduinoLibraries.cmake" )
1094+ file (WRITE "${__ARDUINO_IMPORTED_TARGET_CACHE} " "# Generated from toolchain\n " )
1095+ endif ()
1096+
10491097 if (NOT CMAKE_PROJECT_NAME STREQUAL ArduinoCore) # FIXME Rather check for __ARDUINO_CORE_FILEPATH
10501098 __arduino_add_arduino_core_library()
10511099 endif ()
0 commit comments