Requisits:
Visual Studio 2010
The 2.8.8 Windows version of cmake
a Windows tcl version(tcl 8.6.0.0)

1 - unzip the PreLinkEvent.zip file in a directory.
2 - cd src
3 - Update the build_env.bat batch 
    * Update path variable according to the installation path of cmake and tcl
    * SOURCE_DIR variable must be set to the full path of the src directory
	* BUILD_DIR to the "build" directory located at the same lavel as src
3 - run build_env.bat in a cmd.exe console
4 - run configure.bat in the same console

The solution "versioning.sln" is generated in the "build" directory
This solution can be opened in Visual studio IDE, using the devenv command: devenv versioning.sln and compile from the IDE (ALL_BUILD target)
The solution can also be compiled with one of the two Windows batchs:
* src/link_with_devenv.bat
* test_program_version.obj

Results:
If I link the 'test_program.exe' using the Visual Studio 10 IDE or the src/link_with_devenv.bat, the compilation/link run as expected, that is:
The first time:
The test_program_version.obj is generated by the versioning.tcl tcl scripts (generation of a temporary c file in the build directory and compilation of it 
to generate the test_program_version.obj) run by a custom command fired in a PRE_LINK step.
The .obj file is added to the link command of the test_program.exe (See the src/CMakeLists.txt)
The test_program.exe is linked.

If we run again the link, nothing is recompiled/linked as no changes have been done. 
All is working as described in the documentation of the "add_custom_command": "the [custom] command becomes part of the target and will be only executed when
the target itself is build. If the target is already built, the command will not execute."

Now if we link the test_program.exe using the src/link_with_msbuild.bat, the test_program_version.obj is generated each time, what means that 
the test_program target link is always considered as out of date, even if changes have been done.
See the build\msbuild.log log file we get if we redirect the output of src/link_with_msbuild.bat (uncomment the commented msbuild.exe command
and comment the second one in src/link_with_msbuild.bat batch).

When we build a target from the Visual Studio 10 or using the src/link_with_devenv.bat, we can see in the task manager that a msbuild.exe command is
launched to compile/link the executable.
So why does the add_custom_command with PRE_LINK argument works as expected in one case (when devenv is used) and not in the other (whem msbuild.exe is directly called).

I run compilation of a solution containing about 200 projects using a Windows batch calling msbuild.exe, each night.
The products must be versioned (using the same mechanism as the one used in my small test case) and as a consequence, this bug leads to the link of all products
even if no changes have been performed.


 

