Build failure in Linux when passing flags in add_custom_target

Having done some investigations to narrow down the issue, this is a simplified form of this earlier question.

Essentially I am creating a custom target where the command line requires some flags to be passed using the “-D” command-line option.

[For what it’s worth, the script that I’m running was generated using “configure_file”, but the context does not allow me to place the flags directly into the script at this point - hence I still need the command-line option.]

So my add_custom_target invocation looks like this:

add_custom_target(bisonparse ALL
                                           -P /build/path/GENERATED/bisonparse.cmake

In Windows, this works perfectly.

In Linux however, the generated makefile fails to build. I get a peculiar error message, namely:

CMake Warning:
  No source or binary directory provided.  Both will be assumed to be the
  same as the current working directory, but note that this warning will
  become a fatal error in future CMake releases.

CMake Error: The source directory "/build/path" does not appear to contain CMakeLists.txt.

I can easily get rid of the build failure by commenting-out the two “-D” options, viz.

add_custom_target(bisonparse ALL
                                           -P /build/path/GENERATED/bisonparse.cmake

…but then of course I don’t get the flags that I need.

Any problems I have when using CMake are usually connected with when and where to use quotations, so I’ve tried


…and no quotes at all, and I find that these build successfully but no flags actually get passed.

What should I do?

First, this should be a custom command, not a target unless you want to tun that everytime.
Second, you probably need the VERBATIM flag.

1 Like

You may find the Quoting In CMake blog article I write a while back to be helpful.

Thank you: the VERBATIM flag was the missing piece.

For what it’s worth, I’m using a custom target rather than a custom command because the situation described in the documentation for add_custom_command is applicable here:

Do not list the output in more than one independent target that may build in parallel or the instances of the rule may conflict. Instead, use the add_custom_target() command to drive the command and make the other targets depend on that one.

In my case, I have some 30 targets that are entirely independent of one another but which all depend on this one.

Anyway, once again, thank you.

Thank you; that’s very helpful.

This actually means that that you need a custom target that depends on at least one output of that custom command. Then other targets that need those files need a dependency on this target.