<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Bad logic around CMAKE_OSX_SYSROOT and CMAKE_OSX_ARCHITECTURES"
   href="https://llvm.org/bugs/show_bug.cgi?id=28831">28831</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Bad logic around CMAKE_OSX_SYSROOT and CMAKE_OSX_ARCHITECTURES
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libc++
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>3.9
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>All Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>david@bamsoftware.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, mclow.lists@gmail.com
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=16870" name="attach_16870" title="Fixes CMake logic around some Mac variables.">attachment 16870</a> <a href="attachment.cgi?id=16870&action=edit" title="Fixes CMake logic around some Mac variables.">[details]</a></span>
Fixes CMake logic around some Mac variables.

I'm using CMake v2.8.12.2 and building libcxx like this:
    cd libcxx
    mkdir build
    cd build
    cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE:STRING=Release
-DCMAKE_C_FLAGS="-target x86_64-apple-darwin10" -DCMAKE_CXX_FLAGS="-target
x86_64-apple-darwin10" -DCMAKE_SYSTEM_NAME=Darwin
-DCMAKE_OSX_SYSROOT=/home/build/MacOSX10.7.sdk -DLLVM_PATH=$HOME/llvm ..
I'm cross compiling from GNU/Linux (Debian) to x86_64-apple-darwin10.

There is a bug in lib/CMakeLists.txt's handling of CMAKE_OSX_SYSROOT.

lib/CMakeLists.txt has this code that doesn't do what it is apparently
trying to do (I added comments for branch labels):

    if ( ${CMAKE_OSX_SYSROOT} )
      # Branch A
      list(FIND ${CMAKE_OSX_ARCHITECTURES} "armv7" OSX_HAS_ARMV7)
      if (OSX_HAS_ARMV7)
        # Branch C
        set(OSX_RE_EXPORT_LINE
          "${CMAKE_OSX_SYSROOT}/usr/lib/libc++abi.dylib"
         
"-Wl,-reexported_symbols_list,${CMAKE_CURRENT_SOURCE_DIR}/libc++sjlj-abi.exp")
      else()
        # Branch D
        set(OSX_RE_EXPORT_LINE
          "-Wl,-reexport_library,${CMAKE_OSX_SYSROOT}/usr/lib/libc++abi.dylib")
      endif()
    else()
      # Branch B
      set(OSX_RE_EXPORT_LINE "/usr/lib/libc++abi.dylib
-Wl,-reexported_symbols_list,${CMAKE_CURRENT_SOURCE_DIR}/libc++abi${LIBCXX_LIBCPPABI_VERSION}.exp")
    endif()

By setting CMAKE_OSX_SYSROOT and leaving CMAKE_OSX_ARCHITECTURES blank,
I expected Branch D to be run. Instead, Branch B is always run.

The first bug is in the syntax of the toplevel "if", which causes it to
be false even if CMAKE_OSX_SYSROOT is defined. According to the CMake
documentation,
    <a href="https://cmake.org/cmake/help/v3.3/command/if.html#command:if">https://cmake.org/cmake/help/v3.3/command/if.html#command:if</a>
    "The if command was written very early in CMake’s history,
    predating the ${} variable evaluation syntax, and for
    convenience evaluates variables named by its arguments as shown
    in the above signatures. Note that normal variable evaluation
    with ${} applies before the if command even receives the
    arguments."
The way the condition is written is causing CMAKE_OSX_SYSROOT to be
dereferenced twice instead of once. It should instead be:
    if ( CMAKE_OSX_SYSROOT )
or perhaps better:
    if ( DEFINED CMAKE_OSX_SYSROOT )

After the "if", there is another bug. This code appears to be checking
for "armv7" in CMAKE_OSX_ARCHITECTURES:
    list(FIND ${CMAKE_OSX_ARCHITECTURES} "armv7" OSX_HAS_ARMV7)
    if (OSX_HAS_ARMV7)
The code treats OSX_HAS_ARMV7 as if it were a boolean, but according to
the documentation, it is an index where -1 means not found:
    <a href="https://cmake.org/cmake/help/v3.3/command/list.html#command:list">https://cmake.org/cmake/help/v3.3/command/list.html#command:list</a>
    "FIND will return the index of the element specified in the list
    or -1 if it wasn’t found."
-1 is treated as a true value, so the condition is true except when
"armv7" is the zeroth element of CMAKE_OSX_ARCHITECTURES. According to
<a href="http://www.vtk.org/Wiki/CMake/Examples#Check_if_a_list_contains_a_value">http://www.vtk.org/Wiki/CMake/Examples#Check_if_a_list_contains_a_value</a>,
the way to check for list membership is:
    list(FIND "${CMAKE_OSX_ARCHITECTURES}" "armv7" OSX_HAS_ARMV7)
    if (NOT OSX_HAS_ARMV7 EQUAL -1)

I don't know much about CMake, but the attached patch was sufficient to
get Branch D to execute for me and get the SDK path into
OSX_RE_EXPORT_LINE.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>