cmake-tools.nvim icon indicating copy to clipboard operation
cmake-tools.nvim copied to clipboard

target filename

Open jeanpaulrichter opened this issue 1 year ago • 4 comments

Hi, this is more of a question to be honest: I need the full path of a built executable. Eventually I came up with this:

local target cmake.get_launch_target()
local filename = cmake.get_model_info()[target].nameOnDisk
local path = cmake.get_launch_path(target) .. filename

which works fine, but I was wondering if there is a simpler/preferred way of achieving this without the kind of undocumented get_model_info()? If not, maybe a cmake.get_launch_file or something would be worth considering.

EDIT: another thing: get_launch_path() always returns the same path even if the current build preset changes the configuration. i.e:

"configurePresets": [
{
...
"generator": "Ninja Multi-Config",
"cacheVariables": {
   "CMAKE_CONFIGURATION_TYPES": "Release;RelWithDebInfo;Debug"
}
...
}],
"buildPresets": [
  {
   ...
   configuration: "Debug",
  },
  {
   ...
   configuration: "Release",
  },
]

Anyways thanks for the great plugin!

jeanpaulrichter avatar Jun 24 '24 13:06 jeanpaulrichter

I've added two getters: Use cmake.get_launch_target_path() to get full path of launch target path. Use cmake.get_build_target_path() to get full path of build target path.

Civitasv avatar Jun 25 '24 15:06 Civitasv

thx! The "Ninja Multi-Config" generator might still be a minor problem though. Because it generates all configurations in CMAKE_CONFIGURATION_TYPES at once in the same folder, RUNTIME_OUTPUT_DIRECTORY is also the same, but with subfolders for "Release", "Debug" etc. cmake.get_build_target_path() & cmake.get_launch_target_path() atm seem to ignore the currently selected buildPreset and its configuration: They always return the path of the first configuration in CMAKE_CONFIGURATION_TYPES. So if CMAKE_CONFIGURATION_TYPES="Debug;Release" they will return the path of the debug target even if the selected buildPreset sets the configuration to "Release". Most of the time I'm only interested in the debug target anyway so I kinda can live with it though :) .

jeanpaulrichter avatar Jun 25 '24 18:06 jeanpaulrichter

Can you please check whether this problem still exists after the last presets refactoring?

lceWolf avatar Jan 09 '25 23:01 lceWolf

As far as I can tell the problem persists, but maybe I am just doing it wrong, so here is my test CMakePresets.json :

{
    "version": 3,
    "cmakeMinimumRequired": {
      "major": 3,
      "minor": 26,
      "patch": 0
    },
    "configurePresets": [
      {
        "name": "linux-x64",
        "displayName": "Linux x64",
        "generator": "Ninja Multi-Config",
        "binaryDir": "${sourceDir}/obj/linux-x64",
        "architecture": {
          "value": "x64",
          "strategy": "external"
        },
        "cacheVariables": {
          "CMAKE_CONFIGURATION_TYPES": "Release;Debug"
        }
      }
    ],
    "buildPresets": [
      {
        "name": "linux_x64d",
        "displayName": "Linux x64 Debug",
        "configurePreset": "linux-x64",
        "configuration": "Debug"
      },
      {
        "name": "linux_x64",
        "displayName": "Linux x64 Release",
        "configurePreset": "linux-x64",
        "configuration": "Release"
      }
    ],
    "testPresets": [],
    "vendor": {
      "example.com/ExampleIDE/1.0": {
        "autoFormat": false
      }
    }
  }

Switching preset via :CMakeSelectBuildPreset I can build both obj/linux-x64/Debug/test and obj/linux-x64/Release/test .

But in both cases I fail to get the correct target path when build preset "Linux x64 Debug" is selected:

local target = cmake.get_launch_target()
local filename = cmake.get_model_info()[target].nameOnDisk
local path = cmake.get_launch_path(target) .. filename
-- path = ../obj/linux-x64/Release/test
local path = cmake.get_build_target_path()
-- path = ../obj/linux-x64/Release/test

But when I switch Debug and Release in CMakePresets.json :

"CMAKE_CONFIGURATION_TYPES": "Debug;Release"

get_build_target_path() will return obj/linux-x64/Debug/test even if I select "Linux x64 Release" with :CMakeSelectBuildPreset and successfully build /obj/linux-x64/Release/test via :CMakeBuild .

jeanpaulrichter avatar Feb 13 '25 13:02 jeanpaulrichter

Yeah, previous implementation didn't work well on "Ninja Multi-Config" generator. Can you see if this problem still exists with latest commit?

Civitasv avatar May 17 '25 15:05 Civitasv

Hi, thanks for the update. Sadly with the current version a get the following error when running :CMakeBuild after successfully selecting build type, target, preset ... with CMakeSelect* .

Error executing Lua callback: ...hare/nvim/lazy/cmake-tools.nvim/lua/cmake-tools/init.lua:364: calling 'get_build_target' on bad self (table expected, got nil)
stack traceback:
[C]: in function 'get_build_target'
...hare/nvim/lazy/cmake-tools.nvim/lua/cmake-tools/init.lua:364: in function <...hare/nvim/lazy/cmake-tools.nvim/lua/cmake-tools/init.lua:305>

I'm not 100% sure if I messed up my nvim config somehow, but :CMakeBuild worked fine in previous versions :/

jeanpaulrichter avatar May 22 '25 10:05 jeanpaulrichter

https://github.com/Civitasv/cmake-tools.nvim/issues/320

I am looking into it

lceWolf avatar May 22 '25 10:05 lceWolf

Latest commit should fix this.

Civitasv avatar May 22 '25 16:05 Civitasv

Thx! I now can build Debug configuration again. But BuildPresets still don't seem to work with cmake-tools (as least as I understand them). I uploaded a minimal example here: https://github.com/jeanpaulrichter/cmake-tools-test "cmake --build --preset linux_x64" builds obj/Release/test "cmake --build --preset linux_x64d" builds obj/Debug/test But even if I select "linux_x64" with :CMakeSelectBuildPreset I cannot build obj/Release/test with :CMakeBuild and require("cmake-tools").get_build_target_path() always returns ../obj/Debug/test whether or not linux_x64 or linux_x64d is selected.

jeanpaulrichter avatar May 22 '25 19:05 jeanpaulrichter

I've added support for multi-configuration generators, can you check if it work?

Civitasv avatar May 23 '25 17:05 Civitasv

Yeah as far as I can tell it works great now. Thank you.

jeanpaulrichter avatar May 23 '25 20:05 jeanpaulrichter