-
Notifications
You must be signed in to change notification settings - Fork 216
/
Arduino.cmake
2232 lines (1959 loc) · 86.3 KB
/
Arduino.cmake
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#=============================================================================#
# generate_arduino_firmware(name
# [BOARD board_id]
# [SKETCH sketch_path |
# SRCS src1 src2 ... srcN]
# [HDRS hdr1 hdr2 ... hdrN]
# [LIBS lib1 lib2 ... libN]
# [PORT port]
# [SERIAL serial_cmd]
# [PROGRAMMER programmer_id]
# [AFLAGS flags]
# [NO_AUTOLIBS]
# [MANUAL])
#
#=============================================================================#
#
# generaters firmware and libraries for Arduino devices
#
# The arguments are as follows:
#
# name # The name of the firmware target [REQUIRED]
# BOARD # Board name (such as uno, mega2560, ...) [REQUIRED]
# SKETCH # Arduino sketch [must have SRCS or SKETCH]
# SRCS # Sources [must have SRCS or SKETCH]
# HDRS # Headers
# LIBS # Libraries to link
# ARDLIBS # Arduino libraries to link (Wire, Servo, SPI, etc)
# PORT # Serial port (enables upload support)
# SERIAL # Serial command for serial target
# PROGRAMMER # Programmer id (enables programmer support)
# AFLAGS # Avrdude flags for target
# NO_AUTOLIBS # Disables Arduino library detection
# MANUAL # (Advanced) Only use AVR Libc/Includes
#
# Here is a short example for a target named test:
#
# generate_arduino_firmware(
# NAME test
# SRCS test.cpp
# test2.cpp
# HDRS test.h test2.h
# BOARD uno)
#
# Alternatively you can specify the option by variables:
#
# set(test_SRCS test.cpp test2.cpp)
# set(test_HDRS test.h test2.h
# set(test_BOARD uno)
#
# generate_arduino_firmware(test)
#
# All variables need to be prefixed with the target name (${TARGET_NAME}_${OPTION}).
#
#=============================================================================#
# generate_avr_firmware(name
# [BOARD board_id]
# SRCS src1 src2 ... srcN]
# [HDRS hdr1 hdr2 ... hdrN]
# [LIBS lib1 lib2 ... libN]
# [PORT port]
# [SERIAL serial_cmd]
# [PROGRAMMER programmer_id]
# [AFLAGS flags])
#=============================================================================#
#
# generaters firmware and libraries for AVR devices
# it simply calls generate_arduino_firmware() with NO_AUTOLIBS and MANUAL
#
# The arguments are as follows:
#
# name # The name of the firmware target [REQUIRED]
# BOARD # Board name (such as uno, mega2560, ...) [REQUIRED]
# SRCS # Sources [REQUIRED]
# HDRS # Headers
# LIBS # Libraries to link
# PORT # Serial port (enables upload support)
# SERIAL # Serial command for serial target
# PROGRAMMER # Programmer id (enables programmer support)
# AFLAGS # Avrdude flags for target
#
# Here is a short example for a target named test:
#
# generate_avr_firmware(
# NAME test
# SRCS test.cpp
# test2.cpp
# HDRS test.h test2.h
# BOARD uno)
#
# Alternatively you can specify the option by variables:
#
# set(test_SRCS test.cpp test2.cpp)
# set(test_HDRS test.h test2.h
# set(test_BOARD uno)
#
# generate_avr_firmware(test)
#
# All variables need to be prefixed with the target name (${TARGET_NAME}_${OPTION}).
#
#=============================================================================#
# generate_arduino_library(name
# [BOARD board_id]
# [SRCS src1 src2 ... srcN]
# [HDRS hdr1 hdr2 ... hdrN]
# [LIBS lib1 lib2 ... libN]
# [NO_AUTOLIBS]
# [MANUAL])
#=============================================================================#
# generaters firmware and libraries for Arduino devices
#
# The arguments are as follows:
#
# name # The name of the firmware target [REQUIRED]
# BOARD # Board name (such as uno, mega2560, ...) [REQUIRED]
# SRCS # Sources [REQUIRED]
# HDRS # Headers
# LIBS # Libraries to link
# NO_AUTOLIBS # Disables Arduino library detection
# MANUAL # (Advanced) Only use AVR Libc/Includes
#
# Here is a short example for a target named test:
#
# generate_arduino_library(
# NAME test
# SRCS test.cpp
# test2.cpp
# HDRS test.h test2.h
# BOARD uno)
#
# Alternatively you can specify the option by variables:
#
# set(test_SRCS test.cpp test2.cpp)
# set(test_HDRS test.h test2.h
# set(test_BOARD uno)
#
# generate_arduino_library(test)
#
# All variables need to be prefixed with the target name (${TARGET_NAME}_${OPTION}).
#
#=============================================================================#
# generate_avr_library(name
# [BOARD board_id]
# [SRCS src1 src2 ... srcN]
# [HDRS hdr1 hdr2 ... hdrN]
# [LIBS lib1 lib2 ... libN])
#=============================================================================#
# generaters firmware and libraries for AVR devices
# it simply calls generate_arduino_library() with NO_AUTOLIBS and MANUAL
#
# The arguments are as follows:
#
# name # The name of the firmware target [REQUIRED]
# BOARD # Board name (such as uno, mega2560, ...) [REQUIRED]
# SRCS # Sources [REQUIRED]
# HDRS # Headers
# LIBS # Libraries to link
#
# Here is a short example for a target named test:
#
# generate_avr_library(
# NAME test
# SRCS test.cpp
# test2.cpp
# HDRS test.h test2.h
# BOARD uno)
#
# Alternatively you can specify the option by variables:
#
# set(test_SRCS test.cpp test2.cpp)
# set(test_HDRS test.h test2.h
# set(test_BOARD uno)
#
# generate_avr_library(test)
#
# All variables need to be prefixed with the target name (${TARGET_NAME}_${OPTION}).
#
#=============================================================================#
# generate_arduino_example(name
# LIBRARY library_name
# EXAMPLE example_name
# [BOARD board_id]
# [PORT port]
# [SERIAL serial command]
# [PORGRAMMER programmer_id]
# [AFLAGS avrdude_flags])
#=============================================================================#
#
# name - The name of the library example [REQUIRED]
# LIBRARY - Library name [REQUIRED]
# EXAMPLE - Example name [REQUIRED]
# BOARD - Board ID
# PORT - Serial port [optional]
# SERIAL - Serial command [optional]
# PROGRAMMER - Programmer id (enables programmer support)
# AFLAGS - Avrdude flags for target
#
# Creates a example from the specified library.
#
#
#=============================================================================#
# print_board_list()
#=============================================================================#
#
# Print list of detected Arduino Boards.
#
#=============================================================================#
# print_programmer_list()
#=============================================================================#
#
# Print list of detected Programmers.
#
#=============================================================================#
# print_programmer_settings(PROGRAMMER)
#=============================================================================#
#
# PROGRAMMER - programmer id
#
# Print the detected Programmer settings.
#
#=============================================================================#
# print_board_settings(ARDUINO_BOARD)
#=============================================================================#
#
# ARDUINO_BOARD - Board id
#
# Print the detected Arduino board settings.
#
#=============================================================================#
# register_hardware_platform(HARDWARE_PLATFORM_PATH)
#=============================================================================#
#
# HARDWARE_PLATFORM_PATH - Hardware platform path
#
# Registers a Hardware Platform path.
# See: http://code.google.com/p/arduino/wiki/Platforms
#
# This enables you to register new types of hardware platforms such as the
# Sagnuino, without having to copy the files into your Arduion SDK.
#
# A Hardware Platform is a directory containing the following:
#
# HARDWARE_PLATFORM_PATH/
# |-- bootloaders/
# |-- cores/
# |-- variants/
# |-- boards.txt
# `-- programmers.txt
#
# The board.txt describes the target boards and bootloaders. While
# programmers.txt the programmer defintions.
#
# A good example of a Hardware Platform is in the Arduino SDK:
#
# ${ARDUINO_SDK_PATH}/hardware/arduino/
#
#=============================================================================#
# Configuration Options
#=============================================================================#
#
# ARDUINO_SDK_PATH - Arduino SDK Path
# ARDUINO_AVRDUDE_PROGRAM - Full path to avrdude programmer
# ARDUINO_AVRDUDE_CONFIG_PATH - Full path to avrdude configuration file
#
# ARDUINO_C_FLAGS - C compiler flags
# ARDUINO_CXX_FLAGS - C++ compiler flags
# ARDUINO_LINKER_FLAGS - Linker flags
#
# ARDUINO_DEFAULT_BOARD - Default Arduino Board ID when not specified.
# ARDUINO_DEFAULT_PORT - Default Arduino port when not specified.
# ARDUINO_DEFAULT_SERIAL - Default Arduino Serial command when not specified.
# ARDUINO_DEFAULT_PROGRAMMER - Default Arduino Programmer ID when not specified.
#
#
# ARDUINO_FOUND - Set to True when the Arduino SDK is detected and configured.
# ARDUINO_SDK_VERSION - Set to the version of the detected Arduino SDK (ex: 1.0)
#=============================================================================#
# Author: Tomasz Bogdal (QueezyTheGreat)
# Home: https://github.com/queezythegreat/arduino-cmake
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#=============================================================================#
cmake_minimum_required(VERSION 2.8.5)
include(CMakeParseArguments)
#=============================================================================#
# User Functions
#=============================================================================#
#=============================================================================#
# [PUBLIC/USER]
#
# print_board_list()
#
# see documentation at top
#=============================================================================#
function(PRINT_BOARD_LIST)
foreach(PLATFORM ${ARDUINO_PLATFORMS})
if(${PLATFORM}_BOARDS)
message(STATUS "${PLATFORM} Boards:")
print_list(${PLATFORM}_BOARDS)
message(STATUS "")
endif()
endforeach()
endfunction()
#=============================================================================#
# [PUBLIC/USER]
#
# print_programmer_list()
#
# see documentation at top
#=============================================================================#
function(PRINT_PROGRAMMER_LIST)
foreach(PLATFORM ${ARDUINO_PLATFORMS})
if(${PLATFORM}_PROGRAMMERS)
message(STATUS "${PLATFORM} Programmers:")
print_list(${PLATFORM}_PROGRAMMERS)
endif()
message(STATUS "")
endforeach()
endfunction()
#=============================================================================#
# [PUBLIC/USER]
#
# print_programmer_settings(PROGRAMMER)
#
# see documentation at top
#=============================================================================#
function(PRINT_PROGRAMMER_SETTINGS PROGRAMMER)
if(${PROGRAMMER}.SETTINGS)
message(STATUS "Programmer ${PROGRAMMER} Settings:")
print_settings(${PROGRAMMER})
endif()
endfunction()
# [PUBLIC/USER]
#
# print_board_settings(ARDUINO_BOARD)
#
# see documentation at top
function(PRINT_BOARD_SETTINGS ARDUINO_BOARD)
if(${ARDUINO_BOARD}.SETTINGS)
message(STATUS "Arduino ${ARDUINO_BOARD} Board:")
print_settings(${ARDUINO_BOARD})
endif()
endfunction()
#=============================================================================#
# [PUBLIC/USER]
# see documentation at top
#=============================================================================#
function(GENERATE_ARDUINO_LIBRARY INPUT_NAME)
message(STATUS "Generating ${INPUT_NAME}")
parse_generator_arguments(${INPUT_NAME} INPUT
"NO_AUTOLIBS;MANUAL" # Options
"BOARD" # One Value Keywords
"SRCS;HDRS;LIBS" # Multi Value Keywords
${ARGN})
if(NOT INPUT_BOARD)
set(INPUT_BOARD ${ARDUINO_DEFAULT_BOARD})
endif()
if(NOT INPUT_MANUAL)
set(INPUT_MANUAL FALSE)
endif()
required_variables(VARS INPUT_SRCS INPUT_BOARD MSG "must define for target ${INPUT_NAME}")
set(ALL_LIBS)
set(ALL_SRCS ${INPUT_SRCS} ${INPUT_HDRS})
if(NOT INPUT_MANUAL)
setup_arduino_core(CORE_LIB ${INPUT_BOARD})
endif()
find_arduino_libraries(TARGET_LIBS "${ALL_SRCS}" "")
set(LIB_DEP_INCLUDES)
foreach(LIB_DEP ${TARGET_LIBS})
set(LIB_DEP_INCLUDES "${LIB_DEP_INCLUDES} -I\"${LIB_DEP}\"")
endforeach()
if(NOT ${INPUT_NO_AUTOLIBS})
setup_arduino_libraries(ALL_LIBS ${INPUT_BOARD} "${ALL_SRCS}" "" "${LIB_DEP_INCLUDES}" "")
endif()
list(APPEND ALL_LIBS ${CORE_LIB} ${INPUT_LIBS})
add_library(${INPUT_NAME} ${ALL_SRCS})
get_arduino_flags(ARDUINO_COMPILE_FLAGS ARDUINO_LINK_FLAGS ${INPUT_BOARD} ${INPUT_MANUAL})
set_target_properties(${INPUT_NAME} PROPERTIES
COMPILE_FLAGS "${ARDUINO_COMPILE_FLAGS} ${COMPILE_FLAGS} ${LIB_DEP_INCLUDES}"
LINK_FLAGS "${ARDUINO_LINK_FLAGS} ${LINK_FLAGS}")
target_link_libraries(${INPUT_NAME} ${ALL_LIBS} "-lc -lm")
endfunction()
#=============================================================================#
# [PUBLIC/USER]
# see documentation at top
#=============================================================================#
function(GENERATE_AVR_LIBRARY INPUT_NAME)
message(STATUS "Generating ${INPUT_NAME}")
parse_generator_arguments(${INPUT_NAME} INPUT
"NO_AUTOLIBS;MANUAL" # Options
"BOARD" # One Value Keywords
"SRCS;HDRS;LIBS" # Multi Value Keywords
${ARGN})
if(NOT INPUT_BOARD)
set(INPUT_BOARD ${ARDUINO_DEFAULT_BOARD})
endif()
required_variables(VARS INPUT_SRCS INPUT_BOARD MSG "must define for target ${INPUT_NAME}")
if(INPUT_HDRS)
set( INPUT_HDRS "SRCS ${INPUT_HDRS}" )
endif()
if(INPUT_LIBS)
set( INPUT_LIBS "LIBS ${INPUT_LIBS}" )
endif()
if(INPUT_HDRS)
list(INSERT INPUT_HDRS 0 "HDRS")
endif()
if(INPUT_LIBS)
list(INSERT INPUT_LIBS 0 "LIBS")
endif()
generate_arduino_library( ${INPUT_NAME}
NO_AUTOLIBS
MANUAL
BOARD ${INPUT_BOARD}
SRCS ${INPUT_SRCS}
${INPUT_HDRS}
${INPUT_LIBS} )
endfunction()
#=============================================================================#
# [PUBLIC/USER]
# see documentation at top
#=============================================================================#
function(GENERATE_ARDUINO_FIRMWARE INPUT_NAME)
message(STATUS "Generating ${INPUT_NAME}")
parse_generator_arguments(${INPUT_NAME} INPUT
"NO_AUTOLIBS;MANUAL" # Options
"BOARD;PORT;SKETCH;PROGRAMMER" # One Value Keywords
"SERIAL;SRCS;HDRS;LIBS;ARDLIBS;AFLAGS" # Multi Value Keywords
${ARGN})
if(NOT INPUT_BOARD)
set(INPUT_BOARD ${ARDUINO_DEFAULT_BOARD})
endif()
if(NOT INPUT_PORT)
set(INPUT_PORT ${ARDUINO_DEFAULT_PORT})
endif()
if(NOT INPUT_SERIAL)
set(INPUT_SERIAL ${ARDUINO_DEFAULT_SERIAL})
endif()
if(NOT INPUT_PROGRAMMER)
set(INPUT_PROGRAMMER ${ARDUINO_DEFAULT_PROGRAMMER})
endif()
if(NOT INPUT_MANUAL)
set(INPUT_MANUAL FALSE)
endif()
required_variables(VARS INPUT_BOARD MSG "must define for target ${INPUT_NAME}")
set(ALL_LIBS)
set(ALL_SRCS ${INPUT_SRCS} ${INPUT_HDRS})
set(LIB_DEP_INCLUDES)
if(NOT INPUT_MANUAL)
setup_arduino_core(CORE_LIB ${INPUT_BOARD})
endif()
if(NOT "${INPUT_SKETCH}" STREQUAL "")
get_filename_component(INPUT_SKETCH "${INPUT_SKETCH}" ABSOLUTE)
setup_arduino_sketch(${INPUT_NAME} ${INPUT_SKETCH} ALL_SRCS)
if (IS_DIRECTORY "${INPUT_SKETCH}")
set(LIB_DEP_INCLUDES "${LIB_DEP_INCLUDES} -I\"${INPUT_SKETCH}\"")
else()
get_filename_component(INPUT_SKETCH_PATH "${INPUT_SKETCH}" PATH)
set(LIB_DEP_INCLUDES "${LIB_DEP_INCLUDES} -I\"${INPUT_SKETCH_PATH}\"")
endif()
endif()
required_variables(VARS ALL_SRCS MSG "must define SRCS or SKETCH for target ${INPUT_NAME}")
find_arduino_libraries(TARGET_LIBS "${ALL_SRCS}" "${INPUT_ARDLIBS}")
foreach(LIB_DEP ${TARGET_LIBS})
arduino_debug_msg("Arduino Library: ${LIB_DEP}")
set(LIB_DEP_INCLUDES "${LIB_DEP_INCLUDES} -I\"${LIB_DEP}\"")
endforeach()
if(NOT INPUT_NO_AUTOLIBS)
setup_arduino_libraries(ALL_LIBS ${INPUT_BOARD} "${ALL_SRCS}" "${INPUT_ARDLIBS}" "${LIB_DEP_INCLUDES}" "")
foreach(LIB_INCLUDES ${ALL_LIBS_INCLUDES})
arduino_debug_msg("Arduino Library Includes: ${LIB_INCLUDES}")
set(LIB_DEP_INCLUDES "${LIB_DEP_INCLUDES} ${LIB_INCLUDES}")
endforeach()
endif()
list(APPEND ALL_LIBS ${CORE_LIB} ${INPUT_LIBS})
setup_arduino_target(${INPUT_NAME} ${INPUT_BOARD} "${ALL_SRCS}" "${ALL_LIBS}" "${LIB_DEP_INCLUDES}" "" "${INPUT_MANUAL}")
if(INPUT_PORT)
setup_arduino_upload(${INPUT_BOARD} ${INPUT_NAME} ${INPUT_PORT} "${INPUT_PROGRAMMER}" "${INPUT_AFLAGS}")
endif()
if(INPUT_SERIAL)
setup_serial_target(${INPUT_NAME} "${INPUT_SERIAL}" "${INPUT_PORT}")
endif()
endfunction()
#=============================================================================#
# [PUBLIC/USER]
# see documentation at top
#=============================================================================#
function(GENERATE_AVR_FIRMWARE INPUT_NAME)
# TODO: This is not optimal!!!!
message(STATUS "Generating ${INPUT_NAME}")
parse_generator_arguments(${INPUT_NAME} INPUT
"NO_AUTOLIBS;MANUAL" # Options
"BOARD;PORT;PROGRAMMER" # One Value Keywords
"SERIAL;SRCS;HDRS;LIBS;AFLAGS" # Multi Value Keywords
${ARGN})
if(NOT INPUT_BOARD)
set(INPUT_BOARD ${ARDUINO_DEFAULT_BOARD})
endif()
if(NOT INPUT_PORT)
set(INPUT_PORT ${ARDUINO_DEFAULT_PORT})
endif()
if(NOT INPUT_SERIAL)
set(INPUT_SERIAL ${ARDUINO_DEFAULT_SERIAL})
endif()
if(NOT INPUT_PROGRAMMER)
set(INPUT_PROGRAMMER ${ARDUINO_DEFAULT_PROGRAMMER})
endif()
required_variables(VARS INPUT_BOARD INPUT_SRCS MSG "must define for target ${INPUT_NAME}")
if(INPUT_HDRS)
list(INSERT INPUT_HDRS 0 "HDRS")
endif()
if(INPUT_LIBS)
list(INSERT INPUT_LIBS 0 "LIBS")
endif()
if(INPUT_AFLAGS)
list(INSERT INPUT_AFLAGS 0 "AFLAGS")
endif()
generate_arduino_firmware( ${INPUT_NAME}
NO_AUTOLIBS
MANUAL
BOARD ${INPUT_BOARD}
PORT ${INPUT_PORT}
PROGRAMMER ${INPUT_PROGRAMMER}
SERIAL ${INPUT_SERIAL}
SRCS ${INPUT_SRCS}
${INPUT_HDRS}
${INPUT_LIBS}
${INPUT_AFLAGS} )
endfunction()
#=============================================================================#
# [PUBLIC/USER]
# see documentation at top
#=============================================================================#
function(GENERATE_ARDUINO_EXAMPLE INPUT_NAME)
parse_generator_arguments(${INPUT_NAME} INPUT
"" # Options
"LIBRARY;EXAMPLE;BOARD;PORT;PROGRAMMER" # One Value Keywords
"SERIAL;AFLAGS" # Multi Value Keywords
${ARGN})
if(NOT INPUT_BOARD)
set(INPUT_BOARD ${ARDUINO_DEFAULT_BOARD})
endif()
if(NOT INPUT_PORT)
set(INPUT_PORT ${ARDUINO_DEFAULT_PORT})
endif()
if(NOT INPUT_SERIAL)
set(INPUT_SERIAL ${ARDUINO_DEFAULT_SERIAL})
endif()
if(NOT INPUT_PROGRAMMER)
set(INPUT_PROGRAMMER ${ARDUINO_DEFAULT_PROGRAMMER})
endif()
required_variables(VARS INPUT_LIBRARY INPUT_EXAMPLE INPUT_BOARD
MSG "must define for target ${INPUT_NAME}")
message(STATUS "Generating ${INPUT_NAME}")
set(ALL_LIBS)
set(ALL_SRCS)
setup_arduino_core(CORE_LIB ${INPUT_BOARD})
setup_arduino_example("${INPUT_NAME}" "${INPUT_LIBRARY}" "${INPUT_EXAMPLE}" ALL_SRCS)
if(NOT ALL_SRCS)
message(FATAL_ERROR "Missing sources for example, aborting!")
endif()
find_arduino_libraries(TARGET_LIBS "${ALL_SRCS}" "")
set(LIB_DEP_INCLUDES)
foreach(LIB_DEP ${TARGET_LIBS})
set(LIB_DEP_INCLUDES "${LIB_DEP_INCLUDES} -I\"${LIB_DEP}\"")
endforeach()
setup_arduino_libraries(ALL_LIBS ${INPUT_BOARD} "${ALL_SRCS}" "" "${LIB_DEP_INCLUDES}" "")
list(APPEND ALL_LIBS ${CORE_LIB} ${INPUT_LIBS})
setup_arduino_target(${INPUT_NAME} ${INPUT_BOARD} "${ALL_SRCS}" "${ALL_LIBS}" "${LIB_DEP_INCLUDES}" "" FALSE)
if(INPUT_PORT)
setup_arduino_upload(${INPUT_BOARD} ${INPUT_NAME} ${INPUT_PORT} "${INPUT_PROGRAMMER}" "${INPUT_AFLAGS}")
endif()
if(INPUT_SERIAL)
setup_serial_target(${INPUT_NAME} "${INPUT_SERIAL}" "${INPUT_PORT}")
endif()
endfunction()
#=============================================================================#
# [PUBLIC/USER]
# see documentation at top
#=============================================================================#
function(REGISTER_HARDWARE_PLATFORM PLATFORM_PATH)
string(REGEX REPLACE "/$" "" PLATFORM_PATH ${PLATFORM_PATH})
GET_FILENAME_COMPONENT(PLATFORM ${PLATFORM_PATH} NAME)
if(PLATFORM)
string(TOUPPER ${PLATFORM} PLATFORM)
list(FIND ARDUINO_PLATFORMS ${PLATFORM} platform_exists)
if (platform_exists EQUAL -1)
set(${PLATFORM}_PLATFORM_PATH ${PLATFORM_PATH} CACHE INTERNAL "The path to ${PLATFORM}")
set(ARDUINO_PLATFORMS ${ARDUINO_PLATFORMS} ${PLATFORM} CACHE INTERNAL "A list of registered platforms")
find_file(${PLATFORM}_CORES_PATH
NAMES cores
PATHS ${PLATFORM_PATH}
DOC "Path to directory containing the Arduino core sources.")
find_file(${PLATFORM}_VARIANTS_PATH
NAMES variants
PATHS ${PLATFORM_PATH}
DOC "Path to directory containing the Arduino variant sources.")
find_file(${PLATFORM}_BOOTLOADERS_PATH
NAMES bootloaders
PATHS ${PLATFORM_PATH}
DOC "Path to directory containing the Arduino bootloader images and sources.")
find_file(${PLATFORM}_PROGRAMMERS_PATH
NAMES programmers.txt
PATHS ${PLATFORM_PATH}
DOC "Path to Arduino programmers definition file.")
find_file(${PLATFORM}_BOARDS_PATH
NAMES boards.txt
PATHS ${PLATFORM_PATH}
DOC "Path to Arduino boards definition file.")
if(${PLATFORM}_BOARDS_PATH)
load_arduino_style_settings(${PLATFORM}_BOARDS "${PLATFORM_PATH}/boards.txt")
endif()
if(${PLATFORM}_PROGRAMMERS_PATH)
load_arduino_style_settings(${PLATFORM}_PROGRAMMERS "${ARDUINO_PROGRAMMERS_PATH}")
endif()
if(${PLATFORM}_VARIANTS_PATH)
file(GLOB sub-dir ${${PLATFORM}_VARIANTS_PATH}/*)
foreach(dir ${sub-dir})
if(IS_DIRECTORY ${dir})
get_filename_component(variant ${dir} NAME)
set(VARIANTS ${VARIANTS} ${variant} CACHE INTERNAL "A list of registered variant boards")
set(${variant}.path ${dir} CACHE INTERNAL "The path to the variant ${variant}")
endif()
endforeach()
endif()
if(${PLATFORM}_CORES_PATH)
file(GLOB sub-dir ${${PLATFORM}_CORES_PATH}/*)
foreach(dir ${sub-dir})
if(IS_DIRECTORY ${dir})
get_filename_component(core ${dir} NAME)
set(CORES ${CORES} ${core} CACHE INTERNAL "A list of registered cores")
set(${core}.path ${dir} CACHE INTERNAL "The path to the core ${core}")
endif()
endforeach()
endif()
endif()
endif()
endfunction()
#=============================================================================#
# Internal Functions
#=============================================================================#
#=============================================================================#
# [PRIVATE/INTERNAL]
#
# parse_generator_arguments(TARGET_NAME PREFIX OPTIONS ARGS MULTI_ARGS [ARG1 ARG2 .. ARGN])
#
# PREFIX - Parsed options prefix
# OPTIONS - List of options
# ARGS - List of one value keyword arguments
# MULTI_ARGS - List of multi value keyword arguments
# [ARG1 ARG2 .. ARGN] - command arguments [optional]
#
# Parses generator options from either variables or command arguments
#
#=============================================================================#
macro(PARSE_GENERATOR_ARGUMENTS TARGET_NAME PREFIX OPTIONS ARGS MULTI_ARGS)
cmake_parse_arguments(${PREFIX} "${OPTIONS}" "${ARGS}" "${MULTI_ARGS}" ${ARGN})
error_for_unparsed(${PREFIX})
load_generator_settings(${TARGET_NAME} ${PREFIX} ${OPTIONS} ${ARGS} ${MULTI_ARGS})
endmacro()
#=============================================================================#
# [PRIVATE/INTERNAL]
#
# load_generator_settings(TARGET_NAME PREFIX [SUFFIX_1 SUFFIX_2 .. SUFFIX_N])
#
# TARGET_NAME - The base name of the user settings
# PREFIX - The prefix name used for generator settings
# SUFFIX_XX - List of suffixes to load
#
# Loads a list of user settings into the generators scope. User settings have
# the following syntax:
#
# ${BASE_NAME}${SUFFIX}
#
# The BASE_NAME is the target name and the suffix is a specific generator settings.
#
# For every user setting found a generator setting is created of the follwoing fromat:
#
# ${PREFIX}${SUFFIX}
#
# The purpose of loading the settings into the generator is to not modify user settings
# and to have a generic naming of the settings within the generator.
#
#=============================================================================#
function(LOAD_GENERATOR_SETTINGS TARGET_NAME PREFIX)
foreach(GEN_SUFFIX ${ARGN})
if(${TARGET_NAME}_${GEN_SUFFIX} AND NOT ${PREFIX}_${GEN_SUFFIX})
set(${PREFIX}_${GEN_SUFFIX} ${${TARGET_NAME}_${GEN_SUFFIX}} PARENT_SCOPE)
endif()
endforeach()
endfunction()
#=============================================================================#
# [PRIVATE/INTERNAL]
#
# get_arduino_flags(COMPILE_FLAGS LINK_FLAGS BOARD_ID MANUAL)
#
# COMPILE_FLAGS_VAR -Variable holding compiler flags
# LINK_FLAGS_VAR - Variable holding linker flags
# BOARD_ID - The board id name
# MANUAL - (Advanced) Only use AVR Libc/Includes
#
# Configures the the build settings for the specified Arduino Board.
#
#=============================================================================#
function(get_arduino_flags COMPILE_FLAGS_VAR LINK_FLAGS_VAR BOARD_ID MANUAL)
set(BOARD_CORE ${${BOARD_ID}.build.core})
if(BOARD_CORE)
if(ARDUINO_SDK_VERSION MATCHES "([0-9]+)[.]([0-9]+)")
string(REPLACE "." "" ARDUINO_VERSION_DEFINE "${ARDUINO_SDK_VERSION}") # Normalize version (remove all periods)
set(ARDUINO_VERSION_DEFINE "")
if(CMAKE_MATCH_1 GREATER 0)
set(ARDUINO_VERSION_DEFINE "${CMAKE_MATCH_1}")
endif()
if(CMAKE_MATCH_2 GREATER 10)
set(ARDUINO_VERSION_DEFINE "${ARDUINO_VERSION_DEFINE}${CMAKE_MATCH_2}")
else()
set(ARDUINO_VERSION_DEFINE "${ARDUINO_VERSION_DEFINE}0${CMAKE_MATCH_2}")
endif()
else()
message("Invalid Arduino SDK Version (${ARDUINO_SDK_VERSION})")
endif()
# output
set(COMPILE_FLAGS "-DF_CPU=${${BOARD_ID}.build.f_cpu} -DARDUINO=${ARDUINO_VERSION_DEFINE} -mmcu=${${BOARD_ID}.build.mcu}")
if(DEFINED ${BOARD_ID}.build.vid)
set(COMPILE_FLAGS "${COMPILE_FLAGS} -DUSB_VID=${${BOARD_ID}.build.vid}")
endif()
if(DEFINED ${BOARD_ID}.build.pid)
set(COMPILE_FLAGS "${COMPILE_FLAGS} -DUSB_PID=${${BOARD_ID}.build.pid}")
endif()
if(NOT MANUAL)
set(COMPILE_FLAGS "${COMPILE_FLAGS} -I\"${${BOARD_CORE}.path}\" -I\"${ARDUINO_LIBRARIES_PATH}\"")
endif()
set(LINK_FLAGS "-mmcu=${${BOARD_ID}.build.mcu}")
if(ARDUINO_SDK_VERSION VERSION_GREATER 1.0 OR ARDUINO_SDK_VERSION VERSION_EQUAL 1.0)
if(NOT MANUAL)
set(PIN_HEADER ${${${BOARD_ID}.build.variant}.path})
if(PIN_HEADER)
set(COMPILE_FLAGS "${COMPILE_FLAGS} -I\"${PIN_HEADER}\"")
endif()
endif()
endif()
# output
set(${COMPILE_FLAGS_VAR} "${COMPILE_FLAGS}" PARENT_SCOPE)
set(${LINK_FLAGS_VAR} "${LINK_FLAGS}" PARENT_SCOPE)
else()
message(FATAL_ERROR "Invalid Arduino board ID (${BOARD_ID}), aborting.")
endif()
endfunction()
#=============================================================================#
# [PRIVATE/INTERNAL]
#
# setup_arduino_core(VAR_NAME BOARD_ID)
#
# VAR_NAME - Variable name that will hold the generated library name
# BOARD_ID - Arduino board id
#
# Creates the Arduino Core library for the specified board,
# each board gets it's own version of the library.
#
#=============================================================================#
function(setup_arduino_core VAR_NAME BOARD_ID)
set(CORE_LIB_NAME ${BOARD_ID}_CORE)
set(BOARD_CORE ${${BOARD_ID}.build.core})
if(BOARD_CORE)
if(NOT TARGET ${CORE_LIB_NAME})
set(BOARD_CORE_PATH ${${BOARD_CORE}.path})
find_sources(CORE_SRCS ${BOARD_CORE_PATH} True)
# Debian/Ubuntu fix
list(REMOVE_ITEM CORE_SRCS "${BOARD_CORE_PATH}/main.cxx")
add_library(${CORE_LIB_NAME} ${CORE_SRCS})
get_arduino_flags(ARDUINO_COMPILE_FLAGS ARDUINO_LINK_FLAGS ${BOARD_ID} FALSE)
set_target_properties(${CORE_LIB_NAME} PROPERTIES
COMPILE_FLAGS "${ARDUINO_COMPILE_FLAGS}"
LINK_FLAGS "${ARDUINO_LINK_FLAGS}")
endif()
set(${VAR_NAME} ${CORE_LIB_NAME} PARENT_SCOPE)
endif()
endfunction()
#=============================================================================#
# [PRIVATE/INTERNAL]
#
# find_arduino_libraries(VAR_NAME SRCS ARDLIBS)
#
# VAR_NAME - Variable name which will hold the results
# SRCS - Sources that will be analized
# ARDLIBS - Arduino libraries identified by name (e.g., Wire, SPI, Servo)
#
# returns a list of paths to libraries found.
#
# Finds all Arduino type libraries included in sources. Available libraries
# are ${ARDUINO_SDK_PATH}/libraries and ${CMAKE_CURRENT_SOURCE_DIR}.
#
# Also adds Arduino libraries specifically names in ALIBS. We add ".h" to the
# names and then process them just like the Arduino libraries found in the sources.
#
# A Arduino library is a folder that has the same name as the include header.
# For example, if we have a include "#include <LibraryName.h>" then the following
# directory structure is considered a Arduino library:
#
# LibraryName/
# |- LibraryName.h
# `- LibraryName.c
#
# If such a directory is found then all sources within that directory are considred
# to be part of that Arduino library.
#
#=============================================================================#
function(find_arduino_libraries VAR_NAME SRCS ARDLIBS)
set(ARDUINO_LIBS )
foreach(SRC ${SRCS})
# Skipping generated files. They are, probably, not exist yet.
# TODO: Maybe it's possible to skip only really nonexisting files,
# but then it wiil be less deterministic.
get_source_file_property(_srcfile_generated ${SRC} GENERATED)
# Workaround for sketches, which are marked as generated
get_source_file_property(_sketch_generated ${SRC} GENERATED_SKETCH)
if(NOT ${_srcfile_generated} OR ${_sketch_generated})
if(NOT (EXISTS ${SRC} OR
EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${SRC} OR
EXISTS ${CMAKE_CURRENT_BINARY_DIR}/${SRC}))
message(FATAL_ERROR "Invalid source file: ${SRC}")
endif()
file(STRINGS ${SRC} SRC_CONTENTS)
foreach(LIBNAME ${ARDLIBS})
list(APPEND SRC_CONTENTS "#include <${LIBNAME}.h>")
endforeach()
foreach(SRC_LINE ${SRC_CONTENTS})
if("${SRC_LINE}" MATCHES "^[ \t]*#[ \t]*include[ \t]*[<\"]([^>\"]*)[>\"]")
get_filename_component(INCLUDE_NAME ${CMAKE_MATCH_1} NAME_WE)
get_property(LIBRARY_SEARCH_PATH
DIRECTORY # Property Scope
PROPERTY LINK_DIRECTORIES)
foreach(LIB_SEARCH_PATH ${LIBRARY_SEARCH_PATH} ${ARDUINO_LIBRARIES_PATH} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/libraries ${ARDUINO_EXTRA_LIBRARIES_PATH})
if(EXISTS ${LIB_SEARCH_PATH}/${INCLUDE_NAME}/${CMAKE_MATCH_1})
list(APPEND ARDUINO_LIBS ${LIB_SEARCH_PATH}/${INCLUDE_NAME})
break()
endif()
if(EXISTS ${LIB_SEARCH_PATH}/${CMAKE_MATCH_1})
list(APPEND ARDUINO_LIBS ${LIB_SEARCH_PATH})
break()
endif()
endforeach()
endif()
endforeach()
endif()
endforeach()
if(ARDUINO_LIBS)
list(REMOVE_DUPLICATES ARDUINO_LIBS)
endif()
set(${VAR_NAME} ${ARDUINO_LIBS} PARENT_SCOPE)
endfunction()
#=============================================================================#
# [PRIVATE/INTERNAL]
#
# setup_arduino_library(VAR_NAME BOARD_ID LIB_PATH COMPILE_FLAGS LINK_FLAGS)
#
# VAR_NAME - Vairable wich will hold the generated library names
# BOARD_ID - Board ID
# LIB_PATH - Path of the library
# COMPILE_FLAGS - Compile flags
# LINK_FLAGS - Link flags
#
# Creates an Arduino library, with all it's library dependencies.
#
# ${LIB_NAME}_RECURSE controls if the library will recurse
# when looking for source files.
#
#=============================================================================#
# For known libraries can list recurse here
set(Wire_RECURSE True)
set(Ethernet_RECURSE True)
set(SD_RECURSE True)
function(setup_arduino_library VAR_NAME BOARD_ID LIB_PATH COMPILE_FLAGS LINK_FLAGS)
set(LIB_TARGETS)
set(LIB_INCLUDES)
get_filename_component(LIB_NAME ${LIB_PATH} NAME)
set(TARGET_LIB_NAME ${BOARD_ID}_${LIB_NAME})
if(NOT TARGET ${TARGET_LIB_NAME})
string(REGEX REPLACE ".*/" "" LIB_SHORT_NAME ${LIB_NAME})
# Detect if recursion is needed
if (NOT DEFINED ${LIB_SHORT_NAME}_RECURSE)
set(${LIB_SHORT_NAME}_RECURSE False)
endif()
find_sources(LIB_SRCS ${LIB_PATH} ${${LIB_SHORT_NAME}_RECURSE})
if(LIB_SRCS)
arduino_debug_msg("Generating Arduino ${LIB_NAME} library")
add_library(${TARGET_LIB_NAME} STATIC ${LIB_SRCS})
get_arduino_flags(ARDUINO_COMPILE_FLAGS ARDUINO_LINK_FLAGS ${BOARD_ID} FALSE)
find_arduino_libraries(LIB_DEPS "${LIB_SRCS}" "")
foreach(LIB_DEP ${LIB_DEPS})
setup_arduino_library(DEP_LIB_SRCS ${BOARD_ID} ${LIB_DEP} "${COMPILE_FLAGS}" "${LINK_FLAGS}")
list(APPEND LIB_TARGETS ${DEP_LIB_SRCS})
list(APPEND LIB_INCLUDES ${DEP_LIB_SRCS_INCLUDES})
endforeach()
if (LIB_INCLUDES)
string(REPLACE ";" " " LIB_INCLUDES "${LIB_INCLUDES}")
endif()
set_target_properties(${TARGET_LIB_NAME} PROPERTIES
COMPILE_FLAGS "${ARDUINO_COMPILE_FLAGS} ${LIB_INCLUDES} -I\"${LIB_PATH}\" -I\"${LIB_PATH}/utility\" ${COMPILE_FLAGS}"