![](/img/trans.png)
[英]Use CMake add_custom_command to generate source for another target
[英]Source doesn't exist yet for target depending on custom target using source generated by add_custom_command
我正在嘗試將介子項目移植到 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.
add_library()
命令不接受DEPENDS
關鍵字,因此這不是指定目標依賴項的有效方法。 嘗試使用add_dependencies()
來指定qtlayershell
目標依賴於wayland_protocols
自定義目標來生成源:
add_library(qtlayershell SHARED
${QTLAYERSHELL_SRC}
${WAYLAND_PROTOCOL_SRC}
)
add_dependencies(qtlayershell wayland_protocols)
此外, WAYLAND_PROTOCOL_SRC
和WAYLAND_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})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.