Skip to content

Commit c45a4ef

Browse files
committed
CMake functions
1 parent d1079e4 commit c45a4ef

File tree

9 files changed

+96
-94
lines changed

9 files changed

+96
-94
lines changed

ArduinoLibrary.cmake

Lines changed: 77 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# ArduinoLibrary.cmake
22
# Defines a function to easily add Arduino-style libraries to your CMake project.
3+
#
4+
# Example usage for arduino-SAM library from GitHub
5+
# Place this in your CMakeLists.txt after including ArduinoLibrary.cmake
6+
#
7+
# include(${CMAKE_SOURCE_DIR}/ArduinoLibrary.cmake)
8+
# arduino_library(arduino-SAM "https://github.com/pschatzmann/arduino-SAM")
9+
# target_link_libraries(your_target PRIVATE arduino-SAM)
310

411

512
# arduino_library(<name> <path_or_url> [<tag>])
@@ -29,18 +36,80 @@ function(arduino_library LIB_NAME LIB_PATH)
2936
"${LIB_PATH}/src/*.c"
3037
"${LIB_PATH}/src/*.cpp"
3138
)
32-
add_library(${LIB_NAME} STATIC ${SRC_FILES})
33-
target_compile_options(${LIB_NAME} PRIVATE -DPROGMEM=)
39+
# Only create library if there are source files
40+
if(SRC_FILES)
41+
add_library(${LIB_NAME} STATIC ${SRC_FILES})
42+
target_compile_options(${LIB_NAME} PRIVATE -DPROGMEM=)
43+
# Ensure C files are compiled as C, not C++
44+
set_target_properties(${LIB_NAME} PROPERTIES LINKER_LANGUAGE C)
45+
else()
46+
# Create a header-only interface library if no source files
47+
add_library(${LIB_NAME} INTERFACE)
48+
endif()
3449
target_include_directories(${LIB_NAME} PUBLIC ${INC_DIR})
3550
# Link arduino_emulator to propagate its include directories
3651
if(TARGET arduino_emulator)
37-
target_link_libraries(${LIB_NAME} PUBLIC arduino_emulator)
52+
if(SRC_FILES)
53+
target_link_libraries(${LIB_NAME} PUBLIC arduino_emulator)
54+
else()
55+
# For interface libraries, use INTERFACE linkage
56+
target_link_libraries(${LIB_NAME} INTERFACE arduino_emulator)
57+
endif()
3858
endif()
3959
endfunction()
4060

41-
# Example usage for arduino-SAM library from GitHub
42-
# Place this in your CMakeLists.txt after including ArduinoLibrary.cmake
61+
# arduino_sketch(<name> <ino_file> [LIBRARIES <lib1> <lib2> ...] [DEFINITIONS <def1> <def2> ...])
62+
# Creates an executable target from an Arduino sketch (.ino file)
63+
#
64+
# Parameters:
65+
# name - The name of the executable target
66+
# ino_file - The .ino source file to compile
67+
# LIBRARIES - Optional list of additional libraries to link
68+
# DEFINITIONS - Optional list of compile definitions (without -D prefix)
4369
#
44-
# include(${CMAKE_SOURCE_DIR}/ArduinoLibrary.cmake)
45-
# arduino_library(arduino-SAM "https://github.com/pschatzmann/arduino-SAM")
46-
# target_link_libraries(your_target PRIVATE arduino-SAM)
70+
# Example usage:
71+
# arduino_sketch(blink blink.ino)
72+
# arduino_sketch(my_project main.ino LIBRARIES arduino-SAM SdFat)
73+
# arduino_sketch(sensor_demo sensor.ino LIBRARIES Wire SPI DEFINITIONS DEBUG=1)
74+
# arduino_sketch(wifi_demo wifi.ino DEFINITIONS USE_HTTPS USE_SSL)
75+
function(arduino_sketch name ino_file)
76+
# Parse optional arguments
77+
set(options)
78+
set(oneValueArgs)
79+
set(multiValueArgs LIBRARIES DEFINITIONS)
80+
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
81+
82+
# Set the source file properties to be treated as C++
83+
set_source_files_properties(${ino_file} PROPERTIES LANGUAGE CXX)
84+
85+
# Create the executable
86+
add_executable(${name} ${ino_file})
87+
88+
# Set C++ standard (inherit from parent or use C++11 minimum)
89+
target_compile_features(${name} PRIVATE cxx_std_11)
90+
91+
# Add compiler flags to treat .ino files as C++
92+
target_compile_options(${name} PRIVATE -x c++)
93+
94+
# Add error flags for better code quality
95+
target_compile_options(${name} PRIVATE -Werror)
96+
97+
# Link with arduino emulator library (always required)
98+
target_link_libraries(${name} arduino_emulator)
99+
100+
# Link additional libraries if specified
101+
if(ARG_LIBRARIES)
102+
target_link_libraries(${name} ${ARG_LIBRARIES})
103+
endif()
104+
105+
# Add compile definitions if specified
106+
if(ARG_DEFINITIONS)
107+
target_compile_definitions(${name} PUBLIC ${ARG_DEFINITIONS})
108+
endif()
109+
110+
# Add platform-specific libraries
111+
if (USE_RPI)
112+
target_link_libraries(${name} gpiod)
113+
endif(USE_RPI)
114+
endfunction()
115+

CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
cmake_minimum_required(VERSION 3.11...3.19)
33

44
# Project name and a few useful settings. Other commands can pick up the results
5-
project(arduino_emulator VERSION 0.1 DESCRIPTION "Arduino Emulator for Linux" LANGUAGES CXX)
5+
project(arduino_emulator VERSION 0.1 DESCRIPTION "Arduino Emulator for Linux" LANGUAGES C CXX)
66

77
# Https takes quite some time and is not always neede
88
option(USE_HTTPS "Https Support" OFF)
@@ -59,6 +59,9 @@ install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/ArduinoCore-Linux/cores/arduino/"
5959
target_compile_features(arduino_emulator PUBLIC cxx_std_17)
6060
target_compile_options(arduino_emulator PRIVATE -Wno-nonportable-include-path)
6161

62+
# Include Arduino library functions
63+
include(${CMAKE_CURRENT_SOURCE_DIR}/ArduinoLibrary.cmake)
64+
6265
if (USE_RPI)
6366
target_compile_options(arduino_emulator PUBLIC -DUSE_RPI)
6467
endif(USE_RPI)

examples/blink/CMakeLists.txt

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
11
cmake_minimum_required(VERSION 3.11...3.19)
22

3-
project(blink)
4-
set(CMAKE_CXX_STANDARD 11)
5-
set(DCMAKE_CXX_FLAGS "-Werror")
6-
7-
set_source_files_properties(blink.ino PROPERTIES LANGUAGE CXX)
8-
add_executable(blink blink.ino)
9-
target_compile_options(blink PRIVATE -x c++)
10-
target_link_libraries(blink arduino_emulator)
11-
if (USE_RPI)
12-
target_link_libraries(blink gpiod)
13-
endif(USE_RPI)
3+
# Use the arduino_sketch function to build the blink example
4+
arduino_sketch(blink blink.ino)
Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
cmake_minimum_required(VERSION 3.11...3.19)
21

3-
project(hallo-world)
4-
set(CMAKE_CXX_STANDARD 11)
5-
set(DCMAKE_CXX_FLAGS "-Werror")
6-
7-
set_source_files_properties(hallo-world.ino PROPERTIES LANGUAGE CXX)
8-
add_executable(hallo-world hallo-world.ino)
9-
target_compile_options(hallo-world PRIVATE -x c++)
10-
target_link_libraries(hallo-world arduino_emulator)
11-
12-
if(USE_RPI)
13-
target_link_libraries(hallo-world gpiod)
14-
endif(USE_RPI)
2+
# Use the arduino_sketch function to build the hallo-world example
3+
arduino_sketch(hallo-world hallo-world.ino)

examples/i2c/CMakeLists.txt

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
cmake_minimum_required(VERSION 3.11...3.19)
21

