Skip to content

Commit 42ff20e

Browse files
committed
fix: consider build presets
1 parent 3637e4c commit 42ff20e

4 files changed

Lines changed: 71 additions & 8 deletions

File tree

lua/cmake-tools/build_preset.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
local Path = require("plenary.path")
2+
local osys = require("cmake-tools.osys")
3+
4+
local BuildPreset = {}
5+
6+
function BuildPreset:new(cwd, obj)
7+
local instance = setmetatable(obj or {}, { __index = self })
8+
instance.__index = self
9+
instance.environment = instance.environment or {}
10+
instance.cwd = cwd
11+
12+
return instance
13+
end
14+
15+
function BuildPreset:get_build_target()
16+
if type(self.targets) == "string" then
17+
return self.targets
18+
elseif type(self.targets == "table") then
19+
return table.concat(self.targets, " ")
20+
end
21+
return ""
22+
end
23+
24+
return BuildPreset

lua/cmake-tools/init.lua

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ function cmake.generate(opt, callback)
115115
if presets_exists then
116116
local presets = Presets:parse(config.cwd)
117117

118+
-- Refresh build type to use from CMakePresets
119+
if config.configure_preset then
120+
config.build_type = presets:get_configure_preset(config.configure_preset):get_build_type()
121+
end
122+
118123
if not config.configure_preset then
119124
-- try to determine the confiure preset based on the build preset
120125
if config.build_preset then
@@ -331,7 +336,34 @@ function cmake.build(opt, callback)
331336
end)
332337
end
333338

334-
if opt.target == nil and config.build_target == nil then
339+
local presets_exists = config.base_settings.use_preset and Presets.exists(config.cwd)
340+
if presets_exists then
341+
local presets = Presets:parse(config.cwd)
342+
if not config.build_preset then
343+
return cmake.select_build_preset(function(result)
344+
if not result:is_ok() then
345+
callback(result)
346+
return
347+
end
348+
cmake.generate({ bang = false, fargs = {} }, function(generate_result)
349+
if not generate_result:is_ok() then
350+
callback(generate_result)
351+
return
352+
end
353+
cmake.build(opt, callback)
354+
end)
355+
end)
356+
end
357+
if config.build_preset then
358+
local build_preset = presets:get_build_preset(config.build_preset)
359+
if build_preset then
360+
config.build_target = build_preset:get_build_target()
361+
end
362+
end
363+
end
364+
365+
-- If presets exists, use build target from it
366+
if presets_exists == false and opt.target == nil and config.build_target == nil then
335367
return cmake.select_build_target(true, function(result)
336368
if result:is_ok() then
337369
cmake.build(opt, callback)
@@ -342,7 +374,6 @@ function cmake.build(opt, callback)
342374
end
343375

344376
local args
345-
local presets_exists = config.base_settings.use_preset and Presets.exists(config.cwd)
346377

347378
if presets_exists and config.build_preset then
348379
args = { "--build", "--preset", config.build_preset } -- preset don't need define build dir.
@@ -356,12 +387,9 @@ function cmake.build(opt, callback)
356387
if opt.target ~= nil then
357388
vim.list_extend(args, { "--target", opt.target })
358389
vim.list_extend(args, fargs)
359-
elseif config.build_target == "all" then
390+
elseif config.build_target ~= nil then
360391
vim.list_extend(args, { "--target", "all" })
361392
vim.list_extend(args, fargs)
362-
else
363-
vim.list_extend(args, { "--target", config.build_target })
364-
vim.list_extend(args, fargs)
365393
end
366394

367395
if config.build_type ~= nil then
@@ -837,6 +865,7 @@ function cmake.select_build_preset(callback)
837865
end
838866
if config.build_preset ~= choice then
839867
config.build_preset = choice
868+
config.build_target = presets:get_build_preset(choice):get_build_target()
840869
end
841870
local associated_configure_preset = presets:get_configure_preset(
842871
presets:get_build_preset(choice).configurePreset,

lua/cmake-tools/preset.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ local function parseTree(self, get_preset)
255255
end
256256

257257
function Preset:new(cwd, obj, get_preset)
258-
local instance = setmetatable(obj or {}, self)
258+
local instance = setmetatable(obj or {}, { __index = self })
259259
instance.__index = self
260260
instance.environment = instance.environment or {}
261261
instance.cwd = cwd

lua/cmake-tools/presets.lua

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local Path = require("plenary.path")
22
local Preset = require("cmake-tools.preset")
3+
local BuildPreset = require("cmake-tools.build_preset")
34

45
-- Extends (or creates a new) key-value pair in [dest] in which the
56
-- key is [key] and the value is the resulting list table of merging
@@ -105,6 +106,7 @@ function Presets:parse(cwd)
105106
end
106107
end
107108

109+
-- Instance extends self
108110
local instance = setmetatable(data, self)
109111
self.__index = self
110112

@@ -115,10 +117,18 @@ function Presets:parse(cwd)
115117
return Preset:new(cwd, obj, getPreset)
116118
end
117119

120+
local function createBuildPreset(obj)
121+
return BuildPreset:new(cwd, obj)
122+
end
123+
118124
for _, preset in ipairs(instance.configurePresets) do
119125
preset = createPreset(preset)
120126
end
121127

128+
for _, build_preset in ipairs(instance.buildPresets) do
129+
build_preset = createBuildPreset(build_preset)
130+
end
131+
122132
return instance
123133
end
124134

@@ -191,7 +201,7 @@ function Presets:get_configure_preset(name, opts)
191201
end
192202

193203
function Presets:get_build_preset(name, opts)
194-
return get_preset(name, self.buildPresets, opts)
204+
return get_preset(name, self.buildPresets, { include_hidden = true, include_disabled = true })
195205
end
196206

197207
function Presets.find_preset_files(cwd)

0 commit comments

Comments
 (0)