|
1 | 1 | # ArduinoLibrary.cmake
|
2 | 2 | # 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) |
3 | 10 |
|
4 | 11 |
|
5 | 12 | # arduino_library(<name> <path_or_url> [<tag>])
|
@@ -29,18 +36,80 @@ function(arduino_library LIB_NAME LIB_PATH)
|
29 | 36 | "${LIB_PATH}/src/*.c"
|
30 | 37 | "${LIB_PATH}/src/*.cpp"
|
31 | 38 | )
|
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() |
34 | 49 | target_include_directories(${LIB_NAME} PUBLIC ${INC_DIR})
|
35 | 50 | # Link arduino_emulator to propagate its include directories
|
36 | 51 | 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() |
38 | 58 | endif()
|
39 | 59 | endfunction()
|
40 | 60 |
|
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) |
43 | 69 | #
|
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 | + |
0 commit comments