Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
874 views
in Technique[技术] by (71.8m points)

batch file - 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 FilesMicrosoft Visual Studio 10.0VCvcvarsall.bat" x86

After which I then build the project by calling:

devenv "pathosolutionFileprojectSolution1.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 FilesMicrosoft Visual Studio 10.0VCvcvarsall.bat" x86  
echo "Starting Build for all Projects with proposed changes" 
echo . 
devenv "pathosolutionFileprojectSolution2.sln" /build Debug
devenv "anotherpathosolutionFileprojectSolution3.sln" /build Debug
devenv "yetanotherpathosolutionFileprojectSolution4.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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 FilesMicrosoft Visual Studio 10.0VCvcvarsall.bat" x86
echo "Starting Build for all Projects with proposed changes"
echo .  
devenv "pathosolutionFileprojectSolution2.sln" /build Debug 
devenv "anotherpathosolutionFileprojectSolution3.sln" /build Debug 
devenv "yetanotherpathosolutionFileprojectSolution4.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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...