How do you automate a Visual Studio build? - visual-studio

How do you turn a Visual Studio build that you'd perform in the IDE into a script that you can run from the command line?

With VS2008 you can do this:
devenv solution.sln /build configuration

\Windows\Microsoft.NET\Framework\[YOUR .NET VERSION]\msbuild.exe
Lots of command line parameters, but the simplest is just:
msbuild.exe yoursln.sln

Simplest way: navigate to the directory containing the solution or project file, and run msbuild (assuming you have Visual Studio 2005 or newer).
More flexible ways:
Read up on the MSBuild
reference. There are tons of
customization, especially once
you've installed the MSBuild Community Tasks Project.
Use NAnt. It has existed
for longer than MSBuild and has more
community support, but requires you
to start a project file from
scratch, rather than extending the
existing, Visual Studio-created one.

Here is the script I'm using to completely automate the command line build of x86 AND x64 configurations for the same solution through batch scripts.
This is based on DevEnv.exe as it works if you have a Setup project in your build (msbuild doesn't support building Setup projects).
I'm assuming your setup is 32bit Windows 7 with Visual Studio 2010 setup using the x86 native compiler and x64 cross compiler.
If you're running 64bit windows you may need to change x86_amd64 to amd64 in the batch script depending on your setup.
This is assuming Visual Studio is installed in Program Files and your solution is located in D:\MySoln
Create a file called buildall.bat and add this to it:
D:
cd "D:\MySoln"
if "%1" == "" goto all
if %1 == x86 goto x86
if %1 == x64 goto x64
:x86
%comspec% /k ""C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"" x86 < crosscompilex86.bat
goto eof
:x64
%comspec% /k ""C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"" x86_amd64 < crosscompilex64.bat
goto eof
:all
%comspec% /k ""C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"" x86 < crosscompilex86.bat
if %ERRORLEVEL% NEQ 0 goto eof
%comspec% /k ""C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"" x86_amd64 < crosscompilex64.bat
goto eof
:eof
pause
Now create 2 more batch scripts:
crosscompilex86.bat to build the Release version of a x86 build and include this
devenv MySoln.sln /clean "Release|x86"
IF %ERRORLEVEL% NEQ 0 EXIT /B %ERRORLEVEL%
devenv MySoln.sln /rebuild "Release|x86"
IF %ERRORLEVEL% NEQ 0 EXIT /B %ERRORLEVEL%
crosscompilex64.bat to build the Release version of the x64 build and include this
devenv MySoln.sln /clean "Release|x64"
IF %ERRORLEVEL% NEQ 0 EXIT /B %ERRORLEVEL%
devenv MySoln.sln /rebuild "Release|x64"
IF %ERRORLEVEL% NEQ 0 EXIT /B %ERRORLEVEL%
Now place all 3 batch files along in your solution folder along with MySoln.sln.
You can build both x86 and x64 Release versions by creating a Shortcut on your desktop which run the following commands:
Build All -> D:\MySoln\buildall.bat
Build x86 Release Only -> D:\MySoln\buildall.bat x86
Build x64 Release Only -> D:\MySoln\buildall.bat x64
If you're using another configuration like AnyCPU etc you would need to customize the above scripts accordingly.

NAnt and MSBuild are the most popular tools to automate your build in .NET, and you can find a discussion on there of the pros/cons of each in the Stack Overflow question Best .NET build tool.

Look into build tool NAnt or MSBuild. I believe MSBuild is the build tool for Visual Studio 2005 and later. I am, however, a fan of NAnt...

Take a look at UppercuT. It has a lot of bang for your buck and it does what you are looking for and much more.
UppercuT uses NAnt to build and it is the insanely easy to use Build Framework.
Automated Builds as easy as (1) solution name, (2) source control path, (3) company name for most projects!
http://projectuppercut.org/
Some good explanations here: UppercuT

Here is my batch file using msbuild for VS 2010 Debug configuration:
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe"
iTegra.Web.sln /p:Configuration=Debug /clp:Summary /nologo

As of Visual Studio 2005, all of the project files (at least for .NET based projects) are actual MSBuild files, so you can call MSBuild on the command line and pass it the project file.
The bottom line is that you need to use a "build scripting language" like NAnt or MSBuild (there are others, but these are the mainstream ones right now) if you want to have any real control over your build process.

I had to do this for a C++ project in Visual Studio 2003 so I don't know how relevant this is to later version of visual studio:
In the directory where your executable is created there will be a BuildLog.htm file. Open that file in your browser and then for each section such as:
Creating temporary file "c:\some\path\RSP00003C.rsp" with contents
[
/D "WIN32" /D "_WINDOWS" /D "STRICT" /D "NDEBUG" ..... (lots of other switches)
.\Project.cpp
.\Another.cpp
.\AndAnother.cpp
".\And Yet Another.cpp"
]
Creating command line "cl.exe #c:\some\path\RSP00003C.rsp /nologo"
create a .rsp file with the content between the square brackets (but not including the square brackets) and call it whatever you like. I seem to remember having problems with absolute paths so you may have to make sure all the paths are relative.
Then in your build script add the command line from the BuildLog.htm file but with your .rsp filename:
cl.exe #autobuild01.rsp /nologo
(note there will also be a link.exe section as well as cl.exe)

A more simple way is to change VS 2015 Projects & Solutions configuration:
Go to the Tools tab -> Options -> Projects and Solutions -> Build and Run -> On Run, when projects are out of date (choose Always build). VOILA!
Now your IDE will automatically build your project when you run (F5) it. Hope this helps, any feedback are welcome.

Related

Build and deploy Xamarin to an emulator through command line

I’m building a xamarin android app using visual studio community edition 2019 on windows. Inside visual studio gui I am able to build and deploy to android emulator. I’m willing to do this without using the gui, through command line . I understand msbuild can build. May I know the command to start the emulator and deploy the app?
Steps to Build & Deploy to Android through the command line (replace Sample with your project name):
In your terminal/command line, run this msbuild Sample.Android/Sample.Android.csproj /verbosity:normal /t:Rebuild /t:PackageForAndroid /t:SignAndroidPackage /p:Configuration=Debug
Then, change directory using cd Sample.Android/bin/Debug to find the the signed APK file,
Using ADB(Android Debug Bridge), install it to your device/emulator with this command adb install com.tfp.sample-Signed.apk, explained here.
I have written a more detailed explanation over here.
I'm a beginner batch scrip writer. So feel free to correct this:). This scripts work for me to build the xamarin visual studio project, start the emulator and deploy the apk. I have two batch files, the second one runs in parallel with the first one.
cmd.bat:
"C:\Program Files\Android\android-sdk\platform-tools\adb.exe" start-server
"C:\Program Files\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\msbuild.exe" /p:Configuration=Debug /p:Platform="any cpu" /v:m "E:\App2\App2.sln"
#echo off
if %ERRORLEVEL%% == 0 (
start /min call "E:\App2\Build Commands\install.bat" ""
"C:\Program Files\Android\android-sdk\emulator\emulator.EXE" -no-boot-anim -avd lollypop -prop monodroid.avdname=lollypop
exit
) else (
pause
)
install.bat:
"C:\Program Files\Android\android-sdk\platform-tools\adb.exe" wait-for-device
"C:\Program Files\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\msbuild.exe" -r /t:Install /v:m "E:\App2\App2.Android\App2.Android.csproj" /p:AdbTarget=-e
#echo off
if %ERRORLEVEL% == 0 exit else pause

visual studio 2017: open AND build solution from a command line call

In visual studio 2017 (or probably any version) I want to open a solution in the IDE and start it building from the command line. IF the solution is already open then I just want it to start building (in the IDE).
So I can open the solution like this:
devenv solution.sln
Or I can build it like this:
devenv /build solution.sln
There is also this:
devenv solution.sln /command ...
But the documentation on what "commands" there are is very difficult to find out about... The example is some user made macro, but I assume there are other built in commands? - this may help...?
But I am not sure how to:
Open a solution in the IDE and have it build straight away
If its already open just get it to start building.
Is there some way to do this?
My use case is to kick off the build from within IBM Rhapsody. In MSVS2012 it supported a Rhapsody addin which did these tasks... but addins have been deprecated since 2013 so I can get Rhapsody to do what I want by re-writing its make file content - the makefile will just call a batch file script which will do the commands that I am trying to do in this question - and then Rhapsody plugin done :)
Actually note that:
devenv solution.sln only opens a solution in a new VS IDE instance.
devenv /build solution.sln only builds projects that have changed since the last build without opening VS IDE. To build all projects in a solution, use /rebuild instead.
So, if you want to
Open a solution in the IDE and have it build straight away
you should run the two commands consecutively:
devenv solution.sln
devenv /rebuild solution.sln
Then at the second time use /rebuild only to avoid opening a new VS instance.
Update: you can make a .cmd or .bat file contains the following:
tasklist /fi "imagename eq devenv.exe" /v | find /i "solution" 2>NUL
if "%ERRORLEVEL%"=="0" goto solution_is_running
if "%ERRORLEVEL%"=="1" goto solution_is_closed
:solution_is_running
devenv /rebuild solution.sln
goto:eof
:solution_is_closed
devenv solution.sln
devenv /rebuild solution.sln
goto:eof
Or use:
Taskkill /IM devenv.exe /F
devenv solution.sln
devenv /rebuild solution.sln
devenv solution.sln /Command "Build.RebuildSolution"

