简体   繁体   中英

CMake IF condition returns wrong result

I am trying to build native library for android and I need to perform conditional compilation. So I have following code in my CMakeLists.txt file

#set (TEST_VAL OFF) #uncomment this to force set this value
option(TEST_VAL "Test my value")
if(NOT ${TEST_VAL} EQUAL OFF)
    message(WARNING "TEST MY VAL IS NOT EQUAL OFF ACTUAL VALUE: ${DISABLE_SSE} ")
else ()
    message(WARNING "TEST MY VAL IS ON ACTUAL VALUE: ${DISABLE_SSE} ")
endif()

As you see I am trying to get option value and compile the library.

This is how I provide this option (build.gradle) file.

android {
   ...
    defaultConfig {
        ...
        externalNativeBuild {
            cmake {
                // Passes optional arguments to CMake.
                arguments "-DTEST_VAL=OFF" 
            }
        }

   ...
}

But in my output I see only:

CMake Warning at CMakeLists.txt:35 (message): TEST MY VAL IS NOT EQUAL OFF ACTUAL VALUE: OFF

So how It could that with OFF value it is not equal to OFF? What I am I doing wrong.

Thank you very much.

The EQUAL operator is "true if the given string or variable's value is a valid number and equal to that on the right."

So just do if (NOT TEST_VAL) because just giving the variable's name is "true if the constant is 1, ON, YES, TRUE, Y, or a non-zero number."

If you can't change the code or raise an issue ...

I've run some tests and the following does work:

set(TEST_VAL OFF)
set(OFF 0)

if(NOT ${TEST_VAL} EQUAL OFF)
    message(STATUS "NOT \${TEST_VAL} EQUAL OFF")
endif()

Since EQUAL needs a number for comparison you can define the string OFF to 0 .

So in your examples that would be equivalent to arguments "-DTEST_VAL=OFF -DOFF=0" .

Reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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