Repository navigation
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
254 lines (223 loc) · 10 KB
/
Copy pathCMakeLists.txt
File metadata and controls
254 lines (223 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# Works with 3.11 and tested through 3.19
cmake_minimum_required(VERSION 3.11...3.19)
# Project name and a few useful settings. Other commands can pick up the results
project(arduino_emulator VERSION 0.1 DESCRIPTION "Desktop Arduino Emulator" LANGUAGES C CXX)
# Https takes quite some time and is not always neede
option(USE_HTTPS "Https Support" ON)
option(USE_RPI "Raspberry Pi Support" OFF)
option(USE_REMOTE "Remote API Support" OFF)
option(USE_FIRMATA "Firmata GPIO Support" OFF)
option(BUILD_SHARED_LIBS "Build with shared libraries" OFF)
option(BUILD_EXAMPLES "Build with examples" OFF)
option(USE_FTDI "FTDI hardware support" OFF)
# Https backend: TinyTLS (default) or wolfssl (USE_WOLFSSL=ON). Requires USE_HTTPS.
option(USE_WOLFSSL "Use wolfssl instead of TinyTLS for Https support" OFF)
# Https support: Build with TinyTLS (default) or wolfssl
if(USE_HTTPS)
include(FetchContent)
if(USE_WOLFSSL)
FetchContent_Declare(wolfssl GIT_REPOSITORY "https://github.com/wolfSSL/wolfssl.git" GIT_TAG v5.8.2-stable )
FetchContent_GetProperties(wolfssl)
if(NOT wolfssl)
FetchContent_Populate(wolfssl)
add_subdirectory(${wolfssl_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}/wolfssl)
endif()
add_definitions(-DUSE_HTTPS -DUSE_WOLFSSL -DDEBUG_WOLFSSL)
else()
# TinyTLS is header-only; tests are not needed for the emulator build.
# Use -DFETCHCONTENT_SOURCE_DIR_TINYTLS=<path> to build a local checkout.
set(TINYTLS_BUILD_TESTS OFF CACHE BOOL "" FORCE)
FetchContent_Declare(tinytls GIT_REPOSITORY "https://github.com/pschatzmann/TinyTLS.git" GIT_TAG v0.1.0)
FetchContent_GetProperties(tinytls)
if(NOT tinytls_POPULATED)
FetchContent_Populate(tinytls)
endif()
add_definitions(-DUSE_HTTPS)
endif()
endif()
# FTDI support: Build with libftdi1
if(USE_FTDI)
add_definitions(-DUSE_FTDI)
endif()
# Firmata support: GPIO over a Stream speaking the Firmata protocol
if(USE_FIRMATA)
add_definitions(-DUSE_FIRMATA)
endif()
add_definitions(-DHOST)
# glob, but only for CMake 3.12 or later:
file(GLOB HEADER_LIST CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/ArduinoCore-API/api/*.h")
file(GLOB SRC_LIST CONFIGURE_DEPENDS
"${CMAKE_CURRENT_SOURCE_DIR}/ArduinoCore-API/api/*.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/ArduinoCore-Linux/cores/arduino/*.cpp")
if(USE_RPI)
file(GLOB RPI_SRC CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/ArduinoCore-Linux/cores/rasperry_pi/*.cpp")
list(APPEND SRC_LIST ${RPI_SRC})
endif()
if(USE_FTDI)
file(GLOB FTDI_SRC CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/ArduinoCore-Linux/cores/ftdi/*.cpp")
list(APPEND SRC_LIST ${FTDI_SRC})
endif()
if(USE_FIRMATA)
file(GLOB FIRMATA_SRC CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/ArduinoCore-Linux/cores/firmata/*.cpp")
list(APPEND SRC_LIST ${FIRMATA_SRC})
endif()
# Make an dynamic library
add_library(arduino_emulator ${SRC_LIST} ${HEADER_LIST})
# The https options must reach the targets that link the emulator, not only this
# directory (add_definitions() is directory scoped): WiFiClientSecure is compiled
# without TLS otherwise.
if(USE_HTTPS)
target_compile_definitions(arduino_emulator PUBLIC USE_HTTPS)
if(USE_WOLFSSL)
target_compile_definitions(arduino_emulator PUBLIC USE_WOLFSSL)
endif()
endif()
# Define the directories with the h files
target_include_directories (arduino_emulator PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}/ArduinoCore-API"
"${CMAKE_CURRENT_SOURCE_DIR}/ArduinoCore-Linux/cores/arduino"
"${CMAKE_CURRENT_SOURCE_DIR}/ArduinoCore-Linux/cores/rasperry_pi"
"${CMAKE_CURRENT_SOURCE_DIR}/ArduinoCore-Linux/cores/ftdi"
"${CMAKE_CURRENT_SOURCE_DIR}/ArduinoCore-Linux/cores/firmata"
"${CMAKE_CURRENT_SOURCE_DIR}/ArduinoCore-Linux/libraries"
"${CMAKE_CURRENT_SOURCE_DIR}/ArduinoCore-API/api/deprecated"
)
# Install header files
install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/ArduinoCore-API/api/" # source directory
DESTINATION "include/arduino/api" # target directory
FILES_MATCHING
PATTERN "*.h"
)
install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/ArduinoCore-Linux/cores/arduino/" # source directory
DESTINATION "include/arduino" # target directory
FILES_MATCHING
PATTERN "*.h"
)
# All users of this library will need at least C++17
target_compile_features(arduino_emulator PUBLIC cxx_std_17)
if(WIN32)
target_compile_definitions(arduino_emulator PUBLIC ARDUINO_EMULATOR_WINDOWS=1)
target_link_libraries(arduino_emulator PUBLIC ws2_32)
endif()
# Include Arduino library functions
include(${CMAKE_CURRENT_SOURCE_DIR}/Arduino.cmake)
if (USE_RPI)
message(STATUS "USE_RPI=${USE_RPI}")
# Try pkg-config first (handles include dirs and version info). If pkg-config
# or the .pc file is missing, fall back to find_library.
find_package(PkgConfig QUIET)
set(_gpiod_candidates libgpiod gpiod libgpiod2)
set(_GPIOD_FOUND FALSE)
if(PkgConfig_FOUND)
foreach(_name IN LISTS _gpiod_candidates)
pkg_check_modules(GPIOD_PKG QUIET ${_name})
if(GPIOD_PKG_FOUND)
set(_GPIOD_FOUND TRUE)
set(GPIOD_INCLUDE_DIRS ${GPIOD_PKG_INCLUDE_DIRS})
set(GPIOD_LIBRARIES ${GPIOD_PKG_LIBRARIES})
set(GPIOD_VERSION ${GPIOD_PKG_VERSION})
message(STATUS "Found libgpiod via pkg-config (${_name}) version ${GPIOD_VERSION}")
break()
endif()
endforeach()
endif()
# Fallback: search for the library file if pkg-config didn't find anything
if(NOT _GPIOD_FOUND)
find_library(GPIOD_LIB NAMES gpiod gpiod2 libgpiod libgpiod2)
if(GPIOD_LIB)
set(_GPIOD_FOUND TRUE)
message(STATUS "Found libgpiod (via find_library): ${GPIOD_LIB}")
endif()
endif()
if(_GPIOD_FOUND)
# Add compile definition to enable RPi-specific code paths
target_compile_definitions(arduino_emulator PUBLIC USE_RPI)
# Prefer pkg-config provided include dirs/libs, otherwise use the found library
if(DEFINED GPIOD_INCLUDE_DIRS)
target_include_directories(arduino_emulator PUBLIC ${GPIOD_INCLUDE_DIRS})
endif()
if(DEFINED GPIOD_LIBRARIES)
# GPIOD_LIBRARIES from pkg-config may contain linker flags; use them directly
target_link_libraries(arduino_emulator PUBLIC ${GPIOD_LIBRARIES})
elseif(GPIOD_LIB)
target_link_libraries(arduino_emulator PUBLIC ${GPIOD_LIB})
endif()
# If version information is available and < 2.0, add a compatibility define
if(DEFINED GPIOD_VERSION AND NOT GPIOD_VERSION STREQUAL "")
if(GPIOD_VERSION VERSION_LESS "2.0")
target_compile_definitions(arduino_emulator PUBLIC LIBGPIOD_V1)
message(STATUS "Assuming libgpiod v1 compatibility (version: ${GPIOD_VERSION})")
else()
message(STATUS "libgpiod v2 detected (version: ${GPIOD_VERSION})")
endif()
else()
# If we only found the library file, try to guess v1 vs v2 from the library name
if(GPIOD_LIB AND GPIOD_LIB MATCHES "gpiod2")
message(STATUS "Detected libgpiod2 library file; assuming v2")
else()
# conservative default: assume v1 compatibility when uncertain
target_compile_definitions(arduino_emulator PUBLIC LIBGPIOD_V1)
message(STATUS "libgpiod version unknown: enabling v1 compatibility define")
endif()
endif()
else()
message(STATUS "libgpiod not found — Raspberry Pi gpio support disabled")
endif()
endif(USE_RPI)
if (USE_REMOTE)
message(STATUS "USE_REMOTE=${USE_REMOTE}")
target_compile_options(arduino_emulator PUBLIC -DUSE_REMOTE)
endif(USE_REMOTE)
if(USE_HTTPS)
if(USE_WOLFSSL)
message(STATUS "USE_HTTPS=${USE_HTTPS} (wolfssl)")
# Add external libraries
target_link_libraries(arduino_emulator wolfssl)
else()
message(STATUS "USE_HTTPS=${USE_HTTPS} (TinyTLS)")
# TinyTLS is header-only: add its include directory and install the headers
# next to the Arduino headers, since NetworkClientSecure.h includes them.
target_include_directories(arduino_emulator PUBLIC "${tinytls_SOURCE_DIR}/src")
install(DIRECTORY "${tinytls_SOURCE_DIR}/src/" # source directory
DESTINATION "include/arduino" # target directory
FILES_MATCHING
PATTERN "*.h"
)
endif()
endif(USE_HTTPS)
if(USE_FTDI)
message(STATUS "USE_FTDI=${USE_FTDI}")
# Link with libftdi1
find_package(PkgConfig REQUIRED)
pkg_check_modules(FTDI REQUIRED libftdi1)
target_link_libraries(arduino_emulator ${FTDI_LIBRARIES})
target_include_directories(arduino_emulator PUBLIC ${FTDI_INCLUDE_DIRS})
target_compile_options(arduino_emulator PUBLIC ${FTDI_CFLAGS_OTHER})
# Suppress deprecation warnings for FTDI-related code
target_compile_options(arduino_emulator PRIVATE -Wno-deprecated-declarations)
endif(USE_FTDI)
if(USE_FIRMATA)
message(STATUS "USE_FIRMATA=${USE_FIRMATA}")
endif(USE_FIRMATA)
# build examples
if(BUILD_EXAMPLES)
message(STATUS "BUILD_EXAMPLES=${BUILD_EXAMPLES}")
add_subdirectory("examples")
endif(BUILD_EXAMPLES)
# build and run the native unit tests (Unity is fetched automatically)
option(BUILD_TESTS "Build the native unit tests" OFF)
if(BUILD_TESTS)
message(STATUS "BUILD_TESTS=${BUILD_TESTS}")
include(FetchContent)
FetchContent_Declare(unity GIT_REPOSITORY "https://github.com/ThrowTheSwitch/Unity.git" GIT_TAG v2.5.2)
FetchContent_GetProperties(unity)
if(NOT unity_POPULATED)
FetchContent_Populate(unity)
endif()
add_library(unity STATIC "${unity_SOURCE_DIR}/src/unity.c")
target_include_directories(unity PUBLIC "${unity_SOURCE_DIR}/src")
enable_testing()
add_executable(test_native "${CMAKE_CURRENT_SOURCE_DIR}/test/native/test_native.cpp")
target_link_libraries(test_native PRIVATE arduino_emulator unity)
add_test(NAME test_native COMMAND test_native)
endif(BUILD_TESTS)