mkmostafa
mkmostafa

Reputation: 3171

CMake custom command triggered if and only if the file is called and not exist

Basically what I want to accomplish is to have a dependancy between a library and a custom command to generate SRC if SRC files are called (to be compiled for example.) but if they are called afterwards and were already generated, I don't want the Gen target to be triggered again. This is what I have but it's giving and error that SRC files are not found which is true as they are not generated yet!

add_custom_command(
    TARGET Gen
    COMMAND gen ${FILES} -o SRC
    )

add_library(OBJS OBJECT ${SRC})

add_dependencies(OBJS Gen)

Upvotes: 1

Views: 1169

Answers (1)

Guillaume
Guillaume

Reputation: 10961

To do this, you need to:

  • Add a custom command to generate your file(s)
  • Add a custom target that depends on this/those file(s)
  • Then add a dependency between your library and this new target.

Something like this:

add_custom_command(
    OUTPUT ${SRC}
    COMMAND gen ${FILES} -o ${SRC})

add_custom_target(GENSRC
    DEPENDS ${SRC})

add_library(OBJS OBJECT ${SRC})

add_dependencies(OBJS GENSRC)

Upvotes: 3

Related Questions