Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions lua/cmake-tools/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function Config:no_expand_build_directory_path()
end

---@param build_dir string|function string or a function returning string containing path to the build dir
---@param no_expand_build_dir string|function
---@param no_expand_build_dir? string|function when nil, the stored no-expand build dir is left untouched
function Config:update_build_dir(build_dir, no_expand_build_dir)
if type(build_dir) == "function" then
build_dir = build_dir()
Expand All @@ -102,7 +102,7 @@ function Config:update_build_dir(build_dir, no_expand_build_dir)
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
if no_expand_build_dir ~= nil and 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"
)
Expand All @@ -123,7 +123,9 @@ function Config:update_build_dir(build_dir, no_expand_build_dir)
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()
if no_expand_build_dir then
self.base_settings.build_dir = Path:new(no_expand_build_dir):absolute()
end
end

---Prepare build directory with macro expansion
Expand Down Expand Up @@ -647,10 +649,11 @@ function Config:update_build_directory()
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)
-- binaryDir still contains preset macros, so keep it out of the stored
-- no-expand build dir, which is only expanded for kits and variants
local build_directory = configure_preset.binaryDirExpanded
if build_directory and build_directory ~= "" then
self:update_build_dir(build_directory)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lua/cmake-tools/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ function cmake.generate(opt, callback)

config:update_build_type()

local build_directory, no_expand_build_directory = preset.binaryDirExpanded, preset.binaryDir
if build_directory ~= "" then
config:update_build_dir(build_directory, no_expand_build_directory)
local build_directory = preset.binaryDirExpanded
if build_directory and build_directory ~= "" then
config:update_build_dir(build_directory)
end
config:generate_build_directory()

Expand Down
35 changes: 35 additions & 0 deletions tests/config_preset_build_dir_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
describe("Config.update_build_directory with presets", function()
local dir

before_each(function()
dir = vim.fn.tempname()
vim.fn.mkdir(dir, "p")
local f = assert(io.open(dir .. "/CMakePresets.json", "w"))
f:write([[{
"version": 3,
"configurePresets": [
{ "name": "base", "hidden": true, "generator": "Ninja",
"binaryDir": "${sourceDir}/build/${presetName}" },
{ "name": "Debug", "inherits": "base" }
]
}]])
f:close()
end)

after_each(function()
vim.fn.delete(dir, "rf")
end)

it("keeps preset macros out of the stored no-expand build dir", function()
local const = require("cmake-tools.const")
local Config = require("cmake-tools.config")
local config = Config:new(const)
config.cwd = dir
config.configure_preset = "Debug"

config:update_build_directory()

assert.is_truthy(config.build_directory.filename:find("build/Debug", 1, true))
assert.is_nil(config:no_expand_build_directory_path():find("${sourceDir}", 1, true))
end)
end)
Loading