2

我正在尝试将介子项目移植到 CMake。我的主 CMake 文件中有以下子目录:

add_subdirectory(protos)
add_subdirectory(qtlayershell)
add_subdirectory(demo)

以及以下内容protos/CMakeLists.txt

set(PROTOCOLS
    ${WAYLAND_PROTOCOL_DIR}/stable/xdg-shell/xdg-shell.xml
    wlr-layer-shell-unstable-v1.xml'
)

foreach(XML ${PROTOCOLS})
    get_filename_component(BASENAME ${XML} NAME_WE)
    add_custom_command(
        OUTPUT  ${CMAKE_CURRENT_BINARY_DIR}/wayland-protos/wayland-${BASENAME}-protocol.c
        COMMAND wayland-scanner private-code ${XML} @OUTPUT@
    )
    add_custom_command(
        OUTPUT  ${CMAKE_CURRENT_BINARY_DIR}/wayland-protos/wayland-${BASENAME}-protocol.h
        COMMAND wayland-scanner client-header ${XML} @OUTPUT@
    )

    add_custom_command(
        OUTPUT  ${CMAKE_CURRENT_BINARY_DIR}/wayland-protos/qwayland-${BASENAME}.h
        COMMAND qtwaylandscanner client-header ${XML} @OUTPUT@
    )
    add_custom_command(
        OUTPUT  ${CMAKE_CURRENT_BINARY_DIR}/wayland-protos/${BASENAME}-protocol.cpp
        COMMAND qtwaylandscanner client-code ${XML} @OUTPUT@
    )

    list(APPEND PROTOCOL_SRC
        ${CMAKE_CURRENT_BINARY_DIR}/wayland-protos/wayland-${BASENAME}-protocol.c
        ${CMAKE_CURRENT_BINARY_DIR}/wayland-protos/${BASENAME}-protocol.cpp
    )

    list(APPEND PROTOCOL_HEADERS
        ${CMAKE_CURRENT_BINARY_DIR}/wayland-protos/qwayland-${BASENAME}.h
        ${CMAKE_CURRENT_BINARY_DIR}/wayland-protos/wayland-${BASENAME}-protocol.h
    )
endforeach()


set(WAYLAND_PROTOCOL_SRC ${PROTOCOL_SRC} PARENT_SCOPE)
set(WAYLAND_PROTOCOL_HEADERS ${PROTOCOL_HEADERS} PARENT_SCOPE)

add_custom_target(wayland_protocols DEPENDS ${WAYLAND_PROTOCOL_SRC} ${WAYLAND_PROTOCOL_HEADERS})

在 中qtlayershell/CMakeLists.txt,这样的目标:

add_library(qtlayershell SHARED
    ${QTLAYERSHELL_SRC}
    ${WAYLAND_PROTOCOL_SRC}
    DEPENDS wayland_protocols
)

但我收到一个somelongbuildpath/protos/wayland-protos/wayland-xdg-shell-protocol.c尚不存在的错误。这是完整的错误:

  Cannot find source file:

    /home/noone/KDE/my/build-qtlayershell-Desktop-Debug/protos/wayland-protos/wayland-xdg-shell-protocol.c

  Tried extensions .c .C .c++ .cc .cpp .cxx .cu .m .M .mm .h .hh .h++ .hm
  .hpp .hxx .in .txx


CMake Error at qtlayershell/CMakeLists.txt:9 (add_library):
  No SOURCES given to target: qtlayershell
CMake Generate step failed.  Build files cannot be regenerated correctly.
CMake process exited with exit code 1.
4

1 回答 1

0

add_library()命令不接受DEPENDS关键字,因此这不是指定目标依赖项的有效方法。尝试add_dependencies()改用,以指定qtlayershell目标依赖于wayland_protocols定义目标来生成源:

add_library(qtlayershell SHARED
    ${QTLAYERSHELL_SRC}
    ${WAYLAND_PROTOCOL_SRC}
)
add_dependencies(qtlayershell wayland_protocols)

此外,WAYLAND_PROTOCOL_SRCWAYLAND_PROTOCOL_HEADERS变量设置为,PARENT_SCOPE因此它们对add_custom_target调用不可用。相反,您可以尝试将它们设置为CACHE INTERNAL变量,以便它们在所有范围内都可用:

set(WAYLAND_PROTOCOL_SRC ${PROTOCOL_SRC} CACHE INTERNAL "Wayland sources" FORCE)
set(WAYLAND_PROTOCOL_HEADERS ${PROTOCOL_HEADERS} CACHE INTERNAL "Wayland headers" FORCE)

add_custom_target(wayland_protocols DEPENDS ${WAYLAND_PROTOCOL_SRC} ${WAYLAND_PROTOCOL_HEADERS})
于 2020-05-14T14:41:10.553 回答