]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Lib/CMake/modules.cmake
Adding configurable DebounceDivThreshold
[kiibohd-controller.git] / Lib / CMake / modules.cmake
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
10 ###
11 # CMake Custom Modules Path
12 #
13
14 set ( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/Lib/CMake/" )
15
16
17
18 ###
19 # Module Overrides (Used in the buildall.bash script)
20 #
21 if ( ( DEFINED ScanModuleOverride ) AND ( EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/Scan/${ScanModuleOverride} ) )
22         set( ScanModule ${ScanModuleOverride} )
23 endif ()
24
25
26
27
28 ###
29 # Path Setup
30 #
31 set(  ScanModulePath    "Scan/${ScanModule}"   )
32 set( MacroModulePath   "Macro/${MacroModule}"  )
33 set( OutputModulePath "Output/${OutputModule}" )
34 set( DebugModulePath   "Debug/${DebugModule}"  )
35
36 #| Top-level directory adjustment
37 set( HEAD_DIR "${CMAKE_CURRENT_SOURCE_DIR}" )
38
39
40
41 ###
42 # Module Check Function
43 #
44
45 function ( ModuleCompatibility ModulePath )
46         foreach ( mod_var ${ARGN} )
47                 if ( ${mod_var} STREQUAL ${COMPILER_FAMILY} )
48                         # Module found, no need to scan further
49                         return()
50                 endif ()
51         endforeach ()
52
53         message ( FATAL_ERROR "${ModulePath} does not support the ${COMPILER_FAMILY} family..." )
54 endfunction ()
55
56
57
58 ###
59 # Module Processing
60 #
61
62 #| Go through lists of sources and append paths
63 #| Usage:
64 #|  PathPrepend( OutputListOfSources <Prepend Path> <InputListOfSources> )
65 macro ( PathPrepend Output SourcesPath )
66         unset ( tmpSource )
67
68         # Loop through items
69         foreach ( item ${ARGN} )
70                 # Set the path
71                 set ( tmpSource ${tmpSource} "${SourcesPath}/${item}" )
72         endforeach ()
73
74         # Finalize by writing the new list back over the old one
75         set ( ${Output} ${tmpSource} )
76 endmacro ()
77
78
79
80 ###
81 # Add Module Macro
82 #
83 # Optional Arg 1: Main Module Check, set to True/1 if adding a main module
84
85 function ( AddModule ModuleType ModuleName )
86         # Module path
87         set ( ModulePath                 ${ModuleType}/${ModuleName} )
88         set ( ModuleFullPath ${HEAD_DIR}/${ModuleType}/${ModuleName} )
89
90         # Include setup.cmake file
91         include ( ${ModuleFullPath}/setup.cmake )
92
93         # Check if this is a main module add
94         foreach ( extraArg ${ARGN} )
95                 # Make sure this isn't a submodule
96                 if ( DEFINED SubModule )
97                         message ( FATAL_ERROR
98                         "The '${ModuleName}' module is not a stand-alone module, and requires further setup."
99                         )
100                 endif ()
101         endforeach ()
102
103         # PathPrepend to give proper paths to each of the source files
104         PathPrepend ( Module_SRCS ${ModulePath} ${Module_SRCS} )
105
106         # Check the current scope to see if a sub-module added some source files
107         # Append each of the sources to each type of module srcs list
108         set ( ${ModuleType}_SRCS ${${ModuleType}_SRCS} ${Module_SRCS} )
109
110         # Add .h files
111         add_definitions ( -I${ModuleFullPath} )
112
113         # Check module compatibility
114         ModuleCompatibility( ${ModulePath} ${ModuleCompatibility} )
115
116         # Check if this is a main module add
117         foreach ( extraArg ${ARGN} )
118                 # Display detected source files
119                 if ( NOT DEFINED SubModule )
120                         message ( STATUS "Detected ${ModuleType} Module Source Files:" )
121                         message ( "${${ModuleType}_SRCS}" )
122                 endif ()
123         endforeach ()
124
125         # Check for any capabilities.kll files in the Module
126         set ( kll_capabilities_file "${ModuleFullPath}/capabilities.kll" )
127         if ( EXISTS ${kll_capabilities_file} )
128                 # Add the kll file and any submodule kll files to the running list
129                 set ( ${ModuleType}Module_KLL ${${ModuleType}Module_KLL} ${kll_capabilities_file} )
130         endif ()
131
132
133         # Finally, add the sources and kll files to the parent scope (i.e. return)
134         set ( ${ModuleType}_SRCS ${${ModuleType}_SRCS} PARENT_SCOPE )
135         set ( ${ModuleType}Module_KLL ${${ModuleType}Module_KLL} PARENT_SCOPE )
136 endfunction ()
137
138
139 #| Add main modules
140 AddModule ( Scan   ${ScanModule}   1 )
141 AddModule ( Macro  ${MacroModule}  1 )
142 AddModule ( Output ${OutputModule} 1 )
143 AddModule ( Debug  ${DebugModule}  1 )
144
145
146
147 ###
148 # CMake Module Checking
149 #
150 find_package ( Git REQUIRED )
151 find_package ( Ctags ) # Optional
152
153
154
155 ###
156 # Generate USB Defines
157 #
158
159 #| Manufacturer name
160 set( MANUFACTURER "Kiibohd" )
161
162
163 #| Serial Number
164 #| Attempt to call Git to get the branch, last commit date, and whether code modified since last commit
165
166 #| Modified
167 #| Takes a bit of work to extract the "M " using CMake, and not using it if there are no modifications
168 execute_process( COMMAND ${GIT_EXECUTABLE} status -s -uno --porcelain
169         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
170         OUTPUT_VARIABLE Git_Modified_INFO
171         ERROR_QUIET
172         OUTPUT_STRIP_TRAILING_WHITESPACE
173 )
174 string( LENGTH "${Git_Modified_INFO}" Git_Modified_LENGTH )
175 set( Git_Modified_Status "Clean" )
176 if ( ${Git_Modified_LENGTH} GREATER 2 )
177         string( SUBSTRING "${Git_Modified_INFO}" 1 2 Git_Modified_Flag_INFO )
178         set( Git_Modified_Status "Dirty" )
179 endif ()
180
181 #| Branch
182 execute_process( COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
183         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
184         OUTPUT_VARIABLE Git_Branch_INFO
185         ERROR_QUIET
186         OUTPUT_STRIP_TRAILING_WHITESPACE
187 )
188
189 #| Date
190 execute_process( COMMAND ${GIT_EXECUTABLE} show -s --format=%ci
191         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
192         OUTPUT_VARIABLE Git_Date_INFO
193         ERROR_QUIET
194         OUTPUT_STRIP_TRAILING_WHITESPACE
195 )
196
197 #| Commit Author and Email
198 execute_process( COMMAND ${GIT_EXECUTABLE} show -s --format="%cn <%ce>"
199         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
200         OUTPUT_VARIABLE Git_Commit_Author
201         ERROR_QUIET
202         OUTPUT_STRIP_TRAILING_WHITESPACE
203 )
204
205 #| Commit Revision
206 execute_process( COMMAND ${GIT_EXECUTABLE} show -s --format=%H
207         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
208         OUTPUT_VARIABLE Git_Commit_Revision
209         ERROR_QUIET
210         OUTPUT_STRIP_TRAILING_WHITESPACE
211 )
212
213 #| Origin URL
214 execute_process( COMMAND ${GIT_EXECUTABLE} config --get remote.origin.url
215         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
216         OUTPUT_VARIABLE Git_Origin_URL
217         ERROR_QUIET
218         OUTPUT_STRIP_TRAILING_WHITESPACE
219 )
220
221 #| Build Date
222 execute_process( COMMAND "date" "+%Y-%m-%d %T %z"
223         OUTPUT_VARIABLE Build_Date
224         ERROR_QUIET
225         OUTPUT_STRIP_TRAILING_WHITESPACE
226 )
227
228 #| Last Commit Date
229 set( GitLastCommitDate "${Git_Modified_Status} ${Git_Branch_INFO} - ${Git_Date_INFO}" )
230
231 #| Uses CMake variables to include as defines
232 #| Primarily for USB configuration
233 configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/Lib/_buildvars.h buildvars.h )
234
235
236
237 ###
238 # Source Defines
239 #
240 set( SRCS
241         ${MAIN_SRCS}
242         ${COMPILER_SRCS}
243         ${Scan_SRCS}
244         ${Macro_SRCS}
245         ${Output_SRCS}
246         ${Debug_SRCS}
247 )
248
249 #| Directories to include by default
250 include_directories( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} )
251
252
253
254 ###
255 # ctag Generation
256 #
257
258 if( CTAGS_EXECUTABLE )
259         # Populate list of directories for ctags to parse
260         # NOTE: Doesn't support dots in the folder names...
261         foreach( filename ${SRCS} )
262                 string( REGEX REPLACE "/[a-zA-Z0-9_-]+.c$" "" pathglob ${filename} )
263                 file( GLOB filenames "${pathglob}/*.c" )
264                 set( CTAG_PATHS ${CTAG_PATHS} ${filenames} )
265                 file( GLOB filenames "${pathglob}/*.h" )
266                 set( CTAG_PATHS ${CTAG_PATHS} ${filenames} )
267         endforeach()
268
269         # Generate the ctags
270         execute_process( COMMAND ctags ${CTAG_PATHS}
271                 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
272         )
273 endif()
274