Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one early return from a function in cmake?

Tags:

cmake

How does one early-return from a function in CMake, as in the following example?

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()
like image 790
Billy ONeal Avatar asked Dec 15 '16 05:12

Billy ONeal


1 Answers

You use return() (CMake manual page here), which returns from a function if called when in a function.

For example:

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)

Results in:

$ cmake ../returntest
-- Early Return
-- Later Return
-- Configuring done
...

It works outside functions, too: If you call it from an include()ed file, it returns to the includer, if you call if from an file via add_subdirectory(), it'll return you to the parent file.

like image 76
Inductiveload Avatar answered Sep 30 '22 10:09

Inductiveload