简体   繁体   中英

How to automate (from command-line) the installation of a Visual Studio Build Tools build environment, for C++, .NET, C#, etc

Note: I have already read How can I install the VS2017 version of msbuild on a build server without installing the IDE? but this does not answer with a totally GUI-less script-only install.


Along the years, here is what I noticed:

  • I download a project from Github; or open an old project of mine (say from 4 years ago)

  • run msbuild.exe theproject.sln

  • oops, I don't have the right Visual Studio version / .NET Framework version / the right SDK is missing (example situation here among many others)

  • then spend X hours browsing on websites like https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2019 , install a package, then notice it's not the right one ( msbuild still fails), download another package, install it, etc.

  • at the end you have downloaded 8 GB of packages, waited for the download, waited for the install, for the reboot, and you're still not sure it works

  • your computer is now a mess with 5 different versions of SDKs installed at the same time that probably collide with each other (did version Z overwrite/uninstall version Y or not?)

This might be a solution to avoid this problem:

How to install the required MS build tools from command-line? (I don't want to use any IDE, I want to script everything)

If it was possible, I would just, once for all create a build.bat file for every project, that would be something like:

msbuildget --package=VC14 --installdir=c:\buildtools\vc14     # automatically download and install
C:\buildtools\vc14\bin\msbuild.exe myproject.sln

or

msbuildget --package=.NET-35 --installdir=c:\buildtools\net35   
C:\buildtools\net35\bin\msbuild.exe myproject.sln

How to do this?

With this method, even if you open a 6-year old project, you should be able to build it.

How to automate (from command-line) the installation of a Visual Studio Build Tools build environment, for C++ version X, .NET C# version Z, etc

First, you should note that, all the workloads or packages need to be installed and they will be integrated into Build Tool, what you need is the workload Component ID of them.

You can refer to this document to obtain the related Component ID of buildtool.

Besides , this document also list the command line installed instructions. And the order is the same as the build tool.

Suggestion

You can try the below script:

// This is for desktop development and also add the net framwork 3.5
vs_buildtool_xxx.exe --add Microsoft.VisualStudio.Workload.ManagedDesktopBuildTools^ 
                     --add Microsoft.Net.Component.3.5.DeveloperTools^ 
                     --add Microsoft.Net.Component.4.5.2.TargetingPack^
                     --installPath C:\BuildTools

C:\BuildTools\MSBuild\Current\Bin\MSBuild.exe myproject.sln

And you can add any more workloads or packages by commamd -add with the related Component ID.

If you want to build c++ project, you can try the following example:

vs_buildtool_xxx.exe --add Microsoft.VisualStudio.Workload.VCTools^ 
                     --add Microsoft.VisualStudio.Component.VC.Tools.x86.x64^ 
                     --add Microsoft.VisualStudio.Component.VC.140^
                     --installPath C:\BuildTools

C:\BuildTools\MSBuild\Current\Bin\MSBuild.exe myproject.sln

Microsoft.VisualStudio.Component.VC.140 means that VS2015 build tool for C++.


Important note : using command line is quite different from the vs_installer UI. When you click the c++ build tool in vs_installer UI, you could see that it will install related components automatically.

在此处输入图像描述

These components are listed under Microsoft.VisualStudio.Workload.VCTools workload and you can choose whether or not to install them.

However , it is not to specify that the workload will install all of them.

When you use command line, it will not install any related components automatically, so you should add them one by one manually .

For c++ projects, you could use these commands to install it:

vs_buildtool.exe --add Microsoft.VisualStudio.Workload.MSBuildTools^ 
                 --add Microsoft.VisualStudio.Workload.VCTools^ 
                 --add Microsoft.VisualStudio.Component.VC.Tools.x86.x64^ 
                 --add Microsoft.VisualStudio.Component.Windows10SDK.18362^ 
                 --add Microsoft.VisualStudio.Component.VC.CMake.Project^ 
                 --add Microsoft.VisualStudio.Component.TestTools.BuildTools^ 
                 --add Microsoft.VisualStudio.Component.VC.ASAN^ 
                 --add Microsoft.VisualStudio.Component.VC.140

I've just spent a good while trying to figure this out. I ended up with this:

.\vs_buildtool.exe --passive --wait --add Microsoft.VisualStudio.Workload.VCTools;includeRecommended

Note the use of includeRecommended to avoid adding components individually. includeOptional is also available. You can also use eg --includeRecommended flag to include recommended for all workloads (see here ).

Or to install using winget :

winget install -e --id Microsoft.VisualStudio.2022.BuildTools --override "--passive --wait --add Microsoft.VisualStudio.Workload.VCTools;includeRecommended"

Hope this might be of use to someone else

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM