Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/run-testsuite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ jobs:
paths: "**/test-results.xml"

- name: Prepare upload of build artifacts
if: always()
run: |
echo "platform=${{ inputs.platform }}".replace(":", "-") >> $env:GITHUB_ENV

Expand Down
6 changes: 4 additions & 2 deletions toolchain/Templates/ArduinoLibraryCMakeLists.txt.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
cmake_minimum_required(VERSION ${CMAKE_VERSION})
project(${_libname} VERSION 0.1 LANGUAGES ASM C CXX)
cmake_minimum_required(VERSION ${CMAKE_MINIMUM_REQUIRED_VERSION})
project(${_libname} LANGUAGES ASM C CXX)

message(STATUS "Configuring ${_libname} for ${ARDUINO_BOARD}")

add_library(
${_libname} STATIC
Expand Down
122 changes: 85 additions & 37 deletions toolchain/arduino-cli-toolchain.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ function(__arduino_property_to_variable PROPERTY_NAME OUTPUT_VARIABLE)
set(_host_variable "${_name}_${_host_suffix}")

# Must check for empty _host_suffix because it will be empty initially
# while filling the alias variables in __arduino_find_board_details().
# while filling the alias variables in __arduino_find_properties().
if (_host_suffix AND DEFINED "${_host_variable}")
set("${OUTPUT_VARIABLE}" "${_host_variable}" PARENT_SCOPE)
else()
Expand Down Expand Up @@ -499,7 +499,7 @@ endfunction()
#
# See `arduino_get_property()` and `__arduino_property_to_variable()`.
# ----------------------------------------------------------------------------------------------------------------------
function(__arduino_find_board_details MODE)
function(__arduino_find_properties MODE)
if (MODE STREQUAL UNEXPANDED) # <---------------------------------------------------------- parse function arguments
set(_property_mode UNEXPANDED)
elseif (MODE STREQUAL EXPANDED)
Expand Down Expand Up @@ -696,52 +696,80 @@ endfunction()
# if not hundreds of out-of-tree sources the library is built separately and later gets pulled as IMPORT library.
# ----------------------------------------------------------------------------------------------------------------------
function(__arduino_add_import_library NAME SOURCE_DIR) # [SOURCE_DIR...]
string(TOUPPER "ARDUINO_${NAME}" _prefix)

set(_libname "Arduino${NAME}")
set(_target "Arduino::${NAME}")

set(_library_binary_dir "${CMAKE_BINARY_DIR}/Arduino/${NAME}")
set(_library_source_dir "${CMAKE_BINARY_DIR}/ArduinoFiles/${NAME}")
set(_library_template "${ARDUINO_TOOLCHAIN_DIR}/Templates/ArduinoLibraryCMakeLists.txt.in")
set(_library_filepath "${_library_binary_dir}/lib${_libname}.a")
if (${_prefix}_FILEPATH) # <--------------------------------- check if there already is a version that can be reused
message(STATUS "Using ${_target} from ${${_prefix}_FILEPATH}")

set(_library_directories "${SOURCE_DIR}" ${ARGN})
list(FILTER _library_directories EXCLUDE REGEX "^ *\$")
list(REMOVE_DUPLICATES _library_directories)
list(SORT _library_directories)
set(_library_binary_dir "${${_prefix}_BINARY_DIR}")
set(_library_source_dir "${${_prefix}_BINARY_DIR}")
set(_library_directories "${${_prefix}_INCLUDE_DIRS}")
set(_library_filepath "${${_prefix}_FILEPATH}")
else() # <--------------------------------------------------------------------------------------- build from scratch
set(_library_binary_dir "${CMAKE_BINARY_DIR}/Arduino/${NAME}")
set(_library_source_dir "${CMAKE_BINARY_DIR}/ArduinoFiles/${NAME}")
set(_library_filepath "${_library_binary_dir}/lib${_libname}.a")

__arduino_collect_source_files(_library_sources ${_library_directories}) # <----- collect the library's source files
message(STATUS "Building ${_target} at ${_library_binary_dir}")

list(LENGTH _library_sources _source_file_count)
list(LENGTH _library_directories _source_dir_count)
message(STATUS " ${_source_file_count} source files found for ${_target} in ${_source_dir_count} directories")
set(_library_directories "${SOURCE_DIR}" ${ARGN}) # <---------------- normalize the library's source directories
list(FILTER _library_directories EXCLUDE REGEX "^ *\$")
list(REMOVE_DUPLICATES _library_directories)

list(JOIN _library_sources "\"\n \"" _quoted_library_sources) # <--------- prepare CMake to build out of tree
list(JOIN _library_directories "\"\n \"" _quoted_library_directories)
configure_file("${_library_template}" "${_library_source_dir}/CMakeLists.txt")
__arduino_collect_source_files(_library_sources ${_library_directories}) # <--------- find the library's sources

add_custom_command(
OUTPUT "${_library_binary_dir}/CMakeCache.txt"
DEPENDS "${_library_template}" "${CMAKE_CURRENT_LIST_FILE}"
COMMENT "Configuring ${_target} library"
WORKING_DIRECTORY "${_library_binary_dir}"
list(LENGTH _library_sources _source_file_count)
message(STATUS " ${_source_file_count} source files found for ${_target}")

list(JOIN _library_sources "\"\n \"" _quoted_library_sources) # <----- prepare CMake to build out of tree
list(JOIN _library_directories "\"\n \"" _quoted_library_directories)

COMMAND "${CMAKE_COMMAND}"
set(_library_template "${ARDUINO_TOOLCHAIN_DIR}/Templates/ArduinoLibraryCMakeLists.txt.in")
configure_file("${_library_template}" "${_library_source_dir}/CMakeLists.txt")

add_custom_command(
OUTPUT "${_library_binary_dir}/CMakeCache.txt"
DEPENDS "${_library_template}" "${CMAKE_CURRENT_LIST_FILE}"
COMMENT "Configuring ${_target} library"
WORKING_DIRECTORY "${_library_binary_dir}"

COMMAND "${CMAKE_COMMAND}"
--toolchain "${CMAKE_CURRENT_FUNCTION_LIST_FILE}"
-G "${CMAKE_GENERATOR}" -S "${_library_source_dir}"
-D "__ARDUINO_IMPORTED_TARGET_CACHE=${__ARDUINO_IMPORTED_TARGET_CACHE}"
-D "ARDUINO_BOARD:STRING=${ARDUINO_BOARD}")

add_custom_command(
OUTPUT "${_library_filepath}"
DEPENDS ${_library_sources} "${_library_binary_dir}/CMakeCache.txt"
COMMENT "Building ${_target} library"
WORKING_DIRECTORY "${_library_binary_dir}"
add_custom_command(
OUTPUT "${_library_filepath}"
DEPENDS ${_library_sources} "${_library_binary_dir}/CMakeCache.txt"
COMMENT "Building ${_target} library"
WORKING_DIRECTORY "${_library_binary_dir}"

COMMAND "${CMAKE_COMMAND}" --build "${_library_binary_dir}")
COMMAND "${CMAKE_COMMAND}" --build "${_library_binary_dir}")

add_custom_target("${_libname}_compile" DEPENDS "${_library_filepath}")
add_custom_target("${_libname}_compile" DEPENDS "${_library_filepath}")

set(${_prefix}_BINARY_DIR "${_library_binary_dir}" PARENT_SCOPE) # <------------------- set cache variables
set(${_prefix}_SOURCE_DIR "${_library_source_dir}" PARENT_SCOPE)
set(${_prefix}_FILEPATH "${_library_filepath}" PARENT_SCOPE)
set(${_prefix}_INCLUDE_DIRS "${_library_directories}" PARENT_SCOPE)

file(
APPEND "${__ARDUINO_IMPORTED_TARGET_CACHE}"
"set(${_prefix}_BINARY_DIR \"${_library_binary_dir}\")\n"
"set(${_prefix}_SOURCE_DIR \"${_library_source_dir}\")\n"
"set(${_prefix}_FILEPATH \"${_library_filepath}\")\n"
"set(${_prefix}_INCLUDE_DIRS \"${_library_directories}\")\n")
endif()

add_library("${_target}" STATIC IMPORTED) # <-------------------- define import library for the built static library
add_dependencies("${_target}" "${_libname}_compile")

if (TARGET "${_libname}_compile")
add_dependencies("${_target}" "${_libname}_compile")
endif()

if (NOT NAME STREQUAL "Core")
target_link_libraries("${_target}" INTERFACE Arduino::Core)
Expand All @@ -761,11 +789,22 @@ endfunction()
# Creates an import library for Arduino's core library.
# ----------------------------------------------------------------------------------------------------------------------
function(__arduino_add_arduino_core_library)
set(_library_directories
"${ARDUINO_PROPERTIES_EXPANDED_BUILD_CORE_PATH}"
"${ARDUINO_PROPERTIES_EXPANDED_BUILD_VARIANT_PATH}")
cmake_path(
CONVERT "${ARDUINO_PROPERTIES_EXPANDED_BUILD_CORE_PATH}"
TO_CMAKE_PATH_LIST _core_dirpath
NORMALIZE)

cmake_path(
CONVERT "${ARDUINO_PROPERTIES_EXPANDED_BUILD_VARIANT_PATH}"
TO_CMAKE_PATH_LIST _variant_dirpath
NORMALIZE)

set(_library_directories "${_core_dirpath}" "${_variant_dirpath}")
__arduino_add_import_library(Core ${_library_directories})

foreach(_suffix IN ITEMS BINARY_DIR SOURCE_DIR FILEPATH)
set("ARDUINO_CORE_${_suffix}" "${ARDUINO_CORE_${_suffix}}" PARENT_SCOPE)
endforeach()
endfunction()

# ----------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -976,8 +1015,8 @@ find_program( # <---------------------------------------------------------------
[HKLM/SOFTWARE/Arduino CLI;InstallDir]
"$ENV{PROGRAMFILES}/Arduino CLI")

__arduino_find_board_details(EXPANDED) # <----------------------------------- collect properties and installed libraries
__arduino_find_board_details(UNEXPANDED)
__arduino_find_properties(EXPANDED) # <-------------------------------------- collect properties and installed libraries
__arduino_find_properties(UNEXPANDED)
__arduino_find_libraries()

find_program( # <----------------------------------------------------------------------- find ctags from Arduino runtime
Expand Down Expand Up @@ -1038,14 +1077,23 @@ list( # <-----------------------------------------------------------------------
ARDUINO_BOARD # make it just work
__ARDUINO_PROPERTIES_EXPANDED_CACHE # make it MUCH faster
__ARDUINO_PROPERTIES_UNEXPANDED_CACHE
__ARDUINO_INSTALLED_LIBRARIES_CACHE)
__ARDUINO_INSTALLED_LIBRARIES_CACHE
__ARDUINO_IMPORTED_TARGET_CACHE)

set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) # try_compile() doesn't provide setup() and loop()

set(CMAKE_USER_MAKE_RULES_OVERRIDE # <------------------ align object and library filenames with Arduino for convenience
"${ARDUINO_TOOLCHAIN_DIR}/Arduino/RulesOverride.cmake")

if (CMAKE_PARENT_LIST_FILE MATCHES "CMakeSystem\\.cmake$") # <----------------- define additonal API, additional targets
if (__ARDUINO_IMPORTED_TARGET_CACHE)
message(STATUS "Using library cache from ${__ARDUINO_IMPORTED_TARGET_CACHE}")
include("${__ARDUINO_IMPORTED_TARGET_CACHE}")
else()
set(__ARDUINO_IMPORTED_TARGET_CACHE "${CMAKE_BINARY_DIR}/ArduinoFiles/ArduinoLibraries.cmake")
file(WRITE "${__ARDUINO_IMPORTED_TARGET_CACHE}" "# Generated from toolchain\n")
endif()

if (NOT CMAKE_PROJECT_NAME STREQUAL ArduinoCore) # FIXME Rather check for __ARDUINO_CORE_FILEPATH
__arduino_add_arduino_core_library()
endif()
Expand Down