CMake: How to run a add_custom_command before everything else

cmake

Solution

You should use `add_custom_target` instead and `add_dependencies` to make your normal target depend on it:

add_custom_target(
    myCustomTarget
    COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/mk_config_h.py ${CMAKE_CURRENT_BINARY_DIR}/config.h
)
add_dependencies(myTarget myCustomTarget)

This should ensure that the command is run before compiling the sources of `myTarget`.

Problem

I have a custom command ``` add_custom_command( OUTPUT config.h PRE_BUILD COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/mk_config_h.py ${CMAKE_CURRENT_BINARY_DIR}/config.h ) ``` I'm trying to run it before everything else and I generate unix Makefiles. However PRE_BUILD is only supported for VS2010 which means that `config.h` is build before linking. how do I make a custom command before cmake starts compiling sources.

Original source

Related problems