Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a Pre-Build Event only when Rebuilding

I'm compiling a project which depends on a couple of other open-source projects (specifically, Zlib and LibTIFF), for which Windows makefiles (intended for use with nmake) are provided. I'd like to arrange things such that, whenever I perform a 'Rebuild' on my project, the open-source projects are also rebuilt with the appropriate configuration (Debug/Release, 32/64 bit etc.).

My own project is fairly straightforward but not trivial. I squandered my first few days by trying to figure out how to generate nmake makefiles of my own, either directly or using CMake. Despite having a reasonable amount of experience with GNU make, this caused me to lose the will to live. After coming out of rehab, I decided to stick with a Visual Studio project file for building my own project, and to define a 'Pre-Build event' to execute nmake on the supplier-provided makefiles for my open source libraries.

For example, in my Visual Studio 2010 project page for a 32-bit Release build, using the Intel C++ compiler, I write my Pre-Build Event command line as:

call "C:\Program Files (x86)\Intel\Composer XE 2013 SP1\bin\ipsxe-comp-vars.bat" ia32 vs2010
cd ..\..\..\zlib-1.2.8
nmake /f win32\Makefile.msc zlib.lib
cd ..\tiff-4.0.3
nmake /f Makefile.vc lib

(I realise I could just as well put the above into a batch file, and call the batch file, but I don't want to give myself another file to keep track of). Anyway, this works great, except for one thing: if I decide to change my configuration, I'd like to execute an 'nmake clean' on the two libraries to ensure that they're rebuilt with my new configuration, something along the lines of:

call "C:\Program Files (x86)\Intel\Composer XE 2013 SP1\bin\ipsxe-comp-vars.bat" ia32 vs2010
cd ..\..\..\zlib-1.2.8
IF "%BUILDACTION%"=="REBUILD" nmake /f win32\Makefile.msc clean
nmake /f win32\Makefile.msc zlib.lib
cd ..\tiff-4.0.3
IF "%BUILDACTION%"=="REBUILD" nmake /f Makefile.vc clean
nmake /f Makefile.vc lib

Unfortunately, I can't seem to identify any environment variable which satisifes the role of BUILDACTION here, nor can I see a straightforward way of defining one. So, is there any way of doing what I'm trying to do here, or am I on a hiding to nothing?

like image 553
Eos Pengwern Avatar asked Dec 20 '22 14:12

Eos Pengwern


1 Answers

Once you realise the Rebuild target is just a stub and actually consists of calling Clean follwed by Build (just like in a typical gnu makefile btw), the solution presents itself: use two seperate targets, one for Clean and one for Build. You can leave the pre-build event as-is, and add one for Clean. Open the project file in a text editor and add something like this at the end:

<Target Name="OnCleanOnly" AfterTargets="Clean">
  <Message Text="OnCleanOnly" Importance="High"/>
</Target>

This target will be called automatically when the Clean target is called. Which is the case when you do Clean or Rebuild from within VS, or by calling msbuild myproject /t:Rebuild on the command line for instance.

Note, you say I realise I could just as well put the above into a batch file, and call the batch file, but I don't want to give myself another file to keep track of but I wouldn't dismiss that idea so fast, as it does have the main advantage that you can test if the build event itself is ok simply by running the batch file. If you want to do the same for a target embedded in a project file, you have to fire up a command line, figure out which target to call exactly and run msbuild projectfile /t:target to test it. Much more work :) Also keeping track of files is the job of your versioning system so you shouldn't have to worry about it.

like image 157
stijn Avatar answered Jan 05 '23 03:01

stijn