Philipp
Philipp

Reputation: 11813

Create Visual Studio "Custom Build Step" with CMake

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?

Upvotes: 6

Views: 5583

Answers (2)

tpg2114
tpg2114

Reputation: 15100

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.

Upvotes: 7

gustaf r
gustaf r

Reputation: 1234

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.

Upvotes: 1

Related Questions