Skip to content

Commit

Permalink
feat: Add option to not pass --presets to cmake
Browse files Browse the repository at this point in the history
## Changes Description

On CMake projects that use Conan as their package manager, there are two
presets that are created with the same name. This always causes any
`cmake` command issued by CMakeTools to fail. On those scenarios, the
project usually specify the CMake options (including the preset
location) as an argument to the `cmake` command.

This commit adds the option to not pass the `--preset` argument to the
`cmake` command.
  • Loading branch information
Townk committed May 21, 2024
1 parent d84e9ec commit ccacb83
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ local osys = require("cmake-tools.osys")
require("cmake-tools").setup {
cmake_command = "cmake", -- this is used to specify cmake command path
ctest_command = "ctest", -- this is used to specify ctest command path
cmake_use_presets = true,
cmake_regenerate_on_save = true, -- auto generate when save CMakeLists.txt
cmake_generate_options = { "-DCMAKE_EXPORT_COMPILE_COMMANDS=1" }, -- this will be passed when invoke `CMakeGenerate`
cmake_build_options = {}, -- this will be passed when invoke `CMakeBuild`
Expand Down
14 changes: 14 additions & 0 deletions docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ return {
},
build_dir = nil,
working_dir = "${dir.binary}"
use_presets = true,
generate_options = {},
build_options = {},
}
Expand Down Expand Up @@ -110,6 +111,19 @@ Specify the working directory in which run and debug commands are executed. Supp
| `CMakeSettings` | :white_check_mark: | Defines the working directory for all targets unless overwritten. |
| `CMakeTargetSettings` | :white_check_mark: | Sets the working directory just for this target. No default value - Has to be added manually. |

### `use_presets`

Specify if the `--presets` argument should be used on `cmake` commands. This is useful when you want to control all of the `cmake` options with the `generate_options` and the `build_options`.

```lua
use_presets = true
```

| Command | Supported | Details |
|-------------- | -------------- | -------------- |
| `CMakeSettings` | :white_check_mark: | Flag controlling if the `--presets` argument should be passed to the `cmake` commands |
| `CMakeTargetSettings` | :x: | |

### `build_directory`

Specify the build directory in which generate files should locate.
Expand Down
2 changes: 2 additions & 0 deletions lua/cmake-tools/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ local Config = {
env = {},
build_dir = "",
working_dir = "${dir.binary}",
use_presets = true,
generate_options = {},
build_options = {},
}, -- general config
Expand All @@ -39,6 +40,7 @@ function Config:new(const)

self.base_settings.generate_options = const.cmake_generate_options
self.base_settings.build_options = const.cmake_build_options
self.base_settings.use_presets = const.cmake_use_presets

self.executor = const.cmake_executor
self.runner = const.cmake_runner
Expand Down
1 change: 1 addition & 0 deletions lua/cmake-tools/const.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local osys = require("cmake-tools.osys")
local const = {
cmake_command = "cmake", -- this is used to specify cmake command path
ctest_command = "ctest", -- this is used to specify ctest command path
cmake_use_presets = true, -- when `false`, this is used to define if the `--preset` option should be use on cmake commands
cmake_regenerate_on_save = true, -- auto generate when save CMakeLists.txt
cmake_generate_options = { "-DCMAKE_EXPORT_COMPILE_COMMANDS=1" }, -- this will be passed when invoke `CMakeGenerate`
cmake_build_options = {}, -- this will be passed when invoke `CMakeBuild`
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 @@ -83,7 +83,7 @@ function cmake.generate(opt, callback)
-- if exists presets, preset include all info that cmake
-- needed to execute, so we don't use cmake-kits.json and
-- cmake-variants.[json|yaml] event they exist.
local presets_file = presets.check(config.cwd)
local presets_file = config.base_settings.use_preset and presets.check(config.cwd)
if presets_file and not config.configure_preset then
-- this will also set value for build type from preset.
-- default to be "Debug"
Expand Down Expand Up @@ -242,7 +242,7 @@ function cmake.build(opt, callback)
end

local args
local presets_file = presets.check(config.cwd)
local presets_file = config.base_settings.use_preset and presets.check(config.cwd)

if presets_file and config.build_preset then
args = { "--build", "--preset", config.build_preset } -- preset don't need define build dir.
Expand Down Expand Up @@ -1238,7 +1238,7 @@ function cmake.create_regenerate_on_save_autocmd()
table.insert(pattern, ss)
end

local presets_file = presets.check(config.cwd)
local presets_file = config.base_settings.use_preset and presets.check(config.cwd)
if presets_file then
for _, item in ipairs({
"CMakePresets.json",
Expand Down

0 comments on commit ccacb83

Please sign in to comment.