title: If and definedness of ENV variables
body: <p>I'm a little perplexed (note this is not about the use of <code>$</code> with <code>ENV</code> and <code>if</code>).</p>
  
  <pre><code>cmake_minimum_required(VERSION 3.14)
  set(CMAKE_CONFIGURATION_TYPES Debug)
  
  message("--&gt;$ENV{foobar}&lt;--")
  
  if (DEFINED ENV{foobar})
      message("$foobar is defined")
  else()
      message("$foobar is not defined")
  endif()
  </code></pre>
  
  <p>Run via <code>bash</code> and <code>cmake -DCMAKE_BUILD_TYPE=Debug</code> with no <code>$foobar</code> set, predictably the relevant output is:</p>
  
  <pre><code>--&gt;&lt;--
  $foobar is not defined
  </code></pre>
  
  <p>And subsequent to <code>export foobar=1</code>:</p>
  
  <pre><code>--&gt;1&lt;--
  $foobar is defined
  </code></pre>
  
  <p>But now:</p>
  
  <pre><code>&gt; export foobar=
  </code></pre>
  
  <p>I realize this does leave the variable in the environment (vs. totally removing it with, eg., the bash built-in <code>unset</code>), but it is with a value that would be conventionally considered by many or most users as "undefined".  Unfortunately, <code>cmake</code> doesn't see it that way, however:</p>
  
  <pre><code>&gt; export foobar=
  &gt; cmake -DCMAKE_BUILD_TYPE=Debug
  --&gt;&lt;--
  $foobar is defined
  </code></pre>
  
  <p>While I can remember this, there is no way I am going to presume normal users building my project are.  So that means no more <code>if (DEFINED ENV{foobar})</code>.  </p>
  
  <p>Fortunately there are simple work-arounds to this, such as testing via a proxy variable:</p>
  
  <pre><code>set(Foobar $ENV{foobar})
  message("==&gt;${Foobar}&lt;==")
  if (DEFINED Foobar)
      message("Internal Foobar defined")
  else()
      message("Internal foobar is not defined")
  endif()         
  </code></pre>
  
  <p>The output is now</p>
  
  <pre><code>--&gt;&lt;--
  $foobar is defined
  ==&gt;&lt;==
  Internal foobar is not defined
  </code></pre>
  
  <p>To be fair to cmake, this is not inconsistent -- the internal foobar would be defined as an empty string too if <code>$ENV{foobar}</code> were quoted in the assignment.</p>
  
  <p>My question is just to verify that I have not missed something.</p>