title: Setting SDK on cross-compilation to OSX in CMake
body: <p>I'm cross compiling from Linux to OSX using CMake. To do this I use a toolchain file, so the call is like this:</p>
  
  <pre><code>cmake -G 'Unix Makefiles' -DCMAKE_TOOLCHAIN_FILE=./cmake/toolchains/c.apple.universal.cmake .
  </code></pre>
  
  <p>The toolchain file looks like this:</p>
  
  <pre><code>SET(CMAKE_SYSTEM_NAME Darwin)
  SET(CMAKE_SYSTEM_PROCESSOR universal)
  
  # set compilers...
  INCLUDE("${CMAKE_CURRENT_LIST_DIR}/c.apple.common.cmake")
  </code></pre>
  
  <p>And the other one:</p>
  
  <pre><code>INCLUDE("${CMAKE_CURRENT_LIST_DIR}/../Modules/CMakeMacroSetCCache.cmake")
  
  # specify the cross compiler
  SET_CCACHE(CMAKE_C_COMPILER i686-apple-darwin10-gcc)
  SET_CCACHE(CMAKE_CXX_COMPILER i686-apple-darwin10-g++)
  SET(CMAKE_RANLIB i686-apple-darwin10-ranlib CACHE STRING "" FORCE)
  SET(CMAKE_LIPO i686-apple-darwin10-lipo CACHE STRING "" FORCE)
  
  SET(OSX104_SDK "/usr/lib/apple/SDKs/MacOSX10.4.sdk")
  SET(OSX105_SDK "/usr/lib/apple/SDKs/MacOSX10.5.sdk")
  
  # set SDK
  SET(CMAKE_OSX_DEPLOYMENT_TARGET )
  IF(EXISTS ${OSX104_SDK})
      SET(CMAKE_OSX_SYSROOT ${OSX104_SDK})
  ELSEIF(EXISTS ${OSX105_SDK})
      SET(CMAKE_OSX_SYSROOT ${OSX105_SDK})
  ELSE()
      MESSAGE(FATAL_ERROR "No OSX SDK found!")
  ENDIF()
  MESSAGE(STATUS "Using OSX SDK at ${CMAKE_OSX_SYSROOT}")
  
  SET(CMAKE_PREFIX_PATH ${CMAKE_OSX_SYSROOT})
  SET(CMAKE_FIND_ROOT_PATH ${CMAKE_PREFIX_PATH})
  SET(BOOST_ROOT ${CMAKE_PREFIX_PATH})
  
  # search for programs in the build host directories
  SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
  
  # for libraries and headers in the target directories
  SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
  SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
  </code></pre>
  
  <p>This works so far, but somehow CMake resets CMAKE_OSX_SYSROOT at some point. The output I get is:</p>
  
  <pre><code>-- Set CMAKE_C_COMPILER to /usr/lib/ccache-lipo/i686-apple-darwin10-gcc
  -- Set CMAKE_CXX_COMPILER to /usr/lib/ccache-lipo/i686-apple-darwin10-g++
  -- Using OSX SDK at /usr/lib/apple/SDKs/MacOSX10.5.sdk
  -- Set CMAKE_C_COMPILER to /usr/lib/ccache-lipo/i686-apple-darwin10-gcc
  -- Set CMAKE_CXX_COMPILER to /usr/lib/ccache-lipo/i686-apple-darwin10-g++
  -- Using OSX SDK at /usr/lib/apple/SDKs/MacOSX10.5.sdk
  -- The C compiler identification is GNU
  -- The CXX compiler identification is GNU
  -- Checking whether C compiler has -isysroot
  -- Checking whether C compiler has -isysroot - yes
  -- Checking whether C compiler supports OSX deployment target flag
  -- Checking whether C compiler supports OSX deployment target flag - yes
  -- Check for working C compiler: /usr/lib/ccache-lipo/i686-apple-darwin10-gcc
  -- Check for working C compiler: /usr/lib/ccache-lipo/i686-apple-darwin10-gcc -- works
  -- Detecting C compiler ABI info
  -- Detecting C compiler ABI info - done
  -- Checking whether CXX compiler has -isysroot
  -- Checking whether CXX compiler has -isysroot - yes
  -- Checking whether CXX compiler supports OSX deployment target flag
  -- Checking whether CXX compiler supports OSX deployment target flag - yes
  -- Check for working CXX compiler: /usr/lib/ccache-lipo/i686-apple-darwin10-g++
  -- Check for working CXX compiler: /usr/lib/ccache-lipo/i686-apple-darwin10-g++ -- works
  -- Detecting CXX compiler ABI info
  -- Detecting CXX compiler ABI info - done
  -- Used Toolchain definition file '/srv/jenkins/.../cmake/toolchains/c.apple.universal.cmake'
  -- Configuring for cross-compiling to Darwin on universal
  -- Using platform config cmake/darwin.cmake
  -- Checking /Developer/SDKs/MacOSX.sdk/usr/lib/libSystem.B.dylib for possible architectures
  </code></pre>
  
  <p>The last 3 lines are from the CMakeList.txt. The correct toolchain file was used (it's just a message), SYSTEM_NAME and SYSTEM_PROCESSOR are set correctly but the OSX SDK is wrong. The corresponding CMake code comes from a macro that will set CMAKE_OSX_ARCHITECTURES if it wasn't set before (which is the case here). The line is:</p>
  
  <pre><code>MESSAGE(STATUS "Checking ${CMAKE_OSX_SYSROOT}/usr/lib/libSystem.B.dylib for possible architectures")
  </code></pre>
  
  <p>Am I using CMake wrongly here? Why is CMake resetting the OSX_SYSROOT? According to <a href="http://www.cmake.org/cmake/help/v3.0/variable/CMAKE_OSX_SYSROOT.html" rel="nofollow">http://www.cmake.org/cmake/help/v3.0/variable/CMAKE_OSX_SYSROOT.html</a> it should also affect FIND* commands but I needed to set CMAKE_FIND_ROOT_PATH for it to work.</p>
  
  <p>Strange enough, it worked yesterday. Cleaning the whole build-directory (which I'm sure I did yesterday too) and rerunning CMake resets CMAKE_OSX_SYSROOT now.</p>
  
  <p>If this makes a difference: PROJECT is called before the inclusion end execution of the macro, that shows the wrong sysroot.</p>