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

[Feature request] Run shell script before configuration happens #809

Closed
Cansisti opened this issue Sep 25, 2019 · 24 comments
Closed

[Feature request] Run shell script before configuration happens #809

Cansisti opened this issue Sep 25, 2019 · 24 comments
Labels
enhancement an enhancement to the product that is either not present or an improvement to an existing feature Feature: kits
Milestone

Comments

@Cansisti
Copy link

Cansisti commented Sep 25, 2019

Brief Issue Summary

When working in console I'm using shell script to source many environment variables required for configuration and build. I see there is a setting to add variables separately, but that's a long and often changing list - I'd love to be able to just pass the script name so it's run in the shell cmake-tools is working.

Expected:

Some setting like "pre-configuration commands".

Other Notes/Information

It might be worth mentioning that I'm working with SSH remote connection plugin, and cmake-tools is installed on remote machine.

@bobbrow
Copy link
Member

bobbrow commented Sep 26, 2019

This might be easily doable by augmenting the Kits schema. Something similar is done to load environment variables for the MSVC compiler, but it's internal to the extension.

@bobbrow bobbrow added enhancement an enhancement to the product that is either not present or an improvement to an existing feature Feature: kits labels Sep 26, 2019
@tomj-work
Copy link

tomj-work commented Nov 29, 2019

I would also like to see this functionality - I'm using CMake to build some quite old code, in which some build steps are performed in bash scripts. Because I can't source the required rc file before calling CMake, these legacy steps crash the build. The CMake plugin is therefore not usable for these builds!

@ruilvo
Copy link

ruilvo commented Apr 10, 2020

I'd like this feature too. My particular use case, and I bet shared by many, is to activate a Conda enviroment before building, so I build with the right Python libraries (CMake itself respects activated envs as priority in FindPython).

@bobbrow
Copy link
Member

bobbrow commented Apr 10, 2020

This feature was implemented and will be released with 1.4.

@bobbrow bobbrow modified the milestones: 1.5.0, 1.4.0 Apr 10, 2020
@andreeis
Copy link
Contributor

CMake Tools 1.4.0 Beta is available on GitHub.
https://github.com/microsoft/vscode-cmake-tools/releases/tag/1.4.0-beta

Please try it out and let us know if you encounter any issues.

@lyon-fnal
Copy link

This is essential for my workflow - I have to source several scripts to set up my build environment. This option worked great! Much easier than maintaining a very long list of environment variables. Thanks for adding it!!

@andreeis
Copy link
Contributor

This fix is available in CMake Tools #1.4.0 which has been released.

@arminms
Copy link

arminms commented May 25, 2020

I cannot see the settings in the CMake Tools configuration. I tried to set it manually using cmake.environmentVariablesShellScript and cmake.environmentVariablesShellScriptDirectory to no avail.

@andreeis
Copy link
Contributor

From looking at the PR that introduced this functionality, it should be the "environmentSetupScript" property of a kit. Manually add that to the kit you are using, in the kits file json. Let us know how that goes.

@jheaff1
Copy link

jheaff1 commented Jun 15, 2020

I tried setting the environmentSetupScript property of a kit in cmake-tools-kits.json and it didn't seem to work. Will the given be script be run or "sourced". My setup script contains many environment variables, therefore i would like vscode to effectively run "source "

@cacadam89
Copy link

I tried setting the environmentSetupScript property of a kit in cmake-tools-kits.json and it didn't seem to work. Will the given be script be run or "sourced". My setup script contains many environment variables, therefore i would like vscode to effectively run "source "

I am having the same issue where this property seems to have no effect. In case it is a run instead of source, I tried making a separate script to source what I need, and though it works when I call it manually it is not called when cmake is configured.

@arminms
Copy link

arminms commented Jul 10, 2020

From looking at the PR that introduced this functionality, it should be the "environmentSetupScript" property of a kit. Manually add that to the kit you are using, in the kits file json. Let us know how that goes.

Thank you! Actually, it works for me. I just edited cmake-tools-kits.json and added the following lines to enable Intel toolchain:

  {
    "name": "Intel 19.0.5.281",
    "environmentSetupScript": "/opt/intel/bin/iccvars.sh intel64",
    "compilers": {
      "C": "/opt/intel/compilers_and_libraries_2019.5.281/linux/bin/intel64/icc",
      "CXX": "/opt/intel/compilers_and_libraries_2019.5.281/linux/bin/intel64/icpc",
      "Fortran": "/opt/intel/compilers_and_libraries_2019.5.281/linux/bin/intel64/ifort"
    }
  }

Then, whenever I switch to Intel 19.0.5.281 kit, /opt/intel/bin/iccvars.sh intel64 script runs and setups the environment.

@duanyutong
Copy link

duanyutong commented Jul 22, 2020

environmentSetupScript does not work for me in a remote docker container with CMake Tools 1.4.1. A simple test is the following cmake-kits.json which is successfully picked up by the extension.

