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