Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 806
Philipp Avatar asked Jan 31 '12 04:01

Philipp


People also ask

How do I build CMake in Visual Studio?

Building CMake projects Select the preferred configuration and press F5, or choose the Run (green triangle) button on the toolbar. The project automatically builds first, just like a Visual Studio solution. Right-click on CMakeLists. txt in Solution Explorer and select Build from the context menu.

Does CMake work with C#?

C# CMake learned to support CSharp (C#) as a first-class language that can be enabled via the project() and enable_language() commands. It is currently supported by the Visual Studio Generators for VS 2010 and above.

How do I open CMake GUI in Visual Studio?

Running CMake for Windows / Microsoft Visual C++ (MSVC)Run cmake-gui.exe, which should be in your Start menu under Program Files, there may also be a shortcut on your desktop, or if you built from source, it will be in the build directory.

What is a build step?

A custom build step is a build rule associated with a project. A custom build step can specify a command line to execute, any additional input or output files, and a message to display.


2 Answers

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.

like image 150
tpg2114 Avatar answered Oct 13 '22 11:10

tpg2114


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.

like image 29
gustaf r Avatar answered Oct 13 '22 12:10

gustaf r