Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Simple Build Script for Visual Studio from the Command Line?

I have a lot of Visual Studio Project Solutions in multiple directories (all with the extension .sln) and I want to write a simple batch script that will automatically build all solutions listed in the batch file.

I am able to manually build a solution by starting up the Visual Studio Command Prompt (which is just a command-line instance with the following command executed

"%comspec%" /k "C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86

After which I then build the project by calling:

devenv "path\to\solutionFile\projectSolution1.sln" /build Debug

This will build the project (assuming the project does not have errors) and I rinse and repeat for each project I want built.

However when I have the following in a batch file called build.bat:

"%comspec%" /k "C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86  
echo "Starting Build for all Projects with proposed changes" 
echo . 
devenv "path\to\solutionFile\projectSolution2.sln" /build Debug
devenv "another\path\to\solutionFile\projectSolution3.sln" /build Debug
devenv "yet\another\path\to\solutionFile\projectSolution4.sln" /build Debug
echo "All builds completed."
pause

The batch script only executes the first line, and waits until I type in exit before executing the others. My understanding of this based on the research I have done on batch files and all the StackOverflow questions is that cmd actually invokes another instance of itself that executes vcvarsall.bat to set up the build environment.

This will not work as typing exit kills that instance with devenv set up and the commands after that cannot execute as devenv is not a recognized command (since the exported path will not exist anymore)

In short, how can this be achieved (passing in the rest of the commands to the cmd instance with devenv defined) in a single batch file? I understand this isn't a robust way (and there are a lot of tools that do this) of invoking builds but I'm just hoping to have one batch script to automate the manual work of individually calling these projects.

like image 423
matrixanomaly Avatar asked May 26 '15 16:05

matrixanomaly


2 Answers

Found the solution, as noted by Jimmy, one needs to remove the environment variable %comspec% as that is a shortcut to CMD.exe.

However, just removing "%comspec" /k will cause a CMD instance to open and then exit after a second. I also previously tried the call function which created a separate CMD instance when used with %comspec%

The solution is to add call in front of the first line, and remove %comspec

Here is the final batch file that got things working as intended.

@echo OFF 
call "C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
echo "Starting Build for all Projects with proposed changes"
echo .  
devenv "path\to\solutionFile\projectSolution2.sln" /build Debug 
devenv "another\path\to\solutionFile\projectSolution3.sln" /build Debug 
devenv "yet\another\path\to\solutionFile\projectSolution4.sln" /build Debug 
echo . 
echo "All builds completed." 
pause

Note that @echo OFF tells the batch script to not echo out the commands (such as that call command) into the terminal (errors and warnings will still be shown, however)

like image 90
matrixanomaly Avatar answered Nov 03 '22 12:11

matrixanomaly


If this is already in a batch script, then this line:

"%comspec%" /k "C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86  

Probably should just be "C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86

Why? %comspec% is just an environment variable shortcut to cmd.exe, so as you saw, it's launching a new instance of cmd with the /k option specified (which if you run cmd /? says Carries out the command specified by string but remains). You don't care about it remaining, you don't even want a new cmd.exe when you're already running your batch file.

like image 2
Jimmy Avatar answered Nov 03 '22 11:11

Jimmy