Skip to content

Commit feb4d1c

Browse files
committed
target filename #244
1 parent ae526f1 commit feb4d1c

4 files changed

Lines changed: 112 additions & 7 deletions

File tree

lua/cmake-tools/build_preset.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,11 @@ function BuildPreset:get_build_target()
2424
return ""
2525
end
2626

27+
function BuildPreset:get_build_type()
28+
if self.configuration == nil then
29+
return nil
30+
end
31+
return self.configuration
32+
end
33+
2734
return BuildPreset

lua/cmake-tools/init.lua

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,11 @@ function cmake.generate(opt, callback)
116116
local presets = Presets:parse(config.cwd)
117117

118118
local find_preset = false
119-
-- Refresh build type to use from CMakePresets
120119
if config.configure_preset then
121120
local configure_preset =
122121
presets:get_configure_preset(config.configure_preset, { include_hidden = true })
123122
if configure_preset then
124123
find_preset = true
125-
config.build_type = configure_preset:get_build_type()
126124
end
127125
end
128126

@@ -167,6 +165,9 @@ function cmake.generate(opt, callback)
167165
end
168166
return
169167
end
168+
169+
cmake.update_build_type()
170+
170171
local build_directory, no_expand_build_directory = preset.binaryDirExpanded, preset.binaryDir
171172
if build_directory ~= "" then
172173
config:update_build_dir(build_directory, no_expand_build_directory)
@@ -367,10 +368,8 @@ function cmake.build(opt, callback)
367368
if config.build_preset then
368369
local build_preset = presets:get_build_preset(config.build_preset)
369370
if build_preset then
370-
local build_target_from_build_preset = build_preset:get_build_target()
371-
if build_target_from_build_preset ~= "" then
372-
config.build_target = build_target_from_build_preset
373-
end
371+
cmake.update_build_target(build_preset)
372+
cmake.update_build_type()
374373
end
375374
end
376375
end
@@ -878,7 +877,11 @@ function cmake.select_build_preset(callback)
878877
end
879878
if config.build_preset ~= choice then
880879
config.build_preset = choice
881-
config.build_target = presets:get_build_preset(choice):get_build_target()
880+
881+
local build_preset = presets:get_build_preset(choice)
882+
if build_preset then
883+
cmake.update_build_target(build_preset)
884+
end
882885
end
883886
local associated_configure_preset = presets:get_configure_preset(
884887
presets:get_build_preset(choice).configurePreset,
@@ -1865,4 +1868,56 @@ function cmake.register_telescope_function()
18651868
end
18661869
end
18671870

1871+
function cmake.update_build_target(build_preset)
1872+
local build_target = build_preset:get_build_target()
1873+
if build_target ~= "" then
1874+
config.build_target = build_target
1875+
end
1876+
end
1877+
1878+
function cmake.update_build_type()
1879+
local presets = Presets:parse(config.cwd)
1880+
if not presets then
1881+
return
1882+
end
1883+
if not config.configure_preset then
1884+
return
1885+
end
1886+
local configure_preset =
1887+
presets:get_configure_preset(config.configure_preset, { include_hidden = true })
1888+
if not configure_preset then
1889+
return
1890+
end
1891+
1892+
config.build_type = configure_preset:get_build_type()
1893+
1894+
if not config.build_preset then
1895+
return
1896+
end
1897+
local build_preset = presets:get_build_preset(config.build_preset)
1898+
if not build_preset then
1899+
return
1900+
end
1901+
local configuration_types = configure_preset:get_build_configuration_types()
1902+
1903+
if not configuration_types then
1904+
return
1905+
end
1906+
local build_type_from_build_preset = build_preset:get_build_type()
1907+
1908+
if not build_type_from_build_preset then
1909+
return
1910+
end
1911+
local exists = false
1912+
for _, Item in ipairs(configuration_types) do
1913+
if Item == build_type_from_build_preset then
1914+
exists = true
1915+
break
1916+
end
1917+
end
1918+
if exists then
1919+
config.build_type = build_type_from_build_preset
1920+
end
1921+
end
1922+
18681923
return cmake

lua/cmake-tools/preset.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local Path = require("plenary.path")
22
local osys = require("cmake-tools.osys")
3+
local utils = require("cmake-tools.utils")
34

45
local Preset = {}
56

@@ -274,4 +275,27 @@ function Preset:get_build_type()
274275
return self.cacheVariables and self.cacheVariables.CMAKE_BUILD_TYPE or "Debug"
275276
end
276277

278+
function Preset:get_build_configuration_types()
279+
local generator = self.generator
280+
local multi_configuration_generator = { "Visual Studio", "Xcode", "Ninja Multi-Config" }
281+
local support_multi_configuration = false
282+
for i, val in ipairs(multi_configuration_generator) do
283+
if val == generator then
284+
support_multi_configuration = true
285+
end
286+
end
287+
if not support_multi_configuration then
288+
return nil
289+
end
290+
if self.cacheVariables == nil then
291+
return nil
292+
end
293+
if self.cacheVariables.CMAKE_CONFIGURATION_TYPES == nil then
294+
return nil
295+
end
296+
local configuration_types = self.cacheVariables.CMAKE_CONFIGURATION_TYPES
297+
298+
return utils.split_string_by_delimiter(configuration_types, ";")
299+
end
300+
277301
return Preset

lua/cmake-tools/utils.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,23 @@ function utils.get_nested(tbl, ...)
297297
return value
298298
end
299299

300+
function utils.split_string_by_delimiter(s, delimiter, item)
301+
local answer = {}
302+
local delimiter1 = delimiter or ";"
303+
for x in string.gmatch(s, "(.-)" .. delimiter1) do
304+
table.insert(answer, x)
305+
end
306+
if #answer == 0 then
307+
return nil
308+
elseif item then -- does the user want an entry verses a parsed table?
309+
if #answer >= item then
310+
return answer[item]
311+
else
312+
return nil
313+
end
314+
else
315+
return answer
316+
end
317+
end
318+
300319
return utils

0 commit comments

Comments
 (0)