如何从CMake中的函数早期返回,如下例所示?
function do_the_thing(HAS_PROPERTY_A)
# don't do things that have property A when property A is disabled globally
if (PROPERTY_A_DISABLED AND HAS_PROPERTY_A)
# What do you put here to return?
endif()
# do things and implement magic
endfunction()
Run Code Online (Sandbox Code Playgroud)
您可以使用return()(此处为 CMake手册页),如果在函数中调用,则从函数返回.
例如:
cmake_minimum_required(VERSION 3.0)
project(returntest)
# note: your function syntax was wrong - the function name goes
# after the parenthesis
function (do_the_thing HAS_PROPERTY_A)
if (HAS_PROPERTY_A)
message(STATUS "Early Return")
return()
endif()
message(STATUS "Later Return")
endfunction()
do_the_thing(TRUE)
do_the_thing(FALSE)
Run Code Online (Sandbox Code Playgroud)
结果是:
$ cmake ../returntest
-- Early Return
-- Later Return
-- Configuring done
...
Run Code Online (Sandbox Code Playgroud)
它也可以在函数外部工作:如果你从include()ed文件中调用它,它会返回到includer,如果你从文件中调用add_subdirectory()它,它会返回到父文件.