forked from Civitasv/cmake-tools.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariants.lua
More file actions
286 lines (249 loc) · 8.94 KB
/
variants.lua
File metadata and controls
286 lines (249 loc) · 8.94 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
--[[ local utils = require("cmake-tools.utils") ]]
local variants = {}
local syaml = require("simpleyaml")
-- fallback if no cmake-variants.[yaml|json] is found
local DEFAULT_VARIANTS = { "Debug", "Release", "RelWithDebInfo", "MinSizeRel" }
local DEFAULT_VARIANTS_VAL = {
{ short = "Debug", long = "", kv = { buildType = "Debug" } },
{ short = "Release", long = "", kv = { buildType = "Release" } },
{ short = "RelWithDebInfo", long = "", kv = { buildType = "RelWithDebInfo" } },
{ short = "MinSizeRel", long = "", kv = { buildType = "MinSizeRel" } },
}
-- checks if there is a cmake-variants.[yaml|json] file and parses it to a Lua table
function variants.parse(cwd)
-- helper function to find the config file
-- returns file path if found, nil otherwise
local function findcfg()
local files = vim.fn.readdir(cwd)
local file = nil
for _, f in ipairs(files) do -- iterate over files in current directory
if
f == "cmake-variants.yaml"
or f == "cmake-variants.json"
or f == "CMakeVariants.yaml"
or f == "CMakeVariants.json"
then -- if a variants config file is found
file = vim.fn.resolve(cwd .. "/" .. f)
break
end
end
return file
end
-- start parsing
local config = nil
local file = findcfg() -- check for config file
if file then -- if one is found ...
if file:match(".*%.yaml") then -- .. and is a YAML file, parse it with simpleyaml
config = syaml.parse_file(file)
else -- otherwise parse it with neovim's JSON parser
config = vim.fn.json_decode(vim.fn.readfile(file))
end
end
return config
end
-- returns a list of string descriptions of all possible combinations of configurations, using their short names and optional detailed description
function variants.get(variants_opt, cwd)
-- helper function to collect all short names of choices
local function collect_choices(config)
local defaults = { val = {}, kv = {} }
local choices = {}
local keys = {}
for key, option in pairs(config) do -- for all options
local cs = {}
for _, choice in pairs(option["choices"]) do -- for all choices of that option
table.insert(cs, choice) -- collect their short name
end
defaults.kv[key] = option["default"]
table.insert(defaults.val, option["choices"][option["default"]]["short"])
table.insert(choices, cs)
table.insert(keys, key)
end
return defaults, keys, choices
end
-- helper function to create all possible combinations of choices (cartesian product)
local function create_combinations(keys, choices)
-- The following code is a *modified* version of
-- https://rosettacode.org/wiki/Cartesian_product_of_two_or_more_lists#Functional-esque_(non-iterator)
-- under CC-BY-SA 4.0
-- accessed: 01.10.22
-- BEGIN code from rosettacode
-- support:
local function T(t)
return setmetatable(t, { __index = table })
end
local function clone(t)
local s = T({})
for k, v in ipairs(t) do
s[k] = v
end
return s
end
local function reduce(t, f, acc)
for i = 1, #t do
acc = f(t[i], acc)
end
return acc
end
-- implementation:
local function cartprod(sets)
local temp, prod = T({}), T({})
local function descend(depth)
for _, v in ipairs(sets[depth]) do
temp[depth] = v
if depth == #sets then
prod[#prod + 1] = clone(temp)
else
descend(depth + 1)
end
end
end
descend(1)
return prod
end
-- END code from rosettacode
local combinations = cartprod(choices)
local strings = reduce(combinations, function(t, a)
local function handleItem()
local res = { short = "", long = "", kv = {} }
for i = 1, #t do
res.kv[keys[i]] = t[i]["short"]
res.short = res.short .. t[i]["short"]
if i ~= #t then
res.short = res.short .. " + "
end
end
if variants_opt.long.show then
local length = 0
local max_length = variants_opt.long.max_length
res.long = res.long .. "("
length = length + 1
for i = 1, #t do
local detailed = t[i]["long"]
-- if too long, then just show ...
if length + #detailed >= max_length then
res.long = res.long .. string.sub(detailed, 1, max_length - length) .. "..."
break
end
res.long = res.long .. detailed
if i ~= #t then
res.long = res.long .. " + "
end
length = length + #detailed + 3
end
res.long = res.long .. ")"
end
return res
end
table.insert(a, handleItem())
return a
end, {})
return strings
end
-- start parsing
local config = variants.parse(cwd)
if config then -- if a config is found
local defaults, keys, choices = collect_choices(config) -- collect all possible choices from it
local combinations = create_combinations(keys, choices) -- calculate the cartesian product
table.sort(combinations, function(lhs, rhs)
return lhs.short < rhs.short
end) -- sort lexicographically
return defaults, combinations
end -- otherwise return the defaults
return { val = { DEFAULT_VARIANTS_VAL[1].short }, kv = DEFAULT_VARIANTS_VAL[1].kv },
DEFAULT_VARIANTS_VAL
end
function variants.debuggable(variant, cwd)
-- check if the chosen variants is one of the defaults
for _, defaultvar in ipairs(DEFAULT_VARIANTS) do
if variant == defaultvar then
return variant == "Debug" or variant == "RelWithDebInfo"
end
end
-- otherwise, find the config file to parse
local config = variants.parse(cwd)
if not config then
return false -- silent error (empty arglist) if no config file found
end
-- for each choice in the chosen variant
for choice in string.gmatch(variant, "%s*([^+]+)%s*") do -- split variant string on + to get choices
local choice_found = false
choice = choice:match("^%s*(.-)%s*$") -- trim (or come up with a better regex above)
for _, option in pairs(config) do -- search for the choice
for _, chc in pairs(option["choices"]) do
local short = chc["short"]
if choice == short then -- if the choice is found, add to the argument list according to the defined keys
if chc["buildType"] ~= nil then -- CMAKE_BUILD_TYPE
return chc["buildType"] == "Debug" or chc["buildType"] == "RelWithDebInfo"
end
choice_found = true
break -- choice found, break loops
end
end
if choice_found then
break
end
end
end
return false
end
-- given a variant, build an argument list for CMake
function variants.build_arglist(variant, cwd)
-- helper function to build a simple command line option that defines the CMAKE_BUILD_TYPE variable to `build_type`
local function build_simple(build_type)
return { "-D", "CMAKE_BUILD_TYPE:STRING=" .. build_type }
end
local config = variants.parse(cwd)
if not config then
-- check if the chosen variants is one of the defaults
for _, defaultvar in pairs(DEFAULT_VARIANTS) do
if variant == defaultvar then
return build_simple(variant) -- if so, build the simple arglist and return
end
end
return {} -- silent error (empty arglist) if no config file found
end
local args = {}
-- local function to add an argument to `args`
local function add_args(as)
for _, a in pairs(as) do
table.insert(args, a)
end
end
-- for each choice in the chosen variant
for choice in string.gmatch(variant, "%s*([^+]+)%s*") do -- split variant string on + to get choices
local choice_found = false
choice = choice:match("^%s*(.-)%s*$") -- trim (or come up with a better regex above)
for _, option in pairs(config) do -- search for the choice
for _, chc in pairs(option["choices"]) do
local short = chc["short"]
if choice == short then -- if the choice is found, add to the argument list according to the defined keys
if chc["buildType"] ~= nil then -- CMAKE_BUILD_TYPE
add_args({ "-DCMAKE_BUILD_TYPE=" .. chc["buildType"] })
end
if chc["settings"] ~= nil then -- user DEFINITIONs
for k, v in pairs(chc["settings"]) do
add_args({ "-D" .. k .. "=" .. v })
end
end
if chc["linkage"] ~= nil then -- BUILD_SHARED_LIBS
local function add_linkage(linkage)
add_args({ "-DBUILD_SHARED_LIBS=" .. linkage })
end
if chc["linkage"] == "static" then
add_linkage("OFF")
elseif chc["linkage"] == "shared" then
add_linkage("ON")
end
end
choice_found = true
break -- choice found, break loops
end
end
if choice_found then
break
end
end
end
return args
end
return variants