Mysterious batch file

While searching through my file directories, I came across a batch file that I had not created. While running the program, (after safety removing all external devices and logging out of all possible accounts) A CMD box would open for a brief second and then close. Upon editing the code so the CMD box would pause, I discovered what its doing.
Below is the original text from the batch file:
PUSHD .
cd %windir%\system32
#setlocal
#set logfile="%temp%\bcm_vc80redist.log"
#echo [%date% %time%] START vcredist_x86.exe /q:a /r:i /c:"msiexec /i vcredist.msi /qn" >> %logfile%
vcredist_x86.exe /q:a /r:i /c:"msiexec /i vcredist.msi /qn"
#if errorlevel 0 echo vcredist_x86.exe exited with errorlevel=%errorlevel% >> %logfile%
#echo [%date% %time%] END vcredist_x86.exe /q:a /r:i /c:"msiexec /i vcredist.msi /qn" >> %logfile%
#endlocal
POPD
{Pause}--This isnt part of the original code, this is added to stop the CMD box from closing.
It appears that some point you installed a "Visual C++ Redistributable Package" and the batch file is a leftover from the installation.
You can look in %temp%\bcm_vc80redist.log to see exactly what was installed.
Source Developer Tools
Visual C++ Redistributable Packages for Visual Studio 2013
The Visual C++ Redistributable Packages install run-time components
that are required to run C++ applications that are built by using
Visual Studio 2013.
The Visual C++ Redistributable Packages install run-time components
that are required to run applications that are developed by using
Visual Studio 2013, on computers that don't have Visual Studio 2013
installed. These packages install run-time components of these
libraries: C Runtime (CRT), Standard C++, ATL, MFC, C++ AMP, and
OpenMP.

