-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathconfig.lua
More file actions
607 lines (533 loc) · 18.7 KB
/
config.lua
File metadata and controls
607 lines (533 loc) · 18.7 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
local Path = require("plenary.path")
local scandir = require("plenary.scandir")
local Result = require("cmake-tools.result")
-- local utils = require("cmake-tools.utils") -- Fails lua check. Uncomment this for testing
local Types = require("cmake-tools.types")
local variants = require("cmake-tools.variants")
local Presets = require("cmake-tools.presets")
local kits = require("cmake-tools.kits")
local Config = {
build_directory = nil,
query_directory = nil,
reply_directory = nil,
build_type = nil,
variant = nil,
build_target = nil,
launch_target = nil,
kit = nil,
configure_preset = nil,
build_preset = nil,
base_settings = {
env = {},
build_dir = "",
working_dir = "${dir.binary}",
use_preset = true,
generate_options = {},
build_options = {},
show_disabled_build_presets = true,
ctest_extra_args = {},
}, -- general config
target_settings = {}, -- target specific config
executor = nil,
runner = nil,
env_script = " ",
cwd = vim.loop.cwd(),
}
function Config:new(const)
local obj = {}
setmetatable(obj, { __index = self }) -- when obj cannot find key in its table, it will try to find it from its __index value
obj:update_build_dir(const.cmake_build_directory, const.cmake_build_directory)
obj.base_settings.generate_options = const.cmake_generate_options
obj.base_settings.build_options = const.cmake_build_options
obj.base_settings.use_preset = const.cmake_use_preset
obj.base_settings.show_disabled_build_presets = const.cmake_show_disabled_build_presets
obj.executor = const.cmake_executor
obj.runner = const.cmake_runner
for _, v in pairs(const.ctest_extra_args) do
table.insert(obj.base_settings.ctest_extra_args, v)
end
return obj
end
function Config:build_directory_path()
return self.build_directory.filename
end
function Config:has_build_directory()
return self.build_directory and self.build_directory:exists()
end
---comment
---The reason for storing no expand build directory is to make cwd selecting easier
function Config:no_expand_build_directory_path()
return self.base_settings.build_dir
end
---comment
---@param build_dir string|function string or a function returning string containing path to the build dir
---@param no_expand_build_dir string|function
function Config:update_build_dir(build_dir, no_expand_build_dir)
if type(build_dir) == "function" then
build_dir = build_dir()
end
if type(build_dir) ~= "string" then
error("build_dir needs to be a string or function returning string path to the build_directory")
end
if type(no_expand_build_dir) == "function" then
no_expand_build_dir = no_expand_build_dir()
end
if type(no_expand_build_dir) ~= "string" then
error(
"no_expand_build_dir needs to be a string or function returning string path to the build_directory"
)
end
if require("cmake-tools.osys").iswin32 then
build_dir = build_dir:gsub("/", "\\")
end
local build_path = Path:new(build_dir)
if build_path:is_absolute() then
self.build_directory = Path:new(build_dir)
self.query_directory = Path:new(build_dir, ".cmake", "api", "v1", "query")
self.reply_directory = Path:new(build_dir, ".cmake", "api", "v1", "reply")
else
self.build_directory = Path:new(self.cwd, build_dir)
self.query_directory = Path:new(self.cwd, build_dir, ".cmake", "api", "v1", "query")
self.reply_directory = Path:new(self.cwd, build_dir, ".cmake", "api", "v1", "reply")
end
self.base_settings.build_dir = Path:new(no_expand_build_dir):absolute()
end
---Prepare build directory. Which allows macro expansion.
---@param kit_list table all the kits
function Config:prepare_build_directory(kit_list)
-- macro expansion:
-- ${kit}
-- ${kitGenerator}
-- ${variant:xx}
-- get the detailed info of the selected kit
local build_dir = self:no_expand_build_directory_path()
local kit = self.kit
local variant = self.variant
local kit_info = nil
if kit_list then
for _, item in ipairs(kit_list) do
if item.name == kit then
kit_info = item
end
end
end
build_dir = build_dir:gsub("${kit}", kit_info and kit_info.name or "")
build_dir = build_dir:gsub("${kitGenerator}", kit_info and kit_info.generator or "")
build_dir = build_dir:gsub("${variant:(%w+)}", function(v)
if variant and variant[v] then
return variant[v]
end
return ""
end)
return build_dir
end
function Config:generate_options()
return self.base_settings.generate_options and self.base_settings.generate_options or {}
end
function Config:build_options()
return self.base_settings.build_options and self.base_settings.build_options or {}
end
function Config:show_disabled_build_presets()
return self.base_settings.show_disabled_build_presets
end
function Config:generate_build_directory()
local build_directory = Path:new(self.build_directory)
if not build_directory:mkdir({ parents = true }) then
return Result:new(Types.CANNOT_CREATE_DIRECTORY, false, "cannot create directory")
end
return self:generate_query_files()
end
function Config:generate_query_files()
local query_directory = Path:new(self.query_directory)
if not query_directory:mkdir({ parents = true }) then
return Result:new(Types.CANNOT_CREATE_DIRECTORY, false, "cannot create directory")
end
local codemodel_file = query_directory / "codemodel-v2"
if not codemodel_file:is_file() then
if not codemodel_file:touch() then
return Result:new(
Types.CANNOT_CREATE_CODEMODEL_QUERY_FILE,
nil,
"Unable to create file " .. codemodel_file.filename
)
end
end
local cmakeFiles_file = query_directory / "cmakeFiles-v1"
if not cmakeFiles_file:is_file() then
if not cmakeFiles_file:touch() then
return Result:new(
Types.CANNOT_CREATE_CODEMODEL_QUERY_FILE,
nil,
"Unable to create file " .. cmakeFiles_file.filename
)
end
end
return Result:new(Types.SUCCESS, true, "yeah, that could be")
end
function Config:get_cmake_files()
-- if reply_directory exists
local reply_directory = Path:new(self.reply_directory)
if not reply_directory:exists() then
return Result:new(Types.NOT_CONFIGURED, nil, "Configure fail")
end
local found_files = scandir.scan_dir(reply_directory.filename, { search_pattern = "cmakeFiles*" })
if #found_files == 0 then
return Result:new(Types.CANNOT_FIND_CODEMODEL_FILE, nil, "Unable to find codemodel file")
end
local codemodel = Path:new(found_files[1])
local codemodel_json = vim.json.decode(codemodel:read())
return Result:new(Types.SUCCESS, codemodel_json["inputs"], "find it")
end
function Config:get_codemodel_targets()
-- if reply_directory exists
local reply_directory = Path:new(self.reply_directory)
if not reply_directory:exists() then
return Result:new(Types.NOT_CONFIGURED, nil, "Configure fail")
end
local found_files = scandir.scan_dir(reply_directory.filename, { search_pattern = "codemodel*" })
if #found_files == 0 then
return Result:new(Types.CANNOT_FIND_CODEMODEL_FILE, nil, "Unable to find codemodel file")
end
local codemodel = Path:new(found_files[1])
local codemodel_json = vim.json.decode(codemodel:read())
for _, config in ipairs(codemodel_json["configurations"]) do
if config["name"] == self.build_type then
return Result:new(Types.SUCCESS, config["targets"], "find it")
end
end
return Result:new(Types.SUCCESS, codemodel_json["configurations"][1]["targets"], "find it") -- Return the first else
end
function Config:get_code_model_target_info(codemodel_target)
local reply_directory = Path:new(self.reply_directory)
return vim.json.decode((reply_directory / codemodel_target["jsonFile"]):read())
end
-- Check if launch target is built
function Config:check_launch_target()
-- 1. not configured
local build_directory = Path:new(self.build_directory)
if not build_directory:exists() then
return Result:new(Types.NOT_CONFIGURED, nil, "You need to configure it first")
end
-- 2. not select launch target yet
if not self.launch_target then
return Result:new(Types.NOT_SELECT_LAUNCH_TARGET, nil, "You need to select launch target first")
end
local codemodel_targets = self:get_codemodel_targets()
if codemodel_targets.code ~= Types.SUCCESS then
return codemodel_targets
end
codemodel_targets = codemodel_targets.data
for _, target in ipairs(codemodel_targets) do
if self.launch_target == target["name"] then
local target_info = self:get_code_model_target_info(target)
local type = target_info["type"]:lower():gsub("_", " ")
if type ~= "executable" then
-- 3. selected target cannot execute
return Result:new(Types.NOT_EXECUTABLE, nil, "You need to select a executable target")
end
return Result:new(Types.SUCCESS, target_info, "Success")
end
end
return Result:new(
Types.NOT_A_LAUNCH_TARGET,
nil,
"Unable to find the following target: " .. self.launch_target
)
end
function Config:get_launch_target_from_info(target_info)
local target_path = target_info["artifacts"][1]["path"]
if require("cmake-tools.osys").iswin32 then
target_path = target_path:gsub("/", "\\")
end
target_path = Path:new(target_path)
if not target_path:is_absolute() then
-- then it is a relative path, based on build directory
local build_directory = Path:new(self.build_directory)
target_path = build_directory / target_path
end
-- else it is an absolute path
if not target_path:is_file() then
return Result:new(
Types.SELECTED_LAUNCH_TARGET_NOT_BUILT,
target_path.filename,
"Selected target is not built: " .. target_path.filename
)
end
return Result:new(Types.SUCCESS, target_path.filename, "yeah, that's good")
end
-- Retrieve launch target path: self.launch_target
-- it will first check if this launch target is built
function Config:get_launch_target()
local check_result = self:check_launch_target()
if check_result.code ~= Types.SUCCESS then
return check_result
end
local target_info = check_result.data
return self:get_launch_target_from_info(target_info)
end
-- Check if build target exists
function Config:check_build_target()
-- 1. not configured
local build_directory = Path:new(self.build_directory)
if not build_directory:exists() then
return Result:new(Types.NOT_CONFIGURED, nil, "You need to configure it first")
end
-- 2. not select build target yet
if not self.build_target then
return Result:new(Types.NOT_SELECT_BUILD_TARGET, nil, "You need to select Build target first")
end
local codemodel_targets = self:get_codemodel_targets()
if codemodel_targets.code ~= Types.SUCCESS then
return codemodel_targets
end
codemodel_targets = codemodel_targets.data
for _, target in ipairs(codemodel_targets) do
if self.build_target == target["name"] then
local target_info = self:get_code_model_target_info(target)
-- local type = target_info["type"]:lower():gsub("_", " ")
return Result:new(Types.SUCCESS, target_info, "Success")
end
end
return Result:new(
Types.NOT_A_BUILD_TARGET,
nil,
"Unable to find the following target: " .. self.build_target
)
end
-- Retrieve launch target path: self.launch_target
-- it will first check if this launch target is built
function Config:get_build_target()
local check_result = self:check_build_target()
if check_result.code ~= Types.SUCCESS then
return check_result
end
local target_info = check_result.data
local target_path = target_info["artifacts"][1]["path"]
target_path = Path:new(target_path)
if not target_path:is_absolute() then
-- then it is a relative path, based on build directory
local build_directory = Path:new(self.build_directory)
target_path = build_directory / target_path
end
-- else it is an absolute path
if not target_path:is_file() then
return Result:new(
Types.SELECTED_LAUNCH_TARGET_NOT_BUILT,
nil,
"Selected target is not built: " .. target_path.filename
)
end
return Result:new(Types.SUCCESS, target_path.filename, "yeah, that's good")
end
-- Check if this launch target is debuggable
-- use variants.debuggable
function Config:validate_for_debugging()
local build_type = self.build_type
if not build_type or not variants.debuggable(build_type, self.cwd) then
return Result:new(Types.CANNOT_DEBUG_LAUNCH_TARGET, false, "cannot debug it")
end
return Result:new(Types.SUCCESS, true, "Yeah, it may be")
end
local function get_targets(config, opt)
local targets, display_targets, paths, abs_paths = {}, {}, {}, {}
local sources = {}
if opt.has_all then
table.insert(targets, "all")
table.insert(display_targets, "all")
end
local codemodel_targets = config:get_codemodel_targets()
if codemodel_targets.code ~= Types.SUCCESS then
return codemodel_targets
end
codemodel_targets = codemodel_targets.data
for _, target in ipairs(codemodel_targets) do
local target_info = config:get_code_model_target_info(target)
local target_name = target_info["name"]
local target_name_on_disk = target_info["nameOnDisk"]
if target_name:find("_autogen") == nil then
local type = target_info["type"]:lower():gsub("_", " ")
local display_name = target_name .. " (" .. type .. ")"
local path = target_info["paths"]["build"]
if target_name_on_disk ~= nil then -- only executables have name on disk?
path = path .. "/" .. target_name_on_disk
end
local abs_path = ""
if type == "executable" then
abs_path = config.build_directory .. "/" .. target_info["artifacts"][1]["path"]
end
if not (opt.only_executable and (type ~= "executable")) then
if target_name == config.build_target then
table.insert(targets, 1, target_name)
table.insert(display_targets, 1, display_name)
table.insert(paths, 1, path)
table.insert(abs_paths, 1, abs_path)
else
table.insert(targets, target_name)
table.insert(display_targets, display_name)
table.insert(paths, path)
table.insert(abs_paths, abs_path)
end
end
if opt.query_sources then -- get all source files related to this target
for _, source in ipairs(target_info["sources"]) do
local source_abs_path = config.cwd .. "/" .. source["path"]
table.insert(
sources,
{ path = source_abs_path, type = type, name = target_name, display_name = display_name }
)
end
end
end
end
if opt.query_sources then
return Result:new(Types.SUCCESS, {
targets = targets,
display_targets = display_targets,
paths = paths,
abs_paths = abs_paths,
sources = sources,
}, "Success!")
else
return Result:new(
Types.SUCCESS,
{ targets = targets, display_targets = display_targets, paths = paths, abs_paths = abs_paths },
"Success!"
)
end
end
function Config:get_code_model_info()
local codemodel_targets = self:get_codemodel_targets()
if codemodel_targets.code ~= Types.SUCCESS then
return codemodel_targets
end
local result = {}
codemodel_targets = codemodel_targets.data
for _, target in ipairs(codemodel_targets) do
local target_info = self:get_code_model_target_info(target)
local name = target_info["name"]
result[name] = target_info
end
return result
end
function Config:launch_targets()
return get_targets(self, { has_all = false, only_executable = true })
end
function Config:build_targets()
return get_targets(self, { has_all = true, only_executable = false })
end
function Config:launch_targets_with_sources()
return get_targets(self, { has_all = false, only_executable = true, query_sources = true })
end
local _virtual_targets = nil
function Config:update_targets()
_virtual_targets =
get_targets(self, { has_all = false, only_executable = false, query_sources = true })
end
function Config:build_targets_with_sources()
if not _virtual_targets then
self:update_targets()
end
return _virtual_targets
end
function Config:update_build_type()
local presets_exists = self.base_settings.use_preset and Presets.exists(self.cwd)
if not presets_exists then
return
end
local presets = Presets:parse(self.cwd)
if not presets then
return
end
if not self.configure_preset then
return
end
local configure_preset =
presets:get_configure_preset(self.configure_preset, { include_hidden = true })
if not configure_preset then
return
end
self.build_type = configure_preset:get_build_type()
if not self.build_preset then
return
end
local build_preset = presets:get_build_preset(self.build_preset)
if not build_preset then
return
end
local configuration_types = configure_preset:get_build_configuration_types()
if not configuration_types then
return
end
local build_type_from_build_preset = build_preset:get_build_type()
if not build_type_from_build_preset then
return
end
local exists = false
for _, Item in ipairs(configuration_types) do
if Item == build_type_from_build_preset then
exists = true
break
end
end
if exists then
self.build_type = build_type_from_build_preset
end
end
function Config:update_build_target()
local presets_exists = self.base_settings.use_preset and Presets.exists(self.cwd)
if not presets_exists then
return
end
local presets = Presets:parse(self.cwd)
if not presets then
return
end
if not self.configure_preset then
return
end
local configure_preset =
presets:get_configure_preset(self.configure_preset, { include_hidden = true })
if not configure_preset then
return
end
if not self.build_preset then
return
end
local build_preset = presets:get_build_preset(self.build_preset)
if not build_preset then
return
end
local build_target = build_preset:get_build_target()
if build_target ~= nil then
self.build_target = build_target
end
end
function Config:update_build_directory()
local kits_config = kits.parse(self.cmake_kits_path, self.cwd)
if kits_config then
local build_dir = self:prepare_build_directory(kits_config)
self:update_build_dir(build_dir, self:no_expand_build_directory_path())
return
end
local presets_exists = self.base_settings.use_preset and Presets.exists(self.cwd)
if not presets_exists then
return
end
local presets = Presets:parse(self.cwd)
if presets then
if not self.configure_preset then
return
end
local configure_preset =
presets:get_configure_preset(self.configure_preset, { include_hidden = true })
if not configure_preset then
return
end
local build_directory, no_expand_build_directory =
configure_preset.binaryDirExpanded, configure_preset.binaryDir
if build_directory ~= "" then
self:update_build_dir(build_directory, no_expand_build_directory)
end
end
end
return Config