1 ###| CMAKE Kiibohd Controller Source Configurator |###
3 # Written by Jacob Alexander in 2011-2015 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} )
31 set( ScanModulePath "Scan/${ScanModule}" )
32 set( MacroModulePath "Macro/${MacroModule}" )
33 set( OutputModulePath "Output/${OutputModule}" )
34 set( DebugModulePath "Debug/${DebugModule}" )
36 #| Top-level directory adjustment
37 set( HEAD_DIR "${CMAKE_CURRENT_SOURCE_DIR}" )
42 # Module Check Function
45 function ( ModuleCompatibility ModulePath )
46 foreach ( mod_var ${ARGN} )
47 if ( ${mod_var} STREQUAL ${COMPILER_FAMILY} )
48 # Module found, no need to scan further
53 message ( FATAL_ERROR "${ModulePath} does not support the ${COMPILER_FAMILY} family..." )
62 #| Go through lists of sources and append paths
64 #| PathPrepend( OutputListOfSources <Prepend Path> <InputListOfSources> )
65 macro ( PathPrepend Output SourcesPath )
69 foreach ( item ${ARGN} )
71 set ( tmpSource ${tmpSource} "${SourcesPath}/${item}" )
74 # Finalize by writing the new list back over the old one
75 set ( ${Output} ${tmpSource} )
83 # Optional Arg 1: Main Module Check, set to True/1 if adding a main module
85 function ( AddModule ModuleType ModuleName )
87 set ( ModulePath ${ModuleType}/${ModuleName} )
88 set ( ModuleFullPath ${HEAD_DIR}/${ModuleType}/${ModuleName} )
90 # Include setup.cmake file
91 include ( ${ModuleFullPath}/setup.cmake )
93 # Check if this is a main module add
94 foreach ( extraArg ${ARGN} )
95 # Make sure this isn't a submodule
96 if ( DEFINED SubModule )
98 "The '${ModuleName}' module is not a stand-alone module, and requires further setup."
103 # PathPrepend to give proper paths to each of the source files
104 PathPrepend ( Module_SRCS ${ModulePath} ${Module_SRCS} )
106 # Check the current scope to see if a sub-module added some source files
107 # Append each of the sources to each type of module srcs list
108 set ( ${ModuleType}_SRCS ${${ModuleType}_SRCS} ${Module_SRCS} )
111 add_definitions ( -I${ModuleFullPath} )
113 # Check module compatibility
114 ModuleCompatibility( ${ModulePath} ${ModuleCompatibility} )
116 # Check if this is a main module add
117 foreach ( extraArg ${ARGN} )
118 # Display detected source files
119 if ( NOT DEFINED SubModule )
120 message ( STATUS "Detected ${ModuleType} Module Source Files:" )
121 message ( "${${ModuleType}_SRCS}" )
125 # Check for any capabilities.kll files in the Module
126 set ( kll_capabilities_file "${ModuleFullPath}/capabilities.kll" )
127 if ( EXISTS ${kll_capabilities_file} )
128 # Add the kll file and any submodule kll files to the running list
129 set ( ${ModuleType}Module_KLL ${${ModuleType}Module_KLL} ${kll_capabilities_file} )
133 # Finally, add the sources and kll files to the parent scope (i.e. return)
134 set ( ${ModuleType}_SRCS ${${ModuleType}_SRCS} PARENT_SCOPE )
135 set ( ${ModuleType}Module_KLL ${${ModuleType}Module_KLL} PARENT_SCOPE )
140 AddModule ( Scan ${ScanModule} 1 )
141 AddModule ( Macro ${MacroModule} 1 )
142 AddModule ( Output ${OutputModule} 1 )
143 AddModule ( Debug ${DebugModule} 1 )
148 # CMake Module Checking
150 find_package ( Git REQUIRED )
151 find_package ( Ctags ) # Optional
156 # Generate USB Defines
160 set( MANUFACTURER "Kiibohd" )
164 #| Attempt to call Git to get the branch, last commit date, and whether code modified since last commit
167 #| Takes a bit of work to extract the "M " using CMake, and not using it if there are no modifications
168 execute_process( COMMAND ${GIT_EXECUTABLE} status -s -uno --porcelain
169 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
170 OUTPUT_VARIABLE Git_Modified_INFO
172 OUTPUT_STRIP_TRAILING_WHITESPACE
174 string( LENGTH "${Git_Modified_INFO}" Git_Modified_LENGTH )
175 set( Git_Modified_Status "Clean" )
176 if ( ${Git_Modified_LENGTH} GREATER 2 )
177 string( SUBSTRING "${Git_Modified_INFO}" 1 2 Git_Modified_Flag_INFO )
178 set( Git_Modified_Status "Dirty" )
182 execute_process( COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
183 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
184 OUTPUT_VARIABLE Git_Branch_INFO
186 OUTPUT_STRIP_TRAILING_WHITESPACE
190 execute_process( COMMAND ${GIT_EXECUTABLE} show -s --format=%ci
191 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
192 OUTPUT_VARIABLE Git_Date_INFO
194 OUTPUT_STRIP_TRAILING_WHITESPACE
197 #| Commit Author and Email
198 execute_process( COMMAND ${GIT_EXECUTABLE} show -s --format="%cn <%ce>"
199 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
200 OUTPUT_VARIABLE Git_Commit_Author
202 OUTPUT_STRIP_TRAILING_WHITESPACE
206 execute_process( COMMAND ${GIT_EXECUTABLE} show -s --format=%H
207 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
208 OUTPUT_VARIABLE Git_Commit_Revision
210 OUTPUT_STRIP_TRAILING_WHITESPACE
214 execute_process( COMMAND ${GIT_EXECUTABLE} config --get remote.origin.url
215 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
216 OUTPUT_VARIABLE Git_Origin_URL
218 OUTPUT_STRIP_TRAILING_WHITESPACE
222 execute_process( COMMAND "date" "+%Y-%m-%d %T %z"
223 OUTPUT_VARIABLE Build_Date
225 OUTPUT_STRIP_TRAILING_WHITESPACE
229 set( GitLastCommitDate "${Git_Modified_Status} ${Git_Branch_INFO} - ${Git_Date_INFO}" )
231 #| Uses CMake variables to include as defines
232 #| Primarily for USB configuration
233 configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/Lib/_buildvars.h buildvars.h )
249 #| Directories to include by default
250 include_directories( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} )
258 if( CTAGS_EXECUTABLE )
259 # Populate list of directories for ctags to parse
260 # NOTE: Doesn't support dots in the folder names...
261 foreach( filename ${SRCS} )
262 string( REGEX REPLACE "/[a-zA-Z0-9_-]+.c$" "" pathglob ${filename} )
263 file( GLOB filenames "${pathglob}/*.c" )
264 set( CTAG_PATHS ${CTAG_PATHS} ${filenames} )
265 file( GLOB filenames "${pathglob}/*.h" )
266 set( CTAG_PATHS ${CTAG_PATHS} ${filenames} )
270 execute_process( COMMAND ctags ${CTAG_PATHS}
271 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}