Skip to content

Commit

Permalink
Merge pull request #2283 from ddowling/multiple-sketch-support
Browse files Browse the repository at this point in the history
* Support calling build_sketch() multiple times in the CMakeList.txt file
build multiple targets.
Add an include_guard() on build_sketch.cmake to prevent errors if this
file is included multiple time.

* Fix dependency generation error when the sketch ino file is not in the
project base directory.

* Fix sketch_preprocess_sources to make consistent use of the ${CMAKE_CURRENT_BINARY_DIR} on all generated files.
  • Loading branch information
fpistm authored Mar 4, 2024
2 parents c12103b + 076681a commit 7f5d45e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
10 changes: 7 additions & 3 deletions cmake/build_sketch.cmake
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
cmake_minimum_required(VERSION 3.21)

# Only want the variant and library targets to be defined once
include_guard(GLOBAL)

include(sketch_preprocess_sources)
include(convert_file)

include(set_base_arduino_config)

add_subdirectory(${BUILD_VARIANT_PATH} ./variant)
add_subdirectory(${BUILD_CORE_PATH} ./cores/arduino)
add_subdirectory(${BUILD_LIB_PATH} ./libraries)

function(build_sketch)
add_subdirectory(${BUILD_VARIANT_PATH} ./variant)
add_subdirectory(${BUILD_CORE_PATH} ./cores/arduino)
add_subdirectory(${BUILD_LIB_PATH} ./libraries)


cmake_parse_arguments(PARSE_ARGV 0 SKBD "" "TARGET" "SOURCES;DEPENDS")
Expand Down
22 changes: 12 additions & 10 deletions cmake/sketch_preprocess_sources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,31 @@ function(sketch_preprocess_sources)
set(SRCLIST "")
foreach(SRCFILE IN LISTS SPC_SOURCES)
if (${SRCFILE} MATCHES "\.ino$")
# Convert <file>.ino to ${CMAKE_CURRENT_BINARY_DIR}/<file>.ino.{cpp,h}
cmake_path(GET SRCFILE FILENAME SRC_BASE_NAME)
set(SRC_BINARY_BASE_NAME ${CMAKE_CURRENT_BINARY_DIR}/${SRC_BASE_NAME})

configure_file(
${SRCFILE}
${CMAKE_CURRENT_BINARY_DIR}/${SRC_BASE_NAME}.cpp
${SRC_BINARY_BASE_NAME}.cpp
COPYONLY
)

add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${SRC_BASE_NAME}.h
COMMAND ${ARDUINOCTAGS_EXECUTABLE} -u --language-force=c++ -f ${CMAKE_CURRENT_BINARY_DIR}/${SRC_BASE_NAME}.ctags --c++-kinds=svpf --fields=KSTtzns --line-directives ${CMAKE_CURRENT_BINARY_DIR}/${SRC_BASE_NAME}.cpp
COMMAND ${Python3_EXECUTABLE} ${SCRIPTS_FOLDER}/generate_header.py -i ${CMAKE_CURRENT_BINARY_DIR}/${SRC_BASE_NAME}.ctags -o ${CMAKE_CURRENT_BINARY_DIR}/${SRC_BASE_NAME}.h
add_custom_command(OUTPUT ${SRC_BINARY_BASE_NAME}.h
COMMAND ${ARDUINOCTAGS_EXECUTABLE} -u --language-force=c++ -f ${SRC_BINARY_BASE_NAME}.ctags --c++-kinds=svpf --fields=KSTtzns --line-directives ${SRC_BINARY_BASE_NAME}.cpp
COMMAND ${Python3_EXECUTABLE} ${SCRIPTS_FOLDER}/generate_header.py -i ${SRC_BINARY_BASE_NAME}.ctags -o ${SRC_BINARY_BASE_NAME}.h

DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${SRC_BASE_NAME}.cpp
BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/${SRC_BASE_NAME}.ctags
DEPENDS ${SRC_BINARY_BASE_NAME}.cpp
BYPRODUCTS ${SRC_BINARY_BASE_NAME}.ctags
VERBATIM
)

set_source_files_properties(${SRCFILE}.cpp
set_source_files_properties(${SRC_BINARY_BASE_NAME}.cpp
PROPERTIES
COMPILE_OPTIONS "-include;Arduino.h;-include;${SRCFILE}.h"
OBJECT_DEPENDS "${SRCFILE}.h"
COMPILE_OPTIONS "-include;Arduino.h;-include;${SRC_BINARY_BASE_NAME}.h"
OBJECT_DEPENDS ${SRC_BINARY_BASE_NAME}.h
)
list(APPEND SRCLIST ${SRCFILE}.cpp)
list(APPEND SRCLIST ${SRC_BINARY_BASE_NAME}.cpp)
else()
list(APPEND SRCLIST ${SRCFILE})
endif()
Expand Down

0 comments on commit 7f5d45e

Please sign in to comment.