Skip to content
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

Configure target specific environment variables #90

Closed
hfn92 opened this issue Jul 20, 2023 · 6 comments
Closed

Configure target specific environment variables #90

hfn92 opened this issue Jul 20, 2023 · 6 comments
Labels
enhancement New feature or request

Comments

@hfn92
Copy link
Contributor

hfn92 commented Jul 20, 2023

Describe the problem or limitation you are having
I sometimes need to run executables with specific environment variables.
This could also be useful for generating and building as some compilers (qnx?) require specific env-vars to compile.

Describe the solution you'd like
Inspired by how QtCreator handles environment variables I think it would be useful to be able to configure env-vars for the generate/configure/build step and for individual targets.

My idea is that the env-vars could be configured in a regular vim buffer using lua code. (Similar to the quick window of harpoon)

For example. CMakeConfigureBuildEnvironment would open up a floating window with:

return {
  env { 
    VERBOSE = 1,
  }
}

This would add VERBOSE=1 to env-vars for the generate and build steps

And CMakeConfigureRunEnvironment {target_or_current} for target specific env-vars.

return {
  inherit_build_environment = true,
  env { 
    log_file = "/tmp/log.txt",
    log_level = "trace",
  }
}

This would add VERBOSE=1 log_file = "/tmp/log.txt" log_level = "trace" to env-vars when running the specific target.

This would also be nice since cmake-tools could persist these similar to session.

Describe alternatives you've considered
I can just close nvim set env-vars.

@hfn92 hfn92 added the enhancement New feature or request label Jul 20, 2023
@Civitasv
Copy link
Owner

That's an interesting idea! Currently, for generate step, this plugin already supports setting env by using kits or variants(though we don't support env option yet). So we should consider the precedence of them.

For build and run step, currently there is no convenient way to do this. Maybe we can try to implement your idea.

@hfn92
Copy link
Contributor Author

hfn92 commented Jul 23, 2023

Since since you seem to be okay with the general idea of the feature I started with a bit of a Prototype.

https://github.com/hfn92/cmake-tools.nvim/commits/env-feature

This should already work for launch target but not for building and debugging. And probably only for unix but its a start

@Civitasv
Copy link
Owner

Bravo 👏

@rohit-kumar-j
Copy link
Contributor

@hfn92 , this is a good feature, and I would like to test it out fully for windows. Do you have a test project covering all the test cases for this?

In the mean time, I found an issue on windows, and now pushed a fix....

This was when executing :CMakeRun in terminal mode:

~$ cd "C:\Kdev" && cmake --build C:\Kdev\build --target Ktest_compiler_test && cmd /C "cd "C:\Kdev\build\" && C:\Kdev\build\test/Ktest_compiler_test.exe"  

The ideal command should be:

~$ cd "C:\Kdev" && cmake --build C:\Kdev\build --target Ktest_compiler_test && cmd /C "cd C:\Kdev\build\ && C:\Kdev\build\test/Ktest_compiler_test.exe"

I think Windows didn't expect and additional " for within the cmd /C " ... " command.

launch_path = terminal.prepare_launch_path(launch_path)
full_cmd = "cd " .. launch_path .. " &&"
...
if osys.iswin32 then -- wrap in sub process to prevent env vars from being persited
full_cmd = 'cmd /C "' .. full_cmd .. '"'
end

In terminal.prepare_launch_path:

if osys.iswin32 then
path = '"' .. path .. '"' -- The path is kept in double quotes ... Windows Duh!

@hfn92
Copy link
Contributor Author

hfn92 commented Sep 3, 2023

Hi @rohit-kumar-j. I don't really use windows so it's always hard for me to test 😅 .

But this should cover most cases

int main(int argc, char **argv) {
  auto str = std::getenv("str");
  auto str_with_ws = std::getenv("str_with_ws");
  auto number = std::getenv("number");

  assert(str && str == std::string("test_str"));
  assert(str_with_ws && str_with_ws == std::string("test str"));
  assert(number && number == std::string("1234"));
  return 0;
}

with the config

  env = {
    number = 1234,
    str = "test_str",
    str_with_ws = "test str"
  },

It always depends on the os and the tool whether not " needs to be included on the command line :/

Bit OT but I was wondering if cmake-tools should even handle execution itself at all. Maybe it would make sense to depend on overseer.nvim. It should handle args and env var correctly. And I assume it works for windows too. Maybe thats better than trying to implement and test everything again for every os.

I'm currently on a branch that runs all command trough overseer (hfn92@d256a43). It works fine for me at aleast.

@Civitasv
Copy link
Owner

Civitasv commented Sep 3, 2023

Currently, overseer.nvim is already a choice of executor, and now the only runner is the terminal, we can also make overseer a runner too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants