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

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

Closed
EricHouet opened this issue Dec 13, 2023 · 6 comments · Fixed by #15461
Closed

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

EricHouet opened this issue Dec 13, 2023 · 6 comments · Fixed by #15461
Assignees
Milestone

Comments

@EricHouet
Copy link

EricHouet commented Dec 13, 2023

Environment details

  • Operating System+version: Windows 10 22H2
  • Compiler+version: msvc 193
  • Conan version: 2.0.14
  • Python version: 3.11.6

Steps to reproduce

  1. Create a simple conanfile.py with a dependency. For example:
from conan import ConanFile
from conan.tools.cmake import CMake, cmake_layout, CMakeToolchain, CMakeDeps

class Conan(ConanFile):
   settings = "os", "compiler", "build_type", "arch"

   requires = [("gtest/1.12.1")]

   generators = 'CMakeDeps', 'CMakeToolchain'

   def layout(self):
      cmake_layout(self)
  1. Run conan install . -c tools.env.virtualenv:powershell=True

Expected Result:

  • The generated conanbuild.bat and conanbuild.ps1 basically does the same.

Result:

  • conanbuild.bat activate the Visual Studio 17 environment while conanbuild.ps1 does not.

My understanding of the conanbuild scripts was that the visual studio environment would be activated when running them. So I was expecting the conanbuild.ps1 script to do so.

Maybe my understanding is incorrect but I feel like both version should do the same.

Logs

PS C:\Local> conan profile show
Host profile:
[settings]
arch=x86_64
build_type=Release
compiler=msvc
compiler.cppstd=14
compiler.runtime=dynamic
compiler.runtime_type=Release
compiler.version=193
os=Windows
[conf]
tools.cmake.cmaketoolchain:generator=Ninja

Build profile:
[settings]
arch=x86_64
build_type=Release
compiler=msvc
compiler.cppstd=14
compiler.runtime=dynamic
compiler.runtime_type=Release
compiler.version=193
os=Windows
[conf]
tools.cmake.cmaketoolchain:generator=Ninja

PS C:\Local> conan --version
Conan version 2.0.14
PS C:\Local> python --version
Python 3.11.6
PS C:\Local> conan install . -c tools.env.virtualenv:powershell=True

======== Input profiles ========
Profile host:
[settings]
arch=x86_64
build_type=Release
compiler=msvc
compiler.cppstd=14
compiler.runtime=dynamic
compiler.runtime_type=Release
compiler.version=193
os=Windows
[conf]
tools.cmake.cmaketoolchain:generator=Ninja
tools.env.virtualenv:powershell=True

Profile build:
[settings]
arch=x86_64
build_type=Release
compiler=msvc
compiler.cppstd=14
compiler.runtime=dynamic
compiler.runtime_type=Release
compiler.version=193
os=Windows
[conf]
tools.cmake.cmaketoolchain:generator=Ninja


======== Computing dependency graph ========
Graph root
    conanfile.py: C:\Local\conanfile.py
Requirements
    gtest/1.12.1#3ed1009f8004c2ac29ab5b1d9fbfbf53 - Cache

======== Computing necessary packages ========
Requirements
    gtest/1.12.1#3ed1009f8004c2ac29ab5b1d9fbfbf53:1c9c0f79f33093a353ed3a111698ab362c007c19#96cb726491a605d4ad6dc216969ccb06 - Cache

======== Installing packages ========
gtest/1.12.1: Already installed! (1 of 1)
WARN: deprecated: Usage of deprecated Conan 1.X features that will be removed in Conan 2.X:
WARN: deprecated:     'cpp_info.names' used in: gtest/1.12.1

