-
Notifications
You must be signed in to change notification settings - Fork 86
Better handling of build preset targets #295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
if opt.target == nil and config.build_target == nil then | ||
local presets = Presets:parse(config.cwd) | ||
local build_preset = presets:get_build_preset(config.build_preset) | ||
if (not config.build_preset or config.build_preset and not build_preset.targets) and opt.target == nil and config.build_target == nil then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
build_preset could be nil. You will run into an index into nill error on build_preset.targets
in this case. Maybe check for if (not build_preset or not build_preset.targets) ...
?
if not build_preset or build_preset and not build_preset.targets then | ||
if opt.target ~= nil then | ||
vim.list_extend(args, { "--target", opt.target }) | ||
vim.list_extend(args, fargs) | ||
elseif config.build_target == "all" then | ||
vim.list_extend(args, { "--target", "all" }) | ||
vim.list_extend(args, fargs) | ||
else | ||
vim.list_extend(args, { "--target", config.build_target }) | ||
vim.list_extend(args, fargs) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want the build_preset.targets to take precedence, you can check for if build_preset and build_presets.targets
first and have elseif opt.target
second, right?
end | ||
|
||
if config.build_type ~= nil then | ||
vim.list_extend(args, { "--config", config.build_type }) | ||
if not build_preset then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this additional if?
end | ||
end | ||
else -- build preset does not have a target, ask | ||
config.build_target = nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if the build_preset does not have a target, the currently selected one should be kept.
else | ||
-- XXX: Array targets are not supported in other code paths | ||
-- As a workaround, unset the current build_target if any (it is not used when using build presets) | ||
config.build_target = nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as we do not have a handling for multiple build targets currently, you could either keep the currently selected one (if it is part of the array) or always select the first one from the list. The user can still change the target, if needed.
Resetting the selected target is not desirable, I think
I think a refactoring to support multiple targets might be helpful. But this is beyond the scope of this MR for sure.
I appreciate the PR and think it is a step in the right direction for sure. Very nice write-up of the motivation and your approach! |
In the current implementation, the
targets
field ofCMakePresets.json
is ignored. This leads to inconsistencies between whatcmake-tools.nvim
does and displays and what the user expects to happen.Here is an example
buildPresets
showcasing the 4 possible cases:One expects that :
:CMakeSelectBuildPreset
the internalbuild_target
to be updated. This ensures that callingget_build_target()
returns the target specified in the preset (for lualine for instance).:CMakeBuild
the command executed to becmake --preset <preset_name>
without additional--target
argumentsThe current behaviour priour to this PR is to:
targets
field of theCMakePresets.json
cmake --preset <preset_name> --target <whatever the previously specified target was>
For a more concrete use-case, consider embeded development with a
build-preset
preset with abuild-target
target and and aflash-preset
preset with aflash-target
target that flashes the program onto the device.Currently, the following would happen:
:CMakeSelectBuildPreset build-preset
build-preset
is indeed selectedbuild_target
: none => we expectedbuild-target
instead:CMakeBuild
will prompt for a target, we selectbuild-target
, andcmake --preset build-preset --target build-target
is run:CMakeSelectBuildPreset flash-preset
flash-preset
is indeed selectedbuild_target
:build-target
=> we expectedflash-target
:CMakeBuild
runscmake --preset flash-preset --target build-target
=> we expected to run theflash-target
You get the gist. This PR fixes this behaviour in the following way:
build_target
is changed to what's specified in that preset with one caveat: multiple targetstargets: ["target1", "target2"]
. Since this case is not handled anywhere else in the code, I simply put thebuild_target = nil
cmake --preset <selected_preset>
if at least one target is specified in the preset, otherwise it iscmake --preset <selected_preset> --target <user selected target>
It would probably be better to have a proper way of handle the multiple-targets case. However this seemed too big of a change, for little benefit for me to tackle right now.