Skip to content

Commit dd64e52

Browse files
authored
Merge branch 'Civitasv:master' into master
2 parents 49f717d + bac6ba2 commit dd64e52

23 files changed

Lines changed: 966 additions & 251 deletions

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ local osys = require("cmake-tools.osys")
2828
require("cmake-tools").setup {
2929
cmake_command = "cmake", -- this is used to specify cmake command path
3030
ctest_command = "ctest", -- this is used to specify ctest command path
31+
ctest_show_labels = false, -- also show labels in the test picker
3132
cmake_use_preset = true,
3233
cmake_regenerate_on_save = true, -- auto generate when save CMakeLists.txt
3334
cmake_generate_options = { "-DCMAKE_EXPORT_COMPILE_COMMANDS=1" }, -- this will be passed when invoke `CMakeGenerate`
@@ -48,7 +49,7 @@ require("cmake-tools").setup {
4849
-- copy: this will automatically copy compile commands file to target
4950
-- lsp: this will automatically set compile commands file location using lsp
5051
-- none: this will make this option ignored
51-
target = vim.loop.cwd() -- path to directory, this is used only if action == "soft_link" or action == "copy"
52+
target = vim.loop.cwd, -- path or function returning path to directory, this is used only if action == "soft_link" or action == "copy"
5253
},
5354
cmake_kits_path = nil, -- this is used to specify global cmake kits path, see CMakeKits for detailed usage
5455
cmake_variants_message = {

docs/cmake_kits.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,4 @@ You can also define general global kits in somewhere. To do this, you will have
5151
1. Kits scan is not supported.
5252
2. Option `visualStudio` and `visualStudioArchitecture` is not supported.
5353
3. Option `preferredGenerator` is not supported.
54-
4. Option `cmakeSettings` is not supported.
55-
5. Option `environmentSetupScript` is only supported in the experimental terminal mode.
54+
4. Option `environmentSetupScript` is only supported in the experimental terminal mode.

lua/cmake-tools/build_preset.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
local Path = require("plenary.path")
22
local osys = require("cmake-tools.osys")
33

4+
---@class BuildPreset: CMakeBuildPreset
45
local BuildPreset = {}
56

7+
---@param cwd string
8+
---@param obj CMakeBuildPreset?
9+
---@return BuildPreset
610
function BuildPreset:new(cwd, obj)
711
local instance = setmetatable(obj or {}, { __index = self })
812
instance.__index = self
@@ -16,6 +20,7 @@ function BuildPreset:new(cwd, obj)
1620
return instance
1721
end
1822

23+
---@return string[]|nil
1924
function BuildPreset:get_build_target()
2025
if self.targets == nil then
2126
return nil
@@ -28,13 +33,15 @@ function BuildPreset:get_build_target()
2833
return nil
2934
end
3035

36+
---@return string|nil
3137
function BuildPreset:get_build_type()
3238
if self.configuration == nil then
3339
return nil
3440
end
3541
return self.configuration
3642
end
3743

44+
---@return boolean
3845
function BuildPreset:is_valid()
3946
return self.valid
4047
end

lua/cmake-tools/config.lua

Lines changed: 94 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,57 @@ local variants = require("cmake-tools.variants")
77
local Presets = require("cmake-tools.presets")
88
local kits = require("cmake-tools.kits")
99

10-
local Config = {
11-
build_directory = nil,
12-
query_directory = nil,
13-
reply_directory = nil,
14-
build_type = nil,
15-
variant = nil,
16-
build_target = nil,
17-
launch_target = nil,
18-
kit = nil,
19-
configure_preset = nil,
20-
build_preset = nil,
21-
base_settings = {
22-
env = {},
23-
build_dir = "",
24-
working_dir = "${dir.binary}",
25-
use_preset = true,
26-
generate_options = {},
27-
build_options = {},
28-
show_disabled_build_presets = true,
29-
}, -- general config
30-
target_settings = {}, -- target specific config
31-
executor = nil,
32-
runner = nil,
33-
env_script = " ",
34-
cwd = vim.loop.cwd(),
35-
}
36-
10+
---@class Config.BaseSettings
11+
---@field env table
12+
---@field build_dir string
13+
---@field working_dir string
14+
---@field use_preset boolean
15+
---@field generate_options string[]
16+
---@field build_options string[]
17+
---@field show_disabled_build_presets boolean
18+
---@field ctest_show_labels boolean
19+
20+
---@class Config
21+
---@field build_directory Path?
22+
---@field query_directory Path?
23+
---@field reply_directory Path?
24+
---@field build_type string?
25+
---@field variant table?
26+
---@field build_target string|string[]|nil
27+
---@field launch_target string?
28+
---@field kit string?
29+
---@field configure_preset string?
30+
---@field build_preset string?
31+
---@field test_preset string?
32+
---@field selected_test string?
33+
---@field base_settings Config.BaseSettings
34+
---@field target_settings table
35+
---@field executor table?
36+
---@field runner table?
37+
---@field env_script string
38+
---@field cwd string
39+
local Config = {}
40+
Config.__index = Config
41+
42+
---@param const Const
43+
---@return Config
3744
function Config:new(const)
38-
local obj = {}
39-
setmetatable(obj, { __index = self }) -- when obj cannot find key in its table, it will try to find it from its __index value
45+
local obj = {
46+
base_settings = {
47+
env = {},
48+
build_dir = "",
49+
working_dir = "${dir.binary}",
50+
use_preset = true,
51+
generate_options = {},
52+
build_options = {},
53+
show_disabled_build_presets = true,
54+
ctest_show_labels = false,
55+
}, -- general config
56+
target_settings = {}, -- target specific config
57+
env_script = " ",
58+
cwd = vim.loop.cwd(),
59+
}
60+
setmetatable(obj, Config)
4061

4162
obj:update_build_dir(const.cmake_build_directory, const.cmake_build_directory)
4263

@@ -45,28 +66,30 @@ function Config:new(const)
4566
obj.base_settings.use_preset = const.cmake_use_preset
4667

4768
obj.base_settings.show_disabled_build_presets = const.cmake_show_disabled_build_presets
69+
obj.base_settings.ctest_show_labels = const.ctest_show_labels
4870

4971
obj.executor = const.cmake_executor
5072
obj.runner = const.cmake_runner
5173

5274
return obj
5375
end
5476

77+
---@return string
5578
function Config:build_directory_path()
5679
return self.build_directory.filename
5780
end
5881

82+
---@return boolean
5983
function Config:has_build_directory()
6084
return self.build_directory and self.build_directory:exists()
6185
end
6286

63-
---comment
6487
---The reason for storing no expand build directory is to make cwd selecting easier
88+
---@return string
6589
function Config:no_expand_build_directory_path()
6690
return self.base_settings.build_dir
6791
end
6892

69-
---comment
7093
---@param build_dir string|function string or a function returning string containing path to the build dir
7194
---@param no_expand_build_dir string|function
7295
function Config:update_build_dir(build_dir, no_expand_build_dir)
@@ -103,8 +126,9 @@ function Config:update_build_dir(build_dir, no_expand_build_dir)
103126
self.base_settings.build_dir = Path:new(no_expand_build_dir):absolute()
104127
end
105128

106-
---Prepare build directory. Which allows macro expansion.
129+
---Prepare build directory with macro expansion
107130
---@param kit_list table all the kits
131+
---@return string
108132
function Config:prepare_build_directory(kit_list)
109133
-- macro expansion:
110134
-- ${kit}
@@ -136,18 +160,27 @@ function Config:prepare_build_directory(kit_list)
136160
return build_dir
137161
end
138162

163+
---@return string[]
139164
function Config:generate_options()
140165
return self.base_settings.generate_options and self.base_settings.generate_options or {}
141166
end
142167

168+
---@return string[]
143169
function Config:build_options()
144170
return self.base_settings.build_options and self.base_settings.build_options or {}
145171
end
146172

173+
---@return boolean
147174
function Config:show_disabled_build_presets()
148175
return self.base_settings.show_disabled_build_presets
149176
end
150177

178+
---@return boolean
179+
function Config:ctest_show_labels()
180+
return self.base_settings.ctest_show_labels
181+
end
182+
183+
---@return cmake.Result
151184
function Config:generate_build_directory()
152185
local build_directory = Path:new(self.build_directory)
153186

@@ -157,6 +190,7 @@ function Config:generate_build_directory()
157190
return self:generate_query_files()
158191
end
159192

193+
---@return cmake.Result
160194
function Config:generate_query_files()
161195
local query_directory = Path:new(self.query_directory)
162196
if not query_directory:mkdir({ parents = true }) then
@@ -188,6 +222,7 @@ function Config:generate_query_files()
188222
return Result:new(Types.SUCCESS, true, "yeah, that could be")
189223
end
190224

225+
---@return cmake.Result
191226
function Config:get_cmake_files()
192227
-- if reply_directory exists
193228
local reply_directory = Path:new(self.reply_directory)
@@ -204,6 +239,7 @@ function Config:get_cmake_files()
204239
return Result:new(Types.SUCCESS, codemodel_json["inputs"], "find it")
205240
end
206241

242+
---@return cmake.Result
207243
function Config:get_codemodel_targets()
208244
-- if reply_directory exists
209245
local reply_directory = Path:new(self.reply_directory)
@@ -225,12 +261,15 @@ function Config:get_codemodel_targets()
225261
return Result:new(Types.SUCCESS, codemodel_json["configurations"][1]["targets"], "find it") -- Return the first else
226262
end
227263

264+
---@param codemodel_target table
265+
---@return table
228266
function Config:get_code_model_target_info(codemodel_target)
229267
local reply_directory = Path:new(self.reply_directory)
230268
return vim.json.decode((reply_directory / codemodel_target["jsonFile"]):read())
231269
end
232270

233-
-- Check if launch target is built
271+
---Check if launch target is built
272+
---@return cmake.Result
234273
function Config:check_launch_target()
235274
-- 1. not configured
236275
local build_directory = Path:new(self.build_directory)
@@ -268,6 +307,8 @@ function Config:check_launch_target()
268307
)
269308
end
270309

310+
---@param target_info table
311+
---@return cmake.Result
271312
function Config:get_launch_target_from_info(target_info)
272313
local target_path = target_info["artifacts"][1]["path"]
273314
if require("cmake-tools.osys").iswin32 then
@@ -292,8 +333,8 @@ function Config:get_launch_target_from_info(target_info)
292333
return Result:new(Types.SUCCESS, target_path.filename, "yeah, that's good")
293334
end
294335

295-
-- Retrieve launch target path: self.launch_target
296-
-- it will first check if this launch target is built
336+
---Retrieve launch target path
337+
---@return cmake.Result
297338
function Config:get_launch_target()
298339
local check_result = self:check_launch_target()
299340
if check_result.code ~= Types.SUCCESS then
@@ -304,7 +345,8 @@ function Config:get_launch_target()
304345
return self:get_launch_target_from_info(target_info)
305346
end
306347

307-
-- Check if build target exists
348+
---Check if build target exists
349+
---@return cmake.Result
308350
function Config:check_build_target()
309351
-- 1. not configured
310352
local build_directory = Path:new(self.build_directory)
@@ -338,8 +380,8 @@ function Config:check_build_target()
338380
)
339381
end
340382

341-
-- Retrieve launch target path: self.launch_target
342-
-- it will first check if this launch target is built
383+
---Retrieve build target path
384+
---@return cmake.Result
343385
function Config:get_build_target()
344386
local check_result = self:check_build_target()
345387
if check_result.code ~= Types.SUCCESS then
@@ -366,8 +408,8 @@ function Config:get_build_target()
366408
return Result:new(Types.SUCCESS, target_path.filename, "yeah, that's good")
367409
end
368410

369-
-- Check if this launch target is debuggable
370-
-- use variants.debuggable
411+
---Check if this launch target is debuggable using variants.debuggable
412+
---@return cmake.Result
371413
function Config:validate_for_debugging()
372414
local build_type = self.build_type
373415

@@ -377,6 +419,9 @@ function Config:validate_for_debugging()
377419
return Result:new(Types.SUCCESS, true, "Yeah, it may be")
378420
end
379421

422+
---@param config Config
423+
---@param opt { has_all: boolean, only_executable: boolean, query_sources: boolean? }
424+
---@return cmake.Result
380425
local function get_targets(config, opt)
381426
local targets, display_targets, paths, abs_paths = {}, {}, {}, {}
382427
local sources = {}
@@ -447,6 +492,7 @@ local function get_targets(config, opt)
447492
end
448493
end
449494

