]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Lib/CMake/modules.cmake
Initial work for McHCK mk20dx128vlf5 port.
[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 #| Default Map
116 # TODO Add support for different defaultMaps
117 configure_file( "${ScanModulePath}/defaultMap.h" defaultMap.h )
118
119
120 #| Print list of all module sources
121 message( STATUS "Detected Scan Module Source Files:" )
122 message( "${SCAN_SRCS}" )
123 message( STATUS "Detected Macro Module Source Files:" )
124 message( "${MACRO_SRCS}" )
125 message( STATUS "Detected Output Module Source Files:" )
126 message( "${OUTPUT_SRCS}" )
127 message( STATUS "Detected Debug Module Source Files:" )
128 message( "${DEBUG_SRCS}" )
129
130
131
132 ###
133 # Generate USB Defines
134 #
135
136 #| Manufacturer name
137 set( MANUFACTURER "Kiibohd" )
138
139
140 #| Serial Number
141 #| Attempt to call Git to get the branch, last commit date, and whether code modified since last commit
142
143 #| Modified
144 #| Takes a bit of work to extract the "M " using CMake, and not using it if there are no modifications
145 execute_process( COMMAND git status -s -uno --porcelain
146         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
147         OUTPUT_VARIABLE Git_Modified_INFO
148         ERROR_QUIET
149         OUTPUT_STRIP_TRAILING_WHITESPACE
150 )
151 string( LENGTH "${Git_Modified_INFO}" Git_Modified_LENGTH )
152 set( Git_Modified_Status "Clean" )
153 if ( ${Git_Modified_LENGTH} GREATER 2 )
154         string( SUBSTRING "${Git_Modified_INFO}" 1 2 Git_Modified_Flag_INFO )
155         set( Git_Modified_Status "Dirty" )
156 endif ()
157
158 #| Branch
159 execute_process( COMMAND git rev-parse --abbrev-ref HEAD
160         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
161         OUTPUT_VARIABLE Git_Branch_INFO
162         ERROR_QUIET
163         OUTPUT_STRIP_TRAILING_WHITESPACE
164 )
165
166 #| Date
167 execute_process( COMMAND git show -s --format=%ci
168         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
169         OUTPUT_VARIABLE Git_Date_INFO
170         ERROR_QUIET
171         OUTPUT_STRIP_TRAILING_WHITESPACE
172 )
173
174 #| Commit Author and Email
175 execute_process( COMMAND git show -s --format="%cn <%ce>"
176         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
177         OUTPUT_VARIABLE Git_Commit_Author
178         ERROR_QUIET
179         OUTPUT_STRIP_TRAILING_WHITESPACE
180 )
181
182 #| Commit Revision
183 execute_process( COMMAND git show -s --format=%H
184         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
185         OUTPUT_VARIABLE Git_Commit_Revision
186         ERROR_QUIET
187         OUTPUT_STRIP_TRAILING_WHITESPACE
188 )
189
190 #| Origin URL
191 execute_process( COMMAND git config --get remote.origin.url
192         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
193         OUTPUT_VARIABLE Git_Origin_URL
194         ERROR_QUIET
195         OUTPUT_STRIP_TRAILING_WHITESPACE
196 )
197
198 #| Build Date
199 execute_process( COMMAND "date" "+%Y-%m-%d %T %z"
200         OUTPUT_VARIABLE Build_Date
201         ERROR_QUIET
202         OUTPUT_STRIP_TRAILING_WHITESPACE
203 )
204
205 #| Last Commit Date
206 set( GitLastCommitDate "${Git_Modified_Status} ${Git_Branch_INFO} - ${Git_Date_INFO}" )
207
208 #| Uses CMake variables to include as defines
209 #| Primarily for USB configuration
210 configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/Lib/_buildvars.h buildvars.h )
211
212
213
214 ###
215 # Source Defines
216 #
217 set( SRCS
218         ${MAIN_SRCS}
219         ${COMPILER_SRCS}
220         ${SCAN_SRCS}
221         ${MACRO_SRCS}
222         ${OUTPUT_SRCS}
223         ${DEBUG_SRCS}
224 )
225
226 #| Directories to include by default
227 include_directories( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} )
228
229
230
231 ###
232 # Module Compatibility Check
233 #
234
235 #| Check for whether the set modules are compatible with the specified compiler family
236 ModuleCompatibility( ${ScanModulePath}   ${ScanModuleCompatibility}   )
237 ModuleCompatibility( ${MacroModulePath}  ${MacroModuleCompatibility}  )
238 ModuleCompatibility( ${OutputModulePath} ${OutputModuleCompatibility} )
239 ModuleCompatibility( ${DebugModulePath}  ${DebugModuleCompatibility}  )
240
241
242
243 ###
244 # CMake Module Checking
245 #
246 find_package( Git REQUIRED )
247 find_package( Ctags ) # Optional
248
249
250 ###
251 # ctag Generation
252 #
253
254 if( CTAGS_EXECUTABLE )
255         # Populate list of directories for ctags to parse
256         # NOTE: Doesn't support dots in the folder names...
257         foreach( filename ${SRCS} )
258                 string( REGEX REPLACE "/[a-zA-Z0-9_-]+.c$" "" pathglob ${filename} )
259                 file( GLOB filenames "${pathglob}/*.c" )
260                 set( CTAG_PATHS ${CTAG_PATHS} ${filenames} )
261                 file( GLOB filenames "${pathglob}/*.h" )
262                 set( CTAG_PATHS ${CTAG_PATHS} ${filenames} )
263         endforeach()
264
265         # Generate the ctags
266         execute_process( COMMAND ctags ${CTAG_PATHS}
267                 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
268         )
269 endif()
270
271
272
273 ###
274 # Build Targets
275 #
276
277 #| Create the .ELF file
278 set( TARGET_ELF ${TARGET}.elf )
279 add_executable( ${TARGET_ELF} ${SRCS} )
280
281
282 #| .ELF Properties
283 set_target_properties( ${TARGET_ELF} PROPERTIES
284         LINK_FLAGS ${LINKER_FLAGS}
285         SUFFIX ""                               # XXX Force Windows to keep the .exe off
286 )
287
288
289 #| Convert the .ELF into a .bin to load onto the McHCK
290 set( TARGET_BIN ${TARGET}.dfu.bin )
291 add_custom_command( TARGET ${TARGET_ELF} POST_BUILD
292         COMMAND ${CMAKE_OBJCOPY} ${BIN_FLAGS} ${TARGET_ELF} ${TARGET_BIN}
293         COMMENT "Creating binary file to load:  ${TARGET_BIN}"
294 )
295
296
297 #| Convert the .ELF into a .HEX to load onto the Teensy
298 set( TARGET_HEX ${TARGET}.teensy.hex )
299 add_custom_command( TARGET ${TARGET_ELF} POST_BUILD
300         COMMAND ${CMAKE_OBJCOPY} ${HEX_FLAGS} ${TARGET_ELF} ${TARGET_HEX}
301         COMMENT "Creating iHex file to load:    ${TARGET_HEX}"
302 )
303
304
305 #| Generate the Extended .LSS
306 set( TARGET_LSS ${TARGET}.lss )
307 add_custom_command( TARGET ${TARGET_ELF} POST_BUILD
308         COMMAND ${CMAKE_OBJDUMP} ${LSS_FLAGS} ${TARGET_ELF} > ${TARGET_LSS}
309         COMMENT "Creating Extended Listing:     ${TARGET_LSS}"
310 )
311
312
313 #| Generate the Symbol Table .SYM
314 set( TARGET_SYM ${TARGET}.sym )
315 add_custom_command( TARGET ${TARGET_ELF} POST_BUILD
316         COMMAND ${CMAKE_NM} -n ${TARGET_ELF} > ${TARGET_SYM}
317         COMMENT "Creating Symbol Table:         ${TARGET_SYM}"
318 )
319
320
321 #| Compiler Selection Record
322 add_custom_command( TARGET ${TARGET_ELF} POST_BUILD
323         COMMAND ${CMAKE_SOURCE_DIR}/Lib/CMake/writer compiler ${COMPILER_FAMILY}
324 )
325
326
327
328 ###
329 # Size Information
330 #
331
332 #| After Changes Size Information
333 add_custom_target( SizeAfter ALL
334         COMMAND ${CMAKE_SOURCE_DIR}/Lib/CMake/sizeCalculator ${CMAKE_SIZE} ihex ${TARGET_ELF} ${SIZE_RAM}   " SRAM"
335         COMMAND ${CMAKE_SOURCE_DIR}/Lib/CMake/sizeCalculator ${CMAKE_SIZE} ihex ${TARGET_HEX} ${SIZE_FLASH} "Flash"
336         DEPENDS ${TARGET_ELF}
337         COMMENT "Chip usage for ${CHIP}"
338 )
339
340
341
342 ###
343 # Setup Loader Script and Program
344 #
345
346
347 #| Provides the user with the correct teensy-loader-cli command for the built .HEX file
348 #| Windows
349 if( CMAKE_SYSTEM_NAME MATCHES "Windows" )
350         configure_file( LoadFile/winload load NEWLINE_STYLE UNIX )
351 #| Default
352 else()
353         configure_file( LoadFile/load load NEWLINE_STYLE UNIX )
354 endif()
355
356