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

Passing environment variable to "cmake" tasks #2553

Closed
TheophileBlard opened this issue May 17, 2022 · 6 comments · Fixed by #2636
Closed

Passing environment variable to "cmake" tasks #2553

TheophileBlard opened this issue May 17, 2022 · 6 comments · Fixed by #2636
Assignees
Labels
enhancement an enhancement to the product that is either not present or an improvement to an existing feature Feature: tasks
Milestone

Comments

@TheophileBlard
Copy link

Brief Issue Summary

Is it possible to pass environment variable to cmake tasks ?

I'm currently using the pre-release version of the extension, and want to use cmake tasks to install the binaries to predefined directories using DESTDIR. For example, "shell" tasks provide the "env" keyword in "options", but for "cmake" tasks, only the "cwd" is available.

Thanks!

CMake Tools Diagnostics

No response

Debug Log

No response

Additional Information

CMakeTools version : 1.11.19
VSCode verson: 1.67.0
OS: Ubuntu 21.04 TLS

@elahehrashedi elahehrashedi added enhancement an enhancement to the product that is either not present or an improvement to an existing feature Feature: tasks labels May 17, 2022
@elahehrashedi
Copy link
Contributor

elahehrashedi commented May 17, 2022

You can define an active configurePresets in CMakePresets.json, and set the CMAKE_INSTALL_PREFIX there.
Here is an example:

{
    "version": 2,
    "configurePresets": [
        {
            "name": "dev17-ninja-143",
            "displayName": "Ninja - v143,x64",
            "generator": "Ninja",
            "architecture": {
                "strategy": "external",
                "value": "x64"
            },
            "toolset": {
                "strategy": "external",
                "value": "host=x64,version=14.30"
            },
            "binaryDir": "${sourceDir}/out/build/${presetName}",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Debug",
                "CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}"
            },
            "inherits": "base",
            "environment": {
                "PATH":  "$penv{PATH};D:\\ninja"
              }
        }
    ],

The Cmake Install Task will pick the settings from the active configurePresets and run the install. Will this approach solve your issue?
We can also look into adding more customizable features to cmake tasks in future.

@elahehrashedi
Copy link
Contributor

elahehrashedi commented May 17, 2022

One option that can be added to the configure or build task can be the name of the preset (instead of always picking the default). This feature is not available yet. We can look into adding it in future.
for example:

        {
            "type": "cmake",
            "label": "CMake: build",
            "command": "build",
            "targets": [
                "all"
            ],
            **"preset": "presetName",**
            "group": "build",
            "problemMatcher": [],
            "detail": "CMake template build task"
        }

@TheophileBlard
Copy link
Author

Hi!

The DESTDIR variable is not interpreted at configure time but at install time, and it's not a CMake variable, but an environment variable that CMake uses to relocate the installation.
A typical use of this variable is:

DESTDIR=/package/stage cmake --install .

With the environment keyword of presets it seems that I can set DESTDIR to a custom directory. However, when I use the preset keyword in my tasks.json I get the following error: Property preset is not allowed.. Is this an unreleased feature ? I'm using v1.11.19 of CMake Tools.

Do you plan to add a way to set environment variables directly in CMake tasks ?

@elahehrashedi elahehrashedi added this to the On Deck milestone May 18, 2022
@elahehrashedi elahehrashedi changed the title Passing environment variable to install command in task Passing environment variable to "cmake" tasks May 18, 2022
@elahehrashedi
Copy link
Contributor

The DESTDIR variable is not interpreted at configure time but at install time, and it's not a CMake variable, but an environment variable that CMake uses to relocate the installation.

Thank you for your clarification.

However, when I use the preset keyword in my tasks.json I get the following error: Property preset is not allowed.
We can look into adding this feature in the future. Currently, the default preset will be picked.

Currently, as a workaround, you can add the environment variables in your default build preset.

I will use this issue to track adding environment variables to the cmake tasks.

@TheophileBlard
Copy link
Author

Thank you !

@elahehrashedi
Copy link
Contributor

There is an upcoming fix for this issue in PR #2636

@elahehrashedi elahehrashedi modified the milestones: On Deck, 1.12 Jul 5, 2022
@bobbrow bobbrow linked a pull request Jul 8, 2022 that will close this issue
@github-actions github-actions bot locked and limited conversation to collaborators Oct 11, 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: tasks
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants