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