如何在EXECUTE_PROCESS中使用FILE(GLOB_RECURSE)的结果?

时间:2019-08-25 01:12:07

标签: c++ cmake

我目前在CMake上精神崩溃。让我先向您展示我的工作:

FILE( GLOB_RECURSE FLATBUFFER_SRC "*.fbs" )
STRING( REPLACE ";" " " FLATBUFFER_SRC_STR "${FLATBUFFER_SRC}" )

SET( FLATC_COMMAND "flatc --cpp --gen-mutable -o ${META_INSTALL_LOCATION} ${FLATBUFFER_SRC_STR}" )
MESSAGE( STATUS "${FLATC_COMMAND}" )

EXECUTE_PROCESS(
        COMMAND ${FLATC_COMMAND}
        RESULT_VARIABLE OUT
)

IF ( NOT ${OUT} EQUAL "0" )
    MESSAGE( STATUS "COMMAND EXIT WITH: ${OUT}" )
    MESSAGE( STATUS "META_INSTALL_LOCATION: ${META_INSTALL_LOCATION}" )
    MESSAGE( STATUS "FLATBUFFER_SRC_STR: ${FLATBUFFER_SRC_STR}" )
    MESSAGE( FATAL_ERROR "flatc has FAILED gracefully" )
ENDIF ()

这是输出:

-- flatc --cpp --gen-mutable -o /home/black/Work/hubris/meta /home/black/Work/hubris/cmake-build-debug/META-prefix/src/META/fbs/game/base.fbs /home/black/Work/hubris/cmake-build-debug/META-prefix/src/META/fbs/game/util/vec.fbs
-- Configuring done
-- COMMAND EXIT WITH: No such file or directory
-- META_INSTALL_LOCATION: /home/black/Work/hubris/meta
-- FLATBUFFER_SRC_STR: /home/black/Work/hubris/cmake-build-debug/META-prefix/src/META/fbs/game/base.fbs /home/black/Work/hubris/cmake-build-debug/META-prefix/src/META/fbs/game/util/vec.fbs
CMake Error at CMakeLists.txt:20 (MESSAGE):
  flatc has FAILED gracefully

我的问题是“为什么不起作用?”而且它将保持不变,我希望了解CMake的方式。当我在shell打印命令中运行时:

flatc --cpp --gen-mutable -o /home/black/Work/hubris/meta /home/black/Work/hubris/cmake-build-debug/META-prefix/src/META/fbs/game/base.fbs /home/black/Work/hubris/cmake-build-debug/META-prefix/src/META/fbs/game/util/vec.fbs

它像梦一样运作。我真的很困惑,浪费了很多时间。 谁能向我解释,我可怕做错了什么?

1 个答案:

答案 0 :(得分:2)

FILE(GLOB_RECURSE)的结果是一个列表,当您为COMMAND指定参数时,CMake恰好期望一个列表。因此,只需将此列表传递到execute_process,无需进行任何修改并且不带引号

FILE( GLOB_RECURSE FLATBUFFER_SRC "*.fbs" )

SET( FLATC_COMMAND flatc --cpp --gen-mutable -o ${META_INSTALL_LOCATION} ${FLATBUFFER_SRC} )

EXECUTE_PROCESS(
        COMMAND ${FLATC_COMMAND}
        RESULT_VARIABLE OUT
)

或通过删除中间FLATC_COMMAND变量:

FILE( GLOB_RECURSE FLATBUFFER_SRC "*.fbs" )

EXECUTE_PROCESS(
        COMMAND flatc --cpp --gen-mutable -o ${META_INSTALL_LOCATION} ${FLATBUFFER_SRC}
        RESULT_VARIABLE OUT
)