======== Finalizing install (deploy, generators) ========
conanfile.py: Writing generators to C:\Local\build\Release\generators
conanfile.py: Generator 'CMakeToolchain' calling 'generate()'
conanfile.py: CMakeToolchain generated: conan_toolchain.cmake
conanfile.py: Preset 'conan-release' added to CMakePresets.json. Invoke it manually using 'cmake --preset conan-release' if using CMake>=3.23
conanfile.py: If your CMake version is not compatible with CMakePresets (<3.23) call cmake like: 'cmake <path> -G Ninja -DCMAKE_TOOLCHAIN_FILE=C:\Local\build\Release\generators\conan_toolchain.cmake -DCMAKE_POLICY_DEFAULT_CMP0091=NEW -DCMAKE_BUILD_TYPE=Release'
conanfile.py: CMakeToolchain generated: CMakePresets.json
conanfile.py: Generator 'CMakeDeps' calling 'generate()'
conanfile.py: Generating aggregated env files
conanfile.py: Generated aggregated env files: ['conanbuild.bat', 'conanbuild.ps1', 'conanrun.ps1']
Install finished successfully
PS C:\Local> cat .\build\Release\generators\conanbuild.bat
@echo off
call "%~dp0/conanvcvars.bat"
PS C:\Local> cat .\build\Release\generators\conanvcvars.bat
@echo off
set __VSCMD_ARG_NO_LOGO=1
set VSCMD_SKIP_SENDTELEMETRY=1
echo conanvcvars.bat: Activating environment Visual Studio 17 - amd64 - vcvars_ver=14.3
set "VSCMD_START_DIR=%CD%" && call "C:\Program Files\Microsoft Visual Studio\2022\Professional\VC/Auxiliary/Build/vcvarsall.bat"  amd64 -vcvars_ver=14.3
PS C:\Local> cat .\build\Release\generators\conanbuild.ps1
& "$PSScriptRoot/conanbuildenv-release-x86_64.ps1"
PS C:\Local> cat .\build\Release\generators\conanbuildenv-release-x86_64.ps1
Push-Location $PSScriptRoot
"echo `"Restoring environment`"" | Out-File -FilePath "deactivate_conanbuildenv-release-x86_64.ps1"
$vars = (Get-ChildItem env:*).name
$updated_vars = @()

foreach ($var in $updated_vars)
{
    if ($var -in $vars)
    {
        $var_value = (Get-ChildItem env:$var).value
        Add-Content "deactivate_conanbuildenv-release-x86_64.ps1" "`n`$env:$var = `"$var_value`""
    }
    else
    {
        Add-Content "deactivate_conanbuildenv-release-x86_64.ps1" "`nif (Test-Path env:$var) { Remove-Item env:$var }"
    }
}
Pop-Location
@juansblanco
Copy link
Contributor

juansblanco commented Dec 15, 2023

Hi @EricHouet!
Thanks for your report, I'm looking into it.

@EricHouet
Copy link
Author

Thanks!

According to the CMakeToolchain documentation, generating conanvcvars.bat is expected.

However, nothing is mentionned regarding an eventual conanvcvars.ps1

@juansblanco
Copy link
Contributor

I confirmed there's currently no support for vcvars in Powershell from our side.
It doesn't seem to be something that Powershell supports directly either.

It seems that the implementation is not trivial. I'll bring it to the team.
We could maybe use this to implement it: https://gist.github.com/FeodorFitsner/b34162690ae2838409b7459824d51ed7#file-vcvars-powershell-ps1

For now, I'd suggest to use a different terminal.

@EricHouet
Copy link
Author

Indeed, that is not as trivial as I first though...

Crazy that they did not create a PowerShell equivalent to vcvars.bat

I found this thread:
https://developercommunity.visualstudio.com/t/powershell-version-of-vcvarsallbat/362377

Base on that, this looks like it works:

PS C:\Users\eh> $env:INCLUDE
PS C:\Users\eh> Import-Module "C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"
PS C:\Users\eh> Enter-VsDevShell

cmdlet Enter-VsDevShell at command pipeline position 1
Supply values for the following parameters:
(Type !? for Help.)
VsInstallPath: C:\Program Files\Microsoft Visual Studio\2022\Professional
**********************************************************************
** Visual Studio 2022 Developer PowerShell v17.8.3
** Copyright (c) 2022 Microsoft Corporation
**********************************************************************
PS C:\Users\eh\source\repos> $env:INCLUDE
C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.38.33130\include;C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.38.33130\ATLMFC\include;C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Auxiliary\VS\include;C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\ucrt;C:\Program Files (x86)\Windows Kits\10\\include\10.0.19041.0\\um;C:\Program Files (x86)\Windows Kits\10\\include\10.0.19041.0\\shared;C:\Program Files (x86)\Windows Kits\10\\include\10.0.19041.0\\winrt;C:\Program Files (x86)\Windows Kits\10\\include\10.0.19041.0\\cppwinrt;C:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\include\um
PS C:\Users\eh\source\repos>

Maybe this could be used ?

@EricHouet
Copy link
Author

Even better, according to this, the following should do the trick

& 'C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\Launch-VsDevShell.ps1'

@juansblanco
Copy link
Contributor

Yes! We confirmed that it should work.
I'm working on implementing it in the conanbuild.ps1.

juansblanco added a commit that referenced this issue Jan 29, 2024
Changelog: (Feature): Add support for use of vcvars env variables when
calling from powershell.
Docs: https://github.com/conan-io/docs/pull/XXXX

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
@memsharded memsharded added this to the 2.1 milestone Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants