CMake run custom command before build?
<p>Example source of a binary I want to run before each build, once per <code>add_executable</code>:</p>

<pre><code>#include &lt;stdio.h&gt;

int main(int argc, char *argv[]) {
    for(int i=0; i&lt;argc; ++i)
        printf("argv[%d] = %s\n", i, argv[i]);
    fclose(fopen("foo.hh", "a"));
}
</code></pre>

<p>CMakeLists.txt</p>

<pre><code>cmake_minimum_required(VERSION 3.5)
project(foo_proj)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
set(SOURCE_FILES main.cpp)
# ---- this line changes ----
add_executable(foo_proj ${SOURCE_FILES})
</code></pre>

<p>Attempts:</p>

<pre><code>add_custom_target(create_foo_hh COMMAND /tmp/bin/create_foo_hh)
add_dependencies(${SOURCE_FILES} create_foo_hh)
</code></pre>

<blockquote>
  <p>Error:Cannot add target-level dependencies to non-existent target "main.cpp".
  The add_dependencies works for top-level logical targets created by the add_executable, add_library, or add_custom_target commands.  If you want to add file-level dependencies see the DEPENDS option of the add_custom_target and add_custom_command commands.</p>
</blockquote>

<pre><code>execute_process(COMMAND /tmp/bin/create_foo_hh main.cpp)
</code></pre>

<blockquote>
  <p>No error, but foo.hh isn't created.</p>
</blockquote>

<p>How do I automate the running of this command?</p>