Kiibohd Controller
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

modules.cmake 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. ###| CMAKE Kiibohd Controller Source Configurator |###
  2. #
  3. # Written by Jacob Alexander in 2011-2014 for the Kiibohd Controller
  4. #
  5. # Released into the Public Domain
  6. #
  7. ###
  8. ###
  9. # CMake Custom Modules Path
  10. #
  11. set ( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/Lib/CMake/" )
  12. ###
  13. # Module Overrides (Used in the buildall.bash script)
  14. #
  15. if ( ( DEFINED ScanModuleOverride ) AND ( EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/Scan/${ScanModuleOverride} ) )
  16. set( ScanModule ${ScanModuleOverride} )
  17. endif ()
  18. ###
  19. # Path Setup
  20. #
  21. set( ScanModulePath "Scan/${ScanModule}" )
  22. set( MacroModulePath "Macro/${MacroModule}" )
  23. set( OutputModulePath "Output/${OutputModule}" )
  24. set( DebugModulePath "Debug/${DebugModule}" )
  25. #| Top-level directory adjustment
  26. set( HEAD_DIR "${CMAKE_CURRENT_SOURCE_DIR}" )
  27. ###
  28. # Module Check Function
  29. #
  30. #| Usage:
  31. #| PathPrepend( ModulePath <ListOfFamiliesSupported> )
  32. #| Uses the ${COMPILER_FAMILY} variable
  33. function( ModuleCompatibility ModulePath )
  34. foreach( mod_var ${ARGN} )
  35. if ( ${mod_var} STREQUAL ${COMPILER_FAMILY} )
  36. # Module found, no need to scan further
  37. return()
  38. endif ()
  39. endforeach()
  40. message( FATAL_ERROR "${ModulePath} does not support the ${COMPILER_FAMILY} family..." )
  41. endfunction()
  42. ###
  43. # Module Configuration
  44. #
  45. #| Additional options, usually define settings
  46. add_definitions()
  47. #| Include path for each of the modules
  48. add_definitions(
  49. -I${HEAD_DIR}/${ScanModulePath}
  50. -I${HEAD_DIR}/${MacroModulePath}
  51. -I${HEAD_DIR}/${OutputModulePath}
  52. -I${HEAD_DIR}/${DebugModulePath}
  53. )
  54. ###
  55. # Module Processing
  56. #
  57. #| Go through lists of sources and append paths
  58. #| Usage:
  59. #| PathPrepend( OutputListOfSources <Prepend Path> <InputListOfSources> )
  60. macro( PathPrepend Output SourcesPath )
  61. unset( tmpSource )
  62. # Loop through items
  63. foreach( item ${ARGN} )
  64. # Set the path
  65. set( tmpSource ${tmpSource} "${SourcesPath}/${item}" )
  66. endforeach()
  67. # Finalize by writing the new list back over the old one
  68. set( ${Output} ${tmpSource} )
  69. endmacro()
  70. #| Scan Module
  71. include ( "${ScanModulePath}/setup.cmake" )
  72. PathPrepend( SCAN_SRCS ${ScanModulePath} ${SCAN_SRCS} )
  73. #| Macro Module
  74. include ( "${MacroModulePath}/setup.cmake" )
  75. PathPrepend( MACRO_SRCS ${MacroModulePath} ${MACRO_SRCS} )
  76. #| Output Module
  77. include ( "${OutputModulePath}/setup.cmake" )
  78. PathPrepend( OUTPUT_SRCS ${OutputModulePath} ${OUTPUT_SRCS} )
  79. #| Debugging Module
  80. include ( "${DebugModulePath}/setup.cmake" )
  81. PathPrepend( DEBUG_SRCS ${DebugModulePath} ${DEBUG_SRCS} )
  82. #| Default Map
  83. # TODO Add support for different defaultMaps
  84. configure_file( "${ScanModulePath}/defaultMap.h" defaultMap.h )
  85. #| Print list of all module sources
  86. message( STATUS "Detected Scan Module Source Files:" )
  87. message( "${SCAN_SRCS}" )
  88. message( STATUS "Detected Macro Module Source Files:" )
  89. message( "${MACRO_SRCS}" )
  90. message( STATUS "Detected Output Module Source Files:" )
  91. message( "${OUTPUT_SRCS}" )
  92. message( STATUS "Detected Debug Module Source Files:" )
  93. message( "${DEBUG_SRCS}" )
  94. ###
  95. # CMake Module Checking
  96. #
  97. find_package( Git REQUIRED )
  98. find_package( Ctags ) # Optional
  99. ###
  100. # Generate USB Defines
  101. #
  102. #| Manufacturer name
  103. set( MANUFACTURER "Kiibohd" )
  104. #| Serial Number
  105. #| Attempt to call Git to get the branch, last commit date, and whether code modified since last commit
  106. #| Modified
  107. #| Takes a bit of work to extract the "M " using CMake, and not using it if there are no modifications
  108. execute_process( COMMAND git status -s -uno --porcelain
  109. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  110. OUTPUT_VARIABLE Git_Modified_INFO
  111. ERROR_QUIET
  112. OUTPUT_STRIP_TRAILING_WHITESPACE
  113. )
  114. string( LENGTH "${Git_Modified_INFO}" Git_Modified_LENGTH )
  115. set( Git_Modified_Status "Clean" )
  116. if ( ${Git_Modified_LENGTH} GREATER 2 )
  117. string( SUBSTRING "${Git_Modified_INFO}" 1 2 Git_Modified_Flag_INFO )
  118. set( Git_Modified_Status "Dirty" )
  119. endif ()
  120. #| Branch
  121. execute_process( COMMAND git rev-parse --abbrev-ref HEAD
  122. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  123. OUTPUT_VARIABLE Git_Branch_INFO
  124. ERROR_QUIET
  125. OUTPUT_STRIP_TRAILING_WHITESPACE
  126. )
  127. #| Date
  128. execute_process( COMMAND git show -s --format=%ci
  129. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  130. OUTPUT_VARIABLE Git_Date_INFO
  131. ERROR_QUIET
  132. OUTPUT_STRIP_TRAILING_WHITESPACE
  133. )
  134. #| Commit Author and Email
  135. execute_process( COMMAND git show -s --format="%cn <%ce>"
  136. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  137. OUTPUT_VARIABLE Git_Commit_Author
  138. ERROR_QUIET
  139. OUTPUT_STRIP_TRAILING_WHITESPACE
  140. )
  141. #| Commit Revision
  142. execute_process( COMMAND git show -s --format=%H
  143. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  144. OUTPUT_VARIABLE Git_Commit_Revision
  145. ERROR_QUIET
  146. OUTPUT_STRIP_TRAILING_WHITESPACE
  147. )
  148. #| Origin URL
  149. execute_process( COMMAND git config --get remote.origin.url
  150. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  151. OUTPUT_VARIABLE Git_Origin_URL
  152. ERROR_QUIET
  153. OUTPUT_STRIP_TRAILING_WHITESPACE
  154. )
  155. #| Build Date
  156. execute_process( COMMAND "date" "+%Y-%m-%d %T %z"
  157. OUTPUT_VARIABLE Build_Date
  158. ERROR_QUIET
  159. OUTPUT_STRIP_TRAILING_WHITESPACE
  160. )
  161. #| Last Commit Date
  162. set( GitLastCommitDate "${Git_Modified_Status} ${Git_Branch_INFO} - ${Git_Date_INFO}" )
  163. #| Uses CMake variables to include as defines
  164. #| Primarily for USB configuration
  165. configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/Lib/_buildvars.h buildvars.h )
  166. ###
  167. # Source Defines
  168. #
  169. set( SRCS
  170. ${MAIN_SRCS}
  171. ${COMPILER_SRCS}
  172. ${SCAN_SRCS}
  173. ${MACRO_SRCS}
  174. ${OUTPUT_SRCS}
  175. ${DEBUG_SRCS}
  176. )
  177. #| Directories to include by default
  178. include_directories( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} )
  179. ###
  180. # Module Compatibility Check
  181. #
  182. #| Check for whether the set modules are compatible with the specified compiler family
  183. ModuleCompatibility( ${ScanModulePath} ${ScanModuleCompatibility} )
  184. ModuleCompatibility( ${MacroModulePath} ${MacroModuleCompatibility} )
  185. ModuleCompatibility( ${OutputModulePath} ${OutputModuleCompatibility} )
  186. ModuleCompatibility( ${DebugModulePath} ${DebugModuleCompatibility} )
  187. ###
  188. # ctag Generation
  189. #
  190. if( CTAGS_EXECUTABLE )
  191. # Populate list of directories for ctags to parse
  192. # NOTE: Doesn't support dots in the folder names...
  193. foreach( filename ${SRCS} )
  194. string( REGEX REPLACE "/[a-zA-Z0-9_-]+.c$" "" pathglob ${filename} )
  195. file( GLOB filenames "${pathglob}/*.c" )
  196. set( CTAG_PATHS ${CTAG_PATHS} ${filenames} )
  197. file( GLOB filenames "${pathglob}/*.h" )
  198. set( CTAG_PATHS ${CTAG_PATHS} ${filenames} )
  199. endforeach()
  200. # Generate the ctags
  201. execute_process( COMMAND ctags ${CTAG_PATHS}
  202. WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  203. )
  204. endif()
  205. ###
  206. # Build Targets
  207. #
  208. #| Create the .ELF file
  209. set( TARGET_ELF ${TARGET}.elf )
  210. add_executable( ${TARGET_ELF} ${SRCS} )
  211. #| .ELF Properties
  212. set_target_properties( ${TARGET_ELF} PROPERTIES
  213. LINK_FLAGS ${LINKER_FLAGS}
  214. SUFFIX "" # XXX Force Windows to keep the .exe off
  215. )
  216. #| Convert the .ELF into a .bin to load onto the McHCK
  217. if( DEFINED DFU )
  218. set( TARGET_BIN ${TARGET}.dfu.bin )
  219. add_custom_command( TARGET ${TARGET_ELF} POST_BUILD
  220. COMMAND ${CMAKE_OBJCOPY} ${BIN_FLAGS} ${TARGET_ELF} ${TARGET_BIN}
  221. COMMENT "Creating dfu binary file: ${TARGET_BIN}"
  222. )
  223. endif()
  224. #| Convert the .ELF into a .HEX to load onto the Teensy
  225. if ( DEFINED TEENSY )
  226. set( TARGET_HEX ${TARGET}.teensy.hex )
  227. add_custom_command( TARGET ${TARGET_ELF} POST_BUILD
  228. COMMAND ${CMAKE_OBJCOPY} ${HEX_FLAGS} ${TARGET_ELF} ${TARGET_HEX}
  229. COMMENT "Creating iHex file to load: ${TARGET_HEX}"
  230. )
  231. endif()
  232. #| Generate the Extended .LSS
  233. set( TARGET_LSS ${TARGET}.lss )
  234. add_custom_command( TARGET ${TARGET_ELF} POST_BUILD
  235. COMMAND ${CMAKE_OBJDUMP} ${LSS_FLAGS} ${TARGET_ELF} > ${TARGET_LSS}
  236. COMMENT "Creating Extended Listing: ${TARGET_LSS}"
  237. )
  238. #| Generate the Symbol Table .SYM
  239. set( TARGET_SYM ${TARGET}.sym )
  240. add_custom_command( TARGET ${TARGET_ELF} POST_BUILD
  241. COMMAND ${CMAKE_NM} -n ${TARGET_ELF} > ${TARGET_SYM}
  242. COMMENT "Creating Symbol Table: ${TARGET_SYM}"
  243. )
  244. #| Compiler Selection Record
  245. add_custom_command( TARGET ${TARGET_ELF} POST_BUILD
  246. COMMAND ${CMAKE_SOURCE_DIR}/Lib/CMake/writer compiler ${COMPILER_FAMILY}
  247. )
  248. ###
  249. # Size Information
  250. #
  251. #| After Changes Size Information
  252. add_custom_target( SizeAfter ALL
  253. COMMAND ${CMAKE_SOURCE_DIR}/Lib/CMake/sizeCalculator ${CMAKE_SIZE} ram ${TARGET_ELF} ${SIZE_RAM} " SRAM"
  254. COMMAND ${CMAKE_SOURCE_DIR}/Lib/CMake/sizeCalculator ${CMAKE_SIZE} flash ${TARGET_ELF} ${SIZE_FLASH} "Flash"
  255. DEPENDS ${TARGET_ELF}
  256. COMMENT "Chip usage for ${CHIP}"
  257. )
  258. ###
  259. # Setup Loader Script and Program
  260. #
  261. #| Provides the user with the correct teensy-loader-cli command for the built .HEX file
  262. #| Windows
  263. if( CMAKE_SYSTEM_NAME MATCHES "Windows" )
  264. configure_file( LoadFile/winload load NEWLINE_STYLE UNIX )
  265. #| Default
  266. else()
  267. configure_file( LoadFile/load load NEWLINE_STYLE UNIX )
  268. endif()