Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why CMake option command should be ON or OFF?

Tags:

cmake

According to the cmake documentation:

Provides an option for the user to select as ON or OFF

However, I noticed that the command works with any other values.

My test script:

cmake_minimum_required (VERSION 2.6)

option(MY_VAR "var description" OFF)

message("MY_VAR=${MY_VAR}")
if(MY_VAR)
    message("MY_VAR is not OFF/FALSE/0")
endif()

Results:

>cmake ..
or
>cmake -DMY_VAR=OFF ..

output:
MY_VAR=OFF
>cmake -DMY_VAR=ON ..
output:
MY_VAR=ON
MY_VAR is not OFF/FALSE/0
>cmake -DMY_VAR=FALSE ..
output:
MY_VAR=FALSE
>cmake -DMY_VAR=TRUE ..
output:
MY_VAR=TRUE
MY_VAR is not OFF/FALSE/0
>cmake -DMY_VAR=0 ..
output:
MY_VAR=0
>cmake -DMY_VAR=1 ..
output:
MY_VAR=1
MY_VAR is not OFF/FALSE/0
cmake -DMY_VAR=3 ..
output:
MY_VAR=3
MY_VAR is not OFF/FALSE/0
like image 240
Kuznetsov-M Avatar asked Sep 16 '25 21:09

Kuznetsov-M


1 Answers

Documentation for option command describes just a convention about possible values. By itself, the option does NOT check a value of the variable, so the variable could contain any string.

Actual interpretation of the variable is up to the CMakeLists.txt script.

Usually, options are checked with if(VAR) syntax, so they are interpreted according to the documentation for if command:

True if the constant is 1, ON, YES, TRUE, Y, or a non-zero number. False if the constant is 0, OFF, NO, FALSE, N, IGNORE, NOTFOUND, the empty string, or ends in the suffix -NOTFOUND.

As you can see, the if command allows several values for an option to be True-interpreted, and several values to be False-interpreted. (There is no reason to use values with -NOTFOUND suffix: they are intended to be produced only by a command like find_library.)

like image 161
Tsyvarev Avatar answered Sep 19 '25 16:09

Tsyvarev