title: CMake - How does the if() command treat a symbol? As string or as variable?
body: <p>I am not sure the CMake <code>if()</code> command will treat a symbol in the condition clause as a <strong>variable</strong> or a <strong>string</strong> literal. So I did some experiments.</p>
  
  <p><strong>Script1.cmake</strong></p>
  
  <pre><code>cmake_minimum_required(VERSION 3.15)
  
  set(XXX "YYY") #&lt;========== HERE!!
  
  if(XXX STREQUAL "XXX")
      message("condition 1 is true") # If reach here, XXX is treated as string
  elseif(XXX STREQUAL "YYY")
      message("condition 2 is true") # If reach here, XXX is treated as variable
  endif()
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>condition 2 is true
  </code></pre>
  
  <p>So I come to below <strong>conclusion 1</strong>.</p>
  
  <p>For a symbol in the condition clause:</p>
  
  <ul>
  <li>If the symbol is <strong>defined</strong> as a variable before, CMake will treat it as variable and use its value for evaluation.</li>
  <li>If the symbol is <strong>not defined</strong> as a variable before, CMake will treat it literally as a string.</li>
  </ul>
  
  <p>Then I did another experiment.</p>
  
  <pre><code>set(ON "OFF")
  if(ON)
      message("condition 3 is true") # If reach here, ON is treated as a constant.
  else()
      message("condition 4 is true") # If reach here. ON is treated as a variable.
  endif()
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>condition 3 is true
  </code></pre>
  
  <p>So, though <code>ON</code> is <em>explicitly</em> defined as a variable, the <code>if</code> command still treat it as a constant of TRUE value. This directly contradicts to my previous <strong>conclusion 1</strong>.</p>
  
  <p>So how can I know for sure the CMake if() command will treat a symbol as string or variable??</p>
  
  <h2>ADD 1 - 11:04 AM 7/11/2019</h2>
  
  <p>It seems the <code>if(constant)</code> form precedes other forms of <code>if()</code> statement. (<a href="https://cmake.org/cmake/help/latest/command/if.html#condition-syntax" rel="nofollow noreferrer">src</a>)</p>
  
  <blockquote>
  <pre><code>if(&lt;constant&gt;)
  </code></pre>
    
    <p>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. Named boolean constants
    are case-insensitive. <strong>If the argument is not one of these specific
    constants, it is treated as a variable or string and the following
    signature is used.</strong></p>
  </blockquote>
  
  <p>So for now, I have to refer to the above rule first before applying my <strong>conclusion 1</strong>.
  (This may be an answer, but I am not sure enough yet.)</p>