3-
project(i2c)
4-
set(CMAKE_CXX_STANDARD 11)
5-
set(DCMAKE_CXX_FLAGS "-Werror")
6-
7-
set_source_files_properties(i2c.ino PROPERTIES LANGUAGE CXX)
8-
add_executable(i2c i2c.ino)
9-
target_compile_options(i2c PRIVATE -x c++)
10-
target_link_libraries(i2c arduino_emulator)
11-
if (USE_RPI)
12-
target_link_libraries(i2c gpiod)
13-
endif(USE_RPI)
2+
# Use the arduino_sketch function to build the i2c example
3+
arduino_sketch(i2c i2c.ino)

examples/serial2/CMakeLists.txt

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
cmake_minimum_required(VERSION 3.11...3.19)
21

3-
project(serial2)
4-
set(CMAKE_CXX_STANDARD 11)
5-
set(DCMAKE_CXX_FLAGS "-Werror")
6-
7-
set_source_files_properties(serial2.ino PROPERTIES LANGUAGE CXX)
8-
add_executable(serial2 serial2.ino)
9-
target_compile_options(serial2 PRIVATE -x c++)
10-
target_link_libraries(serial2 arduino_emulator)
11-
if (USE_RPI)
12-
target_link_libraries(serial2 gpiod)
13-
endif(USE_RPI)
2+
# Use the arduino_sketch function to build the serial2 example
3+
arduino_sketch(serial2 serial2.ino)

examples/spi/CMakeLists.txt

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
cmake_minimum_required(VERSION 3.11...3.19)
21

3-
project(spi)
4-
set(CMAKE_CXX_STANDARD 11)
5-
set(DCMAKE_CXX_FLAGS "-Werror")
6-
7-
set_source_files_properties(spi.ino PROPERTIES LANGUAGE CXX)
8-
add_executable(spi spi.ino)
9-
target_compile_options(spi PRIVATE -x c++)
10-
target_link_libraries(spi arduino_emulator)
11-
if (USE_RPI)
12-
target_link_libraries(spi gpiod)
13-
endif(USE_RPI)
2+
# Use the arduino_sketch function to build the spi example
3+
arduino_sketch(spi spi.ino)
Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
1-
cmake_minimum_required(VERSION 3.11...3.19)
21

3-
project(using-arduino-library)
4-
set(CMAKE_CXX_STANDARD 11)
5-
set(DCMAKE_CXX_FLAGS "-Werror")
6-
7-
# add the arduino-SAM library
8-
include(${CMAKE_SOURCE_DIR}/ArduinoLibrary.cmake)
2+
# Add the arduino-SAM library
93
arduino_library(sam "https://github.com/pschatzmann/arduino-SAM")
104

11-
# Define the sketch
12-
set_source_files_properties(using-arduino-library.ino PROPERTIES LANGUAGE CXX)
13-
add_executable(using-arduino-library using-arduino-library.ino)
14-
target_compile_options(using-arduino-library PRIVATE -x c++)
15-
target_link_libraries(using-arduino-library arduino_emulator sam)
16-
if (USE_RPI)
17-
target_link_libraries(using-arduino-library gpiod)
18-
endif(USE_RPI)
5+
# Use the arduino_sketch function to build the example with additional libraries
6+
arduino_sketch(using-arduino-library using-arduino-library.ino LIBRARIES sam)
Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
cmake_minimum_required(VERSION 3.11...3.19)
21

3-
project(wifi-secure)
4-
set(CMAKE_CXX_STANDARD 11)
5-
set(DCMAKE_CXX_FLAGS "-Werror")
6-
7-
set_source_files_properties(wifi-secure.ino PROPERTIES LANGUAGE CXX)
8-
add_executable(wifi-secure wifi-secure.ino)
9-
target_compile_options(wifi-secure PRIVATE -x c++)
10-
target_compile_definitions(wifi-secure PUBLIC -DUSE_HTTPS)
11-
target_link_libraries(wifi-secure arduino_emulator)
2+
# Use the arduino_sketch function to build the wifi-secure example with HTTPS support
3+
arduino_sketch(wifi-secure wifi-secure.ino)

0 commit comments

Comments
 (0)