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} )
115 #| Print list of all module sources
116 message( STATUS "Detected Scan Module Source Files:" )
117 message( "${SCAN_SRCS}" )
118 message( STATUS "Detected Macro Module Source Files:" )
119 message( "${MACRO_SRCS}" )
120 message( STATUS "Detected Output Module Source Files:" )
121 message( "${OUTPUT_SRCS}" )
122 message( STATUS "Detected Debug Module Source Files:" )
123 message( "${DEBUG_SRCS}" )
128 # CMake Module Checking
130 find_package( Git REQUIRED )
131 find_package( Ctags ) # Optional
136 # Generate USB Defines
140 set( MANUFACTURER "Kiibohd" )
144 #| Attempt to call Git to get the branch, last commit date, and whether code modified since last commit
147 #| Takes a bit of work to extract the "M " using CMake, and not using it if there are no modifications
148 execute_process( COMMAND ${GIT_EXECUTABLE} status -s -uno --porcelain
149 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
150 OUTPUT_VARIABLE Git_Modified_INFO
152 OUTPUT_STRIP_TRAILING_WHITESPACE
154 string( LENGTH "${Git_Modified_INFO}" Git_Modified_LENGTH )
155 set( Git_Modified_Status "Clean" )
156 if ( ${Git_Modified_LENGTH} GREATER 2 )
157 string( SUBSTRING "${Git_Modified_INFO}" 1 2 Git_Modified_Flag_INFO )
158 set( Git_Modified_Status "Dirty" )
162 execute_process( COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
163 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
164 OUTPUT_VARIABLE Git_Branch_INFO
166 OUTPUT_STRIP_TRAILING_WHITESPACE
170 execute_process( COMMAND ${GIT_EXECUTABLE} show -s --format=%ci
171 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
172 OUTPUT_VARIABLE Git_Date_INFO
174 OUTPUT_STRIP_TRAILING_WHITESPACE
177 #| Commit Author and Email
178 execute_process( COMMAND ${GIT_EXECUTABLE} show -s --format="%cn <%ce>"
179 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
180 OUTPUT_VARIABLE Git_Commit_Author
182 OUTPUT_STRIP_TRAILING_WHITESPACE
186 execute_process( COMMAND ${GIT_EXECUTABLE} show -s --format=%H
187 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
188 OUTPUT_VARIABLE Git_Commit_Revision
190 OUTPUT_STRIP_TRAILING_WHITESPACE
194 execute_process( COMMAND ${GIT_EXECUTABLE} config --get remote.origin.url
195 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
196 OUTPUT_VARIABLE Git_Origin_URL
198 OUTPUT_STRIP_TRAILING_WHITESPACE
202 execute_process( COMMAND "date" "+%Y-%m-%d %T %z"
203 OUTPUT_VARIABLE Build_Date
205 OUTPUT_STRIP_TRAILING_WHITESPACE
209 set( GitLastCommitDate "${Git_Modified_Status} ${Git_Branch_INFO} - ${Git_Date_INFO}" )
211 #| Uses CMake variables to include as defines
212 #| Primarily for USB configuration
213 configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/Lib/_buildvars.h buildvars.h )
229 #| Directories to include by default
230 include_directories( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} )
235 # Module Compatibility Check
238 #| Check for whether the set modules are compatible with the specified compiler family
239 ModuleCompatibility( ${ScanModulePath} ${ScanModuleCompatibility} )
240 ModuleCompatibility( ${MacroModulePath} ${MacroModuleCompatibility} )
241 ModuleCompatibility( ${OutputModulePath} ${OutputModuleCompatibility} )
242 ModuleCompatibility( ${DebugModulePath} ${DebugModuleCompatibility} )
250 if( CTAGS_EXECUTABLE )
251 # Populate list of directories for ctags to parse
252 # NOTE: Doesn't support dots in the folder names...
253 foreach( filename ${SRCS} )
254 string( REGEX REPLACE "/[a-zA-Z0-9_-]+.c$" "" pathglob ${filename} )
255 file( GLOB filenames "${pathglob}/*.c" )
256 set( CTAG_PATHS ${CTAG_PATHS} ${filenames} )
257 file( GLOB filenames "${pathglob}/*.h" )
258 set( CTAG_PATHS ${CTAG_PATHS} ${filenames} )
262 execute_process( COMMAND ctags ${CTAG_PATHS}
263 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
270 # Setup Loader Script and Program
273 #| Provides the user with the correct teensy-loader-cli command for the built .HEX file
275 if( CMAKE_SYSTEM_NAME MATCHES "Windows" )
276 configure_file( LoadFile/winload load NEWLINE_STYLE UNIX )
279 configure_file( LoadFile/load load NEWLINE_STYLE UNIX )