我使用CMake從Linux交叉編譯爲OSX。要做到這一點,我使用的工具鏈文件,因此調用是這樣的:將交叉編譯的SDK設置爲CMake中的OSX
cmake -G 'Unix Makefiles' -DCMAKE_TOOLCHAIN_FILE=./cmake/toolchains/c.apple.universal.cmake .
工具鏈文件看起來是這樣的:
SET(CMAKE_SYSTEM_NAME Darwin)
SET(CMAKE_SYSTEM_PROCESSOR universal)
# set compilers...
INCLUDE("${CMAKE_CURRENT_LIST_DIR}/c.apple.common.cmake")
而另外一個:
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)
這迄今爲止工作,但不知何故CMake在某個時候重置CMAKE_OSX_SYSROOT。我得到的輸出是:
-- 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
最後3行來自CMakeList.txt。使用了正確的工具鏈文件(它只是一條消息),SYSTEM_NAME和SYSTEM_PROCESSOR設置正確,但OSX SDK錯誤。相應的CMake代碼來自一個宏,如果之前沒有設置CMAKE_OSX_ARCHITECTURES(這裏就是這種情況),它就會設置它。該行是:
MESSAGE(STATUS "Checking ${CMAKE_OSX_SYSROOT}/usr/lib/libSystem.B.dylib for possible architectures")
我在這裏錯誤地使用CMake?爲什麼CMake重置OSX_SYSROOT?根據http://www.cmake.org/cmake/help/v3.0/variable/CMAKE_OSX_SYSROOT.html它應該也會影響FIND *命令,但我需要設置CMAKE_FIND_ROOT_PATH才能工作。
奇怪的是,它昨天工作。清理整個構建目錄(我確信我昨天也做過)並重新運行CMake,現在重置CMAKE_OSX_SYSROOT。
如果這有所作爲:在宏的包含結束執行之前調用PROJECT,該宏顯示錯誤的sysroot。