6

I would like to run a code generator every time my project is built in Visual Studio, even if no source file in the project was changed. Therefore I would like to have a custom build step set up as explained in Visual Studio: Run C++ project Post-Build Event even if project is up-to-date.

How can I create such a build step with CMake?

Community
  • 1
  • 1
Philipp
  • 11,549
  • 8
  • 66
  • 126

2 Answers2

7

I think a custom target is what you are looking for: add_custom_target

From the documentation:

Add a target with no output so it will always be built.

Or if you are generating a code file,

https://cmake.org/cmake/help/v2.8.8/cmake.html#command:add_custom_target

can be run POST_BUILD and generate output.

AzP
  • 1,081
  • 1
  • 16
  • 27
tpg2114
  • 14,112
  • 6
  • 42
  • 57
  • Thanks! Do you know if there is a possibility to fail the Visual Studio build if my custom target (I chose option 1) fails? The return code seems to have no effect? – Philipp Jan 31 '12 at 08:27
  • I don't know for sure, I use a Linux system. But on my machine, if the command used in the target returns non-zero, the build fails and if it returns zero is works. So it could be a VS+CMake issue, or it could be a problem with the version of CMake you are using. I don't own any Windows machines to try! – tpg2114 Jan 31 '12 at 11:02
1

This is afaik not possible with CMake, and is therefore a missing feature for sure.

The answer from Tarydon in the question you refer to, is about setting up precisely what you want - a "Custom Build Step". This means that you still only have your main target (VS Project), with something that looks like a "Post-Build Event" but technically isn't, since Post-Build Events aren't run if the project is up-to-date.

The answer from tpg2114 works, but has one major drawback; it spams your solution with phony projects. In case you have a hundred projects in a solution, having to add another hundred just as post-build wrappers to the first hundred is of course undesirable.

Depending on your situation, it might sometimes be easier to use Post-Build Events and force a rebuild of at least one source file, for the project to actually build and therefore also run your custom command.

gustaf r
  • 1,224
  • 9
  • 14