VS 2010 - Always execute pre-build event - visual-studio-2010

In my solution I have a project with an attached pre-build event to check if certain files need to be regenerated. This all works fine if something changed in the project (or dependent projects). However, if nothing changed the pre-build event is not triggered.
Is there a way to run the pre-build event everytime VS tries to build a project (even if it is clean from a code file standpoint)?
Alternatively, is there any other way to run a script first before building a project so that I can regenerate my files first?

For a Visual C++ project you may specify Command Line in the Custom build step (Configuration Properties - Custom build step) and leave the field Outputs empty. It will be executed even if the project has no changes.
Here is the similar question.

Related

Visual Studio 2010 custom compile using batch file

I have recently converted a mid-sized Visual Studio 2005 solution to Visual Studio 2010.
One of the projects contains files which are not C/C++ files and are compiled using a batch file running a custom build tool. The output of the custom build step is some C++ files, which must be compiled after that.
The output of the custom build step in the properties for the relevant files is correctly set to the generated C++ files.
The problem is that sometimes VS2010 tries to compile the generated C++ files before the files with the custom build step, which means in a clean build it fails to find the C++ files and fails. If I try building several times eventually it would compile the custom files and then the build will succeed, but this is obviously not a good solution for automated build.
In VS2005 there is no problem building this project, but VS2010 fails to determine the correct compile order from the outputs of the custom build step. Is there another way to force correct compile order in VS2010?
Visual Studio supports parallel builds, it can build more than one project at the same time. This will not work properly if it cannot properly see the dependencies between projects. A custom build can certainly be a troublemaker here. The number of parallel builds is configurable, setting it to 1 would be a very crude but effective workaround. Tools + Options, Projects and Solutions, Build and Run, "maximum number of parallel project builds" setting.
But don't do that, parallel builds can be a huge time saver. You fix this kind of problem by setting project dependencies explicitly. Right-click the project that uses the generated C++ files in the Solution Explorer window and click Project Dependencies. Tick the check box for the project that produces the C++ files. In case it is relevant to other readers, check this answer for a way to create a project that only does the custom build step and nothing else.
Visual Studio 2008 by default executes custom build tools first. The order can be changed via right click menu on project with command "Tool Build Order". This facility is not available in Visual Studio 2010. I do not know of a work-around.
Consider using Visual Studio 2010's "Properties >> Configuration Properties >> Build Events >> Pre-Build Event" as the place where you should issue command(s) to build source files that must be compiled first. In the Command Line field, call upon the cl.exe compiler or use a small external makefile to compile those files. Visual Studio will then compile the rest of your project following this initial step.
I resolved my problem when I noticed that my custom build step runs for only one file at a time. It runs for the next file on the next build etc.
The reason apparently is that my custom build steps are calling a batch file and VS2010 creates one temporary batch file to execute all custom build files.
The solution was pointed in this discussion:
http://social.msdn.microsoft.com/Forums/en-HK/msbuild/thread/ca392036-ebdb-4812-930e-f90aa445cca5
It is simply to prefix all calls to batch files with a "call" statement, thus not terminating the execution of the master batch file prematurely.

How to execute a command after every build in Visual Studio 2012?

I want to execute a postbuild command after every build, no matter what project in my solution has changed.
To accomplish this in Visual Studio 2010, I made one empty project depend on every other project, so it always got built, executing its postbuild command. However, Visual Studio 2012 (RC) appears to recognize that the dependency is not real, and will not build this empty project automatically.
I do not see any way to specify a solution postbuild script in solution settings. How can I accomplish this?
I have solved this issue by making my post-build script also delete the output (bin**) of this "empty" project, forcing a rebuild of this project every time. It appears to work satisfactorily so far.
This works (for details see original post):
you may need to change security settings (at your own risk) e.g.: https://stackoverflow.com/a/60284384/2705777

How to force Visual Studio to always rebuild another C# project when running a test project?

I have a project that does some IL injection into an assembly. To test this code I have two projects:
A project that generates an assembly to be injected into.
An MS test project that does the injection and then verifies that injection has occurred.
The problem is that project 1 needs to be rebuilt each time the tests are run so that the assembly is clean.
I've tried
How do I force a rebuild when the project configuration changes?.
Cleaning the project in a pre-build event.
But neither of these approaches work. The only thing that does work that I've found is overwriting a dummy text file in project 1, but this is not acceptable as the IDE opens a message box asking to reload it.
Here are two suggestions to try:
Try cleaning project 1 as a post-build event of the test project. That assumes that you won't need to examine the assembly when the test fails.
Add a pre-build event to your test project that calls devenv with /rebuild on project 1.

Visual Studio pre-build action to skip project build based on condition

In a VS 2008 solution, I have several projects. I run the build on the entire solution with msbuild. Based on the existence of a file on the disk, I want to skip the build for one of the projects. How can I achieve this? Is there a way to use a prebuild action to skip the project build?
PreBuildEvent is used to call custom script in OutDir. There is no special flags to skip building. I know several ways to hack build system and get needed behaviour, but it's not cosher.
1) First of all you can create two solutions (with and without that project). On top of that you can easily create a script which will check existence of the file and call MSBuild to build one or another.
2) You can remove the project from sln, add new simple project (let's say dll) and check existence of the file in BeforeBuild action and call MSBuild to build your custom project. Or you can add this behaviour to the one of the projects in that sln.

Visual Studio 2008 Post Build event -- only run on Rebuild

In Visual Studio 2008 we run a post build event which calls NANT and in turn creates our config files.
e.g.
if $(SolutionDir) == . GOTO end
nant -buildfile:$(SolutionDir)default.build create..web.config
Is there a way to run this only on ReBuild?
I think you can do this by specifying build targets rather than using build events. Try creating an AfterClean target to delete your generated config files, and a BeforeBuild target to create them. Make sure you set up the file dependency for the BeforeBuild target, so msbuild knows it should only run the step if the file isn't present.
I haven't actually tried this, but I beleive msbuild will only run the target if the target files don't exist. When you rebuild, the cleaning process will be invoked, and in turn your AfterClean target.
You can read more about build targets here. The only real downside to using build targets instead of events is that they are not visible anywhere in the VS UI - you will only find them if you inspect the project file.
There may be a more direct solution involving events - have look at the msbuild team blog here.

Resources