]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Lib/CMake/modules.cmake
Adding basic remote capabilities + UART Rx DMA buffers
[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 #| List of modified files
182 execute_process ( COMMAND ${GIT_EXECUTABLE} diff-index --name-only HEAD --
183         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
184         OUTPUT_VARIABLE Git_Modified_Files
185         ERROR_QUIET
186         OUTPUT_STRIP_TRAILING_WHITESPACE
187 )
188 string ( REGEX REPLACE "\n" "\\\\r\\\\n\\\\t" Git_Modified_Files "${Git_Modified_Files}" )
189 set ( Git_Modified_Files "\\r\\n\\t${Git_Modified_Files}" )
190
191 #| Branch
192 execute_process( COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
193         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
194         OUTPUT_VARIABLE Git_Branch_INFO
195         ERROR_QUIET
196         OUTPUT_STRIP_TRAILING_WHITESPACE
197 )
198
199 #| Date
200 execute_process ( COMMAND ${GIT_EXECUTABLE} show -s --format=%ci
201         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
202         OUTPUT_VARIABLE Git_Date_INFO
203         ERROR_QUIET
204         OUTPUT_STRIP_TRAILING_WHITESPACE
205 )
206
207 #| Commit Author and Email
208 execute_process ( COMMAND ${GIT_EXECUTABLE} show -s --format="%cn <%ce>"
209         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
210         OUTPUT_VARIABLE Git_Commit_Author
211         ERROR_QUIET
212         OUTPUT_STRIP_TRAILING_WHITESPACE
213 )
214
215 #| Commit Revision
216 execute_process ( COMMAND ${GIT_EXECUTABLE} show -s --format=%H
217         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
218         OUTPUT_VARIABLE Git_Commit_Revision
219         ERROR_QUIET
220         OUTPUT_STRIP_TRAILING_WHITESPACE
221 )
222
223 #| Origin URL
224 execute_process ( COMMAND ${GIT_EXECUTABLE} config --get remote.origin.url
225         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
226         OUTPUT_VARIABLE Git_Origin_URL
227         ERROR_QUIET
228         OUTPUT_STRIP_TRAILING_WHITESPACE
229 )
230
231 #| Build Date
232 execute_process ( COMMAND "date" "+%Y-%m-%d %T %z"
233         OUTPUT_VARIABLE Build_Date
234         ERROR_QUIET
235         OUTPUT_STRIP_TRAILING_WHITESPACE
236 )
237
238 #| Last Commit Date
239 set ( GitLastCommitDate "${Git_Modified_Status} ${Git_Branch_INFO} - ${Git_Date_INFO}" )
240
241 #| Uses CMake variables to include as defines
242 #| Primarily for USB configuration
243 configure_file ( ${CMAKE_CURRENT_SOURCE_DIR}/Lib/_buildvars.h buildvars.h )
244
245
246
247 ###
248 # Source Defines
249 #
250 set ( SRCS
251         ${MAIN_SRCS}
252         ${COMPILER_SRCS}
253         ${Scan_SRCS}
254         ${Macro_SRCS}
255         ${Output_SRCS}
256         ${Debug_SRCS}
257 )
258
259 #| Directories to include by default
260 include_directories ( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} )
261
262
263
264 ###
265 # ctag Generation
266 #
267
268 if ( CTAGS_EXECUTABLE )
269         # Populate list of directories for ctags to parse
270         # NOTE: Doesn't support dots in the folder names...
271         foreach ( filename ${SRCS} )
272                 string ( REGEX REPLACE "/[a-zA-Z0-9_-]+.c$" "" pathglob ${filename} )
273                 file ( GLOB filenames "${pathglob}/*.c" )
274                 set ( CTAG_PATHS ${CTAG_PATHS} ${filenames} )
275                 file ( GLOB filenames "${pathglob}/*.h" )
276                 set ( CTAG_PATHS ${CTAG_PATHS} ${filenames} )
277         endforeach ()
278
279         # Generate the ctags
280         execute_process ( COMMAND ctags --fields=+l ${CTAG_PATHS}
281                 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
282         )
283 endif ()
284