]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Lib/CMake/modules.cmake
Updating setup.cmake files to use Module hierarchy implicitly
[kiibohd-controller.git] / Lib / CMake / modules.cmake
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
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         set ( Module_SRCS ${${ModuleType}_SRCS} ${Module_SRCS} )
108
109         # Append each of the sources to each type of module srcs list
110         set ( ${ModuleType}_SRCS ${Module_SRCS} )
111
112         # Add .h files
113         add_definitions ( -I${ModuleFullPath} )
114
115         # Check module compatibility
116         ModuleCompatibility( ${ModulePath} ${ModuleCompatibility} )
117
118         # Check if this is a main module add
119         foreach ( extraArg ${ARGN} )
120                 # Display detected source files
121                 if ( NOT DEFINED SubModule )
122                         message ( STATUS "Detected ${ModuleType} Module Source Files:" )
123                         message ( "${${ModuleType}_SRCS}" )
124                 endif ()
125         endforeach ()
126
127         # Finally, add the sources to the parent scope (i.e. return)
128         set ( ${ModuleType}_SRCS ${${ModuleType}_SRCS} PARENT_SCOPE )
129 endfunction ()
130
131
132 #| Add main modules
133 AddModule ( Scan   ${ScanModule}   1 )
134 AddModule ( Macro  ${MacroModule}  1 )
135 AddModule ( Output ${OutputModule} 1 )
136 AddModule ( Debug  ${DebugModule}  1 )
137
138
139
140 ###
141 # CMake Module Checking
142 #
143 find_package ( Git REQUIRED )
144 find_package ( Ctags ) # Optional
145
146
147
148 ###
149 # Generate USB Defines
150 #
151
152 #| Manufacturer name
153 set( MANUFACTURER "Kiibohd" )
154
155
156 #| Serial Number
157 #| Attempt to call Git to get the branch, last commit date, and whether code modified since last commit
158
159 #| Modified
160 #| Takes a bit of work to extract the "M " using CMake, and not using it if there are no modifications
161 execute_process( COMMAND ${GIT_EXECUTABLE} status -s -uno --porcelain
162         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
163         OUTPUT_VARIABLE Git_Modified_INFO
164         ERROR_QUIET
165         OUTPUT_STRIP_TRAILING_WHITESPACE
166 )
167 string( LENGTH "${Git_Modified_INFO}" Git_Modified_LENGTH )
168 set( Git_Modified_Status "Clean" )
169 if ( ${Git_Modified_LENGTH} GREATER 2 )
170         string( SUBSTRING "${Git_Modified_INFO}" 1 2 Git_Modified_Flag_INFO )
171         set( Git_Modified_Status "Dirty" )
172 endif ()
173
174 #| Branch
175 execute_process( COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
176         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
177         OUTPUT_VARIABLE Git_Branch_INFO
178         ERROR_QUIET
179         OUTPUT_STRIP_TRAILING_WHITESPACE
180 )
181
182 #| Date
183 execute_process( COMMAND ${GIT_EXECUTABLE} show -s --format=%ci
184         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
185         OUTPUT_VARIABLE Git_Date_INFO
186         ERROR_QUIET
187         OUTPUT_STRIP_TRAILING_WHITESPACE
188 )
189
190 #| Commit Author and Email
191 execute_process( COMMAND ${GIT_EXECUTABLE} show -s --format="%cn <%ce>"
192         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
193         OUTPUT_VARIABLE Git_Commit_Author
194         ERROR_QUIET
195         OUTPUT_STRIP_TRAILING_WHITESPACE
196 )
197
198 #| Commit Revision
199 execute_process( COMMAND ${GIT_EXECUTABLE} show -s --format=%H
200         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
201         OUTPUT_VARIABLE Git_Commit_Revision
202         ERROR_QUIET
203         OUTPUT_STRIP_TRAILING_WHITESPACE
204 )
205
206 #| Origin URL
207 execute_process( COMMAND ${GIT_EXECUTABLE} config --get remote.origin.url
208         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
209         OUTPUT_VARIABLE Git_Origin_URL
210         ERROR_QUIET
211         OUTPUT_STRIP_TRAILING_WHITESPACE
212 )
213
214 #| Build Date
215 execute_process( COMMAND "date" "+%Y-%m-%d %T %z"
216         OUTPUT_VARIABLE Build_Date
217         ERROR_QUIET
218         OUTPUT_STRIP_TRAILING_WHITESPACE
219 )
220
221 #| Last Commit Date
222 set( GitLastCommitDate "${Git_Modified_Status} ${Git_Branch_INFO} - ${Git_Date_INFO}" )
223
224 #| Uses CMake variables to include as defines
225 #| Primarily for USB configuration
226 configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/Lib/_buildvars.h buildvars.h )
227
228
229
230 ###
231 # Source Defines
232 #
233 set( SRCS
234         ${MAIN_SRCS}
235         ${COMPILER_SRCS}
236         ${Scan_SRCS}
237         ${Macro_SRCS}
238         ${Output_SRCS}
239         ${Debug_SRCS}
240 )
241
242 #| Directories to include by default
243 include_directories( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} )
244
245
246
247 ###
248 # ctag Generation
249 #
250
251 if( CTAGS_EXECUTABLE )
252         # Populate list of directories for ctags to parse
253         # NOTE: Doesn't support dots in the folder names...
254         foreach( filename ${SRCS} )
255                 string( REGEX REPLACE "/[a-zA-Z0-9_-]+.c$" "" pathglob ${filename} )
256                 file( GLOB filenames "${pathglob}/*.c" )
257                 set( CTAG_PATHS ${CTAG_PATHS} ${filenames} )
258                 file( GLOB filenames "${pathglob}/*.h" )
259                 set( CTAG_PATHS ${CTAG_PATHS} ${filenames} )
260         endforeach()
261
262         # Generate the ctags
263         execute_process( COMMAND ctags ${CTAG_PATHS}
264                 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
265         )
266 endif()
267