Skip to content

Add support for vcvars when using Powershell - #15461

Merged
juansblanco merged 17 commits into
conan-io:develop2from
juansblanco:js/feature/vcvars-with-powershell
Jan 29, 2024
Merged

juansblanco merged 17 commits into
conan-io:develop2from
juansblanco:js/feature/vcvars-with-powershell

Conversation

@juansblanco

@juansblanco juansblanco commented Jan 16, 2024

Copy link
Copy Markdown
Contributor

Changelog: Feature: Add support for use of vcvars env variables when calling from powershell.
Docs: conan-io/docs#3541

Trying to activate the generated conanbuild.ps1 file to set the environment from a Powershell console was not working.
To fix this there's now a new conanvcvars.ps1 file that saves the environment from calling conanvcvars.bat to be used in the Powershell.

Fixes: #15267

Comment thread conan/tools/microsoft/visual.py Outdated
generator_path = conanfile.generators_path
content_ps1 = textwrap.dedent(f"""\
pushd "{generator_path}"
cmd /c "conanvcvars.bat&set" |

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could use $PSScriptRoot/conanvcvars.bat instead of directly by name? this would mean we don't have to do pushd and popd - and use the same structure as conanbuild.ps1 already does - unless I'm missing something?

(see docs)

Comment thread conans/client/generators/__init__.py Outdated
Comment thread conans/test/integration/toolchains/env/test_virtualenv_powershell.py Outdated
Comment thread conans/client/generators/__init__.py Outdated
@juansblanco
juansblanco marked this pull request as ready for review January 17, 2024 18:09
@juansblanco

juansblanco commented Jan 18, 2024

Copy link
Copy Markdown
Contributor Author

The deactivate environments needs fixing, I'm looking into it.

Edit: It was already broken when trying to deactivate conanvcvars envs, it will be fixed in a different PR.

if is_ps1:
content_ps1 = textwrap.dedent(f"""\
if (-not $env:VSCMD_ARG_VCVARS_VER){{
Push-Location "$PSScriptRoot"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reverted this back to using "push/pop" as calling the path directly in the cmd command was causing trouble when path had spaces.

@memsharded memsharded left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

Comment thread conans/test/functional/toolchains/env/test_virtualenv_powershell.py Outdated
Comment thread conan/tools/microsoft/visual.py
@juansblanco juansblanco added this to the 2.1 milestone Jan 24, 2024
@juansblanco
juansblanco merged commit f2b5635 into conan-io:develop2 Jan 29, 2024
Comment on lines +161 to +169
cmd /c "conanvcvars.bat&set" |
foreach {{
if ($_ -match "=") {{
$v = $_.split("=", 2); set-item -force -path "ENV:\$($v[0])" -value "$($v[1])"
}}
}}
Pop-Location
write-host conanvcvars.ps1: Activated environment}}
""")

@jmarrec jmarrec Feb 13, 2024

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know no-one asked for my opinion ( :) ) but this makes me uneasy.

here's how you could do it similarly than the Launch-VsDevShell.ps1 ($vspath\Common7\Tools\Launch-VsDevShell.ps1)

$vspath=$(vswhere -products '*' -requires Microsoft.Component.MSBuild -property installationPath -latest)
$devShellModule = "$vspath\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"
Import-Module $devShellModule
Enter-VsDevShell -VsInstallPath $vspath -SkipAutomaticLocation -Arch amd64

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get-Help Enter-VsDevShell

NAME
    Enter-VsDevShell

SYNTAX
    Enter-VsDevShell -VsInstallPath <string> [-SkipExistingEnvironmentVariables] [-StartInPath <string>] [-Arch {Default | x86 | amd64 | arm | arm64}] [-HostArch {Default | x86 | amd64}]
    [-DevCmdArguments <string>] [-DevCmdDebugLevel {None | Basic | Detailed | Trace}] [-SkipAutomaticLocation] [-SetDefaultWindowTitle] [-ReportNewInstanceType {PowerShell | Cmd |
    LaunchScript}] [<CommonParameters>]

    Enter-VsDevShell [-VsInstanceId] <string> [-SkipExistingEnvironmentVariables] [-StartInPath <string>] [-Arch {Default | x86 | amd64 | arm | arm64}] [-HostArch {Default | x86 |
    amd64}] [-DevCmdArguments <string>] [-DevCmdDebugLevel {None | Basic | Detailed | Trace}] [-SkipAutomaticLocation] [-SetDefaultWindowTitle] [-ReportNewInstanceType {PowerShell | Cmd
    | LaunchScript}] [<CommonParameters>]

    Enter-VsDevShell [-Arch {Default | x86 | amd64 | arm | arm64}] [-HostArch {Default | x86 | amd64}] [-Test] [-DevCmdDebugLevel {None | Basic | Detailed | Trace}] [<CommonParameters>]


ALIASES
    None


REMARKS
    None

@jcar87 jcar87 Feb 13, 2024

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jmarrec - thanks for your feedback.

This is the approach we considered initially, but discarded it for a couple of reasons:

  • Launch-VsDevShell.ps1 and the underlying implementation do not expose the same interface that vcvars provides, in particular the ability to specify the toolset with the -vcvars_ver, or using the Windows Store SDK, or specifying the WIndows SDK version. We do rely on mapping Conan settings/ conf to some of these - and the interface for the powershell ones do not expose these.
  • The documented way would be to invoke Launch-VsDevShell.ps1 once located - rather than importing DevShell.dll - this is evidenced:

Launch-VsDevShell.ps1 is the recommended way to initialize Developer PowerShell interactively or for scripting build automation.

we wouldn't want to use undocumented/subject to breakages features, and the Launch-VsDevShell.ps1 does not provide feature parity, so we relied on this fallback.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, thanks a lot for the detailed explanation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] Build env scripts generated with virtualenv:powershell are incomplete

4 participants