Okay you got me curious so I did some further digging and testing.
Michael's example works because the generated header is listed as a source
file in the add_executable() call. Therefore CMake automatically sets up
the required dependency for you. If you don't want to do that, two other
approaches also achieve your end goal. The wording of the OBJECT_DEPENDS
source property documentation gives a clue for one method, although it's
easy to miss (emphasis added by me):

This property need not be used to specify the dependency of a source file
on a generated header file that it includes. Although the property was
originally introduced for this purpose, it is no longer necessary. If the
generated header file is created by a custom command *in the same target as
the source file*, the automatic dependency scanning process will recognize
the dependency. If the generated header file is created *by another target*,
an inter-target dependency should be created with the add_dependencies()
command (if one does not already exist due to linking relationships).


As far as I can tell, the only way to create a custom command in the same
target and have that custom command execute before the source file compiles
is to use a PRE_BUILD custom command, but these are only supported with the
Visual Studio generator and therefore are not so useful. Instead, you can
create another target with add_custom_target() which depends on the
generated header and then use add_dependencies() to specify the executable
target depends on the new custom target, something like this:

    cmake_minimum_required(VERSION 3.0)
    project(simple)

    add_custom_command(
        OUTPUT           ${CMAKE_BINARY_DIR}/tab.h
        COMMAND "mktab > ${CMAKE_BINARY_DIR}/tab.h"
    )
    add_custom_target(gentab DEPENDS ${CMAKE_BINARY_DIR}/tab.h)

    include_directories(${CMAKE_BINARY_DIR})

    add_executable(foo foo.c)
    add_dependencies(foo gentab)

Another choice is to set the OBJECT_DEPENDS property on foo.c, even though
the documentation for that source property says it shouldn't be needed. The
OBJECT_DEPENDS property sets up a dependency between files rather than
targets and can be used like this:

    cmake_minimum_required(VERSION 3.0)
    project(simple)

    add_custom_command(
        OUTPUT           ${CMAKE_BINARY_DIR}/tab.h
        COMMAND "mktab > ${CMAKE_BINARY_DIR}/tab.h"
    )

    include_directories(${CMAKE_BINARY_DIR})

    add_executable(foo foo.c)
    set_source_files_properties(foo.c PROPERTIES OBJECT_DEPENDS
${CMAKE_BINARY_DIR}/tab.h)

Either approach or Michael's would seem to achieve the goal of your
original question.




On Sat, May 20, 2017 at 9:10 AM, Michael Ellery <mellery...@gmail.com>
wrote:

>
> > On May 19, 2017, at 3:48 PM, Urs Thuermann <u...@isnogud.escape.de>
> wrote:
> >
> > Craig Scott <craig.sc...@crascit.com> writes:
> >
> >> A bit of a long-shot, have you tried generating the file with the
> extension
> >> .h instead of .c? That might allow it to be picked up by the dependency
> >> scanner (I don't know if it treats file extensions differently). Also,
> the
> >> add_custom_command(OUTPUT...) call needs to be in the same
> >> CMakeLists.txt
> >
> > No, I have tried that before in several variations, and nothing
> > worked.
> >
>
>
> Here:
>
> https://github.com/mellery451/gen_header
>
> works for with me with makefile generator.
>
> -Mike
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at http://www.kitware.com/
> opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake
>



-- 
Craig Scott
Melbourne, Australia
https://crascit.com
-- 

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more 
information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake

Reply via email to