2016-01-25 94 views
0

比方說,我有一個簡單的項目是這樣的:CMake的 - 檢查的CMakeLists.txt子目錄中成功構建

main 
|-- CMakeLists.txt 
|-- sub1 
|------ CMakeLists.txt 
|------ sub1.cpp 
|------ sub1.h 
|-- sub2 
|------ CMakeLists.txt 
|------ sub2.cpp 
|------ sub2.h 
main/CMakeLists.txt

然後,我有

add_subdirectory(sub1) # Build an executable file called exec1 
add_subdirectory(sub2) # Build an executable file called exec2 
add_custom_target(exec_all DEPENDS exec1 exec2) 

有沒有一種方法可以讓當exec1exec2尚未成功構建時,make exec_all是否運行?爲了更清楚,我希望能夠做到make exec_all但如果說,make exec1失敗,它仍然可以運行,打印出消息說

ERROR: exec1 was not successfully built. exec_all will only run exec2 

在當前CMake這可能還是一個相當遠伸?

+0

到目前爲止,一個我曾嘗試創建一個虛擬的'exec1'和'exec2',但這無助於檢查構建是否失敗。 –

+0

好吧,我甚至不知道在哪些地方花費不斷變化的東西后,我應該在哪裏改變這種情況。我不是要求你做我的工作,我是要求指向我可以看的任何方向。 –

回答

2

如果您不希望依賴scrict,那麼您無需使用DEPENDS選項。只要創建一個執行所需要的行動腳本,並通過COMMAND選項傳遞:

run.sh

# Usage: run.sh target [target ...] 

for target in [email protected]; do 
    if make ${target}; then 
     bin/${target} # Expect executables under bin/ directory. 
    else 
     echo "ERROR: ${target} was not succeffully built. Do not run it" 
    fi 
done 

的CMakeLists.txt

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY bin) # Executables will be generated under bin/ 
add_subdirectory(sub1) # Build an executable file called exec1 
add_subdirectory(sub2) # Build an executable file called exec2 

add_custom_target(exec_all 
    COMMAND /bin/sh -c ${CMAKE_SOURCE_DIR}/run.sh exec1 exec2 
    WORKING_DIRECTORY ${CMAKE_BINARY_DIR} 
) 
+0

這看起來像我能找到的最好的!謝謝! –