495+
---@return table
450496
function Config:get_code_model_info()
451497
local codemodel_targets = self:get_codemodel_targets()
452498
if codemodel_targets.code ~= Types.SUCCESS then
@@ -464,31 +510,37 @@ function Config:get_code_model_info()
464510
return result
465511
end
466512

513+
---@return cmake.Result
467514
function Config:launch_targets()
468515
return get_targets(self, { has_all = false, only_executable = true })
469516
end
470517

518+
---@return cmake.Result
471519
function Config:build_targets()
472520
return get_targets(self, { has_all = true, only_executable = false })
473521
end
474522

523+
---@return cmake.Result
475524
function Config:launch_targets_with_sources()
476525
return get_targets(self, { has_all = false, only_executable = true, query_sources = true })
477526
end
478527

479528
local _virtual_targets = nil
529+
480530
function Config:update_targets()
481531
_virtual_targets =
482532
get_targets(self, { has_all = false, only_executable = false, query_sources = true })
483533
end
484534

535+
---@return cmake.Result?
485536
function Config:build_targets_with_sources()
486537
if not _virtual_targets then
487538
self:update_targets()
488539
end
489540
return _virtual_targets
490541
end
491542

543+
---Update build type from configure and build presets
492544
function Config:update_build_type()
493545
local presets_exists = self.base_settings.use_preset and Presets.exists(self.cwd)
494546
if not presets_exists then
@@ -538,6 +590,7 @@ function Config:update_build_type()
538590
end
539591
end
540592

593+
---Update build target from build preset
541594
function Config:update_build_target()
542595
local presets_exists = self.base_settings.use_preset and Presets.exists(self.cwd)
543596
if not presets_exists then
@@ -570,6 +623,7 @@ function Config:update_build_target()
570623
end
571624
end
572625

626+
---Update build directory from kits or configure preset
573627
function Config:update_build_directory()
574628
local kits_config = kits.parse(self.cmake_kits_path, self.cwd)
575629
if kits_config then

0 commit comments

Comments
 (0)