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 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. ###| CMAKE Kiibohd Controller Source Configurator |###
  2. #
  3. # Written by Jacob Alexander in 2011-2015 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. function ( ModuleCompatibility ModulePath )
  31. foreach ( mod_var ${ARGN} )
  32. if ( ${mod_var} STREQUAL ${COMPILER_FAMILY} )
  33. # Module found, no need to scan further
  34. return()
  35. endif ()
  36. endforeach ()
  37. message ( FATAL_ERROR "${ModulePath} does not support the ${COMPILER_FAMILY} family..." )
  38. endfunction ()
  39. ###
  40. # Module Processing
  41. #
  42. #| Go through lists of sources and append paths
  43. #| Usage:
  44. #| PathPrepend( OutputListOfSources <Prepend Path> <InputListOfSources> )
  45. macro ( PathPrepend Output SourcesPath )
  46. unset ( tmpSource )
  47. # Loop through items
  48. foreach ( item ${ARGN} )
  49. # Set the path
  50. set ( tmpSource ${tmpSource} "${SourcesPath}/${item}" )
  51. endforeach ()
  52. # Finalize by writing the new list back over the old one
  53. set ( ${Output} ${tmpSource} )
  54. endmacro ()
  55. ###
  56. # Add Module Macro
  57. #
  58. # Optional Arg 1: Main Module Check, set to True/1 if adding a main module
  59. function ( AddModule ModuleType ModuleName )
  60. # Module path
  61. set ( ModulePath ${ModuleType}/${ModuleName} )
  62. set ( ModuleFullPath ${HEAD_DIR}/${ModuleType}/${ModuleName} )
  63. # Include setup.cmake file
  64. include ( ${ModuleFullPath}/setup.cmake )
  65. # Check if this is a main module add
  66. foreach ( extraArg ${ARGN} )
  67. # Make sure this isn't a submodule
  68. if ( DEFINED SubModule )
  69. message ( FATAL_ERROR
  70. "The '${ModuleName}' module is not a stand-alone module, and requires further setup."
  71. )
  72. endif ()
  73. endforeach ()
  74. # PathPrepend to give proper paths to each of the source files
  75. PathPrepend ( Module_SRCS ${ModulePath} ${Module_SRCS} )
  76. # Check the current scope to see if a sub-module added some source files
  77. # Append each of the sources to each type of module srcs list
  78. set ( ${ModuleType}_SRCS ${${ModuleType}_SRCS} ${Module_SRCS} )
  79. # Add .h files
  80. add_definitions ( -I${ModuleFullPath} )
  81. # Check module compatibility
  82. ModuleCompatibility( ${ModulePath} ${ModuleCompatibility} )
  83. # Check if this is a main module add
  84. foreach ( extraArg ${ARGN} )
  85. # Display detected source files
  86. if ( NOT DEFINED SubModule )
  87. message ( STATUS "Detected ${ModuleType} Module Source Files:" )
  88. message ( "${${ModuleType}_SRCS}" )
  89. endif ()
  90. endforeach ()
  91. # Check for any capabilities.kll files in the Module
  92. set ( kll_capabilities_file "${ModuleFullPath}/capabilities.kll" )
  93. if ( EXISTS ${kll_capabilities_file} )
  94. # Add the kll file and any submodule kll files to the running list
  95. set ( ${ModuleType}Module_KLL ${${ModuleType}Module_KLL} ${kll_capabilities_file} )
  96. endif ()
  97. # Finally, add the sources and kll files to the parent scope (i.e. return)
  98. set ( ${ModuleType}_SRCS ${${ModuleType}_SRCS} PARENT_SCOPE )
  99. set ( ${ModuleType}Module_KLL ${${ModuleType}Module_KLL} PARENT_SCOPE )
  100. endfunction ()
  101. #| Add main modules
  102. AddModule ( Scan ${ScanModule} 1 )
  103. AddModule ( Macro ${MacroModule} 1 )
  104. AddModule ( Output ${OutputModule} 1 )
  105. AddModule ( Debug ${DebugModule} 1 )
  106. ###
  107. # CMake Module Checking
  108. #
  109. find_package ( Git REQUIRED )
  110. find_package ( Ctags ) # Optional
  111. ###
  112. # Generate USB Defines
  113. #
  114. #| Manufacturer name
  115. set ( MANUFACTURER "Kiibohd" )
  116. #| Serial Number
  117. #| Attempt to call Git to get the branch, last commit date, and whether code modified since last commit
  118. #| Modified
  119. #| Takes a bit of work to extract the "M " using CMake, and not using it if there are no modifications
  120. execute_process ( COMMAND ${GIT_EXECUTABLE} status -s -uno --porcelain
  121. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  122. OUTPUT_VARIABLE Git_Modified_INFO
  123. ERROR_QUIET
  124. OUTPUT_STRIP_TRAILING_WHITESPACE
  125. )
  126. string ( LENGTH "${Git_Modified_INFO}" Git_Modified_LENGTH )
  127. set ( Git_Modified_Status "Clean" )
  128. if ( ${Git_Modified_LENGTH} GREATER 2 )
  129. string ( SUBSTRING "${Git_Modified_INFO}" 1 2 Git_Modified_Flag_INFO )
  130. set ( Git_Modified_Status "Dirty" )
  131. endif ()
  132. #| List of modified files
  133. execute_process ( COMMAND ${GIT_EXECUTABLE} diff-index --name-only HEAD --
  134. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  135. OUTPUT_VARIABLE Git_Modified_Files
  136. ERROR_QUIET
  137. OUTPUT_STRIP_TRAILING_WHITESPACE
  138. )
  139. string ( REGEX REPLACE "\n" "\\\\r\\\\n\\\\t" Git_Modified_Files "${Git_Modified_Files}" )
  140. set ( Git_Modified_Files "\\r\\n\\t${Git_Modified_Files}" )
  141. #| Branch
  142. execute_process( COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
  143. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  144. OUTPUT_VARIABLE Git_Branch_INFO
  145. ERROR_QUIET
  146. OUTPUT_STRIP_TRAILING_WHITESPACE
  147. )
  148. #| Date
  149. execute_process ( COMMAND ${GIT_EXECUTABLE} show -s --format=%ci
  150. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  151. OUTPUT_VARIABLE Git_Date_INFO
  152. ERROR_QUIET
  153. OUTPUT_STRIP_TRAILING_WHITESPACE
  154. )
  155. #| Commit Author and Email
  156. execute_process ( COMMAND ${GIT_EXECUTABLE} show -s --format="%cn <%ce>"
  157. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  158. OUTPUT_VARIABLE Git_Commit_Author
  159. ERROR_QUIET
  160. OUTPUT_STRIP_TRAILING_WHITESPACE
  161. )
  162. #| Commit Revision
  163. execute_process ( COMMAND ${GIT_EXECUTABLE} show -s --format=%H
  164. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  165. OUTPUT_VARIABLE Git_Commit_Revision
  166. ERROR_QUIET
  167. OUTPUT_STRIP_TRAILING_WHITESPACE
  168. )
  169. #| Origin URL
  170. execute_process ( COMMAND ${GIT_EXECUTABLE} config --get remote.origin.url
  171. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  172. OUTPUT_VARIABLE Git_Origin_URL
  173. ERROR_QUIET
  174. OUTPUT_STRIP_TRAILING_WHITESPACE
  175. )
  176. #| Build Date
  177. execute_process ( COMMAND "date" "+%Y-%m-%d %T %z"
  178. OUTPUT_VARIABLE Build_Date
  179. ERROR_QUIET
  180. OUTPUT_STRIP_TRAILING_WHITESPACE
  181. )
  182. #| Last Commit Date
  183. set ( GitLastCommitDate "${Git_Modified_Status} ${Git_Branch_INFO} - ${Git_Date_INFO}" )
  184. #| Uses CMake variables to include as defines
  185. #| Primarily for USB configuration
  186. configure_file ( ${CMAKE_CURRENT_SOURCE_DIR}/Lib/_buildvars.h buildvars.h )
  187. ###
  188. # Source Defines
  189. #
  190. set ( SRCS
  191. ${MAIN_SRCS}
  192. ${COMPILER_SRCS}
  193. ${Scan_SRCS}
  194. ${Macro_SRCS}
  195. ${Output_SRCS}
  196. ${Debug_SRCS}
  197. )
  198. #| Directories to include by default
  199. include_directories ( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} )
  200. ###
  201. # ctag Generation
  202. #
  203. if ( CTAGS_EXECUTABLE )
  204. # Populate list of directories for ctags to parse
  205. # NOTE: Doesn't support dots in the folder names...
  206. foreach ( filename ${SRCS} )
  207. string ( REGEX REPLACE "/[a-zA-Z0-9_-]+.c$" "" pathglob ${filename} )
  208. file ( GLOB filenames "${pathglob}/*.c" )
  209. set ( CTAG_PATHS ${CTAG_PATHS} ${filenames} )
  210. file ( GLOB filenames "${pathglob}/*.h" )
  211. set ( CTAG_PATHS ${CTAG_PATHS} ${filenames} )
  212. endforeach ()
  213. # Generate the ctags
  214. execute_process ( COMMAND ctags --fields=+l ${CTAG_PATHS}
  215. WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  216. )
  217. endif ()