Search code examples
visual-studio-2010batch-filemsbuildmakefilenmake

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?


Solution

  • Stijn put me on the right track: the final solution was manually to add the following section right at the end of my .vcxproj file (immediately before the </Project> which formed the very last line):

      <Target Name="OnCleanOnly" AfterTargets="Clean">
        <Exec Command="call &quot;C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat&quot; x86
                       cd ..\..\..\zlib-1.2.8
                       nmake /f win32\Makefile.msc clean
                       cd ..\tiff-4.0.3
                       nmake /f Makefile.vc clean" />
        <Message Text="Cleaning libraries" Importance="High" />
      </Target>
    

    This ensures, as required, that the 'nmake clean' commands are run every time either 'Clean Solution' or 'Rebuild Solution' are selected from the 'Build' menu. This is precisely what I was hoping to achieve.