add_custom_command中的CMake命令无法运行

时间:2020-06-04 16:15:51

标签: linux cmake gnu-make add-custom-command

作为基于CMake的程序的配置步骤的一部分,我将程序下载到CMAKE_BINARY_DIR的子文件夹中,然后将其作为add_custom_command()的一部分进行调用,以在生成时生成一些源文件。

但是,当运行make来构建项目时,进入自定义构建步骤时总是会收到此错误消息:

: 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

使用make标志重新运行VERBOSE=1以使其打印生成命令,我得到以下输出:

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

其中工具的路径是CMAKE_BINARY_DIR

下的绝对路径

将完整的绝对路径复制到该工具并将其直接粘贴到我的终端,可以使程序启动没有问题。

在这一点上,我对如何使CMake运行自定义构建阶段感到困惑。有什么建议吗?

编辑:我的实际add_custom_command调用如下:

# 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})

0 个答案:

没有答案
相关问题