]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Lib/CMake/kll.cmake
80d44c176822d5f281b0a7e4155ca5d4376e9fdc
[kiibohd-controller.git] / Lib / CMake / kll.cmake
1 ###| CMAKE Kiibohd Controller KLL Configurator |###
2 #
3 # Written by Jacob Alexander in 2014-2015 for the Kiibohd Controller
4 #
5 # Released into the Public Domain
6 #
7 ###
8
9
10 ###
11 # Check if KLL compiler is needed
12 #
13
14 if ( "${MacroModule}" STREQUAL "PartialMap" )
15
16
17
18 ###
19 # KLL Installation (Make sure repo has been cloned)
20 #
21
22 if ( NOT EXISTS "${PROJECT_SOURCE_DIR}/kll/kll.py" )
23         message ( STATUS "Downloading latest kll version:" )
24
25         # Make sure git is available
26         find_package ( Git REQUIRED )
27
28         # Clone kll git repo
29         execute_process ( COMMAND ${GIT_EXECUTABLE} clone https://github.com/kiibohd/kll.git
30                 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
31         )
32 elseif ( REFRESH_KLL ) # Otherwise attempt to update the repo
33         message ( STATUS "Checking for latest kll version:" )
34
35         # Clone kll git repo
36         execute_process ( COMMAND ${GIT_EXECUTABLE} pull --rebase
37                 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/kll
38         )
39 endif () # kll/kll.py exists
40
41
42
43 ###
44 # Prepare KLL layout arguments
45 #
46
47 #| KLL_DEPENDS is used to build a dependency tree for kll.py, this way when files are changed, kll.py gets re-run
48
49 #| Add each of the detected capabilities.kll
50 foreach ( filename ${ScanModule_KLL} ${MacroModule_KLL} ${OutputModule_KLL} ${DebugModule_KLL} )
51         set ( BaseMap_Args ${BaseMap_Args} ${filename} )
52         set ( KLL_DEPENDS ${KLL_DEPENDS} ${filename} )
53 endforeach ()
54
55 #| If set BaseMap cannot be found, use default map
56 set ( pathname "${PROJECT_SOURCE_DIR}/${ScanModulePath}" )
57 if ( NOT EXISTS ${pathname}/${BaseMap}.kll )
58         set ( BaseMap_Args ${BaseMap_Args} ${pathname}/defaultMap.kll )
59         set ( KLL_DEPENDS ${KLL_DEPENDS} ${pathname}/defaultMap.kll )
60 elseif ( EXISTS "${pathname}/${BaseMap}.kll" )
61         set ( BaseMap_Args ${BaseMap_Args} ${pathname}/${BaseMap}.kll )
62         set ( KLL_DEPENDS ${KLL_DEPENDS} ${pathname}/${BaseMap}.kll )
63 else ()
64         message ( FATAL "Could not find '${BaseMap}.kll'" )
65 endif ()
66
67 #| Configure DefaultMap if specified
68 if ( NOT "${DefaultMap}" STREQUAL "" )
69         set ( DefaultMap_Args -d )
70
71         string ( REPLACE " " ";" MAP_LIST ${DefaultMap} ) # Change spaces to semicolons
72         foreach ( MAP ${MAP_LIST} )
73                 # Check if kll file is in build directory, otherwise default to layout directory
74                 if ( EXISTS "${PROJECT_BINARY_DIR}/${MAP}.kll" )
75                         set ( DefaultMap_Args ${DefaultMap_Args} ${MAP}.kll )
76                         set ( KLL_DEPENDS ${KLL_DEPENDS} ${PROJECT_BINARY_DIR}/${MAP}.kll )
77                 elseif ( EXISTS "${PROJECT_SOURCE_DIR}/kll/layouts/${MAP}.kll" )
78                         set ( DefaultMap_Args ${DefaultMap_Args} ${PROJECT_SOURCE_DIR}/kll/layouts/${MAP}.kll )
79                         set ( KLL_DEPENDS ${KLL_DEPENDS} ${PROJECT_SOURCE_DIR}/kll/layouts/${MAP}.kll )
80                 else ()
81                         message ( FATAL "Could not find '${MAP}.kll'" )
82                 endif ()
83         endforeach ()
84 endif ()
85
86 #| Configure PartialMaps if specified
87 if ( NOT "${PartialMaps}" STREQUAL "" )
88         # For each partial layer
89         foreach ( MAP ${PartialMaps} )
90                 set ( PartialMap_Args ${PartialMap_Args} -p )
91
92                 # Combine each layer
93                 string ( REPLACE " " ";" MAP_LIST ${MAP} ) # Change spaces to semicolons
94                 foreach ( MAP_PART ${MAP_LIST} )
95                         # Check if kll file is in build directory, otherwise default to layout directory
96                         if ( EXISTS "${PROJECT_BINARY_DIR}/${MAP_PART}.kll" )
97                                 set ( PartialMap_Args ${PartialMap_Args} ${MAP_PART}.kll )
98                                 set ( KLL_DEPENDS ${KLL_DEPENDS} ${PROJECT_BINARY_DIR}/${MAP_PART}.kll )
99                         elseif ( EXISTS "${PROJECT_SOURCE_DIR}/kll/layouts/${MAP_PART}.kll" )
100                                 set ( PartialMap_Args ${PartialMap_Args} ${PROJECT_SOURCE_DIR}/kll/layouts/${MAP_PART}.kll )
101                                 set ( KLL_DEPENDS ${KLL_DEPENDS} ${PROJECT_SOURCE_DIR}/kll/layouts/${MAP_PART}.kll )
102                         else ()
103                                 message ( FATAL "Could not find '${MAP_PART}.kll'" )
104                         endif ()
105                 endforeach ()
106         endforeach ()
107 endif ()
108
109
110 #| Print list of layout sources used
111 message ( STATUS "Detected Layout Files:" )
112 foreach ( filename ${KLL_DEPENDS} )
113         message ( "${filename}" )
114 endforeach ()
115
116
117
118 ###
119 # Run KLL Compiler
120 #
121
122 #| KLL Options
123 set ( kll_backend   --backend kiibohd )
124 set ( kll_template  --templates ${PROJECT_SOURCE_DIR}/kll/templates/kiibohdKeymap.h ${PROJECT_SOURCE_DIR}/kll/templates/kiibohdDefs.h )
125 set ( kll_outputname generatedKeymap.h kll_defs.h )
126 set ( kll_output    --outputs ${kll_outputname} )
127
128 #| KLL Cmd
129 set ( kll_cmd ${PROJECT_SOURCE_DIR}/kll/kll.py ${BaseMap_Args} ${DefaultMap_Args} ${PartialMap_Args} ${kll_backend} ${kll_template} ${kll_output} )
130 add_custom_command ( OUTPUT ${kll_outputname}
131         COMMAND ${kll_cmd}
132         DEPENDS ${KLL_DEPENDS}
133         COMMENT "Generating KLL Layout"
134 )
135
136 #| KLL Regen Convenience Target
137 add_custom_target ( kll_regen
138         COMMAND ${kll_cmd}
139         COMMENT "Re-generating KLL Layout"
140 )
141
142 #| Append generated file to required sources so it becomes a dependency in the main build
143 set ( SRCS ${SRCS} ${kll_outputname} )
144
145
146
147 endif () # PartialMap
148