CMakeLists.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. cmake_minimum_required(VERSION 3.15)
  2. include(cmake/Utilities.cmake)
  3. set (CMAKE_CXX_STANDARD 11)
  4. MESSAGE(WARNING "
  5. ***************** YOUR ATTENTION PLEASE *****************
  6. CMake support is experimental. There is no guarantee at this time. If you have problems you are encouraged to fall back to the tried-and-true methods.
  7. *********************** THANK YOU **********************
  8. We now return to your regularly scheduled Firmware Build."
  9. )
  10. OPTION(SECONDARY_LANGUAGES "Secondary language support in the firmware" ON)
  11. SET(MAIN_LANGUAGES cs de es fr it pl CACHE STRING "The list of 'main' languages to be included, in the correct order")
  12. SET(COMMUNITY_LANGUAGES nl ro hu hr sk sv no CACHE STRING "The list of community languages to be included, in the correct order")
  13. SET(SELECTED_LANGUAGES ${MAIN_LANGUAGES} ${COMMUNITY_LANGUAGES})
  14. get_dependency_directory(prusa3dboards PRUSA_BOARDS_DIR)
  15. project(Prusa-Firmware)
  16. FILE(STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/Firmware/config.h MAX_SIZE_LINE REGEX "^#define \+LANG_SIZE_RESERVED \+")
  17. STRING(REGEX MATCH "0x[0-9]+" MAX_SIZE_HEX "${MAX_SIZE_LINE}")
  18. math(EXPR LANG_MAX_SIZE "${MAX_SIZE_HEX}" OUTPUT_FORMAT DECIMAL)
  19. message("Language maximum size (from config.h): ${LANG_MAX_SIZE} bytes")
  20. set (LANG_BIN_MAX 249856) # Ditto, this in xflash_layout.h but needs invocation of the preprocessor... :-/
  21. get_recommended_gcc_version(RECOMMENDED_TOOLCHAIN_VERSION)
  22. if(CMAKE_CROSSCOMPILING AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL
  23. ${RECOMMENDED_TOOLCHAIN_VERSION}
  24. )
  25. message(WARNING "Recommended AVR toolchain is ${RECOMMENDED_TOOLCHAIN_VERSION}"
  26. ", but you have ${CMAKE_CXX_COMPILER_VERSION}"
  27. )
  28. elseif(NOT CMAKE_CROSSCOMPILING AND NOT CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  29. message(
  30. WARNING
  31. "Recommended compiler for host tools and unittests is GCC, you have ${CMAKE_CXX_COMPILER_ID}."
  32. )
  33. endif()
  34. # append custom C/C++ flags
  35. if(CUSTOM_COMPILE_OPTIONS)
  36. string(REPLACE " " ";" CUSTOM_COMPILE_OPTIONS "${CUSTOM_COMPILE_OPTIONS}")
  37. add_compile_options(${CUSTOM_COMPILE_OPTIONS})
  38. endif()
  39. #
  40. # Global Compiler & Linker Configuration
  41. #
  42. # include symbols
  43. add_compile_options(-g)
  44. # optimizations
  45. if(CMAKE_CROSSCOMPILING)
  46. if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  47. add_compile_options(-Og)
  48. else()
  49. add_compile_options(-Os)
  50. endif()
  51. # mcu related settings
  52. set(MCU_FLAGS -mmcu=atmega2560 -DF_CPU=16000000L)
  53. add_compile_options(${MCU_FLAGS})
  54. add_link_options(${MCU_FLAGS})
  55. # split and gc sections
  56. add_compile_options(-ffunction-sections -fdata-sections)
  57. add_link_options(-Wl,--gc-sections)
  58. # disable exceptions and related metadata
  59. add_compile_options(-fno-exceptions -fno-unwind-tables)
  60. add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>)
  61. add_link_options(-Wl,--defsym,__exidx_start=0,--defsym,__exidx_end=0)
  62. else()
  63. if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  64. add_compile_options(-O0)
  65. else()
  66. add_compile_options(-O2)
  67. endif()
  68. endif()
  69. # enable all warnings (well, not all, but some)
  70. add_compile_options(-Wall -Wsign-compare)
  71. add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-std=c++14>)
  72. # support _DEBUG macro (some code uses to recognize debug builds)
  73. if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  74. add_compile_definitions(_DEBUG)
  75. endif()
  76. #
  77. # Firmware - get file lists.
  78. #
  79. file(GLOB FW_SOURCES RELATIVE ${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/Firmware/*.c*)
  80. file(GLOB FW_HEADERS RELATIVE ${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/Firmware/*.h*)
  81. file(GLOB AVR_SOURCES RELATIVE ${PROJECT_SOURCE_DIR} ${PRUSA_BOARDS_DIR}/cores/prusa_einsy_rambo/*.c*)
  82. # Setup language resources:
  83. # file(GLOB LANG_VARIANTS RELATIVE ${PROJECT_SOURCE_DIR}/lang/po ${PROJECT_SOURCE_DIR}/lang/po/Firmware_??.po)
  84. # string(REPLACE "Firmware_" "" LANG_VARIANTS "${LANG_VARIANTS}")
  85. # string(REPLACE ".po" "" LANG_VARIANTS "${LANG_VARIANTS}")
  86. # list(SORT LANG_VARIANTS)
  87. # message("Languages found: ${LANG_VARIANTS}")
  88. add_library(avr_core STATIC ${AVR_SOURCES})
  89. target_include_directories(avr_core PRIVATE
  90. ${PRUSA_BOARDS_DIR}/cores/prusa_einsy_rambo/
  91. ${PRUSA_BOARDS_DIR}/variants/prusa_einsy_rambo/
  92. )
  93. target_compile_options(avr_core PUBLIC -mmcu=atmega2560)
  94. # Meta target to build absolutely everything
  95. add_custom_target(ALL_FIRMWARE)
  96. function(fw_add_variant variant_name)
  97. # Create the Configuration_Prusa.h for this variant so it can be #included.
  98. set(VARIANT_CFG_DIR "${CMAKE_CURRENT_BINARY_DIR}/${variant_name}_include")
  99. set(VARIANT_CFG_FILE "${VARIANT_CFG_DIR}/Configuration_prusa.h")
  100. add_custom_command(OUTPUT ${VARIANT_CFG_FILE}
  101. COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/Firmware/variants/${variant_name}.h ${VARIANT_CFG_FILE}
  102. COMMENT "Generating Configuration_prusa.h for ${variant_name}"
  103. BYPRODUCTS ${VARIANT_CFG_DIR}
  104. )
  105. STRING(REPLACE "1_75mm_" "" variant_name "${variant_name}")
  106. STRING(REPLACE "-E3Dv6full" "" variant_name "${variant_name}")
  107. add_executable(${variant_name} ${FW_SOURCES} ${FW_HEADERS} ${VARIANT_CFG_FILE})
  108. set_target_properties(${variant_name} PROPERTIES CXX_STANDARD 14)
  109. target_include_directories(${variant_name} PRIVATE Firmware
  110. ${PRUSA_BOARDS_DIR}/cores/prusa_einsy_rambo/
  111. ${PRUSA_BOARDS_DIR}/variants/prusa_einsy_rambo/
  112. ${VARIANT_CFG_DIR} # Include the header for this variant.
  113. )
  114. # # configure linker script
  115. set(LINKER_SCRIPT ${PRUSA_BOARDS_DIR}/ldscripts/avr6.xn)
  116. target_link_options(${variant_name} PUBLIC -Wl,-T,${LINKER_SCRIPT})
  117. # limit the text section to 248K (256K - 8k reserved for the bootloader)
  118. target_link_options(${variant_name} PUBLIC -Wl,--defsym=__TEXT_REGION_LENGTH__=248K)
  119. # generate firmware.bin file
  120. objcopy(${variant_name} "ihex" ".hex")
  121. # produce ASM listing. Note we also specify the .map as a byproduct so it gets cleaned
  122. # because link_options doesn't have a "generated outputs" feature.
  123. add_custom_command(
  124. TARGET ${variant_name} POST_BUILD COMMAND ${CMAKE_OBJDUMP} -CSd ${variant_name} > ${variant_name}.asm
  125. BYPRODUCTS ${variant_name}.asm ${variant_name}.map
  126. )
  127. # inform about the firmware's size in terminal
  128. add_custom_command(
  129. TARGET ${variant_name} POST_BUILD COMMAND ${CMAKE_SIZE_UTIL} -C --mcu=atmega2560 ${variant_name}
  130. )
  131. report_size(${variant_name})
  132. # generate linker map file
  133. target_link_options(${variant_name} PUBLIC -Wl,-Map=${variant_name}.map)
  134. target_compile_options(${variant_name} PRIVATE) # turn this on for lolz -Wdouble-promotion)
  135. target_compile_definitions(${variant_name} PRIVATE
  136. ARDUINO=10600
  137. __AVR_ATmega2560__
  138. CMAKE_LANG_CONTROL
  139. )
  140. target_link_libraries(${variant_name} avr_core)
  141. if (SECONDARY_LANGUAGES)
  142. target_compile_definitions(${variant_name} PUBLIC LANG_MODE=1)
  143. else()
  144. target_compile_definitions(${variant_name} PUBLIC LANG_MODE=0)
  145. add_dependencies(ALL_FIRMWARE "${variant_name}")
  146. return() #Done, if no languages there's nothing else to do.
  147. endif()
  148. #Construct language map
  149. set(LANG_TMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/lang)
  150. set(LANG_MAP ${LANG_TMP_DIR}/${variant_name}_lang.map)
  151. set(LANG_FWBIN ${CMAKE_BINARY_DIR}/${variant_name}.bin)
  152. set(LANG_FINAL_BIN ${LANG_TMP_DIR}/${variant_name}_lang.bin)
  153. set(LANG_FINAL_HEX ${LANG_TMP_DIR}/${variant_name}_lang.hex)
  154. add_custom_command(OUTPUT ${LANG_FWBIN}
  155. COMMAND "${CMAKE_OBJCOPY}" -I ihex -O binary ${CMAKE_BINARY_DIR}/${variant_name}.hex ${LANG_FWBIN}
  156. DEPENDS ${variant_name}
  157. )
  158. add_custom_command(OUTPUT ${LANG_MAP}
  159. COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/lang/lang-map.py "${CMAKE_BINARY_DIR}/${variant_name}" "${LANG_FWBIN}" > "${LANG_MAP}"
  160. DEPENDS ${LANG_FWBIN}
  161. )
  162. set(LANG_BINS "")
  163. foreach (LANG IN LISTS SELECTED_LANGUAGES)
  164. set(LANG_BIN ${LANG_TMP_DIR}/${variant_name}_${LANG}.bin)
  165. set(PO_FILE "${CMAKE_CURRENT_SOURCE_DIR}/lang/po/Firmware_${LANG}.po")
  166. add_custom_command(OUTPUT ${LANG_BIN}
  167. # COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/lang/lang-check.py --no-warning --map "${LANG_MAP}" "${PO_FILE}"
  168. # COMMAND ${CMAKE_COMMAND} -E echo "Building lang_${LANG}.bin"
  169. COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/lang/lang-build.py ${LANG_MAP} ${PO_FILE} ${LANG_BIN}
  170. # Check bin size:
  171. COMMAND ${CMAKE_COMMAND} -DLANG_MAX_SIZE=${LANG_MAX_SIZE}
  172. -DLANG_FILE=${LANG_BIN}
  173. -P ${PROJECT_CMAKE_DIR}/Check_lang_size.cmake
  174. DEPENDS ${LANG_MAP}
  175. COMMENT "Generating ${variant_name}_${LANG}.bin from .po"
  176. )
  177. LIST(APPEND LANG_BINS ${LANG_BIN})
  178. endforeach()
  179. string(FIND ${variant_name} "MK3" HAS_XFLASH)
  180. if (${HAS_XFLASH} GREATER_EQUAL 0)
  181. if (WIN32)
  182. STRING(REPLACE ";" " + " LANG_CMD_TMP "${LANG_BINS}")
  183. add_custom_command( OUTPUT ${LANG_FINAL_BIN}
  184. COMMAND copy /b ${LANG_CMD_TMP} ${LANG_FINAL_BIN}
  185. DEPENDS ${LANG_BINS}
  186. COMMENT "Merging language binaries (W32)"
  187. )
  188. else()
  189. add_custom_command( OUTPUT ${LANG_FINAL_BIN}
  190. COMMAND cat ${LANG_BINS} > ${LANG_FINAL_BIN}
  191. DEPENDS ${LANG_BINS}
  192. COMMENT "Merging language binaries (Non-W32)"
  193. )
  194. endif()
  195. add_custom_command( OUTPUT ${LANG_FINAL_BIN}
  196. COMMAND ${CMAKE_COMMAND} -DLANG_MAX_SIZE=${LANG_BIN_MAX}
  197. -DLANG_FILE=${LANG_FINAL_BIN}
  198. -P ${PROJECT_CMAKE_DIR}/Check_final_lang_bin_size.cmake
  199. APPEND
  200. )
  201. add_custom_command( OUTPUT ${LANG_FINAL_HEX}
  202. COMMAND ${CMAKE_OBJCOPY} -I binary -O ihex ${LANG_FINAL_BIN} ${LANG_FINAL_HEX}
  203. DEPENDS ${LANG_FINAL_BIN}
  204. COMMENT "Generating Hex for language data"
  205. )
  206. set(LANG_HEX ${CMAKE_BINARY_DIR}/${variant_name}-lang.hex)
  207. if (WIN32)
  208. SET(TEXT_MERGE_CMD "type")
  209. else()
  210. SET(TEXT_MERGE_CMD "cat")
  211. endif()
  212. add_custom_target(${variant_name}-languages
  213. COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/${variant_name}.hex ${LANG_HEX}
  214. COMMAND ${TEXT_MERGE_CMD} ${LANG_FINAL_HEX} >> ${LANG_HEX}
  215. COMMENT "Generating final ${variant_name}-lang.hex"
  216. BYPRODUCTS ${LANG_HEX}
  217. DEPENDS ${LANG_FINAL_HEX}
  218. )
  219. add_dependencies(ALL_FIRMWARE ${variant_name}-languages)
  220. else()
  221. set (ALL_VARIANT_HEXES "")
  222. # Non-xflash, e.g. MK2.5
  223. foreach(LANG IN LISTS SELECTED_LANGUAGES)
  224. SET(LANG_HEX_FN ${variant_name}-en_${LANG})
  225. SET(LANG_HEX ${CMAKE_BINARY_DIR}/${LANG_HEX_FN}.hex)
  226. SET(LANG_BIN ${LANG_TMP_DIR}/${variant_name}_${LANG}.bin)
  227. SET(LANG_FWBIN_TMP ${LANG_TMP_DIR}/${variant_name}-en_${LANG}.bin)
  228. #Intermediate 2-lang bin
  229. add_custom_command(OUTPUT ${LANG_FWBIN_TMP}
  230. COMMAND ${CMAKE_COMMAND} -E copy ${LANG_FWBIN} ${LANG_FWBIN_TMP}
  231. COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/lang/lang-patchsec.py ${CMAKE_BINARY_DIR}/${variant_name} ${LANG_BIN} ${LANG_FWBIN_TMP}
  232. DEPENDS ${LANG_FWBIN} ${LANG_BIN}
  233. COMMENT "Generating ${variant_name}-en_${LANG}.bin"
  234. )
  235. #Final hex:
  236. add_custom_target(${LANG_HEX_FN}
  237. COMMAND ${CMAKE_OBJCOPY} -I binary -O ihex ${LANG_FWBIN_TMP} ${LANG_HEX}
  238. BYPRODUCTS ${LANG_HEX}
  239. DEPENDS ${LANG_FWBIN_TMP}
  240. COMMENT "Creating ${LANG_HEX_FN}.hex"
  241. )
  242. LIST(APPEND ALL_VARIANT_HEXES ${LANG_HEX})
  243. endforeach()
  244. add_custom_target("${variant_name}-All-Languages"
  245. DEPENDS ${ALL_VARIANT_HEXES}
  246. )
  247. add_dependencies(ALL_FIRMWARE "${variant_name}-All-Languages")
  248. endif()
  249. endfunction()
  250. if(CMAKE_CROSSCOMPILING)
  251. file(GLOB FW_VARIANTS RELATIVE ${PROJECT_SOURCE_DIR}/Firmware/variants ${PROJECT_SOURCE_DIR}/Firmware/variants/*.h)
  252. foreach(THIS_VAR IN LISTS FW_VARIANTS)
  253. string(REPLACE ".h" "" TRIMMED_NAME "${THIS_VAR}")
  254. message("Variant added: ${TRIMMED_NAME}")
  255. fw_add_variant(${TRIMMED_NAME})
  256. endforeach(THIS_VAR IN LISTS FW_VARIANTS)
  257. endif()
  258. if(NOT CMAKE_CROSSCOMPILING)
  259. # do not build the firmware by default (tests are the focus if not crosscompiling)
  260. project(cmake_test)
  261. # Prepare "Catch" library for other executables
  262. set(CATCH_INCLUDE_DIR Catch2)
  263. add_library(Catch INTERFACE)
  264. target_include_directories(Catch INTERFACE ${CATCH_INCLUDE_DIR})
  265. # Make test executable
  266. set(TEST_SOURCES
  267. Tests/tests.cpp
  268. Tests/Example_test.cpp
  269. Tests/Timer_test.cpp
  270. Tests/AutoDeplete_test.cpp
  271. Tests/PrusaStatistics_test.cpp
  272. Firmware/Timer.cpp
  273. Firmware/AutoDeplete.cpp
  274. )
  275. add_executable(tests ${TEST_SOURCES})
  276. target_include_directories(tests PRIVATE Tests)
  277. target_link_libraries(tests Catch)
  278. endif()