Skip to content

Commit a06fb54

Browse files
committed
Fix build presets with multiple targets
If a build preset from CMakePresets.json contained multiple targets, BuildPreset:get_build_target would concatenate target names into a single string which would be used as a single command line argument. The build system, such as Ninja, would then complain that the specified target is unknown. This change modifies BuildPreset:get_build_target to return a table of targets instead, which is then unpacked after CMake's --target flag. That way, each target is a separate command line argument and the build system executes a proper multi-target build.
1 parent 1724421 commit a06fb54

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

lua/cmake-tools/build_preset.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ end
1414

1515
function BuildPreset:get_build_target()
1616
if self.targets == nil then
17-
return ""
17+
return nil
1818
end
1919
if type(self.targets) == "string" then
20+
return { self.targets }
21+
elseif type(self.targets) == "table" then
2022
return self.targets
21-
elseif type(self.targets == "table") then
22-
return table.concat(self.targets, " ")
2323
end
24-
return ""
24+
return nil
2525
end
2626

2727
function BuildPreset:get_build_type()

lua/cmake-tools/config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ function Config:update_build_target()
560560
return
561561
end
562562
local build_target = build_preset:get_build_target()
563-
if build_target ~= "" then
563+
if build_target ~= nil then
564564
self.build_target = build_target
565565
end
566566
end

lua/cmake-tools/init.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ function cmake.build(opt, callback)
400400
vim.list_extend(args, { "--target", opt.target })
401401
vim.list_extend(args, fargs)
402402
elseif config.build_target ~= nil then
403-
vim.list_extend(args, { "--target", config.build_target })
403+
vim.list_extend(args, { "--target", unpack(config.build_target) })
404404
vim.list_extend(args, fargs)
405405
end
406406

@@ -947,7 +947,7 @@ function cmake.select_build_target(regenerate, callback)
947947
callback(Result:new_error(Types.NOT_SELECT_BUILD_TARGET, "No target selected: cancelled"))
948948
return
949949
end
950-
config.build_target = targets[idx]
950+
config.build_target = { targets[idx] }
951951
callback(Result:new(Types.SUCCESS, config.build_target, nil))
952952
end)
953953
)

0 commit comments

Comments
 (0)