How to get cmd line build command for VS solution?

This is probably easy but I am getting stuck: when I build a solution in Visual Studio - how do extract the exact cmd line for the current build command in order to be able to do the same build from VisualStudio console?
In the output window I can see the single projects in the solution build commands but not the one for the whole solution.
I am on VS2005.
Any help would be appreciated
Navigate to your Programs menu > Microsoft Visual Studio 2005 > Visual Studio Tools > Visual Studio 2005 Command Prompt.
this command prompt has all the necessary .NET environment variables set for the the command line session. You can change directory to your solution directory (e.g. c:\projects\mySolution) and run
Msbuild.exe mySolution.sln
You can see the various options available using msbuild /?
Msbuild is located at C:\Windows\Microsoft.NET\Framework\v2.0.50727
On top of msbuild /? quick-option check, you may reference the MSBuild Command Line Reference page for more explanations on its usage. And how to build specific targets in solutions.
In addition to what #JohnIdol says correctly, I've found that you need to setup a number VS environment variables. I don't have the name of the batch file in front of me, but you can modify or 'I think' use it. It is in VS program files tree somewhere. Also, as I remember you don't want to be in a standard shell but a .NET setup shell for some paths and such. I'll add details later when I'm at a Windows PC with VS.
EDIT: The batch file mentioned is a shortcut in ProgFiles menu. Here is the details of its properties.
%comspec% /k ""C:\Program Files\Microsoft Visual Studio 8\VC\vcvarsall.bat""x86"
Here is my batch file, using MSBuild to call the solution.
#echo off
:: setup VS2005 command line build environment
set VSINSTALLDIR=C:\Program Files\Microsoft Visual Studio 8
set VCINSTALLDIR=C:\Program Files\Microsoft Visual Studio 8\VC
set FrameworkDir=C:\WINDOWS\Microsoft.NET\Framework
set FrameworkVersion=v2.0.50727
set FrameworkSDKDir=C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0
set DevEnvDir=C:\Program Files\Microsoft Visual Studio 8\Common7\IDE
set PATH=C:\Program Files\Microsoft Visual Studio 8\Common7\IDE;C:\Program Files\Microsoft Visual Studio 8\VC\BIN;C:\Program Files\Microsoft Visual Studio 8\Com
mon7\Tools;C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\bin;C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\bin;C:\Program Files\Microsoft
Visual Studio 8\SDK\v2.0\bin;C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727;C:\Program Files\Microsoft Visual Studio 8\VC\VCPackages;%PATH%
set INCLUDE=C:\Program Files\Microsoft Visual Studio 8\VC\ATLMFC\INCLUDE;C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE;C:\Program Files\Microsoft Visual
Studio 8\VC\PlatformSDK\include;C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\include;%INCLUDE%
set LIB=C:\Program Files\Microsoft Visual Studio 8\VC\ATLMFC\LIB;C:\Program Files\Microsoft Visual Studio 8\VC\LIB;C:\Program Files\Microsoft Visual Studio 8\VC
\PlatformSDK\lib;C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\lib;%LIB%
set LIBPATH=C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727;C:\Program Files\Microsoft Visual Studio 8\VC\ATLMFC\LIB
echo %0 %*
echo %0 %* >> %MrB-LOG%
cd
if not ""=="%~dp1" pushd %~dp1
cd
if exist %~nx1 (
echo VS2005 build of '%~nx1'.
echo VS2005 build of '%~nx1'. >> %MrB-LOG%
set MrB-BUILDLOG=%MrB-BASE%\%MrB-WORK%.%MrB-NICEDATE%.%MrB-NICETIME%.build-errors.log
msbuild.exe %~nx1 /t:Rebuild /p:Configuration=Release > %MrB-BUILDLOG%
findstr /r /c:"[1-9][0-9]* Error(s)" %MrB-BUILDLOG%
if not errorlevel 1 (
echo ERROR: sending notification email for build errors in '%~nx1'.
echo ERROR: sending notification email for build errors in '%~nx1'. >> %MrB-LOG%
call mrb-email "Mr Build isn't happy about build errors in '%~nx1'" %MrB-BUILDLOG%
) else (
findstr /r /c:"[1-9][0-9]* Warning(s)" %MrB-BUILDLOG%
if not errorlevel 1 (
echo ERROR: sending notification email for build warnings in '%~nx1'.
echo ERROR: sending notification email for build warnings in '%~nx1'. >> %MrB-LOG%
call mrb-email "Mr Build isn't happy about build warnings in '%~nx1'" %MrB-BUILDLOG%
) else (
echo Successful build of '%~nx1'.
echo Successful build of '%~nx1'. >> %MrB-LOG%
)
)
) else (
echo ERROR '%1' doesn't exist.
echo ERROR '%1' doesn't exist. >> %MrB-LOG%
)
popd
For VS .NET 2003 you can use devenv.exe to build the solution/project from command line.
devenv solutionfile.sln /build solutionconfig
E.g. usage in batch file:
call "C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Tools\vsvars32.bat"
devenv Tools.sln /build "Release"
If you want to see the exact command line as provided by VS (rather than work it out) then you could try replacing the MSBuild.exe with your own console app that prints out all the parameters to a file.
You could also write out all the environment variables supplied to check which ones VS provides in the background.
I just want to thank the Bejoy on the example. I had big problems with solution rebuild on setup pre build event because they removed most of the macros and this helped me a lot.
Here is my solution based on Bejoy's (considering that .bat file is located in setup root folder which is part of solution):
call "%VS100COMNTOOLS%\vsvars32.bat"
devenv "%CD%\..\soulutionfile.sln" /build "Release"
You can start msbuild from the command line. msbuild understands .sln (solution) files. You can specify the .sln file and the build configuration (debug, release etc.) from the command line.
http://msdn.microsoft.com/en-us/library/ms164311.aspx
Here is the documentation on what you can do with msbuild. MSBuild.exe is installed with the .net framework, not with visual studio. You can find it in c:\windows\microsoft.net\framework\v3.5 (or v2.0.50727)
I searched a bit and found that you can also do a command line build with visual studio using devenv.exe /build, more info here:
http://msdn.microsoft.com/en-us/library/xee0c8y7(VS.80).aspx

visual studio post-build script error calling gacutil

I'm on vista, with VS2005 running as admin. Both vs2005 and vs2008 are installed. If I explicitly use the path to gacutil, it works, but not if I only call gacutil like this:
if NOT $(ConfigurationName) == Release gacutil /f /i "$(TargetPath)"
I would just update the post-build script, but I've been asked to leave it alone. It's a project for a sister company. The path to gacutil is in Visual Studio environment variables, so it should be able to find it. (Tools -> Options -> Projects and Solutions -> VC++ Directories)
Is there some way to tweak visual studio or windows environment variable to get the above post-build script to work? I need to it to build, because it's a dependency of a project I need to code.
TIA, -j
As you suggest, tweaking the Windows PATH environment variable should get things working.
append the directory that contains GACUTIL to the end of the PATH. For example, using the location on my computer, append (without the quotes) ";C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin" to the existing value of the PATH system environment variable.
Restart Visual Studio to pick up the new value.
Dave

Resources