As part of the configuration step of my CMake-based program, I download a program into a subfolder of CMAKE_BINARY_DIR, which is then invoked as part of an add_custom_command() to generate some source files at build time.
However, when running make
to build the project, I always get this error message when getting to the custom build step:
: 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 make
with the VERBOSE=1
flag to make it print build commands, I get the following output:
cd (directory) && (cmake_binary_dir)/path/to/tool (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
where the path to the tool is an absolute path under the CMAKE_BINARY_DIR
Copying the full absolute path to the tool and pasting it directly into my terminal makes the program start up without issue.
I'm at a loss at this point as to how to get CMake to run my custom build stage. Any advice?
EDIT: My actual add_custom_command invocation is the following:
# at this point, PROTOC_COMPILER holds the path to protoc (inside the cmake build directory)
# PROTOBUF_FILES is the collection of .proto files I want to generate
message(STATUS "protoc path: ${PROTOC_COMPILER}")
foreach(PROTOFILE ${PROTOBUF_FILES})
get_filename_component(PROTOFILE_DIR ${PROTOFILE} DIRECTORY)
get_filename_component(PROTOFILE_NAME ${PROTOFILE} NAME_WE) # name without extension
get_filename_component(PROTOFILE_ABSOLUTE_PATH ${PROTOFILE} ABSOLUTE)
set(PROTOFILE_OUTPUT_HEADER_NAME "${FULL_OUTPUT_DIR}/${PROTOFILE_DIR}/${PROTOFILE_NAME}.pb.h")
set(PROTOFILE_OUTPUT_SRC_NAME "${FULL_OUTPUT_DIR}/${PROTOFILE_DIR}/${PROTOFILE_NAME}.pb.cc")
add_custom_command(
OUTPUT "${PROTOFILE_OUTPUT_HEADER_NAME}" "${PROTOFILE_OUTPUT_SRC_NAME}"
COMMENT "Compiling Protobuf file: ${PROTOFILE}"
# make sure the output directory exists.
COMMAND ${CMAKE_COMMAND} -E make_directory "${FULL_OUTPUT_DIR}/${PROTOFILE_DIR}"
# actually generate the files. This is the command that fails.
COMMAND ${PROTOC_COMPILER}
--cpp_out="${FULL_OUTPUT_DIR}" # where to put the output files.
# make sure we can reference includes.
# directory the $PROTOFILE is in. Otherwise protoc complains about it.
-I${CMAKE_CURRENT_SOURCE_DIR}
--grpc_out="${FULL_OUTPUT_DIR}"
--plugin=protoc-gen-grpc="${GRPC_CPP_PLUGIN}"
"${PROTOFILE_ABSOLUTE_PATH}" # the file to build
DEPENDS "${PROTOFILE_ABSOLUTE_PATH}"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
list(APPEND GENERATED_HEADERS ${PROTOFILE_OUTPUT_HEADER_NAME})
list(APPEND GENERATED_SOURCES ${PROTOFILE_OUTPUT_SRC_NAME})
endforeach()
add_library(my-project-protomessages STATIC ${GENERATED_SOURCES} ${GENERATED_HEADERS})
target_include_directories(my-project-protomessages PUBLIC ${FULL_OUTPUT_DIR})