]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Lib/CMake/modules.cmake
Adding compiler change detection.
[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 ###
12 # Module Overrides (Used in the buildall.bash script)
13 #
14 if ( ( DEFINED ScanModuleOverride ) AND ( EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/Scan/${ScanModuleOverride} ) )
15         set( ScanModule ${ScanModuleOverride} )
16 endif ()
17
18
19
20 ###
21 # Path Setup
22 #
23 set(  ScanModulePath    "Scan/${ScanModule}"   )
24 set( MacroModulePath   "Macro/${MacroModule}"  )
25 set( OutputModulePath "Output/${OutputModule}" )
26 set( DebugModulePath   "Debug/${DebugModule}"  )
27
28 #| Top-level directory adjustment
29 set( HEAD_DIR "${CMAKE_CURRENT_SOURCE_DIR}" )
30
31
32
33 ###
34 # Module Check Function
35 #
36
37 #| Usage:
38 #|  PathPrepend( ModulePath <ListOfFamiliesSupported> )
39 #| Uses the ${COMPILER_FAMILY} variable
40 function( ModuleCompatibility ModulePath )
41         foreach( mod_var ${ARGN} )
42                 if ( ${mod_var} STREQUAL ${COMPILER_FAMILY} )
43                         # Module found, no need to scan further
44                         return()
45                 endif ()
46         endforeach()
47
48         message( FATAL_ERROR "${ModulePath} does not support the ${COMPILER_FAMILY} family..." )
49 endfunction()
50
51
52
53 ###
54 # Module Configuration
55 #
56
57 #| Additional options, usually define settings
58 add_definitions()
59
60 #| Include path for each of the modules
61 add_definitions(
62         -I${HEAD_DIR}/${ScanModulePath}
63         -I${HEAD_DIR}/${MacroModulePath}
64         -I${HEAD_DIR}/${OutputModulePath}
65         -I${HEAD_DIR}/${DebugModulePath}
66 )
67
68
69
70 ###
71 # Module Processing
72 #
73
74 #| Go through lists of sources and append paths
75 #| Usage:
76 #|  PathPrepend( OutputListOfSources <Prepend Path> <InputListOfSources> )
77 macro( PathPrepend Output SourcesPath )
78         unset( tmpSource )
79
80         # Loop through items
81         foreach( item ${ARGN} )
82                 # Set the path
83                 set( tmpSource ${tmpSource} "${SourcesPath}/${item}" )
84         endforeach()
85
86         # Finalize by writing the new list back over the old one
87         set( ${Output} ${tmpSource} )
88 endmacro()
89
90
91 #| Scan Module
92 include    (            "${ScanModulePath}/setup.cmake"  )
93 PathPrepend(  SCAN_SRCS  ${ScanModulePath} ${SCAN_SRCS}  )
94
95 #| Macro Module
96 include    (           "${MacroModulePath}/setup.cmake"  )
97 PathPrepend( MACRO_SRCS ${MacroModulePath} ${MACRO_SRCS} )
98
99 #| Output Module
100 include    (             "${OutputModulePath}/setup.cmake"   )
101 PathPrepend( OUTPUT_SRCS  ${OutputModulePath} ${OUTPUT_SRCS} )
102
103 #| Debugging Module
104 include    (           "${DebugModulePath}/setup.cmake"  )
105 PathPrepend( DEBUG_SRCS ${DebugModulePath} ${DEBUG_SRCS} )
106
107
108 #| Default Map
109 # TODO Add support for different defaultMaps
110 configure_file( "${ScanModulePath}/defaultMap.h" defaultMap.h )
111
112
113 #| Print list of all module sources
114 message( STATUS "Detected Scan Module Source Files:" )
115 message( "${SCAN_SRCS}" )
116 message( STATUS "Detected Macro Module Source Files:" )
117 message( "${MACRO_SRCS}" )
118 message( STATUS "Detected Output Module Source Files:" )
119 message( "${OUTPUT_SRCS}" )
120 message( STATUS "Detected Debug Module Source Files:" )
121 message( "${DEBUG_SRCS}" )
122
123
124
125 ###
126 # Generate USB Defines
127 #
128
129 #| Manufacturer name
130 set( MANUFACTURER "Kiibohd" )
131
132
133 #| Serial Number
134 #| Attempt to call Git to get the branch, last commit date, and whether code modified since last commit
135
136 #| Modified
137 #| Takes a bit of work to extract the "M " using CMake, and not using it if there are no modifications
138 execute_process( COMMAND git status -s -uno --porcelain
139         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
140         OUTPUT_VARIABLE Git_Modified_INFO
141         ERROR_QUIET
142         OUTPUT_STRIP_TRAILING_WHITESPACE
143 )
144 string( LENGTH "${Git_Modified_INFO}" Git_Modified_LENGTH )
145 set( Git_Modified_Status "Clean" )
146 if ( ${Git_Modified_LENGTH} GREATER 2 )
147         string( SUBSTRING "${Git_Modified_INFO}" 1 2 Git_Modified_Flag_INFO )
148         set( Git_Modified_Status "Dirty" )
149 endif ()
150
151 #| Branch
152 execute_process( COMMAND git rev-parse --abbrev-ref HEAD
153         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
154         OUTPUT_VARIABLE Git_Branch_INFO
155         ERROR_QUIET
156         OUTPUT_STRIP_TRAILING_WHITESPACE
157 )
158
159 #| Date
160 execute_process( COMMAND git show -s --format=%ci
161         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
162         OUTPUT_VARIABLE Git_Date_INFO
163         ERROR_QUIET
164         OUTPUT_STRIP_TRAILING_WHITESPACE
165 )
166
167 #| Commit Author and Email
168 execute_process( COMMAND git show -s --format="%cn <%ce>"
169         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
170         OUTPUT_VARIABLE Git_Commit_Author
171         ERROR_QUIET
172         OUTPUT_STRIP_TRAILING_WHITESPACE
173 )
174
175 #| Commit Revision
176 execute_process( COMMAND git show -s --format=%H
177         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
178         OUTPUT_VARIABLE Git_Commit_Revision
179         ERROR_QUIET
180         OUTPUT_STRIP_TRAILING_WHITESPACE
181 )
182
183 #| Origin URL
184 execute_process( COMMAND git config --get remote.origin.url
185         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
186         OUTPUT_VARIABLE Git_Origin_URL
187         ERROR_QUIET
188         OUTPUT_STRIP_TRAILING_WHITESPACE
189 )
190
191 #| Build Date
192 execute_process( COMMAND "date" "+%Y-%m-%d %T %z"
193         OUTPUT_VARIABLE Build_Date
194         ERROR_QUIET
195         OUTPUT_STRIP_TRAILING_WHITESPACE
196 )
197
198 #| Last Commit Date
199 set( GitLastCommitDate "${Git_Modified_Status} ${Git_Branch_INFO} - ${Git_Date_INFO}" )
200
201 #| Uses CMake variables to include as defines
202 #| Primarily for USB configuration
203 configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/Lib/_buildvars.h buildvars.h )
204
205
206
207 ###
208 # Source Defines
209 #
210 set( SRCS
211         ${MAIN_SRCS}
212         ${COMPILER_SRCS}
213         ${SCAN_SRCS}
214         ${MACRO_SRCS}
215         ${OUTPUT_SRCS}
216         ${DEBUG_SRCS}
217 )
218
219 #| Directories to include by default
220 include_directories( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} )
221
222
223
224 ###
225 # Module Compatibility Check
226 #
227
228 #| Check for whether the set modules are compatible with the specified compiler family
229 ModuleCompatibility( ${ScanModulePath}   ${ScanModuleCompatibility}   )
230 ModuleCompatibility( ${MacroModulePath}  ${MacroModuleCompatibility}  )
231 ModuleCompatibility( ${OutputModulePath} ${OutputModuleCompatibility} )
232 ModuleCompatibility( ${DebugModulePath}  ${DebugModuleCompatibility}  )
233
234
235
236 ###
237 # CMake Module Checking
238 #
239 find_package( Git REQUIRED )
240
241
242 ###
243 # Build Targets
244 #
245
246 #| Create the .ELF file
247 set( TARGET_ELF ${TARGET}.elf )
248 add_executable( ${TARGET_ELF} ${SRCS} )
249
250
251 #| .ELF Properties
252 set_target_properties( ${TARGET_ELF} PROPERTIES
253         LINK_FLAGS ${LINKER_FLAGS}
254         SUFFIX ""                               # XXX Force Windows to keep the .exe off
255 )
256
257
258 #| Convert the .ELF into a .HEX to load onto the Teensy
259 set( TARGET_HEX ${TARGET}.hex )
260 add_custom_command( TARGET ${TARGET_ELF} POST_BUILD
261         COMMAND ${CMAKE_OBJCOPY} ${HEX_FLAGS} ${TARGET_ELF} ${TARGET_HEX}
262         COMMENT "Creating iHex file to load:    ${TARGET_HEX}"
263 )
264
265
266 #| Generate the Extended .LSS
267 set( TARGET_LSS ${TARGET}.lss )
268 add_custom_command( TARGET ${TARGET_ELF} POST_BUILD
269         COMMAND ${CMAKE_OBJDUMP} ${LSS_FLAGS} ${TARGET_ELF} > ${TARGET_LSS}
270         COMMENT "Creating Extended Listing:     ${TARGET_LSS}"
271 )
272
273
274 #| Generate the Symbol Table .SYM
275 set( TARGET_SYM ${TARGET}.sym )
276 add_custom_command( TARGET ${TARGET_ELF} POST_BUILD
277         COMMAND ${CMAKE_NM} -n ${TARGET_ELF} > ${TARGET_SYM}
278         COMMENT "Creating Symbol Table:         ${TARGET_SYM}"
279 )
280
281
282 #| Compiler Selection Record
283 add_custom_command( TARGET ${TARGET_ELF} POST_BUILD
284         COMMAND ${CMAKE_SOURCE_DIR}/Lib/CMake/writer compiler ${COMPILER_FAMILY}
285 )
286
287
288
289 ###
290 # Size Information
291 #
292
293 #| After Changes Size Information
294 add_custom_target( SizeAfter ALL
295         COMMAND ${CMAKE_SOURCE_DIR}/Lib/CMake/sizeCalculator ${CMAKE_SIZE} ${FORMAT} ${TARGET_ELF} ${SIZE_RAM}   " SRAM"
296         COMMAND ${CMAKE_SOURCE_DIR}/Lib/CMake/sizeCalculator ${CMAKE_SIZE} ${FORMAT} ${TARGET_HEX} ${SIZE_FLASH} "Flash"
297         DEPENDS ${TARGET_ELF}
298         COMMENT "Chip usage for ${CHIP}"
299 )
300
301
302
303 ###
304 # Setup Loader Script and Program
305 #
306
307
308 #| Provides the user with the correct teensy-loader-cli command for the built .HEX file
309 #| Windows
310 if( CMAKE_SYSTEM_NAME MATCHES "Windows" )
311         configure_file( LoadFile/winload load NEWLINE_STYLE UNIX )
312 #| Default
313 else()
314         configure_file( LoadFile/load load NEWLINE_STYLE UNIX )
315 endif()
316
317