[
    {
        "name": "gcc7 setup environment",
        "environmentSetupScript": "export PYTHONPATH=/root/",
        "compilers": {
            "C": "/usr/bin/gcc-7",
            "CXX": "/usr/bin/g++-7"
            }
    }
]

I then have a line of message(STATUS $ENV{PYTHONPATH}) in the CMakeLists.txt to verify it, but It's blank. Configure fails because of missing env vars. I also tried putting the env vars in a .sh script and writing "environmentSetupScript": source file.sh", same issue. All I need is that the shell, whichever the extension uses to execute the cmake calls, respects ~/.bashrc.

@jheaff1
Copy link

jheaff1 commented Jul 22, 2020

@duanyutong Try

"environmentSetupScript": file.sh

@duanyutong
Copy link

duanyutong commented Jul 24, 2020

@jheaff1 Thanks. I tried that in various ways and here are my observations.

  • the .sh file it points to cannot simply contain source ~/.bashrc or source $HOME/.bashrc or even the full absolute path; it has to contain the environment variable definitions itself
  • the extension loads the .sh script only once when the kit is first selected; so, if the script is modified, one has to refresh the kit selection to get it to reload the script; it's pretty unexpected that it doesn't load the latest script on disk upon execution

It seems that executing cmake calls in the same kind of shell as the vscode integrated terminal would be a simpler and more general solution.

@rnwzd
Copy link

rnwzd commented Aug 3, 2020

@jheaff1 Thanks. I tried that in various ways and here are my observations.

  • the .sh file it points to cannot simply contain source ~/.bashrc or source $HOME/.bashrc or even the full absolute path; it has to contain the environment variable definitions itself

I can confirm that it cannot source an external file.
The feature would be essential to integrate the Intel OneApi Toolkit since they provide a script that must be sourced and would be very annoying to rewrite.
https://software.intel.com/content/www/us/en/develop/documentation/get-started-with-intel-oneapi-base-linux/top/before-you-begin.html#before-you-begin_GUID-338EB548-7DB6-410E-B4BF-E65C017389C4

@ztarem
Copy link

ztarem commented Aug 27, 2020

In my case the setup script must be sourced even before any attempt to run CMake. The CMake version installed on the remote is very old and not relevant - only the one set up by the script is. (Same is true for git and Python - only after sourcing the setup script they are of the correct version).

My manual process is:

cd <project-dir>
source setup.sh
cd build
cmake ..., make ..., etc.

@blackliner
Copy link

Same here, need to . /opt/ros/melodic/setup.bash to get the ROS1 env setup right before the cmake .. call. Or else:

[cmake] ImportError: No module named catkin.environment_cache

@renyhp
Copy link

renyhp commented Oct 5, 2020

I'm having issues with the parsing of environment variables. In my case the output of printenv contains these two lines

BASH_FUNC_module()=() {  eval `/usr/bin/modulecmd bash $*`
}

which causes
[kit] Error parsing environment variable: }

Maybe the solution to this issue should be adjusted to parse multiline environment variables, or to actually execute the shell script, rather than only importing the environment...

EDIT: Should I open a new issue instead of hoping to get a response from a closed one?

@ishipachev
Copy link

Can it be a custom command to run before build?
For an example, I'm working remotely on the server with modules: example
Every time before any build I should source a custom script which loads modules I need for my build.
The simplest way for me would be able to call a custom script which loads those variables. But I'm not sure if "source load_script.sh" will help for a kit to build with all modules sourced.

@lesomnus
Copy link

For those who came for ROS.

...
	"cmake.configureSettings": {
		"CMAKE_PREFIX_PATH": "/opt/ros/${ROS_DISTRO}"
	},
...

@pvelesko
Copy link

pvelesko commented Sep 2, 2021

Can confirm that "environmentSetupScript": "source /home/pvelesko/load_modules.sh, does not solve the issue.

#!/bin/bash
module load oneapi
module load hipcl

Modules do not work and sourcing Intel OneAPI setvars.sh script fails with failing to parse variables.
Can this be reopened?
@andreeis

@ishipachev
Copy link

To be honest, I don't understand why this issue is closed?
I came here almost a one year later but now from the other side, not modules, but ROS. And it is still a problem.
I can't debug the script in VSCode as it has to source a ROS environment before running python script which relies on this ROS environment.

@ishipachev
Copy link

Ok, I sort of "solved" this issue to the point that I'm able to use VSCode to run scripts related to ROS.
I start VSCode from the same console I do sourcing of ROS environment. Thus VSCode inherits a proper ROS-aware environment and my scripts work.

But in this case I have a set environment for all launches I will do in this VSCode.

I also still don't know how it can be used to run VSCode remotely on the server with modules. Probably, the only case will be to add sourcing of modules into .bashrc and hope that when VSCode will try to connect to remote server it will also swallow all this sourcing from .bashrc

Both ways are ugly and defenitely something I would like to avoid, but at least it gave me working environment I was looking for.

@github-actions github-actions bot locked and limited conversation to collaborators Jan 31, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement an enhancement to the product that is either not present or an improvement to an existing feature Feature: kits
Projects
None yet
Development

No branches or pull requests