MSVC Projects, Source Control and Build Events

I think it is a good idea to move all the lines of events (e.g. post-build events) into a separate batch file.

Therefore, in projectX, instead of having the following in a post-build step for a particular configuration:

echo copy *.exe to d
copy "$(OutDir)"*.exe "$(SolutionDir)dist"

appears in the *.vcproj file as (all on one line)

CommandLine="echo copy *.exe to dist&‡x0
    D;&‡x0A;copy "$(OutDir)"¦*
    .exe "¦$(SolutionDir)dist"

(but, replace each ‡ by a #; WordPress seems unable to retain the quoted new-line and/or line-feed entities. It also occasionally forgets backslashes, so I’ve replaced those with a broken pipe: ¦.)

just have this:

call projectX.vcproj.PostBuildEvent.bat "$(OutDir)" "$(SolutionDir)"

with the batch file (named, for example, “projectX.vcproj.PostBuildEvent.bat”) containing something like this:

@echo off
REM *** %1 = $(OutDir) ***
REM *** %2 = $(SolutionDir) ***
echo copy *.exe to dist
copy "%~1"*.exe "%~2dist"

[Sometimes you will need to pass the configuration name &c. as an argument, when the steps differ between configurations, i.e. “Release” versus “Debug”]

Why do this?

  1. With a single batch file it may be easier to keep different configurations in sync;
  2. It’s easier to compare configurations;
  3. It’s easier to copy-and-paste to, and compare with, other projects;
  4. It’s easier to view changes in SVN differences;
  5. The batch file less likely to result in unresolved conflicts upon an SVN merge or update, and
  6. easier to resolve when it does;
  7. The event’s steps may be run separately;
  8. Any conditional indentation is preserved; I’ve noticed that MSDEV sometimes loses it.

It would probably be best to cater for missing arguments:

if "%~2" == "" (
  echo missing arguments (exiting / defaulting)
  rem exit 1
  call "%~0" "Release" "."
) else (

Also, perhaps such files should make use of errorlevels…

copy "%~1"*.exe "%~2dist" || exit 1

It might also we worth setting some variables:

@echo off
set OutDir=%~1
set SolutionDir=%~2
echo copy *.exe to dist
copy "%OutDir%"*.exe "%SolutionDir%dist"

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: