add_custom_command converting my absolute executable path to relative?

As part of the configuration stage of my project, I create a small executable program somewhere in the CMAKE_BINARY_DIR tree. I’m then trying to use that program as part of an ‘add_custom_command’ call to generate some source files at build time.

I’m running into an issue where add_custom_command seems to be switching out my absolute path (obtained via find_program) with a relative path. Then the command fails to execute correctly as part of the custom command.

In my case, the generated program gets installed into ${CMAKE_BINARY_DIR}/generated/bin/ directory. I then add ${CMAKE_BINARY_DIR}/generated to my CMAKE_PREFIX_PATH

Then I use the generated program as follows:

find_program(PROGNAME progname)
message(STATUS "PROGNAME is: ${PROGNAME}") # prints the correct absolute path to the generated program
add_custom_command(
    OUTPUT my_output_file.generated.cpp
    COMMAND ${PROGNAME} my_output_file.template.cpp -o my_output_file.generated.cpp  
)

When trying to build the file generated by this rule, I get the following error on my console:

: program not found or is not executable
Please specify a program using absolute path or make sure the program is available in your PATH system variable

Re-running the build with VERBOSE=1 to get the command lines for the build steps, I see the following:

cd (cmake binary dir)/src && ../generated/bin/progname (program arguments)
: program not found or is not executable
Please specify a program using absolute path or make sure the program is available in your PATH system variable

I have checked and the program is in the correct location specified, but I still get the error. I also verified that the program does in fact load and run correctly.

At this point I’m trying to figure out why the program path in the makefile got changed from an absolute path to a relative path. I think it has something to do with the program being under the CMAKE_BINARY_DIR?

Is this a known issue? Is there any way to force CMake to use an absolute path here?

I’ve found that using the WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} clause in my add_custom_command call seems to force the makefile to use the full path to the generated executable.

However I’m still getting the “program not found or is not executable” issue, even though running the command that the Makefile is trying to run works without issue.

1 Like

I have no idea if this would help, but maybe add DEPENDS progname to your add_custom_command? Also possibly don’t try to resolve the path to progname?

adding the DEPENDS ${PROGNAME} to the add_custom_command did not appear to change anything.

In this case I have to resolve the full absolute path to the command since I want to always prefer the version of the executable that I download as part of the configuration step, and not any system-provided version if one is available.

I was thinking DEPENDS progname, not ${PROGNAME}. My thought is that CMake should know about it, if it built it. I was wondering if not using ${PROGNAME} at all might work?

So it turns out that my issue was not with CMake at all. The program I was trying to execute was the protobuf compiler protoc. I was passing an argument to generate gRPC bindings , but I was passing an empty string to the grpc cpp plugin, causing protoc itself to spit out that error message.
Fixing up that reference so it’s no longer an empty string seems to have resolved the issue.

Regardless, thanks for your help!