1 ###| CMAKE Kiibohd Controller Source Configurator |###
3 # Written by Jacob Alexander in 2011-2014 for the Kiibohd Controller
5 # Released into the Public Domain
11 # CMake Custom Modules Path
14 set ( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/Lib/CMake/" )
19 # Module Overrides (Used in the buildall.bash script)
21 if ( ( DEFINED ScanModuleOverride ) AND ( EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/Scan/${ScanModuleOverride} ) )
22 set( ScanModule ${ScanModuleOverride} )
30 set( ScanModulePath "Scan/${ScanModule}" )
31 set( MacroModulePath "Macro/${MacroModule}" )
32 set( OutputModulePath "Output/${OutputModule}" )
33 set( DebugModulePath "Debug/${DebugModule}" )
35 #| Top-level directory adjustment
36 set( HEAD_DIR "${CMAKE_CURRENT_SOURCE_DIR}" )
41 # Module Check Function
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
55 message( FATAL_ERROR "${ModulePath} does not support the ${COMPILER_FAMILY} family..." )
61 # Module Configuration
64 #| Additional options, usually define settings
67 #| Include path for each of the modules
69 -I${HEAD_DIR}/${ScanModulePath}
70 -I${HEAD_DIR}/${MacroModulePath}
71 -I${HEAD_DIR}/${OutputModulePath}
72 -I${HEAD_DIR}/${DebugModulePath}
81 #| Go through lists of sources and append paths
83 #| PathPrepend( OutputListOfSources <Prepend Path> <InputListOfSources> )
84 macro( PathPrepend Output SourcesPath )
88 foreach( item ${ARGN} )
90 set( tmpSource ${tmpSource} "${SourcesPath}/${item}" )
93 # Finalize by writing the new list back over the old one
94 set( ${Output} ${tmpSource} )
99 include ( "${ScanModulePath}/setup.cmake" )
100 PathPrepend( SCAN_SRCS ${ScanModulePath} ${SCAN_SRCS} )
103 include ( "${MacroModulePath}/setup.cmake" )
104 PathPrepend( MACRO_SRCS ${MacroModulePath} ${MACRO_SRCS} )
107 include ( "${OutputModulePath}/setup.cmake" )
108 PathPrepend( OUTPUT_SRCS ${OutputModulePath} ${OUTPUT_SRCS} )
111 include ( "${DebugModulePath}/setup.cmake" )
112 PathPrepend( DEBUG_SRCS ${DebugModulePath} ${DEBUG_SRCS} )
116 # TODO Add support for different defaultMaps
117 configure_file( "${ScanModulePath}/defaultMap.h" defaultMap.h )
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}" )
133 # CMake Module Checking
135 find_package( Git REQUIRED )
136 find_package( Ctags ) # Optional
141 # Generate USB Defines
145 set( MANUFACTURER "Kiibohd" )
149 #| Attempt to call Git to get the branch, last commit date, and whether code modified since last commit
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
157 OUTPUT_STRIP_TRAILING_WHITESPACE
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" )
167 execute_process( COMMAND git rev-parse --abbrev-ref HEAD
168 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
169 OUTPUT_VARIABLE Git_Branch_INFO
171 OUTPUT_STRIP_TRAILING_WHITESPACE
175 execute_process( COMMAND git show -s --format=%ci
176 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
177 OUTPUT_VARIABLE Git_Date_INFO
179 OUTPUT_STRIP_TRAILING_WHITESPACE
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
187 OUTPUT_STRIP_TRAILING_WHITESPACE
191 execute_process( COMMAND git show -s --format=%H
192 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
193 OUTPUT_VARIABLE Git_Commit_Revision
195 OUTPUT_STRIP_TRAILING_WHITESPACE
199 execute_process( COMMAND git config --get remote.origin.url
200 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
201 OUTPUT_VARIABLE Git_Origin_URL
203 OUTPUT_STRIP_TRAILING_WHITESPACE
207 execute_process( COMMAND "date" "+%Y-%m-%d %T %z"
208 OUTPUT_VARIABLE Build_Date
210 OUTPUT_STRIP_TRAILING_WHITESPACE
214 set( GitLastCommitDate "${Git_Modified_Status} ${Git_Branch_INFO} - ${Git_Date_INFO}" )
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 )
234 #| Directories to include by default
235 include_directories( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} )
240 # Module Compatibility Check
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} )
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} )
267 execute_process( COMMAND ctags ${CTAG_PATHS}
268 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
278 #| Create the .ELF file
279 set( TARGET_ELF ${TARGET}.elf )
280 add_executable( ${TARGET_ELF} ${SRCS} )
284 set_target_properties( ${TARGET_ELF} PROPERTIES
285 LINK_FLAGS ${LINKER_FLAGS}
286 SUFFIX "" # XXX Force Windows to keep the .exe off
290 #| Convert the .ELF into a .bin to load onto the McHCK
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}"
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}"
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}"
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}"
326 #| Compiler Selection Record
327 add_custom_command( TARGET ${TARGET_ELF} POST_BUILD
328 COMMAND ${CMAKE_SOURCE_DIR}/Lib/CMake/writer compiler ${COMPILER_FAMILY}
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}"
348 # Setup Loader Script and Program
352 #| Provides the user with the correct teensy-loader-cli command for the built .HEX file
354 if( CMAKE_SYSTEM_NAME MATCHES "Windows" )
355 configure_file( LoadFile/winload load NEWLINE_STYLE UNIX )
358 configure_file( LoadFile/load load NEWLINE_STYLE UNIX )