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" )
181 #| List of modified files
182 execute_process ( COMMAND ${GIT_EXECUTABLE} diff-index --name-only HEAD --
183 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
184 OUTPUT_VARIABLE Git_Modified_Files
186 OUTPUT_STRIP_TRAILING_WHITESPACE
188 string ( REGEX REPLACE "\n" "\\\\r\\\\n\\\\t" Git_Modified_Files "${Git_Modified_Files}" )
189 set ( Git_Modified_Files "\\r\\n\\t${Git_Modified_Files}" )
192 execute_process( COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
193 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
194 OUTPUT_VARIABLE Git_Branch_INFO
196 OUTPUT_STRIP_TRAILING_WHITESPACE
200 execute_process ( COMMAND ${GIT_EXECUTABLE} show -s --format=%ci
201 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
202 OUTPUT_VARIABLE Git_Date_INFO
204 OUTPUT_STRIP_TRAILING_WHITESPACE
207 #| Commit Author and Email
208 execute_process ( COMMAND ${GIT_EXECUTABLE} show -s --format="%cn <%ce>"
209 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
210 OUTPUT_VARIABLE Git_Commit_Author
212 OUTPUT_STRIP_TRAILING_WHITESPACE
216 execute_process ( COMMAND ${GIT_EXECUTABLE} show -s --format=%H
217 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
218 OUTPUT_VARIABLE Git_Commit_Revision
220 OUTPUT_STRIP_TRAILING_WHITESPACE
224 execute_process ( COMMAND ${GIT_EXECUTABLE} config --get remote.origin.url
225 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
226 OUTPUT_VARIABLE Git_Origin_URL
228 OUTPUT_STRIP_TRAILING_WHITESPACE
232 execute_process ( COMMAND "date" "+%Y-%m-%d %T %z"
233 OUTPUT_VARIABLE Build_Date
235 OUTPUT_STRIP_TRAILING_WHITESPACE
239 set ( GitLastCommitDate "${Git_Modified_Status} ${Git_Branch_INFO} - ${Git_Date_INFO}" )
241 #| Uses CMake variables to include as defines
242 #| Primarily for USB configuration
243 configure_file ( ${CMAKE_CURRENT_SOURCE_DIR}/Lib/_buildvars.h buildvars.h )
259 #| Directories to include by default
260 include_directories ( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} )
268 if ( CTAGS_EXECUTABLE )
269 # Populate list of directories for ctags to parse
270 # NOTE: Doesn't support dots in the folder names...
271 foreach ( filename ${SRCS} )
272 string ( REGEX REPLACE "/[a-zA-Z0-9_-]+.c$" "" pathglob ${filename} )
273 file ( GLOB filenames "${pathglob}/*.c" )
274 set ( CTAG_PATHS ${CTAG_PATHS} ${filenames} )
275 file ( GLOB filenames "${pathglob}/*.h" )
276 set ( CTAG_PATHS ${CTAG_PATHS} ${filenames} )
280 execute_process ( COMMAND ctags --fields=+l ${CTAG_PATHS}
281 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}