forked from kiibohd/controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodules.cmake
230 lines (174 loc) · 5.76 KB
/
modules.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
###| CMAKE Kiibohd Controller Source Configurator |###
#
# Written by Jacob Alexander in 2011-2017 for the Kiibohd Controller
#
# Released into the Public Domain
#
###
###
# CMake Custom Modules Path
#
set ( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/Lib/CMake/" )
###
# Host Build Mode (Override ScanModule and OutputModules)
# Or Normal Path Setup
#
if ( HostBuild )
set( ScanModulePath "Scan/TestIn" )
set( MacroModulePath "Macro/${MacroModule}" )
set( OutputModulePath "Output/TestOut" )
set( DebugModulePath "Debug/${DebugModule}" )
#| Normal Path Setup
else ()
set( ScanModulePath "Scan/${ScanModule}" )
set( MacroModulePath "Macro/${MacroModule}" )
set( OutputModulePath "Output/${OutputModule}" )
set( DebugModulePath "Debug/${DebugModule}" )
endif ()
#| Top-level directory adjustment
set( HEAD_DIR "${CMAKE_CURRENT_SOURCE_DIR}" )
###
# Module Check Function
#
function ( ModuleCompatibility ModulePath )
foreach ( mod_var ${ARGN} )
if ( ${mod_var} STREQUAL ${COMPILER_FAMILY} )
# Module found, no need to scan further
return()
endif ()
endforeach ()
message ( FATAL_ERROR "${ModulePath} does not support the ${COMPILER_FAMILY} family..." )
endfunction ()
###
# Module Processing
#
#| Go through lists of sources and append paths
#| Usage:
#| PathPrepend( OutputListOfSources <Prepend Path> <InputListOfSources> )
macro ( PathPrepend Output SourcesPath )
unset ( tmpSource )
# Loop through items
foreach ( item ${ARGN} )
# If the leading character is a / treat as an absolute path
string ( SUBSTRING "${item}" 0 1 character )
string ( SUBSTRING "${item}" 1 2 windows_drv )
if ( character STREQUAL "/" )
set ( tmpSource ${tmpSource} "${item}" )
# Check if a Windows Drive path
elseif ( windows_drv STREQUAL ":/" )
set ( tmpSource ${tmpSource} "${item}" )
# Otherwise just set the path
else ()
set ( tmpSource ${tmpSource} "${SourcesPath}/${item}" )
endif ()
endforeach ()
# Finalize by writing the new list back over the old one
set ( ${Output} ${tmpSource} )
endmacro ()
###
# Add Module Macro
#
# Optional Arg 1: Main Module Check, set to True/1 if adding a main module
function ( AddModule ModuleType ModuleName )
# Module path
set ( ModulePath ${ModuleType}/${ModuleName} )
set ( ModuleFullPath ${HEAD_DIR}/${ModuleType}/${ModuleName} )
# Include setup.cmake file
include ( ${ModuleFullPath}/setup.cmake )
# Check if this is a main module add
foreach ( extraArg ${ARGN} )
# Make sure this isn't a submodule
if ( DEFINED SubModule )
message ( FATAL_ERROR
"The '${ModuleName}' module is not a stand-alone module, and requires further setup."
)
endif ()
endforeach ()
# PathPrepend to give proper paths to each of the source files
PathPrepend ( Module_SRCS ${ModulePath} ${Module_SRCS} )
# Check the current scope to see if a sub-module added some source files
# Append each of the sources to each type of module srcs list
set ( ${ModuleType}_SRCS ${${ModuleType}_SRCS} ${Module_SRCS} )
# Add .h files
add_definitions ( -I${ModuleFullPath} )
# Check module compatibility
# Ignore if overriding the compiler family
if ( NOT CompilerOverride )
ModuleCompatibility( ${ModulePath} ${ModuleCompatibility} )
endif ()
# Check if this is a main module add
foreach ( extraArg ${ARGN} )
# Display detected source files
if ( NOT DEFINED SubModule )
message ( STATUS "Detected ${ModuleType} Module Source Files:" )
message ( "${${ModuleType}_SRCS}" )
endif ()
endforeach ()
# Check for any capabilities.kll files in the Module
set ( kll_capabilities_file "${ModuleFullPath}/capabilities.kll" )
if ( EXISTS ${kll_capabilities_file} )
# Add the kll file and any submodule kll files to the running list
set ( ${ModuleType}Module_KLL ${${ModuleType}Module_KLL} ${kll_capabilities_file} )
endif ()
# Finally, add the sources and kll files to the parent scope (i.e. return)
set ( ${ModuleType}_SRCS ${${ModuleType}_SRCS} PARENT_SCOPE )
set ( ${ModuleType}Module_KLL ${${ModuleType}Module_KLL} PARENT_SCOPE )
endfunction ()
#| Add main modules
if ( HostBuild )
AddModule ( Scan TestIn 1 )
AddModule ( Macro ${MacroModule} 1 )
AddModule ( Output TestOut 1 )
AddModule ( Debug ${DebugModule} 1 )
else ()
AddModule ( Scan ${ScanModule} 1 )
AddModule ( Macro ${MacroModule} 1 )
AddModule ( Output ${OutputModule} 1 )
AddModule ( Debug ${DebugModule} 1 )
endif ()
###
# CMake Build Env Checking
#
include( buildinfo )
#| Uses CMake variables to include as defines
#| Primarily for USB configuration
configure_file ( ${CMAKE_CURRENT_SOURCE_DIR}/Lib/_buildvars.h buildvars.h )
###
# Source Defines
#
set ( SRCS
${MAIN_SRCS}
${COMPILER_SRCS}
${Scan_SRCS}
${Macro_SRCS}
${Output_SRCS}
${Debug_SRCS}
)
#| Directories to include by default
include_directories ( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} )
###
# ctag Generation
#
find_package ( Ctags ) # Optional
if ( CTAGS_EXECUTABLE )
# Populate list of directories for ctags to parse
# NOTE: Doesn't support dots in the folder names...
foreach ( filename ${SRCS} )
string ( REGEX REPLACE "/[a-zA-Z0-9_-]+.c$" "" pathglob ${filename} )
file ( GLOB filenames "${pathglob}/*.c" )
set ( CTAG_PATHS ${CTAG_PATHS} ${filenames} )
file ( GLOB filenames "${pathglob}/*.h" )
set ( CTAG_PATHS ${CTAG_PATHS} ${filenames} )
endforeach ()
# Generate the ctags
execute_process ( COMMAND ${CTAGS_EXECUTABLE} --fields=+l ${CTAG_PATHS}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
endif ()
###
# Create compile_commands.json (Useful for language servers as a ctags alternative)
#
set( CMAKE_EXPORT_COMPILE_COMMANDS ON)
execute_process (
COMMAND ${CMAKE_COMMAND} -E create_symlink ${CMAKE_BINARY_DIR}/compile_commands.json ${CMAKE_SOURCE_DIR}/compile_commands.json
)