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 # Generate USB Defines
137 set( MANUFACTURER "Kiibohd" )
141 #| Attempt to call Git to get the branch, last commit date, and whether code modified since last commit
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
149 OUTPUT_STRIP_TRAILING_WHITESPACE
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" )
159 execute_process( COMMAND git rev-parse --abbrev-ref HEAD
160 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
161 OUTPUT_VARIABLE Git_Branch_INFO
163 OUTPUT_STRIP_TRAILING_WHITESPACE
167 execute_process( COMMAND git show -s --format=%ci
168 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
169 OUTPUT_VARIABLE Git_Date_INFO
171 OUTPUT_STRIP_TRAILING_WHITESPACE
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
179 OUTPUT_STRIP_TRAILING_WHITESPACE
183 execute_process( COMMAND git show -s --format=%H
184 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
185 OUTPUT_VARIABLE Git_Commit_Revision
187 OUTPUT_STRIP_TRAILING_WHITESPACE
191 execute_process( COMMAND git config --get remote.origin.url
192 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
193 OUTPUT_VARIABLE Git_Origin_URL
195 OUTPUT_STRIP_TRAILING_WHITESPACE
199 execute_process( COMMAND "date" "+%Y-%m-%d %T %z"
200 OUTPUT_VARIABLE Build_Date
202 OUTPUT_STRIP_TRAILING_WHITESPACE
206 set( GitLastCommitDate "${Git_Modified_Status} ${Git_Branch_INFO} - ${Git_Date_INFO}" )
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 )
226 #| Directories to include by default
227 include_directories( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} )
232 # Module Compatibility Check
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} )
244 # CMake Module Checking
246 find_package( Git REQUIRED )
247 find_package( Ctags ) # Optional
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} )
266 execute_process( COMMAND ctags ${CTAG_PATHS}
267 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
277 #| Create the .ELF file
278 set( TARGET_ELF ${TARGET}.elf )
279 add_executable( ${TARGET_ELF} ${SRCS} )
283 set_target_properties( ${TARGET_ELF} PROPERTIES
284 LINK_FLAGS ${LINKER_FLAGS}
285 SUFFIX "" # XXX Force Windows to keep the .exe off
289 #| Convert the .ELF into a .HEX to load onto the Teensy
290 set( TARGET_HEX ${TARGET}.hex )
291 add_custom_command( TARGET ${TARGET_ELF} POST_BUILD
292 COMMAND ${CMAKE_OBJCOPY} ${HEX_FLAGS} ${TARGET_ELF} ${TARGET_HEX}
293 COMMENT "Creating iHex file to load: ${TARGET_HEX}"
297 #| Generate the Extended .LSS
298 set( TARGET_LSS ${TARGET}.lss )
299 add_custom_command( TARGET ${TARGET_ELF} POST_BUILD
300 COMMAND ${CMAKE_OBJDUMP} ${LSS_FLAGS} ${TARGET_ELF} > ${TARGET_LSS}
301 COMMENT "Creating Extended Listing: ${TARGET_LSS}"
305 #| Generate the Symbol Table .SYM
306 set( TARGET_SYM ${TARGET}.sym )
307 add_custom_command( TARGET ${TARGET_ELF} POST_BUILD
308 COMMAND ${CMAKE_NM} -n ${TARGET_ELF} > ${TARGET_SYM}
309 COMMENT "Creating Symbol Table: ${TARGET_SYM}"
313 #| Compiler Selection Record
314 add_custom_command( TARGET ${TARGET_ELF} POST_BUILD
315 COMMAND ${CMAKE_SOURCE_DIR}/Lib/CMake/writer compiler ${COMPILER_FAMILY}
324 #| After Changes Size Information
325 add_custom_target( SizeAfter ALL
326 COMMAND ${CMAKE_SOURCE_DIR}/Lib/CMake/sizeCalculator ${CMAKE_SIZE} ${FORMAT} ${TARGET_ELF} ${SIZE_RAM} " SRAM"
327 COMMAND ${CMAKE_SOURCE_DIR}/Lib/CMake/sizeCalculator ${CMAKE_SIZE} ${FORMAT} ${TARGET_HEX} ${SIZE_FLASH} "Flash"
328 DEPENDS ${TARGET_ELF}
329 COMMENT "Chip usage for ${CHIP}"
335 # Setup Loader Script and Program
339 #| Provides the user with the correct teensy-loader-cli command for the built .HEX file
341 if( CMAKE_SYSTEM_NAME MATCHES "Windows" )
342 configure_file( LoadFile/winload load NEWLINE_STYLE UNIX )
345 configure_file( LoadFile/load load NEWLINE_STYLE UNIX )