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
|
local config = require("cmake-explorer.config")
local Path = require("plenary.path")
local utils = require("cmake-explorer.utils")
local FileApi = require("cmake-explorer.file_api")
local VariantConfig = {}
VariantConfig.__index = VariantConfig
local variant_subs = {
["${workspaceFolder}"] = vim.loop.cwd(),
["${userHome}"] = vim.loop.os_homedir(),
}
function VariantConfig:new(obj)
setmetatable(obj, VariantConfig)
obj.subs = obj:_subs()
obj.build_directory = obj:_build_directory()
obj.configure_args = obj:_configure_args()
obj.configure_command = obj:_configure_command()
obj.build_args = obj:_build_args()
obj.build_command = obj:_build_command()
if not obj.fileapis[obj.build_directory] then
local fa = FileApi:new(obj.build_directory)
if fa and fa:exists() then
fa:read_reply()
obj.fileapis[obj.build_directory] = fa
end
end
return obj
end
function VariantConfig:_subs()
return vim.tbl_deep_extend("keep", variant_subs, { ["${buildType}"] = self.buildType })
end
function VariantConfig:_build_directory()
return utils.substitude(config.build_directory, self.subs)
end
function VariantConfig:_configure_args()
local args = {}
if self.generator then
table.insert(args, "-G " .. '"' .. self.generator .. '"')
end
if self.buildType then
table.insert(args, "-DCMAKE_BUILD_TYPE=" .. self.buildType)
end
if self.linkage and string.lower(self.linkage) == "static" then
table.insert(args, "-DCMAKE_BUILD_SHARED_LIBS=OFF")
elseif self.linkage and string.lower(self.linkage) == "shared" then
table.insert(args, "-DCMAKE_BUILD_SHARED_LIBS=ON")
end
for k, v in pairs(self.settings or {}) do
table.insert(args, "-D" .. k .. "=" .. v)
end
table.insert(args, "-DCMAKE_EXPORT_COMPILE_COMMANDS=ON")
table.insert(
args,
"-B" .. Path:new(self.build_directory):make_relative(utils.substitude(config.source_directory, self.subs))
)
return args
end
function VariantConfig:_configure_command()
local ret = {}
ret.cmd = config.cmake_path
ret.args = self.configure_args
ret.cwd = variant_subs["${workspaceFolder}"]
ret.env = vim.tbl_deep_extend("keep", self.env, config.configure_environment, config.environment)
ret.after_success = function()
utils.symlink_compile_commands(self.build_directory, variant_subs["${workspaceFolder}"])
self.fileapis[self.build_directory]:read_reply()
end
ret.before_run = function()
self.current_config_ref = self
local fa = FileApi:new(self.build_directory)
if not fa then
return
end
if not fa:create() then
return
end
self.fileapis[self.build_directory] = fa
return true
end
return ret
end
function VariantConfig:_build_args()
local args = { "--build" }
table.insert(
args,
Path:new(self.build_directory):make_relative(utils.substitude(config.source_directory, self.subs))
)
if #self.buildArgs ~= 0 then
for _, v in ipairs(self.buildArgs) do
table.insert(args, v)
end
elseif #config.build_args ~= 0 then
for _, v in ipairs(config.build_args) do
table.insert(args, v)
end
end
if #self.buildToolArgs ~= 0 or #config.build_tool_args ~= 0 then
table.insert(args, "--")
if #self.buildToolArgs ~= 0 then
for _, v in ipairs(self.buildToolArgs) do
table.insert(args, v)
end
elseif #config.build_tool_args ~= 0 then
for _, v in ipairs(config.build_tool_args) do
table.insert(args, v)
end
end
end
return args
end
function VariantConfig:_build_command()
local ret = {}
ret.cmd = config.cmake_path
ret.args = self.build_args
ret.cwd = variant_subs["${workspaceFolder}"]
ret.env = vim.tbl_deep_extend("keep", self.env, config.configure_environment, config.environment)
return ret
end
local function cartesian_product(sets)
local function collapse_result(res)
local ret = {
short = {},
long = {},
buildType = nil,
linkage = nil,
generator = nil,
buildArgs = {},
buildToolArgs = {},
settings = {},
env = {},
}
local is_default = true
for _, v in ipairs(res) do
if not v.default then
is_default = false
end
ret.short[#ret.short + 1] = v.short
ret.long[#ret.long + 1] = v.long
ret.buildType = v.buildType or ret.buildType
ret.linkage = v.linkage or ret.linkage
ret.generator = v.generator or ret.generator
ret.buildArgs = v.buildArgs or ret.buildArgs
ret.buildToolArgs = v.buildToolArgs or ret.buildToolArgs
for sname, sval in pairs(v.settings or {}) do
ret.settings[sname] = sval
end
for ename, eres in pairs(v.env or {}) do
ret.env[ename] = eres
end
end
ret.display = {}
ret.display.short = table.concat(ret.short, config.variants_display.short_sep)
ret.display.long = table.concat(ret.long, config.variants_display.short_sep)
ret.default = is_default or nil
return ret
end
local result = {}
local set_count = #sets
local function descend(depth)
for k, v in pairs(sets[depth].choices) do
if sets[depth].default ~= k then
result.default = false
end
result[depth] = v
result[depth].default = (k == sets[depth].default)
if depth == set_count then
coroutine.yield(collapse_result(result))
else
descend(depth + 1)
end
end
end
return coroutine.wrap(function()
descend(1)
end)
end
local Project = {}
Project.__index = Project
function Project:from_variants(variants)
local obj =
{ headers = {}, display = { short_len = 10, long_len = 30 }, configs = {}, current_config = nil, fileapis = {} }
for _, v in pairs(variants) do
table.insert(obj.headers, v.description or "")
end
for v in cartesian_product(variants) do
v.fileapis = obj.fileapis
v.current_config_ref = obj.current_config
v = VariantConfig:new(v)
obj.display.short_len = math.max(obj.display.short_len, string.len(v.display.short))
table.insert(obj.configs, v)
if v.default then
obj.current_config = v
end
if not obj.fileapis[v.build_directory] then
local fa = FileApi:new(v.build_directory)
if fa and fa:exists() then
fa:read_reply()
obj.fileapis[v.build_directory] = fa
end
end
end
setmetatable(obj, Project)
return obj
end
function Project:from_presets(presets)
local obj = { { 1, 3, ddf = "" } }
return setmetatable(obj, self)
end
function Project:set_current_config(idx)
self.current_config = self.configs[idx]
end
function Project:set_current_build() end
function Project:configure_command()
return self.current_config.configure_command
end
function Project:current_configure_index()
for k, v in ipairs(self.configs) do
if v == self.current_config then
return k
end
end
return 1
end
function Project:current_build_index()
if not self.current_config then
return 1
end
if getmetatable(self.current_config) == VariantConfig then
return 1
end
end
function Project:configure_display_options()
return self.display_options
end
function Project:build_command()
if not self.current_config then
return
end
if getmetatable(self.current_config) == VariantConfig then
return self.current_config.build_command
end
end
function Project:build_directory()
if not self.current_config then
return
end
return self.current_config.build_directory
end
function Project:list_configs()
return self.configs
end
function Project:list_builds(opts)
if getmetatable(self.current_config) == VariantConfig then
return { self.current_config }
end
end
return Project
|