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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. 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. set ( Module_SRCS ${${ModuleType}_SRCS} ${Module_SRCS} )
  78. # Append each of the sources to each type of module srcs list
  79. set ( ${ModuleType}_SRCS ${Module_SRCS} )
  80. # Add .h files
  81. add_definitions ( -I${ModuleFullPath} )
  82. # Check module compatibility
  83. ModuleCompatibility( ${ModulePath} ${ModuleCompatibility} )
  84. # Check if this is a main module add
  85. foreach ( extraArg ${ARGN} )
  86. # Display detected source files
  87. if ( NOT DEFINED SubModule )
  88. message ( STATUS "Detected ${ModuleType} Module Source Files:" )
  89. message ( "${${ModuleType}_SRCS}" )
  90. endif ()
  91. endforeach ()
  92. # Finally, add the sources to the parent scope (i.e. return)
  93. set ( ${ModuleType}_SRCS ${${ModuleType}_SRCS} PARENT_SCOPE )
  94. endfunction ()
  95. #| Add main modules
  96. AddModule ( Scan ${ScanModule} 1 )
  97. AddModule ( Macro ${MacroModule} 1 )
  98. AddModule ( Output ${OutputModule} 1 )
  99. AddModule ( Debug ${DebugModule} 1 )
  100. ###
  101. # CMake Module Checking
  102. #
  103. find_package ( Git REQUIRED )
  104. find_package ( Ctags ) # Optional
  105. ###
  106. # Generate USB Defines
  107. #
  108. #| Manufacturer name
  109. set( MANUFACTURER "Kiibohd" )
  110. #| Serial Number
  111. #| Attempt to call Git to get the branch, last commit date, and whether code modified since last commit
  112. #| Modified
  113. #| Takes a bit of work to extract the "M " using CMake, and not using it if there are no modifications
  114. execute_process( COMMAND ${GIT_EXECUTABLE} status -s -uno --porcelain
  115. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  116. OUTPUT_VARIABLE Git_Modified_INFO
  117. ERROR_QUIET
  118. OUTPUT_STRIP_TRAILING_WHITESPACE
  119. )
  120. string( LENGTH "${Git_Modified_INFO}" Git_Modified_LENGTH )
  121. set( Git_Modified_Status "Clean" )
  122. if ( ${Git_Modified_LENGTH} GREATER 2 )
  123. string( SUBSTRING "${Git_Modified_INFO}" 1 2 Git_Modified_Flag_INFO )
  124. set( Git_Modified_Status "Dirty" )
  125. endif ()
  126. #| Branch
  127. execute_process( COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
  128. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  129. OUTPUT_VARIABLE Git_Branch_INFO
  130. ERROR_QUIET
  131. OUTPUT_STRIP_TRAILING_WHITESPACE
  132. )
  133. #| Date
  134. execute_process( COMMAND ${GIT_EXECUTABLE} show -s --format=%ci
  135. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  136. OUTPUT_VARIABLE Git_Date_INFO
  137. ERROR_QUIET
  138. OUTPUT_STRIP_TRAILING_WHITESPACE
  139. )
  140. #| Commit Author and Email
  141. execute_process( COMMAND ${GIT_EXECUTABLE} show -s --format="%cn <%ce>"
  142. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  143. OUTPUT_VARIABLE Git_Commit_Author
  144. ERROR_QUIET
  145. OUTPUT_STRIP_TRAILING_WHITESPACE
  146. )
  147. #| Commit Revision
  148. execute_process( COMMAND ${GIT_EXECUTABLE} show -s --format=%H
  149. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  150. OUTPUT_VARIABLE Git_Commit_Revision
  151. ERROR_QUIET
  152. OUTPUT_STRIP_TRAILING_WHITESPACE
  153. )
  154. #| Origin URL
  155. execute_process( COMMAND ${GIT_EXECUTABLE} config --get remote.origin.url
  156. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  157. OUTPUT_VARIABLE Git_Origin_URL
  158. ERROR_QUIET
  159. OUTPUT_STRIP_TRAILING_WHITESPACE
  160. )
  161. #| Build Date
  162. execute_process( COMMAND "date" "+%Y-%m-%d %T %z"
  163. OUTPUT_VARIABLE Build_Date
  164. ERROR_QUIET
  165. OUTPUT_STRIP_TRAILING_WHITESPACE
  166. )
  167. #| Last Commit Date
  168. set( GitLastCommitDate "${Git_Modified_Status} ${Git_Branch_INFO} - ${Git_Date_INFO}" )
  169. #| Uses CMake variables to include as defines
  170. #| Primarily for USB configuration
  171. configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/Lib/_buildvars.h buildvars.h )
  172. ###
  173. # Source Defines
  174. #
  175. set( SRCS
  176. ${MAIN_SRCS}
  177. ${COMPILER_SRCS}
  178. ${Scan_SRCS}
  179. ${Macro_SRCS}
  180. ${Output_SRCS}
  181. ${Debug_SRCS}
  182. )
  183. #| Directories to include by default
  184. include_directories( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} )
  185. ###
  186. # ctag Generation
  187. #
  188. if( CTAGS_EXECUTABLE )
  189. # Populate list of directories for ctags to parse
  190. # NOTE: Doesn't support dots in the folder names...
  191. foreach( filename ${SRCS} )
  192. string( REGEX REPLACE "/[a-zA-Z0-9_-]+.c$" "" pathglob ${filename} )
  193. file( GLOB filenames "${pathglob}/*.c" )
  194. set( CTAG_PATHS ${CTAG_PATHS} ${filenames} )
  195. file( GLOB filenames "${pathglob}/*.h" )
  196. set( CTAG_PATHS ${CTAG_PATHS} ${filenames} )
  197. endforeach()
  198. # Generate the ctags
  199. execute_process( COMMAND ctags ${CTAG_PATHS}
  200. WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  201. )
  202. endif()