简体   繁体   English

目标的源尚不存在,具体取决于使用 add_custom_command 生成的源的自定义目标

[英]Source doesn't exist yet for target depending on custom target using source generated by add_custom_command

I am trying to port a meson project to CMake.我正在尝试将介子项目移植到 CMake。 I have the following subdirectories in my main CMake file:我的主 CMake 文件中有以下子目录:

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

and the following in protos/CMakeLists.txt :以及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})

and in qtlayershell/CMakeLists.txt , a target like this:qtlayershell/CMakeLists.txt中,目标如下:

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

But I am getting an error that somelongbuildpath/protos/wayland-protos/wayland-xdg-shell-protocol.c doesn't exist yet.但是我收到一个错误,即somelongbuildpath/protos/wayland-protos/wayland-xdg-shell-protocol.c尚不存在。 Here is full error:这是完整的错误:

  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.

The add_library() command does not accept a DEPENDS keyword, so this is not a valid way to specify a target dependency. add_library()命令不接受DEPENDS关键字,因此这不是指定目标依赖项的有效方法。 Try using add_dependencies() instead, to specify that the qtlayershell target depends on the wayland_protocols custom target for generation of the sources:尝试使用add_dependencies()来指定qtlayershell目标依赖于wayland_protocols自定义目标来生成源:

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

In addition, the WAYLAND_PROTOCOL_SRC and WAYLAND_PROTOCOL_HEADERS variables are set with PARENT_SCOPE so they are not available to the add_custom_target call.此外, WAYLAND_PROTOCOL_SRCWAYLAND_PROTOCOL_HEADERS变量设置为PARENT_SCOPE ,因此它们对add_custom_target调用不可用。 Instead, you can try setting these as CACHE INTERNAL variables so that they are available in all scopes:相反,您可以尝试将这些设置为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})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM