allow splash image to be loaded from SD on Horus (#5463)
[opentx.git] / radio / src / CMakeLists.txt
blob987dd83aed08e633e48088b5e05392ba5e38cab2
1 include(CMakeForceCompiler)
2 include(Bitmaps)
4 set(PCB_TYPES X7 X9D X9D+ X9E X10 X12S 9X 9XR 9X128 9XR128 9X2561 GRUVIN9X MEGA2560 SKY9X 9XRPRO AR9X)
5 set(GUI_LANGUAGES CZ DE EN ES FR IT PT SK SE PL HU NL)
6 set(TTS_LANGUAGES CZ DE EN ES FR IT PT SK SE PL HU RU)
8 set(PCB "X9D+" CACHE STRING "Radio type, one of: ${PCB_TYPES}")
9 set_property(CACHE PCB PROPERTY STRINGS ${PCB_TYPES})
10 set(TRANSLATIONS "EN" CACHE STRING "Radio language, one of: ${GUI_LANGUAGES}")
11 set_property(CACHE TRANSLATIONS PROPERTY STRINGS ${GUI_LANGUAGES})
12 set(SPLASH "DEFAULT" CACHE STRING "Splash (DEFAULT/OFF/FRSKY)")
13 set_property(CACHE SPLASH PROPERTY STRINGS DEFAULT OFF FRSKY)
14 set(PPM_UNIT "PERCENT_PREC1" CACHE STRING "PPM display unit (US/PERCENT_PREC1/PERCENT_PREC0)")
15 set_property(CACHE PPM_UNIT PROPERTY STRINGS US PERCENT_PREC1 PERCENT_PREC0)
16 set(DEFAULT_MODE "" CACHE STRING "Default sticks mode")
17 set(FONT "STD" CACHE STRING "Choose font : STD or SQT5")
18 set_property(CACHE FONT PROPERTY STRINGS SQT5)
20 option(HELI "Heli menu" ON)
21 option(FLIGHT_MODES "Flight Modes" ON)
22 option(CURVES "Curves" ON)
23 option(GVARS "Global variables" OFF)
24 option(GUI "GUI enabled" ON)
25 option(PPM_CENTER_ADJUSTABLE "PPM center adjustable" ON)
26 option(PPM_LIMITS_SYMETRICAL "PPM limits symetrical" OFF)
27 option(OVERRIDE_CHANNEL_FUNCTION "OverrideChannel function available" ON)
28 option(DANGEROUS_MODULE_FUNCTIONS "Dangerous module functions (RangeCheck / Bind / Module OFF, etc.) available" OFF)
29 option(FAI "Competition mode (no telemetry)" OFF)
30 option(AUTOSOURCE "Automatic source detection in menus" ON)
31 option(AUTOSWITCH "Automatic switch detection in menus" ON)
32 option(SEMIHOSTING "Enable debugger semihosting" OFF)
33 option(JITTER_MEASURE "Enable ADC jitter measurement" OFF)
34 option(WATCHDOG_DISABLED "Disable hardware Watchdog" OFF)
35 if(SDL_FOUND)
36   option(SIMU_AUDIO "Enable simulator audio." ON)
37 endif()
38 option(SIMU_DISKIO "Enable disk IO simulation in simulator. Simulator will use FatFs module and simulated IO layer that  uses \"./sdcard.image\" file as image of SD card. This file must contain whole SD card from first to last sector" OFF)
39 option(SIMU_LUA_COMPILER "Pre-compile and save Lua scripts in simulator." ON)
40 option(FAS_PROTOTYPE "Support of old FAS prototypes (different resistors)" OFF)
41 option(TEMPLATES "Model templates menu" OFF)
42 option(TRACE_SIMPGMSPACE "Turn on traces in simpgmspace.cpp" ON)
43 option(TRACE_LUA_INTERNALS "Turn on traces for Lua internals" OFF)
44 option(FRSKY_STICKS "Reverse sticks for FrSky sticks" OFF)
45 option(NANO "Use nano newlib and binalloc")
46 option(NIGHTLY_BUILD_WARNING "Warn this is a nightly build" OFF)
47 option(USEHORUSBT "X9E BT module replaced by Horus BT module" OFF)
49 # since we reset all default CMAKE compiler flags for firmware builds, provide an alternate way for user to specify additional flags.
50 set(FIRMWARE_C_FLAGS "" CACHE STRING "Additional flags for firmware target c compiler (note: all CMAKE_C_FLAGS[_*] are ignored for firmware/bootloader).")
51 set(FIRMWARE_C_FLAGS_DEBUG "-g" CACHE STRING "Additional flags for firmware target (Debug config) c compiler (note: CMAKE_C_FLAGS_DEBUG is ignored for firmware/bootloader).")
52 set(FIRMWARE_CXX_FLAGS "" CACHE STRING "Additional flags for firmware target c++ compiler (note: all CMAKE_CXX_FLAGS[_*] are ignored for firmware/bootloader).")
53 set(FIRMWARE_CXX_FLAGS_DEBUG "-g" CACHE STRING "Additional flags for firmware target (Debug config) c++ compiler (note: CMAKE_CXX_FLAGS_DEBUG is ignored for firmware/bootloader).")
55 set(THIRDPARTY_DIR thirdparty)
56 set(LUA_DIR ${THIRDPARTY_DIR}/Lua/src)
57 set(COOS_DIR ${THIRDPARTY_DIR}/CoOS)
58 set(FATFS_DIR ${THIRDPARTY_DIR}/FatFs)
59 set(RADIO_BIN_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
61 configure_file(stamp.h.in stamp.h @ONLY)
63 add_subdirectory(translations)
64 add_subdirectory(bitmaps)
65 add_subdirectory(fonts)
67 set(EEPROM_VARIANT  0)
68 set(GVARS_VARIANT   1)
69 set(FRSKY_VARIANT   2)
70 set(3POS_VARIANT    4)
71 set(MAVLINK_VARIANT 8)
72 set(M2561_VARIANT   16384)
73 set(M128_VARIANT    32768)
75 set(FIRMWARE_DEPENDENCIES firmware_translations)
77 set(FATFS_SRC
78   ${FATFS_DIR}/ff.c
79   ${FATFS_DIR}/fattime.c
80   ${FATFS_DIR}/option/ccsbcs.c
83 if(PCB STREQUAL X12S OR PCB STREQUAL X10)
84   include(targets/horus/CMakeLists.txt)
85 elseif(PCB STREQUAL X9E OR PCB STREQUAL X9D+ OR PCB STREQUAL X9D OR PCB STREQUAL X7)
86   include(targets/taranis/CMakeLists.txt)
87 elseif(PCB STREQUAL SKY9X OR PCB STREQUAL 9XRPRO OR PCB STREQUAL AR9X)
88   include(targets/sky9x/CMakeLists.txt)
89 elseif(PCB STREQUAL 9X OR PCB STREQUAL 9XR OR PCB STREQUAL 9X128 OR PCB STREQUAL 9XR128)
90   include(targets/9x/CMakeLists.txt)
91 elseif(PCB STREQUAL 9X2561)
92   set(ARCH AVR)
93   set(MCU atmega2561)
94   string(TOLOWER ${PCB} FLAVOUR)
95   set(EEPROM EEPROM_RLC)
96   set(SDCARD NO)
97   add_definitions(-DEEPROM_VARIANT=${M2561_VARIANT})
98   set(GUI_DIR 128x64)
99   set(TARGET_DIR 9x)
100   set(FIRMWARE_DEPENDENCIES ${FIRMWARE_DEPENDENCIES} 9x_bitmaps)
101   add_definitions(-DPCBSTD -DPCB${PCB} -DCPUM2561)
102   set(TARGET_SRC ${TARGET_SRC} board.cpp)
103   set(GUI_SRC
104     ${GUI_SRC}
105     model_inputs_mixes.cpp
106     radio_diagkeys.cpp
107     radio_diaganas.cpp)
108   if(SP22)
109     add_definitions(-DSP22)
110   endif()
111   if(PWM_BACKLIGHT)
112     add_definitions(-DPWM_BACKLIGHT)
113   endif()
114   set(FIRMWARE_TARGET_SRC ${FIRMWARE_TARGET_SRC} lcd_driver.cpp)
115 elseif(PCB STREQUAL GRUVIN9X)
116   include(targets/gruvin9x/CMakeLists.txt)
117 elseif(PCB STREQUAL MEGA2560)
118   include(targets/mega2560/CMakeLists.txt)
119 else()
120   message(FATAL_ERROR "Unknown PCB '${PCB}'")
121 endif()
123 if(NOT PCB STREQUAL 9XR AND NOT PCB STREQUAL 9XRPRO)
124   option(DBLKEYS "Double Keys" ON)
125   if(DBLKEYS)
126     add_definitions(-DDBLKEYS)
127   endif()
128 endif()
130 if(CPU_TYPE STREQUAL STM32F4)
131   include(targets/common/arm/stm32/f4/CMakeLists.txt)
132 endif()
134 if(CPU_TYPE STREQUAL STM32F2)
135   include(targets/common/arm/stm32/f2/CMakeLists.txt)
136 endif()
138 if(CPU_FAMILY STREQUAL STM32)
139   include(targets/common/arm/stm32/CMakeLists.txt)
140 endif()
142 if(ARCH STREQUAL ARM)
143   include(targets/common/arm/CMakeLists.txt)
144 else()
145   include(targets/common/avr/CMakeLists.txt)
146 endif()
148 include_directories(targets/${TARGET_DIR} ${THIRDPARTY_DIR})
150 foreach(LANGUAGE ${GUI_LANGUAGES})
151   if(TRANSLATIONS STREQUAL ${LANGUAGE})
152     add_definitions(-DTRANSLATIONS_${LANGUAGE} -DTRANSLATIONS="${LANGUAGE}")
153   endif()
154 endforeach()
156 set(SRC ${SRC} debug.cpp)
158 if(${EEPROM} STREQUAL SDCARD)
159   set(SRC ${SRC} storage/storage_common.cpp storage/sdcard_raw.cpp)
160 elseif(${EEPROM} STREQUAL EEPROM_RLC)
161   set(SRC ${SRC} storage/storage_common.cpp storage/eeprom_common.cpp storage/eeprom_rlc.cpp)
162   add_definitions(-DEEPROM -DEEPROM_RLC)
163 else()
164   set(SRC ${SRC} storage/storage_common.cpp storage/eeprom_common.cpp storage/eeprom_raw.cpp)
165   add_definitions(-DEEPROM -DEEPROM_RAW)
166 endif()
168 if(ARCH STREQUAL ARM AND NOT PCB STREQUAL X12S AND NOT PCB STREQUAL X10)
169   set(SRC ${SRC} storage/eeprom_conversions.cpp)
170 endif()
172 add_definitions(-DFLAVOUR="${FLAVOUR}")
174 include_directories(${CMAKE_CURRENT_SOURCE_DIR})
175 include_directories(${CMAKE_CURRENT_BINARY_DIR})
176 include_directories(${CMAKE_CURRENT_BINARY_DIR}/bitmaps/${GUI_DIR})
178 add_subdirectory(lua)
179 include(gui/CMakeLists.txt)
181 if(RAMBACKUP)
182   add_definitions(-DRAMBACKUP)
183   set(SRC ${SRC} storage/rambackup.cpp storage/rlc.cpp)
184 endif()
186 if(TEMPLATES)
187   add_definitions(-DTEMPLATES)
188   set(SRC ${SRC} templates.cpp)
189   set(GUI_SRC ${GUI_SRC} model_templates.cpp)
190 endif()
192 if(NOT LUA STREQUAL NO)
193   add_definitions(-DLUA)
194   if(LUA_COMPILER)
195     add_definitions(-DLUA_COMPILER)
196   endif()
197   if(LUA_ALLOCATOR_TRACER AND DEBUG)
198     add_definitions(-DLUA_ALLOCATOR_TRACER)
199   endif()
200   if(NOT "${LUA_SCRIPT_LOAD_MODE}" STREQUAL "")
201     add_definitions(-DLUA_SCRIPT_LOAD_MODE="${LUA_SCRIPT_LOAD_MODE}")
202   endif()
203   include_directories(${LUA_DIR})
204   set(FIRMWARE_DEPENDENCIES ${FIRMWARE_DEPENDENCIES} ${LUA_EXPORT})
205   if(LUA STREQUAL YES)
206     add_definitions(-DLUA_MODEL_SCRIPTS)
207     set(GUI_SRC ${GUI_SRC} model_custom_scripts.cpp)
208   endif()
209   set(SRC ${SRC} lua/interface.cpp lua/api_general.cpp lua/api_lcd.cpp lua/api_model.cpp)
210   if(PCB STREQUAL X12S OR PCB STREQUAL X10)
211     set(SRC ${SRC} lua/widgets.cpp)
212   endif()
213   set(LUA_SRC lapi.c lcode.c lctype.c ldebug.c ldo.c ldump.c lfunc.c lgc.c llex.c lmem.c lobject.c lopcodes.c lparser.c
214     lstate.c lstring.c ltable.c lrotable.c ltm.c lundump.c lvm.c lzio.c linit.c
215     lbaselib.c lmathlib.c lbitlib.c loadlib.c lauxlib.c ltablib.c lcorolib.c liolib.c lstrlib.c
216     )
217   foreach(FILE ${LUA_SRC})
218     set(SRC ${SRC} ${LUA_DIR}/${FILE})
219   endforeach()
220   if(MSVC)
221     # silence lots of warnings regarding "insecure" use of functions (sprintf, strcpy, etc) from Lua code
222     set(WARNING_FLAGS "${WARNING_FLAGS} /D_CRT_SECURE_NO_WARNINGS")
223   endif()
224 endif()
226 if(HELI)
227   add_definitions(-DHELI)
228 endif()
230 if(FLIGHT_MODES)
231   add_definitions(-DFLIGHT_MODES)
232   set(GUI_SRC ${GUI_SRC} model_flightmodes.cpp)
233 endif()
235 if(CURVES)
236   add_definitions(-DCURVES)
237   set(SRC ${SRC} curves.cpp)
238 endif()
240 if(GVARS)
241   add_definitions(-DGVARS)
242   set(SRC ${SRC} gvars.cpp)
243   math(EXPR EEPROM_VARIANT ${EEPROM_VARIANT}+${GVARS_VARIANT})
244 endif()
246 if(FAI STREQUAL YES)
247   add_definitions(-DFAI)
248 elseif(FAI STREQUAL CHOICE)
249   add_definitions(-DFAI_CHOICE)
250 endif()
252 if(FAS_PROTOTYPE)
253   add_definitions(-DFAS_PROTOTYPE)
254 endif()
256 if(AUTOSOURCE)
257   add_definitions(-DAUTOSOURCE)
258 endif()
260 if(AUTOSWITCH)
261   add_definitions(-DAUTOSWITCH)
262 endif()
264 if(JITTER_MEASURE)
265   add_definitions(-DJITTER_MEASURE)
266 endif()
268 if(WATCHDOG_DISABLED)
269   add_definitions(-DWATCHDOG_DISABLED)
270 endif()
272 if(SIMU_AUDIO)
273   add_definitions(-DSIMU_AUDIO)
274 endif()
276 if(SDCARD)
277   add_definitions(-DSDCARD)
278   include_directories(${FATFS_DIR} ${FATFS_DIR}/option)
279   set(SRC ${SRC} sdcard.cpp rtc.cpp logs.cpp)
280   set(FIRMWARE_SRC ${FIRMWARE_SRC} ${FATFS_SRC})
281 endif()
283 if(SHUTDOWN_CONFIRMATION)
284   add_definitions(-DSHUTDOWN_CONFIRMATION)
285 endif()
287 if(PPM_UNIT STREQUAL US)
288   add_definitions(-DPPM_UNIT_US)
289 elseif(PPM_UNIT STREQUAL PERCENT_PREC1)
290   add_definitions(-DPPM_UNIT_PERCENT_PREC1)
291 else()
292   add_definitions(-DPPM_UNIT_PERCENT_PREC0)
293 endif()
295 if(PPM_CENTER_ADJUSTABLE)
296   add_definitions(-DPPM_CENTER_ADJUSTABLE)
297 endif()
299 if(PPM_LIMITS_SYMETRICAL)
300   add_definitions(-DPPM_LIMITS_SYMETRICAL)
301 endif()
303 if(OVERRIDE_CHANNEL_FUNCTION)
304   add_definitions(-DOVERRIDE_CHANNEL_FUNCTION)
305 endif()
307 if(DANGEROUS_MODULE_FUNCTIONS)
308   add_definitions(-DDANGEROUS_MODULE_FUNCTIONS)
309 endif()
311 if(SPLASH STREQUAL FRSKY)
312   add_definitions(-DSPLASH -DSPLASH_FRSKY)
313 elseif(NOT SPLASH STREQUAL OFF)
314   add_definitions(-DSPLASH)
315 endif()
317 if(NOT DEFAULT_MODE STREQUAL "")
318   add_definitions(-DDEFAULT_MODE=${DEFAULT_MODE})
319 endif()
321 if(TRACE_SIMPGMSPACE)
322   add_definitions(-DTRACE_SIMPGMSPACE)
323 endif()
325 if(TRACE_LUA_INTERNALS)
326   add_definitions(-DTRACE_LUA_INTERNALS_ENABLED)
327 endif()
329 if(FRSKY_STICKS)
330   add_definitions(-DFRSKY_STICKS)
331 endif()
333 if(NOT FONT STREQUAL "")
334   add_definitions(-DFONT=${DEFAULT_MODE})
335 endif()
337 if(EEPROM_VARIANT_NEEDED)
338   add_definitions(-DEEPROM_VARIANT=${EEPROM_VARIANT})
339 endif()
341 if(NIGHTLY_BUILD_WARNING)
342   add_definitions(-DNIGHTLY_BUILD_WARNING)
343 endif(NIGHTLY_BUILD_WARNING)
345 if(USEHORUSBT)
346   add_definitions(-DUSEHORUSBT)
347 endif(USEHORUSBT)
349 set(SRC
350   ${SRC}
351   opentx.cpp
352   functions.cpp
353   strhelpers.cpp
354   switches.cpp
355   mixer.cpp
356   stamp.cpp
357   timers.cpp
358   trainer_input.cpp
359   )
361 if(GUI)
362   add_definitions(-DGUI)
363   set(SRC
364     ${SRC}
365     keys.cpp
366     maths.cpp
367     translations.cpp
368     gui/gui_common.cpp
369     )
370   foreach(FILE ${GUI_SRC})
371     set(SRC ${SRC} gui/${GUI_DIR}/${FILE})
372   endforeach()
373   if(ARCH STREQUAL ARM)
374     set(SRC
375       ${SRC}
376       gui/gui_common_arm.cpp
377       )
378   endif()
379 endif()
381 foreach(FILE ${TARGET_SRC})
382   set(SRC targets/${TARGET_DIR}/${FILE} ${SRC})
383 endforeach()
385 foreach(FILE ${FIRMWARE_TARGET_SRC})
386   set(FIRMWARE_SRC ${FIRMWARE_SRC} targets/${TARGET_DIR}/${FILE})
387 endforeach()
389 foreach(FILE ${PULSES_SRC})
390   set(SRC ${SRC} pulses/${FILE})
391 endforeach()
393 add_definitions(-DCORRECT_NEGATIVE_SHIFTS)
395 if(NOT MSVC)
396   set(WARNING_FLAGS "${WARNING_FLAGS} -Wall -Wno-strict-aliasing -Wformat -Wreturn-type -Wunused -Wuninitialized -Wunknown-pragmas -Wno-switch -Wtype-limits")
397   if(WARNINGS_AS_ERRORS)
398     set(WARNING_FLAGS "${WARNING_FLAGS} -Werror")
399   endif(WARNINGS_AS_ERRORS)
400 else()
401   add_definitions(-DHAVE_STRUCT_TIMESPEC)  # this is for pthread.h
402   if(WARNINGS_AS_ERRORS)
403     set(WARNING_FLAGS "${WARNING_FLAGS} /WX")
404   endif(WARNINGS_AS_ERRORS)
405 endif()
407 add_subdirectory(targets/simu)
408 if(NOT MSVC)
409   add_subdirectory(tests)
410 endif()
412 set(SRC ${SRC} ${FIRMWARE_SRC})
415 ##### firmware target #####
417 option(FIRMWARE_TARGET "Configure Firmware target (can be turned of for compiling Companion only)" ON)
418 if(NOT FIRMWARE_TARGET)
419   message(STATUS "firmware target disabled")
420   return()
421 endif()
423 set(CMAKE_EXECUTABLE_SUFFIX ".elf")
424 # trick to remove the -rdynamic and --out-implib issues
425 set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
426 set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
427 # similar for -mmacosx-version-min="
428 set(CMAKE_C_OSX_DEPLOYMENT_TARGET_FLAG "")
429 set(CMAKE_CXX_OSX_DEPLOYMENT_TARGET_FLAG "")
430 # reset some CMake default flags which assume optimization levels/etc
431 set(CMAKE_C_FLAGS "${FIRMWARE_C_FLAGS}")
432 set(CMAKE_C_FLAGS_DEBUG "${FIRMWARE_C_FLAGS_DEBUG}")
433 set(CMAKE_C_FLAGS_RELEASE "")
434 set(CMAKE_C_FLAGS_MINSIZEREL "")
435 set(CMAKE_C_FLAGS_RELWITHDEBINFO "")
436 set(CMAKE_CXX_FLAGS "${FIRMWARE_CXX_FLAGS}")
437 set(CMAKE_CXX_FLAGS_DEBUG "${FIRMWARE_CXX_FLAGS_DEBUG}")
438 set(CMAKE_CXX_FLAGS_RELEASE "")
439 set(CMAKE_CXX_FLAGS_MINSIZEREL "")
440 set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "")
441 # customize linker command
442 set(CMAKE_EXE_LINKER_FLAGS "")
443 set(CMAKE_C_LINK_EXECUTABLE "<CMAKE_C_COMPILER> <FLAGS> <LINK_FLAGS> <OBJECTS>  -o <TARGET>")
444 set(CMAKE_CXX_LINK_EXECUTABLE "<CMAKE_CXX_COMPILER> <FLAGS> <LINK_FLAGS> <OBJECTS>  -o <TARGET>")
446 use_cxx11()  # ensure gnu++11 in CXX_FLAGS with CMake < 3.1
448 find_program(BASH bash HINTS "c:/cygwin/bin/" "c:/msys/bin/" DOC "bash shell for firmware size report (eg. c:/cygwin/bin/bash.exe on Windows).")
450 if(NOT MSVC)
451   set(OPT s)
453   if(ARCH STREQUAL ARM)
455     enable_language(ASM)
456     set(CMAKE_C_COMPILER arm-none-eabi-gcc)
457     set(CMAKE_CXX_COMPILER arm-none-eabi-g++)
458     set(CMAKE_ASM_COMPILER arm-none-eabi-as)
460     set(CMAKE_SYSTEM_NAME Generic)
461     set(CMAKE_SYSTEM_VERSION 1)
462     set(CMAKE_ASM_COMPILE_OBJECT "<CMAKE_ASM_COMPILER> -o <OBJECT> <SOURCE>")
464     set(COMMON_FLAGS "-mcpu=${MCU} -mthumb -fomit-frame-pointer -fverbose-asm -Wa,-ahlms=firmware.lst -O${OPT} -gdwarf-2 -DHSE_VALUE=${HSE_VALUE} -fno-exceptions -fdata-sections -ffunction-sections ${WARNING_FLAGS}")
465     set(COMMON_FLAGS "${COMMON_FLAGS} ${FPU_FLAGS}")
467     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COMMON_FLAGS} -Wimplicit")
468     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMMON_FLAGS}")
470     # these are in addition to CMAKE_CXX_FLAGS
471     set(CMAKE_EXE_LINKER_FLAGS "-lm -T${RADIO_SRC_DIRECTORY}/${LINKER_SCRIPT} -Wl,-Map=firmware.map,--cref,--no-warn-mismatch,--gc-sections")
472     if(SEMIHOSTING)
473       add_definitions(-DSEMIHOSTING)
474       set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --specs=rdimon.specs")
475     endif()
476     # Use newlib nano, which saves a few kilobytes.
477     if(NOT NANO STREQUAL NO)
478       set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --specs=nano.specs")
479       if(NOT LUA STREQUAL NO)
480         # Lua needs %g and %f
481         set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -u _printf_float -u _scanf_float")
482       endif()
483       # Nano's malloc does work well with lua, use our own
484       add_definitions(-DUSE_BIN_ALLOCATOR)
485       set(SRC ${SRC} bin_allocator.cpp)
486     endif()
489     if(PCB STREQUAL X9D OR PCB STREQUAL X9D+ OR PCB STREQUAL X9E OR PCB STREQUAL X7 OR PCB STREQUAL X10 OR PCB STREQUAL X12S)
490       add_subdirectory(targets/common/arm/stm32/bootloader)
491       include_directories(${CMAKE_CURRENT_BINARY_DIR}/targets/common/arm/stm32/bootloader)
492       set(FIRMWARE_DEPENDENCIES ${FIRMWARE_DEPENDENCIES} bootloader)
493     elseif(OPENRC_BOOTLOADER)
494       # We fetch Mike's bootloader as it is not included inside OpenTX
495       message(STATUS "Downloading Mike's bootloader: https://github.com/MikeBland/OpenRcBootloader/releases/download/V1.9/${OPENRC_BOOTLOADER}")
496       file(DOWNLOAD https://github.com/MikeBland/OpenRcBootloader/releases/download/V1.9/${OPENRC_BOOTLOADER} ${CMAKE_CURRENT_BINARY_DIR}/bootloader.lbm STATUS status)
497       list(GET status 0 error_code)
498       if(error_code)
499         list(GET status 1 error_message)
500         message(FATAL_ERROR "Download failed: ${error_message}")
501       endif()
502     endif()
504     add_executable(firmware ${SRC} ${FIRMWARE_HEADERS})
505     link_libraries(firmware -lstdc++)
506     add_dependencies(firmware ${FIRMWARE_DEPENDENCIES})
508     add_custom_command(
509       TARGET firmware POST_BUILD
510       COMMAND arm-none-eabi-objcopy -O binary firmware.elf firmware.bin
511       WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
512     )
514     find_program(GAWK gawk HINTS "c:/cygwin/bin/" "c:/msys/bin/" DOC "gawk for firmware size report.")  # OS X might only have "awk"
515     if (BASH AND GAWK)
516       add_custom_target(firmware-size
517         COMMAND ${BASH} -kc '${RADIO_DIRECTORY}/util/elf-size-report.sh --mcu=${CPU_TYPE_FULL} ${SIZE_TARGET_MEM_DEFINE} firmware.elf'
518         DEPENDS firmware
519         WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
520         )
521     else()
522       add_custom_target(firmware-size
523         COMMAND arm-none-eabi-size -A firmware.elf
524         DEPENDS firmware
525         WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
526         )
527     endif()
529   else()  # AVR
531     set(CMAKE_C_COMPILER avr-gcc)
532     set(CMAKE_CXX_COMPILER avr-gcc)
534     set(COMMON_FLAGS "-mmcu=${MCU} -DF_CPU=16000000UL -O${OPT} -gdwarf-2 -fno-exceptions -flto -fwhole-program -fno-inline-small-functions -mstrict-X ${WARNING_FLAGS}")
535     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COMMON_FLAGS}")
536     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMMON_FLAGS}")
537     set(CMAKE_EXE_LINKER_FLAGS "-Wno-uninitialized")  # bogus "uninitialized" warnings from ld
539     include_directories(storage gui/${GUI_DIR})
541     set(ALLSRC ${PROJECT_BINARY_DIR}/allsrc.cpp)
542     if (BASH AND NOT WIN32)
543       add_custom_command(
544         OUTPUT ${ALLSRC}
545         COMMAND ${BASH} -kc 'rm -f ${ALLSRC}\; for f in ${SRC}\; do echo "\\# 1 \\\"$$f\\\"" >> ${ALLSRC}\; cat "$$f" >> ${ALLSRC}\; done'
546         WORKING_DIRECTORY ${RADIO_SRC_DIRECTORY}
547         DEPENDS ${SRC}
548         )
549     elseif(WIN32)
550       string(REPLACE "/" "\\" SRC_WIN "${SRC}")
551       string(REPLACE "/" "\\" ALLSRC_WIN "${ALLSRC}")
552       set(ALLSRC_BAT
553           "setlocal EnableDelayedExpansion"
554           "del \"${ALLSRC_WIN}\""
555           "for %%f in (${SRC_WIN}) do ("
556             "set p=\"%%f\""
557             "set p=!p:^\\=/! \necho \# 1 !p! >> \"${ALLSRC_WIN}\""
558             "type %%f >> \"${ALLSRC_WIN}\""
559           ")\n"
560         )
561       string(REPLACE ";" "\n" ALLSRC_BAT "${ALLSRC_BAT}")
562       file(WRITE "${ALLSRC}.bat" "${ALLSRC_BAT}")
563       add_custom_command(
564         OUTPUT ${ALLSRC}
565         COMMAND "${ALLSRC}.bat"
566         WORKING_DIRECTORY ${RADIO_SRC_DIRECTORY}
567         DEPENDS ${SRC}
568         )
569     else()
570       set(ALLSRC ${SRC})
571     endif()
572     add_executable(firmware ${ALLSRC})
574     add_dependencies(firmware ${FIRMWARE_DEPENDENCIES})
576     add_custom_command(
577       TARGET firmware POST_BUILD
578       COMMAND avr-objcopy -O ihex -R .eeprom firmware.elf firmware.hex
579       WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
580       )
582     add_custom_target(firmware.lss
583       COMMAND avr-objdump -h -S firmware.elf > firmware.lss
584       DEPENDS firmware
585       WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
586       )
588     add_custom_target(firmware-size
589       COMMAND avr-size --format=avr --mcu=${MCU} firmware.elf
590       DEPENDS firmware
591       WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
592       )
594   endif()  # ARM or AVR
596   if(CPU_FAMILY STREQUAL STM32)
597     add_custom_target(flash
598       COMMAND dfu-util --alt 0 --dfuse-address 0x08000000:leave -d 0483:df11 -D firmware.bin
599       DEPENDS firmware
600       WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
601     )
603     add_custom_target(jtag-flash
604       COMMAND st-flash --reset write firmware.bin 0x8000000
605       DEPENDS firmware
606       WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
607       )
608   else()
609     add_custom_target(flash
610       COMMAND avrdude -c usbasp -p ${AVRDUDE_MCU} -U flash:w:firmware.hex:i
611       DEPENDS firmware
612       WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
613       )
614   endif()
616   PrintTargetReport("firmware")
618 endif(NOT MSVC)